Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Traces routed inside Trace.ts are always routed on top layer #66

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 20 additions & 4 deletions lib/components/primitive-components/Trace.ts
Original file line number Diff line number Diff line change
Expand Up @@ -377,6 +377,7 @@ export class Trace extends PrimitiveComponent<typeof traceProps> {
this.renderError(
`Could not find a common layer (using hints) for trace ${this.getString()}`,
)
return
}

// Cache the PCB obstacles, they'll be needed for each segment between
Expand Down Expand Up @@ -431,7 +432,7 @@ export class Trace extends PrimitiveComponent<typeof traceProps> {
pointsToConnect: [a, b],
},
],
layerCount: 1,
layerCount: 2, // Change to 2 to support top and bottom layers
bounds: {
minX: Math.min(a.x, b.x) - BOUNDS_MARGIN,
maxX: Math.max(a.x, b.x) + BOUNDS_MARGIN,
Expand All @@ -442,8 +443,8 @@ export class Trace extends PrimitiveComponent<typeof traceProps> {
})
const traces = ijump.solveAndMapToTraces()
if (traces.length === 0) {
console.log(
`Could not find a route between ${a} and ${b} for ${this} (TODO render error)`,
this.renderError(
`Could not find a route between ${JSON.stringify(a)} and ${JSON.stringify(b)} for ${this}`,
)
return
}
Expand All @@ -452,8 +453,23 @@ export class Trace extends PrimitiveComponent<typeof traceProps> {
routes.push(trace.route)
}

const mergedRoute = mergeRoutes(routes)

// Add vias where layer changes occur
const routeWithVias = mergedRoute.reduce((acc, point, index, array) => {
acc.push(point)
if (index < array.length - 1 && point.layer !== array[index + 1].layer) {
acc.push({
...point,
via: true,
via_to_layer: array[index + 1].layer,
})
}
return acc
}, [] as PCBTrace["route"])

const pcb_trace = db.pcb_trace.insert({
route: mergeRoutes(routes),
route: routeWithVias,
source_trace_id: this.source_trace_id!,
})
this._portsRoutedOnPcb = ports
Expand Down
29 changes: 29 additions & 0 deletions tests/components/multilayer-trace.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { test, expect } from "bun:test"
import { getTestFixture } from "tests/fixtures/get-test-fixture"

test("multilayer trace with tracehint", () => {
const { circuit } = getTestFixture()

circuit.add(
<board width="20mm" height="20mm">
<resistor name="R1" resistance="10k" footprint="0402" pcbX={5} pcbY={5} />
<resistor name="R2" resistance="10k" footprint="0402" pcbX={15} pcbY={15} />
<trace path={["R1.p2", "R2.p1"]}>
<tracehint x={10} y={10} layer="bottom" />
</trace>
</board>
)

circuit.render()

const traces = circuit.db.pcb_trace.list()
expect(traces).toHaveLength(1)

const trace = traces[0]
expect(trace.route).toHaveLength(3)
expect(trace.route[0].layer).toBe("top")
expect(trace.route[1].layer).toBe("bottom")
expect(trace.route[2].layer).toBe("top")

expect(circuit).toMatchPcbSnapshot(import.meta.path)
})
Loading