fdm/scripts/prepare-data.js

53 lines
1.7 KiB
JavaScript

import { readFile, writeFile, mkdir } from "node:fs/promises";
import { parseGcode, createDemoGcode } from "../src/lib/gcode.js";
import { packGcode } from "../src/lib/packed-gcode.js";
import { prepareStream } from "./prepare-stream.js";
const printSource = await readFile(
new URL("../public/data/print-job.gcode", import.meta.url),
"utf8",
);
const packed = packGcode(
printSource,
"鸡你太美_0.2mm_PLA_Generic Klipper Printer_5h20m.gcode",
);
await writeFile(
new URL("../public/data/print-job.bin", import.meta.url),
Buffer.from(packed.buffer.buffer),
);
if (packed.metadata.thumbnail) {
await writeFile(
new URL("../public/data/print-job.png", import.meta.url),
Buffer.from(packed.metadata.thumbnail, "base64"),
);
delete packed.metadata.thumbnail;
}
await writeFile(
new URL("../public/data/print-job.json", import.meta.url),
JSON.stringify(packed.metadata),
);
console.log(
`打印任务:${packed.metadata.count} 段,${packed.metadata.layers.length} 层,切片估时 ${packed.metadata.estimatedSeconds} 秒`,
);
await prepareStream(packed, printSource);
const source = await readFile(
new URL("../public/data/spring-festival.gcode", import.meta.url),
"utf8",
);
await mkdir(new URL("../public/data/", import.meta.url), { recursive: true });
const original = parseGcode(source, "春节序曲.gcode");
const demo = parseGcode(createDemoGcode(), "褶光花器 · FDM 成型示例");
for (const [name, data] of [
["original", original],
["demo", demo],
]) {
await writeFile(
new URL(`../public/data/${name}.json`, import.meta.url),
JSON.stringify(data),
);
console.log(
`${data.name}: ${data.segments.length} 段路径,${data.layers.length} 个挤出层,${data.duration.toFixed(1)} 秒`,
);
}