Files
FreeCADWasmDev/tools/carrier/catalog.mjs
wangdequan 87b2a6cb8a feat(carrier): 航空母舰 PartDesign 有头创建工具包与初学者教学
- 新数据链 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>
2026-11-25 18:46:27 +08:00

134 lines
4.7 KiB
JavaScript

/**
* Canonical catalog of the 20 carrier PartDesign parts.
*
* Like tools/reducer/catalog.mjs, this module owns no part list of its own. The
* single source is the case spec (one inventory for builders, toolkit and
* teaching), joined with the three evidence trees on disk:
* - web/teaching/cases/carrier-partdesign/out/replay/<Document>.json (build record)
* - web/tools/carrier-partdesign-course/out/ui-steps/<Document>.json (UI steps)
* - web/tools/carrier-partdesign-course/out/ui-run/<Document>.json (last headed run)
*
* A part is only offered when both replay and ui-steps exist, so `create-part`
* can never start a browser for a part that has nothing to replay.
*/
import fs from "node:fs";
import path from "node:path";
import { fileURLToPath } from "node:url";
const HERE = path.dirname(fileURLToPath(import.meta.url));
export const REPO_ROOT = path.resolve(HERE, "../..");
export const CASE_DIR = path.join(REPO_ROOT, "web/teaching/cases/carrier-partdesign");
export const SPEC_FILE = path.join(CASE_DIR, "spec.json");
export const REPLAY_DIR = path.join(CASE_DIR, "out/replay");
export const PROJECT_DIR = path.join(REPO_ROOT, "web/tools/carrier-partdesign-course");
export const PROJECT_FILE = path.join(PROJECT_DIR, "project.json");
const STEPS_DIR = path.join(PROJECT_DIR, "out/ui-steps");
const RUN_DIR = path.join(PROJECT_DIR, "out/ui-run");
const STILLS_DIR = path.join(PROJECT_DIR, "ui-stills");
function readJson(file) {
try {
return JSON.parse(fs.readFileSync(file, "utf8"));
} catch {
return null;
}
}
export function spec() {
const payload = readJson(SPEC_FILE);
if (!payload) throw new Error(`缺少案例清单:${SPEC_FILE}`);
return payload;
}
export function projectManifest() {
const manifest = readJson(PROJECT_FILE);
if (!manifest) throw new Error(`缺少项目清单:${PROJECT_FILE}`);
return manifest;
}
function stepsOf(document) {
const payload = readJson(path.join(STEPS_DIR, `${document}.json`));
return Array.isArray(payload?.steps) ? payload.steps : null;
}
function replayOf(document) {
return readJson(path.join(REPLAY_DIR, `${document}.json`));
}
function finalVolume(steps) {
const volumes = (steps ?? []).map(step => step.volume).filter(volume => typeof volume === "number" && volume > 0);
return volumes.length ? volumes.at(-1) : null;
}
/** All parts, in spec order, enriched with step counts and recorded volume. */
export function parts() {
const manifest = spec();
const systems = manifest.systems ?? {};
return (manifest.parts ?? []).map(entry => {
const steps = stepsOf(entry.document);
const replay = replayOf(entry.document);
return {
id: entry.id,
document: entry.document,
title: entry.title,
system: entry.system,
systemLabel: systems[entry.system] ?? entry.system,
module: entry.module,
featureTypes: entry.featureTypes ?? [],
placement: entry.placement ?? null,
steps: steps?.length ?? 0,
volume: finalVolume(replay?.steps),
reconstructable: Boolean(steps && steps.length && replay?.steps?.length),
stepsFile: path.relative(REPO_ROOT, path.join(STEPS_DIR, `${entry.document}.json`)),
runFile: path.relative(REPO_ROOT, path.join(RUN_DIR, `${entry.document}.json`)),
stillsDir: path.relative(REPO_ROOT, path.join(STILLS_DIR, entry.id)),
};
});
}
export function groups() {
const systems = spec().systems ?? {};
const all = parts();
return Object.entries(systems)
.map(([id, label]) => {
const members = all.filter(part => part.system === id);
return { id, label, count: members.length, ids: members.map(part => part.id) };
})
.filter(group => group.count > 0);
}
const ID_PATTERN = /^[A-Za-z][A-Za-z0-9_-]{0,63}$/;
/**
* Resolve "P06", "p06" or "CarrierElevators" to a part.
* Rejects anything that is not a plain token so a shell fragment can never be
* forwarded to the driver.
*/
export function find(key) {
if (typeof key !== "string" || !ID_PATTERN.test(key)) {
const error = new Error(`非法零件标识:${String(key).slice(0, 40)}`);
error.code = "E_PART";
throw error;
}
const wanted = key.toLowerCase();
const all = parts();
const match = all.find(part => part.id.toLowerCase() === wanted)
?? all.find(part => part.document.toLowerCase() === wanted);
if (!match) {
const error = new Error(`没有该零件:${key}`);
error.code = "E_PART";
throw error;
}
return match;
}
export function stats() {
const all = parts();
return {
parts: all.length,
steps: all.reduce((sum, part) => sum + part.steps, 0),
reconstructable: all.filter(part => part.reconstructable).length,
volume: all.reduce((sum, part) => sum + (part.volume ?? 0), 0),
};
}