Sketch: align view to plane when editing a rotated component sketch (#9029) (#9042)

* claim: #9029

Co-authored-by: Cursor <cursoragent@cursor.com>

* Square sketch axes when looking at a turned plane (#9029)

Look At only yaw-aligned sketches facing world up/down, so a sketch on a rotated component stayed tilted on screen. Roll (or yaw at the poles) so the sketch's x runs right and y is upright.

Co-authored-by: Cursor <cursoragent@cursor.com>

---------

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
SilentWolf75
2026-10-30 04:27:24 +00:00
committed by GitHub
parent ef628d1c86
commit b8e93270b4
2 changed files with 73 additions and 6 deletions
+33 -6
View File
@@ -3828,16 +3828,43 @@ fn look_at_sketch_from(app: &mut SolveApp, again: bool) {
let st = app.session.world_state();
if let Some(ss) = app.session.active_sketch.and_then(|id| st.sketch(id)) {
let n = facing(app, ss.plane.normal(), again);
let mut to = app.cam.looking_from(n);
// Keep the sketch's own x axis to the right when looking straight down or up.
if n.z.abs() > 0.999 {
let x = ss.plane.x;
to.yaw = if n.z > 0.0 { (-x.y).atan2(x.x) } else { x.y.atan2(x.x) };
}
let to = square_sketch_axes(app.cam.looking_from(n), ss.plane.x, ss.plane.y);
app.animate_to(onto_sketch(to, ss));
}
}
/// Orient the camera so the sketch's x axis runs right and its y axis is as upright as it can
/// be (#9029). Facing the plane alone left a sketch on a turned component tilted on screen.
/// Along world up/down, yaw turns the image (roll stays 0, as Top/Bottom views do); elsewhere
/// roll squares the axes.
pub(crate) fn square_sketch_axes(mut cam: solidcraft_engine::render::Camera, sketch_x: Vec3, sketch_y: Vec3) -> solidcraft_engine::render::Camera {
let b = cam.back();
let in_view = |v: Vec3| v - b * v.dot(b);
let Some(mut x) = in_view(sketch_x).normalized() else {
return cam;
};
// Prefer the sense of x that keeps sketch y pointing up on screen.
if in_view(sketch_y).dot(b.cross(x)) < 0.0 {
x = -x;
}
if b.dot(cam.up_dir()).abs() > 0.999 {
// Looking straight up or down: yaw alone turns the image about the view.
let framed = cam.up.to_frame(x);
let yaw = if b.dot(cam.up_dir()) > 0.0 { (-framed.y).atan2(framed.x) } else { framed.y.atan2(framed.x) };
if yaw.is_finite() {
cam.yaw = yaw;
cam.roll = 0.0;
}
return cam;
}
let (right, up, _) = cam.basis();
let roll = x.dot(up).atan2(x.dot(right));
if roll.is_finite() {
cam.roll = roll;
}
cam
}
/// `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
+40
View File
@@ -91,3 +91,43 @@ fn the_viewport_menu_looks_at_a_plane_under_the_cursor() {
let normal = solidcraft_engine::geom::Plane::named("XZ").unwrap().normal();
assert!((h.app.cam.back().cross(normal)).len() < 1e-6, "facing the plane along its normal: {:?}", h.app.cam.back());
}
/// #9029: Look At on a non-horizontal sketch squares its axes to the screen (not only faces it).
#[test]
fn look_at_squares_a_vertical_sketch_to_the_screen() {
let mut h = Harness::new();
h.app.home.open = false;
h.app.preferences.look_at_anim = false;
h.app.run("sketch.create", json!({"plane": "XZ", "name": "Wall"})).unwrap();
h.app.run("sketch.rectangle.two_point", json!({"p0": [0, 0], "p1": [40, 20]})).unwrap();
crate::dialogs::look_at_sketch(&mut h.app);
let plane = h.app.session.world_state().sketch(h.app.session.active_sketch.unwrap()).unwrap().plane;
let (right, up, back) = h.app.cam.basis();
assert!((back.cross(plane.normal())).len() < 1e-6, "facing the sketch: back={back:?} n={:?}", plane.normal());
assert!(right.dot(plane.x).abs() > 0.999, "sketch x to the right: right={right:?} x={:?}", plane.x);
assert!(up.dot(plane.y) > 0.999, "sketch y upright: up={up:?} y={:?}", plane.y);
}
/// #9029: editing a sketch after its component is turned faces the world plane and squares axes.
#[test]
fn editing_a_sketch_in_a_turned_component_faces_its_world_plane() {
let mut h = Harness::new();
h.app.home.open = false;
h.app.preferences.look_at_anim = false;
h.app.run("component.create", json!({"name": "Part"})).unwrap();
h.app.run("sketch.create", json!({"plane": "XY", "name": "Face"})).unwrap();
h.app.run("sketch.rectangle.two_point", json!({"p0": [0, 0], "p1": [20, 10]})).unwrap();
h.app.run("sketch.finish", json!({})).unwrap();
let occ = h.app.session.doc.occurrences.iter().find(|o| h.app.session.doc.components.iter().any(|c| c.id == o.component && c.name == "Part"));
let Some(occ) = occ.cloned() else { panic!("Part occurrence") };
h.app.run("occurrence.ground", json!({"occurrence": occ.id, "grounded": false})).unwrap();
// Stand the part up: 90° about X so its XY faces world +Y.
h.app.run("occurrence.move", json!({"occurrence": occ.id, "axis": [1, 0, 0], "angle": "90 deg", "capture": true})).unwrap();
let id = h.app.session.doc.find_feature("Face").map(|f| f.id).unwrap();
h.app.edit_sketch(id);
let plane = h.app.session.world_state().sketch(id).unwrap().plane;
let (right, up, back) = h.app.cam.basis();
assert!((back.cross(plane.normal())).len() < 1e-5, "facing world plane: back={back:?} n={:?}", plane.normal());
assert!(right.dot(plane.x).abs() > 0.99, "x right: {right:?} vs {:?}", plane.x);
assert!(up.dot(plane.y) > 0.99, "y up: {up:?} vs {:?}", plane.y);
}