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

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 <nathalie@cfw-ag.ch>
Co-authored-by: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
Nathalie Schönwetter
2026-10-24 21:52:49 +00:00
committed by GitHub
parent 9e065aa0d5
commit e2b8bf5a05
2 changed files with 47 additions and 1 deletions
+6 -1
View File
@@ -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.
+41
View File
@@ -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:?}");
}
}