Skip to content

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

Patterns: one before the feature it copies follows that feature's parameters (#6726) (#6727)

semmelrinde4d06daf87b1f
diff --git a/crates/doc/src/eval.rs b/crates/doc/src/eval.rs
index 52591157f4..174291b8d3 100644
--- a/crates/doc/src/eval.rs
+++ b/crates/doc/src/eval.rs
@@ -1465,9 +1465,45 @@ fn chain_fingerprint(
}
fp = h.finish();
}
+ // A pattern or mirror of features replays its sources' definitions wherever they are in the
+ // timeline. One made with the timeline rolled back comes before them, where the chain doesn't
+ // cover them: its copies kept the size the sources had when they were made.
+ if let FeatureKind::Pattern { features, bodies, .. } | FeatureKind::Mirror { features, bodies, .. } = &f.kind
+ && bodies.is_empty()
+ && !features.is_empty()
+ {
+ let mut h = std::collections::hash_map::DefaultHasher::new();
+ fp.hash(&mut h);
+ for g in replayed_sources(doc, features) {
+ fingerprint(doc, 0, g, vals, false, steps).hash(&mut h);
+ }
+ fp = h.finish();
+ }
fp
}
+/// The features a pattern or mirror of `features` replays: each source, and the sources of one
+/// that is itself a pattern or mirror of features (see `copy_plan`), each once.
+fn replayed_sources<'a>(doc: &'a Document, features: &[crate::FeatureName]) -> Vec<&'a Feature> {
+ let mut out: Vec<&Feature> = Vec::new();
+ let mut todo: Vec<String> = crate::plain(features);
+ // A bound on the walk, as on the copy plan's nesting: a design can't make it run on.
+ for _ in 0..1024 {
+ let Some(name) = todo.pop() else { break };
+ let Ok(g) = source_feature(doc, &name) else { continue };
+ if out.iter().any(|x| x.id == g.id) {
+ continue;
+ }
+ if let FeatureKind::Pattern { features, bodies, .. } | FeatureKind::Mirror { features, bodies, .. } = &g.kind
+ && bodies.is_empty()
+ {
+ todo.extend(crate::plain(features));
+ }
+ out.push(g);
+ }
+ out
+}
+
/// Document-level inputs any feature may read: construction planes (resolved by name), sheet
/// metal and plastic rules (and the parameters they use), components and body placement.
fn context_digest(doc: &Document, vals: &BTreeMap<String, Value>) -> u64 {
diff --git a/crates/engine/src/cmd/patterns_tests.rs b/crates/engine/src/cmd/patterns_tests.rs
index f551643684..5b5bc88fc3 100644
--- a/crates/engine/src/cmd/patterns_tests.rs
+++ b/crates/engine/src/cmd/patterns_tests.rs
@@ -170,6 +170,55 @@ fn feature_circular_center_flip_uses_partial_arc_endpoints_and_full_circle_spaci
}
}
+/// A pattern made with the timeline rolled back to the start comes before the box it copies and
+/// replays the box from its definition. A change of the box's parameter reaches the copies as it
+/// reaches the box: the pattern's result was kept (its copies the old length) because nothing
+/// before it in the timeline had changed.
+#[test]
+fn a_pattern_before_the_feature_it_copies_follows_that_features_parameter() {
+ let mut s = Session::default();
+ run(&mut s, "parameters.add", json!({"name": "w", "expression": "18 mm"}));
+ run(&mut s, "solid.box", json!({"length": "w", "width": 10, "height": 10}));
+ run(&mut s, "timeline.rollback", json!({"position": 0}));
+ run(&mut s, "solid.pattern.rectangular", json!({"features": ["Box1"], "dir1": [1, 0, 0], "count1": 2, "spacing1": 30}));
+ let lengths = |st: &solidcraft_doc::ModelState| {
+ st.bodies.iter().map(|b| (b.name.clone(), b.body.outline_bounds())).map(|(n, bb)| (n, bb.max.x - bb.min.x)).collect::<Vec<_>>()
+ };
+ let fresh = |s: &Session| {
+ let mut m = solidcraft_doc::Model::new();
+ m.no_cache = true;
+ m.evaluate(&s.doc);
+ lengths(&m.state())
+ };
+ for step in [json!({"name": "w", "expression": "21 mm"}), json!({"name": "w", "expression": "25 mm"})] {
+ run(&mut s, "parameters.change", step);
+ assert_eq!(lengths(&s.model.state()), fresh(&s), "rolled back");
+ run(&mut s, "timeline.rollback", json!({}));
+ assert_eq!(lengths(&s.model.state()), fresh(&s), "rolled forward");
+ run(&mut s, "timeline.rollback", json!({"position": 1}));
+ }
+ run(&mut s, "timeline.rollback", json!({}));
+ let got = lengths(&s.model.state());
+ assert!(got.len() == 2 && got.iter().all(|(_, l)| (l - 25.0).abs() < 1e-9), "the box and its copy, 25 mm: {got:?}");
+}
+
+/// A pattern whose source was deleted copies nothing. A feature made later under the freed name
+/// is found by that name, after the pattern in the timeline: the model is what a fresh evaluation
+/// makes of the design, where the pattern's empty result had been kept.
+#[test]
+fn a_pattern_finds_a_later_feature_that_takes_its_deleted_sources_name() {
+ let mut s = Session::default();
+ run(&mut s, "solid.cylinder", json!({"radius": 2, "height": 15, "axis": "Y", "base": [-16, 6, -5]}));
+ run(&mut s, "solid.pattern.rectangular", json!({"features": ["Cylinder1"], "dir1": [1, 0, 0], "count1": 2, "spacing1": 19}));
+ run(&mut s, "timeline.delete", json!({"feature": "Cylinder1"}));
+ run(&mut s, "solid.cylinder", json!({"radius": 5, "height": 19, "base": [-3, -7, -1]}));
+ let bodies = |st: &solidcraft_doc::ModelState| st.bodies.iter().map(|b| (b.name.clone(), b.body.outline_bounds().min.x)).collect::<Vec<_>>();
+ let mut fresh = solidcraft_doc::Model::new();
+ fresh.no_cache = true;
+ fresh.evaluate(&s.doc);
+ assert_eq!(bodies(&s.model.state()), bodies(&fresh.state()));
+}
+
#[test]
fn feature_pattern_rejects_malformed_alignment_and_flip_atomically() {
let mut s = feature_pattern_plate();