Skip to content
Merged
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
7 changes: 7 additions & 0 deletions lib/components/primitive-components/CourtyardRect.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { decomposeTSR } from "transformation-matrix"
import { PrimitiveComponent } from "../base-components/PrimitiveComponent"
import { courtyardRectProps } from "@tscircuit/props"

Expand Down Expand Up @@ -34,6 +35,11 @@ export class CourtyardRect extends PrimitiveComponent<
this.parent?.pcb_component_id ??
this.getPrimitiveContainer()?.pcb_component_id!

const decomposedTransform = decomposeTSR(
this._computePcbGlobalTransformBeforeLayout(),
)
const ccw_rotation = (decomposedTransform.rotation.angle * 180) / Math.PI

const pcb_courtyard_rect = db.pcb_courtyard_rect.insert({
pcb_component_id,
layer,
Expand All @@ -43,6 +49,7 @@ export class CourtyardRect extends PrimitiveComponent<
},
width: props.width,
height: props.height,
ccw_rotation: ccw_rotation || undefined,
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.

Critical Bug: Zero rotation will be stored as undefined

The expression ccw_rotation || undefined will incorrectly evaluate to undefined when ccw_rotation is 0 (zero degrees). This is because 0 is a falsy value in JavaScript.

Impact: Components with 0° rotation will have their rotation stored as undefined instead of 0, potentially breaking rotation-dependent logic elsewhere in the codebase.

Fix: Either remove the || undefined entirely, or use a proper nullish check:

ccw_rotation: ccw_rotation ?? undefined,

or simply:

ccw_rotation: ccw_rotation,
Suggested change
ccw_rotation: ccw_rotation || undefined,
ccw_rotation: ccw_rotation,

Spotted by Graphite

Fix in Graphite


Is this helpful? React 👍 or 👎 to let us know.

subcircuit_id: subcircuit?.subcircuit_id ?? undefined,
pcb_group_id: this.getGroup()?.pcb_group_id ?? undefined,
})
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -60,13 +60,13 @@
"bun-match-svg": "0.0.12",
"calculate-elbow": "^0.0.12",
"chokidar-cli": "^3.0.0",
"circuit-json": "^0.0.402",
"circuit-json": "^0.0.403",
"circuit-json-to-bpc": "^0.0.13",
"circuit-json-to-connectivity-map": "^0.0.23",
"circuit-json-to-gltf": "^0.0.73",
"circuit-json-to-simple-3d": "^0.0.9",
"circuit-json-to-spice": "^0.0.34",
"circuit-to-svg": "^0.0.336",
"circuit-to-svg": "^0.0.337",
"concurrently": "^9.1.2",
"connectivity-map": "^1.0.0",
"debug": "^4.3.6",
Expand Down
1 change: 1 addition & 0 deletions tests/drc/__snapshots__/courtyard-overlap3-pcb.snap.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions tests/drc/__snapshots__/courtyard-overlap4-pcb.snap.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
56 changes: 56 additions & 0 deletions tests/drc/courtyard-overlap3.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import { test, expect } from "bun:test"
import { getTestFixture } from "../fixtures/get-test-fixture"

/**
* Two 0805 inductors, L2 rotated 90°, placed so their courtyards overlap.
*
* 0805 courtyard: width=3.35mm, height=1.9mm.
* L1 (0°) at (0,0): courtyard x=[-1.675, 1.675], y=[-0.95, 0.95].
* L2 (90°) at (2.5, 0): with pcb_courtyard_outline the polygon rotates correctly,
* so effective x=[-0.95,0.95], y=[-1.675,1.675] shifted → x=[1.55, 3.45].
* Courtyards overlap in x at [1.55, 1.675].
* Pads of L2 are at x=2.5±0.5125, well clear of L1's pads ending at x=1.425.
*/
test("drc courtyard overlap detects overlapping 0805 inductors (one rotated 90°)", async () => {
const { circuit } = getTestFixture()

circuit.add(
<board width="20mm" height="20mm">
<inductor
name="L1"
footprint="0805"
inductance="10uH"
pcbX={0}
pcbY={0}
/>
<inductor
name="L2"
footprint="0805"
inductance="10uH"
pcbX={2.5}
pcbY={0}
pcbRotation={90}
/>
</board>,
)

await circuit.renderUntilSettled()

const circuitJson = circuit.getCircuitJson()

const padOverlapErrors = circuitJson.filter(
(e: any) => e.type === "pcb_footprint_overlap_error",
)
expect(padOverlapErrors.length).toBe(0)

const courtyardErrors = circuitJson.filter(
(e: any) => e.type === "pcb_courtyard_overlap_error",
)
expect(courtyardErrors.length).toBeGreaterThan(0)
expect((courtyardErrors[0] as any).message).toContain("overlaps")

expect(circuit).toMatchPcbSnapshot(import.meta.path, {
shouldDrawErrors: true,
showCourtyards: true,
})
})
58 changes: 58 additions & 0 deletions tests/drc/courtyard-overlap4.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
import { test, expect } from "bun:test"
import { getTestFixture } from "../fixtures/get-test-fixture"

/**
* Two 0603 capacitors, C2 rotated 45°, placed so their courtyards overlap.
*
* 0603 courtyard: width=2.95mm, height=1.45mm.
* At 45°, AABB = (2.95 + 1.45) * cos(45°) ≈ 3.111mm on each side, half = 1.556mm.
* C1 (0°) at (0,0): courtyard x=[-1.475, 1.475].
* C2 (45°) at (2.7,0): courtyard x=[1.144, 4.256].
* Courtyards overlap by ~0.331mm.
*
* 0603 pads: width=2.45mm, height=0.95mm.
* C1 pads right edge = 1.225mm; C2 pads left edge ≈ 1.498mm → no pad overlap.
*/
test("drc courtyard overlap detects overlapping 0603 capacitors (one rotated 45°)", async () => {
const { circuit } = getTestFixture()

circuit.add(
<board width="20mm" height="20mm">
<capacitor
name="C1"
footprint="0603"
capacitance="100nF"
pcbX={0}
pcbY={0}
/>
<capacitor
name="C2"
footprint="0603"
capacitance="100nF"
pcbX={2.7}
pcbY={0}
pcbRotation={45}
/>
</board>,
)

await circuit.renderUntilSettled()

const circuitJson = circuit.getCircuitJson()

const padOverlapErrors = circuitJson.filter(
(e: any) => e.type === "pcb_footprint_overlap_error",
)
expect(padOverlapErrors.length).toBe(0)

const courtyardErrors = circuitJson.filter(
(e: any) => e.type === "pcb_courtyard_overlap_error",
)
expect(courtyardErrors.length).toBeGreaterThan(0)
expect((courtyardErrors[0] as any).message).toContain("overlaps")

expect(circuit).toMatchPcbSnapshot(import.meta.path, {
shouldDrawErrors: true,
showCourtyards: true,
})
})
Loading