// Turn a finished part into the exact list of UI operations that would rebuild it. // // The replay records each step's action and arguments, but it deliberately // compacts sketches to {count, sha256}: a trace should not carry thousands of // coordinates. The finished FCStd still holds them, and the kernel's snapshot // dumps every sketch's real geometry and constraints (PartDesignDocumentWorkflow // .cpp:1101-1150). So the UI instructions are generated from the same document // the videos render, never retyped by hand. /** * Default page per command kind. * * The standalone PartDesign workspace is the only page that renders both the * sketch editor and the profile features, so every kind defaults to it. The old * copy of this table sent gears to workbench.html while the driver had already * moved gears onto partdesign.html — a project can still override any entry * through `project.json`'s `pages`. */ export const PAGE_FOR = { sketch: "partdesign.html", feature: "partdesign.html", gear: "partdesign.html", primitive: "partdesign.html", transform: "partdesign.html", dressup: "partdesign.html", }; const FEATURE_COMMAND = { Pad: "PartDesign_Pad", Pocket: "PartDesign_Pocket", Revolution: "PartDesign_Revolution", Groove: "PartDesign_Groove", Hole: "PartDesign_Hole", Loft: "PartDesign_AdditiveLoft", Pipe: "PartDesign_AdditivePipe", Helix: "PartDesign_AdditiveHelix", SubtractiveLoft: "PartDesign_SubtractiveLoft", SubtractivePipe: "PartDesign_SubtractivePipe", SubtractiveHelix: "PartDesign_SubtractiveHelix", Boolean: "PartDesign_Boolean", }; const TRANSFORM_COMMAND = { PolarPattern: "PartDesign_PolarPattern", LinearPattern: "PartDesign_LinearPattern", Mirrored: "PartDesign_Mirrored", Scaled: "PartDesign_Scaled", }; /** * Feature arguments that must survive into the UI step untouched. * * The panel names differ per feature type, but the replay already records them * under the kernel's own keys (`setFeature` accepts them verbatim), so copying * the known set is enough and keeps one table instead of one builder per type. */ const FEATURE_EXTRA = [ "sections", "ruled", "closed", "spine", "spineEdges", "pipeMode", "transition", "referenceMode", "auxiliarySpine", "auxiliarySpineEdges", "auxiliaryCurvilinear", "binormal", "diameter", "depth", "depthType", ]; const DRESS_UP_COMMAND = { Fillet: "PartDesign_Fillet", Chamfer: "PartDesign_Chamfer" }; /** Round to 1e-4 mm: finer than any drawing tolerance, and keeps the text short. */ // Round only to kill float noise; 4 decimals broke arc closure (pi -> 3.1416 // leaves a 1.6e-4 mm gap at r=41, far outside OCCT's 1e-7 confusion). const r4 = value => Math.round(Number(value) * 1e9) / 1e9; const triple = value => (Array.isArray(value) ? value.slice(0, 3).map(r4) : null); /** * Describe one sketch for the geometry+constraint editor. * * The editor is laid out as rows: `Geometry N type` plus X/Y/Z (and radius or * angles), then `Constraint N type` plus its geometry/point slots. Keeping the * row order identical to the kernel's own means the numbering the learner reads * matches the numbering the kernel reported. */ export function describeSketch(sketch) { const geometries = (sketch.geometries ?? []).map(geometry => { const row = { type: geometry.type }; if (geometry.type === "line") { row.start = triple(geometry.start); row.end = triple(geometry.end); } else if (geometry.type === "circle") { row.center = triple(geometry.center); row.radius = r4(geometry.radius); } else if (geometry.type === "arc") { row.center = triple(geometry.center); row.radius = r4(geometry.radius); row.startAngle = r4(geometry.startAngle); row.endAngle = r4(geometry.endAngle); } else if (geometry.type === "ellipse") { row.center = triple(geometry.center); row.majorRadius = r4(geometry.majorRadius); row.minorRadius = r4(geometry.minorRadius); } else if (geometry.type === "point") { row.point = triple(geometry.point); } else if (geometry.type === "bspline") row.points = (geometry.points ?? []).map(triple); return row; }); const constraints = (sketch.constraints ?? []).map(constraint => { const row = { type: constraint.type }; for (const key of ["first", "second", "third"]) if (constraint[key] != null) row[key] = constraint[key]; for (const key of ["firstPos", "secondPos", "thirdPos"]) { if (constraint[key] && constraint[key] !== "none") row[key] = constraint[key]; } if (constraint.value != null) row.value = r4(constraint.value); if (constraint.name) row.name = constraint.name; return row; }); return { profileType: sketch.profileType ?? "Geometry", position: triple(sketch.position) ?? [0, 0, 0], // Sketches are not always parallel to XY. Bolt patterns on a Y-facing face // need a rotated support plane; the kernel snapshot now reports it and // `setSketch` accepts it. placementRotation: Array.isArray(sketch.placementRotation) ? sketch.placementRotation.map(r4) : [0, 0, 0, 1], // Named constraints carry the dimensions of the simple profiles: a Circle // sketch has no geometry list at all, its radius IS the named constraint // "Radius" (the same conflation desktop Sketcher uses). Dropping this made // every Circle sketch non-reproducible. parameters: Object.fromEntries(Object.entries(sketch.parameters ?? {}).map(([key, value]) => [key, r4(value)])), geometries, constraints, }; } /** * Build the UI step list for one document from its replay and a kernel snapshot. * @param {object} [options] `pages` and `featureCommands` from the project * manifest; anything absent falls back to the defaults above. */ export function uiStepsFor(document, replay, snapshotObjects, options = {}) { const pages = { ...PAGE_FOR, ...(options.pages ?? {}) }; const featureCommands = { ...FEATURE_COMMAND, ...(options.featureCommands ?? {}) }; const sketches = new Map(); for (const object of snapshotObjects ?? []) { if (object.type === "Sketcher::SketchObject") sketches.set(object.name, object); } const failures = []; const steps = []; for (const [index, step] of replay.steps.entries()) { const args = step.args ?? {}; const objectName = args.object; const base = { step: step.step, label: step.label, action: step.action, object: objectName ?? null }; if (step.action === "setSketch") { const sketch = sketches.get(objectName); if (!sketch) { failures.push(`${document} step ${step.step}: sketch ${objectName} not in the document`); continue; } const described = describeSketch(sketch); // The replay's digest is the contract that these are the same arrays. if (args.geometries && typeof args.geometries === "object" && "count" in args.geometries && args.geometries.count !== described.geometries.length) failures.push(`${document} step ${step.step}: ${described.geometries.length} geometries but the trace recorded ${args.geometries.count}`); // A simple profile without its dimension cannot be reproduced by hand. if (described.profileType === "Circle" && !(described.parameters?.Radius > 0)) failures.push(`${document} step ${step.step}: Circle sketch ${objectName} has no Radius`); if (described.profileType === "Rectangle" && !(described.parameters?.Width > 0 && described.parameters?.Height > 0)) failures.push(`${document} step ${step.step}: Rectangle sketch ${objectName} lacks Width/Height`); steps.push({ ...base, kind: "sketch", page: pages.sketch, command: "PartDesign_NewSketch", profileType: described.profileType, position: described.position, placementRotation: described.placementRotation, parameters: described.parameters, geometries: described.geometries, constraints: described.constraints, requireFullyConstrained: false, }); } else if (step.action === "setFeature") { const command = featureCommands[args.featureType]; if (!command) { failures.push(`${document} step ${step.step}: unknown featureType ${args.featureType}`); continue; } const extra = {}; for (const key of FEATURE_EXTRA) if (args[key] !== undefined) extra[key] = args[key]; steps.push({ ...base, kind: "feature", page: pages.feature, command, featureType: args.featureType, profile: args.profile ?? null, profileType: null, position: null, geometries: [], constraints: [], length: args.length ?? null, midplane: args.midplane === true, reversed: args.reversed === true, angle: args.angle ?? (args.featureType === "Revolution" ? 360 : null), axis: args.axis ?? null, ...extra, }); } else if (step.action === "setGear") { steps.push({ ...base, kind: "gear", page: pages.gear, command: "PartDesign_InvoluteGear", gearType: args.gearType, gearParameters: args.gearParameters ?? {}, position: triple(args.position) ?? [0, 0, 0], placementRotation: Array.isArray(args.placementRotation) ? args.placementRotation.map(r4) : [0, 0, 0, 1], geometries: [], constraints: [], }); } else if (step.action === "setPrimitive") { steps.push({ ...base, kind: "primitive", page: pages.primitive, command: "PartDesign_AdditiveSphere", primitiveType: args.primitiveType, parameters: args.parameters ?? {}, position: triple(args.position) ?? [0, 0, 0], geometries: [], constraints: [], }); } else if (step.action === "setTransform") { // Older records omit transformType; they were all polar patterns, and the // driver branches on this field, so defaulting keeps them identical. const transformType = args.transformType ?? "PolarPattern"; const command = TRANSFORM_COMMAND[transformType]; if (!command) { failures.push(`${document} step ${step.step}: unknown transformType ${transformType}`); continue; } steps.push({ ...base, kind: "transform", page: pages.transform, command, transformType, originals: args.originals ?? [], occurrences: args.occurrences ?? null, angle: args.angle ?? null, rotationAxis: args.rotationAxis ?? null, mirrorPlane: args.mirrorPlane ?? null, direction: args.direction ?? null, length: args.length ?? null, reversed: args.reversed === true, geometries: [], constraints: [], }); } else if (step.action === "setDressUp") { const command = DRESS_UP_COMMAND[args.dressUpType]; if (!command) { failures.push(`${document} step ${step.step}: unknown dressUpType ${args.dressUpType}`); continue; } steps.push({ ...base, kind: "dressup", page: pages.dressup, command, dressUpType: args.dressUpType, base: args.base ?? null, size: args.size ?? null, edges: args.edges ?? [], useAllEdges: args.useAllEdges === true, chamferType: args.chamferType ?? null, size2: args.size2 ?? null, angle: args.angle ?? null, flipDirection: args.flipDirection === true, geometries: [], constraints: [], }); } else { failures.push(`${document} step ${step.step}: unhandled action ${step.action}`); continue; } void index; } return { document, steps, failures }; }