Skip to content

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

fix: copy a pattern's features in timeline order, not in the order they were picked (#4873)

Nathalie Schönwettere2b8bf5a059a
diff --git a/crates/doc/src/eval.rs b/crates/doc/src/eval.rs
index 4b596b08a9..39162c1415 100644
--- a/crates/doc/src/eval.rs
+++ b/crates/doc/src/eval.rs
@@ -5718,8 +5718,13 @@ fn replay(
format!("{list} suppressed and not copied")
});
}
+ // The copies are made in timeline order, whatever order the features were picked in: a
+ // chamfer picked before the hole it blends would otherwise find no edge to copy.
+ let mut kept: Vec<String> = features.iter().filter(|n| !suppressed.contains(n)).cloned().collect();
+ let at = |name: &String| source_feature(doc, name).ok().and_then(|f| doc.features.iter().position(|x| std::ptr::eq(x, f))).unwrap_or(usize::MAX);
+ kept.sort_by_key(at);
+ kept.dedup();
let (mut missed, mut total) = (0usize, 0usize);
- let kept: Vec<String> = features.iter().filter(|n| !suppressed.contains(n)).cloned().collect();
let plan = copy_plan(doc, vals, st, &kept, mats)?;
for (src, mats, first) in &plan {
// A fillet or chamfer repeats at the copied edges.
diff --git a/crates/engine/src/cmd/patterns_tests.rs b/crates/engine/src/cmd/patterns_tests.rs
index 7375af0172..a8c24c4e65 100644
--- a/crates/engine/src/cmd/patterns_tests.rs
+++ b/crates/engine/src/cmd/patterns_tests.rs
@@ -803,3 +803,44 @@ fn patterns_and_mirrors_of_patterns_and_mirrors() {
assert!(rel(after, want) < 1e-5, "{what}: {after} mm³, {added} more holes than {before} = {want}");
}
}
+
+/// A disc with an off-centre hole whose top and bottom edges are chamfered.
+fn disc_with_chamfered_hole() -> Session {
+ let mut s = Session::default();
+ run(&mut s, "solid.cylinder", json!({"radius": 30, "height": 10, "name": "Disc"}));
+ run(&mut s, "solid.hole", json!({"position": [20, 0, 10], "diameter": 6, "name": "Bore"}));
+ run(&mut s, "solid.chamfer", json!({"edges": [[23, 0, 10]], "distance": 0.5, "name": "TopChamfer"}));
+ run(&mut s, "solid.chamfer", json!({"edges": [[23, 0, 0]], "distance": 0.5, "name": "BottomChamfer"}));
+ s
+}
+
+#[test]
+fn circular_pattern_of_a_hole_and_its_chamfers_copies_all_of_them() {
+ let mut one = disc_with_chamfered_hole();
+ let v1 = volume(&mut one);
+ let mut s = disc_with_chamfered_hole();
+ let v0 = volume(&mut s);
+ assert!(rel(v0, v1) < 1e-9);
+ run(&mut s, "solid.pattern.circular", json!({"features": ["Bore", "TopChamfer", "BottomChamfer"], "axis": "Z", "count": 6, "angle": 360}));
+ let v = volume(&mut s);
+ println!("{v1} -> {v}");
+ assert!(v < v1 - 5.0 * 0.9 * (std::f64::consts::PI * 9.0 * 10.0), "pattern had no effect: {v1} -> {v}");
+}
+
+/// The features are copied in timeline order, not in the order they were picked in: a chamfer
+/// picked before its hole used to find no edge ("no edge near ...").
+#[test]
+fn pattern_of_features_picked_in_any_order_copies_them_in_timeline_order() {
+ let mut reference = disc_with_chamfered_hole();
+ run(
+ &mut reference,
+ "solid.pattern.circular",
+ json!({"features": ["Bore", "TopChamfer", "BottomChamfer"], "axis": "Z", "count": 6, "angle": 360}),
+ );
+ let expected = volume(&mut reference);
+ for picked in [["BottomChamfer", "TopChamfer", "Bore"], ["TopChamfer", "Bore", "BottomChamfer"]] {
+ let mut s = disc_with_chamfered_hole();
+ run(&mut s, "solid.pattern.circular", json!({"features": picked, "axis": "Z", "count": 6, "angle": 360}));
+ assert!(rel(volume(&mut s), expected) < 1e-9, "{picked:?}");
+ }
+}