-
Notifications
You must be signed in to change notification settings - Fork 24
Check for vias inside pads #180
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
Merged
Merged
Changes from all commits
Commits
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,75 @@ | ||
| import { getPrimaryId } from "@tscircuit/circuit-json-util" | ||
| import type { AnyCircuitElement, PcbPlacementError, PcbVia } from "circuit-json" | ||
| import { | ||
| type PadElement, | ||
| getPadBounds, | ||
| getPads, | ||
| } from "./check-pad-clearance/common" | ||
| import { isPointInPad } from "./check-traces-are-contiguous/is-point-in-pad" | ||
| import { SpatialObjectIndex } from "./data-structures/SpatialIndex" | ||
| import { getPcbBoard } from "./drc-defaults" | ||
| import { getLayersOfPcbElement } from "./util/getLayersOfPcbElement" | ||
| import { getReadableNameForFootprintPad } from "./util/get-readable-names" | ||
|
|
||
| export function checkViasInPads( | ||
| circuitJson: AnyCircuitElement[], | ||
| ): PcbPlacementError[] { | ||
| const board = getPcbBoard(circuitJson) | ||
| if ( | ||
| board && | ||
| "is_via_in_pad_allowed" in board && | ||
| board.is_via_in_pad_allowed === true | ||
| ) { | ||
| return [] | ||
| } | ||
|
|
||
| const vias = circuitJson.filter( | ||
| (element): element is PcbVia => element.type === "pcb_via", | ||
| ) | ||
| const pads = getPads(circuitJson) | ||
| if (vias.length === 0 || pads.length === 0) return [] | ||
|
|
||
| const padOrdinals = new Map( | ||
| pads.map((pad, index) => [getPrimaryId(pad), index]), | ||
| ) | ||
| const padIndex = new SpatialObjectIndex<PadElement>({ | ||
| objects: pads, | ||
| getBounds: getPadBounds, | ||
| getId: getPrimaryId, | ||
| }) | ||
| const errors: PcbPlacementError[] = [] | ||
|
|
||
| for (const via of vias) { | ||
| const nearbyPads = padIndex.getObjectsInBounds({ | ||
| minX: via.x, | ||
| minY: via.y, | ||
| maxX: via.x, | ||
| maxY: via.y, | ||
| }) | ||
|
|
||
| for (const pad of nearbyPads) { | ||
| const viaLayers = getLayersOfPcbElement(via) | ||
| const padLayers = getLayersOfPcbElement(pad) | ||
| if (!viaLayers.some((layer) => padLayers.includes(layer))) continue | ||
| if (!isPointInPad(via, pad)) continue | ||
|
|
||
| const padId = getPrimaryId(pad) | ||
| const padOrdinal = padOrdinals.get(padId) ?? 0 | ||
| const padName = getReadableNameForFootprintPad( | ||
| circuitJson, | ||
| pad, | ||
| padOrdinal, | ||
| ) | ||
|
|
||
| errors.push({ | ||
| type: "pcb_placement_error", | ||
| pcb_placement_error_id: `via_in_pad_${via.pcb_via_id}_${padId}`, | ||
| error_type: "pcb_placement_error", | ||
| message: `Via at (${via.x.toFixed(2)}mm, ${via.y.toFixed(2)}mm) is inside ${padName}`, | ||
| subcircuit_id: via.subcircuit_id ?? pad.subcircuit_id, | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,157 @@ | ||
| import { expect, test } from "bun:test" | ||
| import type { | ||
| AnyCircuitElement, | ||
| PcbBoard, | ||
| PcbSmtPad, | ||
| PcbVia, | ||
| } from "circuit-json" | ||
| import { checkViasInPads } from "lib/check-vias-in-pads" | ||
| import { runAllPlacementChecks } from "lib/run-all-checks" | ||
| import { containsCircuitJsonId } from "lib/util/get-readable-names" | ||
|
|
||
| const makeBoard = ( | ||
| isViaInPadAllowed?: boolean, | ||
| ): PcbBoard & { is_via_in_pad_allowed?: boolean } => ({ | ||
| type: "pcb_board", | ||
| pcb_board_id: "pcb_board_1", | ||
| center: { x: 0, y: 0 }, | ||
| width: 20, | ||
| height: 20, | ||
| thickness: 1.6, | ||
| num_layers: 4, | ||
| material: "fr4", | ||
| ...(isViaInPadAllowed === undefined | ||
| ? {} | ||
| : { is_via_in_pad_allowed: isViaInPadAllowed }), | ||
| }) | ||
|
|
||
| const rectPad: PcbSmtPad = { | ||
| type: "pcb_smtpad", | ||
| pcb_smtpad_id: "pcb_smtpad_1", | ||
| shape: "rect", | ||
| x: 0, | ||
| y: 0, | ||
| width: 1, | ||
| height: 1, | ||
| layer: "top", | ||
| } | ||
|
|
||
| const viaInRectPad: PcbVia = { | ||
| type: "pcb_via", | ||
| pcb_via_id: "pcb_via_1", | ||
| x: 0.2, | ||
| y: 0.1, | ||
| hole_diameter: 0.2, | ||
| outer_diameter: 0.4, | ||
| layers: ["top", "bottom"], | ||
| } | ||
|
|
||
| test("reports a via whose center is inside an SMD pad", async () => { | ||
| const circuitJson: AnyCircuitElement[] = [makeBoard(), rectPad, viaInRectPad] | ||
|
|
||
| const errors = checkViasInPads(circuitJson) | ||
|
|
||
| expect(errors).toHaveLength(1) | ||
| expect(errors[0]).toMatchObject({ | ||
| type: "pcb_placement_error", | ||
| pcb_placement_error_id: "via_in_pad_pcb_via_1_pcb_smtpad_1", | ||
| error_type: "pcb_placement_error", | ||
| }) | ||
| expect(errors[0].message).toContain("is inside SMD pad") | ||
| expect(containsCircuitJsonId(errors[0].message)).toBe(false) | ||
| expect(await runAllPlacementChecks(circuitJson)).toContainEqual(errors[0]) | ||
| }) | ||
|
|
||
| test("uses true pad geometry for circular, polygon, and plated-hole pads", () => { | ||
| const circuitJson: AnyCircuitElement[] = [ | ||
| makeBoard(), | ||
| { | ||
| type: "pcb_smtpad", | ||
| pcb_smtpad_id: "pcb_smtpad_circle", | ||
| shape: "circle", | ||
| x: -4, | ||
| y: 0, | ||
| radius: 0.5, | ||
| layer: "top", | ||
| }, | ||
| { | ||
| type: "pcb_smtpad", | ||
| pcb_smtpad_id: "pcb_smtpad_polygon", | ||
| shape: "polygon", | ||
| points: [ | ||
| { x: -0.5, y: -0.5 }, | ||
| { x: 0.5, y: -0.5 }, | ||
| { x: 0, y: 0.5 }, | ||
| ], | ||
| layer: "top", | ||
| }, | ||
| { | ||
| type: "pcb_plated_hole", | ||
| pcb_plated_hole_id: "pcb_plated_hole_1", | ||
| shape: "circle", | ||
| x: 4, | ||
| y: 0, | ||
| outer_diameter: 1, | ||
| hole_diameter: 0.5, | ||
| layers: ["top", "bottom"], | ||
| }, | ||
| { | ||
| ...viaInRectPad, | ||
| pcb_via_id: "pcb_via_circle", | ||
| x: -4.2, | ||
| y: 0, | ||
| }, | ||
| { | ||
| ...viaInRectPad, | ||
| pcb_via_id: "pcb_via_polygon", | ||
| x: 0, | ||
| y: 0, | ||
| }, | ||
| { | ||
| ...viaInRectPad, | ||
| pcb_via_id: "pcb_via_plated_hole", | ||
| x: 4, | ||
| y: 0, | ||
| }, | ||
| ] | ||
|
|
||
| expect(checkViasInPads(circuitJson)).toHaveLength(3) | ||
| }) | ||
|
|
||
| test("ignores vias outside pads and vias on non-overlapping layers", () => { | ||
| const circuitJson: AnyCircuitElement[] = [ | ||
| makeBoard(), | ||
| rectPad, | ||
| { | ||
| ...viaInRectPad, | ||
| pcb_via_id: "pcb_via_outside", | ||
| x: 2, | ||
| y: 2, | ||
| }, | ||
| { | ||
| ...viaInRectPad, | ||
| pcb_via_id: "pcb_via_inner_layers", | ||
| x: 0, | ||
| y: 0, | ||
| layers: ["inner1", "inner2"], | ||
| }, | ||
| ] | ||
|
|
||
| expect(checkViasInPads(circuitJson)).toEqual([]) | ||
| }) | ||
|
|
||
| test("respects the board via-in-pad allowance", () => { | ||
| const allowedCircuit: AnyCircuitElement[] = [ | ||
| makeBoard(true), | ||
| rectPad, | ||
| viaInRectPad, | ||
| ] | ||
| const disallowedCircuit: AnyCircuitElement[] = [ | ||
| makeBoard(false), | ||
| rectPad, | ||
| viaInRectPad, | ||
| ] | ||
|
|
||
| expect(checkViasInPads(allowedCircuit)).toEqual([]) | ||
| expect(checkViasInPads(disallowedCircuit)).toHaveLength(1) | ||
| }) | ||
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.
This file contains 4
test(...)calls (at lines 49, 65, 121, and 143), but the style guide requires that a*.test.tsfile may have AT MOST onetest(...). After the first test, the remaining tests should be split into separate, numbered files. For example:check-vias-in-pads1.test.ts,check-vias-in-pads2.test.ts,check-vias-in-pads3.test.ts, andcheck-vias-in-pads4.test.ts.Spotted by Graphite (based on custom rule: Custom rule)

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