SolidCraft: an open-source parametric 3D CAD application built in pure Rust. Desktop, web, and automation through MCP.
Selection follows body rename so Delete targets the right body (#9737) (#9789)
Dahak Builds4973638b5600
diff --git a/crates/engine/src/cmd/browser.rs b/crates/engine/src/cmd/browser.rsindex d588fd1471..0bb872a9ed 100644--- a/crates/engine/src/cmd/browser.rs+++ b/crates/engine/src/cmd/browser.rs@@ -1134,9 +1134,6 @@ fn rename_body(s: &mut Session, p: &Value) -> Result<Value> {return Err(bad(cmd, e));}d.rename_body_keys(&old, name);- for b in s.visibility.locked_bodies.iter_mut().filter(|b| **b == old) {- *b = name.to_string();- }s.remember_body_rename(&previous, &old, name);Ok(json!({"body": old, "name": name}))}@@ -1318,6 +1315,82 @@ mod tests {assert!(!s.body_visible("Stud"));}+ /// Two 5 mm boxes, `A` and `B`, as the #9737 repro script makes them.+ fn two_boxes(s: &mut Session) {+ run(s, "solid.box", json!({"length": 5, "width": 5, "height": 5, "body_name": "A"}));+ run(s, "solid.box", json!({"length": 5, "width": 5, "height": 5, "corner": [10, 0, 0], "body_name": "B"}));+ }++ fn delete_selection(s: &mut Session) -> Value {+ let items: Vec<Value> = s.selection.iter().map(|x| serde_json::to_value(x).unwrap()).collect();+ match s.execute("selection.delete", &json!({"items": items})) {+ Ok(v) => v,+ Err(e) => panic!("selection.delete: {e}"),+ }+ }++ #[test]+ fn selection_follows_body_rename_and_delete_hits_the_renamed_body() {+ // #9737: after renaming a selected body the selection is the new name, and Delete+ // deletes it — the "nothing to delete" and the wrong-body delete are both gone.+ let mut s = Session::default();+ two_boxes(&mut s);+ run(&mut s, "select.set", json!({"items": [{"type": "body", "name": "A"}]}));+ run(&mut s, "body.rename", json!({"body": "A", "name": "Renamed"}));+ assert_eq!(s.selection, vec![crate::Sel::Body { name: "Renamed".into() }]);+ delete_selection(&mut s);+ assert_eq!(names(&s), ["B"]);+ // The old name is free; B takes it, and Delete finds nothing — B is never deleted.+ run(&mut s, "body.rename", json!({"body": "B", "name": "A"}));+ assert!(s.execute("selection.delete", &json!({"items": []})).is_err());+ assert_eq!(names(&s), ["A"]);+ }++ #[test]+ fn renaming_the_selection_leaves_the_old_name_for_another_body_to_take() {+ // The wrong-body delete, head on: B takes A's name before Delete is pressed. The+ // selection followed the rename, so Delete takes `Renamed` and B (now `A`) survives.+ let mut s = Session::default();+ two_boxes(&mut s);+ run(&mut s, "select.set", json!({"items": [{"type": "body", "name": "A"}]}));+ run(&mut s, "body.rename", json!({"body": "A", "name": "Renamed"}));+ run(&mut s, "body.rename", json!({"body": "B", "name": "A"}));+ assert_eq!(s.selection, vec![crate::Sel::Body { name: "Renamed".into() }]);+ delete_selection(&mut s);+ assert_eq!(names(&s), ["A"]);+ }++ #[test]+ fn face_selection_follows_the_body_rename() {+ let mut s = Session::default();+ two_boxes(&mut s);+ let face = json!({"type": "face", "body": "A", "index": 0, "point": [2.5, 2.5, 0]});+ run(&mut s, "select.set", json!({"items": [face]}));+ run(&mut s, "body.rename", json!({"body": "A", "name": "Renamed"}));+ assert_eq!(s.selection, vec![crate::Sel::Face { body: "Renamed".into(), index: 0, point: [2.5, 2.5, 0.0].into() }]);+ }++ #[test]+ fn a_selected_body_that_goes_away_leaves_the_selection() {+ // A stale selection is how a later body taking the name gets deleted: once the body+ // is gone the selection drops it, at the refresh that removed the body.+ let mut s = Session::default();+ two_boxes(&mut s);+ run(&mut s, "select.set", json!({"items": [{"type": "body", "name": "A"}]}));+ run(&mut s, "solid.remove", json!({"bodies": ["A"]}));+ assert!(s.selection.is_empty(), "the gone body must not stay selected: {:?}", s.selection);+ }++ #[test]+ fn select_set_refuses_a_body_name_that_is_not_there() {+ let mut s = Session::default();+ two_boxes(&mut s);+ assert!(s.execute("select.set", &json!({"items": [{"type": "body", "name": "NoSuchBody"}]})).is_err());+ assert!(s.selection.is_empty());+ run(&mut s, "select.set", json!({"items": [{"type": "body", "name": "A"}, {"type": "body", "name": "B"}]}));+ assert_eq!(s.selection.len(), 2);+ }+#[test]fn show_all_bodies_clears_stale_hidden_names() {for folder in ["bodies", "all"] {diff --git a/crates/engine/src/cmd/edit.rs b/crates/engine/src/cmd/edit.rsindex a52f3d82a9..5aa3bb6e7f 100644--- a/crates/engine/src/cmd/edit.rs+++ b/crates/engine/src/cmd/edit.rs@@ -1233,8 +1233,19 @@ fn select_set(s: &mut Session, p: &Value) -> Result<Value> {let mut sel: Vec<Sel> = Vec::new();for it in items {let one: Sel = serde_json::from_value(it).map_err(|e| bad("select.set", e.to_string()))?;- if let Sel::Occurrence { path } = &one {- s.occurrence_bodies(path, true).map_err(|e| bad("select.set", e.to_string()))?;+ match &one {+ Sel::Occurrence { path } => {+ s.occurrence_bodies(path, true).map_err(|e| bad("select.set", e.to_string()))?;+ }+ // A selection may not name a body that isn't there: left selected it would delete+ // nothing, or the next body to take that name (#9737).+ other => {+ if let Some(name) = other.body_name()+ && s.world_state().body(name).is_none()+ {+ return Err(bad("select.set", format!("no body `{name}`")));+ }+ }}sel.push(one);}diff --git a/crates/engine/src/lib.rs b/crates/engine/src/lib.rsindex 3b0380a78b..6989149acb 100644--- a/crates/engine/src/lib.rs+++ b/crates/engine/src/lib.rs@@ -175,6 +175,18 @@ pub enum Sel {},}+impl Sel {+ /// The body this item names, when it names one: the body itself, or the body a face, edge+ /// or vertex belongs to. Session state that names a body has to follow its renames (#9737).+ pub(crate) fn body_name(&self) -> Option<&str> {+ match self {+ Sel::Body { name } => Some(name),+ Sel::Face { body, .. } | Sel::Edge { body, .. } | Sel::Vertex { body, .. } => Some(body),+ _ => None,+ }+ }+}+/// A body rename between two designs (see `Session::body_renames`).#[derive(Clone)]struct BodyRename {@@ -1009,6 +1021,13 @@ impl Session {self.follow_axis_point_renames(previous);let rename = self.body_rename(previous);self.remap_hidden_bodies(previous, &self.model.state(), rename.as_ref().map(|(old, new, _)| (old.as_str(), new.as_str())));+ // Locked bodies follow the rename here rather than in `body.rename`, so a rename that+ // reaches the design any other way (undo, an outside edit) is covered too (#9737).+ if let Some((old, new, _)) = &rename {+ for b in self.visibility.locked_bodies.iter_mut().filter(|b| **b == *old) {+ *b = new.clone();+ }+ }if let Some((old, new, names_only)) = rename&& names_only&& self.model.rename_body(&self.doc, &old, &new)diff --git a/crates/engine/src/occurrences.rs b/crates/engine/src/occurrences.rsindex 1e00d50e30..4ec5faed2c 100644--- a/crates/engine/src/occurrences.rs+++ b/crates/engine/src/occurrences.rs@@ -132,8 +132,10 @@ pub(crate) struct RemovedLaterCache {}impl Session {- /// Hidden names follow the same body and exact occurrence path through a rename. Undo- /// history restores visibility separately. Never parse display labels:+ /// Hidden names and the bodies the selection names follow the same body and exact+ /// occurrence path through a rename (#9737: a selected body stays selected under its new+ /// name, so Delete hits the body the user actually selected, never one that later takes+ /// the old name). Undo history restores visibility separately. Never parse display labels:/// ambiguous labels and names containing slashes use the document's canonical naming.pub(crate) fn remap_hidden_bodies(&mut self,@@ -141,7 +143,8 @@ impl Session {state: &solidcraft_doc::ModelState,rename: Option<(&str, &str)>,) {- if self.visibility.hidden_bodies.is_empty() {+ let selected: std::collections::BTreeSet<&str> = self.selection.iter().filter_map(|s| s.body_name()).collect();+ if self.visibility.hidden_bodies.is_empty() && selected.is_empty() {return;}if rename.is_none() {@@ -165,7 +168,7 @@ impl Session {for (component, path, _) in previous.placements() {for body in state.bodies.iter().filter(|body| previous.model_body_component(body) == component) {let old = before.name(&body.name, component, &path);- if hidden.contains(old.as_str()) {+ if hidden.contains(old.as_str()) || selected.contains(old.as_str()) {let source = rename.filter(|(old, _)| *old == body.name).map_or(body.name.as_str(), |(_, new)| new);names.insert(old, after.name(source, component, &path));}@@ -176,6 +179,16 @@ impl Session {hidden.clone_from(new);}}+ for sel in &mut self.selection {+ let name = match sel {+ Sel::Body { name } => name,+ Sel::Face { body, .. } | Sel::Edge { body, .. } | Sel::Vertex { body, .. } => body,+ _ => continue,+ };+ if let Some(new) = names.get(name.as_str()) {+ name.clone_from(new);+ }+ }}/// Visibility keeps construction names, while undo history keeps feature ids. Follow the@@ -745,6 +758,15 @@ impl Session {}let selection = std::mem::take(&mut self.selection);self.selection = selection.into_iter().filter(|s| !matches!(s, Sel::Occurrence {path} if self.occurrence_placement(path).is_err())).collect();+ // A selected body that is gone cannot be deleted, measured or shown: drop it, so a+ // later body taking its name is never deleted in its place (#9737).+ if self.selection.iter().any(|s| s.body_name().is_some()) {+ let world = self.world_state();+ self.selection.retain(|s| match s.body_name() {+ Some(name) => world.body(name).is_some(),+ None => true,+ });+ }let hidden = std::mem::take(&mut self.visibility.hidden_occurrences);self.visibility.hidden_occurrences = hidden.into_iter().filter(|p| self.occurrence_placement(p).is_ok()).collect();}