Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
18 changes: 18 additions & 0 deletions lib/check-pcb-components-overlap/doPcbElementsOverlap.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,31 @@
import { getBoundsOfPcbElements } from "@tscircuit/circuit-json-util"
import { doBoundsOverlap } from "@tscircuit/math-utils"
import type { PcbHole, PcbPlatedHole, PcbSmtPad } from "circuit-json"
import { getLayersOfPcbElement } from "lib/util/getLayersOfPcbElement"

export type OverlappableElement = PcbSmtPad | PcbPlatedHole | PcbHole

function getElementLayers(elem: OverlappableElement): string[] {
return getLayersOfPcbElement(elem as any)
}

function doLayersOverlap(
layers1: string[],
layers2: string[],
): boolean {
if (layers1.length === 0 || layers2.length === 0) return true
return layers1.some((l) => layers2.includes(l))
}

export function doPcbElementsOverlap(
elem1: OverlappableElement,
elem2: OverlappableElement,
): boolean {
// If the elements are on completely different layers they cannot overlap
const layers1 = getElementLayers(elem1)
const layers2 = getElementLayers(elem2)
if (!doLayersOverlap(layers1, layers2)) return false

const bounds1 = getBoundsOfPcbElements([elem1])
const bounds2 = getBoundsOfPcbElements([elem2])
return doBoundsOverlap(bounds1, bounds2)
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import { expect, test } from "bun:test"
import { convertCircuitJsonToPcbSvg } from "circuit-to-svg"
import type { AnyCircuitElement } from "circuit-json"
import { checkPcbComponentOverlap } from "lib/check-pcb-components-overlap/checkPcbComponentOverlap"

test("SMT pads on different layers at the same position should NOT be flagged as overlapping", () => {
const soup: AnyCircuitElement[] = [
{
type: "pcb_board",
pcb_board_id: "board1",
center: { x: 0, y: 0 },
width: 10,
height: 10,
thickness: 1.6,
num_layers: 2,
material: "fr4" as const,
},
{
type: "pcb_smtpad",
pcb_smtpad_id: "pad_top",
shape: "rect",
x: 0.5,
y: 0,
width: 2,
height: 2,
layer: "top",
},
{
type: "pcb_smtpad",
pcb_smtpad_id: "pad_bottom",
shape: "rect",
x: -0.5,
y: 0,
width: 2,
height: 2,
layer: "bottom",
},
]

const errors = checkPcbComponentOverlap(soup)
expect(errors).toHaveLength(0)

soup.push(...errors)
expect(
convertCircuitJsonToPcbSvg(soup, { shouldDrawErrors: true }),
).toMatchSvgSnapshot(import.meta.path)
})
Loading