Skip to content

Commit

Permalink
Merge pull request #71 from Abse2001/main
Browse files Browse the repository at this point in the history
smoothie board repro
  • Loading branch information
seveibar authored Dec 21, 2024
2 parents 03e17f4 + 94087d9 commit ea89bff
Show file tree
Hide file tree
Showing 4 changed files with 3,068 additions and 44 deletions.
97 changes: 53 additions & 44 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 @@ -541,7 +541,8 @@ function processImage(nodes: ASTNode[]): Image {
if (key === "outline") {
image.outlines!.push(processOutline(node.children!))
} else if (key === "pin") {
image.pins!.push(processPin(node.children!))
const pin = processPin(node.children!)
if (pin) image.pins!.push(pin)
}
}
}
Expand All @@ -564,27 +565,28 @@ function processOutline(nodes: ASTNode[]): Outline {
return outline as Outline
}

function processPin(nodes: ASTNode[]): Pin {
function processPin(nodes: ASTNode[]): Pin | null {
const pin: Partial<Pin> = {}

try {
// Get padstack name
if (nodes[1]?.type === "Atom") {
pin.padstack_name = String(nodes[1].value)
} else {
throw new Error("Missing padstack name")
// Get padstack name
if (nodes[1]?.type !== "Atom") {
debug("Unsupported pin padstack_name format:", nodes)
return null
}
pin.padstack_name = String(nodes[1].value)

// Get pin number
if (nodes[2]?.type === "Atom") {
if (typeof nodes[2].value === "number") {
pin.pin_number = nodes[2].value
} else {
const parsed = parseInt(String(nodes[2].value), 10)
pin.pin_number = Number.isNaN(parsed) ? String(nodes[2].value) : parsed
}
if (nodes[2]?.type !== "Atom") {
debug("Unsupported pin number format:", nodes)
return null
}

if (typeof nodes[2].value === "number") {
pin.pin_number = nodes[2].value
} else {
throw new Error("Missing pin number")
const parsed = parseInt(String(nodes[2].value), 10)
pin.pin_number = Number.isNaN(parsed) ? String(nodes[2].value) : parsed
}

// Handle coordinates, accounting for scientific notation
Expand Down Expand Up @@ -818,14 +820,16 @@ export function processNetwork(nodes: ASTNode[]): Network {
}
}
})

return network as Network
}

function processNet(nodes: ASTNode[]): Net {
const net: Partial<Net> = {}
if (nodes[1].type === "Atom" && typeof nodes[1].value === "string") {
net.name = nodes[1].value
} else {
net.name = nodes[1].value?.toString()
debug("net name was not a string", net.name)
}

nodes.slice(2).forEach((node) => {
Expand All @@ -843,7 +847,6 @@ function processNet(nodes: ASTNode[]): Net {
})
}
})

return net as Net
}

Expand Down Expand Up @@ -929,14 +932,15 @@ export function processWiring(nodes: ASTNode[]): Wiring {
node.children![0].type === "Atom" &&
node.children![0].value === "via"
) {
wiring.wires!.push(processVia(node.children!))
const via = processVia(node.children!)
if (via) wiring.wires!.push(via)
}
})

return wiring as Wiring
}

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

// Find the path node which contains coordinates
Expand All @@ -947,35 +951,40 @@ function processVia(nodes: ASTNode[]): Wire {
node.children[0].value === "path",
)

if (pathNode?.children) {
const coords = pathNode.children
.filter((node) => node.type === "Atom" && typeof node.value === "number")
.slice(-2) // Take last two numbers as x,y coordinates
if (!pathNode?.children) {
debug("Warning: Missing path node for via", nodes)
return null
}

if (coords.length === 2) {
wire.path = {
layer: "all", // vias connect all layers
width: 0, // width is defined by the padstack
coordinates: coords.map((node) => node.value as number),
}
wire.type = "via"

// Find net name if present
const netNode = nodes.find(
(node) =>
node.type === "List" &&
node.children?.[0]?.type === "Atom" &&
node.children[0].value === "net",
)
if (netNode?.children?.[1]?.type === "Atom") {
wire.net = String(netNode.children[1].value)
}
const coords = pathNode.children
.filter((node) => node.type === "Atom" && typeof node.value === "number")
.slice(-2) // Take last two numbers as x, y coordinates

return wire as Wire
}
if (coords.length !== 2) {
debug("Warning: Missing or incomplete coordinates for via", nodes)
return null
}

wire.path = {
layer: "all", // vias connect all layers
width: 0, // width is defined by the padstack
coordinates: coords.map((node) => node.value as number),
}
wire.type = "via"

// Find net name if present
const netNode = nodes.find(
(node) =>
node.type === "List" &&
node.children?.[0]?.type === "Atom" &&
node.children[0].value === "net",
)

if (netNode?.children?.[1]?.type === "Atom") {
wire.net = String(netNode.children[1].value)
}

throw new Error("Invalid via format")
return wire as Wire
}

function processWire(nodes: ASTNode[]): Wire {
Expand Down
Loading

0 comments on commit ea89bff

Please sign in to comment.