diff --git a/crates/engine/src/cmd/mod.rs b/crates/engine/src/cmd/mod.rs index f9673ecf3..864bf7827 100644 --- a/crates/engine/src/cmd/mod.rs +++ b/crates/engine/src/cmd/mod.rs @@ -36,6 +36,7 @@ mod face; mod features; mod features_more; pub use features::{auto_operation, extrude_reach}; +pub use sketch_modify::trim_piece; mod component_link; mod config; mod file; diff --git a/crates/engine/src/cmd/sketch_modify.rs b/crates/engine/src/cmd/sketch_modify.rs index 6cc0d8dad..2b57abd26 100644 --- a/crates/engine/src/cmd/sketch_modify.rs +++ b/crates/engine/src/cmd/sketch_modify.rs @@ -307,6 +307,37 @@ fn bracket(sk: &Sketch, ci: usize, tq: f64) -> (Vec<(f64, Vec2, usize)>, bool) { (before.into_iter().chain(after).collect(), false) } +/// The piece of `curve` a trim at `at` takes away, as points along it, for the Trim tool to show +/// under the cursor (#3052): the part between the crossings either side of `at`, as `sketch.trim` +/// cuts it, or the whole curve where nothing crosses it. `None` where trim works otherwise (a +/// free-form or infinite curve) or the curve isn't in the sketch: the tool shows the curve. +pub fn trim_piece(sk: &Sketch, curve: &str, at: Vec2) -> Option> { + let ci = sk.curve_index(curve)?; + let c = sk.curves.get(ci)?; + if c.infinite || c.kind.is_freeform() { + return None; + } + let sh = sk.shape(ci)?; + let tq = sh.param(sh.project(at)); + let (t0, t1) = sh.range(); + let (cuts, full) = bracket(sk, ci, tq); + let (a, b) = match (cuts.as_slice(), full) { + ([], _) => (t0, t1), + // A whole circle: the arc between the two crossings round `at`. + ([x, y], true) => { + if tq > x.0 && tq < y.0 { + (x.0, y.0) + } else { + (y.0, x.0 + TAU) + } + } + (_, true) => return None, + (cuts, false) => (cuts.iter().rev().find(|c| c.0 < tq).map_or(t0, |c| c.0), cuts.iter().find(|c| c.0 > tq).map_or(t1, |c| c.0)), + }; + let n = if matches!(sh, Shape::Line { .. }) { 1 } else { 64 }; + Some((0..=n).map(|i| sh.point(a + (b - a) * f64::from(i) / f64::from(n))).collect()) +} + fn trim(s: &mut Session, p: &Value) -> Result { let cmd = "sketch.trim"; let at = req_vec2(cmd, p, "at")?; diff --git a/crates/engine/src/cmd/sketch_modify_tests.rs b/crates/engine/src/cmd/sketch_modify_tests.rs index 846cd32bd..a560d7f0c 100644 --- a/crates/engine/src/cmd/sketch_modify_tests.rs +++ b/crates/engine/src/cmd/sketch_modify_tests.rs @@ -1619,3 +1619,46 @@ fn trim_keeps_the_rest_of_a_dimensioned_line_where_it_was() { assert!(corner(&s).dist(Vec2::new(40.0, 23.0)) < 1e-6, "the step moved to {:?}", corner(&s)); assert!(forty(&s).is_empty(), "{:?}", forty(&s)); } + +/// #3052: the piece Trim shows under the cursor is the one `sketch.trim` takes away there: its +/// ends lie on the crossings either side of the cursor (or the curve's own end), and after the +/// trim no curve runs through its middle any more. A line crossed twice, its middle and an end +/// piece; a circle crossed by a line, the half round the cursor; a line nothing crosses, whole. +#[test] +fn trim_piece_is_what_a_trim_there_takes_away() { + let near = |a: Vec2, b: [f64; 2]| (a - Vec2::new(b[0], b[1])).len() < 1e-6; + let crossed_line = || { + let mut s = new_sketch(); + let line = ids(&run(&mut s, "sketch.line", json!({"points": [[0,0], [30,0]], "connect": false}))["curves"])[0].clone(); + run(&mut s, "sketch.line", json!({"points": [[10,-5], [10,5]], "connect": false})); + run(&mut s, "sketch.line", json!({"points": [[20,-5], [20,5]], "connect": false})); + (s, line) + }; + let crossed_circle = || { + let mut s = new_sketch(); + let circle = ids(&run(&mut s, "sketch.circle.center", json!({"center": [0,0], "radius": 10}))["curves"])[0].clone(); + run(&mut s, "sketch.line", json!({"points": [[-20,0], [20,0]], "connect": false})); + (s, circle) + }; + let cases: [(&dyn Fn() -> (Session, String), [f64; 2], [[f64; 2]; 2], [f64; 2]); 4] = [ + (&crossed_line, [15.0, 0.0], [[10.0, 0.0], [20.0, 0.0]], [15.0, 0.0]), + (&crossed_line, [4.0, 0.0], [[0.0, 0.0], [10.0, 0.0]], [5.0, 0.0]), + (&crossed_circle, [0.0, 10.0], [[10.0, 0.0], [-10.0, 0.0]], [0.0, 10.0]), + (&crossed_circle, [0.0, -10.0], [[10.0, 0.0], [-10.0, 0.0]], [0.0, -10.0]), + ]; + for (make, at, ends, inside) in cases { + let (mut s, curve) = make(); + let piece = super::trim_piece(&sketch(&s), &curve, Vec2::new(at[0], at[1])).unwrap(); + let (first, last) = (piece[0], piece[piece.len() - 1]); + assert!((near(first, ends[0]) && near(last, ends[1])) || (near(first, ends[1]) && near(last, ends[0])), "{at:?}: {first:?} {last:?}"); + let mid = if piece.len() > 2 { piece[piece.len() / 2] } else { (first + last) * 0.5 }; + assert!(near(mid, inside), "{at:?}: {mid:?}"); + run(&mut s, "sketch.trim", json!({"curve": curve, "at": at})); + let sk = sketch(&s); + assert!(!(0..sk.curves.len()).filter_map(|i| sk.shape(i)).any(|sh| sh.dist(mid) < 1e-6), "the trim at {at:?} took the piece shown away"); + } + let mut s = new_sketch(); + let lone = ids(&run(&mut s, "sketch.line", json!({"points": [[0,0], [8,0]], "connect": false}))["curves"])[0].clone(); + let piece = super::trim_piece(&sketch(&s), &lone, Vec2::new(3.0, 0.0)).unwrap(); + assert!(near(piece[0], [0.0, 0.0]) && near(piece[piece.len() - 1], [8.0, 0.0]), "{piece:?}"); +} diff --git a/crates/ui-egui/src/sketch_corner_ui_tests.rs b/crates/ui-egui/src/sketch_corner_ui_tests.rs index 6b75068c9..25cac7d37 100644 --- a/crates/ui-egui/src/sketch_corner_ui_tests.rs +++ b/crates/ui-egui/src/sketch_corner_ui_tests.rs @@ -595,3 +595,28 @@ fn fillet_chamfer_corner_preview_draws_curves() { let sk = h.app.session.world_state().sketch(sid).unwrap().sketch.clone(); assert_eq!(sk.curves.len(), 2, "chamfer preview must not modify the sketch"); } + +/// #3052: with Trim, the hover over a line crossed by two others is the piece between them, the +/// one a click takes away; the whole line is no longer shown hovered. With another tool, nothing. +#[test] +fn trim_hover_shows_the_piece_a_click_takes_away() { + let mut h = crate::scenario::Harness::new(); + h.app.run("sketch.create", json!({"plane":"XY"})).unwrap(); + let line = h.app.run("sketch.line", json!({"points": [[0,0],[30,0]], "connect": false})).unwrap()["curves"][0].as_str().unwrap().to_string(); + h.app.run("sketch.line", json!({"points": [[10,-5],[10,5]], "connect": false})).unwrap(); + h.app.run("sketch.line", json!({"points": [[20,-5],[20,5]], "connect": false})).unwrap(); + h.settle(); + let sid = h.app.session.active_sketch.unwrap(); + h.step(&json!({"start":"sketch.trim"})).unwrap(); + h.step(&json!({"move":{"world":[15,0,0]}})).unwrap(); + assert!(matches!(&h.app.viewport.hover, Some(crate::viewport::Hit::SketchCurve { id, .. }) if *id == line), "{:?}", h.app.viewport.hover); + let piece = trim_hover_piece(&h.app, sid, &line).unwrap(); + let ends = [piece[0], piece[piece.len() - 1]]; + assert!( + ends.iter().all(|p| p.y.abs() < 1e-6) && ends.iter().any(|p| (p.x - 10.0).abs() < 1e-6) && ends.iter().any(|p| (p.x - 20.0).abs() < 1e-6), + "{ends:?}" + ); + h.step(&json!({"key":"Escape"})).unwrap(); + h.step(&json!({"move":{"world":[15,0,0]}})).unwrap(); + assert!(trim_hover_piece(&h.app, sid, &line).is_none(), "only Trim shows a piece"); +} diff --git a/crates/ui-egui/src/sketch_tools.rs b/crates/ui-egui/src/sketch_tools.rs index 5f05fb120..1e9b987df 100644 --- a/crates/ui-egui/src/sketch_tools.rs +++ b/crates/ui-egui/src/sketch_tools.rs @@ -999,6 +999,33 @@ pub fn finish(app: &mut SolveApp, tool: &mut Tool) -> bool { } } +/// The cursor's Trim piece, keyed by design revision, sketch, curve and cursor: the viewport's +/// hover and the preview both ask for it each frame, and it intersects the curve with every other. +type TrimPieceKey = (u64, u64, String, [u64; 2]); + +thread_local! { + static TRIM_PIECE: std::cell::RefCell>)>> = const { std::cell::RefCell::new(None) }; +} + +/// With Trim active and the cursor over curve `id` of `sketch` (the active sketch): the piece a +/// click takes away (#3052), which the hover shows in place of the whole curve. `None` with +/// another tool, or for a curve trim cuts otherwise (`solidcraft_engine::cmd::trim_piece`). +pub(crate) fn trim_hover_piece(app: &SolveApp, sketch: u64, id: &str) -> Option> { + let tool = app.tool.as_ref().filter(|t| t.cmd == "sketch.trim")?; + let (at, _) = tool.hover.as_ref()?; + if active_sketch(app) != Some(sketch) { + return None; + } + let key = (app.session.revision, sketch, id.to_string(), [at.x.to_bits(), at.y.to_bits()]); + if let Some(piece) = TRIM_PIECE.with(|c| c.borrow().as_ref().filter(|(k, _)| *k == key).map(|(_, p)| p.clone())) { + return piece; + } + let st = app.session.world_state(); + let piece = solidcraft_engine::cmd::trim_piece(&st.sketch(sketch)?.sketch, id, *at); + TRIM_PIECE.with(|c| *c.borrow_mut() = Some((key, piece.clone()))); + piece +} + /// Rubber band for point tools and pick markers. pub fn preview(app: &SolveApp, t: &Tool, painter: &egui::Painter, proj: &Proj) { let Some(sid) = active_sketch(app) else { return }; @@ -1006,6 +1033,13 @@ pub fn preview(app: &SolveApp, t: &Tool, painter: &egui::Painter, proj: &Proj) { let Some(ss) = st.sketch(sid) else { return }; let to = |p: Vec2| proj.to_screen(ss.plane.to_world(p)); let tk = crate::theme::Tokens::get(); + if let Some(crate::viewport::Hit::SketchCurve { sketch, id, .. }) = &app.viewport.hover + && let Some(piece) = trim_hover_piece(app, *sketch, id) + { + let world: Vec<_> = piece.iter().map(|p| ss.plane.to_world(*p)).collect(); + // In the error colour, thicker than a hover: it is what the click deletes. + crate::tools::paint_sketch_path(&world, 0, painter, proj, Stroke::new(4.5, tk.error)); + } let stroke = Stroke::new(1.5, tk.rubber_band); if let Some((hp, snap)) = &t.hover && let Some(sp) = to(*hp) diff --git a/crates/ui-egui/src/viewport.rs b/crates/ui-egui/src/viewport.rs index 8e3264ba4..22661bd6d 100644 --- a/crates/ui-egui/src/viewport.rs +++ b/crates/ui-egui/src/viewport.rs @@ -3172,6 +3172,8 @@ fn hover_highlight(app: &SolveApp, painter: &egui::Painter, proj: &Proj) { let col = Tokens::get().hover_profile_edge; let st = app.session.world_state(); match &app.viewport.hover { + // Trim shows the piece it would take away instead (`sketch_tools::preview`, #3052). + Some(Hit::SketchCurve { sketch, id, .. }) if crate::sketch_tools::trim_hover_piece(app, *sketch, id).is_some() => {} Some(Hit::SketchCurve { sketch, id, .. }) => { if let Some(ss) = st.sketch(*sketch) && let Some(ci) = ss.sketch.curve_index(id)