feat(pcb): 실크에 부품 이름표(R1, C4 …) — 한 획 글꼴, 몸체 위·아래·옆·가운데 중 구리와 덜 겹치는 자리 선택, 아랫면 가공 시 동박면에서 바르게 읽히도록 좌우 반전
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
8371f5dab3
commit
87f8f3da6e
2 files changed
+105
-7
No files matched your search
+14
-7
@@ -5,7 +5,7 @@ import { placedPads } from './footprints';
|
||||
import type { MillSettings, PcbProject, Routed } from './types';
|
||||
import { DEFAULT_MILL, JUMPER_PAD, JUMPER_DRILL } from './types';
|
||||
import { layerNames } from './dsn';
|
||||
import { silkLines } from './silk';
|
||||
import { silkLines, labelLines } from './silk';
|
||||
|
||||
// eslint-disable-next-line @typescript-eslint/no-var-requires
|
||||
const ClipperLib: any = require('clipper-lib');
|
||||
@@ -175,25 +175,32 @@ export function buildGcode(p: PcbProject, routed: Routed): GcodeResult {
|
||||
// 실크(부품 윤곽): 동박면에 얕게 새긴다. 배선·패드(+공구 반지름+여유)와 겹치는 부분은 잘라내 배선이 끊기지 않게 한다.
|
||||
if (m.silk !== false && p.parts.length) {
|
||||
const silkLayer = p.rules.layers === 2 ? 'F.Cu' : p.rules.copper;
|
||||
const subj: IP[][] = silkLines(p.parts).map(l => l.map(([x, y]) => ip(x, y)));
|
||||
const copper = offsetClosed(copperUnion(p, routed, silkLayer), m.toolDia / 2 + 0.15);
|
||||
const diff = (() => {
|
||||
const mir = mirrorFor(silkLayer);
|
||||
// 구리 밖 AND 보드 가장자리 0.3mm 안쪽만 남긴다.
|
||||
const clipSilk = (lines: [number, number][][]): IP[][] => {
|
||||
const subj: IP[][] = lines.map(l => l.map(([x, y]) => ip(x, y)));
|
||||
const c = new ClipperLib.Clipper();
|
||||
c.AddPaths(subj, ClipperLib.PolyType.ptSubject, false);
|
||||
c.AddPaths(copper, ClipperLib.PolyType.ptClip, true);
|
||||
const tree = new ClipperLib.PolyTree();
|
||||
c.Execute(ClipperLib.ClipType.ctDifference, tree, ClipperLib.PolyFillType.pftNonZero, ClipperLib.PolyFillType.pftNonZero);
|
||||
const open = (ClipperLib.Clipper.OpenPathsFromPolyTree(tree) as IP[][]).filter(pp => pp.length > 1);
|
||||
const c2 = new ClipperLib.Clipper(); // 보드 가장자리 0.3mm 안쪽으로만
|
||||
const c2 = new ClipperLib.Clipper();
|
||||
c2.AddPaths(open, ClipperLib.PolyType.ptSubject, false);
|
||||
c2.AddPath([ip(0.3, 0.3), ip(W - 0.3, 0.3), ip(W - 0.3, H - 0.3), ip(0.3, H - 0.3)], ClipperLib.PolyType.ptClip, true);
|
||||
const t2 = new ClipperLib.PolyTree();
|
||||
c2.Execute(ClipperLib.ClipType.ctIntersection, t2, ClipperLib.PolyFillType.pftNonZero, ClipperLib.PolyFillType.pftNonZero);
|
||||
return orderPaths((ClipperLib.Clipper.OpenPathsFromPolyTree(t2) as IP[][]).filter(pp => pp.length > 1));
|
||||
})();
|
||||
return (ClipperLib.Clipper.OpenPathsFromPolyTree(t2) as IP[][]).filter(pp => pp.length > 1);
|
||||
};
|
||||
const plen = (ps: IP[][]) => ps.reduce((a, pp) => a + pp.slice(1).reduce((q, v, i) => q + Math.hypot(v.X - pp[i].X, v.Y - pp[i].Y), 0), 0) / S;
|
||||
const rawLen = (ls: [number, number][][]) => ls.reduce((a, l) => a + l.slice(1).reduce((q, v, i) => q + Math.hypot(v[0] - l[i][0], v[1] - l[i][1]), 0), 0);
|
||||
const shapes = silkLines(p.parts);
|
||||
const labels = labelLines(p.parts, W, H, mir, ls => { const t = rawLen(ls); return t > 0 ? plen(clipSilk(ls)) / t : 0; });
|
||||
const diff = orderPaths(clipSilk([...shapes, ...labels]));
|
||||
const inBoard = diff;
|
||||
if (inBoard.length) {
|
||||
const mir = mirrorFor(silkLayer), z = m.silkZ ?? -0.05;
|
||||
const z = m.silkZ ?? -0.05;
|
||||
const L = head(`실크(부품 윤곽) 새기기 (Ø${m.toolDia}mm 비트, 깊이 ${z}mm)`);
|
||||
for (const pp of inBoard) {
|
||||
const pts = pp.map(toMm);
|
||||
|
||||
@@ -72,3 +72,94 @@ export function silkLines(parts: PartInst[]): Line[] {
|
||||
}
|
||||
return out;
|
||||
}
|
||||
|
||||
// ── 부품 이름표(R1, C4, U2 …) — 한 획 글꼴 ───────────────────────────────────────────────
|
||||
// 4×6 격자의 선분 글꼴(y 아래로). 알파벳·숫자만(이름표용).
|
||||
const GLYPH: Record<string, Line[]> = {
|
||||
'0': [[[0, 0], [4, 0], [4, 6], [0, 6], [0, 0]], [[0, 6], [4, 0]]],
|
||||
'1': [[[1, 1], [2, 0], [2, 6]], [[1, 6], [3, 6]]],
|
||||
'2': [[[0, 1], [1, 0], [3, 0], [4, 1], [4, 2], [0, 6], [4, 6]]],
|
||||
'3': [[[0, 0], [4, 0], [2, 3], [4, 4], [4, 5], [3, 6], [1, 6], [0, 5]]],
|
||||
'4': [[[3, 6], [3, 0], [0, 4], [4, 4]]],
|
||||
'5': [[[4, 0], [0, 0], [0, 3], [3, 3], [4, 4], [4, 5], [3, 6], [0, 6]]],
|
||||
'6': [[[4, 0], [1, 0], [0, 1], [0, 5], [1, 6], [3, 6], [4, 5], [4, 4], [3, 3], [0, 3]]],
|
||||
'7': [[[0, 0], [4, 0], [1, 6]]],
|
||||
'8': [[[1, 0], [3, 0], [4, 1], [4, 2], [3, 3], [1, 3], [0, 2], [0, 1], [1, 0]], [[1, 3], [0, 4], [0, 5], [1, 6], [3, 6], [4, 5], [4, 4], [3, 3]]],
|
||||
'9': [[[0, 6], [3, 6], [4, 5], [4, 1], [3, 0], [1, 0], [0, 1], [0, 2], [1, 3], [4, 3]]],
|
||||
A: [[[0, 6], [0, 2], [2, 0], [4, 2], [4, 6]], [[0, 4], [4, 4]]],
|
||||
B: [[[0, 6], [0, 0], [3, 0], [4, 1], [4, 2], [3, 3], [0, 3]], [[3, 3], [4, 4], [4, 5], [3, 6], [0, 6]]],
|
||||
C: [[[4, 1], [3, 0], [1, 0], [0, 1], [0, 5], [1, 6], [3, 6], [4, 5]]],
|
||||
D: [[[0, 0], [0, 6], [3, 6], [4, 5], [4, 1], [3, 0], [0, 0]]],
|
||||
E: [[[4, 0], [0, 0], [0, 6], [4, 6]], [[0, 3], [3, 3]]],
|
||||
F: [[[4, 0], [0, 0], [0, 6]], [[0, 3], [3, 3]]],
|
||||
G: [[[4, 1], [3, 0], [1, 0], [0, 1], [0, 5], [1, 6], [3, 6], [4, 5], [4, 3], [2, 3]]],
|
||||
H: [[[0, 0], [0, 6]], [[4, 0], [4, 6]], [[0, 3], [4, 3]]],
|
||||
I: [[[1, 0], [3, 0]], [[2, 0], [2, 6]], [[1, 6], [3, 6]]],
|
||||
J: [[[4, 0], [4, 5], [3, 6], [1, 6], [0, 5]]],
|
||||
K: [[[0, 0], [0, 6]], [[4, 0], [0, 3], [4, 6]]],
|
||||
L: [[[0, 0], [0, 6], [4, 6]]],
|
||||
M: [[[0, 6], [0, 0], [2, 3], [4, 0], [4, 6]]],
|
||||
N: [[[0, 6], [0, 0], [4, 6], [4, 0]]],
|
||||
O: [[[1, 0], [3, 0], [4, 1], [4, 5], [3, 6], [1, 6], [0, 5], [0, 1], [1, 0]]],
|
||||
P: [[[0, 6], [0, 0], [3, 0], [4, 1], [4, 2], [3, 3], [0, 3]]],
|
||||
Q: [[[1, 0], [3, 0], [4, 1], [4, 5], [3, 6], [1, 6], [0, 5], [0, 1], [1, 0]], [[2, 4], [4, 6]]],
|
||||
R: [[[0, 6], [0, 0], [3, 0], [4, 1], [4, 2], [3, 3], [0, 3]], [[2, 3], [4, 6]]],
|
||||
S: [[[4, 1], [3, 0], [1, 0], [0, 1], [0, 2], [1, 3], [3, 3], [4, 4], [4, 5], [3, 6], [1, 6], [0, 5]]],
|
||||
T: [[[0, 0], [4, 0]], [[2, 0], [2, 6]]],
|
||||
U: [[[0, 0], [0, 5], [1, 6], [3, 6], [4, 5], [4, 0]]],
|
||||
V: [[[0, 0], [2, 6], [4, 0]]],
|
||||
W: [[[0, 0], [1, 6], [2, 3], [3, 6], [4, 0]]],
|
||||
X: [[[0, 0], [4, 6]], [[4, 0], [0, 6]]],
|
||||
Y: [[[0, 0], [2, 3], [4, 0]], [[2, 3], [2, 6]]],
|
||||
Z: [[[0, 0], [4, 0], [0, 6], [4, 6]]],
|
||||
};
|
||||
export const LABEL_H = 1.8;
|
||||
const lineLen = (ls: Line[]) => ls.reduce((a, l) => a + l.slice(1).reduce((s, q, i) => s + Math.hypot(q[0] - l[i][0], q[1] - l[i][1]), 0), 0);
|
||||
|
||||
/** 글자 한 줄을 중심 (cx, cy) 에 만든다. mirror 면 좌우로 뒤집어(아랫면 가공 뒤 반전돼 동박면에서 바르게 읽히게) 만든다. */
|
||||
export function textLines(text: string, cx: number, cy: number, mirror: boolean): { lines: Line[]; w: number; h: number } {
|
||||
const sc = LABEL_H / 6, adv = 5 * sc, chars = [...text.toUpperCase()].filter(ch => GLYPH[ch]);
|
||||
const w = Math.max(0, chars.length * adv - sc), lines: Line[] = [];
|
||||
chars.forEach((ch, i) => {
|
||||
for (const st of GLYPH[ch]) lines.push(st.map(([gx, gy]) => {
|
||||
const lx = (i * 5 + gx) * sc - w / 2;
|
||||
return [cx + (mirror ? -lx : lx), cy + gy * sc - LABEL_H / 2] as Pt;
|
||||
}));
|
||||
});
|
||||
return { lines, w, h: LABEL_H };
|
||||
}
|
||||
|
||||
/**
|
||||
* 부품마다 이름표 자리를 몸체 위·아래·좌·우·(큰 부품은 가운데)에서 골라 새긴다.
|
||||
* keep(lines) = 그 선 중 구리와 겹치지 않고 남는 길이 비율(0~1). 다른 이름표·다른 부품 몸체와 겹치면 감점. 남는 비율이 0.7 미만이면 생략.
|
||||
*/
|
||||
export function labelLines(parts: PartInst[], W: number, H: number, mirror: boolean, keep: (ls: Line[]) => number): Line[] {
|
||||
const boxes = new Map<string, { x0: number; y0: number; x1: number; y1: number }>();
|
||||
for (const part of parts) {
|
||||
const f = getFootprint(part.fp); if (!f) continue;
|
||||
const xs: number[] = [], ys: number[] = [];
|
||||
for (const [x, y] of [[f.body.x0, f.body.y0], [f.body.x1, f.body.y0], [f.body.x1, f.body.y1], [f.body.x0, f.body.y1]]) { const [dx, dy] = rotPoint(x, y, part.rot); xs.push(part.x + dx); ys.push(part.y + dy); }
|
||||
boxes.set(part.ref, { x0: Math.min(...xs), y0: Math.min(...ys), x1: Math.max(...xs), y1: Math.max(...ys) });
|
||||
}
|
||||
const placed: { x0: number; y0: number; x1: number; y1: number }[] = [];
|
||||
const hit = (a: any, b: any) => a.x0 < b.x1 && a.x1 > b.x0 && a.y0 < b.y1 && a.y1 > b.y0;
|
||||
const out: Line[] = [];
|
||||
for (const part of parts) {
|
||||
const bx = boxes.get(part.ref); if (!bx || !part.ref) continue;
|
||||
const t0 = textLines(part.ref, 0, 0, mirror), g = 0.6, cx = (bx.x0 + bx.x1) / 2, cy = (bx.y0 + bx.y1) / 2;
|
||||
const cands: [number, number][] = [[cx, bx.y0 - t0.h / 2 - g], [cx, bx.y1 + t0.h / 2 + g], [bx.x0 - t0.w / 2 - g, cy], [bx.x1 + t0.w / 2 + g, cy]];
|
||||
if (bx.x1 - bx.x0 >= t0.w + 2 && bx.y1 - bx.y0 >= t0.h + 1) cands.push([cx, cy]);
|
||||
let best: { lines: Line[]; score: number; box: any } | null = null;
|
||||
for (const [x, y] of cands) {
|
||||
const box = { x0: x - t0.w / 2 - 0.2, x1: x + t0.w / 2 + 0.2, y0: y - t0.h / 2 - 0.2, y1: y + t0.h / 2 + 0.2 };
|
||||
if (box.x0 < 0.4 || box.y0 < 0.4 || box.x1 > W - 0.4 || box.y1 > H - 0.4) continue;
|
||||
const t = textLines(part.ref, x, y, mirror);
|
||||
let score = keep(t.lines);
|
||||
for (const [r, ob] of boxes) if (r !== part.ref && hit(box, ob)) score -= 0.5;
|
||||
for (const pb of placed) if (hit(box, pb)) score -= 0.5;
|
||||
if (!best || score > best.score) best = { lines: t.lines, score, box };
|
||||
}
|
||||
if (best && best.score >= 0.7 && lineLen(best.lines) > 0) { out.push(...best.lines); placed.push(best.box); }
|
||||
}
|
||||
return out;
|
||||
}
|
||||
Reference in new issue
Block a user