From 9f2b5f691b67d384ff490ab8c45b8e9f8f915281 Mon Sep 17 00:00:00 2001 From: kim Date: Wed, 21 Oct 2026 12:44:36 +0900 Subject: [PATCH] =?UTF-8?q?=EC=A1=B0=EB=A6=BD=20=EC=84=A4=EB=AA=85?= =?UTF-8?q?=EC=84=9C=20=EA=B7=B8=EB=A6=BC=EC=97=90=20=EB=B6=80=ED=92=88=20?= =?UTF-8?q?=EB=B2=94=EB=A1=80(=EB=B2=88=ED=98=B8=20=ED=92=8D=EC=84=A0+?= =?UTF-8?q?=EB=B6=80=ED=92=88=20=EC=9D=B4=EB=A6=84)=20=EC=B6=94=EA=B0=80?= =?UTF-8?q?=20=E2=80=94=20=EC=99=84=EC=84=B1/=EB=B6=84=ED=95=B4=EB=8F=84?= =?UTF-8?q?=EB=8A=94=20=EC=A0=84=EC=B2=B4=20=EB=B6=80=ED=92=88,=20?= =?UTF-8?q?=EB=8B=A8=EA=B3=84=20=EA=B7=B8=EB=A6=BC=EC=9D=80=20=EA=B7=B8=20?= =?UTF-8?q?=EB=8B=A8=EA=B3=84=20=EB=B6=80=ED=92=88?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-Authored-By: Claude Opus 5.5 --- src/tools/workshop-assembly-render.ts | 68 +++++++++++++++++++++++++++ 1 file changed, 68 insertions(+) diff --git a/src/tools/workshop-assembly-render.ts b/src/tools/workshop-assembly-render.ts index 79cb154..1dd80e6 100644 --- a/src/tools/workshop-assembly-render.ts +++ b/src/tools/workshop-assembly-render.ts @@ -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 { + 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 { 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 */ } }