From e2b8bf5a059a51c7bf90b51bb35591e0a9e68517 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nathalie=20Sch=C3=B6nwetter?= <1506194+Make-o-Rama@users.noreply.github.com> Date: Sat, 24 Oct 2026 21:52:49 +0000 Subject: [PATCH] fix: copy a pattern's features in timeline order, not in the order they were picked (#4873) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit A circular or rectangular pattern of a hole and the chamfers on its edges failed with "no edge near ..." when a chamfer was picked before the hole: the chamfer was copied first, onto a body that had no hole there yet. Co-authored-by: Nathalie Schönwetter Co-authored-by: Claude Sonnet 5.5 --- crates/doc/src/eval.rs | 7 ++++- crates/engine/src/cmd/patterns_tests.rs | 41 +++++++++++++++++++++++++ 2 files changed, 47 insertions(+), 1 deletion(-) diff --git a/crates/doc/src/eval.rs b/crates/doc/src/eval.rs index 4b596b08a..39162c141 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 = 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 = 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 7375af017..a8c24c4e6 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:?}"); + } +}