Skip to content

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

Measure: a fillet's torus or a sphere reports its radius (#7159) (#7160)

rbousigues88a0ed5a99235f
diff --git a/crates/engine/src/cmd/measure_sel.rs b/crates/engine/src/cmd/measure_sel.rs
index 00f3450b3e..af38dd7ad9 100644
--- a/crates/engine/src/cmd/measure_sel.rs
+++ b/crates/engine/src/cmd/measure_sel.rs
@@ -353,7 +353,8 @@ fn describe(s: &Session, sel: &Sel) -> Option<Item> {
let planar = normals.first().copied().filter(|n0| normals.iter().all(|n| n.dot(*n0) > 1.0 - 1e-6));
let mut json = json!({"type": "face", "area_mm2": area, "planar": planar.is_some()});
let verts: Vec<Vec3> = tris.iter().flat_map(|t| t.iter().copied()).collect();
- let shape = match solidcraft_kernel::face_surface(&b.body, *index) {
+ let surface = solidcraft_kernel::face_surface(&b.body, *index);
+ let shape = match surface {
Some(solidcraft_kernel::FaceSurface::Cylinder { origin, axis, radius }) => {
super::measure_shape::cylinder(origin, axis, radius, &verts).unwrap_or(Shape::Other)
}
@@ -365,6 +366,7 @@ fn describe(s: &Session, sel: &Sel) -> Option<Item> {
_ => Shape::Other,
};
add_round(&mut json, &shape);
+ super::measure_shape::add_curved(&mut json, surface);
let slot = if matches!(shape, Shape::Round { .. }) && !shape.full() { face_slot(b, &m, &shape, &verts) } else { None };
if let Some(sl) = &slot {
json["slot"] = sl.json();
diff --git a/crates/engine/src/cmd/measure_shape.rs b/crates/engine/src/cmd/measure_shape.rs
index 71fb6904a4..cbe9b712ea 100644
--- a/crates/engine/src/cmd/measure_shape.rs
+++ b/crates/engine/src/cmd/measure_shape.rs
@@ -68,6 +68,27 @@ pub(crate) fn add_round(v: &mut Value, shape: &Shape) {
});
}
+/// The radius of a toroidal or spherical face, added to its description: a fillet round a
+/// circular edge is a torus whose tube radius is the fillet's (`major_radius` is the ring
+/// through the middle of the rolling ball), a ball or a spherical corner patch a sphere. These
+/// faces have no axis to measure from, so they stay out of [`Shape`].
+pub(crate) fn add_curved(v: &mut Value, surface: Option<solidcraft_kernel::FaceSurface>) {
+ use solidcraft_kernel::FaceSurface;
+ let (radius, kind) = match surface {
+ Some(FaceSurface::Torus { minor, major, .. }) if minor.is_finite() && minor > 0.0 => {
+ if major.is_finite() {
+ v["major_radius"] = json!(major.abs());
+ }
+ (minor, "torus")
+ }
+ Some(FaceSurface::Sphere { radius, .. }) if radius.is_finite() && radius > 0.0 => (radius, "sphere"),
+ _ => return,
+ };
+ v["diameter"] = json!(2.0 * radius);
+ v["radius"] = json!(radius);
+ v["surface"] = json!(kind);
+}
+
/// Cylinder `axis` through `origin` of radius `radius` over the points of its face: the
/// round shape, centred level with the middle of the face, with its extent and coverage.
pub(crate) fn cylinder(origin: Vec3, axis: UnitVec3, radius: f64, pts: &[Vec3]) -> Option<Shape> {
diff --git a/crates/engine/src/measure_centre_tests.rs b/crates/engine/src/measure_centre_tests.rs
index 36e0eda924..f0bff39b11 100644
--- a/crates/engine/src/measure_centre_tests.rs
+++ b/crates/engine/src/measure_centre_tests.rs
@@ -336,6 +336,48 @@ fn a_cylinders_top_circle_is_as_long_as_its_circumference() {
assert!(close(&r["items"][0]["length_mm"], TAU * 5.0, 1e-9), "{r}");
}
+/// The first face whose analytic surface satisfies `want`, as a pick.
+fn face_of(s: &Session, want: impl Fn(solidcraft_kernel::FaceSurface) -> bool) -> Value {
+ let st = s.model.state();
+ let b = st.bodies.first().unwrap();
+ let m = b.mesh();
+ let n = m.tri_face.iter().map(|f| *f as usize + 1).max().unwrap();
+ let f = (0..n).find(|&f| solidcraft_kernel::face_surface(&b.body, f).is_some_and(&want)).expect("face");
+ let p = m.triangles.iter().zip(&m.tri_face).find(|(_, tf)| **tf as usize == f).and_then(|(t, _)| m.tri(t)).unwrap()[0];
+ json!({"type": "face", "body": b.name, "index": f, "point": p})
+}
+
+/// A fillet round the foot of a boss is a toroidal face: it measures its radius (the fillet's)
+/// next to its area, and says it is a torus with its ring radius too.
+#[test]
+fn a_fillet_round_a_boss_reports_its_radius() {
+ use solidcraft_kernel::FaceSurface;
+ let mut s = Session::default();
+ run(&mut s, "solid.box", json!({"length": 40, "width": 40, "height": 10}));
+ run(&mut s, "solid.cylinder", json!({"radius": 5, "height": 12, "base": [20, 20, 10], "operation": "join"}));
+ run(&mut s, "solid.fillet", json!({"edges": [[25, 20, 10]], "radius": 3}));
+ let fillet = face_of(&s, |f| matches!(f, FaceSurface::Torus { minor, .. } if (minor - 3.0).abs() < 1e-6));
+ let r = measure(&mut s, vec![fillet]);
+ assert!(close(&r["radius"], 3.0, 1e-6) && close(&r["diameter"], 6.0, 1e-6), "{r}");
+ assert_eq!(r["items"][0]["surface"], "torus", "{r}");
+ // The ring through the middle of the rolling ball: the boss's radius plus the fillet's.
+ assert!(close(&r["items"][0]["major_radius"], 8.0, 1e-6), "{r}");
+ // The area is still there.
+ assert!(r["items"][0]["area_mm2"].as_f64().is_some_and(|a| a > 1.0), "{r}");
+}
+
+/// A ball's face reports its radius.
+#[test]
+fn a_sphere_face_reports_its_radius() {
+ use solidcraft_kernel::FaceSurface;
+ let mut s = Session::default();
+ run(&mut s, "solid.sphere", json!({"radius": 7, "center": [0, 0, 0]}));
+ let ball = face_of(&s, |f| matches!(f, FaceSurface::Sphere { .. }));
+ let r = measure(&mut s, vec![ball]);
+ assert!(close(&r["radius"], 7.0, 1e-6) && close(&r["diameter"], 14.0, 1e-6), "{r}");
+ assert_eq!(r["items"][0]["surface"], "sphere", "{r}");
+}
+
/// A plate with holes through STEP: the holes come back as cylinders, and measure the same.
#[test]
fn step_round_trip_of_a_plate_with_holes() {