Skip to content

SolidCraft: an open-source parametric 3D CAD application built in pure Rust. Desktop, web, and automation through MCP.

Body names: a placed copy's name is taken by the rule, repair and configuration column commands (#12584)

Danielea46e0730049
diff --git a/crates/engine/src/cmd/browser.rs b/crates/engine/src/cmd/browser.rs
index cbb624bd66..cfd7e681d4 100644
--- a/crates/engine/src/cmd/browser.rs
+++ b/crates/engine/src/cmd/browser.rs
@@ -1193,7 +1193,9 @@ fn repair_body(s: &mut Session, p: &Value) -> Result<Value> {
use solidcraft_doc::expr::Kind;
use solidcraft_kernel::{Continuity, SurfaceKind};
let cmd = "body.repair";
- let name = str_(p, "body").filter(|b| !b.trim().is_empty()).ok_or_else(|| bad(cmd, "`body` must name the part to repair"))?.to_string();
+ let name = str_(p, "body").filter(|b| !b.trim().is_empty()).ok_or_else(|| bad(cmd, "`body` must name the part to repair"))?;
+ // A placed copy's name means the body it shows (#9740); the repair goes on that body.
+ let name = s.own_body_name(name).ok_or_else(|| bad(cmd, format!("no body `{name}`")))?;
let state = s.model.state();
let source = state.body(&name).ok_or_else(|| bad(cmd, format!("no body `{name}`")))?;
if source.body.is_mesh() {
@@ -1546,6 +1548,23 @@ mod tests {
assert!(back.iter().any(|n| n == "KB") && back.iter().any(|n| n == "KB (K:2)"), "{back:?}");
}
+ /// Repair takes a placed copy's name (`KB (K:2)`) for the body it shows, as Rename does.
+ #[test]
+ fn repair_part_takes_a_placed_copys_name() {
+ let mut s = Session::default();
+ run(&mut s, "component.create", json!({"name": "K"}));
+ run(&mut s, "solid.box", json!({"body_name": "KB", "length": 10, "width": 10, "height": 10}));
+ run(&mut s, "surface.unstitch", json!({"body": "KB", "faces": [[5, 5, 10]]}));
+ run(&mut s, "occurrence.copy", json!({"component": "K", "translate": [30, 0, 0]}));
+ assert!(s.model.state().body("KB").is_some_and(|b| b.body.is_surface()));
+ let features = s.doc.features.len();
+ run(&mut s, "body.repair", json!({"body": "KB (K:2)", "patch_holes": true}));
+ assert_eq!(s.doc.features.len(), features + 1);
+ assert!(s.model.state().body("KB").is_some_and(|b| !b.body.is_surface()), "the body both placements show is closed");
+ let err = s.execute("body.repair", &json!({"body": "KB (K:9)"})).unwrap_err().to_string();
+ assert!(err.contains("no body `KB (K:9)`"), "{err}");
+ }
+
#[test]
fn repair_part_patches_a_missing_face_as_one_undoable_feature() {
let mut s = Session::default();
diff --git a/crates/engine/src/cmd/config.rs b/crates/engine/src/cmd/config.rs
index cd43fbd321..0a263e5ac1 100644
--- a/crates/engine/src/cmd/config.rs
+++ b/crates/engine/src/cmd/config.rs
@@ -136,10 +136,12 @@ fn column_of(s: &Session, p: &Value, cmd: &str) -> Result<Column> {
for (key, appearance) in [("material", false), ("appearance", true)] {
if let Some(body) = str_(p, key) {
let c = if appearance { Column::Appearance(body.to_string()) } else { Column::Material(body.to_string()) };
- if !s.doc.configs.columns.contains(&c) && s.model.state().body(body).is_none() {
- return Err(bad(cmd, format!("no body `{body}`")));
+ if s.doc.configs.columns.contains(&c) {
+ return Ok(c);
}
- return Ok(c);
+ // A placed copy's name means the body it shows (#9740); the column is keyed by that body's own name.
+ let body = s.own_body_name(body).ok_or_else(|| bad(cmd, format!("no body `{body}`")))?;
+ return Ok(if appearance { Column::Appearance(body) } else { Column::Material(body) });
}
}
if let Some(n) = str_(p, "param") {
@@ -538,6 +540,20 @@ mod tests {
use crate::Session;
+ /// A placed copy's body name (`KB (K:2)`, as inspect.measure lists it) means the body it
+ /// shows: the column is keyed by that body's own name, for every placement.
+ #[test]
+ fn a_column_by_a_placed_copys_name_is_keyed_by_the_body_it_shows() {
+ let mut s = Session::default();
+ run(&mut s, "component.create", json!({"name": "K"}));
+ run(&mut s, "solid.box", json!({"body_name": "KB", "length": 10, "width": 10, "height": 10}));
+ run(&mut s, "occurrence.copy", json!({"component": "K", "translate": [30, 0, 0]}));
+ run(&mut s, "config.column", json!({"material": "KB (K:2)"}));
+ assert!(s.doc.configs.columns.contains(&super::Column::Material("KB".into())), "{:?}", s.doc.configs.columns);
+ let err = s.execute("config.column", &json!({"material": "KB (K:9)"})).unwrap_err().to_string();
+ assert!(err.contains("no body `KB (K:9)`"), "{err}");
+ }
+
fn run(s: &mut Session, id: &str, p: Value) -> Value {
match s.execute(id, &p) {
Ok(v) => v,
diff --git a/crates/engine/src/cmd/plastic.rs b/crates/engine/src/cmd/plastic.rs
index a875e6815d..7b0f47af2d 100644
--- a/crates/engine/src/cmd/plastic.rs
+++ b/crates/engine/src/cmd/plastic.rs
@@ -178,10 +178,20 @@ fn assign_rule(s: &mut Session, p: &Value) -> Result<Value> {
if bodies.is_empty() || bodies.len() > 10_000 {
return Err(bad(cmd, "`bodies` must list bodies"));
}
+ // A placed copy's name means the body it shows (#9740); the rule is kept by that body's own
+ // name. The placed bodies are named once for the whole list, not once per name.
let st = s.model.state();
- if let Some(b) = bodies.iter().find(|b| st.body(b).is_none()) {
- return Err(bad(cmd, format!("no body `{b}`")));
- }
+ let mut frames = None;
+ let bodies = bodies
+ .iter()
+ .map(|b| {
+ if st.body(b).is_some() {
+ return Ok(b.clone());
+ }
+ let frames = frames.get_or_insert_with(|| s.body_frames());
+ frames.iter().find(|(world, ..)| world == b).map(|(_, own, _)| own.clone()).ok_or_else(|| bad(cmd, format!("no body `{b}`")))
+ })
+ .collect::<Result<Vec<_>>>()?;
let rule = str_(p, "rule").map(str::trim).unwrap_or_default().to_string();
if !rule.is_empty() && s.doc.plastic_rule(&rule).is_none() {
return Err(bad(cmd, format!("no plastic rule `{rule}` (plastic.rules lists them)")));
@@ -350,6 +360,22 @@ mod tests {
(a - b).abs() / b.abs().max(1e-12)
}
+ /// A placed copy's body name (`KB (K:2)`, as inspect.measure lists it) means the body it
+ /// shows: the rule goes on that body, under its own name, for every placement.
+ #[test]
+ fn a_rule_assigned_by_a_placed_copys_name_goes_on_the_body_it_shows() {
+ let mut s = Session::default();
+ run(&mut s, "component.create", json!({"name": "K"}));
+ run(&mut s, "solid.box", json!({"body_name": "KB", "length": 10, "width": 10, "height": 10}));
+ run(&mut s, "occurrence.copy", json!({"component": "K", "translate": [30, 0, 0]}));
+ run(&mut s, "plastic.manage_rules", json!({"name": "Thin", "thickness": "1.2 mm"}));
+ let r = run(&mut s, "plastic.assign_rule", json!({"bodies": ["KB (K:2)"], "rule": "Thin"}));
+ assert_eq!(r["bodies"], json!(["KB"]), "{r}");
+ assert_eq!(s.doc.plastic.assigned.get("KB").map(String::as_str), Some("Thin"));
+ let err = s.execute("plastic.assign_rule", &json!({"bodies": ["KB (K:9)"], "rule": "Thin"})).unwrap_err().to_string();
+ assert!(err.contains("no body `KB (K:9)`"), "{err}");
+ }
+
#[test]
fn boss_with_hole_fillet_and_ribs() {
let mut s = Session::default();