From 59cd55f53135470875d5359f7307f7a4909ddf05 Mon Sep 17 00:00:00 2001 From: Colin Faulkingham Date: Tue, 20 Oct 2026 15:55:58 +0000 Subject: [PATCH] Sketch: keep three-point arcs attached to the through point (#2166) (#3610) * claim: #2166 * Fix three-point arc through-point attachments (#2166) --- crates/engine/src/cmd/sketch.rs | 14 +++-- crates/engine/src/cmd/sketch_modify_tests.rs | 54 ++++++++++++++++++++ crates/ui-egui/src/sketch_corner_ui_tests.rs | 26 ++++++++++ crates/ui-egui/src/tools.rs | 2 +- 4 files changed, 90 insertions(+), 6 deletions(-) diff --git a/crates/engine/src/cmd/sketch.rs b/crates/engine/src/cmd/sketch.rs index 5d9170243..77c1b4f7f 100644 --- a/crates/engine/src/cmd/sketch.rs +++ b/crates/engine/src/cmd/sketch.rs @@ -78,7 +78,7 @@ pub static COMMANDS: &[CommandSpec] = &[ .icon("circle3") .enabled(in_sketch) .params(concat!("p0, p1, p2", connect_doc!(), created_doc!())), - CommandSpec::new("sketch.arc.three_point", "3-Point Arc", arc_three).at("SKETCH", "CREATE").icon("arc3").enabled(in_sketch).params(concat!("start, end, through", connect_doc!(), created_doc!())), + CommandSpec::new("sketch.arc.three_point", "3-Point Arc", arc_three).at("SKETCH", "CREATE").icon("arc3").enabled(in_sketch).params(concat!("start, end, through: [x,y] or point ref (a referenced through point stays on the arc)", connect_doc!(), created_doc!())), CommandSpec::new("sketch.arc.center_point", "Center Point Arc", arc_center) .at("SKETCH", "CREATE") .icon("arc_center") @@ -1193,18 +1193,22 @@ fn arc_three(s: &mut Session, p: &Value) -> Result { let id = str_(p, "id").map(str::to_string); let (out, info) = draw(s, p, cmd, |sk, doc| { let (a, b) = (req_parg(sk, doc, p, "start", cmd)?, req_parg(sk, doc, p, "end", cmd)?); - let t = req_vec2(cmd, p, "through")?; - let (o, _) = circumcircle(a.pos(), t, b.pos()).ok_or_else(|| bad(cmd, "the points are collinear"))?; + let t = req_parg(sk, doc, p, "through", cmd)?; + let (o, _) = circumcircle(a.pos(), t.pos(), b.pos()).ok_or_else(|| bad(cmd, "the points are collinear"))?; // Counter-clockwise from start to end must pass the through point; otherwise swap. let ang = |q: Vec2| (q - o).angle(); let ccw_span = |from: f64, to: f64| (to - from).rem_euclid(std::f64::consts::TAU); - let ccw = ccw_span(ang(a.pos()), ang(t)) < ccw_span(ang(a.pos()), ang(b.pos())); + let ccw = ccw_span(ang(a.pos()), ang(t.pos())) < ccw_span(ang(a.pos()), ang(b.pos())); let (s0, e0) = if ccw { (a, b) } else { (b, a) }; let ci = sk.add_arc(o, s0.pos(), e0.pos(), [None, s0.idx(), e0.idx()], id.as_deref())?; if let Some(c) = sk.curves.get_mut(ci) { c.reversed = !ccw; } - Ok((ids_of(sk, &[ci]), Vec::new())) + let mut cons = Vec::new(); + if let Some(point) = t.idx() { + cons.push(add_c(sk, ConstraintKind::PointOnCurve { p: point, c: ci })?); + } + Ok((ids_of(sk, &[ci]), cons)) })?; Ok(result(out, info)) } diff --git a/crates/engine/src/cmd/sketch_modify_tests.rs b/crates/engine/src/cmd/sketch_modify_tests.rs index b8a1f21b3..a65fbf258 100644 --- a/crates/engine/src/cmd/sketch_modify_tests.rs +++ b/crates/engine/src/cmd/sketch_modify_tests.rs @@ -337,6 +337,60 @@ fn a_tangent_arc_starts_from_a_trimmed_end() { ); } +#[test] +fn three_point_arc_keeps_referenced_through_points_when_dimensions_change() { + for (through, scale) in [(json!("height.end"), 1.0), (json!({"ref":"height.end"}), 1.0), (json!("mid:height"), 2.0)] { + for reverse in [false, true] { + let mut s = new_sketch(); + run(&mut s, "sketch.line", json!({"points":[[-20,0],[20,0]],"ids":["base"]})); + run(&mut s, "sketch.constraint.fix", json!({"entity":"base"})); + run(&mut s, "sketch.line", json!({"points":["origin",[0,10.0 * scale]],"ids":["height"],"infer":true})); + let dim = run(&mut s, "sketch.dimension", json!({"entities":["height"],"value":10.0 * scale})); + let before = s.doc.to_json(); + let (start, end) = if reverse { ("base.end", "base.start") } else { ("base.start", "base.end") }; + let out = run(&mut s, "sketch.arc.three_point", json!({"start":start,"end":end,"through":through,"id":"arc"})); + let sk = sketch(&s); + let arc = sk.curve_index("arc").unwrap(); + let constraint = sk.constraints.iter().find(|k| matches!(k.kind, ConstraintKind::PointOnCurve { c, .. } if c == arc)).unwrap(); + assert!(ids(&out["created"]["constraints"]).contains(&constraint.id), "{out}"); + run(&mut s, "edit.undo", json!({})); + assert_eq!(s.doc.to_json(), before, "the arc and its attachment are one undo step"); + run(&mut s, "edit.redo", json!({})); + run(&mut s, "parameters.change", json!({"name":dim["param"],"expression":format!("{} mm", 15.0 * scale)})); + + let sid = s.active_sketch.unwrap(); + // Saving and reopening preserves the ordinary point-on-curve constraint too. + let reopened = Session::new(solidcraft_doc::Document::from_json(&s.doc.to_json()).unwrap()); + for session in [&s, &reopened] { + let state = session.model.state(); + let ss = state.sketch(sid).unwrap(); + let sk = &ss.sketch; + let arc = sk.curve_index("arc").unwrap(); + assert!(ss.report.ok(), "{:?}", ss.report); + assert!(close(sk.radius(arc).unwrap(), 125.0 / 6.0), "{:?}", sk.radius(arc)); + let shape = sk.shape(arc).unwrap(); + for point in [Vec2::new(-20.0, 0.0), Vec2::new(20.0, 0.0), Vec2::new(0.0, 15.0)] { + assert!(shape.dist(point) < 1e-6, "{through}, reverse={reverse}: {point:?}"); + } + } + } + } +} + +#[test] +fn three_point_arc_coordinates_stay_free_and_invalid_references_are_atomic() { + let mut s = new_sketch(); + run(&mut s, "sketch.line", json!({"points":["origin",[0,10]],"ids":["height"]})); + let out = run(&mut s, "sketch.arc.three_point", json!({"start":[-20,0],"end":[20,0],"through":[0,10],"id":"arc","connect":false})); + assert!(ids(&out["created"]["constraints"]).is_empty(), "coordinates must not fix the arc or attach it to a nearby point"); + let before = s.doc.to_json(); + for through in [json!("missing.end"), json!({"ref":"missing.end"}), json!([null, 10]), json!("origin"), json!("mid:height")] { + // The midpoint snap creates a point before the collinear arc is refused. + assert!(s.execute("sketch.arc.three_point", &json!({"start":[0,-10],"end":[0,20],"through":through})).is_err(), "{through}"); + assert_eq!(s.doc.to_json(), before, "failed through point {through} changed the sketch"); + } +} + #[test] fn new_creation_tools() { let mut s = new_sketch(); diff --git a/crates/ui-egui/src/sketch_corner_ui_tests.rs b/crates/ui-egui/src/sketch_corner_ui_tests.rs index 25cac7d37..0df701c34 100644 --- a/crates/ui-egui/src/sketch_corner_ui_tests.rs +++ b/crates/ui-egui/src/sketch_corner_ui_tests.rs @@ -1,6 +1,32 @@ use super::*; use solidcraft_engine::sketch::CurveKind; +#[test] +fn three_point_arc_follows_its_snapped_through_point_after_dimension_change() { + let mut h = crate::scenario::Harness::new(); + h.app.run("sketch.create", json!({"plane":"XY"})).unwrap(); + h.app.run("sketch.line", json!({"points":[[-20,0],[20,0]],"ids":["base"]})).unwrap(); + h.app.run("sketch.constraint.fix", json!({"entity":"base"})).unwrap(); + h.app.run("sketch.line", json!({"points":["origin",[0,10]],"ids":["height"],"infer":true})).unwrap(); + let dim = h.app.run("sketch.dimension", json!({"entities":["height"],"value":10})).unwrap(); + h.settle(); + h.step(&json!({"start":"sketch.arc.three_point"})).unwrap(); + for point in [[-20, 0, 0], [20, 0, 0], [0, 10, 0]] { + h.step(&json!({"click":{"world":point}})).unwrap(); + } + h.app.run("parameters.change", json!({"name":dim["param"],"expression":"15 mm"})).unwrap(); + let sid = h.app.session.active_sketch.unwrap(); + let state = h.app.session.model.state(); + let ss = state.sketch(sid).unwrap(); + let sk = &ss.sketch; + let arc = sk.curves.iter().position(|c| matches!(c.kind, CurveKind::Arc { .. })).unwrap(); + let through = sk.point(sk.resolve_point("height.end").unwrap()).unwrap(); + assert!(ss.report.ok()); + assert!(through.dist(Vec2::new(0.0, 15.0)) < 1e-6, "{through:?}"); + let distance = sk.shape(arc).unwrap().dist(through); + assert!(distance < 1e-6, "the arc misses its snapped through point by {distance} mm"); +} + #[test] fn clicked_periodic_fit_closes_at_start_and_right_click_keeps_natural() { let mut h = crate::scenario::Harness::new(); diff --git a/crates/ui-egui/src/tools.rs b/crates/ui-egui/src/tools.rs index 773e9d074..c434a46df 100644 --- a/crates/ui-egui/src/tools.rs +++ b/crates/ui-egui/src/tools.rs @@ -398,7 +398,7 @@ fn run_shape(app: &mut SolveApp, t: &Tool) -> Result, String> { "sketch.circle.center" => json!({"center": arg(&p[0]), "radius": d(0, 1)}), "sketch.circle.two_point" => json!({"p0": xy(&p[0]), "p1": xy(&p[1])}), "sketch.circle.three_point" => json!({"p0": xy(&p[0]), "p1": xy(&p[1]), "p2": xy(&p[2])}), - "sketch.arc.three_point" => json!({"start": arg(&p[0]), "end": arg(&p[1]), "through": xy(&p[2])}), + "sketch.arc.three_point" => json!({"start": arg(&p[0]), "end": arg(&p[1]), "through": arg(&p[2])}), "sketch.arc.center_point" => match crate::sketch_dims::locked_value(app, t, "Sweep") { Some(sweep) => json!({"center": arg(&p[0]), "start": arg(&p[1]), "sweep": sweep.to_degrees()}), None => json!({"center": arg(&p[0]), "start": arg(&p[1]), "end": xy(&p[2])}),