48 lines
2.4 KiB
JavaScript
48 lines
2.4 KiB
JavaScript
// Exports one STL per part from E18.FCStd using the Node WASM probe (no browser).
|
|
import fs from "node:fs";
|
|
import path from "node:path";
|
|
import { fileURLToPath } from "node:url";
|
|
import { loadWasmProbe } from "../../../../tools/wasm-probe-runtime.mjs";
|
|
|
|
const here = path.dirname(fileURLToPath(import.meta.url));
|
|
const webRoot = path.resolve(here, "../../..");
|
|
const checkpoint = path.join(webRoot, "tools", "line-course", "checkpoints", "E18.FCStd");
|
|
const outDir = path.join(webRoot, "teaching", "cases", "line-parts");
|
|
fs.mkdirSync(outDir, { recursive: true });
|
|
const document = "LineCourseExport";
|
|
const { kernel } = await loadWasmProbe(path.join(webRoot, "public/kernel"), "FreeCADWebDocumentProbe");
|
|
const raw = (cmd, args) => JSON.parse(kernel.ccall("fc_web_dispatch_json", "string", ["string"],
|
|
[JSON.stringify({ cmd, args })]));
|
|
kernel.FS.writeFile(`/tmp/${document}.FCStd`, new Uint8Array(fs.readFileSync(checkpoint)));
|
|
const opened = raw("part.document", { document, action: "open", path: `/tmp/${document}.FCStd`, includeMesh: false });
|
|
if (!opened.ok) throw new Error(`open failed: ${JSON.stringify(opened)}`);
|
|
const py = String.raw`
|
|
import FreeCAD as App, Mesh, os, json
|
|
d = App.getDocument('${document}')
|
|
os.makedirs('/tmp/line-stl', exist_ok=True)
|
|
rows = []
|
|
for o in d.Objects:
|
|
s = getattr(o, 'Shape', None)
|
|
if s is None or s.isNull():
|
|
continue
|
|
path = '/tmp/line-stl/' + o.Label + '.stl'
|
|
Mesh.export([o], path)
|
|
rows.append({'label': o.Label, 'bytes': os.path.getsize(path)})
|
|
open('/tmp/line-stl/rows.json', 'w').write(json.dumps(rows))
|
|
`;
|
|
const executed = raw("part.documentViewCommand", {
|
|
owner: "DocumentProbe", document, command: "Std_CommandLine",
|
|
parameters: { operation: "submit", source: `exec(${JSON.stringify(py)})` },
|
|
});
|
|
if (!executed.ok) throw new Error(`python failed: ${JSON.stringify(executed)}`);
|
|
const rows = JSON.parse(kernel.FS.readFile("/tmp/line-stl/rows.json", { encoding: "utf8" }));
|
|
const mapping = {};
|
|
for (const row of rows) {
|
|
const bytes = kernel.FS.readFile(`/tmp/line-stl/${row.label}.stl`);
|
|
fs.writeFileSync(path.join(outDir, `${row.label}.stl`), Buffer.from(bytes));
|
|
mapping[row.label] = row.bytes;
|
|
}
|
|
fs.writeFileSync(path.join(outDir, "mapping.json"), `${JSON.stringify(mapping, null, 2)}\n`);
|
|
kernel.PThread?.terminateAllThreads?.();
|
|
console.log(JSON.stringify({ ok: rows.length > 0, files: rows.length, outDir, totalBytes: rows.reduce((sum, row) => sum + row.bytes, 0) }));
|