diff --git a/crates/engine/src/cmd/features.rs b/crates/engine/src/cmd/features.rs index c7d750fab..0a424d0b2 100644 --- a/crates/engine/src/cmd/features.rs +++ b/crates/engine/src/cmd/features.rs @@ -108,7 +108,7 @@ pub static COMMANDS: &[CommandSpec] = &[ CommandSpec::new("construct.plane.at_angle", "Plane at Angle", plane_angle) .at("SOLID", "CONSTRUCT") .icon("plane_angle") - .params("base: XY|XZ|YZ|plane name; axis: X|Y|Z|construction axis name (where it is now)|{origin, dir}; angle: expr; name?"), + .params("base: XY|XZ|YZ|plane name, or a planar face {face: [x,y,z], body?, index?, path?}; axis: X|Y|Z|construction axis name (where it is now)|{origin, dir}; angle: expr; name?"), CommandSpec::new("solid.split_face", "Split Face", split_face) .at("SOLID", "MODIFY") .icon("split_face") @@ -1634,11 +1634,19 @@ fn plane_offset(s: &mut Session, p: &Value) -> Result { fn plane_angle(s: &mut Session, p: &Value) -> Result { let cmd = "construct.plane.at_angle"; - let base = plane_param(s, p.get("base"), cmd)?; // The plane keeps the axis line as it is now. let (axis_origin, axis_dir, _) = axis_line(s, p, cmd)?; let angle = req_expr(cmd, p, "angle")?; check_expr(s, &angle, Kind::Angle, cmd, "angle")?; + // A planar body face: attached exactly, as a sketch on that face is, so the plane follows it. + if let Some(face) = p.get("base").filter(|b| b.get("face").is_some()) { + let base = super::sketch::face::capture(s, face, cmd)?; + super::sketch::face::capture_positions(s, &base)?; + let kind = FeatureKind::ConstructionPlane { plane: solidcraft_doc::PlaneRef::AtAngle { base: Box::new(base), axis_origin, axis_dir, angle } }; + // The capture is already in the authoring frame. + return add_feature_in(s, p, kind, false); + } + let base = plane_param(s, p.get("base"), cmd)?; add_feature( s, p, diff --git a/crates/engine/src/cmd/offset_plane_face_tests.rs b/crates/engine/src/cmd/offset_plane_face_tests.rs index 16687bf10..c0e52fc46 100644 --- a/crates/engine/src/cmd/offset_plane_face_tests.rs +++ b/crates/engine/src/cmd/offset_plane_face_tests.rs @@ -99,3 +99,59 @@ fn offset_plane_redefined_on_itself_is_refused() { run(&mut s, "timeline.redefine", redefine(json!({"face": [30, 0, 10]}))); assert!((plane(&s, "Side").origin.y + 7.0).abs() < 1e-9, "{:?}", plane(&s, "Side")); } + +#[test] +fn plane_at_angle_from_a_planar_face_follows_the_face() { + let mut s = Session::default(); + run(&mut s, "solid.box", json!({"length": 40, "width": 30, "height": 20, "name": "Base"})); + + // Top face (z = 20), 45 degrees about X axis. + run(&mut s, "construct.plane.at_angle", json!({"base": {"face": [5, 5, 20]}, "axis": "X", "angle": "45 deg", "name": "Slanted"})); + let slanted = plane(&s, "Slanted"); + // Base plane has normal Z and its origin on the top face, (0, 0, 20). Rotated 45 degrees + // about X: the origin is (0, -20/sqrt(2), 20/sqrt(2)) and the normal [0, -sin(45), cos(45)]. + let half = 1.0_f64 / 2.0_f64.sqrt(); + assert!(slanted.origin.x.abs() < 1e-9, "{slanted:?}"); + assert!((slanted.origin.y - -20.0 * half).abs() < 1e-9, "{slanted:?}"); + assert!((slanted.origin.z - 20.0 * half).abs() < 1e-9, "{slanted:?}"); + assert!((slanted.normal().y - (-half)).abs() < 1e-9, "{slanted:?}"); + assert!((slanted.normal().z - half).abs() < 1e-9, "{slanted:?}"); + + let feature = s.doc.find_feature("Slanted").unwrap(); + let FeatureKind::ConstructionPlane { plane: PlaneRef::AtAngle { base, .. } } = &feature.kind else { panic!("at_angle plane") }; + assert!(matches!(base.as_ref(), PlaneRef::Face { anchor: Some(_), .. }), "{base:?}"); + + // Height changes: the slanted plane moves with the top face (0, 0, 30). + run(&mut s, "timeline.edit", json!({"feature": "Base", "set": {"height": "30"}})); + let slanted2 = plane(&s, "Slanted"); + assert!(slanted2.origin.x.abs() < 1e-9, "{slanted2:?}"); + assert!((slanted2.origin.y - -30.0 * half).abs() < 1e-9, "{slanted2:?}"); + assert!((slanted2.origin.z - 30.0 * half).abs() < 1e-9, "{slanted2:?}"); + assert!((slanted2.normal().y - (-half)).abs() < 1e-9, "{slanted2:?}"); + assert!((slanted2.normal().z - half).abs() < 1e-9, "{slanted2:?}"); + + // Saved and reopened, the attachment still resolves. + let reopened = Session::new(Document::from_json(&s.doc.to_json()).unwrap()); + let reopened_slanted = plane(&reopened, "Slanted"); + assert!((reopened_slanted.origin.y - -30.0 * half).abs() < 1e-9, "{reopened_slanted:?}"); + assert!((reopened_slanted.origin.z - 30.0 * half).abs() < 1e-9, "{reopened_slanted:?}"); + assert!((reopened_slanted.normal().y - (-half)).abs() < 1e-9, "{reopened_slanted:?}"); +} + +#[test] +fn plane_at_angle_rejects_bad_face_picks_without_panicking() { + let mut s = Session::default(); + run(&mut s, "solid.box", json!({"length": 40, "width": 30, "height": 20})); + for base in [ + json!({"face": [100, 100, 100]}), + json!({"face": [1e300, 0, 0]}), + json!({"face": "top"}), + json!({"face": [5, 5]}), + json!({"face": [5, 5, 20], "index": 0}), + json!({"face": [5, 5, 20], "body": 7, "index": 0}), + ] { + let features = s.doc.features.len(); + assert!(s.execute("construct.plane.at_angle", &json!({"base": base, "axis": "X", "angle": "45 deg"})).is_err(), "{base}"); + assert_eq!(s.doc.features.len(), features, "{base}"); + } +} diff --git a/crates/ui-egui/src/dialogs.rs b/crates/ui-egui/src/dialogs.rs index bbbbeea5f..762879b80 100644 --- a/crates/ui-egui/src/dialogs.rs +++ b/crates/ui-egui/src/dialogs.rs @@ -109,7 +109,7 @@ crate::dialog_descriptor::dialog_descriptors! { FeatureKind::ConstructionPlane { plane: PlaneRef::Offset { distance, .. } }) { offset: String = "0 mm".into() => distance.to_string(), } => json!({"offset": offset}); - AnglePlane("construct.plane.at_angle", "PLANE AT ANGLE", [("Plane", PLANES, false), ("Axis", AXES, false)], + AnglePlane("construct.plane.at_angle", "PLANE AT ANGLE", [("Plane", PLANES | PLANAR_FACES, false), ("Axis", AXES, false)], FeatureKind::ConstructionPlane { plane: PlaneRef::AtAngle { angle, .. } }) { angle: String = "45 deg".into() => angle.to_string(), } => json!({"angle": angle}); @@ -3418,10 +3418,7 @@ fn dialog_commands(app: &SolveApp, d: &Dialog) -> Result, S } Kind::AnglePlane { .. } => { need(0, "a plane")?; - let base = match sels(d, 0).first() { - Some(Sel::Plane { name }) => name.clone(), - _ => return Err("pick an origin or construction plane".into()), - }; + let base = plane_arg(app, sels(d, 0).first())?; let axis = match sels(d, 1).first() { Some(Sel::Axis { name }) => json!(name), Some(x) => {