Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions crates/ui-egui/src/sketch3d_frame_ui_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -341,3 +341,19 @@ fn point3d_tool_places_points_and_stays() {
assert!(wires[1].fit[0].dist(Vec3::new(20.0, 10.0, 10.0)) < 1e-6, "{:?}", wires[1].fit);
assert!(h.app.tool.as_ref().is_some_and(|t| t.cmd == "sketch.point3d"), "the tool stays for the next point");
}

/// Snap points in 3D sketches (#11229): hovering a box's top edge wakes its middle, and the 3D
/// Point tool takes the middle itself from just beside it on the edge (a woken point beats the
/// edge it lies on within `snap_wake::ON`).
#[test]
fn point3d_takes_a_model_edges_woken_middle() {
let (mut h, _) = box_and_3d_sketch("Point3dSnap");
h.app.start("sketch.point3d");
h.frames(2);
h.step(&json!({"move": {"world": [12.5, 0, 10]}})).unwrap();
assert!(!h.app.viewport.woken.points.is_empty(), "hover {:?}", h.app.viewport.hover);
let [x, y] = h.at(&json!({"world": [15.02, 0, 10]})).unwrap();
let proj = crate::viewport::projection(&h.app, h.app.viewport.rect.unwrap());
let picked = crate::sketch3d::point_at(&h.app, &proj, egui::pos2(x as f32, y as f32));
assert!(picked.is_some_and(|(q, _)| q.dist(Vec3::new(15.0, 0.0, 10.0)) < 1e-6), "{picked:?}");
}
43 changes: 43 additions & 0 deletions crates/ui-egui/src/sketch_guidance_ui_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -632,3 +632,46 @@ fn a_point_near_a_visible_grid_intersection_snaps_to_it_7114() {
let hover = h.app.tool.as_ref().unwrap().hover.as_ref().unwrap().0;
assert_eq!(hover, at, "the point lands on the visible grid intersection");
}

/// A box with a Ø6 hole at (15, 20), seen from the top, and a sketch on XY under it.
fn holed_box_sketch() -> Harness {
let mut h = Harness::new();
h.app.home.open = false;
h.app.set_origin_shown(false);
h.app.ui.show_browser = false;
h.app.preferences.look_at_anim = false;
h.app.run("solid.box", json!({"length": 60, "width": 40, "height": 10})).unwrap();
h.app.run("solid.cylinder", json!({"radius": 3, "height": 12, "base": [15, 20, -1], "operation": "cut"})).unwrap();
h.app.run("sketch.create", json!({"plane": "XY"})).unwrap();
// Snap off: only a snap, not the grid, can put the point exactly on the centre.
h.app.run("sketch.options", json!({"snap": false})).unwrap();
step(&mut h, json!({"call":"ui.view","params":{"view":"top"}}));
step(&mut h, json!({"call":"ui.camera","params":{"target":[30,20,0],"distance":160}}));
h
}

fn sketch_points(h: &Harness) -> Vec<solidcore_record::Vec2> {
let st = h.app.session.world_state().unwrap();
st.sketch(h.app.session.active_sketch.unwrap()).unwrap().sketch.points.iter().map(|p| p.pos).collect()
}

