Skip to content

Commit d13a1a0

Browse files
committed
Fix NaN KiCad segments from through_obstacle trace route points
1 parent 1e3bb66 commit d13a1a0

2 files changed

Lines changed: 77 additions & 4 deletions

File tree

lib/pcb/stages/AddTracesStage.ts

Lines changed: 33 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,24 @@ export class AddTracesStage extends ConverterStage<CircuitJson, KicadPcb> {
2222
this.pcbTraces = this.ctx.db.pcb_trace.list()
2323
}
2424

25+
private getRoutePointPosition(point: any, edge: "start" | "end") {
26+
const position =
27+
typeof point.x === "number" && typeof point.y === "number"
28+
? { x: point.x, y: point.y }
29+
: point[edge]
30+
31+
if (
32+
typeof position?.x !== "number" ||
33+
typeof position?.y !== "number" ||
34+
!Number.isFinite(position.x) ||
35+
!Number.isFinite(position.y)
36+
) {
37+
return null
38+
}
39+
40+
return position
41+
}
42+
2543
override _step(): void {
2644
const { kicadPcb, c2kMatPcb, pcbNetMap } = this.ctx
2745

@@ -52,17 +70,28 @@ export class AddTracesStage extends ConverterStage<CircuitJson, KicadPcb> {
5270
for (let i = 0; i < trace.route.length - 1; i++) {
5371
const startPoint = trace.route[i]
5472
const endPoint = trace.route[i + 1]
73+
const startPosition = this.getRoutePointPosition(startPoint, "end")
74+
const endPosition = this.getRoutePointPosition(endPoint, "start")
75+
76+
if (!startPosition || !endPosition) continue
5577

5678
// Transform points to KiCad coordinates
5779
const transformedStart = applyToPoint(c2kMatPcb, {
58-
x: startPoint.x,
59-
y: startPoint.y,
80+
x: startPosition.x,
81+
y: startPosition.y,
6082
})
6183
const transformedEnd = applyToPoint(c2kMatPcb, {
62-
x: endPoint.x,
63-
y: endPoint.y,
84+
x: endPosition.x,
85+
y: endPosition.y,
6486
})
6587

88+
if (
89+
transformedStart.x === transformedEnd.x &&
90+
transformedStart.y === transformedEnd.y
91+
) {
92+
continue
93+
}
94+
6695
let netInfo: PcbNetInfo | undefined
6796
if (pcbNetMap) {
6897
let connectivityKey: string | undefined =
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
import { expect, test } from "bun:test"
2+
import type { CircuitJson } from "circuit-json"
3+
import { CircuitJsonToKicadPcbConverter } from "lib/pcb/CircuitJsonToKicadPcbConverter"
4+
5+
const circuitJson: CircuitJson = [
6+
{
7+
type: "source_net",
8+
source_net_id: "source_net_1",
9+
name: "N1",
10+
subcircuit_connectivity_map_key: "net1",
11+
},
12+
{
13+
type: "source_trace",
14+
source_trace_id: "source_trace_1",
15+
connected_source_net_ids: ["source_net_1"],
16+
subcircuit_connectivity_map_key: "net1",
17+
},
18+
{
19+
type: "pcb_trace",
20+
pcb_trace_id: "pcb_trace_1",
21+
source_trace_id: "source_trace_1",
22+
route: [
23+
{ route_type: "wire", x: 0, y: 0, width: 0.15, layer: "top" },
24+
{
25+
route_type: "through_obstacle",
26+
start: { x: 0, y: 0 },
27+
end: { x: 0, y: 0 },
28+
from_layer: "top",
29+
to_layer: "inner1",
30+
width: 0.15,
31+
},
32+
{ route_type: "wire", x: 1, y: 0, width: 0.15, layer: "inner1" },
33+
],
34+
},
35+
] as any
36+
37+
test("repro18: through_obstacle route points do not emit NaN segments", () => {
38+
const converter = new CircuitJsonToKicadPcbConverter(circuitJson)
39+
converter.runUntilFinished()
40+
const output = converter.getOutputString()
41+
42+
expect(output).not.toContain("NaN")
43+
expect(output).toContain("(end 101 100)")
44+
})

0 commit comments

Comments
 (0)