Skip to content

Commit 39cd654

Browse files
authored
feat: include trace route length in circuit key (#364)
* feat: include trace route length in circuit key * fix: properly type trace route length
1 parent c7507c6 commit 39cd654

File tree

2 files changed

+38
-2
lines changed

2 files changed

+38
-2
lines changed

src/lib/calculate-circuit-json-key.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { getBoundsOfPcbElements, getElementId } from "@tscircuit/soup-util"
2-
import type { AnyCircuitElement } from "circuit-json"
2+
import type { AnyCircuitElement, PcbTrace } from "circuit-json"
33

44
const formatToFixed4 = (value: number): string =>
55
Number.isFinite(value) ? value.toFixed(4) : "NaN"
@@ -36,8 +36,13 @@ export const calculateCircuitJsonKey = (
3636
formatToFixed4(bounds.maxX),
3737
formatToFixed4(bounds.maxY),
3838
].join(",")
39+
let signature = `${id}:${boundsStr}`
40+
if (element.type === "pcb_trace") {
41+
const routeLength = ((element as PcbTrace).route ?? []).length
42+
signature += `:${routeLength}`
43+
}
3944

40-
elementSignatures.push(`${id}:${boundsStr}`)
45+
elementSignatures.push(signature)
4146
}
4247

4348
if (elementSignatures.length === 0) {

tests/lib/calculate-circuit-json-key.test.ts

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -381,6 +381,37 @@ describe("calculateCircuitJsonKey", () => {
381381
})
382382
})
383383

384+
describe("pcb_trace route updates", () => {
385+
it("should change key when pcb_trace route length changes", () => {
386+
const baseTrace = {
387+
type: "pcb_trace",
388+
pcb_trace_id: "trace1",
389+
route: [
390+
{ x: 0, y: 0, width: 0.1 },
391+
{ x: 1, y: 1, width: 0.1 },
392+
],
393+
layer: "top",
394+
} as any
395+
396+
const circuitJson1: AnyCircuitElement[] = [baseTrace]
397+
const circuitJson2: AnyCircuitElement[] = [
398+
{
399+
...baseTrace,
400+
route: [
401+
{ x: 0, y: 0, width: 0.1 },
402+
{ x: 0.5, y: 0.5, width: 0.1 },
403+
{ x: 1, y: 1, width: 0.1 },
404+
],
405+
} as any,
406+
]
407+
408+
const result1 = calculateCircuitJsonKey(circuitJson1)
409+
const result2 = calculateCircuitJsonKey(circuitJson2)
410+
411+
expect(result1).not.toBe(result2)
412+
})
413+
})
414+
384415
describe("key format validation", () => {
385416
it("should always return string in format 'count_hash'", () => {
386417
const circuitJson: AnyCircuitElement[] = [

0 commit comments

Comments
 (0)