SolidCraft: an open-source parametric 3D CAD application built in pure Rust. Desktop, web, and automation through MCP.
Timeline: suppressing a feature keeps later features' body names (#4302) (#4311)
Frankd2eb8cf639f7
diff --git a/crates/doc/src/eval.rs b/crates/doc/src/eval.rsindex 4dea9fb839..6f5c84e947 100644--- a/crates/doc/src/eval.rs+++ b/crates/doc/src/eval.rs@@ -716,9 +716,11 @@ pub struct ModelState {/// Shared between states: a feature that does not change a sketch keeps it as it was.pub sketches: Vec<Arc<SolvedSketch>>,pub body_counter: usize,- /// Body names the design used and must not use again (a name a `body.rename` freed, #4957).- /// The counter starts over at every evaluation, so without these a design that is evaluated- /// again after a rename hands the freed name to the next body.+ /// Body names the design used and must not use again (a name a `body.rename` freed, #4957),+ /// and the names stored on the suppressed features so far, so a body made while a feature is+ /// suppressed keeps its name once it is back (#4302). The counter starts over at every+ /// evaluation, so without these a design that is evaluated again hands such a name to the next+ /// body.pub reserved_body_names: std::collections::BTreeSet<String>,/// Sheet metal bodies (by body name) and their flat patterns.pub sheets: Vec<crate::sheet::SheetBody>,@@ -1778,6 +1780,8 @@ impl Model {let mut prev_fp = 0u64;// The names the design must not use again travel with the state (#4957).let mut state = std::sync::Arc::new(ModelState { reserved_body_names: doc.reserved_body_names.clone(), ..(*self.empty).clone() });+ // Those and the names stored on the suppressed features so far (#4302).+ let mut reserved = doc.reserved_body_names.clone();let mut out: Vec<FeatureResult> = Vec::with_capacity(doc.features.len());let mut reuse = !self.no_cache;let mut recomputed = 0;@@ -1804,6 +1808,9 @@ impl Model {// A state the saved model did not keep is evaluated again from the one before it; the// results after it that the model did keep still stand (#3220).let unkept = reuse && self.results.get(i).filter(current).is_some_and(|old| old.unkept);+ if f.suppressed && !rolled_back {+ reserved.extend(f.body_names.iter().map(|n| n.trim()).filter(|n| !n.is_empty()).map(str::to_string));+ }if reuse&& !unkept&& let Some(old) = self.results.get(i).filter(current)@@ -1831,6 +1838,11 @@ impl Model {});continue;}+ // Taken from the design each time: a state taken from an earlier evaluation may have+ // been made before a rename freed a name or a feature was suppressed.+ if state.reserved_body_names != reserved {+ Arc::make_mut(&mut state).reserved_body_names.clone_from(&reserved);+ }// The same feature on the same inputs: its earlier result.let key = cache_key(f, fp, fingerprint(0, f, &vals, false, &self.step_digests) ^ placement_digest(doc, f, Some(&state)), ctx, &state);let kept = self.kept.0.as_ref().filter(|_| !self.no_cache);@@ -1874,9 +1886,8 @@ impl Model {recomputed += 1;let t0 = now();let mut next = (*state).clone();- // The names the design must not use again travel with the state (#4957): a state- // taken from an earlier evaluation may have been made before a rename freed one.- next.reserved_body_names = doc.reserved_body_names.clone();+ // The names the design must not use again travel with the state (#4957, #4302).+ next.reserved_body_names.clone_from(&reserved);for b in &mut next.bodies {b.made = crate::lineage::Made::Before;}diff --git a/crates/engine/src/cmd/browser.rs b/crates/engine/src/cmd/browser.rsindex 425d594b73..1e2e1abaef 100644--- a/crates/engine/src/cmd/browser.rs+++ b/crates/engine/src/cmd/browser.rs@@ -551,6 +551,13 @@ fn member_pins(s: &Session, fid: u64) -> Option<BTreeMap<String, String>> {(lines.len() == made.len()).then(|| lines.into_iter().zip(made).collect())}+/// Whether the `i`-th new body of `f` is numbered by the body counter (`BodyN`): it has no stored+/// name of its own and is not numbered after the feature's first name (`Ribs (4)`).+pub(crate) fn counted(f: &solidcraft_doc::Feature, i: usize) -> bool {+ let first = f.body_names.first().is_some_and(|n| !n.trim().is_empty());+ f.body_names.get(i).is_none_or(|n| n.trim().is_empty()) && (f.shell_piece_order || i < f.body_names.len() || !first)+}+fn rename_body(s: &mut Session, p: &Value) -> Result<Value> {let cmd = "body.rename";let old = str_(p, "body").ok_or_else(|| bad(cmd, "`body` must be a body name"))?.to_string();@@ -578,10 +585,6 @@ fn rename_body(s: &mut Session, p: &Value) -> Result<Value> {// own, and every counted body after it would move down one (`Body3` becoming `Body1`) and// break what names it. Those later features keep the names they made too (#1109): each one's// own result holds them, also for bodies a later feature joins away.- let counted = |f: &solidcraft_doc::Feature, i: usize| {- let first = f.body_names.first().is_some_and(|n| !n.trim().is_empty());- f.body_names.get(i).is_none_or(|n| n.trim().is_empty()) && (f.shell_piece_order || i < f.body_names.len() || !first)- };// Bodies later features name after this one (`Top (1)`, a split's other piece) keep their// names too: they would otherwise follow it, and break what names them (#3510).let derived = format!("{old} (");diff --git a/crates/engine/src/cmd/edit.rs b/crates/engine/src/cmd/edit.rsindex ee69fbd160..ee475be191 100644--- a/crates/engine/src/cmd/edit.rs+++ b/crates/engine/src/cmd/edit.rs@@ -582,6 +582,24 @@ fn feature_ids(s: &Session, p: &Value, cmd: &str) -> Result<Vec<u64>> {Ok(ids)}+/// The names the features from the first of `ids` to be suppressed on made, for those that made a+/// body numbered by the counter (`BodyN`). Counted bodies take the next free number each time the+/// design is computed, so once a feature that made one is suppressed, every counted body after it+/// would move down one (`Body2` becoming `Body1`) and break what names it, such as a Move of+/// `Body2` (#4302). Stored as their names, they stay as they were, as when a counted body is+/// renamed (#1109). The suppressed feature keeps its own too: the counter passes the names of+/// suppressed features by, so a body made meanwhile does not take `Body1` and lose it again when+/// the feature is back.+fn counted_names_from(s: &Session, ids: &[u64]) -> Vec<(u64, Vec<String>)> {+ let made = |f: &Feature| -> Vec<String> {+ s.model.result(f.id).map(|r| r.state.bodies.iter().filter(|b| b.feature == f.id).map(|b| b.name.clone()).collect()).unwrap_or_default()+ };+ let makes_counted = |f: &Feature| (0..made(f).len()).any(|i| super::browser::counted(f, i));+ let first = s.doc.features.iter().position(|f| ids.contains(&f.id) && !f.suppressed && makes_counted(f));+ let Some(at) = first else { return Vec::new() };+ s.doc.features.iter().skip(at).filter(|f| makes_counted(f)).map(|f| (f.id, made(f))).collect()+}+fn suppress(s: &mut Session, p: &Value) -> Result<Value> {let cmd = "timeline.suppress";let ids = feature_ids(s, p, cmd)?;@@ -591,8 +609,14 @@ fn suppress(s: &mut Session, p: &Value) -> Result<Value> {// Toggle: one feature flips; several are all suppressed unless all already are.None => !ids.iter().all(|id| s.doc.feature(*id).is_some_and(|f| f.suppressed)),};+ let keep = if want { counted_names_from(s, &ids) } else { Vec::new() };// One command, so one undo step however many features it changes.let doc = s.doc_mut();+ for (id, made) in &keep {+ if let Some(f) = doc.feature_mut(*id) {+ f.body_names.clone_from(made);+ }+ }for id in &ids {let f = doc.feature_mut(*id).ok_or_else(|| bad(cmd, format!("no feature {id}")))?;f.suppressed = want;diff --git a/crates/engine/src/cmd/edit_tests.rs b/crates/engine/src/cmd/edit_tests.rsindex 24fdb4a99f..0e99580f3a 100644--- a/crates/engine/src/cmd/edit_tests.rs+++ b/crates/engine/src/cmd/edit_tests.rs@@ -733,6 +733,58 @@ fn suppress_takes_a_list_of_features_as_one_undo_step() {assert!(s.doc.find_feature("B").is_none() && s.doc.find_feature("C").is_none());}+/// #4302: suppressing a feature that made a counted body (`Body1`) leaves the counted bodies of+/// later features as they were: `Body2` used to become `Body1`, and a Move of `Body2` failed.+#[test]+fn suppressing_a_feature_keeps_later_counted_body_names() {+ let mut s = Session::default();+ run(&mut s, "solid.box", json!({"length": 10, "width": 10, "height": 10, "name": "A"}));+ run(&mut s, "solid.box", json!({"length": 10, "width": 10, "height": 10, "corner": [20, 0, 0], "name": "B"}));+ run(&mut s, "solid.box", json!({"length": 10, "width": 10, "height": 10, "corner": [40, 0, 0], "name": "C"}));+ run(&mut s, "solid.move", json!({"bodies": ["Body2"], "translate": [0, 30, 0], "name": "Move"}));+ let bodies = |s: &Session| -> Vec<(String, String)> {+ let st = s.model.state();+ st.bodies.iter().map(|b| (b.name.clone(), s.doc.feature(b.feature).map(|f| f.name.clone()).unwrap_or_default())).collect()+ };+ let named = |pairs: &[(&str, &str)]| pairs.iter().map(|(b, f)| (b.to_string(), f.to_string())).collect::<Vec<_>>();+ let works = |s: &Session| s.doc.features.iter().all(|f| s.model.result(f.id).is_some_and(|r| r.error.is_none()));+ let moved = |s: &Session| crate::cmd::inspect::measure_json(s.model.state().body("Body2").unwrap())["bbox"]["min"].clone();+ assert_eq!(bodies(&s), named(&[("Body1", "A"), ("Body2", "B"), ("Body3", "C")]));+ let at = moved(&s);++ run(&mut s, "timeline.suppress", json!({"feature": "A", "suppressed": true}));+ assert_eq!(bodies(&s), named(&[("Body2", "B"), ("Body3", "C")]));+ assert!(works(&s), "the Move still finds Body2");+ assert_eq!(moved(&s), at, "and still moves B's body");++ // Back again: A takes its number back, the others keep theirs.+ run(&mut s, "timeline.suppress", json!({"feature": "A", "suppressed": false}));+ assert_eq!(bodies(&s), named(&[("Body1", "A"), ("Body2", "B"), ("Body3", "C")]));+ assert!(works(&s));++ // Several at once: the first one keeps the names of everything after it; undo restores all.+ run(&mut s, "timeline.suppress", json!({"features": ["B", "A"], "suppressed": true}));+ assert_eq!(bodies(&s), named(&[("Body3", "C")]));+ run(&mut s, "edit.undo", json!({}));+ assert_eq!(bodies(&s), named(&[("Body1", "A"), ("Body2", "B"), ("Body3", "C")]));+ assert!(works(&s));++ // A body made while A is suppressed does not take A's `Body1`, so it keeps its name once A is+ // back (it used to be `Body1`, then `Body4`).+ run(&mut s, "timeline.suppress", json!({"feature": "A", "suppressed": true}));+ run(&mut s, "solid.box", json!({"length": 10, "width": 10, "height": 10, "corner": [60, 0, 0], "name": "D"}));+ assert_eq!(bodies(&s), named(&[("Body2", "B"), ("Body3", "C"), ("Body4", "D")]));+ // Also once the design is opened again with the model its file keeps, which does not keep+ // the names passed by: D computed again from a kept state is still `Body4`.+ let saved = s.model.saved(&s.doc).unwrap();+ let mut reopened = Session::opened(solidcraft_doc::Document::from_json(&s.doc.to_json()).unwrap(), Some(&saved));+ run(&mut reopened, "timeline.edit", json!({"feature": "D", "set": {"length": "12"}}));+ assert_eq!(bodies(&reopened), named(&[("Body2", "B"), ("Body3", "C"), ("Body4", "D")]));+ run(&mut s, "timeline.suppress", json!({"feature": "A", "suppressed": false}));+ assert_eq!(bodies(&s), named(&[("Body1", "A"), ("Body2", "B"), ("Body3", "C"), ("Body4", "D")]));+ assert!(works(&s));+}+/// The value at a dotted path of a definition.fn dotted<'a>(def: &'a Value, path: &str) -> &'a Value {path.split('.').fold(def, |v, k| &v[k])