Skip to content
Open
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
16 changes: 16 additions & 0 deletions lib/components/primitive-components/SmtPad.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import type { Port } from "./Port"
import { selectPortForPcbPrimitive } from "./Port/selectPortForPcbPrimitive"
import { getAxisAlignedSizeFromRotatedRect } from "lib/utils/pcb/get-axis-aligned-size-from-rotated-rect"
import { getPolygonSolderPasteContours } from "lib/utils/pcb/get-polygon-solder-paste-contours"
import { mapSoldermaskSideMargins } from "lib/utils/pcb/map-soldermask-side-margins"

export class SmtPad extends PrimitiveComponent<typeof smtPadProps> {
pcb_smtpad_id: string | null = null
Expand Down Expand Up @@ -229,6 +230,11 @@ export class SmtPad extends PrimitiveComponent<typeof smtPadProps> {
port_hints: portHints,
is_covered_with_solder_mask: isCoveredWithSolderMask,
soldermask_margin: soldermaskMargin,
...mapSoldermaskSideMargins({
pad: props,
transform: globalTransform,
emittedCcwRotationDegrees: finalRotationDegrees,
}),
subcircuit_id: subcircuit?.subcircuit_id ?? undefined,
pcb_group_id: this.getGroup()?.pcb_group_id ?? undefined,
} as PcbSmtPadRotatedRect) as PcbSmtPadRotatedRect
Expand All @@ -245,6 +251,10 @@ export class SmtPad extends PrimitiveComponent<typeof smtPadProps> {
port_hints: portHints,
is_covered_with_solder_mask: isCoveredWithSolderMask,
soldermask_margin: soldermaskMargin,
...mapSoldermaskSideMargins({
pad: props,
transform: globalTransform,
}),
x: position.x,
y: position.y,
subcircuit_id: subcircuit?.subcircuit_id ?? undefined,
Expand Down Expand Up @@ -306,6 +316,12 @@ export class SmtPad extends PrimitiveComponent<typeof smtPadProps> {
port_hints: portHints,
is_covered_with_solder_mask: isCoveredWithSolderMask,
soldermask_margin: soldermaskMargin,
...mapSoldermaskSideMargins({
pad: props,
transform: globalTransform,
padCcwRotationDegrees: baseRotation,
emittedCcwRotationDegrees: padRotation,
}),
subcircuit_id: subcircuit?.subcircuit_id ?? undefined,
pcb_group_id: this.getGroup()?.pcb_group_id ?? undefined,
} as PcbSmtPadRotatedRect) as PcbSmtPadRotatedRect
Expand Down
63 changes: 63 additions & 0 deletions lib/utils/pcb/map-soldermask-side-margins.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
import type { Matrix } from "transformation-matrix"

type SideMarginKey =
| "soldermask_margin_left"
| "soldermask_margin_right"
| "soldermask_margin_top"
| "soldermask_margin_bottom"

/**
* Maps per-side solder mask margins from the pad's own frame (as written on
* the <smtpad>) to the frame of the emitted pcb_smtpad, which is the board
* frame rotated by the pad's ccw_rotation. Rotation and bottom-side flips
* move which board-facing edge each margin belongs to.
*/
export const mapSoldermaskSideMargins = ({
pad,
transform,
padCcwRotationDegrees = 0,
emittedCcwRotationDegrees = 0,
}: {
pad: {
solderMaskMarginLeft?: number
solderMaskMarginRight?: number
solderMaskMarginTop?: number
solderMaskMarginBottom?: number
}
transform: Matrix
padCcwRotationDegrees?: number
emittedCcwRotationDegrees?: number
}): Partial<Record<SideMarginKey, number>> => {
const result: Partial<Record<SideMarginKey, number>> = {}
const pre = (padCcwRotationDegrees * Math.PI) / 180
const post = (-emittedCcwRotationDegrees * Math.PI) / 180

const sides: Array<[number | undefined, number, number]> = [
[pad.solderMaskMarginLeft, -1, 0],
[pad.solderMaskMarginRight, 1, 0],
[pad.solderMaskMarginTop, 0, 1],
[pad.solderMaskMarginBottom, 0, -1],
]

for (const [value, x, y] of sides) {
if (value === undefined) continue
const px = x * Math.cos(pre) - y * Math.sin(pre)
const py = x * Math.sin(pre) + y * Math.cos(pre)
const bx = transform.a * px + transform.c * py
const by = transform.b * px + transform.d * py
const ex = bx * Math.cos(post) - by * Math.sin(post)
const ey = bx * Math.sin(post) + by * Math.cos(post)

const key: SideMarginKey =
Math.abs(ex) >= Math.abs(ey)
? ex > 0
? "soldermask_margin_right"
: "soldermask_margin_left"
: ey > 0
? "soldermask_margin_top"
: "soldermask_margin_bottom"
result[key] = value
}

return result
}
Loading
Loading