Skip to content

Commit

Permalink
Merge pull request #16 from tscircuit/fix/convert-circuitjson-to-dsn-…
Browse files Browse the repository at this point in the history
…file

fix: convert circuit json to dsn file
  • Loading branch information
imrishabh18 authored Nov 14, 2024
2 parents 53ffe64 + 3b79481 commit e6591d9
Show file tree
Hide file tree
Showing 5 changed files with 641 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,20 @@ import type { AnyCircuitElement } from "circuit-json"
import type { DsnPcb, Padstack, ComponentGroup } from "../types"
import { processComponentsAndPads } from "./process-components-and-pads"
import { processNets } from "./process-nets"
import { processPcbTraces } from "./process-pcb-traces"

export function convertCircuitJsonToDsnJson(
circuitElements: AnyCircuitElement[],
): DsnPcb {
// Find the PCB board element
const pcbBoard = circuitElements.find(
(element) => element.type === "pcb_board",
) as AnyCircuitElement & {
width: number
height: number
center: { x: number; y: number }
}

const pcb: DsnPcb = {
filename: "",
parser: {
Expand Down Expand Up @@ -40,10 +50,7 @@ export function convertCircuitJsonToDsnJson(
path: {
layer: "pcb",
width: 0,
coordinates: [
158000, -108000, 147500, -108000, 147500, -102000, 158000, -102000,
158000, -108000,
],
coordinates: calculateBoardBoundary(pcbBoard),
},
},
via: "Via[0-1]_600:300_um",
Expand Down Expand Up @@ -118,10 +125,38 @@ export function convertCircuitJsonToDsnJson(
const componentGroups = groupComponents(circuitElements)
processComponentsAndPads(componentGroups, circuitElements, pcb)
processNets(circuitElements, pcb)
processPcbTraces(circuitElements, pcb)

return pcb
}

function calculateBoardBoundary(pcbBoard: {
width: number
height: number
center: { x: number; y: number }
}): number[] {
// Convert dimensions from mm to μm and calculate corners
const halfWidth = (pcbBoard.width * 1000) / 2
const halfHeight = (pcbBoard.height * 1000) / 2
const centerX = pcbBoard.center.x * 1000
const centerY = pcbBoard.center.y * 1000

// Return coordinates for a rectangular boundary path
// Format: [x1, y1, x2, y2, x3, y3, x4, y4, x1, y1] to close the path
return [
centerX - halfWidth,
centerY - halfHeight, // Top left
centerX + halfWidth,
centerY - halfHeight, // Top right
centerX + halfWidth,
centerY + halfHeight, // Bottom right
centerX - halfWidth,
centerY + halfHeight, // Bottom left
centerX - halfWidth,
centerY - halfHeight, // Back to top left to close the path
]
}

function groupComponents(
circuitElements: AnyCircuitElement[],
): ComponentGroup[] {
Expand Down
41 changes: 41 additions & 0 deletions lib/dsn-pcb/circuit-json-to-dsn-json/process-pcb-traces.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import type { AnyCircuitElement } from "circuit-json"
import type { DsnPcb } from "../types"

export function processPcbTraces(
circuitElements: AnyCircuitElement[],
pcb: DsnPcb,
) {
for (const element of circuitElements) {
if (element.type === "pcb_trace") {
const pcbTrace = element

const netName =
pcbTrace.source_trace_id || `Net-${pcb.network.nets.length + 1}`

const wire = {
path: {
layer:
pcbTrace.route[0].route_type === "wire"
? pcbTrace.route[0].layer === "top"
? "F.Cu"
: "B.Cu"
: "F.Cu", // Default to F.Cu if not a wire route
width:
pcbTrace.route[0].route_type === "wire"
? pcbTrace.route[0].width * 1000
: 200, // Convert mm to um, or use a default value
coordinates: [] as number[],
},
net: netName,
type: "route",
}

for (const point of pcbTrace.route) {
wire.path.coordinates.push(point.x * 1000) // Convert mm to um
wire.path.coordinates.push(-point.y * 1000) // Negate Y to match DSN coordinate system
}

pcb.wiring.wires.push(wire)
}
}
}
Loading

0 comments on commit e6591d9

Please sign in to comment.