diff --git a/crates/ui-egui/src/dialogs.rs b/crates/ui-egui/src/dialogs.rs index 347005c5d..dbd15b403 100644 --- a/crates/ui-egui/src/dialogs.rs +++ b/crates/ui-egui/src/dialogs.rs @@ -3055,7 +3055,7 @@ fn shown_origin_plane(app: &SolveApp, name: &str) -> Option { } /// The world plane of an origin plane or a construction plane by name, where it is shown. -fn plane_by_name(s: &Session, name: &str) -> Option { +pub(crate) fn plane_by_name(s: &Session, name: &str) -> Option { if let Some(p) = solidcraft_engine::geom::Plane::named(name) { return Some(p); } diff --git a/crates/ui-egui/src/file_picker.rs b/crates/ui-egui/src/file_picker.rs index c0f31508e..a61cbc122 100644 --- a/crates/ui-egui/src/file_picker.rs +++ b/crates/ui-egui/src/file_picker.rs @@ -388,4 +388,27 @@ mod tests { h.frames(2); assert_eq!(*asked.borrow(), [vec!["csv", "json"], vec!["png", "jpg", "jpeg"]]); } + + /// #4717: a canvas placed with a click on the origin of a sketch on XY, seen from the top, + /// lies on XY. The origin's XZ square is under the cursor there too, edge-on, and the image + /// went onto it, out of sight. + #[test] + fn a_canvas_clicked_at_the_sketch_origin_lies_where_it_can_be_seen() { + let mut h = crate::scenario::Harness::new(); + h.app.run("sketch.create", json!({"plane": "XY"})).unwrap(); + for step in [ + json!({"make_image": "pic.png"}), + json!({"choose_file": "pic.png"}), + json!({"call": "ui.view", "params": {"view": "top"}}), + json!({"start": "canvas.insert"}), + json!({"click": {"world": [0, 0, 0]}}), + ] { + h.step(&step).unwrap(); + } + let doc = &h.app.session.doc; + let canvas = doc.canvases.first().expect("the canvas is placed"); + let (vals, _) = doc.param_values(); + let n = doc.canvas_plane(&vals, canvas).unwrap().normal(); + assert!(n.z.abs() > 0.99, "the canvas lies on {n:?}, not on XY"); + } } diff --git a/crates/ui-egui/src/sketch_tools.rs b/crates/ui-egui/src/sketch_tools.rs index 7e6c14b67..358886db5 100644 --- a/crates/ui-egui/src/sketch_tools.rs +++ b/crates/ui-egui/src/sketch_tools.rs @@ -851,9 +851,13 @@ pub fn on_click(app: &mut SolveApp, tool: &mut Tool, proj: &Proj, pos: Pos2) { Mode::ModelRef if matches!(cmd, "canvas.insert" | "canvas.decal") => { let Some(path) = IMAGE.with(|i| i.borrow().clone()) else { return }; let decal = cmd == "canvas.decal"; + // A plane the view shows edge-on can't show the image: at the origin its square is + // under the cursor too, and the image went onto it out of sight (#4717). + let (_, ray) = proj.ray(pos); + let seen = |name: &str| crate::dialogs::plane_by_name(&app.session, name).is_some_and(|pl| pl.normal().dot(ray).abs() > 0.2 * ray.len()); let target = hits.iter().find_map(|h| match h { Hit::Face { point, .. } => Some((json!({"face": [point.x, point.y, point.z]}), *point)), - Hit::Plane { name, point } if !decal => Some((json!(name), *point)), + Hit::Plane { name, point } if !decal && seen(name) => Some((json!(name), *point)), _ => None, }); // Empty space while sketching: the sketch plane.