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
2 changes: 2 additions & 0 deletions index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,5 @@ export {
runAllPlacementChecks,
runAllRoutingChecks,
} from "./lib/run-all-checks"

export { checkConnectorAccessibleOrientation } from "./lib/check-connector-accessible-orientation"
99 changes: 99 additions & 0 deletions lib/check-connector-accessible-orientation.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
import { getBoardBounds } from "@tscircuit/circuit-json-util"
import type {
AnyCircuitElement,
PcbBoard,
PcbComponent,
PcbConnectorNotInAccessibleOrientationWarning,
} from "circuit-json"
import { getReadableNameForComponent } from "./util/get-readable-names"

type FacingDirection = "x-" | "x+" | "y+" | "y-"

function getFacingDirection(component: PcbComponent): FacingDirection | null {
if (!component.center || !component.cable_insertion_center) return null

const dx = component.cable_insertion_center.x - component.center.x
const dy = component.cable_insertion_center.y - component.center.y

if (Math.abs(dx) < 1e-6 && Math.abs(dy) < 1e-6) return null

if (Math.abs(dx) >= Math.abs(dy)) {
return dx >= 0 ? "x+" : "x-"
}

return dy >= 0 ? "y+" : "y-"
}

function getRecommendedFacingDirection(
component: PcbComponent,
bounds: { minX: number; maxX: number; minY: number; maxY: number },
): FacingDirection | null {
if (!component.center) return null

const distances = [
{ direction: "x-" as const, distance: component.center.x - bounds.minX },
{ direction: "x+" as const, distance: bounds.maxX - component.center.x },
{ direction: "y-" as const, distance: component.center.y - bounds.minY },
{ direction: "y+" as const, distance: bounds.maxY - component.center.y },
]

distances.sort((a, b) => a.distance - b.distance)
return distances[0]?.direction ?? null
}

export function checkConnectorAccessibleOrientation(
circuitJson: AnyCircuitElement[],
): PcbConnectorNotInAccessibleOrientationWarning[] {
const board = circuitJson.find(
(el): el is PcbBoard => el.type === "pcb_board",
)
if (!board) return []

const bounds = (() => {
try {
return getBoardBounds(board)
} catch {
return null
}
})()
if (!bounds) return []

const warnings: PcbConnectorNotInAccessibleOrientationWarning[] = []

const components = circuitJson.filter(
(el): el is PcbComponent => el.type === "pcb_component",
)

for (const component of components) {
if (!component.cable_insertion_center) continue

const facingDirection = getFacingDirection(component)
const recommendedFacingDirection = getRecommendedFacingDirection(
component,
bounds,
)

if (!facingDirection || !recommendedFacingDirection) continue
if (facingDirection === recommendedFacingDirection) continue

const componentName = getReadableNameForComponent(
circuitJson,
component.pcb_component_id,
)

warnings.push({
type: "pcb_connector_not_in_accessible_orientation_warning",
warning_type: "pcb_connector_not_in_accessible_orientation_warning",
pcb_connector_not_in_accessible_orientation_warning_id: `pcb_connector_not_in_accessible_orientation_warning_${component.pcb_component_id}`,
message: `${componentName} is facing ${facingDirection} but should face ${recommendedFacingDirection} so the connector is accessible from the board edge`,
pcb_component_id: component.pcb_component_id,
source_component_id: component.source_component_id,
pcb_board_id: board.pcb_board_id,
facing_direction: facingDirection,
recommended_facing_direction: recommendedFacingDirection,
subcircuit_id: component.subcircuit_id,
})
}

return warnings
}
2 changes: 2 additions & 0 deletions lib/run-all-checks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { checkEachPcbTraceNonOverlapping } from "./check-each-pcb-trace-non-over
import { checkPcbComponentsOutOfBoard } from "./check-pcb-components-out-of-board/checkPcbComponentsOutOfBoard"
import { checkViasOffBoard } from "./check-pcb-components-out-of-board/checkViasOffBoard"
import { checkPcbComponentOverlap } from "./check-pcb-components-overlap/checkPcbComponentOverlap"
import { checkConnectorAccessibleOrientation } from "./check-connector-accessible-orientation"
import { checkPinMustBeConnected } from "./check-pin-must-be-connected"
import { checkSameNetViaSpacing } from "./check-same-net-via-spacing"
import { checkSourceTracesHavePcbTraces } from "./check-source-traces-have-pcb-traces"
Expand All @@ -16,6 +17,7 @@ export async function runAllPlacementChecks(circuitJson: AnyCircuitElement[]) {
...checkViasOffBoard(circuitJson),
...checkPcbComponentsOutOfBoard(circuitJson),
...checkPcbComponentOverlap(circuitJson),
...checkConnectorAccessibleOrientation(circuitJson),
]
}

Expand Down
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,15 @@
},
"devDependencies": {
"@biomejs/biome": "^1.9.0",
"@tscircuit/circuit-json-util": "^0.0.72",
"@tscircuit/circuit-json-util": "^0.0.83",
"@tscircuit/log-soup": "^1.0.2",
"@types/bun": "^1.2.8",
"@types/debug": "^4.1.12",
"bun-match-svg": "^0.0.11",
"circuit-to-svg": "^0.0.333",
"circuit-json": "^0.0.380",
"circuit-json": "^0.0.391",
"debug": "^4.3.5",
"tscircuit": "^0.0.1399",
"tscircuit": "^0.0.1439",
"zod": "^3.23.8",
"tsup": "^8.2.3"
},
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
144 changes: 144 additions & 0 deletions tests/lib/check-connector-accessible-orientation.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,144 @@
import { expect, test } from "bun:test"
import { convertCircuitJsonToPcbSvg } from "circuit-to-svg"
import { Circuit } from "tscircuit"
import { checkConnectorAccessibleOrientation } from "lib/check-connector-accessible-orientation"

