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
113 changes: 100 additions & 13 deletions src/helpers/boolean-difference.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,79 @@ interface FootprintElement {
route?: Array<{ x: number; y: number }>
points?: Array<{ x: number; y: number }>
shape?: string
corner_radius?: number | null
radius?: number
}

function createRoundedRectPolygon(
x: number,
y: number,
width: number,
height: number,
cornerRadius: number,
): Flatten.Polygon {
const halfWidth = width / 2
const halfHeight = height / 2
const radius = Math.min(Math.max(cornerRadius, 0), halfWidth, halfHeight)

if (radius === 0) {
return new Flatten.Polygon([
[x - halfWidth, y - halfHeight],
[x + halfWidth, y - halfHeight],
[x + halfWidth, y + halfHeight],
[x - halfWidth, y + halfHeight],
])
}

const left = x - halfWidth
const right = x + halfWidth
const bottom = y - halfHeight
const top = y + halfHeight
const point = (px: number, py: number) => new Flatten.Point(px, py)

return new Flatten.Polygon([
new Flatten.Segment(
point(left + radius, bottom),
point(right - radius, bottom),
),
new Flatten.Arc(
point(right - radius, bottom + radius),
radius,
-Math.PI / 2,
0,
true,
),
new Flatten.Segment(
point(right, bottom + radius),
point(right, top - radius),
),
new Flatten.Arc(
point(right - radius, top - radius),
radius,
0,
Math.PI / 2,
true,
),
new Flatten.Segment(point(right - radius, top), point(left + radius, top)),
new Flatten.Arc(
point(left + radius, top - radius),
radius,
Math.PI / 2,
Math.PI,
true,
),
new Flatten.Segment(
point(left, top - radius),
point(left, bottom + radius),
),
new Flatten.Arc(
point(left + radius, bottom + radius),
radius,
Math.PI,
(3 * Math.PI) / 2,
true,
),
])
}

/**
Expand All @@ -39,33 +112,47 @@ function elementToPolygon(element: FootprintElement): Flatten.Polygon | null {
try {
if (
element.type === "pcb_smtpad" &&
element.width &&
element.height &&
element.shape === "circle" &&
element.x !== undefined &&
element.y !== undefined
) {
// Create rectangular pad polygon
const halfWidth = element.width / 2
const halfHeight = element.height / 2
const points = [
new Flatten.Point(element.x - halfWidth, element.y - halfHeight),
new Flatten.Point(element.x + halfWidth, element.y - halfHeight),
new Flatten.Point(element.x + halfWidth, element.y + halfHeight),
new Flatten.Point(element.x - halfWidth, element.y + halfHeight),
]
return new Flatten.Polygon(points)
const radius =
element.radius ?? Math.min(element.width ?? 0, element.height ?? 0) / 2

return radius > 0
? new Flatten.Polygon(
new Flatten.Circle(new Flatten.Point(element.x, element.y), radius),
)
: null
}

if (
element.type === "pcb_smtpad" &&
element.shape === "polygon" &&
element.points &&
element.points.length > 0
) {
// Handle polygon-shaped SMT pads
const points = element.points.map((p) => new Flatten.Point(p.x, p.y))
return new Flatten.Polygon(points)
}

if (
element.type === "pcb_smtpad" &&
(element.shape === "rect" || element.shape === undefined) &&
element.width &&
element.height &&
element.x !== undefined &&
element.y !== undefined
) {
return createRoundedRectPolygon(
element.x,
element.y,
element.width,
element.height,
element.corner_radius ?? 0,
)
}

if (
element.type === "pcb_plated_hole" &&
element.outer_diameter &&
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Loading