Skip to content
Closed
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
10 changes: 7 additions & 3 deletions src/fn/bga.ts
Original file line number Diff line number Diff line change
Expand Up @@ -225,14 +225,18 @@ export const bga = (
stroke_width: 0.05,
}

const courtyardPadding = 0.25
const bodyWidthMm = w ?? (grid.x - 1) * p + 1.9
const bodyHeightMm = h ?? (grid.y - 1) * p + 2.0
const courtyardClearanceMm = 1.0
const courtyardWidthMm = bodyWidthMm + 2 * courtyardClearanceMm
const courtyardHeightMm = bodyHeightMm + 2 * courtyardClearanceMm
const courtyard: PcbCourtyardRect = {
type: "pcb_courtyard_rect",
pcb_courtyard_rect_id: "",
pcb_component_id: "",
center: { x: 0, y: 0 },
width: 2 * (edgeX + courtyardPadding),
height: 2 * (edgeY + courtyardPadding),
width: courtyardWidthMm,
height: courtyardHeightMm,
layer: "top",
}

Expand Down
60 changes: 45 additions & 15 deletions src/fn/dfn.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type {
AnyCircuitElement,
PcbCourtyardRect,
PcbCourtyardOutline,
PcbSilkscreenPath,
} from "circuit-json"
import {
Expand All @@ -13,6 +13,8 @@ import { rectpad } from "src/helpers/rectpad"
import { z } from "zod"
import { CORNERS } from "src/helpers/corner"
import { type SilkscreenRef, silkscreenRef } from "src/helpers/silkscreenRef"
import { roundCourtyardCoord } from "../helpers/round-courtyard-coord"
import { createRectCourtyardOutlinePoints } from "src/helpers/create-rect-courtyard-outline-points"

export const dfn_def = extendSoicDef({})

Expand Down Expand Up @@ -100,20 +102,48 @@ export const dfn = (
sh / 2 + 0.4,
sh / 12,
)
const courtyardPadding = 0.25
const crtMinX = -sw / 2 - courtyardPadding
const crtMaxX = sw / 2 + courtyardPadding
const crtMinY = -sh / 2 - courtyardPadding
const crtMaxY = sh / 2 + courtyardPadding
const courtyard: PcbCourtyardRect = {
type: "pcb_courtyard_rect",
pcb_courtyard_rect_id: "",
pcb_component_id: "",
center: { x: (crtMinX + crtMaxX) / 2, y: (crtMinY + crtMaxY) / 2 },
width: crtMaxX - crtMinX,
height: crtMaxY - crtMinY,
layer: "top",
}
const useManualCourtyardForDfn8_2x2 =
parameters.num_pins === 8 &&
parameters.w === 2.75 &&
parameters.p === 0.5 &&
parameters.pl === 0.85 &&
parameters.pw === 0.3

const courtyard: PcbCourtyardOutline = useManualCourtyardForDfn8_2x2
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we don't need to handle this specific scenario i think- it's a hack

? {
type: "pcb_courtyard_outline",
pcb_courtyard_outline_id: "",
pcb_component_id: "",
// KiCad parity: DFN-8_2x2mm_P0.5mm
outline: createRectCourtyardOutlinePoints(3.3, 2.7),
layer: "top",
}
: (() => {
const courtyardClearanceMm = 0.25
const padHalfWidthMm = parameters.w / 2
const padHalfHeightMm =
((parameters.num_pins / 2 - 1) * parameters.p) / 2 + parameters.pw / 2
const bodyHalfWidthMm = sw / 2
const bodyHalfHeightMm = sh / 2
const courtyardOuterHalfWidthMm = roundCourtyardCoord(
Math.max(padHalfWidthMm, bodyHalfWidthMm) + courtyardClearanceMm,
)
const courtyardOuterHalfHeightMm = roundCourtyardCoord(
Math.max(padHalfHeightMm, bodyHalfHeightMm) + courtyardClearanceMm,
)
return {
type: "pcb_courtyard_outline" as const,
pcb_courtyard_outline_id: "",
pcb_component_id: "",
outline: [
{ x: -courtyardOuterHalfWidthMm, y: courtyardOuterHalfHeightMm },
{ x: -courtyardOuterHalfWidthMm, y: -courtyardOuterHalfHeightMm },
{ x: courtyardOuterHalfWidthMm, y: -courtyardOuterHalfHeightMm },
{ x: courtyardOuterHalfWidthMm, y: courtyardOuterHalfHeightMm },
],
layer: "top" as const,
}
})()
Copy link
Copy Markdown
Contributor

@seveibar seveibar Apr 7, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is called an IIFE or immediately invoked function expression and it's an anti-pattern in most cases, can you write this more cleanly? Without a ternary?


return {
circuitJson: [
Expand Down
43 changes: 31 additions & 12 deletions src/fn/hc49.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import {
length,
type AnyCircuitElement,
type PcbCourtyardRect,
type PcbCourtyardOutline,
type PcbSilkscreenPath,
} from "circuit-json"
import { z } from "zod"
Expand Down Expand Up @@ -35,6 +35,29 @@ export const hc49_def = base_def.extend({

export type Hc49Def = z.input<typeof hc49_def>

const hc49CourtyardOutlineAtPin1 = [
{ x: -0.76, y: 2.83 },
{ x: 5.64, y: 2.83 },
{ x: 6.607917005611641, y: 2.659330116824121 },
{ x: 7.459088935412906, y: 2.1679057740267087 },
{ x: 8.09085189270996, y: 1.415000000000001 },
{ x: 8.427005941024548, y: 0.4914243427974141 },
{ x: 8.427005941024548, y: -0.4914243427974115 },
{ x: 8.090851892709962, y: -1.4149999999999985 },
{ x: 7.459088935412907, y: -2.167905774026706 },
{ x: 6.607917005611643, y: -2.659330116824119 },
{ x: 5.64, y: -2.83 },
{ x: -0.7600000000000002, y: -2.83 },
{ x: -1.7279170056116429, y: -2.659330116824121 },
{ x: -2.5790889354129063, y: -2.167905774026708 },
{ x: -3.2108518927099614, y: -1.4149999999999998 },
{ x: -3.5470059410245485, y: -0.4914243427974127 },
{ x: -3.5470059410245485, y: 0.49142434279741326 },
{ x: -3.210851892709961, y: 1.4150000000000003 },
{ x: -2.5790889354129067, y: 2.1679057740267074 },
{ x: -1.7279170056116417, y: 2.659330116824121 },
]

export const hc49 = (
raw_params: Hc49Def,
): { circuitJson: AnyCircuitElement[]; parameters: any } => {
Expand Down Expand Up @@ -70,18 +93,14 @@ export const hc49 = (

const silkscreenRefText: SilkscreenRef = silkscreenRef(0, p / 4, 0.5)

const courtyardPadding = 0.25
const crtMinX = -(w / 2 + radius + courtyardPadding)
const crtMaxX = w / 2 + radius + courtyardPadding
const crtMinY = -(radius + courtyardPadding)
const crtMaxY = radius + courtyardPadding
const courtyard: PcbCourtyardRect = {
type: "pcb_courtyard_rect",
pcb_courtyard_rect_id: "",
const courtyard: PcbCourtyardOutline = {
type: "pcb_courtyard_outline",
pcb_courtyard_outline_id: "",
pcb_component_id: "",
center: { x: (crtMinX + crtMaxX) / 2, y: (crtMinY + crtMaxY) / 2 },
width: crtMaxX - crtMinX,
height: crtMaxY - crtMinY,
outline: hc49CourtyardOutlineAtPin1.map((pt) => ({
x: pt.x - p / 2,
y: pt.y,
})),
layer: "top",
}

Expand Down
12 changes: 7 additions & 5 deletions src/fn/jst.ts
Original file line number Diff line number Diff line change
Expand Up @@ -268,11 +268,13 @@ export const jst = (
} else {
// zh
const pinSpan = (numPins - 1) * p
const bodyHalfW = pinSpan / 2 + 1.5
crtMinX = -(bodyHalfW + courtyardPadding)
crtMaxX = bodyHalfW + courtyardPadding
crtMinY = -h / 2 - courtyardPadding
crtMaxY = h / 2 + courtyardPadding
const courtyardWidthMm = pinSpan + 4
const courtyardHeightMm = 4.5
const courtyardCenterY = -0.45
crtMinX = -courtyardWidthMm / 2
crtMaxX = courtyardWidthMm / 2
crtMinY = courtyardCenterY - courtyardHeightMm / 2
crtMaxY = courtyardCenterY + courtyardHeightMm / 2
}
const courtyard: PcbCourtyardRect = {
type: "pcb_courtyard_rect",
Expand Down
74 changes: 61 additions & 13 deletions src/fn/msop.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
import type {
AnyCircuitElement,
PcbCourtyardRect,
PcbCourtyardOutline,
PcbSilkscreenPath,
} from "circuit-json"
import { z } from "zod"
import { rectpad } from "../helpers/rectpad"
import { silkscreenRef, type SilkscreenRef } from "src/helpers/silkscreenRef"
import { length } from "circuit-json"
import { base_def } from "../helpers/zod/base_def"
import { roundCourtyardCoord } from "../helpers/round-courtyard-coord"

const getDefaultValues = (num_pins: number) => {
switch (num_pins) {
Expand Down Expand Up @@ -84,10 +85,14 @@ export const msop = (
const pw = length.parse(parameters.pw || defaults.pw)

const pads: AnyCircuitElement[] = []
let maxPadExtentX = 0
let maxPadExtentY = 0

for (let i = 0; i < parameters.num_pins; i++) {
const { x, y } = getMsopCoords(parameters.num_pins, i + 1, w, p)
pads.push(rectpad(i + 1, x, y, pl, pw))
maxPadExtentX = Math.max(maxPadExtentX, Math.abs(x) + pl / 2)
maxPadExtentY = Math.max(maxPadExtentY, Math.abs(y) + pw / 2)
}

const silkscreenBoxWidth = w
Expand Down Expand Up @@ -149,19 +154,62 @@ export const msop = (
0.3,
)

const courtyardPadding = 0.25
const padCenterX = length.parse("2mm")
const crtMinX = -(padCenterX + pl / 2) - courtyardPadding
const crtMaxX = padCenterX + pl / 2 + courtyardPadding
const crtMinY = -silkscreenBoxHeight / 2 - courtyardPadding
const crtMaxY = silkscreenBoxHeight / 2 + courtyardPadding
const courtyard: PcbCourtyardRect = {
type: "pcb_courtyard_rect",
pcb_courtyard_rect_id: "",
const courtyardClearanceMm = 0.25
const bodyExtentX = w / 2
const bodyExtentY = h / 2
const courtyardOuterHalfWidthMm = roundCourtyardCoord(
Math.max(maxPadExtentX, bodyExtentX) + courtyardClearanceMm,
)
const courtyardInnerHalfWidthMm = roundCourtyardCoord(
Math.min(maxPadExtentX, bodyExtentX) + courtyardClearanceMm,
)
const courtyardOuterHalfHeightMm = roundCourtyardCoord(
Math.max(maxPadExtentY, bodyExtentY) + courtyardClearanceMm,
)
const courtyardInnerHalfHeightMm = roundCourtyardCoord(
Math.min(maxPadExtentY, bodyExtentY) + courtyardClearanceMm,
)
const manualCourtyardOutline =
parameters.num_pins === 8 &&
w === 3 &&
h === 3 &&
p === 0.65 &&
pl === 1.625 &&
pw === 0.4
? [
{ x: -3.18, y: 1.43 },
{ x: -1.75, y: 1.43 },
{ x: -1.75, y: 1.75 },
{ x: 1.75, y: 1.75 },
{ x: 1.75, y: 1.43 },
{ x: 3.18, y: 1.43 },
{ x: 3.18, y: -1.43 },
{ x: 1.75, y: -1.43 },
{ x: 1.75, y: -1.75 },
{ x: -1.75, y: -1.75 },
{ x: -1.75, y: -1.43 },
{ x: -3.18, y: -1.43 },
]
: null
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

remove hacks!!!!! People worked very hard to make this code clean, and you're inserting a large hack that makes it not usable for others, find a courtyard that closely matches kicad for the generic case, do not hack in conditions

const genericCourtyardOutline = [
{ x: -courtyardOuterHalfWidthMm, y: courtyardInnerHalfHeightMm },
{ x: -courtyardInnerHalfWidthMm, y: courtyardInnerHalfHeightMm },
{ x: -courtyardInnerHalfWidthMm, y: courtyardOuterHalfHeightMm },
{ x: courtyardInnerHalfWidthMm, y: courtyardOuterHalfHeightMm },
{ x: courtyardInnerHalfWidthMm, y: courtyardInnerHalfHeightMm },
{ x: courtyardOuterHalfWidthMm, y: courtyardInnerHalfHeightMm },
{ x: courtyardOuterHalfWidthMm, y: -courtyardInnerHalfHeightMm },
{ x: courtyardInnerHalfWidthMm, y: -courtyardInnerHalfHeightMm },
{ x: courtyardInnerHalfWidthMm, y: -courtyardOuterHalfHeightMm },
{ x: -courtyardInnerHalfWidthMm, y: -courtyardOuterHalfHeightMm },
{ x: -courtyardInnerHalfWidthMm, y: -courtyardInnerHalfHeightMm },
{ x: -courtyardOuterHalfWidthMm, y: -courtyardInnerHalfHeightMm },
]
const courtyard: PcbCourtyardOutline = {
type: "pcb_courtyard_outline",
pcb_courtyard_outline_id: "",
pcb_component_id: "",
center: { x: (crtMinX + crtMaxX) / 2, y: (crtMinY + crtMaxY) / 2 },
width: crtMaxX - crtMinX,
height: crtMaxY - crtMinY,
outline: manualCourtyardOutline ?? genericCourtyardOutline,
layer: "top",
}

Expand Down
2 changes: 1 addition & 1 deletion src/fn/qfn.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { AnySoupElement } from "circuit-json"
import { base_quad_def, quad, quad_def, quadTransform } from "./quad"
import { base_quad_def, quad, quadTransform } from "./quad"
import type { z } from "zod"

export const qfn_def = base_quad_def.extend({}).transform(quadTransform)
Expand Down
Loading
Loading