diff --git a/crates/ui-egui/src/dialogs_assembly.rs b/crates/ui-egui/src/dialogs_assembly.rs index f403256bafe..d9cdf9e3ebe 100644 --- a/crates/ui-egui/src/dialogs_assembly.rs +++ b/crates/ui-egui/src/dialogs_assembly.rs @@ -1024,14 +1024,27 @@ pub fn snap_of(s: &Session, sel: &Sel) -> Option { } } Sel::Vertex { body, point } => { + // A woken centre (a flat face's, a round edge's) is kept by that face or edge, so it + // follows the part as a pick of the face or edge does (#11231). + if let Some(source) = centre_source(&st, body, *point) { + return snap_of(s, &source); + } // The corner of the edges meeting there, kept by their names, z along the part's +Z // as a point's is. let z = occurrence_of_body(s, body) .map_or(Vec3::Z, |o| solidcraft_engine::doc::apply_vector(&solidcraft_engine::doc::joints::occurrence_world(&s.doc, o), Vec3::Z)); - ( - body.as_str(), - SnapPt { at: *point, z: Vec3::Z, kind: SnapKind::Vertex, param: json!({"edge": pt(*point), "corner": pt(*point), "z": pt(z)}) }, - ) + let mut param = json!({"edge": pt(*point), "corner": pt(*point), "z": pt(z)}); + // A woken point on a part that is no corner (an edge's middle, #11231) is kept by the + // edge it lies on, so it follows that edge as a pick of the edge does. + let tol = 1e-6 * (1.0 + point.len()); + if st + .body(body) + .is_some_and(|b| !b.mesh().edges.iter().any(|e| [e.first(), e.last()].into_iter().flatten().any(|q| q.dist(*point) <= tol))) + && let Value::Object(m) = &mut param + { + m.remove("corner"); + } + (body.as_str(), SnapPt { at: *point, z: Vec3::Z, kind: SnapKind::Vertex, param }) } Sel::SketchPoint { id } => { // A sketch point's world position, its plane's normal as the joint's z, kept by its @@ -1105,6 +1118,19 @@ pub(crate) fn takes_snaps(d: &Dialog) -> bool { d.active_input().is_some_and(|i| i.accept == SNAPS) } +/// The flat face or round edge of `body` whose centre is at `at`, if one is. +fn centre_source(st: &solidcraft_engine::doc::ModelState, body: &str, at: Vec3) -> Option { + let b = st.body(body)?; + let near = |p: Vec3| p.dist(at) <= 1e-6 * (1.0 + at.len()); + let m = b.mesh(); + if let Some((i, e)) = m.edges.iter().enumerate().find(|(_, e)| circle_of(e).is_some_and(|(c, _)| near(c))) { + return Some(Sel::Edge { body: body.into(), index: i, point: *e.first()? }); + } + let tol = (b.body.size() * 2e-3).max(1e-3); + let f = b.body.faces(tol).ok()?.into_iter().find(|f| f.plane_normal.is_some() && near(f.centroid))?; + Some(Sel::Face { body: body.into(), index: f.index, point: f.centroid }) +} + /// 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); diff --git a/crates/ui-egui/src/dialogs_cam.rs b/crates/ui-egui/src/dialogs_cam.rs index ee8c3d6f216..705e082c3ca 100644 --- a/crates/ui-egui/src/dialogs_cam.rs +++ b/crates/ui-egui/src/dialogs_cam.rs @@ -15,7 +15,7 @@ use super::{ROUND, joint_row, occurrence, pick, picked_radius, trim, turn}; use crate::SolveApp; use crate::dialogs::{combo, field, form_row}; use crate::dialogs_assembly::Asm; -use crate::selection::{EDGES, FACES, PROFILES, SelInput, SelectionExt as _, VERTICES}; +use crate::selection::{EDGES, FACES, PROFILES, SNAP_POINTS, SelInput, SelectionExt as _, VERTICES, point_value}; use solidcraft_theme::Tokens; /// The follower types, as `joint.cam` takes them, and their labels. @@ -73,7 +73,7 @@ pub fn start(app: &SolveApp) -> (Asm, Vec) { let inputs = vec![ SelInput::new("Profile", PROFILES | EDGES, true), SelInput::new("Roller (optional)", ROUND, false), - SelInput::new("Tip (optional)", FACES | EDGES | VERTICES, false), + SelInput::new("Tip (optional)", FACES | EDGES | VERTICES | SNAP_POINTS, false), ]; (Asm::Cam(Box::new(f)), inputs) } @@ -195,8 +195,15 @@ pub fn commands(app: &SolveApp, f: &CamForm, inputs: &[SelInput]) -> Result p["tip"] = json!({"point": point_value(*point)}), + Some(x) => { + if let Some(snap) = crate::dialogs_assembly::snap_of(s, x) { + p["tip"] = snap.param; + } + } + None => {} } if let Some(index) = f.editing { p["link"] = json!(index); diff --git a/crates/ui-egui/src/dialogs_cam_tests.rs b/crates/ui-egui/src/dialogs_cam_tests.rs index 995e4cb671a..02f9a1487cf 100644 --- a/crates/ui-egui/src/dialogs_cam_tests.rs +++ b/crates/ui-egui/src/dialogs_cam_tests.rs @@ -102,6 +102,50 @@ fn a_picked_cam_edge_makes_the_cam() { assert!((v.first().copied().unwrap_or(f64::NAN) - (800f64.sqrt() - 40.0)).abs() < 1e-3, "{v:?}"); } +/// A snap point picked as the tip (on no body) is the tip's point, on the follower there; a corner +/// picked on the follower keeps the corner it was. +#[test] +fn a_snap_point_is_the_cam_tip() { + let mut app = valve(); + app.start("joint.cam"); + let mut d = app.dialog.take().unwrap(); + assert_ne!(d.inputs[TIP].accept & crate::selection::SNAP_POINTS, 0, "the tip takes snap points"); + d.inputs[PROFILE].items = vec![edge(&app, Vec3::new(-20.0, 0.0, 5.0))]; + d.inputs[TIP].items = vec![Sel::Vertex { body: String::new(), point: Vec3::new(40.0, 0.0, 5.0) }]; + let p = apply_commands(&app, &d).unwrap().remove(0).1; + assert_eq!(p["tip"], json!({"point": [40.0, 0.0, 5.0]}), "{p}"); + for (id, p) in apply_commands(&app, &d).unwrap() { + app.run(&id, p).unwrap(); + } + assert_eq!(app.session.doc.assembly.links.len(), 1, "the cam is made with the snapped tip"); +} + +/// A snapped middle of the tappet's end edge (a point on the part, no corner) is kept by that +/// edge, so the tip follows it as a pick of the edge does; a real corner stays a corner. +#[test] +fn a_snapped_edge_middle_tip_is_kept_by_its_edge() { + let app = valve(); + let st = app.session.world_state().unwrap(); + let body = st.bodies.iter().find(|b| b.mesh().bounds().max.x > 59.0).map(|b| b.name.clone()).unwrap(); + let mid = crate::dialogs_assembly::snap_of(&app.session, &Sel::Vertex { body: body.clone(), point: Vec3::new(60.0, 0.0, 5.0) }).unwrap(); + assert!(mid.param.get("corner").is_none() && mid.param["edge"] == json!([60.0, 0.0, 5.0]), "{}", mid.param); + let corner = crate::dialogs_assembly::snap_of(&app.session, &Sel::Vertex { body, point: Vec3::new(60.0, 2.0, 5.0) }).unwrap(); + assert_eq!(corner.param["corner"], json!([60.0, 2.0, 5.0]), "{}", corner.param); +} + +/// A snapped centre of the tappet's flat end face is kept by that face, so the tip follows the +/// face as the part changes, as a pick of the face does. +#[test] +fn a_snapped_face_centre_tip_is_kept_by_its_face() { + let app = valve(); + let st = app.session.world_state().unwrap(); + let b = st.bodies.iter().find(|b| b.mesh().bounds().max.x > 59.0).unwrap(); + let tol = (b.body.size() * 2e-3).max(1e-3); + let end = b.body.faces(tol).unwrap().into_iter().find(|f| f.plane_normal.is_some() && f.centroid.x > 59.0).unwrap(); + let tip = crate::dialogs_assembly::snap_of(&app.session, &Sel::Vertex { body: b.name.clone(), point: end.centroid }).unwrap(); + assert!(tip.param.get("face").is_some(), "{}", tip.param); +} + /// The dialog's rows pass their English text, which the catalogs translate by its key. #[test] fn the_cam_dialog_labels_translate() { diff --git a/crates/ui-egui/src/dialogs_surface.rs b/crates/ui-egui/src/dialogs_surface.rs index 95cf2c34114..9643aac0b0b 100644 --- a/crates/ui-egui/src/dialogs_surface.rs +++ b/crates/ui-egui/src/dialogs_surface.rs @@ -16,7 +16,7 @@ use solidcraft_engine::kernel::{Construction, Continuity}; use crate::SolveApp; use crate::dialogs::{Kind, OP_LABELS, OPS, combo, form_row, op_index}; use crate::selection::{ - AXES, AXIS_PICKS, BODIES, CURVES, EDGES, FACES, PLANES, PROFILES, SelInput, SelectionExt as _, VERTICES, items, point_value as pt, + AXES, AXIS_PICKS, BODIES, CURVES, EDGES, FACES, PLANES, PROFILES, SNAP_POINTS, SelInput, SelectionExt as _, VERTICES, items, point_value as pt, }; use crate::ui_kit; @@ -241,7 +241,9 @@ fn inputs(k: &Surf) -> Vec { } Surf::Blend { .. } => vec![SelInput::new("From edges", EDGES, true), SelInput::new("To edges", EDGES, true)], Surf::Boundary { .. } => vec![SelInput::new("Curves", CURVES | EDGES, true), SelInput::new("Cross curves", CURVES | EDGES, true)], - Surf::Patch { .. } => vec![SelInput::new("Boundary", PROFILES | CURVES | EDGES, true), SelInput::new("Pass through", VERTICES | FACES, true)], + Surf::Patch { .. } => { + vec![SelInput::new("Boundary", PROFILES | CURVES | EDGES, true), SelInput::new("Pass through", VERTICES | FACES | SNAP_POINTS, true)] + } Surf::Unstitch => vec![SelInput::new("Body, or faces to take out", BODIES | FACES, true)], Surf::ReverseNormal => vec![SelInput::new("Surface, or faces to turn over", BODIES | FACES, true)], Surf::Ruled { .. } => { diff --git a/crates/ui-egui/src/preselect_tests.rs b/crates/ui-egui/src/preselect_tests.rs index 9a0a4e28c9c..0eca10f4a8c 100644 --- a/crates/ui-egui/src/preselect_tests.rs +++ b/crates/ui-egui/src/preselect_tests.rs @@ -2963,6 +2963,11 @@ fn patch_dialog_takes_continuity_and_points_and_brings_them_back() { } let (id, p) = apply_commands(&app, &d).unwrap().remove(0); assert_eq!((id.as_str(), &p["continuity"], &p["points"]), ("surface.patch", &json!("g1"), &json!([[0.0, 0.0, 36.0]])), "{p}"); + // A snap point (on no body: an edge's middle, a face's centre) passes through as a point too. + assert_ne!(d.inputs[1].accept & crate::selection::SNAP_POINTS, 0, "Pass through takes snap points"); + let mut snapped = d.clone(); + snapped.inputs[1].items = vec![Sel::Vertex { body: String::new(), point: Vec3::new(0.0, 0.0, 36.0) }]; + assert_eq!(apply_commands(&app, &snapped).unwrap()[0].1["points"], json!([[0.0, 0.0, 36.0]])); app.session.execute(&id, &p).unwrap(); let top = app.session.model.state().bodies.last().unwrap().mesh().bounds().max.z; assert!(top > 35.9, "the cap reaches the point: {top}");