-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
221 additions
and
153 deletions.
There are no files selected for viewing
149 changes: 0 additions & 149 deletions
149
lib/dsn-pcb/circuit-json-to-dsn-json/process-pcb-traces.ts
This file was deleted.
Oops, something went wrong.
41 changes: 41 additions & 0 deletions
41
lib/dsn-pcb/circuit-json-to-dsn-json/process-pcb-traces/findOrCreateViaPadstack.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
import type { DsnPcb, Padstack } from "lib/dsn-pcb/types" | ||
|
||
export function findOrCreateViaPadstack( | ||
pcb: DsnPcb, | ||
outerDiameter: number, | ||
holeDiameter: number, | ||
): string { | ||
const viaName = `Via[0-1]_${outerDiameter}:${holeDiameter}_um` | ||
|
||
// Check if padstack already exists | ||
const existingPadstack = pcb.library.padstacks.find((p) => p.name === viaName) | ||
|
||
if (existingPadstack) { | ||
return viaName | ||
} | ||
|
||
// Create new padstack for via | ||
const viaPadstack: Padstack = { | ||
name: viaName, | ||
attach: "off", | ||
shapes: [ | ||
{ | ||
shapeType: "circle", | ||
layer: "F.Cu", | ||
diameter: outerDiameter, | ||
}, | ||
{ | ||
shapeType: "circle", | ||
layer: "B.Cu", | ||
diameter: outerDiameter, | ||
}, | ||
], | ||
hole: { | ||
shape: "circle", | ||
diameter: holeDiameter, | ||
}, | ||
} | ||
|
||
pcb.library.padstacks.push(viaPadstack) | ||
return viaName | ||
} |
155 changes: 155 additions & 0 deletions
155
lib/dsn-pcb/circuit-json-to-dsn-json/process-pcb-traces/index.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,155 @@ | ||
import type { | ||
AnyCircuitElement, | ||
LayerRef, | ||
PcbTrace, | ||
PcbTraceRoutePoint, | ||
} from "circuit-json" | ||
import type { DsnPcb } from "../../types" | ||
import Debug from "debug" | ||
import { findOrCreateViaPadstack } from "./findOrCreateViaPadstack" | ||
|
||
const debug = Debug("dsn-converter:process-pcb-traces") | ||
|
||
const DEFAULT_VIA_DIAMETER = 600 // μm | ||
const DEFAULT_VIA_HOLE = 300 // μm | ||
|
||
interface Wire { | ||
path: { | ||
layer: string | ||
width: number | ||
coordinates: number[] | ||
} | ||
net: string | ||
type: string | ||
} | ||
|
||
function createWire(opts: { | ||
layer: LayerRef | ||
widthMm: number | ||
netName: string | ||
}): Wire { | ||
return { | ||
path: { | ||
layer: opts.layer === "top" ? "F.Cu" : "B.Cu", | ||
width: opts.widthMm * 1000, | ||
coordinates: [], | ||
}, | ||
net: opts.netName, | ||
type: "route", | ||
} | ||
} | ||
|
||
export function processPcbTraces( | ||
circuitElements: AnyCircuitElement[], | ||
pcb: DsnPcb, | ||
) { | ||
for (const element of circuitElements) { | ||
if (element.type === "pcb_trace") { | ||
const pcbTrace = element | ||
debug("PCB TRACE\n----------\n", pcbTrace) | ||
const netName = | ||
pcbTrace.source_trace_id || `Net-${pcb.network.nets.length + 1}` | ||
|
||
let currentLayer = "" | ||
let currentWire: Wire | null = null | ||
|
||
// Process each point in the route | ||
for (let i = 0; i < pcbTrace.route.length; i++) { | ||
const point = pcbTrace.route[i] as PcbTraceRoutePoint | ||
debug("POINT\n------\n", point) | ||
|
||
if (point.route_type === "wire") { | ||
// If layer changed or this is the first point, start a new wire | ||
const hasLayerChanged = currentLayer && point.layer !== currentLayer | ||
const isFirstPoint = !currentWire | ||
|
||
if (isFirstPoint) { | ||
// Start new wire on new layer | ||
currentWire = createWire({ | ||
layer: point.layer, | ||
widthMm: point.width, | ||
netName, | ||
}) | ||
|
||
pcb.wiring.wires.push(currentWire) | ||
currentLayer = point.layer | ||
} | ||
|
||
if (currentWire && !hasLayerChanged) { | ||
// Add coordinates to current wire | ||
currentWire.path.coordinates.push(point.x * 1000) | ||
currentWire.path.coordinates.push(point.y * 1000) | ||
continue | ||
} | ||
|
||
if (hasLayerChanged) { | ||
const prevPoint = pcbTrace.route[i - 1] | ||
const viaPadstackName = findOrCreateViaPadstack( | ||
pcb, | ||
DEFAULT_VIA_DIAMETER, | ||
DEFAULT_VIA_HOLE, | ||
) | ||
|
||
// Add via reference to structure if not already there | ||
if (!pcb.structure.via) { | ||
pcb.structure.via = viaPadstackName | ||
} | ||
|
||
// Create wire segment for via placement | ||
pcb.wiring.wires.push({ | ||
path: { | ||
layer: currentLayer === "top" ? "F.Cu" : "B.Cu", | ||
width: DEFAULT_VIA_DIAMETER, | ||
coordinates: [prevPoint.x * 1000, prevPoint.y * 1000], | ||
}, | ||
net: netName, | ||
type: "via", | ||
}) | ||
} | ||
continue | ||
} | ||
|
||
if (point.route_type === "via") { | ||
debug("VIA\n----\n", point) | ||
|
||
// End current wire | ||
if (currentWire) { | ||
currentWire.path.coordinates.push(point.x * 1000) | ||
currentWire.path.coordinates.push(point.y * 1000) | ||
currentWire = null | ||
} | ||
|
||
// Handle explicit via points | ||
const viaPadstackName = findOrCreateViaPadstack( | ||
pcb, | ||
DEFAULT_VIA_DIAMETER, | ||
DEFAULT_VIA_HOLE, | ||
) | ||
|
||
debug("VIA PADSTACK NAME:", viaPadstackName) | ||
|
||
// Add via reference to structure if not already there | ||
if (!pcb.structure.via) { | ||
pcb.structure.via = viaPadstackName | ||
} | ||
|
||
// Create wire segment for via placement | ||
pcb.wiring.wires.push({ | ||
path: { | ||
layer: point.from_layer === "top" ? "F.Cu" : "B.Cu", | ||
width: DEFAULT_VIA_DIAMETER, | ||
coordinates: [point.x * 1000, point.y * 1000], | ||
}, | ||
net: netName, | ||
type: "via", | ||
}) | ||
debug("WIRING", pcb.wiring) | ||
|
||
currentLayer = point.to_layer | ||
currentWire = null // Start fresh wire after via | ||
} | ||
} | ||
} | ||
} | ||
debug("PCB WIRING AT END", pcb.wiring) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.