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
123 changes: 123 additions & 0 deletions lib/check-courtyard-overlap/checkCourtyardOverlap.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
import {
areBoundsOverlappingPolygon,
getBoundsFromPoints,
} from "@tscircuit/math-utils"
import type {
AnyCircuitElement,
PcbCourtyardOverlapError,
PcbCourtyardRect,
PcbCourtyardCircle,
PcbCourtyardOutline,
} from "circuit-json"

type CourtyardElement =
| PcbCourtyardRect
| PcbCourtyardCircle
| PcbCourtyardOutline

function getCourtyardPolygon(el: CourtyardElement): { x: number; y: number }[] {
if (el.type === "pcb_courtyard_rect") {
const hw = el.width / 2
const hh = el.height / 2
return [
{ x: el.center.x - hw, y: el.center.y - hh },
{ x: el.center.x + hw, y: el.center.y - hh },
{ x: el.center.x + hw, y: el.center.y + hh },
{ x: el.center.x - hw, y: el.center.y + hh },
]
}
if (el.type === "pcb_courtyard_circle") {
const N = 32
return Array.from({ length: N }, (_, i) => {
const a = (2 * Math.PI * i) / N
return {
x: el.center.x + el.radius * Math.cos(a),
y: el.center.y + el.radius * Math.sin(a),
}
})
}
return el.outline
}

function getComponentName(
circuitJson: AnyCircuitElement[],
pcbComponentId: string,
): string {
const pcbComponent = circuitJson.find(
(el) =>
el.type === "pcb_component" && el.pcb_component_id === pcbComponentId,
)
if (pcbComponent?.type !== "pcb_component") return pcbComponentId
const sourceComponent = circuitJson.find(
(el) =>
el.type === "source_component" &&
el.source_component_id === pcbComponent.source_component_id,
)
if (sourceComponent?.type === "source_component" && sourceComponent.name) {
return sourceComponent.name
}
return pcbComponentId
}

/**
* Check for overlapping PCB component courtyards.
* Returns one error per pair of components whose courtyard elements overlap.
*/
export function checkCourtyardOverlap(
circuitJson: AnyCircuitElement[],
): PcbCourtyardOverlapError[] {
const courtyards = circuitJson.filter(
(el): el is CourtyardElement =>
el.type === "pcb_courtyard_rect" ||
el.type === "pcb_courtyard_circle" ||
el.type === "pcb_courtyard_outline",
)

// Group by component
const byComponent = new Map<string, CourtyardElement[]>()
for (const el of courtyards) {
const id = el.pcb_component_id
if (!byComponent.has(id)) byComponent.set(id, [])
byComponent.get(id)!.push(el)
}

const componentIds = Array.from(byComponent.keys())
const errors: PcbCourtyardOverlapError[] = []

for (let i = 0; i < componentIds.length; i++) {
for (let j = i + 1; j < componentIds.length; j++) {
const idA = componentIds[i]
const idB = componentIds[j]

let overlapping = false
outer: for (const a of byComponent.get(idA)!) {
for (const b of byComponent.get(idB)!) {
const polyA = getCourtyardPolygon(a)
const polyB = getCourtyardPolygon(b)
const boundsA = getBoundsFromPoints(polyA)
const boundsB = getBoundsFromPoints(polyB)
if (!boundsA || !boundsB) continue
if (
areBoundsOverlappingPolygon(boundsA, polyB) ||
areBoundsOverlappingPolygon(boundsB, polyA)
) {
overlapping = true
break outer
}
}
}

if (overlapping) {
errors.push({
type: "pcb_courtyard_overlap_error",
pcb_error_id: `pcb_courtyard_overlap_${idA}_${idB}`,
error_type: "pcb_courtyard_overlap_error",
message: `Courtyard of ${getComponentName(circuitJson, idA)} overlaps with courtyard of ${getComponentName(circuitJson, idB)}`,
pcb_component_ids: [idA, idB],
})
}
}
}

return errors
}
2 changes: 2 additions & 0 deletions lib/run-all-checks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,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 { checkCourtyardOverlap } from "./check-courtyard-overlap/checkCourtyardOverlap"
import { checkConnectorAccessibleOrientation } from "./check-connector-accessible-orientation"
import { checkPinMustBeConnected } from "./check-pin-must-be-connected"
import { checkNoGroundPinDefined } from "./check-no-ground-pin-defined"
Expand All @@ -20,6 +21,7 @@ export async function runAllPlacementChecks(circuitJson: AnyCircuitElement[]) {
...checkViasOffBoard(circuitJson),
...checkPcbComponentsOutOfBoard(circuitJson),
...checkPcbComponentOverlap(circuitJson),
...checkCourtyardOverlap(circuitJson),
...checkConnectorAccessibleOrientation(circuitJson),
]
}
Expand Down
8 changes: 4 additions & 4 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.83",
"@tscircuit/circuit-json-util": "^0.0.90",
"@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.395",
"circuit-to-svg": "^0.0.334",
"circuit-json": "^0.0.400",
"debug": "^4.3.5",
"tscircuit": "^0.0.1439",
"tscircuit": "^0.0.1487",
"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.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Loading