SolidCraft: an open-source parametric 3D CAD application built in pure Rust. Desktop, web, and automation through MCP.
fix(doc): permit circular edge centered on axis in axis_point plane construction (#12851) (#12882)
ashxddaga24c7bb7d70d3
diff --git a/crates/doc/src/construct.rs b/crates/doc/src/construct.rsindex 6287428e0b..e57b02075f 100644--- a/crates/doc/src/construct.rs+++ b/crates/doc/src/construct.rs@@ -196,6 +196,45 @@ fn edge_pick<'a>(st: &'a ModelState, at: Vec3, name: &Option<String>) -> Result<}}+/// The closest point on a polyline to `target`, projecting onto each segment rather than+/// choosing only its vertices. This keeps an edge pick on the edge between tessellation points.+fn closest_point_on_polyline(pts: &[Vec3], target: Vec3) -> Option<Vec3> {+ let mut best: Option<(f64, Vec3)> = None;+ for w in pts.windows(2) {+ let (a, b) = (w[0], w[1]);+ let ab = b - a;+ let len_sq = ab.dot(ab);+ let q = if len_sq < 1e-18 {+ a+ } else {+ let t = ((target - a).dot(ab) / len_sq).clamp(0.0, 1.0);+ a + ab * t+ };+ let d = target.dist(q);+ if best.as_ref().is_none_or(|(bd, _)| d < *bd) {+ best = Some((d, q));+ }+ }+ best.map(|(_, q)| q)+}++/// The point on an edge nearest a pick location, projecting onto exact edge geometry+/// (such as `kernel::edge_circle`) when available, else onto its drawn polyline segments.+fn edge_point_at_pick(st: &ModelState, pick: Vec3, name: &Option<String>) -> Option<Vec3> {+ let (b, i, pts) = edge_pick(st, pick, name).ok()?;+ if let Some(c) = kernel::edge_circle(&b.body, i) {+ let v = pick - c.center;+ let v_perp = v - *c.axis * v.dot(*c.axis);+ if let Some(dir) = v_perp.normalized() {+ return Some(c.center + dir * c.radius);+ }+ }+ // Until the kernel can project onto any edge geometry (circles are exact above via+ // kernel::edge_circle), the fallback projects onto the drawn polyline. This is exact for+ // straight edges and approximates splines within mesh display tolerance.+ closest_point_on_polyline(&pts, pick)+}+/// The circle a picked edge lies on, from its exact curve (`kernel::edge_circle`) when the drawn/// edge lies on it, else fitted to the drawn points: (centre, unit normal).fn edge_circle(b: &ModelBody, index: usize, pts: &[Vec3]) -> Option<(Vec3, Vec3)> {@@ -743,7 +782,29 @@ pub(crate) fn resolve_geo_plane(ctx: &Ctx, p: &PlaneRef) -> Result<Plane> {PlaneRef::AxisPoint { axis, point: at } => {let (origin, dir) = line(ctx, axis)?;let dir = dir.normalized().ok_or_else(|| bad("the axis has no direction"))?;- let delta = point(ctx, at)? - origin;+ let p = match point(ctx, at) {+ Ok(p) => {+ let delta = p - origin;+ let across = delta - dir * delta.dot(dir);+ if across.len().is_finite() && across.len() >= 1e-9 {+ p+ } else if let GeoRef::Edge { at: pick, name } = at.as_ref() {+ // When the centre of a circular edge lies on the axis, resolve to the picked point on the edge.+ edge_point_at_pick(ctx.st, *pick, name).unwrap_or(p)+ } else {+ p+ }+ }+ Err(e) => {+ // Non-circular edges are not converted to centres by `point`; resolve to the picked edge point.+ if let GeoRef::Edge { at: pick, name } = at.as_ref() {+ edge_point_at_pick(ctx.st, *pick, name).ok_or(e)?+ } else {+ return Err(e);+ }+ }+ };+ let delta = p - origin;let across = delta - dir * delta.dot(dir);if !across.len().is_finite() || across.len() < 1e-9 {return Err(bad("the point is on the axis: pick a point off the axis to define a unique plane"));diff --git a/crates/engine/src/construct_tests.rs b/crates/engine/src/construct_tests.rsindex 2ae063c7af..8a5886d117 100644--- a/crates/engine/src/construct_tests.rs+++ b/crates/engine/src/construct_tests.rs@@ -213,6 +213,44 @@ fn plane_through_axis_and_point_accepts_straight_model_and_sketch_lines() {assert!(parallel(plane(&s, "Side").normal(), Vec3::Y));}+/// Issue #12851: construct.plane.axis_point with an edge pick whose circle centre lies on the+/// axis (e.g. rim of a cylinder or revolved torus). The centre is on the axis, so resolving the+/// edge pick to the centre would refuse with "the point is on the axis". Instead, resolve to the+/// picked point on the edge, supporting picks at any angle around the rim.+#[test]+fn plane_through_axis_and_circular_edge_centered_on_axis_succeeds() {+ let mut s = Session::default();+ run(&mut s, "solid.cylinder", json!({"base": [0, 0, 0], "axis": [0, 1, 0], "radius": 15, "height": 30, "body_name": "Cyl"}));+ // Edge at 0 degrees round the rim (x = 15, y = 30, z = 0).+ run(+ &mut s,+ "construct.plane.axis_point",+ json!({+ "axis": {"axis": "Y"},+ "point": {"edge": [15, 30, 0]},+ "name": "PlaneAt0Deg"+ }),+ );+ let pl0 = plane(&s, "PlaneAt0Deg");+ assert!(parallel(pl0.normal(), Vec3::Z), "normal is {:?}", pl0.normal());+ assert!(on_plane(&pl0, Vec3::new(15.0, 30.0, 0.0)));+ assert!(on_plane(&pl0, Vec3::new(0.0, 0.0, 0.0)));++ // Edge pick at 90 degrees round the rim (x = 0, y = 30, z = 15).+ run(+ &mut s,+ "construct.plane.axis_point",+ json!({+ "axis": {"axis": "Y"},+ "point": {"edge": [0, 30, 15]},+ "name": "PlaneAt90Deg"+ }),+ );+ let pl90 = plane(&s, "PlaneAt90Deg");+ assert!(parallel(pl90.normal(), Vec3::X), "normal is {:?}", pl90.normal());+ assert!(on_plane(&pl90, Vec3::new(0.0, 30.0, 15.0)));+}+#[test]fn plane_through_axis_and_point_in_a_moved_component_keeps_its_world_picks() {let mut s = Session::default();