Interface design document and manual now share one authoritative surface inventory (333 surfaces across 9 categories, gap 0), and every chapter carries real Playwright screenshots of the panels it documents. - tools/fcweb: ui-surface-scan/ui-surface-inventory (deterministic --check), build-ui-doc renders panel catalog, workbench matrix and mapping tables; blank toolbar crops (<1 KB) are skipped and pruned instead of embedded - web/tools/manual: generate-panel-index (ch16), audit-ui-gaps, generate-final-status, generate-gaps-doc; pager is per-chapter so the manual no longer overflows horizontally - manual: 130 screenshot references / 128 PNGs, panel sections for ch02-ch13 - web/src: utility menus stop click propagation (menu reopened on item click); workbench shell gains an OPFS document picker so workbench.html can bind a real document instead of the fixed workbench-document id; Sketcher panel moves from the command column to the main stage Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
260 lines
11 KiB
JavaScript
260 lines
11 KiB
JavaScript
// PartDesign client: Body, sketches, profile features, gears.
|
|
//
|
|
// Serves `cmd: "partdesign.document"` via FreeCADWebPartDesignRuntimeProbe
|
|
// (PartDesignRuntimeProbe.cpp:1353 -> PartDesignDocumentWorkflow.cpp:1196).
|
|
//
|
|
// Contract notes that differ from the Part probe and are easy to get wrong:
|
|
// - primitives use `primitiveType` (Part uses `primitive`)
|
|
// - primitives take a `position` array, not `primitivePlacement`
|
|
// - setSketch accepts profileType Rectangle | Circle | Geometry | Wire
|
|
// - setFeature takes `length` for Pad/Pocket (not `depth`)
|
|
// - sketches and datums report volume: null -- only solids have volume
|
|
import {
|
|
STEM_PARTDESIGN, KernelError, openKernel, createSession, writeTrace,
|
|
} from "./kernel.mjs";
|
|
import { storeBytes } from "./opfs.mjs";
|
|
|
|
export const PARTDESIGN_SCHEMA = "freecad-web-partdesign-document/v1";
|
|
export const PARTDESIGN_MODULE = "PartDesign";
|
|
|
|
const NAME = /^[A-Za-z_][A-Za-z0-9_]{0,63}$/;
|
|
const PROFILE_TYPES = ["Rectangle", "Circle", "Geometry", "Wire"];
|
|
const PRIMITIVES = [
|
|
"AdditiveBox", "AdditiveCylinder", "AdditiveSphere", "AdditiveCone",
|
|
"AdditiveEllipsoid", "AdditiveTorus", "AdditivePrism", "AdditiveWedge",
|
|
"SubtractiveBox", "SubtractiveCylinder", "SubtractiveSphere", "SubtractiveCone",
|
|
"SubtractiveEllipsoid", "SubtractiveTorus", "SubtractivePrism", "SubtractiveWedge",
|
|
];
|
|
const FEATURES = [
|
|
"Pad", "Pocket", "Revolution", "Groove", "Hole", "Loft", "Pipe",
|
|
"SubtractiveLoft", "SubtractivePipe",
|
|
];
|
|
const GEAR_TYPES = ["InvoluteGear", "Sprocket"];
|
|
const DRESS_UPS = ["Fillet", "Chamfer", "Draft", "Thickness"];
|
|
|
|
function assertName(name, what = "object") {
|
|
if (typeof name !== "string" || !NAME.test(name)) {
|
|
throw new KernelError(`${what} name must match ${NAME} (got ${JSON.stringify(name)})`, { code: "bad_name" });
|
|
}
|
|
return name;
|
|
}
|
|
|
|
function assertPositive(value, what) {
|
|
if (typeof value !== "number" || !Number.isFinite(value) || value <= 0) {
|
|
throw new KernelError(`${what} must be a positive finite number (got ${JSON.stringify(value)})`, { code: "bad_dimension" });
|
|
}
|
|
return value;
|
|
}
|
|
|
|
function vector3(value, fallback, what) {
|
|
if (value == null) return fallback;
|
|
if (!Array.isArray(value) || value.length !== 3 || value.some(item => typeof item !== "number" || !Number.isFinite(item))) {
|
|
throw new KernelError(`${what} must be a 3-number array (got ${JSON.stringify(value)})`, { code: "bad_vector" });
|
|
}
|
|
return value;
|
|
}
|
|
|
|
export class PartDesignDocument {
|
|
constructor(session) {
|
|
this.session = session;
|
|
this.document = session.document;
|
|
this.kernel = session.kernel;
|
|
}
|
|
|
|
get call() { return this.session.call; }
|
|
get timed() { return this.session.timed; }
|
|
get timings() { return this.session.timings; }
|
|
get events() { return this.session.events; }
|
|
|
|
// ---- document / body ----------------------------------------------------
|
|
create() { return this.call("create"); }
|
|
open(file) { return this.call("open", { path: file }); }
|
|
close() { return this.call("close"); }
|
|
saveDocument({ name } = {}) {
|
|
const staged = `/tmp/${this.document}.FCStd`;
|
|
this.call("save", { path: staged });
|
|
const bytes = this.kernel.FS.readFile(staged);
|
|
return { format: "FCStd", ...storeBytes(name ?? `${this.document}.FCStd`, bytes) };
|
|
}
|
|
|
|
/** Create a body. The default name matches what the desktop workbench uses. */
|
|
body(name = "Body") {
|
|
assertName(name, "body");
|
|
return this.call("createBody", { object: name });
|
|
}
|
|
|
|
// ---- sketch -------------------------------------------------------------
|
|
/**
|
|
* Rectangle and Circle profiles take { Width, Height } / { Radius };
|
|
* `Geometry` takes a pre-built geometry list; `Wire` takes `points`.
|
|
*/
|
|
sketch(object, { body = "Body", profileType = "Rectangle", parameters, position, points, placementRotation, label } = {}) {
|
|
assertName(object, "sketch");
|
|
if (!PROFILE_TYPES.includes(profileType)) {
|
|
throw new KernelError(`unknown profileType ${JSON.stringify(profileType)}; expected ${PROFILE_TYPES.join(", ")}`, { code: "bad_profile" });
|
|
}
|
|
const args = {
|
|
object,
|
|
body,
|
|
profileType,
|
|
position: vector3(position, [0, 0, 0], "sketch position"),
|
|
};
|
|
if (profileType === "Wire") {
|
|
if (!Array.isArray(points) || points.length < 3) {
|
|
throw new KernelError("Wire profile needs at least three points", { code: "bad_profile" });
|
|
}
|
|
args.points = points;
|
|
} else if (profileType !== "Geometry") {
|
|
if (!parameters || typeof parameters !== "object") {
|
|
throw new KernelError(`${profileType} profile needs parameters`, { code: "bad_profile" });
|
|
}
|
|
if (profileType === "Circle") assertPositive(parameters.Radius, "Radius");
|
|
else {
|
|
assertPositive(parameters.Width, "Width");
|
|
assertPositive(parameters.Height, "Height");
|
|
}
|
|
args.parameters = parameters;
|
|
}
|
|
if (Array.isArray(placementRotation)) {
|
|
if (placementRotation.length !== 4) throw new KernelError("placementRotation must have 4 numbers", { code: "bad_vector" });
|
|
args.placementRotation = placementRotation;
|
|
}
|
|
let result = this.call("setSketch", args);
|
|
if (label) result = this.call("setLabel", { object, label: String(label) });
|
|
return result;
|
|
}
|
|
|
|
// ---- profile features ---------------------------------------------------
|
|
/**
|
|
* Pad / Pocket / Revolution / Groove / Hole / Loft / Pipe.
|
|
* `length` is the native dimension for Pad and Pocket; Revolution and Groove
|
|
* take `angle`; Hole takes `depth` (or depthType "ThroughAll").
|
|
*/
|
|
feature(object, { featureType = "Pad", body = "Body", profile, length, angle, depth, depthType, midplane, reversed, axis, label } = {}) {
|
|
assertName(object, "feature");
|
|
if (!FEATURES.includes(featureType)) {
|
|
throw new KernelError(`unknown featureType ${JSON.stringify(featureType)}; expected ${FEATURES.join(", ")}`, { code: "bad_feature" });
|
|
}
|
|
assertName(profile, "profile");
|
|
const args = { object, featureType, body, profile };
|
|
if (featureType === "Revolution" || featureType === "Groove") {
|
|
if (angle != null) args.angle = assertPositive(angle, "angle");
|
|
if (axis) args.axis = axis;
|
|
} else if (featureType === "Hole") {
|
|
if (depthType) args.depthType = depthType;
|
|
if (depth != null) args.depth = assertPositive(depth, "depth");
|
|
} else if (featureType !== "Loft" && featureType !== "Pipe") {
|
|
if (length != null) args.length = assertPositive(length, "length");
|
|
}
|
|
if (midplane != null) args.midplane = Boolean(midplane);
|
|
if (reversed != null) args.reversed = Boolean(reversed);
|
|
let result = this.call("setFeature", args);
|
|
if (label) result = this.call("setLabel", { object, label: String(label) });
|
|
return result;
|
|
}
|
|
|
|
pad(object, args) { return this.feature(object, { ...args, featureType: "Pad" }); }
|
|
pocket(object, args) { return this.feature(object, { ...args, featureType: "Pocket" }); }
|
|
|
|
/** Body primitive, e.g. AdditiveBox / SubtractiveCylinder. */
|
|
primitive(object, { body = "Body", primitiveType, parameters = {}, position, label } = {}) {
|
|
assertName(object, "primitive");
|
|
if (!PRIMITIVES.includes(primitiveType)) {
|
|
throw new KernelError(`unknown primitiveType ${JSON.stringify(primitiveType)}; expected one of ${PRIMITIVES.join(", ")}`, { code: "bad_primitive" });
|
|
}
|
|
const args = { object, body, primitiveType, parameters };
|
|
if (position) args.position = vector3(position, [0, 0, 0], "primitive position");
|
|
let result = this.call("setPrimitive", args);
|
|
if (label) result = this.call("setLabel", { object, label: String(label) });
|
|
return result;
|
|
}
|
|
|
|
/** Involute gear (or sprocket) as a PartDesign feature. */
|
|
gear(object, { body = "Body", gearType = "InvoluteGear", gearParameters, label } = {}) {
|
|
assertName(object, "gear");
|
|
if (!GEAR_TYPES.includes(gearType)) {
|
|
throw new KernelError(`unknown gearType ${JSON.stringify(gearType)}; expected ${GEAR_TYPES.join(", ")}`, { code: "bad_gear" });
|
|
}
|
|
if (!gearParameters || typeof gearParameters !== "object") {
|
|
throw new KernelError("gear requires gearParameters", { code: "bad_gear" });
|
|
}
|
|
if (!Number.isInteger(gearParameters.NumberOfTeeth)) {
|
|
throw new KernelError("NumberOfTeeth must be an integer", { code: "bad_gear" });
|
|
}
|
|
let result = this.call("setGear", { object, body, gearType, gearParameters });
|
|
if (label) result = this.call("setLabel", { object, label: String(label) });
|
|
return result;
|
|
}
|
|
|
|
dressUp(object, { dressUpType, body = "Body", ...rest } = {}) {
|
|
if (!DRESS_UPS.includes(dressUpType)) {
|
|
throw new KernelError(`unknown dressUpType ${JSON.stringify(dressUpType)}; expected ${DRESS_UPS.join(", ")}`, { code: "bad_dressup" });
|
|
}
|
|
return this.call("setDressUp", { object, dressUpType, body, ...rest });
|
|
}
|
|
|
|
/** Mass properties of a selection; returns the kernel's report object. */
|
|
massProperties({ selection, saveResult = false } = {}) {
|
|
if (!Array.isArray(selection) || selection.length === 0) {
|
|
throw new KernelError("massProperties requires a non-empty selection", { code: "bad_request" });
|
|
}
|
|
return this.call("massProperties", { selection, saveResult });
|
|
}
|
|
|
|
// ---- inspection ---------------------------------------------------------
|
|
snapshot({ mesh = false, objects } = {}) {
|
|
const args = { includeMesh: mesh === true, builderInfo: false };
|
|
if (mesh && Array.isArray(objects)) args.includeMeshObjects = objects;
|
|
return this.call("snapshot", args);
|
|
}
|
|
|
|
objects() { return this.snapshot().objects ?? []; }
|
|
objectOf(name) { return this.objects().find(item => item.name === name) ?? null; }
|
|
volumeOf(name) { return this.objectOf(name)?.volume ?? null; }
|
|
|
|
meshOf(name) {
|
|
const result = this.snapshot({ mesh: true, objects: [name] });
|
|
const item = (result?.objects ?? []).find(entry => entry.name === name);
|
|
if (!item) throw new KernelError(`snapshot returned no object ${name}`, { code: "missing_object" });
|
|
const mesh = item.mesh ?? {};
|
|
const round = values => (values ?? []).map(value => Math.round(value * 1000) / 1000);
|
|
const positions = round(mesh.positions);
|
|
return {
|
|
name,
|
|
label: item.label ?? name,
|
|
volume: item.volume ?? null,
|
|
bounds: mesh.bounds ?? null,
|
|
positions,
|
|
normals: round(mesh.normals),
|
|
triangleCount: mesh.triangleCount ?? positions.length / 3,
|
|
};
|
|
}
|
|
|
|
trace() { return this.session.trace(); }
|
|
writeTrace(extra) { return writeTrace(this.session, { extra }); }
|
|
}
|
|
|
|
/**
|
|
* Open a PartDesign document with a Body already in it -- that is the state the
|
|
* desktop workbench hands you, and every feature needs a body to attach to.
|
|
*/
|
|
export async function openPartDesign(document, { kernelDirs, print, printErr, builder, body = "Body", create = true } = {}) {
|
|
if (!document) throw new KernelError("openPartDesign requires a document name", { code: "bad_request" });
|
|
const handle = await openKernel({ stem: STEM_PARTDESIGN, dirs: kernelDirs, print, printErr });
|
|
const session = createSession({
|
|
handle,
|
|
cmd: "partdesign.document",
|
|
schema: PARTDESIGN_SCHEMA,
|
|
module: PARTDESIGN_MODULE,
|
|
document,
|
|
extra: builder ? { builder } : {},
|
|
});
|
|
const part = new PartDesignDocument(session, handle.directory);
|
|
part.dispose = () => handle.close();
|
|
if (create) {
|
|
part.create();
|
|
part.body(body);
|
|
}
|
|
return part;
|
|
}
|