Skip to content

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

Renaming a body keeps the evaluated timeline, off the window thread (#2467) (#2468)

Kai Evans3093ce1d8779
diff --git a/crates/doc/src/eval.rs b/crates/doc/src/eval.rs
index ab63bea778..4dea9fb839 100644
--- a/crates/doc/src/eval.rs
+++ b/crates/doc/src/eval.rs
@@ -2026,23 +2026,36 @@ impl Model {
/// Follow a rename of body `old` to `new` without evaluating anything (#3044): `doc` is the
/// 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.
/// 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, the body is changed by a later
- /// feature, or is sheet metal, threaded, or named in a later feature's face references.
+ /// 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.
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(made) = self.results.get(at).and_then(|r| r.state.body(old)).map(|b| Arc::as_ptr(&b.mesh)) else { return false };
- // Untouched from the feature that made it to the end, and nothing else holds its name.
- let untouched = self.results.iter().skip(at).all(|r| r.state.body(old).is_some_and(|b| Arc::as_ptr(&b.mesh) == made));
+ 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 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 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 !untouched || named_elsewhere || faces_named {
+ if !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 359aa08c27..c2c138dc88 100644
--- a/crates/engine/src/tests.rs
+++ b/crates/engine/src/tests.rs
@@ -4022,6 +4022,64 @@ fn renaming_an_imported_assembly_body_reads_nothing_again() {
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.
+#[test]
+fn renaming_a_body_later_features_change_evaluates_nothing() {
+ let mut s = Session::default();
+ run(&mut s, "sketch.create", json!({"plane": "XY"}));
+ run(&mut s, "sketch.rectangle.two_point", json!({"p0": [0, 0], "p1": [80, 50]}));
+ run(&mut s, "sketch.finish", json!({}));
+ run(&mut s, "solid.extrude", json!({"distance": 10}));
+ run(
+ &mut s,
+ "solid.hole",
+ json!({"position": [15, 12, 10], "diameter": 5, "type": "counterbore", "cb_diameter": 9, "cb_depth": 3, "name": "Hole1"}),
+ );
+ run(
+ &mut s,
+ "solid.pattern.rectangular",
+ json!({"features": ["Hole1"], "dir1": [1, 0, 0], "count1": 3, "spacing1": 25, "dir2": [0, 1, 0], "count2": 2, "spacing2": 25}),
+ );
+ run(&mut s, "solid.chamfer", json!({"edges": [[40, 0, 10], [80, 25, 10]], "distance": 1}));
+ let looks = |st: &solidcraft_doc::ModelState| {
+ st.bodies.iter().map(|b| (b.name.clone(), (*solidcraft_doc::naming::face_names(b)).clone())).collect::<Vec<_>>()
+ };
+ let before = looks(&s.model.state());
+ let v = body_volume(&s.model.state());
+ let old = before.first().map(|b| b.0.clone()).unwrap();
+ s.evaluations = 0;
+ run(&mut s, "body.rename", json!({"body": old, "name": "Plate"}));
+ assert_eq!(s.evaluations, 0, "a rename evaluates nothing");
+ let after = looks(&s.model.state());
+ assert_eq!(after.first().map(|b| b.0.as_str()), Some("Plate"));
+ let mut fresh = Session::new((*s.doc).clone());
+ fresh.refresh();
+ assert_eq!(looks(&fresh.model.state()), after, "the bodies and face names evaluating the renamed design gives");
+ assert!(rel(body_volume(&fresh.model.state()), v) < 1e-9);
+ run(&mut s, "edit.undo", json!({}));
+ assert_eq!(looks(&s.model.state()), before);
+ run(&mut s, "edit.redo", json!({}));
+ assert_eq!(looks(&s.model.state()), after);
+ 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.
+#[test]
+fn renaming_a_body_whose_copies_take_its_name_evaluates_the_design() {
+ 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}));
+ let names = |s: &Session| s.model.state().bodies.iter().map(|b| b.name.clone()).collect::<Vec<_>>();
+ assert!(names(&s).iter().any(|n| n.starts_with("X (")), "{:?}", names(&s));
+ run(&mut s, "body.rename", json!({"body": "X", "name": "First"}));
+ let mut fresh = Session::new((*s.doc).clone());
+ fresh.refresh();
+ assert_eq!(names(&fresh), names(&s));
+}
+
/// #3044: on a design with thousands of bodies the cheap commands stay cheap: showing, hiding,
/// selecting, colouring and renaming a body or a feature, and undoing them, recompute no feature
/// (renames don't even look over the timeline) and leave every body's display mesh as it was, so
diff --git a/crates/ui-egui/src/context_menu.rs b/crates/ui-egui/src/context_menu.rs
index a2f28542eb..7ba10f0b8e 100644
--- a/crates/ui-egui/src/context_menu.rs
+++ b/crates/ui-egui/src/context_menu.rs
@@ -1477,15 +1477,7 @@ pub(crate) fn commit_rename(app: &mut SolveApp, r: &Rename) {
return;
}
match &r.what {
- RenameWhat::Body(old) => {
- if app.run("body.rename", json!({ "body": old, "name": name })).is_ok() {
- for list in [&mut app.session.visibility.hidden_bodies, &mut app.ui.locked_bodies] {
- for b in list.iter_mut().filter(|b| *b == old) {
- *b = name.clone();
- }
- }
- }
- }
+ RenameWhat::Body(old) => drop(app.run_heavy("body.rename", json!({ "body": old, "name": name }))),
RenameWhat::Feature(f) => drop(app.run("timeline.rename", json!({ "feature": f, "name": name }))),
RenameWhat::Component(c) => drop(app.run("component.rename", json!({ "component": c, "name": name }))),
RenameWhat::Group(g) => crate::browser::group_action(app, "ui.renameGroup", &json!({ "group": g, "name": name })),
diff --git a/crates/ui-egui/src/history_off_thread_tests.rs b/crates/ui-egui/src/history_off_thread_tests.rs
index 80356746be..8b6b710726 100644
--- a/crates/ui-egui/src/history_off_thread_tests.rs
+++ b/crates/ui-egui/src/history_off_thread_tests.rs
@@ -504,3 +504,44 @@ fn without_a_wait_the_delete_checks_in_place() {
assert!(h.app.delete_check.is_none());
assert!(h.app.session.doc.feature(boss).is_none());
}
+
+/// A body renamed in its rename box (#2467): off the window thread, the features after the one
+/// that made it (a pattern, a chamfer) kept as they are, and the body's lock following the new
+/// name.
+#[test]
+fn a_body_renamed_in_the_browser_keeps_the_pattern_after_it() {
+ let mut h = Harness::new();
+ h.app.home.open = false;
+ for (id, p) in [
+ ("sketch.create", json!({"plane": "XY"})),
+ ("sketch.rectangle.two_point", json!({"p0": [0, 0], "p1": [80, 50]})),
+ ("sketch.finish", json!({})),
+ ("solid.extrude", json!({"distance": 10})),
+ ("solid.hole", json!({"position": [15, 12, 10], "diameter": 5, "name": "Hole1"})),
+ (
+ "solid.pattern.rectangular",
+ json!({"features": ["Hole1"], "dir1": [1, 0, 0], "count1": 3, "spacing1": 25, "dir2": [0, 1, 0], "count2": 2, "spacing2": 25}),
+ ),
+ ("solid.chamfer", json!({"edges": [[40, 0, 10]], "distance": 1})),
+ ] {
+ h.app.run(id, p).unwrap();
+ }
+ h.app.ui.locked_bodies.push("Body1".into());
+ h.app.apply_off_thread = Some(Duration::ZERO);
+ crate::context_menu::start_rename(&mut h.app, crate::context_menu::RenameWhat::Body("Body1".into()), egui::pos2(0.0, 0.0));
+ h.frames(2);
+ h.step(&json!({"text": "Plate"})).unwrap();
+ let names = |h: &Harness| h.app.session.model.state().bodies.iter().map(|b| b.name.clone()).collect::<Vec<_>>();
+ let mesh = |h: &Harness| h.app.session.model.state().bodies.first().map(|b| b.mesh());
+ let before = mesh(&h);
+ // What Enter does, without the frames after it that would see the rename in.
+ let rename = h.app.menu.rename.take().unwrap();
+ crate::context_menu::commit_rename(&mut h.app, &rename);
+ assert!(h.app.computing_since().is_some(), "computing");
+ assert_eq!(names(&h), ["Body1"], "the design is as it was while it computes");
+ until_settled(&mut h);
+ assert_eq!(names(&h), ["Plate"]);
+ let kept = before.zip(mesh(&h)).is_some_and(|(a, b)| std::sync::Arc::ptr_eq(&a, &b));
+ assert!(kept, "no feature computed again");
+ assert_eq!(h.app.ui.locked_bodies, ["Plate"]);
+}
diff --git a/crates/ui-egui/src/lib.rs b/crates/ui-egui/src/lib.rs
index 2131cd6389..7e61ba57db 100644
--- a/crates/ui-egui/src/lib.rs
+++ b/crates/ui-egui/src/lib.rs
@@ -831,6 +831,14 @@ impl SolveApp {
}
// Curves drawn in construction (or centerline) mode take that line type.
sketch_palette::apply_mode(self, id, v);
+ // Locks follow a body's rename, from the rename box or anywhere else.
+ if id == "body.rename"
+ && let (Some(old), Some(new)) = (v.get("body").and_then(Value::as_str), v.get("name").and_then(Value::as_str))
+ {
+ for b in self.ui.locked_bodies.iter_mut().filter(|b| *b == old) {
+ *b = new.to_string();
+ }
+ }
}
self.finish_command(id, &r);
r