SolidCraft: an open-source parametric 3D CAD application built in pure Rust. Desktop, web, and automation through MCP.
Create Sketch: the grid previews the origin plane under the pointer (#6975) (#7404)
semmelrindec8c953402230
diff --git a/crates/ui-egui/src/browser.rs b/crates/ui-egui/src/browser.rsindex cde4b678b1..ff532eaa41 100644--- a/crates/ui-egui/src/browser.rs+++ b/crates/ui-egui/src/browser.rs@@ -135,6 +135,8 @@ pub struct TreeState {/// The bodies a row at the top of the history lights this frame (`history_rows`), for the/// browser, drawn after it, to keep.history_hover: Vec<String>,+ /// The plane a row at the top of the history lights this frame, kept the same way (#6975).+ history_plane: Option<String>,/// A Section Analysis hidden with its eye in the Analysis folder: its plane (origin,/// normal), so the eye can bring it back (#822).pub hidden_section: Option<(solidcraft_engine::geom::Vec3, solidcraft_engine::geom::Vec3)>,@@ -610,6 +612,8 @@ struct Actions {menu: Option<(Pos2, Target)>,hover_bodies: Vec<String>,hovered: bool,+ /// The plane whose row is under the pointer, lit in the view (#6975).+ hover_plane: Option<String>,drop: Option<DropAction>,/// What the selection and the 3D view's pointer light in the tree.lit: Lit,@@ -1182,7 +1186,7 @@ pub(crate) fn history_rows(app: &mut SolveApp, ui: &mut egui::Ui) {ui.scope(|ui| {ui.spacing_mut().item_spacing.y = 0.0;folder_rows_at(app, ui, component, path.as_deref(), "bodies", 0, &mut acts, &bodies);- origin_rows(app, ui, 0, &acts);+ origin_rows(app, ui, 0, &mut acts);});app.tree.reveal = reveal;// The browser's own rows reuse these order slots (`TreeState::remember`).@@ -1190,6 +1194,8 @@ pub(crate) fn history_rows(app: &mut SolveApp, ui: &mut egui::Ui) {DRAWN.with(|d| d.borrow_mut().truncate(drawn));app.viewport.hover_bodies = acts.hover_bodies.clone();app.tree.history_hover = acts.hover_bodies;+ app.viewport.hover_plane = acts.hover_plane.clone();+ app.tree.history_plane = acts.hover_plane;if let Some(d) = acts.drop {apply_drop(app, d);}@@ -1206,6 +1212,8 @@ fn finish_browser(app: &mut SolveApp, ui: &mut egui::Ui, mut acts: Actions) {// The history's rows, drawn before the browser, light bodies too (`history_rows`).let history = std::mem::take(&mut app.tree.history_hover);app.viewport.hover_bodies = if acts.hover_bodies.is_empty() { history } else { acts.hover_bodies };+ let history_plane = app.tree.history_plane.take();+ app.viewport.hover_plane = acts.hover_plane.or(history_plane);if let Some(d) = acts.drop {apply_drop(app, d);}@@ -1588,7 +1596,7 @@ fn origin_things() -> [(&'static str, &'static str, Option<Thing>); 7] {]}-fn origin_rows(app: &mut SolveApp, ui: &mut egui::Ui, depth: usize, acts: &Actions) {+fn origin_rows(app: &mut SolveApp, ui: &mut egui::Ui, depth: usize, acts: &mut Actions) {let key = "origin";let open = is_open(app, key, false);let lit = |thing: &Option<Thing>| thing.as_ref().map_or((false, false), |t| acts.lit.thing(t));@@ -1655,6 +1663,9 @@ fn origin_rows(app: &mut SolveApp, ui: &mut egui::Ui, depth: usize, acts: &Actio{crate::context_menu::open_for(app, p, Target::OriginPlane { name: k.into() });}+ if r.hovered && icon == "plane" {+ acts.hover_plane = Some(k.into());+ }if r.clicked&& let Some(x) = sel{@@ -2520,6 +2531,11 @@ fn entry_row(if r.hovered && !e.bodies.is_empty() {acts.hover(e.bodies.iter());}+ if r.hovered+ && let Thing::Plane(name) = &e.thing+ {+ acts.hover_plane = Some(name.clone());+ }if let Some(p) = r.secondary&& let Some(t) = e.target.clone(){diff --git a/crates/ui-egui/src/viewport.rs b/crates/ui-egui/src/viewport.rsindex 8f00aac488..4db5c308af 100644--- a/crates/ui-egui/src/viewport.rs+++ b/crates/ui-egui/src/viewport.rs@@ -5,7 +5,7 @@ use crate::ui_kit::{Container, Surface};use egui::{Align2, Color32, FontId, Pos2, Rect, Sense, Shape, Stroke, pos2, vec2};use serde_json::json;use solidcraft_engine::Sel;-use solidcraft_engine::geom::{Vec2, Vec3};+use solidcraft_engine::geom::{Plane, Vec2, Vec3};use solidcraft_engine::render::{Camera, Mat4, Rgb};use solidcraft_engine::sketch::CurveKind;use solidcraft_engine::view::{GridFade, GridPlane, colors, grid_ext, grid_plane, grid_step, sketch_lines_with_sampler};@@ -162,6 +162,9 @@ pub struct ViewportState {pub hover_feature: Option<u64>,/// Bodies whose browser row is under the cursor: highlighted the same way.pub hover_bodies: Vec<String>,+ /// The origin or construction plane whose browser row is under the cursor: lit as if the+ /// pointer were on it in the view.+ pub hover_plane: Option<String>,/// The model point an orbit turns around (Preferences: orbit around the cursor).pub orbit_pivot: Option<Vec3>,/// The orbit drag under way, summed from where it began (see [`crate::navigation::orbit`]).@@ -489,6 +492,8 @@ fn sketch_scene_key(app: &SolveApp, proj: &Proj) -> u64 {// Finished sketches are drawn over the bodies when the preference says so.app.preferences.reference_geometry_through_bodies.hash(&mut h);app.preferences.sketch_halo.hash(&mut h);+ // The grid previews the plane Create Sketch would take (#6975).+ previewed_sketch_plane(app).map(|p| [p.origin, p.x, p.y].map(|v| [v.x, v.y, v.z].map(f64::to_bits))).hash(&mut h);(std::sync::Arc::as_ptr(&crate::open_meshing::shown_world(app)) as usize).hash(&mut h);crate::sketch_display::projection_key(proj, app.viewport.pixels_per_point()).hash(&mut h);h.finish()@@ -1491,12 +1496,13 @@ fn build_sketch_scene(app: &SolveApp, proj: &Proj) -> GpuScene {let s = &app.session;let st = crate::open_meshing::shown_world(app);let mut fill_budget = 131_072;- let sketch_plane = solidcraft_engine::view::open_sketch_plane(s);+ let sketch_plane = solidcraft_engine::view::open_sketch_plane(s).or_else(|| previewed_sketch_plane(app));if sketch_plane.is_some() || (app.ui.show_grid && !active_view(app).hide_grid) {let (minor, major) = grid_step(app.cam.half_height());- // An open sketch always has its grid on its own plane; otherwise the ground plane: XY with- // Z up, XZ with Y up (laid out in the camera's Z-up frame), or the upright origin plane- // facing the view when the ground is seen edge-on.+ // An open sketch always has its grid on its own plane, and so does the plane Create+ // Sketch would take; otherwise the ground plane: XY with Z up, XZ with Y up (laid out in+ // the camera's Z-up frame), or the upright origin plane facing the view when the ground+ // is seen edge-on.let (c, u, v, slant, ext, far_reach, w3): (Vec3, Vec3, Vec3, f64, f64, f64, Box<dyn Fn(Vec3) -> [f32; 3]>) = if let Some(pl) = &sketch_plane {let (c, u, v) = solidcraft_engine::view::sketch_grid_frame(pl, app.cam.target, major);// How steeply the sketch's plane is seen: the grid reaches as far on it as the view@@ -1734,6 +1740,24 @@ pub fn origin_planes(app: &SolveApp) -> Vec<(&'static str, Vec3, [Vec3; 4])> {.collect()}+/// The plane Create Sketch would put the sketch on, while it waits for one (#6975): the origin+/// or construction plane under the pointer, in the view or in the browser. The grid is drawn on+/// it, so the sketch's plane shows before the click.+pub fn previewed_sketch_plane(app: &SolveApp) -> Option<Plane> {+ if app.session.active_sketch.is_some() || !app.picking_sketch_plane() {+ return None;+ }+ let name = match (&app.viewport.hover_plane, &app.viewport.hover) {+ (Some(name), _) | (None, Some(Hit::Plane { name, .. })) => name,+ _ => return None,+ };+ solidcraft_engine::view::construction_planes(&app.session)+ .into_iter()+ .find(|(_, n, _)| n == name)+ .map(|(_, _, p)| p)+ .or_else(|| origin_planes(app).into_iter().find(|(n, _, _)| *n == name.as_str()).and_then(|(n, _, _)| Plane::named(n)))+}+/// A point on origin plane `name`'s square that a click at its screen point picks: the middle,/// or else the middle of a quarter the other planes don't cover from this view (in an iso view/// the XZ square hides the middle of the XY one). The middle when no candidate is picked.@@ -3078,6 +3102,7 @@ fn highlight_geometry_key(app: &SolveApp) -> u64 {hover_drawn(&app.viewport.hover).hash(&mut h);app.viewport.hover_feature.hash(&mut h);app.viewport.hover_bodies.hash(&mut h);+ app.viewport.hover_plane.hash(&mut h);// Which mesh lights a face follows how the model draws its body (see `shown_mesh`), and at// which display quality its plan and edges are made.solidcraft_engine::doc::display_quality::current().hash(&mut h);@@ -3349,7 +3374,7 @@ pub(crate) fn build_highlight(app: &SolveApp) -> GpuScene {// Origin planes and construction planes.let plane_state = |name: &str| {let selected = sel.iter().any(|x| matches!(x, Sel::Plane { name: n } if n == name));- let hovered = matches!(hover, Some(Hit::Plane { name: n, .. }) if n == name);+ let hovered = matches!(hover, Some(Hit::Plane { name: n, .. }) if n == name) || app.viewport.hover_plane.as_deref() == Some(name);(selected, hovered)};let mut planes: Vec<(String, [Vec3; 4], Color32)> =@@ -10195,6 +10220,49 @@ mod grid_plane_tests {assert_eq!(*app.viewport.grid_plane.lock().unwrap(), GridPlane::Ground, "a clear turn off the ground returns");}+ /// #6975: while Create Sketch waits for its plane, the grid lies on the origin plane under+ /// the pointer, or under the pointer in the browser, so the sketch's plane shows before the+ /// click. Otherwise it stays on the ground.+ #[test]+ fn create_sketch_previews_the_plane_under_the_pointer_on_the_grid() {+ let rect = Rect::from_min_size(Pos2::ZERO, egui::vec2(800.0, 600.0));+ let mut app = SolveApp::new(solidcraft_engine::Session::default(), crate::Services::default());+ app.ui.show_grid = true;+ app.preferences.grid_follows_view = false;+ let grid = |app: &SolveApp| -> Vec<[f32; 3]> {+ build_sketch_scene(app, &projection(app, rect))+ .under+ .as_chunks::<32>()+ .0+ .iter()+ .flat_map(|l| [&l[0..12], &l[12..24]])+ .map(|b| [0, 4, 8].map(|i| f32::from_le_bytes(b[i..i + 4].try_into().unwrap())))+ .collect()+ };+ // Every grid point has coordinate `axis` 0: the grid lies on that origin plane.+ let on = |app: &SolveApp, axis: usize| {+ let points = grid(app);+ !points.is_empty() && points.iter().all(|p| p[axis].abs() < 1e-3)+ };+ assert!(on(&app, 2), "the ground grid");+ app.viewport.hover = Some(Hit::Plane { name: "XZ".into(), point: Vec3::ZERO });+ assert!(on(&app, 2), "a plane under the pointer outside Create Sketch moves nothing");+ app.start("sketch.create");+ assert!(on(&app, 1), "on XZ, under the pointer");+ let key = sketch_scene_key(&app, &projection(&app, rect));+ app.viewport.hover = None;+ app.viewport.hover_plane = Some("YZ".into());+ assert_ne!(sketch_scene_key(&app, &projection(&app, rect)), key, "the cached grid is drawn again");+ assert!(on(&app, 0), "on YZ, whose browser row is under the pointer");+ app.viewport.hover_plane = None;+ assert!(on(&app, 2), "back on the ground");+ // With the grid turned off, the preview still shows the plane, as an open sketch's grid does.+ app.ui.show_grid = false;+ assert!(grid(&app).is_empty());+ app.viewport.hover = Some(Hit::Plane { name: "XZ".into(), point: Vec3::ZERO });+ assert!(on(&app, 1), "the preview grid is drawn even with the grid off");+ }+/// The grid's centre is where the view's axis meets the ground, but at a grazing slant (a/// camera 10 mm above the ground, looking almost along it) no further than the grid's/// half-size from the point under the target, so the ground in front of the camera keeps@@ -10600,6 +10668,48 @@ mod grid_plane_tests {assert!(sc.guide_trans.is_empty());}+ /// #6975: while Create Sketch waits for its plane, the plane under the pointer is lit, and a+ /// plane's browser row under the pointer lights it the same way.+ #[test]+ fn create_sketch_lights_the_plane_under_the_pointer_or_its_browser_row() {+ let mut app = SolveApp::new(solidcraft_engine::Session::default(), crate::Services::default());+ app.start("sketch.create");+ let plain = build_highlight(&app).xray;+ app.viewport.hover = Some(Hit::Plane { name: "XZ".into(), point: Vec3::ZERO });+ let lit = build_highlight(&app).xray;+ assert_ne!(lit, plain, "the plane under the pointer is lit");+ app.viewport.hover = None;+ let key = highlight_key(&app);+ app.viewport.hover_plane = Some("XZ".into());+ assert_ne!(highlight_key(&app), key, "the highlight is built again");+ assert_eq!(build_highlight(&app).xray, lit, "its browser row lights it the same way");+ }++ /// #6975: New Sketch shows the origin planes; the plane under the pointer, in the view or as+ /// its row in the browser, is the one previewed, and a click sketches on it.+ #[test]+ fn create_sketch_previews_the_hovered_plane_and_a_click_sketches_on_it() {+ let mut h = crate::scenario::Harness::new();+ h.app.tree.expanded.insert("origin".into());+ h.step(&json!({"start":"sketch.create"})).unwrap();+ assert_eq!(origin_planes(&h.app).len(), 3, "the origin planes are in the scene while choosing");+ assert!(previewed_sketch_plane(&h.app).is_none(), "nothing under the pointer yet");+ // Whether `plane` lies on origin plane `name`.+ let on = |plane: Option<Plane>, name: &str| {+ plane.zip(Plane::named(name)).is_some_and(|(a, b)| a.normal().dot(b.normal()).abs() > 0.999 && a.origin.dot(b.normal()).abs() < 1e-9)+ };+ h.step(&json!({"move":{"plane":"YZ"}})).unwrap();+ assert!(on(previewed_sketch_plane(&h.app), "YZ"), "YZ under the pointer in the view");+ let row = crate::scenario::handle_at("row:XY").expect("the XY row");+ h.call("ui.move", json!({"x": row.x, "y": row.y}));+ assert_eq!(h.app.viewport.hover_plane.as_deref(), Some("XY"));+ assert!(on(previewed_sketch_plane(&h.app), "XY"), "XY under the pointer in the browser");+ h.step(&json!({"click":{"plane":"XZ"}})).unwrap();+ let state = h.app.session.world_state();+ assert!(on(state.sketches.first().map(|s| s.plane), "XZ"), "the click sketches on XZ");+ assert!(previewed_sketch_plane(&h.app).is_none(), "the open sketch has its own grid");+ }+/// A finished sketch is depth-tested unless the preference draws reference geometry through/// bodies; switching it redraws the sketch scene while nothing else changes.#[test]diff --git a/docs/book/src/guide/sketching.md b/docs/book/src/guide/sketching.mdindex 118b574bcc..2d58929bc4 100644--- a/docs/book/src/guide/sketching.md+++ b/docs/book/src/guide/sketching.md@@ -3,6 +3,9 @@A sketch lies on a plane: an origin plane, a construction plane or a planar face. Start one with**Create Sketch** (`sketch.create`) and click a plane, or pick a face first. Right-clicking aplane, in the view or on its browser row, offers **Create Sketch** on that plane too.+While Create Sketch waits for its plane, the origin planes are shown, and the plane under the+pointer (in the view, or its row in the browser) lights up and takes the grid, so you see which+plane the sketch will lie on before you click.**Finish Sketch** returns to the model. **Cancel Sketch** (`sketch.cancel`) returns withoutkeeping what changed since the sketch was created or opened for editing: a new sketch is removed,an edited one goes back to how it was, as one step that Undo brings back.