Skip to content

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

Create Sketch: pick an origin plane through a body in front of it (#5431) (#6597)

yhamil048312bf0cb8cc
diff --git a/crates/ui-egui/src/viewport.rs b/crates/ui-egui/src/viewport.rs
index bc8b8da38a..f63e6ebe27 100644
--- a/crates/ui-egui/src/viewport.rs
+++ b/crates/ui-egui/src/viewport.rs
@@ -3088,22 +3088,34 @@ fn pick_hits(app: &SolveApp, proj: &Proj, pos: Pos2, other_sketch_points: bool,
for (t, _, h) in bestp.into_iter().chain(profiles_all) {
surf.push((t - slack, h));
}
+ let front = bestf.as_ref().map(|(t, _)| *t);
if all {
surf.extend(faces.sorted());
} else if let Some(f) = bestf {
surf.push(f);
}
- // Planes give way to a face or profile at the same depth (a floor on the XZ plane).
+ let mut planes: Vec<(f64, Hit)> = Vec::new();
for (name, _, q) in origin_planes(app) {
if let Some(t) = ray_quad(o, d, &q) {
- surf.push((t + slack, Hit::Plane { name: name.into(), point: o + d * t }));
+ planes.push((t, Hit::Plane { name: name.into(), point: o + d * t }));
}
}
for (name, _, q) in construction_quads(app) {
if let Some(t) = ray_quad(o, d, &q) {
- surf.push((t + slack, Hit::Plane { name, point: o + d * t }));
+ planes.push((t, Hit::Plane { name, point: o + d * t }));
}
}
+ planes.sort_by(|a, b| a.0.total_cmp(&b.0));
+ // Planes give way to a face or profile at the same depth (a floor on the XZ plane). While a
+ // dialog waits for a plane they are drawn over the bodies, so when the nearest face hides the
+ // nearest plane, the planes are picked through it: just ahead of that face, in their own
+ // depth order, still behind a profile on it (#5431).
+ let through = match (front, planes.first()) {
+ (Some(f), Some((t, _))) if app.picking_plane() && *t > f + slack => Some(f - slack * 0.5),
+ _ => None,
+ };
+ // The sort is stable, so planes moved ahead of the face keep their order.
+ surf.extend(planes.into_iter().map(|(t, h)| (through.unwrap_or(t + slack), h)));
surf.sort_by(|a, b| a.0.total_cmp(&b.0));
hits.extend(surf.into_iter().map(|x| x.1));
hits
diff --git a/crates/ui-egui/src/viewport_plane_target_tests.rs b/crates/ui-egui/src/viewport_plane_target_tests.rs
index 818506c253..7ce8df63c3 100644
--- a/crates/ui-egui/src/viewport_plane_target_tests.rs
+++ b/crates/ui-egui/src/viewport_plane_target_tests.rs
@@ -1,5 +1,6 @@
use crate::scenario::Harness;
use serde_json::json;
+use solidcraft_engine::doc::{FeatureKind, PlaneRef};
use solidcraft_engine::geom::prelude::*;
/// `ui.at {"plane": …}` gives a point that a click picks that plane at, even where another
@@ -21,3 +22,75 @@ fn clicking_an_origin_plane_target_sketches_on_that_plane() {
}
}
}
+
+/// A box at the origin: the three origin plane squares lie on or behind its faces from every
+/// view (#5431).
+fn box_at_origin() -> Harness {
+ let mut h = Harness::new();
+ h.app.run("solid.box", json!({"length":50,"width":50,"height":50})).unwrap();
+ h.call("ui.view", json!({"view":"iso"}));
+ h.call("ui.view", json!({"view":"fit"}));
+ h.settle();
+ h
+}
+
+/// The plane the sketch being edited was created on (a plane flipped to face the view, as the
+/// plane it was flipped from).
+fn sketch_plane(h: &Harness) -> PlaneRef {
+ let sketch = h.app.session.active_sketch.expect("the click creates a sketch");
+ match &h.app.session.doc.feature(sketch).unwrap().kind {
+ FeatureKind::Sketch { plane: PlaneRef::Flipped { base, .. }, .. } => (**base).clone(),
+ FeatureKind::Sketch { plane, .. } => plane.clone(),
+ other => panic!("feature {sketch} is not a sketch: {other:?}"),
+ }
+}
+
+/// While Create Sketch waits for a plane, the origin planes are drawn over the bodies, so a
+/// click on one the box hides picks it through the box (#5431). From this view the box hides the
+/// XY and YZ squares; the XZ square lies on its front face, and a face still wins that tie (#31).
+/// Each click goes where the scenarios click a plane: on its square, clear of the other squares.
+#[test]
+fn create_sketch_picks_an_origin_plane_through_a_body() {
+ use crate::viewport::Hit;
+ for (plane, hidden) in [("XY", true), ("YZ", true), ("XZ", false)] {
+ let mut h = box_at_origin();
+ h.step(&json!({"start":"sketch.create"})).unwrap();
+ let at = h.at(&json!({ "plane": plane })).unwrap();
+ let proj = crate::viewport::projection(&h.app, h.app.viewport.rect.unwrap());
+ let pos = egui::pos2(at[0] as f32, at[1] as f32);
+ let hits = crate::viewport::pick(&h.app, &proj, pos);
+ let face = hits.iter().find_map(|x| if let Hit::Face { point, .. } = x { Some(*point) } else { None });
+ let on_plane = hits.iter().find_map(|x| match x {
+ Hit::Plane { name, point } if name == plane => Some(*point),
+ _ => None,
+ });
+ let (Some(face), Some(on_plane)) = (face, on_plane) else { panic!("{plane}: the box and the plane are under the pointer: {hits:?}") };
+ let (o, _) = proj.ray(pos);
+ if hidden {
+ assert!(on_plane.dist(o) > face.dist(o) + 1.0, "{plane}: the box hides the plane where it is clicked");
+ }
+ h.step(&json!({"click": at})).unwrap();
+ h.settle();
+ let picked = sketch_plane(&h);
+ if hidden {
+ assert_eq!(picked, PlaneRef::Origin { name: plane.into() }, "{plane}: sketched on the origin plane");
+ } else {
+ assert!(matches!(picked, PlaneRef::Face { .. }), "{plane}: sketched on the face the plane lies on, not {picked:?}");
+ }
+ }
+}
+
+/// Away from the origin plane squares, Create Sketch still takes the body's face.
+#[test]
+fn create_sketch_still_picks_a_face_beside_the_origin_planes() {
+ let mut h = box_at_origin();
+ h.step(&json!({"start":"sketch.create"})).unwrap();
+ let at = h.at(&json!({"world": [45, 45, 50]})).unwrap();
+ let proj = crate::viewport::projection(&h.app, h.app.viewport.rect.unwrap());
+ let hits = crate::viewport::pick(&h.app, &proj, egui::pos2(at[0] as f32, at[1] as f32));
+ assert!(!hits.iter().any(|x| matches!(x, crate::viewport::Hit::Plane { .. })), "no origin plane under the pointer: {hits:?}");
+ h.step(&json!({"click": at})).unwrap();
+ h.settle();
+ let plane = sketch_plane(&h);
+ assert!(matches!(plane, PlaneRef::Face { .. }), "sketched on the top face, not {plane:?}");
+}
diff --git a/crates/ui-egui/tests/scenarios/view_confirm_corner.json b/crates/ui-egui/tests/scenarios/view_confirm_corner.json
index b8f45c7fed..6916f18b0d 100644
--- a/crates/ui-egui/tests/scenarios/view_confirm_corner.json
+++ b/crates/ui-egui/tests/scenarios/view_confirm_corner.json
@@ -23,8 +23,8 @@
{"start": "sketch.create"},
{"click": {"plane": "XY"}},
{"start": "sketch.line"},
- {"click": {"sketch": [50, 0]}},
- {"click": {"sketch": [70, 10]}},
+ {"click": {"sketch": [10, 10]}},
+ {"click": {"sketch": [30, 20]}},
{"click": {"handle": "view:confirm:cancel"}},
{"click": {"handle": "view:confirm:cancel"}},
{"expect": {"sketching": true, "sketch_curves": {"own": 1}}},