Inspect: measure names_only lists every world body without measuring (#10402)

A caller that only needs the bodies' names (assembly occurrences included,
e.g. "Body2 (Part:2)") had to measure every body, or read document.inspect,
which lists definition bodies only. `inspect.measure {names_only: true}`
returns each world body's name and its definition's name, honouring the
`bodies` filter; an unknown name is refused before anything is measured.
The MCP `measure` tool passes it through, directly and as a batch step;
`summary` leaves such a list as it is, since there is nothing to narrow.

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
NOWN-DEVandClaude Opus 5.5 authored and GitHub committed 2026-11-01 02:12:23 +00:00
1 parent 7366fe0ecc
commit a69a7c6ffe
4 files changed
+54 -15

No files matched your search

+8 -4
View File
@@ -27,7 +27,7 @@ pub static COMMANDS: &[CommandSpec] = &[
.icon("measure")
.key("I")
.noundo()
.params("bodies?: [names] (default all) → closed per body; volume_mm3, center_of_mass, density_g_cm3 and mass_g only for closed, consistently oriented bodies; area, bbox and counts for all; total.open_bodies_skipped counts bodies excluded from volume/mass totals; or items: [1-2 selections] (face/edge: type, body, point; index optional) → length/area/volume; diameter, radius, circumference or arc_length/angle_deg of circles, arcs and cylinders; slot sizes; min (distance_mm, from, to), max, center_distance (centre to centre, or from an axis) and angle"),
.params("bodies?: [names] (default all); names_only?: bool (each world body's name and its definition's, nothing measured) → closed per body; volume_mm3, center_of_mass, density_g_cm3 and mass_g only for closed, consistently oriented bodies; area, bbox and counts for all; total.open_bodies_skipped counts bodies excluded from volume/mass totals; or items: [1-2 selections] (face/edge: type, body, point; index optional) → length/area/volume; diameter, radius, circumference or arc_length/angle_deg of circles, arcs and cylinders; slot sizes; min (distance_mm, from, to), max, center_distance (centre to centre, or from an axis) and angle"),
CommandSpec::new("document.inspect", "Inspect Design", inspect).icon("inspect").noundo().params("measure?: bool (include body measurements)"),
CommandSpec::new("document.revision", "Design Revision", revision).icon("inspect").noundo().params(
"→ document_id (this open design's id: kept while it stays open, another for each design started or opened), \
@@ -123,6 +123,13 @@ fn measure(s: &mut Session, p: &Value) -> Result<Value> {
let want = string_list(cmd, p, "bodies")?;
let st = s.world_state();
let selected: Vec<_> = st.bodies.iter().filter(|b| want.is_empty() || want.contains(&b.name)).collect();
if !want.is_empty() && selected.len() != want.len() {
return Err(bad(cmd, "unknown body name"));
}
if bool_(p, "names_only").unwrap_or(false) {
let bodies: Vec<_> = selected.iter().map(|b| json!({"name": b.name, "definition": b.definition_name()})).collect();
return Ok(json!({"body_count": bodies.len(), "bodies": bodies}));
}
// Each body is measured on its own: on all cores, in body order.
let mut bodies: Vec<Value> = solidcraft_kernel::map_on_threads(&selected, 16, |b| measure_json(b))
.into_iter()
@@ -140,9 +147,6 @@ fn measure(s: &mut Session, p: &Value) -> Result<Value> {
b["mass_g"] = json!(v * rho / 1000.0);
}
}
if !want.is_empty() && bodies.len() != want.len() {
return Err(bad(cmd, "unknown body name"));
}
// Sheet metal keeps the edges where bends meet flat faces (so bends stay visible): count its
// B-rep faces as they are, without merging coplanar neighbours.
let sheets = s.model.state().sheets.clone();
+20
View File
@@ -124,3 +124,23 @@ fn bench_generated_assembly() {
"volume_mm3": m["total"]["volume_mm3"]})
);
}
#[test]
fn names_only_lists_every_world_body_without_measuring() {
let mut s = Session::new(generated_assembly(3));
let full = s.execute("inspect.measure", &json!({})).unwrap();
let names = s.execute("inspect.measure", &json!({"names_only": true})).unwrap();
assert_eq!(names["body_count"], full["body_count"]);
let world = s.world_state();
for ((n, f), b) in names["bodies"].as_array().unwrap().iter().zip(full["bodies"].as_array().unwrap()).zip(&world.bodies) {
assert_eq!(n["name"], f["name"]);
assert_eq!(n["definition"], json!(b.definition_name()));
assert!(n.get("volume_mm3").is_none(), "{n}");
}
assert!(names.get("total").is_none());
// The `bodies` filter holds, and an unknown name is still refused.
let first = names["bodies"][0]["name"].clone();
let one = s.execute("inspect.measure", &json!({"names_only": true, "bodies": [first]})).unwrap();
assert_eq!(one["body_count"], 1);
assert!(s.execute("inspect.measure", &json!({"names_only": true, "bodies": ["no such body"]})).is_err());
}
+7
View File
@@ -809,6 +809,13 @@ fn batch_stops_at_first_error_and_rolls_back() {
assert_eq!(r["result"]["isError"], true);
assert!(r["result"]["content"][0]["text"].as_str().unwrap().contains("solid.extrude"));
assert_eq!(tool(&mut s, "measure", json!({}))["body_count"], 2);
// `names_only` passes through, directly and as a batch step.
let n = tool(&mut s, "measure", json!({"names_only": true}));
assert_eq!(n["body_count"], 2, "{n}");
assert!(n["bodies"][0]["name"].is_string() && n["bodies"][0].get("volume_mm3").is_none(), "{n}");
assert_eq!(tool(&mut s, "measure", json!({"names_only": true, "summary": true})), n, "summary has nothing to narrow in a names list");
let r = tool(&mut s, "batch", json!({"commands": [{"command": "measure", "params": {"names_only": true, "summary": true}}]}));
assert!(r.to_string().contains("\"definition\""), "{r}");
}
/// #5853: a misspelled command, however late in the batch, refuses the batch before its first
+19 -11
View File
@@ -206,6 +206,7 @@ pub fn tool_definitions() -> Vec<Value> {
obj(
json!({
"body": string("Body name (default: all bodies)"),
"names_only": {"type": "boolean", "description": "Only each body's name (assembly occurrences included) and its definition's, without measuring (default false)"},
"items": {"type": "array", "items": {"type": "object"}, "description": "One or two selections to measure instead of bodies, e.g. {\"type\": \"face\", \"body\": \"Body1\", \"index\": 3, \"point\": [x,y,z]}"},
"summary": {"type": "boolean", "description": "Per body only name, volume_mm3, bbox {min, max}, faces and closed, and total.volume_mm3 (default false: the full reply)"},
}),
@@ -784,6 +785,21 @@ fn str_arg<'a>(a: &'a Map<String, Value>, k: &str) -> Option<&'a str> {
a.get(k).and_then(Value::as_str)
}
/// `measure`'s arguments (without `items`) as `inspect.measure` takes them.
fn measure_params(a: &Map<String, Value>) -> Value {
let mut p = pick(a, &["names_only"]);
if let Some(body) = str_arg(a, "body") {
p["bodies"] = json!([body]);
}
p
}
/// `summary` narrows measured bodies; a `names_only` list has nothing to narrow.
fn measure_summary_wanted(a: &Map<String, Value>) -> bool {
let flag = |k: &str| a.get(k).and_then(Value::as_bool).unwrap_or(false);
flag("summary") && !flag("names_only")
}
fn dispatch(b: &mut dyn Backend, name: &str, a: &Map<String, Value>) -> Result<Value, String> {
match name {
"list_commands" => {
@@ -824,12 +840,8 @@ fn dispatch(b: &mut dyn Backend, name: &str, a: &Map<String, Value>) -> Result<V
if let Some(items) = a.get("items") {
return exec(b, "inspect.measure", json!({ "items": items }));
}
let p = match str_arg(a, "body") {
Some(body) => json!({"bodies": [body]}),
None => json!({}),
};
let m = exec(b, "inspect.measure", p)?;
Ok(if a.get("summary").and_then(Value::as_bool).unwrap_or(false) { measure_summary(m) } else { m })
let m = exec(b, "inspect.measure", measure_params(a))?;
Ok(if measure_summary_wanted(a) { measure_summary(m) } else { m })
}
"body_topology" => {
let p = pick(a, &["body"]);
@@ -948,11 +960,7 @@ fn batch_step(step: &Value) -> Result<(String, Value, Option<Narrow>), String> {
}
"measure" => match a.get("items") {
Some(items) => mapped("inspect.measure", json!({"items": items})),
None => {
let p = str_arg(a, "body").map_or(json!({}), |body| json!({"bodies": [body]}));
let summary = a.get("summary").and_then(Value::as_bool).unwrap_or(false);
Ok(("inspect.measure".into(), p, summary.then_some(Narrow::MeasureSummary)))
}
None => Ok(("inspect.measure".into(), measure_params(a), measure_summary_wanted(a).then_some(Narrow::MeasureSummary))),
},
"inspect_design" => mapped("document.inspect", json!({"measure": a.get("measure").and_then(Value::as_bool).unwrap_or(true)})),
"undo" => mapped("edit.undo", json!({})),