-
Notifications
You must be signed in to change notification settings - Fork 14
feat: add DRC check for vias too close to pads #113
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
lucaferri-dev
wants to merge
8
commits into
tscircuit:main
Choose a base branch
from
lucaferri-dev:feat/check-vias-too-close-to-pads
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 2 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
a8175bc
feat: add DRC check for vias too close to pads
lucaferri-dev 0783bc3
refactor: split via-to-pad-spacing tests into separate files
lucaferri-dev 0334297
fix: add PCB snapshots to via-to-pad spacing tests
lucaferri-dev 0c04968
fix: add realistic PCB snapshot test and fix getPadRadius half-diagonal
lucaferri-dev 1bc5814
feat: add clear PCB snapshot test with real tscircuit circuit
lucaferri-dev 4a243c0
fix: remove noisy pcb-snapshot test, keep clean JSON-fixture snapshot
lucaferri-dev 6cb31b6
refactor: simplify via-to-pad check using getBoundsOfPcbElements
lucaferri-dev b525442
test: add single via-to-pad spacing test with realistic circuit
lucaferri-dev File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,177 @@ | ||
| import type { | ||
| AnyCircuitElement, | ||
| PcbVia, | ||
| PcbSmtPad, | ||
| PcbPlatedHole, | ||
| PcbViaClearanceError, | ||
| } from "circuit-json" | ||
| import { getReadableNameForElement } from "@tscircuit/circuit-json-util" | ||
| import { DEFAULT_VIA_TO_PAD_MARGIN, EPSILON } from "lib/drc-defaults" | ||
|
|
||
| type Pad = PcbSmtPad | PcbPlatedHole | ||
|
|
||
| /** | ||
| * Get the effective radius of a pad for clearance calculations. | ||
| * For rectangular pads, returns the half-diagonal (conservative bounding circle). | ||
| * For circular pads/plated holes, returns the actual radius. | ||
| */ | ||
| function getPadRadius(pad: Pad): number { | ||
| if (pad.type === "pcb_smtpad") { | ||
| if (pad.shape === "circle") return pad.radius | ||
| if ( | ||
| pad.shape === "rect" || | ||
| pad.shape === "rotated_rect" || | ||
| pad.shape === "pill" || | ||
| pad.shape === "rotated_pill" | ||
| ) { | ||
| return Math.max(pad.width, pad.height) / 2 | ||
| } | ||
| return 0 | ||
| } | ||
| if (pad.type === "pcb_plated_hole") { | ||
| if (pad.shape === "circle") return pad.outer_diameter / 2 | ||
| if (pad.shape === "oval" || pad.shape === "pill") { | ||
| return Math.max(pad.outer_width, pad.outer_height) / 2 | ||
| } | ||
| if (pad.shape === "pill_hole_with_rect_pad") { | ||
| return Math.max(pad.rect_pad_width, pad.rect_pad_height) / 2 | ||
| } | ||
| return 0 | ||
| } | ||
| return 0 | ||
| } | ||
|
|
||
| function getPadId(pad: Pad): string { | ||
| if (pad.type === "pcb_smtpad") return pad.pcb_smtpad_id | ||
| return pad.pcb_plated_hole_id | ||
| } | ||
|
|
||
| /** | ||
| * Compute the minimum distance between a via (circle) and a rectangular pad. | ||
| * Returns the edge-to-edge gap (negative if overlapping). | ||
| */ | ||
| function distanceViaToRectPad( | ||
| via: PcbVia, | ||
| pad: { x: number; y: number; width: number; height: number }, | ||
| ): number { | ||
| const halfW = pad.width / 2 | ||
| const halfH = pad.height / 2 | ||
| // Nearest point on rectangle to via center | ||
| const nearestX = Math.max(pad.x - halfW, Math.min(via.x, pad.x + halfW)) | ||
| const nearestY = Math.max(pad.y - halfH, Math.min(via.y, pad.y + halfH)) | ||
| const dist = Math.hypot(via.x - nearestX, via.y - nearestY) | ||
| return dist - via.outer_diameter / 2 | ||
| } | ||
|
|
||
| /** | ||
| * Compute the edge-to-edge gap between a via and a circular pad. | ||
| */ | ||
| function distanceViaToCirclePad( | ||
| via: PcbVia, | ||
| padX: number, | ||
| padY: number, | ||
| padRadius: number, | ||
| ): number { | ||
| const dist = Math.hypot(via.x - padX, via.y - padY) | ||
| return dist - via.outer_diameter / 2 - padRadius | ||
| } | ||
|
|
||
| /** | ||
| * Compute the edge-to-edge gap between a via and any pad type. | ||
| */ | ||
| function computeGap(via: PcbVia, pad: Pad): number { | ||
| if (pad.type === "pcb_smtpad") { | ||
| if (pad.shape === "circle") { | ||
| return distanceViaToCirclePad(via, pad.x, pad.y, pad.radius) | ||
| } | ||
| if ( | ||
| pad.shape === "rect" || | ||
| pad.shape === "rotated_rect" || | ||
| pad.shape === "pill" || | ||
| pad.shape === "rotated_pill" | ||
| ) { | ||
| return distanceViaToRectPad(via, { | ||
| x: pad.x, | ||
| y: pad.y, | ||
| width: pad.width, | ||
| height: pad.height, | ||
| }) | ||
| } | ||
| // Fallback: use bounding circle | ||
| return distanceViaToCirclePad(via, pad.x, pad.y, getPadRadius(pad)) | ||
| } | ||
| if (pad.type === "pcb_plated_hole") { | ||
| if (pad.shape === "circle") { | ||
| return distanceViaToCirclePad(via, pad.x, pad.y, pad.outer_diameter / 2) | ||
| } | ||
| if (pad.shape === "oval" || pad.shape === "pill") { | ||
| return distanceViaToRectPad(via, { | ||
| x: pad.x, | ||
| y: pad.y, | ||
| width: pad.outer_width, | ||
| height: pad.outer_height, | ||
| }) | ||
| } | ||
| if (pad.shape === "pill_hole_with_rect_pad") { | ||
| return distanceViaToRectPad(via, { | ||
| x: pad.x, | ||
| y: pad.y, | ||
| width: pad.rect_pad_width, | ||
| height: pad.rect_pad_height, | ||
| }) | ||
| } | ||
| return distanceViaToCirclePad(via, pad.x, pad.y, getPadRadius(pad)) | ||
| } | ||
| return Number.POSITIVE_INFINITY | ||
| } | ||
|
|
||
| export function checkViaToPadSpacing( | ||
| circuitJson: AnyCircuitElement[], | ||
| { | ||
| minSpacing = DEFAULT_VIA_TO_PAD_MARGIN, | ||
| }: { minSpacing?: number } = {}, | ||
| ): PcbViaClearanceError[] { | ||
| const vias = circuitJson.filter((el) => el.type === "pcb_via") as PcbVia[] | ||
| const pads: Pad[] = [ | ||
| ...(circuitJson.filter((el) => el.type === "pcb_smtpad") as PcbSmtPad[]), | ||
| ...(circuitJson.filter( | ||
| (el) => el.type === "pcb_plated_hole", | ||
| ) as PcbPlatedHole[]), | ||
| ] | ||
|
|
||
| if (vias.length === 0 || pads.length === 0) return [] | ||
|
|
||
| const errors: PcbViaClearanceError[] = [] | ||
|
|
||
| for (const via of vias) { | ||
| for (const pad of pads) { | ||
| const gap = computeGap(via, pad) | ||
| if (gap + EPSILON >= minSpacing) continue | ||
|
|
||
| const padId = getPadId(pad) | ||
| const pairId = [via.pcb_via_id, padId].sort().join("_") | ||
|
|
||
| errors.push({ | ||
| type: "pcb_via_clearance_error", | ||
| pcb_error_id: `via_pad_close_${pairId}`, | ||
| message: `Via ${getReadableNameForElement( | ||
| circuitJson, | ||
| via.pcb_via_id, | ||
| )} is too close to pad ${getReadableNameForElement( | ||
| circuitJson, | ||
| padId, | ||
| )} (gap: ${gap.toFixed(3)}mm, minimum: ${minSpacing}mm)`, | ||
| error_type: "pcb_via_clearance_error", | ||
| pcb_via_ids: [via.pcb_via_id], | ||
| minimum_clearance: minSpacing, | ||
| actual_clearance: gap, | ||
| pcb_center: { | ||
| x: (via.x + pad.x) / 2, | ||
| y: (via.y + pad.y) / 2, | ||
| }, | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| return errors | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
31 changes: 31 additions & 0 deletions
31
tests/lib/check-via-to-pad-spacing/custom-min-spacing.test.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,31 @@ | ||
| import { expect, test } from "bun:test" | ||
| import { checkViaToPadSpacing } from "lib/check-via-to-pad-spacing" | ||
| import type { AnyCircuitElement } from "circuit-json" | ||
|
|
||
| test("respects custom minSpacing parameter", () => { | ||
| const soup: AnyCircuitElement[] = [ | ||
| { | ||
| type: "pcb_via", | ||
| pcb_via_id: "via1", | ||
| x: 0, | ||
| y: 0, | ||
| hole_diameter: 0.3, | ||
| outer_diameter: 0.6, | ||
| layers: ["top", "bottom"], | ||
| }, | ||
| { | ||
| type: "pcb_smtpad", | ||
| pcb_smtpad_id: "pad1", | ||
| shape: "rect", | ||
| x: 1.0, | ||
| y: 0, | ||
| width: 0.4, | ||
| height: 0.3, | ||
| layer: "top", | ||
| }, | ||
| ] | ||
| // Via edge at 0.3, pad left edge at 0.8 => gap = 0.5mm | ||
| // With default 0.2mm: no error. With 0.6mm: error | ||
| expect(checkViaToPadSpacing(soup)).toHaveLength(0) | ||
| expect(checkViaToPadSpacing(soup, { minSpacing: 0.6 })).toHaveLength(1) | ||
| }) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| import { expect, test } from "bun:test" | ||
| import { checkViaToPadSpacing } from "lib/check-via-to-pad-spacing" | ||
| import type { AnyCircuitElement } from "circuit-json" | ||
|
|
||
| test("returns empty array when no pads", () => { | ||
| const soup: AnyCircuitElement[] = [ | ||
| { | ||
| type: "pcb_via", | ||
| pcb_via_id: "via1", | ||
| x: 0, | ||
| y: 0, | ||
| hole_diameter: 0.3, | ||
| outer_diameter: 0.6, | ||
| layers: ["top", "bottom"], | ||
| }, | ||
| ] | ||
| expect(checkViaToPadSpacing(soup)).toHaveLength(0) | ||
| }) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| import { expect, test } from "bun:test" | ||
| import { checkViaToPadSpacing } from "lib/check-via-to-pad-spacing" | ||
| import type { AnyCircuitElement } from "circuit-json" | ||
|
|
||
| test("returns empty array when no vias", () => { | ||
| const soup: AnyCircuitElement[] = [ | ||
| { | ||
| type: "pcb_smtpad", | ||
| pcb_smtpad_id: "pad1", | ||
| shape: "rect", | ||
| x: 0, | ||
| y: 0, | ||
| width: 0.4, | ||
| height: 0.3, | ||
| layer: "top", | ||
| }, | ||
| ] | ||
| expect(checkViaToPadSpacing(soup)).toHaveLength(0) | ||
| }) |
30 changes: 30 additions & 0 deletions
30
tests/lib/check-via-to-pad-spacing/via-close-to-circle-pad.test.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,30 @@ | ||
| import { expect, test } from "bun:test" | ||
| import { checkViaToPadSpacing } from "lib/check-via-to-pad-spacing" | ||
| import type { AnyCircuitElement } from "circuit-json" | ||
|
|
||
| test("no error when via gap to circular SMT pad equals minSpacing", () => { | ||
| const soup: AnyCircuitElement[] = [ | ||
| { | ||
| type: "pcb_via", | ||
| pcb_via_id: "via1", | ||
| x: 0, | ||
| y: 0, | ||
| hole_diameter: 0.3, | ||
| outer_diameter: 0.6, | ||
| layers: ["top", "bottom"], | ||
| }, | ||
| { | ||
| type: "pcb_smtpad", | ||
| pcb_smtpad_id: "pad1", | ||
| shape: "circle", | ||
| x: 0.7, | ||
| y: 0, | ||
| radius: 0.2, | ||
| layer: "top", | ||
| }, | ||
| ] | ||
| // center-to-center = 0.7, via radius = 0.3, pad radius = 0.2 => gap = 0.2mm | ||
| // gap + EPSILON (0.005) >= 0.2mm minSpacing => no error | ||
| const errors = checkViaToPadSpacing(soup) | ||
| expect(errors).toHaveLength(0) | ||
| }) |
33 changes: 33 additions & 0 deletions
33
tests/lib/check-via-to-pad-spacing/via-close-to-plated-hole.test.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,33 @@ | ||
| import { expect, test } from "bun:test" | ||
| import { checkViaToPadSpacing } from "lib/check-via-to-pad-spacing" | ||
| import type { AnyCircuitElement } from "circuit-json" | ||
|
|
||
| test("returns error when via is too close to a plated hole", () => { | ||
| const soup: AnyCircuitElement[] = [ | ||
| { | ||
| type: "pcb_via", | ||
| pcb_via_id: "via1", | ||
| x: 0, | ||
| y: 0, | ||
| hole_diameter: 0.3, | ||
| outer_diameter: 0.6, | ||
| layers: ["top", "bottom"], | ||
| }, | ||
| { | ||
| type: "pcb_plated_hole", | ||
| pcb_plated_hole_id: "hole1", | ||
| shape: "circle", | ||
| x: 0.6, | ||
| y: 0, | ||
| hole_diameter: 0.3, | ||
| outer_diameter: 0.5, | ||
| layers: ["top", "bottom"], | ||
| pcb_component_id: "comp1", | ||
| pcb_port_id: "port1", | ||
| }, | ||
| ] | ||
| // center-to-center = 0.6, via radius = 0.3, hole radius = 0.25 => gap = 0.05mm < 0.2mm | ||
| const errors = checkViaToPadSpacing(soup) | ||
| expect(errors).toHaveLength(1) | ||
| expect(errors[0].message).toContain("too close to pad") | ||
| }) |
29 changes: 29 additions & 0 deletions
29
tests/lib/check-via-to-pad-spacing/via-far-from-pad.test.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,29 @@ | ||
| import { expect, test } from "bun:test" | ||
| import { checkViaToPadSpacing } from "lib/check-via-to-pad-spacing" | ||
| import type { AnyCircuitElement } from "circuit-json" | ||
|
|
||
| test("no error when via is far from pad", () => { | ||
| const soup: AnyCircuitElement[] = [ | ||
| { | ||
| type: "pcb_via", | ||
| pcb_via_id: "via1", | ||
| x: 0, | ||
| y: 0, | ||
| hole_diameter: 0.3, | ||
| outer_diameter: 0.6, | ||
| layers: ["top", "bottom"], | ||
| }, | ||
| { | ||
| type: "pcb_smtpad", | ||
| pcb_smtpad_id: "pad1", | ||
| shape: "rect", | ||
| x: 2, | ||
| y: 0, | ||
| width: 0.4, | ||
| height: 0.3, | ||
| layer: "top", | ||
| }, | ||
| ] | ||
| const errors = checkViaToPadSpacing(soup) | ||
| expect(errors).toHaveLength(0) | ||
| }) |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Documentation bug: Comment on line 15-16 claims the function "returns the half-diagonal (conservative bounding circle)" for rectangular pads, but the implementation at line 27 returns
Math.max(pad.width, pad.height) / 2, not the actual half-diagonal.The correct half-diagonal would be:
For a 1mm × 0.3mm pad:
This underestimates the bounding circle by ~4-5% for rectangular pads. While this fallback path is only used for unknown shapes (the main paths use proper rectangle-to-circle distance), it could still cause missed spacing violations for unsupported pad shapes.
Spotted by Graphite

Is this helpful? React 👍 or 👎 to let us know.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The comment and implementation are now consistent — the current code on line 27 uses
Math.sqrt(pad.width ** 2 + pad.height ** 2) / 2which is the half-diagonal. This was fixed in commit 0c04968. The bot may have been looking at an older version.