/// Snap points in sketch tools (#11229): hovering a hole's rim wakes its centre, and a sketch
/// point clicked near it lands on the centre projected into the sketch. With the preference
/// off the click stays where it is.
#[test]
fn sketch_point_snaps_to_a_model_holes_woken_centre() {
let centre = solidcore_record::Vec2::new(15.0, 20.0);
for on in [true, false] {
let mut h = holed_box_sketch();
h.call("ui.prefs", json!({"snap_points": on}));
h.app.start("sketch.point");
step(&mut h, json!({"move":{"world":[18,20,10]}}));
assert_eq!(h.app.viewport.woken.points.is_empty(), !on, "{:?}", h.app.viewport.woken);
let before = sketch_points(&h).len();
step(&mut h, json!({"click":{"world":[15.4,20.3,10]}}));
let pts = sketch_points(&h);
assert_eq!(pts.len(), before + 1, "{pts:?}");
let placed = pts[before];
assert_eq!(placed.dist(centre) < 1e-6, on, "snap points {on}: {placed:?}");
}
}
27 changes: 24 additions & 3 deletions crates/ui-egui/src/sketch_tools.rs
Original file line number Diff line number Diff line change
Expand Up @@ -619,10 +619,22 @@ pub fn refine_snap(
let px = 1.0 / (a.distance(b) as f64).max(1e-9);
let tol = 7.0 * px;
// A model vertex under the cursor: drawing on it projects it into the sketch.
// A snap point of a model edge or another sketch's curve (#11229) is one too: an end of a
// body's edge is a vertex, projected and linked like one; a centre or a middle (and a point
// of another sketch) is no vertex to link, so the point goes where it projects to.
for h in pick(app, proj, a) {
if let Hit::Vertex { point, .. } = h {
SNAP.with(|s| *s.borrow_mut() = Some(("Pt", plane.to_local(point))));
return Some((plane.to_local(point), Some(format!("vertex:{},{},{}", point.x, point.y, point.z))));
if let Hit::Vertex { point, body } = h {
use solidcraft_engine::snap::SnapKind;
let vertex = Some(format!("vertex:{},{},{}", point.x, point.y, point.z));
let (label, arg) = match crate::snap_wake::woken_at(app, point).map(|p| p.kind) {
Some(SnapKind::Centre | SnapKind::FaceCentre) => ("Ctr", None),
Some(SnapKind::Middle) => ("Mid", None),
Some(SnapKind::End) if body.is_empty() => ("End", None),
Some(SnapKind::End) => ("End", vertex),
Some(SnapKind::Vertex) | None => ("Pt", vertex),
};
SNAP.with(|s| *s.borrow_mut() = Some((label, plane.to_local(point))));
return Some((plane.to_local(point), arg));
}
}
let mid_only = crate::tool_modifiers::target_midpoint();
Expand Down Expand Up @@ -901,6 +913,15 @@ pub fn takes(id: &str, hit: &Hit) -> bool {
}
}

/// Whether a tool places points on the sketch plane (and so takes snap points, #11229).
pub fn places_points(t: &Tool) -> bool {
match t.kind {
Kind::Draw(_) => true,
Kind::Ext => matches!(mode(t.cmd), Some(Mode::Points(_))),
_ => false,
}
}

fn mode(id: &str) -> Option<Mode> {
if solidcraft_engine::cmd::construction_line_mode(id).is_some() {
return Some(Mode::Construction);
Expand Down
38 changes: 30 additions & 8 deletions crates/ui-egui/src/snap_wake.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
//! Measure), hovering an edge, a face or a sketch curve wakes the points it offers (`solidcraft_engine::snap`):
//! a hole's centre, an edge's middle and ends, a face's centre. Woken points are drawn as small
//! glyphs and picked like vertices from further away, so a centre in empty space can be clicked.
//! Sketch tools that place points take them too (#11229): a 2D tool's point lands on the
//! sketch plane where the snap point projects to, a 3D sketch tool's on the point itself.
//! On by default; the `snap_points` preference turns them off.
//! They stay awake across empty space and faces, and sleep when another edge or curve is hovered
//! (a face only replaces a face's points), or when the input stops taking them.
Expand Down Expand Up @@ -38,15 +40,30 @@ impl Woken {
}
}

/// Whether snap points are on and the active dialog input takes them.
/// Whether snap points are on and the active dialog input or sketch tool takes them.
pub fn wanted(app: &SolveApp) -> bool {
app.preferences.snap_points && app.dialog.as_ref().and_then(|d| d.active_input()).is_some_and(|i| i.accept & crate::selection::SNAP_POINTS != 0)
app.preferences.snap_points
&& (app.dialog.as_ref().and_then(|d| d.active_input()).is_some_and(|i| i.accept & crate::selection::SNAP_POINTS != 0) || sketch_drawing(app))
}

/// Whether a sketch tool that places points is active (2D or 3D).
fn sketch_drawing(app: &SolveApp) -> bool {
app.session.active_sketch.is_some() && app.tool.as_ref().is_some_and(|t| crate::sketch_tools::places_points(t) || crate::sketch3d::drawing(app))
Comment thread
plexi32 marked this conversation as resolved.
Resolved
}

/// Whether woken points may be picked or drawn: as [`wanted`], or in a sketch whose tool a click
/// has taken out of the app while it runs (the woken points are the last hover's; `update`
/// puts them to sleep once nothing takes them).
fn usable(app: &SolveApp) -> bool {
wanted(app) || (app.preferences.snap_points && app.tool.is_none() && app.session.active_sketch.is_some())
}

/// The item a hit wakes points of: an edge or a face of a body, or a sketch curve (a line's
/// middle, a circle's centre).
fn item_of(h: &Hit) -> Option<Sel> {
/// middle, a circle's centre). Not a curve of the sketch being edited: its tools snap to its own
/// points and middles already.
fn item_of(app: &SolveApp, h: &Hit) -> Option<Sel> {
match h {
Hit::SketchCurve { sketch, .. } if app.tool.is_some() && app.session.active_sketch == Some(*sketch) => None,
Hit::Edge { body, index, mid, .. } => Some(Sel::Edge { body: body.clone(), index: *index, point: *mid }),
Hit::Face { body, index, point } => Some(Sel::Face { body: body.clone(), index: *index, point: *point }),
Hit::SketchCurve { sketch, id, .. } => Some(Sel::ExternalSketchCurve { sketch: *sketch, id: id.clone() }),
Expand Down Expand Up @@ -84,8 +101,8 @@ pub fn update(app: &mut SolveApp, hits: &[Hit]) {
if app.viewport.woken.revision != app.session.revision {
app.viewport.woken = Woken { revision: app.session.revision, ..Woken::default() };
}
let curve = hits.iter().find_map(item_of).filter(|i| matches!(i, Sel::ExternalSketchCurve { .. }));
let Some(item) = curve.or_else(|| app.viewport.hover.as_ref().and_then(item_of)) else { return };
let curve = hits.iter().find_map(|h| item_of(app, h)).filter(|i| matches!(i, Sel::ExternalSketchCurve { .. }));
let Some(item) = curve.or_else(|| app.viewport.hover.as_ref().and_then(|h| item_of(app, h))) else { return };
if app.viewport.woken.has(&item) {
return;
}
Expand Down Expand Up @@ -169,7 +186,7 @@ pub fn snapped(app: &SolveApp, at: Vec3) -> Option<&(Sel, SnapPoint)> {
/// The woken points within reach of `pos`, nearest first, as vertex hits (with their distance
/// and kind).
pub fn hits(app: &SolveApp, proj: &Proj, pos: Pos2) -> Vec<(f32, Hit, SnapKind)> {
if !wanted(app) {
if !usable(app) {
return Vec::new();
}
let mut v: Vec<(f32, Hit, SnapKind)> = shown(app)
Expand All @@ -186,7 +203,7 @@ pub fn hits(app: &SolveApp, proj: &Proj, pos: Pos2) -> Vec<(f32, Hit, SnapKind)>
/// Draw the woken points: a ring with a cross for a centre, a ring with a dot for a face's
/// centre, a triangle for a middle, a square for an end. Vertices draw as vertices already.
pub fn draw(app: &SolveApp, painter: &Painter, proj: &Proj) {
if !wanted(app) {
if !usable(app) {
return;
}
let t = Tokens::get();
Expand Down Expand Up @@ -226,3 +243,8 @@ pub fn inspect(app: &SolveApp) -> serde_json::Value {
pub fn label(p: Vec3) -> String {
format!("({:.2}, {:.2}, {:.2}) mm", p.x, p.y, p.z)
}

/// The woken point at `at`, if `at` is one (a vertex hit made by [`hits`]).
pub fn woken_at(app: &SolveApp, at: Vec3) -> Option<&SnapPoint> {
app.viewport.woken.points.iter().find(|p| p.at == at)
}
Loading