Skip to content

SolidCraft: an open-source parametric 3D CAD application built in pure Rust. Desktop, web, and automation through MCP.

Sketch: a click picks the sketch's own curve over a projected one under it (#5257) (#5260)

Kai Evans5fbe058051b8
diff --git a/crates/ui-egui/src/select_other_tests.rs b/crates/ui-egui/src/select_other_tests.rs
index 47a22bfaa7..f722456cbb 100644
--- a/crates/ui-egui/src/select_other_tests.rs
+++ b/crates/ui-egui/src/select_other_tests.rs
@@ -1,4 +1,5 @@
-//! Select Other: press and hold, the viewport menu's entry, click cycling and dialog inputs.
+//! Select Other: press and hold, the viewport menu's entry, click cycling and dialog inputs; and
+//! which of overlapping sketch curves a click picks.
use serde_json::{Value, json};
use solidcraft_engine::Sel;
@@ -20,6 +21,47 @@ fn two_lines() -> (Harness, Value) {
(h, at)
}
+/// Two short lines of the sketch's own (`a` on x 5–15, `b` on x 25–35) drawn along a projected
+/// line (x 0–40, y 0, a box edge below the sketch plane): the ids of `a`, `b` and the projected
+/// one, and screen points on `a`, on `b` and on the projected line alone.
+struct OnProjected {
+ h: Harness,
+ a: String,
+ b: String,
+ projected: String,
+ at_a: Value,
+ at_b: Value,
+ at_projected: Value,
+}
+
+fn own_lines_on_a_projected_line() -> OnProjected {
+ let mut h = Harness::new();
+ h.app.set_origin_shown(false);
+ h.app.run("solid.box", json!({"length":40,"width":10,"height":10,"corner":[0,0,-20]})).unwrap();
+ h.app.run("sketch.create", json!({"plane":"XY"})).unwrap();
+ h.app.run("sketch.project", json!({"refs":[{"edge":[20,0,-10]}]})).unwrap();
+ for (x0, x1) in [(5, 15), (25, 35)] {
+ h.app.run("sketch.line", json!({"points":[[x0,0],[x1,0]],"infer":false,"connect":false})).unwrap();
+ }
+ h.app.cam.distance = 100.0;
+ h.settle();
+ let sk = h.app.session.doc.sketch(h.app.session.active_sketch.unwrap()).unwrap();
+ let projected: Vec<String> = sk.curves.iter().filter(|c| c.link.is_some()).map(|c| c.id.clone()).collect();
+ let own: Vec<String> = sk.curves.iter().filter(|c| c.link.is_none()).map(|c| c.id.clone()).collect();
+ assert_eq!((projected.len(), own.len()), (1, 2), "{:?}", sk.curves);
+ let at = |h: &mut Harness, x: f64| h.call("ui.at", json!({"sketch":[x,0]}))["result"].clone();
+ // Away from the midpoints (10, 30 and 20), which are picks of their own.
+ let (at_a, at_b, at_projected) = (at(&mut h, 7.0), at(&mut h, 27.0), at(&mut h, 2.0));
+ OnProjected { h, a: own[0].clone(), b: own[1].clone(), projected: projected[0].clone(), at_a, at_b, at_projected }
+}
+
+fn hovered_curve(h: &Harness) -> Option<String> {
+ match &h.app.viewport.hover {
+ Some(crate::viewport::Hit::SketchCurve { id, .. }) => Some(id.clone()),
+ _ => None,
+ }
+}
+
fn labels(h: &Harness) -> Vec<String> {
crate::select_other::describe(&h.app)["rows"].as_array().into_iter().flatten().filter_map(|r| r["label"].as_str().map(String::from)).collect()
}
@@ -308,3 +350,27 @@ fn select_other_opened_with_shift_adds_its_choice_to_the_selection() {
h.step(&json!({"click": {"handle": format!("select_other:{i}")}})).unwrap();
assert_eq!(sorted(curves(&h)), ["l1", "l2"]);
}
+
+#[test]
+fn a_click_picks_the_sketchs_own_curve_over_a_projected_one_under_it() {
+ let OnProjected { mut h, a, b, projected, at_a, at_b, at_projected } = own_lines_on_a_projected_line();
+ h.step(&json!({"move": at_a})).unwrap();
+ assert_eq!(hovered_curve(&h).as_ref(), Some(&a), "the hover shows what a click picks");
+ h.step(&json!({"click": at_a})).unwrap();
+ assert_eq!(curves(&h), std::slice::from_ref(&a));
+ h.frames(30);
+ h.step(&json!({"click": at_b, "shift": true})).unwrap();
+ assert_eq!(sorted(curves(&h)), sorted(vec![a.clone(), b.clone()]));
+ // Where nothing of the sketch's own is, the projected line is picked.
+ h.frames(30);
+ h.step(&json!({"move": at_projected})).unwrap();
+ assert_eq!(hovered_curve(&h).as_ref(), Some(&projected));
+ h.step(&json!({"click": at_projected})).unwrap();
+ assert_eq!(curves(&h), std::slice::from_ref(&projected));
+ // Select Other lists the sketch's own line first, and the projected one under it.
+ h.frames(30);
+ h.step(&json!({"hold": at_a})).unwrap();
+ assert!(row_of(&h, &a) < row_of(&h, &projected), "{:?}", labels(&h));
+ h.step(&json!({"click": {"handle": format!("select_other:{}", row_of(&h, &projected))}})).unwrap();
+ assert_eq!(curves(&h), [projected]);
+}
diff --git a/crates/ui-egui/src/viewport.rs b/crates/ui-egui/src/viewport.rs
index e5471e2699..4e614fa191 100644
--- a/crates/ui-egui/src/viewport.rs
+++ b/crates/ui-egui/src/viewport.rs
@@ -1692,22 +1692,26 @@ fn pick_hits(app: &SolveApp, proj: &Proj, pos: Pos2, other_sketch_points: bool,
}
}
hits.extend(best.hits());
- // Sketch curves (the active sketch wins ties).
+ // Sketch curves: the active sketch's own before projected and other sketches' ones, then
+ // nearest (the active sketch wins ties).
let mut bestc = Near::new(all);
for sid in visible_sketches(app) {
let Some(ss) = st.sketch(sid) else { continue };
- let bonus = if s.active_sketch == Some(sid) { 2.0 } else { 0.0 };
+ let active = s.active_sketch == Some(sid);
+ let bonus = if active { 2.0 } else { 0.0 };
+ let tier = |own: bool| u8::from(!(active && own));
for (i, c) in ss.sketch.curves.iter().enumerate() {
if !solidcraft_engine::view::sketch_curve_visible(&ss.sketch, i) {
continue;
}
let straight = matches!(c.kind, CurveKind::Line { .. });
+ let tier = tier(c.link.is_none() || ss.sketch.is_text_curve(i));
for part in &crate::sketch_display::curve_sample(app, sid, i, &ss.plane, proj).parts {
let pts: Vec<Pos2> = part.iter().filter_map(|q| proj.to_screen(*q)).collect();
for w in pts.windows(2) {
let dd = seg_dist(pos, w[0], w[1]) - bonus;
- if dd < 6.0 && bestc.wants(dd) {
- bestc.offer(dd, Hit::SketchCurve { sketch: sid, id: c.id.clone(), straight });
+ if dd < 6.0 && bestc.wants((tier, dd)) {
+ bestc.offer((tier, dd), Hit::SketchCurve { sketch: sid, id: c.id.clone(), straight });
}
}
}
@@ -1715,11 +1719,12 @@ fn pick_hits(app: &SolveApp, proj: &Proj, pos: Pos2, other_sketch_points: bool,
// 3D sketch curves (paths for sweeps and pipes).
for w in &ss.sketch.wires {
let straight = w.fit.len() == 2;
+ let tier = tier(w.link.is_none());
let pts: Vec<Pos2> = w.pts.iter().filter_map(|q| proj.to_screen(*q)).collect();
for seg in pts.windows(2) {
let dd = seg_dist(pos, seg[0], seg[1]) - bonus;
- if dd < 6.0 && bestc.wants(dd) {
- bestc.offer(dd, Hit::SketchCurve { sketch: sid, id: w.id.clone(), straight });
+ if dd < 6.0 && bestc.wants((tier, dd)) {
+ bestc.offer((tier, dd), Hit::SketchCurve { sketch: sid, id: w.id.clone(), straight });
}
}
}