Skip to content

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

Rename: an imported body with numbered siblings is renamed without evaluating (#7274) (#7277)

JK3D388266ca371b
diff --git a/crates/doc/src/eval.rs b/crates/doc/src/eval.rs
index 5b410c68fe..38e7c50efb 100644
--- a/crates/doc/src/eval.rs
+++ b/crates/doc/src/eval.rs
@@ -2356,35 +2356,44 @@ impl Model {
/// design after the rename, which changed nothing but names, so every result keeps its
/// geometry and meshes and only the body's name (and the names worked out from it) changes.
/// Later features that change the body (a pattern, a chamfer, a join) keep their results
- /// too (#2467): a feature's geometry doesn't depend on its bodies' names.
+ /// too (#2467): a feature's geometry doesn't depend on its bodies' names. A body no later
+ /// feature changes is followed even when other bodies are numbered after it (`X (2)`, the
+ /// solids of a STEP file that share a label): `body.rename` keeps their names.
/// False, leaving the model as it was, when the rename can't be followed that way and the
/// design must be evaluated: the model isn't the design's, a body of that name comes and goes
- /// or isn't the same body throughout, a later body's name is worked out from it (`X (2)`), a
- /// message quotes it, or it is sheet metal, threaded, or named in a later feature's face
- /// references.
+ /// or isn't the same body throughout, a later body's name is worked out from a body later
+ /// features change, a message quotes it, or it is sheet metal, threaded, or named in a later
+ /// feature's face references.
pub fn rename_body(&mut self, doc: &Document, old: &str, new: &str) -> bool {
if self.results.len() != doc.features.len() || self.results.iter().zip(&doc.features).any(|(r, f)| r.id != f.id || r.stale) {
return false;
}
let Some(at) = self.results.iter().position(|r| r.state.body(old).is_some()) else { return false };
- let Some(feature) = self.results.get(at).and_then(|r| r.state.body(old)).map(|b| b.feature) else { return false };
- // One body from the feature that made it until it is gone (joined into another), and no
- // other body's name comes from its name.
+ let Some((feature, made)) = self.results.get(at).and_then(|r| r.state.body(old)).map(|b| (b.feature, Arc::as_ptr(&b.mesh))) else {
+ return false;
+ };
+ // Untouched from the feature that made it to the end: `body.rename` keeps the names of
+ // its siblings and of later bodies named after it (`X (2)`, the solids of a STEP file
+ // with one label), so evaluating the renamed design gives the same names.
+ let untouched = self.results.iter().skip(at).all(|r| r.state.body(old).is_some_and(|b| Arc::as_ptr(&b.mesh) == made));
+ // Or one body from the feature that made it until it is gone (joined into another), and
+ // no other body's name comes from its name.
let held: Vec<bool> = self.results.iter().skip(at).map(|r| r.state.body(old).is_some()).collect();
let one_run = held.iter().skip_while(|h| **h).all(|h| !*h);
let derived = format!("{old} (");
- let same_body = self
- .results
- .iter()
- .skip(at)
- .all(|r| r.state.bodies.iter().all(|b| if b.name == old { b.feature == feature } else { !b.name.starts_with(&derived) }));
+ let same_body = || {
+ self.results
+ .iter()
+ .skip(at)
+ .all(|r| r.state.bodies.iter().all(|b| if b.name == old { b.feature == feature } else { !b.name.starts_with(&derived) }))
+ };
let quoted = self.results.iter().skip(at).any(|r| [&r.error, &r.warning].into_iter().flatten().any(|m| m.contains(old)));
let named_elsewhere = self
.results
.iter()
.any(|r| r.state.body(new).is_some() || r.state.sheets.iter().any(|x| x.body == old) || r.state.threads.iter().any(|t| t.body == old));
let faces_named = doc.features.iter().skip(at + 1).any(|f| f.face_names.iter().any(|n| n.contains(old)));
- if !one_run || !same_body || quoted || named_elsewhere || faces_named {
+ if !(untouched || (one_run && same_body())) || quoted || named_elsewhere || faces_named {
return false;
}
// States shared between results stay shared.
diff --git a/crates/engine/src/tests.rs b/crates/engine/src/tests.rs
index dea0aaeba8..2d1b892dee 100644
--- a/crates/engine/src/tests.rs
+++ b/crates/engine/src/tests.rs
@@ -4293,6 +4293,40 @@ fn renaming_an_imported_assembly_body_reads_nothing_again() {
let _ = std::fs::remove_dir_all(&dir);
}
+/// Renaming one of an imported file's solids whose siblings are numbered after it (`Bolt` beside
+/// `Bolt (2)`, as the solids of a STEP file with one label are built) evaluates nothing either: the
+/// rename keeps the siblings' names, so the model is the one evaluating the renamed design gives.
+#[test]
+fn renaming_an_imported_body_with_numbered_siblings_evaluates_nothing() {
+ let dir = std::env::temp_dir().join(format!("solidcraft-step-rename-siblings-{}", std::process::id()));
+ std::fs::create_dir_all(&dir).unwrap();
+ let mut a = Session::default();
+ run(&mut a, "solid.box", json!({"length": 10, "width": 10, "height": 10, "body_name": "Bolt"}));
+ run(&mut a, "solid.box", json!({"length": 4, "width": 4, "height": 4, "center": [30, 0, 2], "operation": "new", "body_name": "Bolt (2)"}));
+ let p = dir.join("Bolts.step");
+ run(&mut a, "file.export", json!({"path": p.to_string_lossy()}));
+ let mut s = Session::default();
+ run(&mut s, "doc.open", json!({"path": p.to_string_lossy()}));
+ let names = |s: &Session| s.model.state().bodies.iter().map(|b| b.name.clone()).collect::<Vec<_>>();
+ let before = names(&s);
+ let first = before.iter().find(|n| !n.ends_with(" (2)")).cloned().unwrap();
+ assert!(before.contains(&format!("{first} (2)")), "a sibling numbered after it: {before:?}");
+ s.evaluations = 0;
+ run(&mut s, "body.rename", json!({"body": first, "name": "Renamed"}));
+ assert_eq!(s.evaluations, 0, "a rename evaluates nothing");
+ let after = names(&s);
+ assert!(after.contains(&"Renamed".to_string()) && after.contains(&format!("{first} (2)")), "{after:?}");
+ let mut fresh = Session::new((*s.doc).clone());
+ fresh.refresh();
+ assert_eq!(names(&fresh), after, "the same names as evaluating the renamed design");
+ run(&mut s, "edit.undo", json!({}));
+ assert_eq!(names(&s), before);
+ run(&mut s, "edit.redo", json!({}));
+ assert_eq!(names(&s), after);
+ assert_eq!(s.evaluations, 0, "nor do its undo and redo");
+ let _ = std::fs::remove_dir_all(&dir);
+}
+
/// #2467: renaming a body that later features change (a hole patterned over it, then a chamfer)
/// evaluates nothing, and leaves the bodies, volumes and face names evaluating the renamed
/// design gives; undo and redo do the same.