Skip to content

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

Extrude: a profile drawn twice is cut once, not as two coincident tools (#5543) (#5789)

DuBearte0752d8d79c9a
diff --git a/crates/engine/src/boolean_regression_tests.rs b/crates/engine/src/boolean_regression_tests.rs
index f34b41709a..9a6b73a4a9 100644
--- a/crates/engine/src/boolean_regression_tests.rs
+++ b/crates/engine/src/boolean_regression_tests.rs
@@ -117,6 +117,47 @@ fn revolve_cut_round_a_box_corner_does_not_panic() {
}
}
+/// #5543: a grid of 121 holes cut through a plate, four of its circles drawn a second time on
+/// top of themselves. Each of those was two profiles of one region and so two coincident tools:
+/// the cut retried with the tool nudged and took fifty times as long. The profiles drawn twice
+/// are cut once: no retry, and the plate is the one the 121 circles drawn once make.
+#[test]
+fn circles_drawn_twice_are_cut_once() {
+ let cut = |repeats: usize| {
+ let mut s = Session::default();
+ s.execute("sketch.create", &json!({"plane": "XY"})).unwrap();
+ s.execute("sketch.rectangle.two_point", &json!({"p0": [-35, -35], "p1": [35, 35]})).unwrap();
+ s.execute("sketch.finish", &json!({})).unwrap();
+ s.execute("solid.extrude", &json!({"distance": 2, "operation": "new"})).unwrap();
+ s.execute("sketch.create", &json!({"plane": "XY", "offset": 2})).unwrap();
+ let mut centres = Vec::new();
+ for i in -7i32..8 {
+ for j in -8i32..9 {
+ let (x, y) = (f64::from(j) * 3.45 + if i % 2 != 0 { 1.725 } else { 0.0 }, f64::from(i) * 3.0);
+ if x.hypot(y) <= 20.7 {
+ centres.push([x, y]);
+ }
+ }
+ }
+ assert_eq!(centres.len(), 121);
+ for c in centres.iter().chain(centres.iter().take(repeats)) {
+ s.execute("sketch.circle.center", &json!({"center": c, "radius": 1.15})).unwrap();
+ }
+ s.execute("sketch.finish", &json!({})).unwrap();
+ let r = s.execute("solid.extrude", &json!({"operation": "cut", "direction": "negative", "distance": 2, "targets": ["Body1"]})).unwrap();
+ assert!(r["warning"].is_null(), "{}", r["warning"]);
+ let state = s.model.state();
+ let body = &state.body("Body1").unwrap().body;
+ (body.volume_at(1e-4).unwrap(), body.face_count())
+ };
+ let (once, faces_once) = cut(0);
+ let (twice, faces_twice) = cut(4);
+ let analytic = 70.0 * 70.0 * 2.0 - 121.0 * std::f64::consts::PI * 1.15 * 1.15 * 2.0;
+ assert!((once - analytic).abs() < 0.2, "{once} vs {analytic}");
+ assert!((twice - once).abs() < 1e-9 * once, "{twice} vs {once}");
+ assert_eq!(faces_twice, faces_once);
+}
+
/// #1985: the STEP export of a small enclosure never finished. The export reads its own file
/// back, and meshing the screw hole's seam cylinder never ended: the boundary walk took
/// projected angles millions of turns apart. The file is written, reads back cleanly, and holds
diff --git a/crates/sketch/src/profiles.rs b/crates/sketch/src/profiles.rs
index ef7054473b..369f88021d 100644
--- a/crates/sketch/src/profiles.rs
+++ b/crates/sketch/src/profiles.rs
@@ -1172,9 +1172,53 @@ fn rejoined(segs: Vec<Seg2>, joins: &impl Fn(&Seg2, &Seg2) -> bool) -> Vec<Seg2>
out
}
+/// `regions` with each region that repeats an earlier one (the same boundary within a small
+/// tolerance) left out.
+fn without_repeats(regions: &[Region2]) -> Vec<Region2> {
+ // Comparing every pair is quadratic: beyond this many the regions are kept as they are.
+ const MAX_COMPARED: usize = 5000;
+ if regions.len() < 2 || regions.len() > MAX_COMPARED {
+ return regions.to_vec();
+ }
+ let keys: Vec<(f64, Vec2, f64)> =
+ regions.iter().map(|r| (r.outer.signed_area().abs(), r.outer.centroid(), r.outer.segs.iter().map(Seg2::length).sum::<f64>())).collect();
+ let mut kept: Vec<usize> = Vec::new();
+ for (i, r) in regions.iter().enumerate() {
+ let Some(&(area, centroid, size)) = keys.get(i) else { continue };
+ let tol = 1e-7 * (1.0 + size);
+ let repeat = kept.iter().any(|&k| {
+ keys.get(k).is_some_and(|&(a, c, _)| (a - area).abs() <= tol * (1.0 + size) && c.dist(centroid) <= tol)
+ && regions.get(k).is_some_and(|other| same_region(r, other, tol))
+ });
+ if !repeat {
+ kept.push(i);
+ }
+ }
+ kept.iter().filter_map(|&i| regions.get(i).cloned()).collect()
+}
+
+/// Whether two regions have the same boundary within `tol`: the same outer loop and the same
+/// holes, whichever segment each loop starts with.
+fn same_region(a: &Region2, b: &Region2, tol: f64) -> bool {
+ let on = |p: Vec2, l: &Loop2| l.segs.iter().any(|s| s.point_at(s.closest_param(p)).dist(p) <= tol);
+ // Each loop's corners and segment middles lie on the other loop.
+ let covers = |x: &Loop2, y: &Loop2| x.segs.iter().all(|s| on(s.start(), y) && on(s.mid(), y));
+ let same_loop = |x: &Loop2, y: &Loop2| {
+ (x.signed_area().abs() - y.signed_area().abs()).abs() <= tol * (1.0 + x.segs.iter().map(Seg2::length).sum::<f64>())
+ && covers(x, y)
+ && covers(y, x)
+ };
+ a.holes.len() == b.holes.len() && same_loop(&a.outer, &b.outer) && a.holes.iter().all(|h| b.holes.iter().any(|g| same_loop(h, g)))
+}
+
/// Union of regions that touch along shared boundary segments (one extrude of several adjacent
/// profiles makes one body). Disjoint regions stay separate.
pub fn merge_regions(regions: &[Region2]) -> Vec<Region2> {
+ // A region drawn again on top of another is one region (#5543): a circle drawn twice at one
+ // place is two profiles, and extruded as two coincident tools a cut through a grid of holes
+ // with four such circles retried with the tool nudged and took fifty times as long.
+ let distinct = without_repeats(regions);
+ let regions = distinct.as_slice();
let tol = MERGE_TOL * 10.0;
// Every boundary segment oriented with the region on its left.
let mut segs: Vec<Seg2> = Vec::new();