Skip to content
Open
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
26 changes: 21 additions & 5 deletions lib/check-traces-are-contiguous/check-traces-are-contiguous.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ function routePointConnectsToAnotherExpectedPort(
const expectedPads = padMap.get(expectedPort.pcb_port_id)
return (
expectedPads?.some((pad) =>
isPointInPad({ x: point.x, y: point.y }, pad),
isPointInPad({ x: point.x, y: point.y }, pad, point.layer),
) ?? false
)
})
Expand Down Expand Up @@ -273,13 +273,21 @@ function checkTracesAreContiguous(
const isFirstPointConnected =
firstPoint.route_type === "wire" &&
pads.some((pad) =>
isPointInPad({ x: firstPoint.x, y: firstPoint.y }, pad),
isPointInPad(
{ x: firstPoint.x, y: firstPoint.y },
pad,
firstPoint.layer,
),
)

const isLastPointConnected =
lastPoint.route_type === "wire" &&
pads.some((pad) =>
isPointInPad({ x: lastPoint.x, y: lastPoint.y }, pad),
isPointInPad(
{ x: lastPoint.x, y: lastPoint.y },
pad,
lastPoint.layer,
),
)

if (!isFirstPointConnected && !isLastPointConnected) {
Expand Down Expand Up @@ -326,15 +334,23 @@ function checkTracesAreContiguous(
if (
firstPoint.route_type === "wire" &&
pads.some((pad) =>
isPointInPad({ x: firstPoint.x, y: firstPoint.y }, pad),
isPointInPad(
{ x: firstPoint.x, y: firstPoint.y },
pad,
firstPoint.layer,
),
)
) {
firstConnectsToAnyPad = true
}
if (
lastPoint.route_type === "wire" &&
pads.some((pad) =>
isPointInPad({ x: lastPoint.x, y: lastPoint.y }, pad),
isPointInPad(
{ x: lastPoint.x, y: lastPoint.y },
pad,
lastPoint.layer,
),
)
) {
lastConnectsToAnyPad = true
Expand Down
11 changes: 11 additions & 0 deletions lib/check-traces-are-contiguous/is-point-in-pad.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,18 @@ function isPointOnSegment(
export function isPointInPad(
point: { x: number; y: number },
pad: PcbSmtPad | PcbPlatedHole,
layer?: string,
): boolean {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See if we have a layer type we can import from circuit-json. Otherwise looks good

// A trace endpoint only connects to a pad on the same layer (a top-layer
// wire can't land on a bottom-layer smt pad without a via).
if (layer !== undefined) {
const padIsOnLayer =
pad.type === "pcb_smtpad"
? pad.layer === layer
: pad.layers.some((padLayer) => padLayer === layer)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Simplify this, remove ternary

if (!padIsOnLayer) return false
}

if (pad.type === "pcb_smtpad") {
if (pad.shape === "circle") {
return getDistanceBetweenPoints(point, pad) <= pad.radius
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
278 changes: 136 additions & 142 deletions tests/lib/check-traces-are-contiguous.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -410,148 +410,142 @@ test("still reports a source-trace branch that does not reach a required port",
)
})

test.failing(
"errors when a trace endpoint touches a pad on a different layer",
() => {
// A top-layer wire endpoint at (1,0) coincides with a BOTTOM-layer pad.
// They cannot connect without a via, so this should be flagged.
const circuitJson = [
{
type: "source_trace",
source_trace_id: "trace_1",
connected_source_port_ids: ["port_1", "port_2"],
},
{
type: "pcb_port",
pcb_port_id: "pp1",
source_port_id: "port_1",
x: 0,
y: 0,
layers: ["top"],
},
{
type: "pcb_port",
pcb_port_id: "pp2",
source_port_id: "port_2",
x: 1,
y: 0,
layers: ["top"],
},
{
type: "pcb_smtpad",
pcb_smtpad_id: "pad_1",
pcb_port_id: "pp1",
shape: "rect",
x: 0,
y: 0,
width: 0.5,
height: 0.5,
layer: "top",
},
{
type: "pcb_smtpad",
pcb_smtpad_id: "pad_2",
pcb_port_id: "pp2",
shape: "rect",
x: 1,
y: 0,
width: 0.5,
height: 0.5,
layer: "bottom",
},
{
type: "pcb_trace",
pcb_trace_id: "t1",
source_trace_id: "trace_1",
route: [
{ route_type: "wire", x: 0, y: 0, layer: "top", width: 0.2 },
{ route_type: "wire", x: 1, y: 0, layer: "top", width: 0.2 },
],
},
] as AnyCircuitElement[]
test("errors when a trace endpoint touches a pad on a different layer", () => {
// A top-layer wire endpoint at (1,0) coincides with a BOTTOM-layer pad.
// They cannot connect without a via, so this should be flagged.
const circuitJson = [
{
type: "source_trace",
source_trace_id: "trace_1",
connected_source_port_ids: ["port_1", "port_2"],
},
{
type: "pcb_port",
pcb_port_id: "pp1",
source_port_id: "port_1",
x: 0,
y: 0,
layers: ["top"],
},
{
type: "pcb_port",
pcb_port_id: "pp2",
source_port_id: "port_2",
x: 1,
y: 0,
layers: ["top"],
},
{
type: "pcb_smtpad",
pcb_smtpad_id: "pad_1",
pcb_port_id: "pp1",
shape: "rect",
x: 0,
y: 0,
width: 0.5,
height: 0.5,
layer: "top",
},
{
type: "pcb_smtpad",
pcb_smtpad_id: "pad_2",
pcb_port_id: "pp2",
shape: "rect",
x: 1,
y: 0,
width: 0.5,
height: 0.5,
layer: "bottom",
},
{
type: "pcb_trace",
pcb_trace_id: "t1",
source_trace_id: "trace_1",
route: [
{ route_type: "wire", x: 0, y: 0, layer: "top", width: 0.2 },
{ route_type: "wire", x: 1, y: 0, layer: "top", width: 0.2 },
],
},
] as AnyCircuitElement[]

const errors = checkTracesAreContiguous(circuitJson)
expect(errors.length).toBeGreaterThan(0)
},
)
const errors = checkTracesAreContiguous(circuitJson)
expect(errors.length).toBeGreaterThan(0)
})

test.failing(
"draws an error where a trace endpoint touches a pad on a different layer",
() => {
// Same scenario rendered with shouldDrawErrors: a top-layer wire ends on
// the BOTTOM-layer pad on the right. On unfixed code the check misses it,
// so no error is drawn; the fix makes the error marker appear.
const circuitJson = [
{
type: "pcb_board",
pcb_board_id: "board_1",
center: { x: 0.5, y: 0 },
width: 3,
height: 2,
thickness: 1.6,
num_layers: 2,
material: "fr4",
},
{
type: "source_trace",
source_trace_id: "trace_1",
connected_source_port_ids: ["port_1", "port_2"],
},
{
type: "pcb_port",
pcb_port_id: "pp1",
source_port_id: "port_1",
x: 0,
y: 0,
layers: ["top"],
},
{
type: "pcb_port",
pcb_port_id: "pp2",
source_port_id: "port_2",
x: 1,
y: 0,
layers: ["top"],
},
{
type: "pcb_smtpad",
pcb_smtpad_id: "pad_1",
pcb_port_id: "pp1",
shape: "rect",
x: 0,
y: 0,
width: 0.5,
height: 0.5,
layer: "top",
},
{
type: "pcb_smtpad",
pcb_smtpad_id: "pad_2",
pcb_port_id: "pp2",
shape: "rect",
x: 1,
y: 0,
width: 0.5,
height: 0.5,
layer: "bottom",
},
{
type: "pcb_trace",
pcb_trace_id: "t1",
source_trace_id: "trace_1",
route: [
{ route_type: "wire", x: 0, y: 0, layer: "top", width: 0.2 },
{ route_type: "wire", x: 1, y: 0, layer: "top", width: 0.2 },
],
},
] as AnyCircuitElement[]
test("draws an error where a trace endpoint touches a pad on a different layer", () => {
// Same scenario rendered with shouldDrawErrors: a top-layer wire ends on
// the BOTTOM-layer pad on the right. On unfixed code the check misses it,
// so no error is drawn; the fix makes the error marker appear.
const circuitJson = [
{
type: "pcb_board",
pcb_board_id: "board_1",
center: { x: 0.5, y: 0 },
width: 3,
height: 2,
thickness: 1.6,
num_layers: 2,
material: "fr4",
},
{
type: "source_trace",
source_trace_id: "trace_1",
connected_source_port_ids: ["port_1", "port_2"],
},
{
type: "pcb_port",
pcb_port_id: "pp1",
source_port_id: "port_1",
x: 0,
y: 0,
layers: ["top"],
},
{
type: "pcb_port",
pcb_port_id: "pp2",
source_port_id: "port_2",
x: 1,
y: 0,
layers: ["top"],
},
{
type: "pcb_smtpad",
pcb_smtpad_id: "pad_1",
pcb_port_id: "pp1",
shape: "rect",
x: 0,
y: 0,
width: 0.5,
height: 0.5,
layer: "top",
},
{
type: "pcb_smtpad",
pcb_smtpad_id: "pad_2",
pcb_port_id: "pp2",
shape: "rect",
x: 1,
y: 0,
width: 0.5,
height: 0.5,
layer: "bottom",
},
{
type: "pcb_trace",
pcb_trace_id: "t1",
source_trace_id: "trace_1",
route: [
{ route_type: "wire", x: 0, y: 0, layer: "top", width: 0.2 },
{ route_type: "wire", x: 1, y: 0, layer: "top", width: 0.2 },
],
},
] as AnyCircuitElement[]

const errors = checkTracesAreContiguous(circuitJson)
expect(
convertCircuitJsonToPcbSvg([...circuitJson, ...errors], {
shouldDrawErrors: true,
}),
).toMatchSvgSnapshot(import.meta.path, "trace-endpoint-on-different-layer")
expect(errors.length).toBeGreaterThan(0)
},
)
const errors = checkTracesAreContiguous(circuitJson)
expect(
convertCircuitJsonToPcbSvg([...circuitJson, ...errors], {
shouldDrawErrors: true,
}),
).toMatchSvgSnapshot(import.meta.path, "trace-endpoint-on-different-layer")
expect(errors.length).toBeGreaterThan(0)
})
Loading