diff --git a/crates/engine/src/cmd/mod.rs b/crates/engine/src/cmd/mod.rs index c76ed7bd8..6ea963eda 100644 --- a/crates/engine/src/cmd/mod.rs +++ b/crates/engine/src/cmd/mod.rs @@ -53,7 +53,7 @@ pub use sketch_construction::{ ConstructionLine, construction_line_choice, construction_line_mode, construction_line_solutions, construction_line_takes_length, construction_segment, }; -pub use sketch_create::tangent_arc_preview; +pub use sketch_create::{tangent_arc_preview, tangent_arc_starts_at}; pub use sketch_import::drawing_import_command; pub use sketch_modify::{extend_arc_preview, extend_preview, trim_piece}; pub use view_dxf::sketch::exportable as view_dxf_exportable; diff --git a/crates/engine/src/cmd/sketch_create.rs b/crates/engine/src/cmd/sketch_create.rs index de8101fa4..01db3b6c3 100644 --- a/crates/engine/src/cmd/sketch_create.rs +++ b/crates/engine/src/cmd/sketch_create.rs @@ -873,6 +873,27 @@ pub fn tangent_arc_preview(sk: &Sketch, curve: &str, start: &str, end: Vec2) -> Some(out) } +/// The picked point, then any other point at the same place: a trimmed arc's end is its own +/// point, lying on the end of the curve that trimmed it, which is the one a click picks (#1485). +fn tangent_start_candidates(sk: &Sketch, picked: usize) -> Vec { + let at = sk.point(picked).unwrap_or_default(); + let tol = 1e-6 * at.len().max(1.0); + std::iter::once(picked).chain((0..sk.points.len()).filter(|&q| q != picked && sk.point(q).is_some_and(|x| x.dist(at) <= tol))).collect() +} + +/// The first drawn curve (and which of the candidate points) a tangent arc can leave from. +fn tangent_start_curve(sk: &Sketch, candidates: &[usize]) -> Option<(usize, usize)> { + candidates + .iter() + .find_map(|&q| sk.curves.iter().enumerate().find(|(i, c)| !c.construction && leaving_dir(sk, *i, q).is_some()).map(|(i, _)| (i, q))) +} + +/// Whether `sketch.arc.tangent` can start at sketch point `start` (an id such as `l1.end`): the +/// end of a drawn line, arc or spline there. A tool asks before taking the click (#10653). +pub fn tangent_arc_starts_at(sk: &Sketch, start: &str) -> bool { + sk.resolve_point(start).is_some_and(|picked| tangent_start_curve(sk, &tangent_start_candidates(sk, picked)).is_some()) +} + fn arc_tangent(s: &mut Session, p: &Value) -> Result { let cmd = "sketch.arc.tangent"; // An id ends the arc on that point, so a chain can close; coordinates make a new point. @@ -887,14 +908,16 @@ fn arc_tangent(s: &mut Session, p: &Value) -> Result { (None, Some(at)) => (at, None), (None, None) => return Err(bad(cmd, "missing `end`")), }; + // A snapped place that is no point yet (`on:l3:…` on a line, `mid:l1` at its middle: a + // click between a line's ends) is no end to start from: said so, not as an unknown point + // (#10653). Point ids (`l1.end`, `p3`, `origin`) have no colon. + let not_an_end = || bad(cmd, "`start` must be the end point of a line or arc"); + if str_(p, "start").is_some_and(|start| start.contains(':')) { + return Err(not_an_end()); + } let st = req_parg(sk, doc, p, "start", cmd)?; - let picked = st.idx().ok_or_else(|| bad(cmd, "`start` must be the end point of a line or arc"))?; - // The picked point, then any other point at the same place: a trimmed arc's end is its own - // point, lying on the end of the curve that trimmed it, which is the one a click picks (#1485). - let at = st.pos(); - let tol = 1e-6 * at.len().max(1.0); - let candidates: Vec = - std::iter::once(picked).chain((0..sk.points.len()).filter(|&q| q != picked && sk.point(q).is_some_and(|x| x.dist(at) <= tol))).collect(); + let picked = st.idx().ok_or_else(not_an_end)?; + let candidates = tangent_start_candidates(sk, picked); let (ci, pi) = match str_(p, "curve") { Some(id) => { let ci = sk.curve_index(id).ok_or_else(|| bad(cmd, format!("unknown curve `{id}`")))?; @@ -905,10 +928,7 @@ fn arc_tangent(s: &mut Session, p: &Value) -> Result { .ok_or_else(|| bad(cmd, "`start` is not an end of that curve"))?; (ci, pi) } - None => candidates - .iter() - .find_map(|&q| sk.curves.iter().enumerate().find(|(i, c)| !c.construction && leaving_dir(sk, *i, q).is_some()).map(|(i, _)| (i, q))) - .ok_or_else(|| bad(cmd, "no line or arc ends at `start`"))?, + None => tangent_start_curve(sk, &candidates).ok_or_else(|| bad(cmd, "no line or arc ends at `start`"))?, }; let t = leaving_dir(sk, ci, pi).ok_or_else(|| bad(cmd, "`start` is not an end of that curve"))?; let s0 = st.pos(); diff --git a/crates/engine/src/cmd/sketch_modify_tests.rs b/crates/engine/src/cmd/sketch_modify_tests.rs index ea69228da..8ad0a94af 100644 --- a/crates/engine/src/cmd/sketch_modify_tests.rs +++ b/crates/engine/src/cmd/sketch_modify_tests.rs @@ -4140,6 +4140,27 @@ fn corner_tools_refuse_all_spatial_curves_with_an_unsupported_message() { } } +/// #10653: a tangent arc started on a line between its ends (`on:l1:…`, which a click there +/// gives) is refused as no end to start from, not as an unknown point; `tangent_arc_starts_at` +/// tells a tool which points it can start at. +#[test] +fn a_tangent_arc_starts_only_at_a_curves_end() { + let mut s = new_sketch(); + let line = ids(&run(&mut s, "sketch.line", json!({"points": [[5, 0], [15, 0]], "connect": false, "infer": false}))["curves"])[0].clone(); + let before = s.doc.to_json(); + let on = format!("on:{line}:10,0"); + for start in [on.clone(), format!("mid:{line}")] { + let err = s.execute("sketch.arc.tangent", &json!({"start": start, "end": [20, 10]})).map(|v| v.to_string()).unwrap_err().to_string(); + assert!(err.contains("`start` must be the end point of a line or arc") && !err.contains("unknown sketch point"), "{start}: {err}"); + assert_eq!(s.doc.to_json(), before); + } + let sk = sketch(&s); + assert!(crate::cmd::tangent_arc_starts_at(&sk, &format!("{line}.end"))); + assert!(crate::cmd::tangent_arc_starts_at(&sk, &format!("{line}.start"))); + assert!(!crate::cmd::tangent_arc_starts_at(&sk, &on)); + assert!(!crate::cmd::tangent_arc_starts_at(&sk, "origin"), "no curve ends at the origin here"); +} + /// `sketch.arc.tangent` ends on an existing point when `end` names it, so a chain can close, and /// makes a new point for coordinates as before (#9489). The preview is the arc the command draws. #[test] diff --git a/crates/ui-egui/src/sketch_moving_click_tests.rs b/crates/ui-egui/src/sketch_moving_click_tests.rs index f0bef1582..ba5e3919c 100644 --- a/crates/ui-egui/src/sketch_moving_click_tests.rs +++ b/crates/ui-egui/src/sketch_moving_click_tests.rs @@ -197,6 +197,26 @@ fn chain_with_a_line(on: bool) -> Harness { h } +/// #10653: Tangent Arc's first click on a line between its ends is refused at once, with the +/// tool's hint as the error, and picks nothing; a click on the line's end starts the arc. +#[test] +fn a_tangent_arc_first_click_off_a_curves_end_is_refused_at_once() { + let mut h = Harness::new(); + h.app.run("sketch.create", json!({"plane":"XY"})).unwrap(); + h.app.run("sketch.line", json!({"points":[[0,0],[20,0]],"connect":false,"infer":false})).unwrap(); + h.app.cam.distance = 120.0; + h.settle(); + h.step(&json!({"start":"sketch.arc.tangent"})).unwrap(); + click_at(&mut h, [10.0, 0.0]); + let refused = h.app.status.as_ref().is_some_and(|s| s.2 && s.0.contains("click the end of a line or arc")); + assert!(refused, "{:?}", h.app.status); + assert_eq!(h.app.tool.as_ref().map(|t| t.pts.len()), Some(0), "nothing picked"); + click_at(&mut h, [20.0, 0.0]); + click_at(&mut h, [30.0, 10.0]); + assert_eq!(curves(&h), 2, "{:?}", h.app.status); + assert!(sketch(&h).curves.iter().any(|c| matches!(c.kind, CurveKind::Arc { .. })), "the arc from the line's end"); +} + fn click_at(h: &mut Harness, at: [f64; 2]) { h.step(&json!({"move":{"sketch":at}})).unwrap(); h.frames(2); diff --git a/crates/ui-egui/src/sketch_tools.rs b/crates/ui-egui/src/sketch_tools.rs index b8378aba5..330d283c4 100644 --- a/crates/ui-egui/src/sketch_tools.rs +++ b/crates/ui-egui/src/sketch_tools.rs @@ -1258,6 +1258,13 @@ pub(crate) fn place_point(app: &mut SolveApp, tool: &mut Tool, p: (Vec2, Option< return; } let p = crate::sketch_dims::placed(app, tool, &p); + // Tangent Arc starts at the end of a line, arc or spline. A first click anywhere else (on a + // line between its ends, in empty space) is refused now, saying where to click, instead of + // failing at the second click on a point the command can't take (#10653). + if tool.cmd == "sketch.arc.tangent" && tool.pts.is_empty() && !tangent_start(app, &p) { + app.set_status(hint(tool.cmd).unwrap_or_default(), true); + return; + } tool.pts.push(p); if n > 0 && tool.pts.len() >= n { if run_points(app, tool) { @@ -1269,6 +1276,13 @@ pub(crate) fn place_point(app: &mut SolveApp, tool: &mut Tool, p: (Vec2, Option< } } +/// Whether a click picked a sketch point a tangent arc can start at. +fn tangent_start(app: &SolveApp, p: &(Vec2, Option)) -> bool { + let Some(id) = &p.1 else { return false }; + let state = app.session.world_state(); + app.session.active_sketch.and_then(|sid| state.sketch(sid)).is_some_and(|ss| solidcraft_engine::cmd::tangent_arc_starts_at(&ss.sketch, id)) +} + fn run_points(app: &mut SolveApp, t: &Tool) -> bool { let p = &t.pts; let width = |a: Vec2, b: Vec2, c: Vec2| -> f64 { 2.0 * (b - a).normalized().map(|d| d.cross(c - a).abs()).unwrap_or(1.0) };