diff --git a/crates/ui-egui/src/dialogs.rs b/crates/ui-egui/src/dialogs.rs index ff349bd76..f177a6b1f 100644 --- a/crates/ui-egui/src/dialogs.rs +++ b/crates/ui-egui/src/dialogs.rs @@ -2824,7 +2824,7 @@ fn hole_defaults() -> Kind { /// Start a sketch on a picked plane or planar face and turn the view to face it. pub fn start_sketch(app: &mut SolveApp, sel: &Sel) -> Result<(), String> { let mut params = match sel { - Sel::Plane { name } => json!({ "plane": name }), + Sel::Plane { name } => json!({ "plane": shown_origin_plane(app, name).unwrap_or_else(|| json!(name)) }), Sel::Face { .. } => json!({ "plane": crate::sketch_attachment_ui::face_plane(app, sel).ok_or("Selected face placement no longer exists")? }), _ => return Err("Select an origin plane, construction plane or planar body face".into()), }; @@ -2861,17 +2861,24 @@ fn flip_toward_viewer(app: &SolveApp, name: &str) -> Option<&'static str> { Some(if plane.y.dot(up) > 1e-9 { "y" } else { "x" }) } -/// The world plane of an origin plane or a construction plane by name. +/// The origin plane `name` as the viewport and the browser show it and the user picks it: the +/// design's, in the world, written out for `sketch.create`, which puts it into the active +/// component's frame once. By name it would be the active component's own plane, and once that +/// component has moved (a stood-up import is turned a quarter about X) its XY is the world's XZ: +/// the sketch, the view turning to it and the extrude all went the wrong way. `None` while the +/// active component sits at the origin (the name says the same) or for a construction plane. +fn shown_origin_plane(app: &SolveApp, name: &str) -> Option { + let p = solidcraft_engine::geom::Plane::named(name)?; + if app.session.doc.find_feature(name).is_some() || solidcraft_engine::doc::is_identity(&app.session.active_component_frame()) { + return None; + } + Some(json!({"origin": [0.0, 0.0, 0.0], "x_dir": pt(p.x), "y_dir": pt(p.y)})) +} + +/// The world plane of an origin plane or a construction plane by name, where it is shown. fn plane_by_name(app: &SolveApp, name: &str) -> Option { if let Some(p) = solidcraft_engine::geom::Plane::named(name) { - // In an active component its origin planes are the component's. - let m = app.session.active_component_frame(); - let (o, x, y) = ( - solidcraft_engine::doc::apply_point(&m, p.origin), - solidcraft_engine::doc::apply_vector(&m, p.x), - solidcraft_engine::doc::apply_vector(&m, p.y), - ); - return solidcraft_engine::geom::Plane::new(o, x, y); + return Some(p); } let st = app.session.world_state(); st.construct.iter().find(|c| c.name == name).and_then(|c| match &c.geom { diff --git a/crates/ui-egui/tests/stand_up_offer.rs b/crates/ui-egui/tests/stand_up_offer.rs index e767d426e..a1fa59462 100644 --- a/crates/ui-egui/tests/stand_up_offer.rs +++ b/crates/ui-egui/tests/stand_up_offer.rs @@ -147,3 +147,61 @@ fn old_preferences_files_still_load() { let empty: solidcraft_ui_egui::prefs::Prefs = serde_json::from_str("{}").unwrap(); assert!(empty.offer_stand_up); } + +type V3 = solidcraft_engine::geom::Vec3; + +/// A stood-up SolidWorks file (its component still the active one), a sketch started by +/// `start`, a rectangle far from the part at x 200..220, y 0..10 on it, extruded 15 mm: the +/// sketch's world normal, the direction the view turned to look from, and the new body's bounds. +fn sketched_and_extruded(start: impl FnOnce(&mut Harness)) -> (V3, V3, solidcraft_engine::geom::Aabb3) { + let mut h = opened_from("'SolidWorks 2021','SolidWorks 2021'", |h| h.app.preferences.look_at_anim = false); + // Stand it up through the offer when one is waiting (a SolidWorks file is offered, not turned silently). + if h.app.stand_up.is_some() { + solidcraft_ui_egui::stand_up::stand_up(&mut h.app).unwrap(); + h.frames(2); + } + assert!((height(&h) - 40.0).abs() < 1e-6, "stood up: {}", height(&h)); + assert_ne!(h.app.session.active_component, 0, "the import's component is the one worked in"); + let before: Vec = h.app.session.world_state().bodies.iter().map(|b| b.name.clone()).collect(); + start(&mut h); + h.frames(2); + let sketch = h.app.session.active_sketch.expect("a sketch is open"); + let normal = h.app.session.world_state().sketch(sketch).unwrap().plane.normal(); + let back = h.app.cam.back(); + h.app.run("sketch.rectangle.two_point", json!({"p0": [200, 0], "p1": [220, 10]})).unwrap(); + h.app.run("solid.extrude", json!({"distance": 15})).unwrap(); + let world = h.app.session.world_state(); + let made: Vec<_> = world.bodies.iter().filter(|b| !before.contains(&b.name)).collect(); + assert_eq!(made.len(), 1); + (normal, back, made[0].mesh().bounds()) +} + +fn near(a: V3, b: [f64; 3]) -> bool { + a.dist(V3::new(b[0], b[1], b[2])) < 1e-6 +} + +#[test] +fn a_sketch_on_the_xy_plane_shown_after_standing_up_extrudes_up() { + // The origin planes the viewport and browser show are the design's: the XY picked there + // stays flat, the view looks down on it and the extrude goes up, though the stood-up + // component's own XY is now upright. + let (normal, back, b) = sketched_and_extruded(|h| { + solidcraft_ui_egui::dialogs::start_sketch(&mut h.app, &solidcraft_engine::Sel::Plane { name: "XY".into() }).unwrap(); + }); + assert!(near(normal, [0.0, 0.0, 1.0]), "faces up: {normal:?}"); + assert!((back.z - 1.0).abs() < 1e-3, "looks down on it: {back:?}"); + assert!(near(b.min, [200.0, 0.0, 0.0]) && near(b.max, [220.0, 10.0, 15.0]), "{b:?}"); +} + +#[test] +fn a_sketch_on_the_stood_up_components_own_xy_is_turned_once() { + // By name, a command's XY is the active component's own: upright now, facing -Y. The + // sketch, the view and the extrude all follow it, the stand-up applied once. + let (normal, back, b) = sketched_and_extruded(|h| { + h.app.run("sketch.create", json!({"plane": "XY"})).unwrap(); + solidcraft_ui_egui::dialogs::look_at_sketch(&mut h.app); + }); + assert!(near(normal, [0.0, -1.0, 0.0]), "faces -Y: {normal:?}"); + assert!((back.y + 1.0).abs() < 1e-3, "looks at it from -Y: {back:?}"); + assert!(near(b.min, [200.0, -15.0, 0.0]) && near(b.max, [220.0, 0.0, 10.0]), "{b:?}"); +}