Skip to content

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

Measure: an exact circle's or arc's length agrees with its circumference (#9086) (#9227)

austin62jaked9aa73b597e2
diff --git a/crates/engine/src/cmd/measure_sel.rs b/crates/engine/src/cmd/measure_sel.rs
index 7279dd2e7f..f55cbfe992 100644
--- a/crates/engine/src/cmd/measure_sel.rs
+++ b/crates/engine/src/cmd/measure_sel.rs
@@ -104,8 +104,9 @@ fn fit_circle(pts: &[Vec3], tol: f64) -> Option<(Vec3, UnitVec3, f64)> {
}
/// The round shape of mesh edge `index` of body `b`: from the kernel's exact curve when it
-/// is the same edge (its ends on the polyline), else fitted to the polyline's points.
-fn edge_round(b: &solidcraft_doc::ModelBody, m: &Mesh, index: usize, whole: bool) -> Option<Shape> {
+/// is the same edge (its ends on the polyline), else fitted to the polyline's points; and
+/// whether it is the exact one.
+fn edge_round(b: &solidcraft_doc::ModelBody, m: &Mesh, index: usize, whole: bool) -> Option<(Shape, bool)> {
let pts = m.edges.get(index)?;
let tol = (m.bounds().diagonal().max(1.0) * 1e-6).max(1e-9);
let exact = solidcraft_kernel::edge_circle(&b.body, index).filter(|c| {
@@ -113,6 +114,7 @@ fn edge_round(b: &solidcraft_doc::ModelBody, m: &Mesh, index: usize, whole: bool
let on = pts.iter().all(|p| (p.dist(c.center) - c.radius).abs() <= tol * 10.0);
ends && on
});
+ let is_exact = exact.is_some();
let (center, axis, radius, angle) = match exact {
Some(c) => (c.center, c.axis, c.radius, if whole { std::f64::consts::TAU } else { c.angle }),
None => {
@@ -123,7 +125,17 @@ fn edge_round(b: &solidcraft_doc::ModelBody, m: &Mesh, index: usize, whole: bool
(center, axis, radius, angle)
}
};
- Some(Shape::Round { center, axis, radius, angle, planar: true, lo: 0.0, hi: 0.0 })
+ Some((Shape::Round { center, axis, radius, angle, planar: true, lo: 0.0, hi: 0.0 }, is_exact))
+}
+
+/// The length of a measured curve: an exact circle's or arc's own (radius times angle, as its
+/// circumference and arc length are), else that of the polyline drawn for it, which runs inside
+/// a curve (#9086).
+fn curve_len(shape: &Shape, exact: bool, polyline: f64) -> f64 {
+ match *shape {
+ Shape::Round { radius, angle, .. } if exact => radius * angle,
+ _ => polyline,
+ }
}
/// Points along a circle or arc (`angle` from `start` about `axis`).
@@ -192,7 +204,8 @@ fn sketch_curve(ss: &solidcraft_doc::SolvedSketch, id: &str) -> Option<Item> {
(b - a).normalized()
}
_ => {
- json["length_mm"] = json!(polyline_len(&pts));
+ // A sketch's circle or arc is exact.
+ json["length_mm"] = json!(curve_len(&shape, true, polyline_len(&pts)));
add_round(&mut json, &shape);
None
}
@@ -290,19 +303,18 @@ fn describe(s: &Session, sel: &Sel) -> Option<Item> {
let pieces: Vec<&Vec<Vec3>> = rim.iter().filter_map(|&i| m.edges.get(i)).collect();
let len: f64 = pieces.iter().map(|p| polyline_len(p)).sum();
let points = pieces.iter().flat_map(|p| densify(p)).collect();
- let mut json = json!({"type": "edge", "length_mm": len});
- let shape = edge_round(b, &m, index, true).unwrap_or(Shape::Other);
+ let (shape, exact) = edge_round(b, &m, index, true).unwrap_or((Shape::Other, false));
+ let mut json = json!({"type": "edge", "length_mm": curve_len(&shape, exact, len)});
add_round(&mut json, &shape);
return Some(Item { geo: Geo::Points(points), dir: None, json, shape, slot: None });
}
let (a, z) = (*pts.first()?, *pts.last()?);
let straight = (z - a).normalized().filter(|d| pts.iter().all(|p| (*p - a).cross(*d).len() < 1e-6 * (1.0 + a.dist(z))));
- let len = polyline_len(pts);
- let mut json = json!({"type": "edge", "length_mm": len});
- let shape = match straight {
- Some(_) => Shape::Line { a, b: z },
- None => edge_round(b, &m, index, false).unwrap_or(Shape::Other),
+ let (shape, exact) = match straight {
+ Some(_) => (Shape::Line { a, b: z }, false),
+ None => edge_round(b, &m, index, false).unwrap_or((Shape::Other, false)),
};
+ let mut json = json!({"type": "edge", "length_mm": curve_len(&shape, exact, polyline_len(pts))});
add_round(&mut json, &shape);
Some(Item { geo: Geo::Points(densify(pts)), dir: straight, json, shape, slot: None })
}
diff --git a/crates/engine/src/measure_centre_tests.rs b/crates/engine/src/measure_centre_tests.rs
index 9c87130a87..36e0eda924 100644
--- a/crates/engine/src/measure_centre_tests.rs
+++ b/crates/engine/src/measure_centre_tests.rs
@@ -159,6 +159,8 @@ fn two_holes_measure_centre_to_centre_with_min_and_max() {
// One rim: Ø, R and circumference first-class.
let r = measure(&mut s, vec![ea]);
assert!(close(&r["diameter"], 6.0, 1e-9) && close(&r["radius"], 3.0, 1e-9) && close(&r["circumference"], TAU * 3.0, 1e-9), "{r}");
+ // Its length is its circumference, not the length of the polyline drawn for it (#9086).
+ assert!(close(&r["items"][0]["length_mm"], TAU * 3.0, 1e-9), "{r}");
assert_eq!(r["items"][0]["round"], "circle", "{r}");
}
@@ -259,9 +261,11 @@ fn sketch_circle_arc_and_slot() {
run(&mut s, "sketch.arc.center_point", json!({"center": [30, 0], "start": [34, 0], "sweep": 90}))["curves"][0].as_str().unwrap().to_string();
let r = measure(&mut s, vec![json!({"type": "sketch_curve", "id": c})]);
assert!(close(&r["diameter"], 10.0, 1e-9) && close(&r["radius"], 5.0, 1e-9) && close(&r["circumference"], 10.0 * PI, 1e-9), "{r}");
+ assert!(close(&r["items"][0]["length_mm"], 10.0 * PI, 1e-9), "{r}");
let r = measure(&mut s, vec![json!({"type": "sketch_curve", "id": a})]);
assert!(close(&r["radius"], 4.0, 1e-9) && close(&r["diameter"], 8.0, 1e-9), "{r}");
assert!(close(&r["angle_deg"], 90.0, 1e-9) && close(&r["arc_length"], 2.0 * PI, 1e-9), "{r}");
+ assert!(close(&r["items"][0]["length_mm"], 2.0 * PI, 1e-9), "{r}");
assert_eq!(r["items"][0]["round"], "arc");
// Circle to arc: centre to centre.
let r = measure(&mut s, vec![json!({"type": "sketch_curve", "id": c}), json!({"type": "sketch_curve", "id": a})]);
@@ -315,9 +319,23 @@ fn a_fillet_edge_is_an_arc() {
let r = measure(&mut s, vec![e]);
assert!(close(&r["radius"], 5.0, 1e-9) && close(&r["diameter"], 10.0, 1e-9), "{r}");
assert!(close(&r["angle_deg"], 90.0, 1e-6) && close(&r["arc_length"], 2.5 * PI, 1e-6), "{r}");
+ assert!(close(&r["items"][0]["length_mm"], 2.5 * PI, 1e-6), "{r}");
assert!(close(&r["center"][0], 35.0, 1e-9) && close(&r["center"][1], 25.0, 1e-9), "{r}");
}
+/// A cylinder's top circle is as long as its circumference, in the same reply: the length
+/// comes from the exact circle, not from the polyline drawn for it, which runs inside it
+/// (#9086, the report's own cylinder).
+#[test]
+fn a_cylinders_top_circle_is_as_long_as_its_circumference() {
+ let mut s = Session::default();
+ run(&mut s, "solid.cylinder", json!({"radius": 5, "height": 20, "base": [40, 10, 0], "operation": "new"}));
+ let e = edge(&s, |p| (p.z - 20.0).abs() < 1e-9 && ((p - Vec3::new(40.0, 10.0, 20.0)).len() - 5.0).abs() < 1e-6);
+ let r = measure(&mut s, vec![e]);
+ assert!(close(&r["circumference"], TAU * 5.0, 1e-9), "{r}");
+ assert!(close(&r["items"][0]["length_mm"], TAU * 5.0, 1e-9), "{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() {