From 2cc6615c2d46db85e146a2d5a74f003e61f10137 Mon Sep 17 00:00:00 2001 From: Ajmal Musthafa <52122158+trojancode@users.noreply.github.com> Date: Wed, 28 Oct 2026 15:57:06 +0000 Subject: [PATCH] Enhance two-tangent circle tool: add default radius handling and improve interaction hints (#8116) --- crates/ui-egui/src/sketch_corner_ui_tests.rs | 23 ++++++++-- crates/ui-egui/src/sketch_dims.rs | 9 ++++ crates/ui-egui/src/sketch_tools.rs | 45 ++++++++++++++++---- 3 files changed, 64 insertions(+), 13 deletions(-) diff --git a/crates/ui-egui/src/sketch_corner_ui_tests.rs b/crates/ui-egui/src/sketch_corner_ui_tests.rs index 1a17b4f95..bf006cec1 100644 --- a/crates/ui-egui/src/sketch_corner_ui_tests.rs +++ b/crates/ui-egui/src/sketch_corner_ui_tests.rs @@ -653,10 +653,10 @@ fn trim_hover_shows_the_piece_a_click_takes_away() { } /// #3141: the 2-tangent circle tool, clicked on two sides of a rectangle drawn away from the -/// origin, puts its circle in the corner between the clicks without moving the rectangle, and -/// fixed sides no longer make it fail. +/// origin, offers its default radius before it puts the circle in the corner between the clicks. +/// A typed radius reaches the command, neither path moves the rectangle, and fixed sides work. #[test] -fn two_tangent_circle_clicked_on_two_sides_fits_the_corner_and_moves_nothing() { +fn two_tangent_circle_takes_its_default_or_a_typed_radius_and_moves_nothing() { for fixed in [false, true] { let mut h = crate::scenario::Harness::new(); h.app.run("sketch.create", json!({"plane":"XY"})).unwrap(); @@ -673,10 +673,25 @@ fn two_tangent_circle_clicked_on_two_sides_fits_the_corner_and_moves_nothing() { h.step(&json!({"start":"sketch.circle.two_tangent"})).unwrap(); h.step(&json!({"click":{"world":[-550,0,0]}})).unwrap(); h.step(&json!({"click":{"world":[-800,250,0]}})).unwrap(); + let before_circle = h.app.session.model.state().sketch(sid).unwrap().sketch.clone(); + assert!(!before_circle.curves.iter().any(|c| matches!(c.kind, CurveKind::Circle { .. })), "the second pick waits for Radius"); + let tool = h.app.tool.as_ref().unwrap(); + assert_eq!(tool.dims.iter().map(|dimension| dimension.label).collect::>(), ["Radius"]); + let offered = tool.dims[0].value.parse::().unwrap_or_default(); + assert!((offered - 50.0).abs() < 1e-6, "the shorter 500 mm side offers 50 mm: {offered}"); + let radius = if fixed { + h.step(&json!({"key":"A","cmd":true})).unwrap(); + h.step(&json!({"text":"30"})).unwrap(); + 30.0 + } else { + 50.0 + }; + h.step(&json!({"key":"Enter"})).unwrap(); let sk = h.app.session.model.state().sketch(sid).unwrap().sketch.clone(); let circle = sk.curves.iter().position(|c| matches!(c.kind, CurveKind::Circle { .. })); let circle = circle.unwrap_or_else(|| panic!("fixed {fixed}: no circle: {:?}", h.app.status)); - assert!(sk.center(circle).unwrap().dist(Vec2::new(-750.0, 50.0)) < 1e-6, "fixed {fixed}: {:?}", sk.center(circle)); + assert!(sk.center(circle).unwrap().dist(Vec2::new(-800.0 + radius, radius)) < 1e-6, "fixed {fixed}: {:?}", sk.center(circle)); + assert!((sk.radius(circle).unwrap() - radius).abs() < 1e-6, "fixed {fixed}: {:?}", sk.radius(circle)); let after = points(&h); assert!(before.iter().zip(&after).all(|(a, b)| a.dist(*b) < 1e-9), "fixed {fixed}: the rectangle moved: {after:?}"); } diff --git a/crates/ui-egui/src/sketch_dims.rs b/crates/ui-egui/src/sketch_dims.rs index e26abefe7..f601faa84 100644 --- a/crates/ui-egui/src/sketch_dims.rs +++ b/crates/ui-egui/src/sketch_dims.rs @@ -45,6 +45,7 @@ pub fn boxes_for(cmd: &str, n: usize) -> Vec { ("sketch.circle.center", 1) => vec![b("Diameter", Length)], // The two points are a diameter's ends (#3278). ("sketch.circle.two_point", 1) => vec![b("Diameter", Length)], + ("sketch.circle.two_tangent", 2) => vec![b("Radius", Length)], ("sketch.arc.center_point" | "sketch.slot.arc_center", 1) => vec![b("Radius", Length)], ("sketch.arc.center_point" | "sketch.slot.arc_center", 2) => vec![b("Sweep", Angle)], ("sketch.polygon.inscribed" | "sketch.polygon.circumscribed", 1) => vec![b("Radius", Length), b("Sides", Unitless)], @@ -271,6 +272,9 @@ fn live(app: &SolveApp, t: &Tool, e: Vec2, px: f64) -> Vec<(f64, Vec2)> { } ("sketch.circle.center", 1) => vec![(2.0 * (e - a).len(), (a + e) * 0.5 + dir_or_x(e - a).perp() * off)], ("sketch.circle.two_point", 1) => vec![((e - a).len(), (a + e) * 0.5 + dir_or_x(e - a).perp() * off)], + ("sketch.circle.two_tangent", 2) => { + vec![(crate::sketch_tools::two_tangent_default_radius(app, t), (first + a) * 0.5 + dir_or_x(a - first).perp() * off)] + } ("sketch.arc.center_point" | "sketch.slot.arc_center", 1) | ("sketch.polygon.inscribed" | "sketch.polygon.circumscribed", 1) => { vec![((e - a).len(), (a + e) * 0.5), (6.0, a + (e - a) * 0.5 + (e - a).perp() * 0.3)] } @@ -428,6 +432,11 @@ pub fn locked_value(app: &SolveApp, t: &Tool, label: &str) -> Option { locked(app, t, label) } +/// The expression typed into a locked value box, kept verbatim for a command parameter. +pub(crate) fn locked_expression<'a>(t: &'a Tool, label: &str) -> Option<&'a str> { + t.dims.iter().chain(&t.dim_history).find(|dimension| dimension.label == label && dimension.locked).map(|dimension| dimension.value.as_str()) +} + /// Give the keyboard back to the box it was in, after a click in another panel took it (#2927). /// The box that owned the keyboard is still the one that had it last frame; the active tool must /// still show its boxes, so a finished or restarted shape is left alone. diff --git a/crates/ui-egui/src/sketch_tools.rs b/crates/ui-egui/src/sketch_tools.rs index e9155c5c6..c0f18bc01 100644 --- a/crates/ui-egui/src/sketch_tools.rs +++ b/crates/ui-egui/src/sketch_tools.rs @@ -657,7 +657,7 @@ pub fn hint(id: &str) -> Option { "sketch.edit_text" => "Edit Text: click a letter interior or outline", "sketch.blend_curve" => "Blend curve: click the ends of two curves", "sketch.constraint.curvature" => "Curvature: click two curves that share an end", - "sketch.circle.two_tangent" => "2-tangent circle: click two curves (the circle goes between the two clicks)", + "sketch.circle.two_tangent" => "2-tangent circle: click two curves, then enter the radius (the circle goes between the two clicks)", "sketch.circle.three_tangent" => "3-tangent circle: click three curves", "sketch.constrainer" => "Constrain: click one or two entities; right-click to apply", "sketch.constraint.polygon" => "Polygon: click the lines of a closed chain; right-click to apply", @@ -779,6 +779,14 @@ fn round_nice(x: f64) -> f64 { n * p } +/// The radius offered after the two curves of a tangent circle have been picked. +pub(crate) fn two_tangent_default_radius(app: &SolveApp, tool: &Tool) -> f64 { + match tool.picks.as_slice() { + [a, b] => round_nice(curve_len(app, a).min(curve_len(app, b)) * 0.1), + _ => 1.0, + } +} + /// A click with an extra tool. pub fn periodic_start_snap(app: &SolveApp, tool: &Tool, proj: &Proj, pos: Pos2) -> Option { if tool.cmd != "sketch.spline.fit_point" || tool.pts.len() < 3 || crate::drag_snap::suppressed() { @@ -970,13 +978,19 @@ pub fn on_click(app: &mut SolveApp, tool: &mut Tool, proj: &Proj, pos: Pos2) { let _ = app.run(cmd, json!({"curves": [id]})); return; } + // Once both tangent curves are picked, the Radius box owns the interaction until + // Enter commits it or Escape cancels it. A click behind the box must not replace or + // append another curve. + if cmd == "sketch.circle.two_tangent" && tool.picks.len() >= 2 { + return; + } if !tool.picks.contains(&id) { tool.picks.push(id); if let Some((p, _)) = at { tool.pts.push((p, None)); } } - if n > 0 && tool.picks.len() >= n { + if n > 0 && tool.picks.len() >= n && cmd != "sketch.circle.two_tangent" { run_entities(app, tool); tool.picks.clear(); tool.pts.clear(); @@ -1023,10 +1037,22 @@ pub fn on_click(app: &mut SolveApp, tool: &mut Tool, proj: &Proj, pos: Pos2) { } } -/// Put down the next point of a point tool: a click, or Enter in one of its value boxes. The -/// last point runs the command. Typed values are checked first, so a rejected one stays in its -/// box to be corrected rather than reaching the command. +/// Put down the next point of a point tool, or confirm an extra tool's value box with Enter. +/// Typed values are checked first, so a rejected one stays in its box to be corrected rather +/// than reaching the command. pub(crate) fn place_point(app: &mut SolveApp, tool: &mut Tool, p: (Vec2, Option)) { + if tool.cmd == "sketch.circle.two_tangent" && tool.picks.len() == 2 { + if let Err(error) = crate::sketch_dims::validate(app, tool) { + app.set_status(error, true); + return; + } + if run_entities(app, tool) { + tool.picks.clear(); + tool.pts.clear(); + crate::sketch_dims::reset(tool); + } + return; + } let Some(Mode::Points(n)) = mode(tool.cmd) else { return }; if let Err(error) = crate::sketch_dims::validate(app, tool) { app.set_status(error, true); @@ -1081,12 +1107,13 @@ fn run_points(app: &mut SolveApp, t: &Tool) -> bool { crate::tools::draw(app, t, params).is_ok() } -fn run_entities(app: &mut SolveApp, t: &Tool) { +fn run_entities(app: &mut SolveApp, t: &Tool) -> bool { let k = &t.picks; let params = match t.cmd { "sketch.constraint.curvature" => json!({"a": k[0], "b": k[1]}), "sketch.circle.two_tangent" => { - let r = round_nice(curve_len(app, &k[0]).min(curve_len(app, &k[1])) * 0.1); + let r = crate::sketch_dims::locked_expression(t, "Radius") + .map_or_else(|| json!(two_tangent_default_radius(app, t)), |expression| json!(expression)); // Between the two clicks: each lies on its curve, so the circle in the corner they // span is the one nearest their midpoint (#3141). let near = xy(t.pts.iter().fold(Vec2::ZERO, |a, p| a + p.0) / t.pts.len().max(1) as f64); @@ -1099,9 +1126,9 @@ fn run_entities(app: &mut SolveApp, t: &Tool) { } "sketch.constrainer" => json!({"entities": k}), "sketch.constraint.polygon" => json!({"lines": k}), - _ => return, + _ => return false, }; - let _ = app.run(t.cmd, params); + app.run(t.cmd, params).is_ok() } /// Right-click with an extra tool: finish a chain or move to the next stage. Returns false