SolidCraft: an open-source parametric 3D CAD application built in pure Rust. Desktop, web, and automation through MCP.
Avoid rebuilding bodies when rename updates feature references (#6287) (#10588)
Colin Faulkinghambc4d35b0977d
diff --git a/crates/doc/src/document.rs b/crates/doc/src/document.rsindex 696881ae3f..42667471fb 100644--- a/crates/doc/src/document.rs+++ b/crates/doc/src/document.rs@@ -3164,8 +3164,8 @@ impl Document {/// The single body rename (old name, new name) that turns `previous` into this design,/// when that is all that changed: the features' stored body names (`made` gives the names- /// each feature's bodies have now, by feature id) and what [`Document::rename_body_keys`]- /// renames. `None` for any other change.+ /// each feature's bodies have now, by feature id), typed body references, participants and+ /// what [`Document::rename_body_keys`] renames. `None` for any other change.pub fn body_rename_from(&self, previous: &Document, made: impl Fn(u64) -> Vec<String>) -> Option<(String, String)> {if self.features.len() != previous.features.len() {return None;@@ -3196,8 +3196,16 @@ impl Document {}let (old, new) = rename?;let mut expected = previous.clone();+ let bodies = std::collections::BTreeMap::from([(old.clone(), new.clone())]);for (f, g) in expected.features.iter_mut().zip(&self.features) {f.body_names.clone_from(&g.body_names);+ // Updating references to the renamed body changes no feature input other than its+ // name (#6287). Keep the full comparison below: geometry, expressions, sketch links+ // and face anchors must still agree before the model may reuse its geometry.+ f.kind.remap_bodies(&bodies);+ for body in f.participants.iter_mut().filter(|body| **body == old) {+ body.clone_from(&new);+ }}expected.rename_body_keys(&old, &new);(expected == *self).then_some((old, new))diff --git a/crates/engine/src/cmd/browser.rs b/crates/engine/src/cmd/browser.rsindex 685f961d5d..39e6d4b284 100644--- a/crates/engine/src/cmd/browser.rs+++ b/crates/engine/src/cmd/browser.rs@@ -1802,7 +1802,9 @@ mod tests {run(&mut s, "solid.box", json!({"length": 5, "width": 5, "height": 5, "corner": [20, 0, 0], "operation": "new", "body_name": "Tab"}));run(&mut s, "solid.combine", json!({"target": "A", "tools": ["Tab"], "operation": "join", "name": "Joined"}));let want = total(&mut s);+ s.evaluations = 0;run(&mut s, "body.rename", json!({"body": "A", "name": "B"}));+ assert_eq!(s.evaluations, 0, "renaming primitive participants changes no geometry");assert!(s.model.results.iter().all(|r| r.error.is_none()),"{:?}",@@ -1812,6 +1814,7 @@ mod tests {assert!((total(&mut s) - want).abs() < 1e-6);run(&mut s, "edit.undo", json!({}));assert_eq!(s.doc.find_feature("Hole").unwrap().participants, vec!["A".to_string()]);+ assert_eq!(s.evaluations, 0);}/// #1109: renaming counted bodies (`Body1`, `Body2` of one pattern) leaves the counted bodies@@ -1875,14 +1878,80 @@ mod tests {assert_eq!(names(&s), vec!["Plate".to_string(), "B".to_string()]);assert_eq!(s.evaluations, 0, "undo and redo of a rename evaluate nothing");assert_eq!(meshes(&s), shown);- // A body a later feature uses is renamed in that feature too: that is evaluated.+ // A later feature's body reference follows the rename without rebuilding geometry.run(&mut s, "solid.combine", json!({"target": "Plate", "tools": ["B"], "operation": "join", "keep_tools": true}));+ let shown = meshes(&s);s.evaluations = 0;run(&mut s, "body.rename", json!({"body": "B", "name": "Tool"}));- assert!(s.evaluations > 0);+ assert_eq!(s.evaluations, 0);+ assert_eq!(meshes(&s), shown);assert!(names(&s).contains(&"Tool".to_string()));}+ /// #6287: one rename in a timeline of named body references used to rebuild every feature+ /// and replace every mesh, including the other bodies' meshes.+ #[test]+ fn renaming_a_body_with_downstream_references_keeps_every_mesh() {+ let mut s = Session::default();+ for i in 0..18 {+ run(+ &mut s,+ "solid.box",+ json!({"length": 40, "width": 30, "height": 10, "center": [50 * i, 0, 0], "operation": "new", "body_name": format!("Block{i}")}),+ );+ }+ for _ in 0..2 {+ for i in 0..18 {+ run(&mut s, "solid.move", json!({"bodies": [format!("Block{i}")], "translate": [0, 5, 0]}));+ }+ }+ assert_eq!(s.doc.features.len(), 54);+ let meshes = |s: &Session| s.model.state().bodies.iter().map(|b| b.mesh()).collect::<Vec<_>>();+ let looks = |s: &Session| {+ s.model.state().bodies.iter().map(|b| (b.name.clone(), (*solidcraft_doc::naming::face_names(b)).clone())).collect::<Vec<_>>()+ };+ let shown = meshes(&s);+ let before = looks(&s);+ let measurements = run(&mut s, "inspect.measure", json!({}))["total"].clone();+ assert!((measurements["volume_mm3"].as_f64().unwrap() - 216_000.0).abs() < 1e-6);+ s.evaluations = 0;+ run(&mut s, "body.rename", json!({"body": "Block0", "name": "Bracket"}));+ assert_eq!(s.evaluations, 0, "renaming references must not evaluate the timeline");+ let after = looks(&s);+ assert_eq!(after[0].0, "Bracket");+ let mut fresh = Session::new((*s.doc).clone());+ fresh.refresh();+ assert_eq!(looks(&fresh), after, "the same bodies and face names as rebuilding");+ assert_eq!(run(&mut fresh, "inspect.measure", json!({}))["total"], measurements);+ run(&mut s, "edit.undo", json!({}));+ assert_eq!(looks(&s), before);+ run(&mut s, "edit.redo", json!({}));+ assert_eq!(looks(&s), after);+ assert_eq!(s.evaluations, 0, "undo and redo must also retain the evaluated model");+ assert!(shown.iter().zip(meshes(&s)).all(|(a, b)| std::sync::Arc::ptr_eq(a, &b)), "every body retains its display mesh");+ }++ #[test]+ fn a_body_rename_with_changed_dimensions_still_evaluates() {+ let mut s = Session::default();+ run(&mut s, "solid.box", json!({"length": 40, "width": 30, "height": 10, "body_name": "Block"}));+ run(&mut s, "solid.move", json!({"bodies": ["Block"], "translate": [5, 0, 0]}));+ let before = s.doc.clone();+ let model = s.model.clone();+ run(&mut s, "body.rename", json!({"body": "Block", "name": "Bracket"}));+ // An outside edit can include a rename and a geometry change together. It must not+ // take the rename shortcut merely because the later Move's body reference matches.+ let solidcraft_doc::FeatureKind::Box { height, .. } = &mut s.doc_mut().features[0].kind else { panic!("expected the box") };+ *height = "20".into();+ s.model = model;+ s.evaluations = 0;+ s.refresh_from(&before, false);+ assert_eq!(s.evaluations, 1);+ assert_eq!(names(&s), ["Bracket"]);+ let volume = run(&mut s, "inspect.measure", json!({}))["total"]["volume_mm3"].as_f64().unwrap();+ assert!((volume - 24_000.0).abs() < 1e-6, "the changed height must be evaluated: {volume}");+ }+#[test]fn renaming_a_body_leaves_other_text_with_its_name_alone() {let mut s = Session::default();diff --git a/crates/engine/src/tests.rs b/crates/engine/src/tests.rsindex 540f634df4..e689a8ff87 100644--- a/crates/engine/src/tests.rs+++ b/crates/engine/src/tests.rs@@ -4516,10 +4516,10 @@ fn renaming_a_body_later_features_change_evaluates_nothing() {assert_eq!(s.evaluations, 0, "nor do its undo and redo");}-/// A body whose name later bodies' names are worked out from (`X` patterned into `X (2)`) is still-/// renamed by evaluating the design, so the copies follow it as they would on opening it.+/// A body whose name later bodies' names are worked out from (`X` patterned into `X (2)`) keeps+/// the same copies and face names as opening the renamed design.#[test]-fn renaming_a_body_whose_copies_take_its_name_evaluates_the_design() {+fn renaming_a_body_keeps_its_copies_consistent_with_rebuilding() {let mut s = Session::default();run(&mut s, "solid.box", json!({"length": 10, "width": 10, "height": 10, "body_name": "X"}));run(&mut s, "solid.pattern.rectangular", json!({"bodies": ["X"], "dir1": [1, 0, 0], "count1": 3, "spacing1": 20}));@@ -4529,6 +4529,9 @@ fn renaming_a_body_whose_copies_take_its_name_evaluates_the_design() {let mut fresh = Session::new((*s.doc).clone());fresh.refresh();assert_eq!(names(&fresh), names(&s));+ for (a, b) in fresh.model.state().bodies.iter().zip(&s.model.state().bodies) {+ assert_eq!(solidcraft_doc::naming::face_names(a), solidcraft_doc::naming::face_names(b));+ }}/// #3044: on a design with thousands of bodies the cheap commands stay cheap: showing, hiding,