Skip to content

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

Hole: size the tool by the body it drills, not the largest body (#2904) (#2975)

rlartotf42c06806374
diff --git a/crates/doc/src/eval.rs b/crates/doc/src/eval.rs
index 615dbb2e82..063ce5489b 100644
--- a/crates/doc/src/eval.rs
+++ b/crates/doc/src/eval.rs
@@ -2892,6 +2892,38 @@ fn hole_tools(
Ok(hole_tools_and_prisms(vals, st, position, dir, diameter, depth, hole)?.into_iter().map(|(tool, _)| tool).collect())
}
+/// The scale a hole's tools are sized by: the largest body the hole's axis meets (#2904), so a
+/// large body elsewhere in the model changes neither the tool's reach above the face (and so
+/// the countersink's outline) nor its depth. A hole whose axis meets no body is sized by the
+/// largest body of the model, as before.
+fn hole_axis_size(st: &ModelState, position: &Vec3, dir: Vec3) -> f64 {
+ let along = |b: &ModelBody| {
+ // The axis (an unbounded line) against the body's box, grown by the display tolerance.
+ let bb = b.mesh().bounds();
+ if bb.is_empty() {
+ return false;
+ }
+ let pad = 2.0 * b.display_tol();
+ let (mut lo, mut hi) = (f64::NEG_INFINITY, f64::INFINITY);
+ for (o, d, min, max) in
+ [(position.x, dir.x, bb.min.x, bb.max.x), (position.y, dir.y, bb.min.y, bb.max.y), (position.z, dir.z, bb.min.z, bb.max.z)]
+ {
+ if d.abs() < 1e-12 {
+ if o < min - pad || o > max + pad {
+ return false;
+ }
+ } else {
+ let (a, c) = ((min - pad - o) / d, (max + pad - o) / d);
+ lo = lo.max(a.min(c));
+ hi = hi.min(a.max(c));
+ }
+ }
+ lo <= hi
+ };
+ let met = st.bodies.iter().filter(|b| along(b)).map(|b| b.body.size()).fold(0.0, f64::max);
+ if met > 0.0 { met } else { st.bodies.iter().map(|b| b.body.size()).fold(1.0, f64::max) }
+}
+
/// [`hole_tools`], each with its exact geometry when it is a straight cylinder along the axis
/// (a plain hole, a counterbore, a drill point left in the air past a through hole), so sheet
/// metal can keep it as a flat cut-out or pocket. Each tool's faces are named by the part of the
@@ -2910,7 +2942,7 @@ fn hole_tools_and_prisms(
if !(r > 1e-6) {
return Err(DocError::Invalid("hole diameter must be positive".into()));
}
- let size = st.bodies.iter().map(|b| b.body.size()).fold(1.0, f64::max);
+ let size = hole_axis_size(st, position, dir);
let top = (size * 0.01).max(0.1);
let bottom = match depth {
Some(d) => -val(vals, d, Kind::Length)?,
diff --git a/crates/engine/src/cmd/single_hole_tests.rs b/crates/engine/src/cmd/single_hole_tests.rs
index 52644219c3..9249abbee0 100644
--- a/crates/engine/src/cmd/single_hole_tests.rs
+++ b/crates/engine/src/cmd/single_hole_tests.rs
@@ -98,3 +98,38 @@ fn a_single_hole_takes_one_point_and_no_flip() {
assert!(s.execute("solid.hole", &p).is_err(), "{p}");
}
}
+
+/// A lid (rounded corners) with a countersunk hole 1.5 mm from its edge, and a second body `far` away from it.
+/// Returns the lid's volume (the total less the far body's) after the hole and the bodies the hole changed.
+fn lid_with_far_body(far_size: [f64; 3]) -> (f64, Value) {
+ let mut s = Session::default();
+ run(&mut s, "sketch.create", json!({"plane": "XY", "offset": "7.1 mm"}));
+ run(&mut s, "sketch.rectangle.two_point", json!({"p0": [-40.2, -9.85], "p1": [40.2, 13.05]}));
+ run(&mut s, "sketch.fillet", json!({"points": ["l1.start", "l2.start", "l3.start", "l4.start"], "radius": "2 mm"}));
+ run(&mut s, "sketch.finish", json!({}));
+ run(&mut s, "solid.extrude", json!({"distance": "2 mm", "operation": "new", "body_name": "lid"}));
+ run(
+ &mut s,
+ "solid.box",
+ json!({"length": far_size[0], "width": far_size[1], "height": far_size[2], "corner": [158, -60, -5.9], "body_name": "far"}),
+ );
+ let r = run(
+ &mut s,
+ "solid.hole",
+ json!({"position": [-38.7, -8.35, 9.1], "diameter": "2.4 mm", "type": "countersink", "cs_diameter": "4.2 mm", "cs_angle": "90 deg"}),
+ );
+ let lid = volume(&mut s) - far_size.iter().product::<f64>();
+ (lid, r["changed_bodies"].clone())
+}
+
+/// #2904: the tool of a hole depends on the part it drills, not on how big some other body of the
+/// model is: a large body far away leaves the countersink, the volume removed and the bodies
+/// changed as they are with a 1 mm cube there.
+#[test]
+fn a_far_body_does_not_change_a_countersunk_hole() {
+ let (small, small_changed) = lid_with_far_body([1.0, 1.0, 1.0]);
+ let (large, large_changed) = lid_with_far_body([178.0, 160.0, 12.5]);
+ assert!((small - large).abs() < 1e-3, "lid volume {small} with a 1 mm far body, {large} with a large one");
+ assert_eq!(small_changed, large_changed, "bodies changed");
+ assert_eq!(large_changed, json!(["lid"]), "only the lid is drilled");
+}