Skip to content

Commit

Permalink
checkpoint, fixing via processing
Browse files Browse the repository at this point in the history
  • Loading branch information
seveibar committed Dec 16, 2024
1 parent d38d733 commit 8ee972a
Show file tree
Hide file tree
Showing 6 changed files with 221 additions and 153 deletions.
149 changes: 0 additions & 149 deletions lib/dsn-pcb/circuit-json-to-dsn-json/process-pcb-traces.ts

This file was deleted.

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 lib/dsn-pcb/circuit-json-to-dsn-json/process-pcb-traces/index.ts
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)
}
6 changes: 5 additions & 1 deletion lib/dsn-pcb/circuit-json-to-dsn-json/stringify-dsn-json.ts
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,11 @@ export const stringifyDsnJson = (dsnJson: DsnPcb): string => {
// Wiring section
result += `${indent}(wiring\n`
;(dsnJson.wiring?.wires ?? []).forEach((wire) => {
result += `${indent}${indent}(wire ${stringifyPath(wire.path, 3)}(net ${stringifyValue(wire.net)})(type ${wire.type}))\n`
if (wire.type === "via") {
result += `${indent}${indent}(via ${stringifyPath(wire.path, 3)}(net ${stringifyValue(wire.net)}))\n`
} else {
result += `${indent}${indent}(wire ${stringifyPath(wire.path, 3)}(net ${stringifyValue(wire.net)})(type ${wire.type}))\n`
}
})
result += `${indent})\n`

Expand Down
11 changes: 11 additions & 0 deletions lib/dsn-pcb/dsn-json-to-circuit-json/parse-dsn-to-dsn-json.ts
Original file line number Diff line number Diff line change
Expand Up @@ -921,11 +921,22 @@ export function processWiring(nodes: ASTNode[]): Wiring {
) {
wiring.wires!.push(processWire(node.children!))
}
if (
node.type === "List" &&
node.children![0].type === "Atom" &&
node.children![0].value === "via"
) {
wiring.wires!.push(processVia(node.children!))
}
})

return wiring as Wiring
}

function processVia(nodes: ASTNode[]): Wire {
// TODO
}

function processWire(nodes: ASTNode[]): Wire {
const wire: Partial<Wire> = {}

Expand Down
Loading

0 comments on commit 8ee972a

Please sign in to comment.