- 新数据链 web/teaching/cases/carrier-partdesign:20 件零件(spec.json 唯一来源)、 内核建造器 parts/*.mjs、traces/FCStd/replay 全量产物;装配 20 插入 + 19 关节。 - 工具包 tools/carrier:catalog / preflight / create-part / create-assembly / create-all / list / self-test / README,按零件与装配拆分为独立工具。 - 共享驱动扩展 web/tools/cad-ui-rebuild:Loft/Pipe/Hole/Fillet/Chamfer/Mirrored/ LinearPattern 交互,Reversed 复选框补齐;装配 saveAssemblyBundle 先 Accept 再保存。 - 教学:lessons.mjs 新增 carrier 组(环境课 + 20 件 + 装配),suite-runner 增加 carrier-part/carrier-assembly,生成 carrier-toolkit.html 并接入 index 与控制台证据。 验证:20 件 210 步全部有头通过(P05 修复后 39640 m³;P06 回归 8/8); 装配 20/20 插入、19/19 关节、20 组件 0 畸形、0 mismatch;check:carrier、 check:reducer、check-console --live 全绿。 Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
176 lines
7.9 KiB
JavaScript
176 lines
7.9 KiB
JavaScript
#!/usr/bin/env node
|
||
/**
|
||
* Purpose: 在真实界面里装配整艘航空母舰(20 个零件 + 19 个固定关节 + 求解)。
|
||
*
|
||
* 本工具只做编排:装配回放由 web/teaching/cases/carrier-partdesign/build-assembly-replay.mjs
|
||
* 从 assembly-spec.json 生成,插入、关节与求解全部由既有引擎
|
||
* web/tools/cad-ui-rebuild/assemble.mjs 在界面上完成(插入用 Insert Link 面板,
|
||
* 关节与求解走工作台命令面板,与 E18 课相同)。这里读取引擎报告并归一化。
|
||
*
|
||
* Usage:
|
||
* node tools/carrier/create-assembly.mjs --dry
|
||
* node tools/carrier/create-assembly.mjs --headed
|
||
* node tools/carrier/create-assembly.mjs --out /tmp/carrier-assembly
|
||
*
|
||
* Exit: 0 通过 / 1 产品失败(插入/关节/求解与回放不符)/ 2 前置缺失
|
||
*/
|
||
import fs from "node:fs";
|
||
import path from "node:path";
|
||
import { spawn } from "node:child_process";
|
||
import { REPO_ROOT } from "./catalog.mjs";
|
||
import { ensureOrigin, preflight } from "./preflight.mjs";
|
||
|
||
const ASSEMBLE = path.join(REPO_ROOT, "web/tools/cad-ui-rebuild/assemble.mjs");
|
||
const PROJECT_DIR = path.join(REPO_ROOT, "web/tools/carrier-partdesign-course");
|
||
const SPEC_DIR = path.join(REPO_ROOT, "web/teaching/cases/carrier-partdesign");
|
||
const SPEC_FILE = path.join(SPEC_DIR, "assembly-spec.json");
|
||
const GENERATOR = path.join(SPEC_DIR, "build-assembly-replay.mjs");
|
||
const REPLAY_FILE = path.join(SPEC_DIR, "out/replay/CarrierAssembly.json");
|
||
|
||
function parseArgs(argv) {
|
||
const options = { headed: true, dry: false, serve: true, out: null, json: false, regenerate: false };
|
||
for (let index = 0; index < argv.length; index += 1) {
|
||
const arg = argv[index];
|
||
if (arg === "--headed") options.headed = true;
|
||
else if (arg === "--headless") options.headed = false;
|
||
else if (arg === "--dry") options.dry = true;
|
||
else if (arg === "--no-serve") options.serve = false;
|
||
else if (arg === "--regenerate") options.regenerate = true;
|
||
else if (arg === "--json") options.json = true;
|
||
else if (arg === "--out") options.out = path.resolve(argv[++index] ?? "");
|
||
else if (arg === "--help" || arg === "-h") {
|
||
console.log("node tools/carrier/create-assembly.mjs [--headed|--headless] [--dry] [--regenerate] [--no-serve] [--out <dir>] [--json]");
|
||
process.exit(0);
|
||
} else {
|
||
console.error(`未知参数:${arg}`);
|
||
process.exit(2);
|
||
}
|
||
}
|
||
return options;
|
||
}
|
||
|
||
function readSpec() {
|
||
if (!fs.existsSync(SPEC_FILE)) throw new Error(`缺少装配清单 ${SPEC_FILE}`);
|
||
return JSON.parse(fs.readFileSync(SPEC_FILE, "utf8"));
|
||
}
|
||
|
||
/** The replay is generated data: regenerate when missing or older than its spec. */
|
||
function ensureReplay(spec, regenerate) {
|
||
const stale = !fs.existsSync(REPLAY_FILE)
|
||
|| fs.statSync(REPLAY_FILE).mtimeMs < Math.max(fs.statSync(SPEC_FILE).mtimeMs, fs.statSync(GENERATOR).mtimeMs);
|
||
if (!stale && !regenerate) return { generated: false, file: REPLAY_FILE };
|
||
const code = spawn(process.execPath, [GENERATOR], { cwd: SPEC_DIR, stdio: "inherit" });
|
||
return new Promise(resolve => code.on("close", exit => {
|
||
if (exit !== 0) throw new Error(`装配回放生成失败(exit ${exit})`);
|
||
resolve({ generated: true, file: REPLAY_FILE, spec });
|
||
}));
|
||
}
|
||
|
||
function assemblyChecks(spec) {
|
||
const checks = [];
|
||
const add = (id, ok, detail, fix = "") => checks.push({ id, ok, detail, fix });
|
||
add("spec", Boolean(spec.inserts?.length && spec.joints?.length),
|
||
`装配清单:${spec.inserts?.length ?? 0} 次插入 / ${spec.joints?.length ?? 0} 个关节 / 接地 ${spec.groundDocument}`);
|
||
add("replay", fs.existsSync(REPLAY_FILE), REPLAY_FILE, "node web/teaching/cases/carrier-partdesign/build-assembly-replay.mjs");
|
||
const samples = path.join(REPO_ROOT, "web/teaching/cases/carrier-partdesign/out/FCStd");
|
||
const missing = spec.inserts.map(item => item.sourceDocument).filter(document => !fs.existsSync(path.join(samples, `${document}.FCStd`)));
|
||
add("samples", missing.length === 0, missing.length ? `缺少源文档:${missing.join(", ")}` : `${spec.inserts.length} 份源文档齐全`, "先运行 web/teaching/cases/carrier-partdesign/build.mjs --all");
|
||
add("engine", fs.existsSync(ASSEMBLE), ASSEMBLE);
|
||
return checks;
|
||
}
|
||
|
||
function readRunReport() {
|
||
const file = path.join(PROJECT_DIR, "out/ui-run", "CarrierAssembly-assembly.json");
|
||
try {
|
||
return { file, report: JSON.parse(fs.readFileSync(file, "utf8")) };
|
||
} catch {
|
||
return { file, report: null };
|
||
}
|
||
}
|
||
|
||
function runEngine(options) {
|
||
const argv = [process.execPath, ASSEMBLE, "--project", PROJECT_DIR];
|
||
if (options.headed) argv.push("--headed");
|
||
return new Promise(resolve => {
|
||
const child = spawn(argv[0], argv.slice(1), { cwd: REPO_ROOT, stdio: "inherit" });
|
||
child.on("error", error => resolve({ code: -1, error }));
|
||
child.on("close", code => resolve({ code: code ?? -1 }));
|
||
});
|
||
}
|
||
|
||
async function main() {
|
||
const options = parseArgs(process.argv.slice(2));
|
||
const spec = readSpec();
|
||
|
||
const generic = await preflight();
|
||
const checks = [...generic.checks.filter(check => check.id !== "parts"), ...assemblyChecks(spec)];
|
||
const ok = checks.every(check => check.ok);
|
||
if (options.dry) {
|
||
for (const check of checks) console.log(` ${check.ok ? "✓" : "✗"} ${check.id}: ${check.detail}`);
|
||
console.log(ok ? "前置全部满足(dry run,未启动浏览器)" : "前置未满足(dry run)");
|
||
process.exit(ok ? 0 : 2);
|
||
}
|
||
if (!ok) {
|
||
for (const check of checks.filter(item => !item.ok)) console.error(`前置缺失 ${check.id}: ${check.detail} → ${check.fix}`);
|
||
console.error("未启动浏览器(前置失败不是产品失败)。");
|
||
process.exit(2);
|
||
}
|
||
|
||
await ensureReplay(spec, options.regenerate);
|
||
|
||
let origin = null;
|
||
if (options.serve) {
|
||
origin = await ensureOrigin();
|
||
console.log(origin.started ? `已为 ${origin.origin} 启动静态服务` : `复用已在运行的应用入口 ${origin.origin}`);
|
||
}
|
||
|
||
const startedAt = Date.now();
|
||
let result;
|
||
try {
|
||
result = await runEngine(options);
|
||
} finally {
|
||
await origin?.close();
|
||
}
|
||
|
||
const { file: runFile, report } = readRunReport();
|
||
const dir = options.out ?? path.join(REPO_ROOT, "docs/current/carrier-partdesign-toolkit/runs",
|
||
`${new Date().toISOString().replace(/[:.]/g, "-")}-assembly`);
|
||
fs.mkdirSync(dir, { recursive: true });
|
||
|
||
const lastSolve = report?.solves?.at(-1) ?? null;
|
||
const summary = {
|
||
schema: "freecad-web/carrier-toolkit/assembly/v1",
|
||
document: spec.document,
|
||
label: spec.label,
|
||
expected: { inserts: spec.inserts.length, joints: spec.joints.length, solves: 2 },
|
||
inserted: report?.inserted ?? 0,
|
||
joints: report?.joints?.length ?? 0,
|
||
components: lastSolve?.components ?? 0,
|
||
malformedComponents: lastSolve?.malformed ?? null,
|
||
mismatches: report?.mismatches ?? [],
|
||
fatal: report?.fatal ?? null,
|
||
seconds: Math.round(report?.seconds ?? (Date.now() - startedAt) / 1000),
|
||
engineExitCode: result.code,
|
||
engineReport: path.relative(REPO_ROOT, runFile),
|
||
verdict: !report ? "run_fail"
|
||
: report.fatal || (report.mismatches ?? []).length ? "product_fail"
|
||
: report.inserted !== spec.inserts.length || (report.joints ?? []).length !== spec.joints.length
|
||
|| lastSolve?.malformed ? "product_fail"
|
||
: "pass",
|
||
finishedAt: new Date().toISOString(),
|
||
};
|
||
fs.writeFileSync(path.join(dir, "report.json"), JSON.stringify(summary, null, 2));
|
||
|
||
if (options.json) console.log(JSON.stringify(summary));
|
||
else {
|
||
console.log(`装配 ${spec.document}: ${summary.verdict}(插入 ${summary.inserted}/${spec.inserts.length},关节 ${summary.joints}/${spec.joints.length},求解组件 ${summary.components},畸形 ${summary.malformedComponents},${summary.seconds}s)`);
|
||
console.log(`报告:${path.relative(REPO_ROOT, path.join(dir, "report.json"))}`);
|
||
}
|
||
process.exit(summary.verdict === "pass" ? 0 : summary.verdict === "run_fail" ? 2 : 1);
|
||
}
|
||
|
||
main().catch(error => {
|
||
console.error(`失败:${error?.message ?? error}`);
|
||
process.exit(2);
|
||
});
|