From 55a82d66fbe5df66b50345a65f60108815d93614 Mon Sep 17 00:00:00 2001 From: Frank <58932224+plexi32@users.noreply.github.com> Date: Wed, 11 Nov 2026 11:42:18 +0100 Subject: [PATCH 1/2] Snap points for joints (#11227) Co-Authored-By: Claude Opus 5.5 --- crates/ui-egui/src/dialogs_assembly.rs | 34 +++++++- crates/ui-egui/src/dialogs_assembly_tests.rs | 87 ++++++++++++++++++++ crates/ui-egui/src/viewport.rs | 24 +++++- 3 files changed, 139 insertions(+), 6 deletions(-) diff --git a/crates/ui-egui/src/dialogs_assembly.rs b/crates/ui-egui/src/dialogs_assembly.rs index d0bd738b329..26626f6dea1 100644 --- a/crates/ui-egui/src/dialogs_assembly.rs +++ b/crates/ui-egui/src/dialogs_assembly.rs @@ -80,7 +80,8 @@ pub(crate) fn joint_choice(name: &str, kind: JointKind) -> String { /// What a joint origin input accepts: snaps on faces, edges and vertices, the world origin, a /// sketch point (so a joint can be placed on the design origin or a hole centre, like Fusion's), /// and the design's planes, axes and construction points. -pub const SNAPS: crate::selection::Accept = FACES | EDGES | VERTICES | ORIGIN | POINTS | PLANES | AXES | CONSTRUCTION_POINTS; +pub const SNAPS: crate::selection::Accept = + FACES | EDGES | VERTICES | ORIGIN | POINTS | PLANES | AXES | CONSTRUCTION_POINTS | crate::selection::SNAP_POINTS; /// Commands whose effect is where occurrences sit (previewed on the placed model). const MOVES: &[&str] = &[ "joint.create", @@ -948,6 +949,35 @@ pub fn snap_of(s: &Session, sel: &Sel) -> Option { Some(snap) } +/// A woken snap point picked in a joint origin input (#11227) stands for the edge or face that +/// woke it when that item's own snap is there (a hole's centre, an edge's middle, a face's +/// centre): the joint keeps it by name, as a pick of the item itself would. Any other woken point +/// (an arc's end, a cylinder's far end) stays a point. +/// `snaps`: whether the dialog's active input takes joint snaps ([`takes_snaps`]). +pub(crate) fn woken_pick(app: &SolveApp, snaps: bool, sel: Sel) -> Sel { + let Sel::Vertex { body, point } = &sel else { return sel }; + let tol = 1e-4 * (1.0 + point.len()); + let woken = &app.viewport.woken; + // Only the woken item's own body: another body's point there is that body's. + let of_body = |i: &&Sel| matches!(i, Sel::Edge { body: b, .. } | Sel::Face { body: b, .. } if b == body); + if !snaps || !woken.points.iter().any(|p| p.at.dist(*point) <= tol) { + return sel; + } + // The rim's item, or a face of its body crossed on the way to its centre. + [&woken.item, &woken.face] + .into_iter() + .flatten() + .filter(of_body) + .find(|item| picked_snap(app, item).is_some_and(|s| s.at.dist(*point) <= tol)) + .cloned() + .unwrap_or(sel) +} + +/// Whether a dialog's active input takes joint snaps (a joint's origin, a trace's point). +pub(crate) fn takes_snaps(d: &Dialog) -> bool { + d.active_input().is_some_and(|i| i.accept == SNAPS) +} + /// Each pick's snap, measured once for the world state, design and open sketch it was measured in. #[derive(Default)] pub(crate) struct SnapMemo(std::sync::Mutex); @@ -2018,7 +2048,7 @@ pub fn overlay(app: &SolveApp, painter: &egui::Painter, proj: &Proj, d: &mut Dia } // The snap under the cursor (found again only when the hovered item changes). let accepting = d.inputs.get(d.active).is_some_and(|i| i.accept == SNAPS); - let under = app.viewport.hover.as_ref().and_then(crate::viewport::hit_sel).filter(|_| accepting); + let under = app.viewport.hover.as_ref().and_then(crate::viewport::hit_sel).filter(|_| accepting).map(|s| woken_pick(app, accepting, s)); match under { Some(sel) => { let fresh = !matches!(hover, Some((s, _)) if *s == sel); diff --git a/crates/ui-egui/src/dialogs_assembly_tests.rs b/crates/ui-egui/src/dialogs_assembly_tests.rs index 61a038ae55f..0b428c3179d 100644 --- a/crates/ui-egui/src/dialogs_assembly_tests.rs +++ b/crates/ui-egui/src/dialogs_assembly_tests.rs @@ -1538,6 +1538,93 @@ fn interference_checks_off_the_window_thread_and_drops_a_check_of_a_changed_desi assert_eq!(state(&app), (None, 0, false), "a check of the design before the change is dropped"); } +/// The Joint dialog over two boxes, Component 1 taking a pick, in the harness (a real viewport). +fn joint_picking_component_1() -> crate::scenario::Harness { + let mut h = crate::scenario::Harness::new(); + h.app = two_boxes(); + h.app.cam.distance = 160.0; + h.settle(); + h.app.start("joint.create"); + h +} + +/// Hovering an edge wakes all its snap points in a joint input (#11227); its middle, clicked from +/// past the edge's own reach, is the edge as a joint keeps it: the same origin a pick of the edge +/// makes. +#[test] +fn a_woken_middle_picks_the_edge_a_joint_keeps() { + let mut h = joint_picking_component_1(); + h.step(&json!({ "move": { "world": [15, 0, 10] } })).unwrap(); + let middle = Vec3::new(10.0, 0.0, 10.0); + let woken: Vec = h.app.viewport.woken.points.iter().map(|p| p.at).collect(); + for p in [middle, Vec3::new(0.0, 0.0, 10.0), Vec3::new(20.0, 0.0, 10.0)] { + assert!(woken.iter().any(|w| w.dist(p) < 1e-6), "{p:?} in {woken:?}"); + } + let at = h.call("ui.at", json!({ "world": [10, 0, 10] }))["result"].clone(); + let below = json!([at[0].as_f64().unwrap(), at[1].as_f64().unwrap() + 12.0]); + h.step(&json!({ "click": below })).unwrap(); + let d = h.app.dialog.as_ref().unwrap(); + let picked = d.inputs[0].items.clone(); + assert!(matches!(picked.as_slice(), [Sel::Edge { body, .. }] if body == "Body1"), "{picked:?}"); + let edge = edge_sel(&h.app.session, middle).unwrap(); + let (got, want) = (snap_of(&h.app.session, &picked[0]).unwrap(), snap_of(&h.app.session, &edge).unwrap()); + assert_eq!(got.kind, SnapKind::EdgeMid); + assert_eq!(got.param, want.param, "stored as an edge pick is"); +} + +/// A woken point that is not its item's own snap (a straight edge's end) stays a point, and a +/// face's centre is the face; with no woken item a vertex is a vertex, as before. +#[test] +fn woken_points_stand_for_their_item_only_where_its_snap_is() { + let mut app = two_boxes(); + app.start("joint.create"); + let edge = edge_sel(&app.session, Vec3::new(10.0, 0.0, 10.0)).unwrap(); + let corner = Sel::Vertex { body: "Body1".into(), point: Vec3::new(0.0, 0.0, 10.0) }; + assert_eq!(woken_pick(&app, true, corner.clone()), corner, "nothing woken"); + app.viewport.woken = crate::snap_wake::Woken { + item: Some(edge.clone()), + points: solidcraft_engine::snap::snap_points(&app.session, &edge), + revision: app.session.revision, + ..Default::default() + }; + assert_eq!(woken_pick(&app, true, corner.clone()), corner, "an end stays a point"); + assert_eq!(woken_pick(&app, true, Sel::Vertex { body: "Body1".into(), point: Vec3::new(10.0, 0.0, 10.0) }), edge); + // Another body's point at the same place stays that body's. + let other = Sel::Vertex { body: "Body2".into(), point: Vec3::new(10.0, 0.0, 10.0) }; + assert_eq!(woken_pick(&app, true, other.clone()), other, "a point of another body"); + // The dialog the caller has decides, not the app's: one whose input doesn't take snaps keeps + // the point. + let mut moved = app.dialog.clone().unwrap(); + assert!(takes_snaps(&moved)); + moved.inputs[0].accept = FACES; + assert!(!takes_snaps(&moved)); + let mid = Sel::Vertex { body: "Body1".into(), point: Vec3::new(10.0, 0.0, 10.0) }; + assert_eq!(woken_pick(&app, takes_snaps(&moved), mid.clone()), mid); + let face = face_sel(&app.session, Vec3::new(10.0, 10.0, 10.0)).unwrap(); + app.viewport.woken = crate::snap_wake::Woken { + item: Some(face.clone()), + points: solidcraft_engine::snap::snap_points(&app.session, &face), + revision: app.session.revision, + ..Default::default() + }; + assert_eq!(woken_pick(&app, true, Sel::Vertex { body: "Body1".into(), point: Vec3::new(10.0, 10.0, 10.0) }), face); +} + +/// A face of the rim's own body crossed on the way to its centre adds its centre to the rim's +/// points; that centre, picked, is the face, and the rim's middle is still the rim. +#[test] +fn a_crossed_faces_centre_is_the_face() { + let mut app = two_boxes(); + app.start("joint.create"); + let edge = edge_sel(&app.session, Vec3::new(10.0, 0.0, 10.0)).unwrap(); + let face = face_sel(&app.session, Vec3::new(10.0, 10.0, 10.0)).unwrap(); + let mut points = solidcraft_engine::snap::snap_points(&app.session, &edge); + points.extend(solidcraft_engine::snap::snap_points(&app.session, &face)); + app.viewport.woken = crate::snap_wake::Woken { item: Some(edge.clone()), face: Some(face.clone()), points, revision: app.session.revision }; + assert_eq!(woken_pick(&app, true, Sel::Vertex { body: "Body1".into(), point: Vec3::new(10.0, 10.0, 10.0) }), face); + assert_eq!(woken_pick(&app, true, Sel::Vertex { body: "Body1".into(), point: Vec3::new(10.0, 0.0, 10.0) }), edge); +} + #[test] fn joint_dialog_applies_custom_name_or_falls_back_to_type_default() { // 1. Empty name field: commands() generates payload without "name", backend creates "Rigid Joint1" diff --git a/crates/ui-egui/src/viewport.rs b/crates/ui-egui/src/viewport.rs index 143e40a8f29..cd38a1173c7 100644 --- a/crates/ui-egui/src/viewport.rs +++ b/crates/ui-egui/src/viewport.rs @@ -3928,9 +3928,14 @@ pub(crate) fn sketch_constraint_at(app: &SolveApp, pos: Pos2, hit: Option<&Hit>) pub fn candidate(app: &SolveApp, hits: &[Hit]) -> Option<(Hit, Sel)> { if let Some(d) = app.dialog.as_ref().filter(|d| d.wants_picks()) { let snap = d.face_snap(&app.session, hits); - return hits - .iter() - .find_map(|h| d.candidate_on(&app.session, h, snap.as_ref()).map(|s| (h.clone(), crate::dialogs_move::component_pick(app, h, s)))); + return hits.iter().find_map(|h| { + d.candidate_on(&app.session, h, snap.as_ref()).map(|s| { + ( + h.clone(), + crate::dialogs_assembly::woken_pick(app, crate::dialogs_assembly::takes_snaps(d), crate::dialogs_move::component_pick(app, h, s)), + ) + }) + }); } hits.iter().filter(|h| pick_allowed(app, h)).find_map(|h| hit_candidate(app, h)) } @@ -3943,7 +3948,18 @@ pub fn candidates(app: &SolveApp, hits: &[Hit]) -> Vec<(Hit, Sel)> { let snap = d.face_snap(&app.session, hits); return hits .iter() - .filter_map(|h| d.candidate_on(&app.session, h, snap.as_ref()).map(|s| (h.clone(), crate::dialogs_move::component_pick(app, h, s)))) + .filter_map(|h| { + d.candidate_on(&app.session, h, snap.as_ref()).map(|s| { + ( + h.clone(), + crate::dialogs_assembly::woken_pick( + app, + crate::dialogs_assembly::takes_snaps(d), + crate::dialogs_move::component_pick(app, h, s), + ), + ) + }) + }) .collect(); } hits.iter().filter(|h| pick_allowed(app, h)).filter_map(|h| hit_candidate(app, h)).collect() From 7f2e1e47409307c3a79a3bdc77bdfad202fb4d95 Mon Sep 17 00:00:00 2001 From: Frank <58932224+plexi32@users.noreply.github.com> Date: Thu, 12 Nov 2026 22:19:05 +0100 Subject: [PATCH 2/2] Joint tests: the Woken literal ends with ..Default::default() for #11810's kept_over Co-Authored-By: Claude Opus 5.5 --- crates/ui-egui/src/dialogs_assembly_tests.rs | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/crates/ui-egui/src/dialogs_assembly_tests.rs b/crates/ui-egui/src/dialogs_assembly_tests.rs index 0b428c3179d..0c03903d898 100644 --- a/crates/ui-egui/src/dialogs_assembly_tests.rs +++ b/crates/ui-egui/src/dialogs_assembly_tests.rs @@ -1620,7 +1620,11 @@ fn a_crossed_faces_centre_is_the_face() { let face = face_sel(&app.session, Vec3::new(10.0, 10.0, 10.0)).unwrap(); let mut points = solidcraft_engine::snap::snap_points(&app.session, &edge); points.extend(solidcraft_engine::snap::snap_points(&app.session, &face)); - app.viewport.woken = crate::snap_wake::Woken { item: Some(edge.clone()), face: Some(face.clone()), points, revision: app.session.revision }; + // `..Default::default()` keeps this compiling once #11810 adds `kept_over`. + #[allow(clippy::needless_update)] + let woken = + crate::snap_wake::Woken { item: Some(edge.clone()), face: Some(face.clone()), points, revision: app.session.revision, ..Default::default() }; + app.viewport.woken = woken; assert_eq!(woken_pick(&app, true, Sel::Vertex { body: "Body1".into(), point: Vec3::new(10.0, 10.0, 10.0) }), face); assert_eq!(woken_pick(&app, true, Sel::Vertex { body: "Body1".into(), point: Vec3::new(10.0, 0.0, 10.0) }), edge); }