Files
FreeCADWasmDev/tools/reducer/catalog.mjs
wangdequan b876ad5742 feat(reducer): 二级减速器有头创建工具包与初学者教学
- tools/reducer:目录/前置/单件/批量/装配/自测,复用 cad-ui-rebuild 引擎
- 教学控制台新增「二级减速器」分组:23 件逐件课 + 装配课 + 环境课(job kind reducer-part / reducer-assembly)
- web/teaching/reducer-toolkit.html 初学者配套页(生成物)+ Playwright 双视口门禁
- 首页补工具入口;工具手册随 npm 脚本变更重生成

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
2026-11-25 07:31:24 +08:00

134 lines
4.8 KiB
JavaScript

/**
* Canonical catalog of the 23 secondary-reducer parts.
*
* This module deliberately owns no part list of its own: it reads the project
* manifest that the shared UI driver already uses
* (web/tools/reducer-parts-course/project.json) and joins it with the two
* evidence trees that already exist on disk:
* - out/ui-steps/<Document>.json — the per-step UI actions drive.mjs replays
* - out/replay/<Document>.json — the build record, including final volume
*
* A part is only offered when both files 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 PROJECT_DIR = path.join(REPO_ROOT, "web/tools/reducer-parts-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");
/** Document prefix → teaching group. Longest prefix wins. */
const GROUPS = [
{ id: "gears", label: "齿轮", prefixes: ["Gear"] },
{ id: "shafts", label: "轴", prefixes: ["Shaft"] },
{ id: "bearings", label: "轴承", prefixes: ["Bearing"] },
{ id: "keys", label: "平键", prefixes: ["Key"] },
{ id: "seals", label: "骨架油封", prefixes: ["Seal"] },
{ id: "coupling", label: "联轴器", prefixes: ["Coupling"] },
{ id: "housing", label: "箱体与箱盖", prefixes: ["Housing", "Inspection"] },
];
export function groupOf(document) {
const match = GROUPS.find((group) => group.prefixes.some((prefix) => document.startsWith(prefix)));
return match ?? { id: "other", label: "其他", prefixes: [] };
}
function readJson(file) {
try {
return JSON.parse(fs.readFileSync(file, "utf8"));
} catch {
return null;
}
}
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(PROJECT_DIR, "out/replay", `${document}.json`))
?? readJson(path.join(REPO_ROOT, "web/teaching/cases/secondary-reducer-pro/out/replay", `${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;
}
export function projectManifest() {
const manifest = readJson(PROJECT_FILE);
if (!manifest) throw new Error(`缺少项目清单:${PROJECT_FILE}`);
return manifest;
}
/** All parts, in manifest order, enriched with step counts and recorded volume. */
export function parts() {
const manifest = projectManifest();
return (manifest.parts ?? []).map((entry) => {
const steps = stepsOf(entry.document);
const replay = replayOf(entry.document);
const group = groupOf(entry.document);
return {
id: entry.id,
document: entry.document,
title: entry.title,
group: group.id,
groupLabel: group.label,
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() {
return GROUPS.map((group) => {
const members = parts().filter((part) => part.group === group.id);
return { id: group.id, label: group.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 "Bearing6206_Inner" 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,
};
}