From f034511471389a2388a26de4f9579e736f91d0ca Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Asbj=C3=B8rn=20R=C3=B8rvik?= <11362831+asbis@users.noreply.github.com> Date: Thu, 22 Oct 2026 17:41:56 +0200 Subject: [PATCH] Stop reporting the bounding-box centre as a centre of mass (v1.30.2) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Default renders (partStats "bbox", i.e. no material declared) skip OCCT measurement, and core then filled centerOfMass with the AABB centre. create_shape and get_render_status printed it as `CoM=`: an L-bracket showed CoM=(0.00, 0.00, 17.50) against a true (0, -7.5, 10), which misleads any agent reasoning about tipping or balance. - core: "bbox" no longer sets centerOfMass. The bbox centre was already on every part as boundsCenter, so nothing is lost. - mcp-server: per-part rows print CoM= only for a measured value, and otherwise `bbox centre=(…)` from the bbox min/max. The get_render_status description says volume/area/CoM/mass need a material. - viewer: the parts panel reads boundsCenter for "Bounds center" and only says "Center of mass" for a measured one. - Assembly CoM aggregation is unchanged: it weights by measured volume, so the estimate never fed it. - Test: an L-bracket assembly through the real engine. The default render has no CoM= and labels the bbox centre; partStats "full" still reports (0, -7.5, 10) per part plus the volume-weighted aggregate. - Versions: extension, mcp-server and the plugin marketplace move to 1.30.2, with a CHANGELOG entry. Co-Authored-By: Claude Opus 5.5 --- .claude-plugin/marketplace.json | 2 +- CHANGELOG.md | 18 +++++++ packages/core/src/index.ts | 33 ++++++------- packages/core/src/tessellate.ts | 24 ++++++---- packages/extension/package.json | 2 +- packages/mcp-server/package.json | 2 +- packages/mcp-server/src/engine.ts | 5 ++ packages/mcp-server/src/tools.test.ts | 67 ++++++++++++++++++++++++++- packages/mcp-server/src/tools.ts | 9 +++- packages/shared/src/messages.ts | 5 +- packages/viewer/src/index.ts | 23 +++++---- skills/shapeitup/SKILL.md | 2 +- 12 files changed, 149 insertions(+), 43 deletions(-) diff --git a/.claude-plugin/marketplace.json b/.claude-plugin/marketplace.json index 4a81869..e36a156 100644 --- a/.claude-plugin/marketplace.json +++ b/.claude-plugin/marketplace.json @@ -12,7 +12,7 @@ { "name": "shapeitup", "description": "CAD scripting toolkit: create/modify .shape.ts files, validate geometry, check collisions, export STEP/STL/3MF via Replicad + OpenCascade. Runs headless; live 3D preview available if the ShapeItUp VS Code extension is installed.", - "version": "1.30.1", + "version": "1.30.2", "author": { "name": "Asbjorn Rorvik", "email": "rorvik.asbjorn@gmail.com" diff --git a/CHANGELOG.md b/CHANGELOG.md index 7c001c2..9ae1345 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,6 +9,24 @@ Releases 1.17.0–1.24.0 are described only in their Format based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/); the project follows semantic versioning at the extension level. +## [1.30.2] - 2026-10-22 + +Extension `1.30.2` / mcp-server `1.30.2` — a centre of mass is only reported +when it was measured. + +### Fixed +- **The bounding-box centre was reported as the centre of mass.** Without a + declared material, renders skip OCCT measurement, and core then filled + each part's `centerOfMass` with its bounding-box centre. `create_shape` and + `get_render_status` printed that as `CoM=`: an L-bracket showed + `CoM=(0.00, 0.00, 17.50)` against a true `(0, -7.5, 10)`, so an agent + working out tipping or balance got a wrong answer that looked measured. + `centerOfMass` is now only set when the part was measured (a declared + `material`, or `partStats: "full"`). Otherwise the per-part row reads + `bbox centre=(…)`, and the viewer's parts panel shows "Bounds center" from + `boundsCenter`. The assembly centre of mass is unchanged. It is weighted + by measured volume, so it never included the estimate. + ## [1.30.1] - 2026-10-12 Extension `1.30.1` / mcp-server `1.30.1` — Codex setup docs and three fixes. diff --git a/packages/core/src/index.ts b/packages/core/src/index.ts index d66b82c..e049cda 100644 --- a/packages/core/src/index.ts +++ b/packages/core/src/index.ts @@ -774,7 +774,8 @@ export interface Core { meshQuality?: MeshQuality; /** * Issue #6: how much per-part measurement to do. Default is `"bbox"` — - * fast CoM from the AABB centre, volume / surfaceArea omitted. Pass + * no measurement, so volume / surfaceArea / centerOfMass are omitted + * (the AABB centre is always on `boundsCenter`). Pass * `"full"` only when the caller actually needs the OCCT-measured * numbers (e.g. an aggregator computing total mass / CoM). */ @@ -1344,9 +1345,9 @@ export async function initCore( (shouldAutoDegrade ? "preview" : "final"); // Issue #6: gate per-part B-Rep measurement. On a 14-part assembly the // two measureShape*Properties calls together spent ~2.5 s on the hot - // path. Default `"bbox"` skips both and derives centerOfMass from the - // AABB centre — that's still a useful approximation for assembly - // aggregation, and per-part volume/surfaceArea rarely gets consumed. + // path. Default `"bbox"` skips both, so volume / surfaceArea / + // centerOfMass stay unmeasured — per-part volume/surfaceArea rarely gets + // consumed, and a guessed centre of mass is worse than none. // A declared material — script-level `export const material` OR a per-part // `material` on any returned part — is an explicit opt-in to mass // reporting, and mass needs the OCCT-measured volume that only "full" @@ -1423,22 +1424,16 @@ export async function initCore( if (effectiveMaterial && typeof t.volume === "number") { t.mass = (effectiveMaterial.density * t.volume) / 1000; } - } else if (partStats === "bbox") { - // Cheap CoM: use the shape's AABB centre (O(1), no OCCT measure). - // volume / surfaceArea / mass stay omitted — callers who need - // exact numbers pass partStats: "full". Fall through silently if - // the shape has no usable bbox (MeshShape duck-type, test mocks). - const bounds = readBoundsSafe(shape); - if (bounds) { - t.centerOfMass = [ - (bounds[0][0] + bounds[1][0]) / 2, - (bounds[0][1] + bounds[1][1]) / 2, - (bounds[0][2] + bounds[1][2]) / 2, - ]; - } } - // partStats === "none": nothing to do. volume/surfaceArea/mass/CoM - // all stay undefined on the emitted part. + // partStats "bbox" / "none": nothing to measure. volume/surfaceArea/ + // mass/centerOfMass all stay undefined on the emitted part. + // + // "bbox" used to fill centerOfMass with the AABB centre. That is not a + // centre of mass for anything asymmetric (an L-bracket's reads + // (0, 0, 17.5) against a true (0, -7.5, 10)), and every consumer + // printed it as one, so an agent reasoning about tipping got a wrong + // answer with no hint that it was a guess. The bbox centre is still on + // the part as `boundsCenter`, which tessellatePart sets at every level. // Face descriptors for viewer picking. Deliberately NOT gated on // partStats: that knob trades measurement cost against BOM numbers, diff --git a/packages/core/src/tessellate.ts b/packages/core/src/tessellate.ts index 59664a5..10dc836 100644 --- a/packages/core/src/tessellate.ts +++ b/packages/core/src/tessellate.ts @@ -175,13 +175,19 @@ export interface TessellatedPart { edgeGroups?: Uint32Array; volume?: number; surfaceArea?: number; + /** + * The measured centroid — only set at `partStats: "full"` (OCCT's volume + * properties, or tetrahedron integration of the mesh for a MeshShape). + * Never an estimate: when nothing was measured this is undefined, and the + * bounding-box centre is on {@link boundsCenter} instead. + */ centerOfMass?: [number, number, number]; /** * The centre of the OCCT bounding box — exactly what `shape.boundingBox.center` - * evaluates to in a user's script. + * evaluates to in a user's script. Set at every `partStats` level. * - * Distinct from `centerOfMass`, which is the true centroid when the part was - * measured in full and only falls back to this. The viewer's Rotate command + * Distinct from `centerOfMass`, which is the true centroid and is only there + * when the part was measured in full. The viewer's Rotate command * needs the value the FILE will compute, not the one that is geometrically * nicer, because the pivot it writes is that expression. */ @@ -420,10 +426,12 @@ export interface FaceInfo { * from the emitted part. Cheapest; use when the caller only needs geometry * for rendering. * - * `"bbox"` (default) — still skip the two OCCT measurement calls, but derive - * a centerOfMass from the shape's bounding-box centre so assemblies get a - * usable aggregate CoM without paying the ~200 ms/part B-Rep cost. volume / - * surfaceArea stay omitted. + * `"bbox"` (default) — skip the two OCCT measurement calls; volume / + * surfaceArea / centerOfMass stay omitted. The bounding-box centre is still + * reported, as `boundsCenter` (every level gets it), and never as a + * centerOfMass: on an L-bracket the two are 10.6 mm apart. Today + * this measures exactly as little as `"none"`; the name is kept because + * callers pass it. * * `"full"` — pre-partStats behaviour: call both measurement functions and * populate volume / surfaceArea / centerOfMass (plus mass if a material is @@ -462,7 +470,7 @@ export interface TessellateOptions { * `tessellatePart` itself (which never calls measureShape*Properties) — * co-located here so callers configure all tess-time policy in one place. * Default is `"bbox"` (issue #6: skip 2.5 s of B-Rep measurement on a - * 14-part assembly; derive CoM from the AABB centre instead). + * 14-part assembly; volume / CoM are then simply not reported). */ partStats?: PartStatsLevel; } diff --git a/packages/extension/package.json b/packages/extension/package.json index 120dc23..a3e36f9 100644 --- a/packages/extension/package.json +++ b/packages/extension/package.json @@ -2,7 +2,7 @@ "name": "shapeitup-vscode", "displayName": "ShapeItUp", "description": "Scripted CAD for VS Code — write TypeScript, see 3D, export STEP/STL/3MF (Bambu Studio & OrcaSlicer ready). AI-powered with Claude Code MCP integration.", - "version": "1.30.1", + "version": "1.30.2", "publisher": "shapeitup", "license": "MIT", "icon": "icon.png", diff --git a/packages/mcp-server/package.json b/packages/mcp-server/package.json index c3c56c9..6c94a5c 100644 --- a/packages/mcp-server/package.json +++ b/packages/mcp-server/package.json @@ -1,6 +1,6 @@ { "name": "@shapeitup/mcp-server", - "version": "1.30.1", + "version": "1.30.2", "description": "ShapeItUp MCP server — scripted CAD tools (Replicad/OpenCascade) for Claude Code, Codex, Cursor, Claude Desktop, and other MCP clients.", "type": "module", "bin": { diff --git a/packages/mcp-server/src/engine.ts b/packages/mcp-server/src/engine.ts index 16f2c0b..b18e2b1 100644 --- a/packages/mcp-server/src/engine.ts +++ b/packages/mcp-server/src/engine.ts @@ -629,6 +629,11 @@ export interface ShapeProperties { name: string; volume?: number; surfaceArea?: number; + /** + * Measured centroid. Present only when core measured the part + * (`partStats: "full"`, which a declared material implies) — never the + * bounding-box centre, which is `boundingBox.min`/`max`'s midpoint. + */ centerOfMass?: [number, number, number]; /** * Per-part bounding box. `x`/`y`/`z` remain the SIZE along each axis (back diff --git a/packages/mcp-server/src/tools.test.ts b/packages/mcp-server/src/tools.test.ts index d9e1149..c13f881 100644 --- a/packages/mcp-server/src/tools.test.ts +++ b/packages/mcp-server/src/tools.test.ts @@ -1,6 +1,9 @@ import { describe, it, expect } from "vitest"; import { validateSyntaxPure, detectPathDoubling, detectPathDoublingInfo, extractSignatures, safeHandler, computePartsLine, computeEffectiveMeshQuality, formatCollisionPairs, formatSweepCollisions, formatLastScreenshotLine, registerTools, getVersionTag, getViewerStatus, formatViewerBlock, partExtendsBelowBed, formatStatusText, workspaceMismatchWarning, type CollisionEntry, type SweepCollisionEntry } from "./tools.js"; -import type { EngineStatus } from "./engine.js"; +import { executeShapeFile, type EngineStatus } from "./engine.js"; +import { mkdtempSync, writeFileSync, rmSync } from "node:fs"; +import { tmpdir } from "node:os"; +import { join } from "node:path"; // --------------------------------------------------------------------------- // Bug #6 — validate_syntax must trust .method() calls whose receiver was @@ -1502,6 +1505,68 @@ describe("formatStatusText — geometry warning dedup", () => { }); }); +// An L-bracket's bounding-box centre is (0, 0, 17.5); its centre of mass is +// (0, -7.5, 10). The default render used to print the first as `CoM=`, which +// sends an agent reasoning about tipping or balance the wrong way. The second +// part is only there because per-part rows print for assemblies. +describe("formatStatusText — a centre of mass is only claimed when measured", () => { + const BRACKET = [ + `import { drawRectangle, drawCircle } from "replicad";`, + `export default function main() {`, + ` const base = drawRectangle(60, 40).sketchOnPlane("XY").extrude(5);`, + ` const wall = drawRectangle(60, 5).sketchOnPlane("XY").extrude(35).translate(0, -17.5, 0);`, + ` const pin = drawCircle(2).sketchOnPlane("XY").extrude(10).translate(0, 10, 5);`, + ` return [`, + ` { shape: base.fuse(wall), name: "bracket" },`, + ` { shape: pin, name: "pin" },`, + ` ];`, + `}`, + ].join("\n"); + + const run = async (partStats?: "full") => { + const workdir = mkdtempSync(join(tmpdir(), "siu-com-")); + const storage = mkdtempSync(join(tmpdir(), "siu-com-storage-")); + try { + const entry = join(workdir, "bracket.shape.ts"); + writeFileSync(entry, BRACKET); + const { status } = await executeShapeFile(entry, storage, undefined, partStats ? { partStats } : undefined); + expect(status.success).toBe(true); + return { status, text: formatStatusText(status) }; + } finally { + rmSync(workdir, { recursive: true, force: true }); + rmSync(storage, { recursive: true, force: true }); + } + }; + + it("labels the bounding-box centre as such in a default render", async () => { + const { status, text } = await run(); + const bracket = status.properties!.parts!.find((p) => p.name === "bracket")!; + expect(bracket.centerOfMass).toBeUndefined(); + expect(status.properties!.centerOfMass).toBeUndefined(); + expect(text).not.toContain("CoM="); + expect(text).not.toContain("center of mass"); + expect(text).toMatch(/- bracket: .*bbox centre=\(0\.00, 0\.00, 17\.50\)/); + }, 60_000); + + it("still reports the measured centre of mass, per part and aggregated, at partStats full", async () => { + const { status, text } = await run("full"); + const bracket = status.properties!.parts!.find((p) => p.name === "bracket")!; + const [x, y, z] = bracket.centerOfMass!; + expect(x).toBeCloseTo(0, 3); + expect(y).toBeCloseTo(-7.5, 3); + expect(z).toBeCloseTo(10, 3); + // OCCT's x comes back as a hair below zero, so it prints "-0.00". + expect(text).toMatch(/- bracket: .*CoM=\(-?0\.00, -7\.50, 10\.00\)/); + expect(text).not.toContain("bbox centre"); + + // Volume-weighted over both bodies: 21000 mm³ at y = -7.5, a 2 mm pin at y = 10. + const pinVolume = Math.PI * 2 * 2 * 10; + const aggregate = status.properties!.centerOfMass!; + expect(aggregate[1]).toBeCloseTo((21000 * -7.5 + pinVolume * 10) / (21000 + pinVolume), 3); + expect(text).toContain("center of mass:"); + }, 60_000); +}); + describe("workspaceMismatchWarning — relative-dir routed to wrong workspace", () => { const base = { directory: "examples/wall-organizer", diff --git a/packages/mcp-server/src/tools.ts b/packages/mcp-server/src/tools.ts index 06e8a11..9f5a44b 100644 --- a/packages/mcp-server/src/tools.ts +++ b/packages/mcp-server/src/tools.ts @@ -5287,7 +5287,7 @@ export function registerTools(server: McpServer) { server.tool( "get_render_status", - "Get the result of the last shape render — whether it succeeded or failed, with stats, geometric properties (volume, area, center of mass, mass when material is exported), and bounding box. Reads the shared status file, which both MCP-driven and VSCode-driven renders write to. Includes currentParams — the resolved values of every exported param, so you don't need to re-read the file to inspect parameter state. For multi-part assemblies, returns per-part stats (name, volume, surface area, center of mass, bounding box, mass) — no separate list_parts tool needed.", + "Get the result of the last shape render — whether it succeeded or failed, with stats, geometric properties, and bounding box. Volume, area, center of mass and mass are measured only when the script exports a material; without one they are omitted, never estimated. Reads the shared status file, which both MCP-driven and VSCode-driven renders write to. Includes currentParams — the resolved values of every exported param, so you don't need to re-read the file to inspect parameter state. For multi-part assemblies, returns per-part stats (name, volume, surface area, center of mass, bounding box, mass; the bounding-box centre when no center of mass was measured) — no separate list_parts tool needed.", {}, safeHandler("get_render_status", async () => { const statusFile = join(GLOBAL_STORAGE, "shapeitup-status.json"); @@ -7305,6 +7305,13 @@ function formatProperties( if (typeof p.mass === "number") bits.push(`mass=${fmt(p.mass)}g`); if (p.centerOfMass) bits.push(`CoM=${fmtPt(p.centerOfMass)}`); if (p.boundingBox) bits.push(`bbox=${fmt(p.boundingBox.x)}x${fmt(p.boundingBox.y)}x${fmt(p.boundingBox.z)}`); + // Where the part sits, for when nothing was measured. Named for what it + // is: on an L-bracket it is 10.6 mm from the real centre of mass, and it + // used to be printed as `CoM=`. + const { min, max } = p.boundingBox ?? {}; + if (!p.centerOfMass && min && max) { + bits.push(`bbox centre=${fmtPt([(min[0] + max[0]) / 2, (min[1] + max[1]) / 2, (min[2] + max[2]) / 2])}`); + } if (bits.length) lines.push(` - ${p.name}: ${bits.join(", ")}`); // Printability: only surface the section when there's actually a // concern — clean parts (manifold + feature ≥ nozzle) stay silent so diff --git a/packages/shared/src/messages.ts b/packages/shared/src/messages.ts index b29c2e3..298735a 100644 --- a/packages/shared/src/messages.ts +++ b/packages/shared/src/messages.ts @@ -523,7 +523,10 @@ export interface TessellatedPart { */ boundsCenter?: [number, number, number]; // Geometric properties computed from the original OCCT shape (not the mesh). - // Optional because measurement can fail on degenerate geometry. + // Only present when the core measured the part (`partStats: "full"`, which a + // declared material implies), and even then measurement can fail on + // degenerate geometry. centerOfMass is never a bounding-box estimate — that + // is `boundsCenter`. volume?: number; surfaceArea?: number; centerOfMass?: [number, number, number]; diff --git a/packages/viewer/src/index.ts b/packages/viewer/src/index.ts index 37a8a3a..00f6e06 100644 --- a/packages/viewer/src/index.ts +++ b/packages/viewer/src/index.ts @@ -327,7 +327,10 @@ interface PartInfo { * the point the written expression will. */ boundsCenter?: [number, number, number]; - /** Measured on the OCCT shape, not the mesh. Absent on degenerate geometry. */ + /** + * Measured on the OCCT shape, not the mesh. Absent unless the core measured + * the part (a declared material), or when measurement failed. + */ volume?: number; surfaceArea?: number; centerOfMass?: [number, number, number]; @@ -3180,7 +3183,8 @@ function updatePartsList() { const hasStats = part.volume !== undefined || part.surfaceArea !== undefined || - part.centerOfMass !== undefined; + part.centerOfMass !== undefined || + part.boundsCenter !== undefined; const row = document.createElement("div"); const armed = @@ -3234,14 +3238,15 @@ function updatePartsList() { }; if (part.volume !== undefined) addProp("Volume", formatVolume(part.volume)); if (part.surfaceArea !== undefined) addProp("Surface", formatArea(part.surfaceArea)); - if (part.centerOfMass) { - // Which centre this is depends on how the part was measured. Without - // volume the core took the cheap path and this is the bounding-box - // centre, which is NOT the centre of mass for anything asymmetric. - // Labelling both "Center" would quietly overstate one of them. + // The core only sets centerOfMass when it measured one; otherwise the + // nearest thing it has is the bounding-box centre, which is NOT the centre + // of mass for anything asymmetric. Labelling both "Center" would quietly + // overstate one of them. + const centre = part.centerOfMass ?? part.boundsCenter; + if (centre) { addProp( - part.volume === undefined ? "Bounds center" : "Center of mass", - part.centerOfMass.map((n) => formatNum(n, 1)).join(", "), + part.centerOfMass ? "Center of mass" : "Bounds center", + centre.map((n) => formatNum(n, 1)).join(", "), true, ); } diff --git a/skills/shapeitup/SKILL.md b/skills/shapeitup/SKILL.md index 2b941f1..90c797d 100644 --- a/skills/shapeitup/SKILL.md +++ b/skills/shapeitup/SKILL.md @@ -233,7 +233,7 @@ return [ ]; ``` -Each part gets its own color in the viewer and becomes a named component in STEP files. Per-part stats (volume, surface area, CoM, bbox, mass) are returned by `get_render_status`. +Each part gets its own color in the viewer and becomes a named component in STEP files. Per-part stats (volume, surface area, CoM, bbox, mass) are returned by `get_render_status`. Volume, area, CoM and mass are measured only when the file exports a `material`; without one a part reports just its bbox and `bbox centre`, which is not its centre of mass. Mark mockup / reference parts with `analyze: false` to suppress printability and minFeature warnings for geometry you won't fabricate (motors, pre-bought tubes, collision-check stand-ins):