SolidCraft: an open-source parametric 3D CAD application built in pure Rust. Desktop, web, and automation through MCP.
Look At frames the sketch's geometry, not its plane's origin (#3198) (#3199)
fugazistera3f28c0b89dc
diff --git a/crates/ui-egui/src/context_menu.rs b/crates/ui-egui/src/context_menu.rsindex b71b08e23b..871e4e600d 100644--- a/crates/ui-egui/src/context_menu.rs+++ b/crates/ui-egui/src/context_menu.rs@@ -1233,8 +1233,7 @@ pub fn run_item(app: &mut SolveApp, item: &Item, at: Pos2) {"ui.lookAt" => {let st = app.session.world_state();if let Some(ss) = id_of(&p, "sketch").and_then(|id| st.sketch(id)) {- let mut to = app.cam.looking_from(ss.plane.normal());- to.target = ss.plane.origin;+ let to = crate::dialogs::onto_sketch(app.cam.looking_from(ss.plane.normal()), ss);app.animate_to(to);}}diff --git a/crates/ui-egui/src/dialogs.rs b/crates/ui-egui/src/dialogs.rsindex 532279f4e8..a815204a58 100644--- a/crates/ui-egui/src/dialogs.rs+++ b/crates/ui-egui/src/dialogs.rs@@ -3689,9 +3689,29 @@ fn look_at_sketch_from(app: &mut SolveApp, again: bool) {let x = ss.plane.x;to.yaw = if n.z > 0.0 { (-x.y).atan2(x.x) } else { x.y.atan2(x.x) };}+ app.animate_to(onto_sketch(to, ss));+ }+}++/// `to` (already facing the sketch) moved onto sketch `ss` (#3198): centred on its geometry, and+/// zoomed out when that would not fit in the view; it never zooms in, so a zoom the user chose+/// stays. A sketch without geometry keeps its plane's origin. Centring on the origin alone left a+/// sketch drawn away from it (a part placed far from the origin, as imports often are) out of view.+pub(crate) fn onto_sketch(mut to: solidcraft_engine::render::Camera, ss: &solidcraft_engine::doc::SolvedSketch) -> solidcraft_engine::render::Camera {+ let Some((lo, hi)) = ss.sketch.bounds() else {to.target = ss.plane.origin;- app.animate_to(to);+ return to;+ };+ let mut b = Aabb3::default();+ for p in [lo, hi, solidcraft_engine::geom::Vec2::new(lo.x, hi.y), solidcraft_engine::geom::Vec2::new(hi.x, lo.y)] {+ b.add(ss.plane.to_world(p));+ }+ let keep = to.distance;+ to.fit(&b);+ if keep.is_finite() && keep > to.distance {+ to.distance = keep;}+ to}/// The side to look from at a plane with normal `n`: its own side, or with `again` the otherdiff --git a/crates/ui-egui/src/lib.rs b/crates/ui-egui/src/lib.rsindex 437f9fed0e..60870c71f8 100644--- a/crates/ui-egui/src/lib.rs+++ b/crates/ui-egui/src/lib.rs@@ -74,6 +74,8 @@ pub mod keymap;mod linked_source;#[cfg(test)]mod loft_rails_dialog_tests;+#[cfg(test)]+mod look_at_tests;mod material_card_review;#[cfg(test)]mod measurement_ui_tests;diff --git a/crates/ui-egui/src/look_at_tests.rs b/crates/ui-egui/src/look_at_tests.rsnew file mode 100644index 0000000000..753f8422c6--- /dev/null+++ b/crates/ui-egui/src/look_at_tests.rs@@ -0,0 +1,62 @@+//! Look At on a sketch (#3198): it faces the sketch and frames its geometry, not its plane's+//! origin, so a sketch drawn far from the origin (an imported part placed there) stays in view.++use serde_json::json;+use solidcraft_engine::geom::Vec3;++use crate::scenario::Harness;++/// A 20 × 30 rectangle at x 330–350, y 475–505 on the XY plane raised 8 mm, as an imported part+/// rebuilt where it was placed; the camera jumps straight to where Look At sends it.+fn far_sketch() -> (Harness, u64) {+ let mut h = Harness::new();+ h.app.home.open = false;+ h.app.preferences.look_at_anim = false;+ h.app.run("sketch.create", json!({"plane": "XY", "offset": 8, "name": "Far"})).unwrap();+ h.app.run("sketch.rectangle.two_point", json!({"p0": [330, 475], "p1": [350, 505]})).unwrap();+ h.app.run("sketch.finish", json!({})).unwrap();+ let id = h.app.session.doc.find_feature("Far").map(|f| f.id).unwrap();+ (h, id)+}++fn near(a: Vec3, b: Vec3) -> bool {+ (a - b).len() < 1e-6+}++/// The camera's view half-width (mm) the rectangle needs: its half diagonal.+const HALF_DIAGONAL: f64 = 18.027_756_377_319_946;++#[test]+fn the_menus_look_at_frames_a_sketch_far_from_the_origin() {+ let (mut h, id) = far_sketch();+ h.app.cam.distance = 20.0;+ let items = crate::context_menu::items(&h.app, &crate::context_menu::Target::Sketch { id });+ let look = items.iter().find(|i| i.id == "ui.lookAt").cloned().expect("the sketch menu has Look At");+ crate::context_menu::run_item(&mut h.app, &look, egui::Pos2::ZERO);+ assert!(near(h.app.cam.target, Vec3::new(340.0, 490.0, 8.0)), "centred on the rectangle, not the plane's origin: {:?}", h.app.cam.target);+ assert!((h.app.cam.back() - Vec3::new(0.0, 0.0, 1.0)).len() < 1e-6, "facing the sketch from above");+ let need = HALF_DIAGONAL / 0.4 * 1.15;+ assert!(h.app.cam.distance >= need - 1e-6, "zoomed out to fit it: {} < {need}", h.app.cam.distance);+}++#[test]+fn look_at_on_the_active_sketch_keeps_a_wider_zoom() {+ let (mut h, id) = far_sketch();+ h.app.session.active_sketch = Some(id);+ h.app.cam.distance = 5000.0;+ crate::dialogs::look_at_sketch(&mut h.app);+ assert!(near(h.app.cam.target, Vec3::new(340.0, 490.0, 8.0)), "{:?}", h.app.cam.target);+ assert_eq!(h.app.cam.distance, 5000.0, "it never zooms in on a zoom the user chose");+}++#[test]+fn an_empty_sketch_keeps_its_planes_origin() {+ let mut h = Harness::new();+ h.app.home.open = false;+ h.app.preferences.look_at_anim = false;+ h.app.run("sketch.create", json!({"plane": "XY", "offset": 8})).unwrap();+ h.app.cam.distance = 123.0;+ crate::dialogs::look_at_sketch(&mut h.app);+ assert!(near(h.app.cam.target, Vec3::new(0.0, 0.0, 8.0)), "{:?}", h.app.cam.target);+ assert_eq!(h.app.cam.distance, 123.0);+}