조립 설명서 그림에 부품 범례(번호 풍선+부품 이름) 추가 — 완성/분해도는 전체 부품, 단계 그림은 그 단계 부품

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
kimandClaude Opus 5.5 committed 2026-10-21 12:44:36 +09:00
1 parent 2fad885bb9
commit 9f2b5f691b
1 file changed
+68
+68
View File
@@ -131,6 +131,60 @@ if boxes:
im.crop((max(0, x0), max(0, y0), min(W, x1), min(H, y1))).save(f)
`;
// 부품 범례: 잘라 낸 그림 오른쪽에 "번호 풍선 + 부품 이름" 목록 칸을 붙인다(그림 속 풍선 번호가 무슨 부품인지 그림만 봐도 알게).
// 칸 너비는 전체 부품 이름 중 가장 긴 것 기준(width_names)이라 단계 그림끼리 크기가 같다. 목록이 그림보다 길면 아래로 늘린다.
const LEGEND_SCRIPT = `
import sys, json
from PIL import Image, ImageDraw, ImageFont
job = json.load(open(sys.argv[1]))
def F(sz):
for fp in ('/usr/share/fonts/truetype/nanum/NanumSquareRoundB.ttf', '/usr/share/fonts/truetype/nanum/NanumGothicBold.ttf', '/usr/share/fonts/truetype/dejavu/DejaVuSans-Bold.ttf'):
try:
return ImageFont.truetype(fp, sz)
except Exception:
pass
return ImageFont.load_default()
fn, fb, fh = F(16), F(14), F(15)
tmp = ImageDraw.Draw(Image.new('RGB', (1, 1)))
tw = max([tmp.textlength(n, font=fn) for n in job['width_names']] + [80])
ROW, PAD, R = 30, 16, 12
pw = int(PAD + 2 * R + 10 + tw + PAD)
for f in job['files']:
im = Image.open(f['path']).convert('RGB')
W, H = im.size
rows = f['rows']
need = PAD + 26 + len(rows) * ROW + PAD
out = Image.new('RGB', (W + pw, max(H, need)), (255, 255, 255))
out.paste(im, (0, 0))
d = ImageDraw.Draw(out)
x0 = W
d.line((x0 + 4, PAD, x0 + 4, max(H, need) - PAD), fill=(226, 230, 236), width=2)
d.text((x0 + PAD, PAD), job.get('title', '부품'), fill=(107, 119, 133), font=fh)
y = PAD + 26
for n, name in rows:
cx, cy = x0 + PAD + R, y + ROW // 2
d.ellipse((cx - R, cy - R, cx + R, cy + R), fill=(245, 158, 11))
t = str(n)
bb = d.textbbox((0, 0), t, font=fb)
d.text((cx - (bb[2] - bb[0]) / 2 - bb[0], cy - (bb[3] - bb[1]) / 2 - bb[1]), t, fill=(255, 255, 255), font=fb)
bb = d.textbbox((0, 0), name, font=fn)
d.text((cx + R + 10, cy - (bb[3] - bb[1]) / 2 - bb[1]), name, fill=(31, 41, 55), font=fn)
y += ROW
out.save(f['path'])
`;
function runLegend(job: { title: string; width_names: string[]; files: { path: string; rows: [number, string][] }[] }, tmpDir: string): Promise<void> {
return new Promise((resolve) => {
if (!job.files.length) return resolve();
const jobPath = path.join(tmpDir, `legend${Date.now()}.json`);
fs.writeFileSync(jobPath, JSON.stringify(job));
const proc = spawn('python3', ['-c', LEGEND_SCRIPT, jobPath], { stdio: 'ignore' });
const timer = setTimeout(() => { try { proc.kill('SIGKILL'); } catch { /* noop */ } }, 60_000);
proc.on('error', () => { clearTimeout(timer); resolve(); }); // 범례 실패는 치명적이지 않다(풍선 번호만 남음)
proc.on('close', () => { clearTimeout(timer); resolve(); });
});
}
function runCrop(files: string[], tmpDir: string, pad = 28): Promise<void> {
return new Promise((resolve) => {
if (!files.length) return resolve();
@@ -326,6 +380,20 @@ async function doRender(workspace: string, projectId: string, assembly: Assembly
await runCrop([path.join(outDir, 'overview.png')], tmp);
await runCrop([path.join(outDir, 'exploded.png')], tmp);
// 2.6) 부품 범례(번호+이름) — 완성/분해도는 전체 부품, 단계 그림은 그 단계에 붙이는 부품
const row = (p: AssemblyPart): [number, string] => [numberOf.get(p.id)!, p.qty > 1 ? `${p.name} × ${p.qty}` : p.name];
const allRows = assembly.parts.map(row);
await runLegend({
title: '부품',
width_names: allRows.map(r => r[1]),
files: [
{ path: path.join(outDir, 'overview.png'), rows: allRows },
{ path: path.join(outDir, 'exploded.png'), rows: allRows },
...stepFiles.map((f, i) => ({ path: path.join(outDir, f), rows: (sets[i]?.added || []).map(row) }))
.filter(f => f.rows.length),
],
}, tmp);
// 3) 옛 단계 그림 정리(단계 수가 줄었을 때)
for (const f of fs.readdirSync(outDir)) {
if (/^step-\d+\.png$/.test(f) && !stepFiles.includes(f)) { try { fs.unlinkSync(path.join(outDir, f)); } catch { /* noop */ } }