Skip to content
Draft
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
7 changes: 5 additions & 2 deletions src/fn/res.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { AnySoupElement } from "circuit-json"
import { passive, type PassiveDef } from "../helpers/passive-fn"
import { type PassiveDef, passive } from "../helpers/passive-fn"
import { res0402Array2 } from "../helpers/res0402-array2"
import { res0402Array4 } from "../helpers/res0402-array4"
import { res0603Array2 } from "../helpers/res0603-array2"
Expand Down Expand Up @@ -87,5 +87,8 @@ export const res = (
}
}

return { circuitJson: passive(rawParameters), parameters: rawParameters }
return {
circuitJson: passive(rawParameters),
parameters: rawParameters,
}
}
25 changes: 14 additions & 11 deletions src/helpers/passive-fn.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import mm from "@tscircuit/mm"
import type {
AnyCircuitElement,
PcbCourtyardRect,
PcbSilkscreenPath,
} from "circuit-json"
import { distance, length } from "circuit-json"
import { z } from "zod"
import { rectpad } from "../helpers/rectpad"
import mm from "@tscircuit/mm"
import { platedhole } from "./platedhole"
import { z } from "zod"
import { length, distance } from "circuit-json"
import { type SilkscreenRef, silkscreenRef } from "./silkscreenRef"
import { base_def } from "./zod/base_def"

Expand Down Expand Up @@ -342,29 +342,32 @@ export const passive = (params: PassiveDef): AnyCircuitElement[] => {

const textY = textbottom ? -ph / 2 - 0.9 : ph / 2 + 0.9
const silkscreenRefText: SilkscreenRef = silkscreenRef(0, textY, 0.2)
const courtyard =
sz?.courtyard_width_mm && sz.courtyard_height_mm
? createCourtyardRect(sz.courtyard_width_mm, sz.courtyard_height_mm)
: null
const platedHoleOuterDiameter = pw / 0.8
const padWidth = tht ? platedHoleOuterDiameter : pw
const padHeight = tht ? platedHoleOuterDiameter : ph
const courtyard = createCourtyardRect(
sz?.courtyard_width_mm ?? Math.max(p + padWidth, w ?? 0),
sz?.courtyard_height_mm ?? Math.max(padHeight, h ?? 0),
)
const shouldRoundPads = roundedPads ?? sz?.rounded_pads ?? false
const cornerRadius = shouldRoundPads
? Math.min(0.125, Math.min(pw, ph) / 8)
: undefined

if (tht) {
return [
platedhole(1, -p / 2, 0, pw, (pw * 1) / 0.8),
platedhole(2, p / 2, 0, pw, (pw * 1) / 0.8),
platedhole(1, -p / 2, 0, pw, platedHoleOuterDiameter),
platedhole(2, p / 2, 0, pw, platedHoleOuterDiameter),
...silkscreenLines,
silkscreenRefText,
...(courtyard ? [courtyard] : []),
courtyard,
]
}
return [
rectpad(["1", "left"], -p / 2, 0, pw, ph, cornerRadius),
rectpad(["2", "right"], p / 2, 0, pw, ph, cornerRadius),
...silkscreenLines,
silkscreenRefText,
...(courtyard ? [courtyard] : []),
courtyard,
]
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
13 changes: 13 additions & 0 deletions tests/cap.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,3 +46,16 @@ test("cap 2512", () => {
const svgContent = convertCircuitJsonToPcbSvg(soup)
expect(svgContent).toMatchSvgSnapshot(import.meta.path, "cap_2512")
})

test("custom capacitor footprint derives its courtyard from pad dimensions", () => {
const soup = fp.string("cap_p2mm_pw0.8mm_ph1.2mm").circuitJson()
const courtyard = soup.find(
(element) => element.type === "pcb_courtyard_rect",
)

expect(courtyard).toMatchObject({
center: { x: 0, y: 0 },
width: 2.8,
height: 1.2,
})
})
21 changes: 15 additions & 6 deletions tests/res.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { test, expect } from "bun:test"
import { expect, test } from "bun:test"
import { convertCircuitJsonToPcbSvg } from "circuit-to-svg"
import { fp } from "../src/footprinter"

Expand Down Expand Up @@ -68,12 +68,21 @@ test("1206_x4", () => {
expect(svgContent).toMatchSvgSnapshot(import.meta.path, "1206_x4")
})

test("custom passive footprints do not get implicit courtyards", () => {
const soup = fp().res().p("1.1mm").pw("0.5mm").ph("0.7mm").circuitJson()
test("custom resistor footprint derives its courtyard from pad dimensions", () => {
const soup = fp.string("res_p1.9324mm_pw1.1325mm_ph1.377mm").circuitJson()
const courtyard = soup.find(
(element) => element.type === "pcb_courtyard_rect",
)

expect(
soup.some((element) => String(element.type).startsWith("pcb_courtyard")),
).toBe(false)
expect(courtyard?.center).toEqual({ x: 0, y: 0 })
expect(courtyard?.width).toBeCloseTo(1.9324 + 1.1325)
expect(courtyard?.height).toBeCloseTo(1.377)

const svgContent = convertCircuitJsonToPcbSvg(soup, { showCourtyards: true })
expect(svgContent).toMatchSvgSnapshot(
import.meta.path,
"res_p1.9324mm_pw1.1325mm_ph1.377mm",
)
})

test("0402 uses the explicit KiCad courtyard", () => {
Expand Down
Loading