const TYPE_C_6P_FOOTPRINT = (
<footprint>
<platedhole
portHints={["pin7"]}
pcbX="4.319904999999949mm"
pcbY="-2.300014400000009mm"
holeWidth="0.5999987999999999mm"
holeHeight="1.3999972mm"
outerWidth="1.0999978mm"
outerHeight="1.8999962mm"
shape="pill"
/>
<platedhole
portHints={["pin8"]}
pcbX="-4.3199050000000625mm"
pcbY="-2.300014400000009mm"
holeWidth="0.5999987999999999mm"
holeHeight="1.3999972mm"
outerWidth="1.0999978mm"
outerHeight="1.8999962mm"
shape="pill"
/>
<platedhole
portHints={["pin9"]}
pcbX="4.319904999999949mm"
pcbY="1.5000795999999355mm"
holeWidth="0.5999987999999999mm"
holeHeight="1.3999972mm"
outerWidth="1.0999978mm"
outerHeight="1.8999962mm"
shape="pill"
/>
<platedhole
portHints={["pin10"]}
pcbX="-4.3199050000000625mm"
pcbY="1.5000795999999355mm"
holeWidth="0.5999987999999999mm"
holeHeight="1.3999972mm"
outerWidth="1.0999978mm"
outerHeight="1.8999962mm"
shape="pill"
/>
<smtpad
portHints={["pin11"]}
pcbX="-2.6998930000000883mm"
pcbY="1.7500155999998697mm"
width="0.7999983999999999mm"
height="1.1999975999999999mm"
shape="rect"
/>
<smtpad
portHints={["pin12"]}
pcbX="-1.4999970000001213mm"
pcbY="1.7500155999998697mm"
width="0.6999986mm"
height="1.1999975999999999mm"
shape="rect"
/>
<smtpad
portHints={["pin13"]}
pcbX="-0.4999990000001162mm"
pcbY="1.7500155999998697mm"
width="0.6999986mm"
height="1.1999975999999999mm"
shape="rect"
/>
<smtpad
portHints={["pin14"]}
pcbX="0.4999990000000025mm"
pcbY="1.7500155999998697mm"
width="0.6999986mm"
height="1.1999975999999999mm"
shape="rect"
/>
<smtpad
portHints={["pin15"]}
pcbX="1.4999970000000076mm"
pcbY="1.7500155999998697mm"
width="0.6999986mm"
height="1.1999975999999999mm"
shape="rect"
/>
<smtpad
portHints={["pin16"]}
pcbX="2.6998929999999746mm"
pcbY="1.7500155999998697mm"
width="0.7999983999999999mm"
height="1.1999975999999999mm"
shape="rect"
/>
</footprint>
)

test("connector orientation warning is emitted when cable insertion points inward", async () => {
const circuit = new Circuit()
circuit.add(
<board width="30mm" height="20mm" routingDisabled>
<connector
name="J1"
pcbX="-13mm"
pcbY="0mm"
footprint={TYPE_C_6P_FOOTPRINT}
pinLabels={{ 1: ["A"], 2: ["B"] }}
/>
</board>,
)

await circuit.renderUntilSettled()
const circuitJson = circuit.getCircuitJson()
const warnings = checkConnectorAccessibleOrientation(circuitJson as any)

expect(warnings).toHaveLength(1)
expect(warnings[0]).toMatchObject({
type: "pcb_connector_not_in_accessible_orientation_warning",
facing_direction: "y-",
recommended_facing_direction: "x-",
})

expect(
convertCircuitJsonToPcbSvg([...circuitJson, ...warnings] as any, {
shouldDrawErrors: true,
}),
).toMatchSvgSnapshot(import.meta.path)
})

test("connector orientation check skips components without cable_insertion_center", async () => {
const circuit = new Circuit()
circuit.add(
<board width="30mm" height="20mm" routingDisabled>
<chip name="U1" pcbX="10mm" pcbY="0mm" pinLabels={{ 1: ["P1"] }} />
</board>,
)

await circuit.renderUntilSettled()
const circuitJson = circuit.getCircuitJson()

const warnings = checkConnectorAccessibleOrientation(circuitJson as any)
expect(warnings).toHaveLength(0)
})
2 changes: 1 addition & 1 deletion tests/repros/__snapshots__/repro01.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/repros/__snapshots__/repro02.snap.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
7 changes: 6 additions & 1 deletion tests/repros/repro01.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,12 @@ const OverlapRepro = () => (
)

test("repro01: overlapping pcb_smtpad error uses concise pin references", async () => {
const circuit = new Circuit()
const circuit = new Circuit({
platform: {
netlistDrcChecksDisabled: true,
placementDrcChecksDisabled: true,
},
})
circuit.add(<OverlapRepro />)

await circuit.renderUntilSettled()
Expand Down
Loading