fix(stl-cad): 구멍·불리언(cut 등) 결과를 바이너리 STL로 내보내고, 없는 출력 폴더도 만든다 — OpenSCAD 기본 ASCII 출력이라 썸네일·바닥 정렬이 안 되던 문제, 새 폴더 cut 시 ENOENT

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
kimandClaude Sonnet 5.5 committed 2026-09-30 14:43:36 +09:00
1 parent 6b6f44cfe3
commit 31313319b1
2 files changed
+45 -3

No files matched your search

+9 -3
View File
@@ -268,8 +268,9 @@ export async function transformStl(opts: {
// 항상 임시 STL로 뽑고 rename으로 교체한다(원자적).
const tmpOut = outPath.replace(/\.stl$/i, `.transform-tmp-${Date.now()}.stl`);
const scadPath = tmpOut.replace(/\.stl$/i, '.scad');
fs.writeFileSync(scadPath, body);
// 출력 폴더가 아직 없을 수 있다 — 임시 .scad도 그 폴더에 쓰므로 만든 뒤에 써야 한다(2026-09-30, 새 폴더로 cut하면 ENOENT).
fs.mkdirSync(path.dirname(tmpOut), { recursive: true });
fs.writeFileSync(scadPath, body);
const render = await runOpenscad(['-o', tmpOut, '--export-format=binstl', scadPath], 120_000);
try { fs.unlinkSync(scadPath); } catch { /* noop */ }
if (!render.ok) {
@@ -309,6 +310,7 @@ export async function renderStlPng(stlPath: string, view: string, outPngPath: st
];
const scadPath = outPngPath.replace(/\.png$/i, '.scad');
fs.mkdirSync(path.dirname(scadPath), { recursive: true });
fs.writeFileSync(scadPath, `import(${JSON.stringify(stlPath)});\n`);
const scadArgs = ['-o', outPngPath, `--imgsize=${imgSize},${imgSize}`, `--colorscheme=${colorscheme}`];
@@ -370,14 +372,18 @@ export async function applyBooleanOp(opts: {
}
const scadPath = outPath.replace(/\.stl$/i, `_${Date.now()}.scad`);
fs.writeFileSync(scadPath, body);
// 출력 폴더가 아직 없을 수 있다 — 임시 .scad도 그 폴더에 쓰므로 만든 뒤에 써야 한다(2026-09-30, 새 폴더로 cut하면 ENOENT).
fs.mkdirSync(path.dirname(outPath), { recursive: true });
const render = await runOpenscad(['-o', outPath, scadPath], 120_000);
fs.writeFileSync(scadPath, body);
// 바이너리로 내보낸다. 기본값은 ASCII라서, 구멍·불리언 결과가 이 서버의 STL 도구(bbox·바닥 정렬·썸네일·편집기)에
// 읽히지 않았다(2026-09-30). transformStl과 같은 --export-format=binstl.
const render = await runOpenscad(['-o', outPath, '--export-format=binstl', scadPath], 120_000);
try { fs.unlinkSync(scadPath); } catch { /* noop */ }
if (!render.ok) return { ok: false, detail: render.detail };
if (!fs.existsSync(outPath) || fs.statSync(outPath).size < 100) {
return { ok: false, detail: '연산은 끝났는데 결과 STL 파일이 비정상입니다.' };
}
convertAsciiStlToBinary(outPath); // 혹시 ASCII로 나온 경우의 안전망(바이너리면 아무것도 안 함)
return { ok: true };
}
+36
View File
@@ -0,0 +1,36 @@
/**
* applyBooleanOp — 존재하지 않는 출력 폴더로 cut해도 폴더를 만들고 성공해야 한다
*
* 사건(2026-09-30): 임시 .scad를 출력 폴더에 쓰는데 그 폴더를 만들기 전에 써서 ENOENT로 죽었다
* (3018 집진 슈 시험 조각을 새 폴더로 자를 때). OpenSCAD가 없는 환경에서는 건너뛴다.
*/
import { test, describe } from 'node:test';
import assert from 'node:assert/strict';
import fs from 'fs';
import os from 'os';
import path from 'path';
import { applyBooleanOp, readStlBBox, OPENSCAD_BIN } from '../src/tools/stl-cad-core';
function cubeStl(size: number): Buffer {
// 12개 삼각형짜리 정육면체(바이너리 STL)
const v = [[0,0,0],[1,0,0],[1,1,0],[0,1,0],[0,0,1],[1,0,1],[1,1,1],[0,1,1]].map((p) => p.map((c) => c * size));
const f = [[0,2,1],[0,3,2],[4,5,6],[4,6,7],[0,1,5],[0,5,4],[1,2,6],[1,6,5],[2,3,7],[2,7,6],[3,0,4],[3,4,7]];
const b = Buffer.alloc(84 + f.length * 50);
b.writeUInt32LE(f.length, 80);
let off = 84;
for (const t of f) { off += 12; for (const i of t) for (const c of v[i]) { b.writeFloatLE(c, off); off += 4; } off += 2; }
return b;
}
describe('applyBooleanOp — 새 출력 폴더', { skip: !fs.existsSync(OPENSCAD_BIN) }, () => {
test('없는 하위 폴더로 cut해도 폴더를 만들고 결과를 쓴다', async () => {
const dir = fs.mkdtempSync(path.join(os.tmpdir(), 'cutnew-'));
const src = path.join(dir, 'cube.stl');
fs.writeFileSync(src, cubeStl(10));
const out = path.join(dir, '새 폴더', '하위', 'cut.stl');
const r = await applyBooleanOp({ primaryPath: src, outPath: out, op: 'cut', x: 5, y: 5, z: 10, w: 40, h: 40, d: 10 } as any);
assert.equal(r.ok, true, r.ok ? '' : (r as any).detail);
const bb = readStlBBox(out);
assert.ok(bb.max[2] <= 5.01, `z 5 위쪽이 잘려야 함: ${bb.max[2]}`);
});
});