From a33619fd2b679b083bc264aef39c9776c5e8e087 Mon Sep 17 00:00:00 2001 From: leValdemar19 Date: Mon, 2 Nov 2026 00:13:22 +0000 Subject: [PATCH] Tangent Plane: line it up with a reference plane, at an angle (#8822) (#9002) * claim: #8822 * Tangent Plane: line it up with a reference plane, at an angle (#8822) `construct.plane.tangent` takes an optional `reference` (a plane or planar face) and `angle`. On a cylinder, cone or torus the plane touches where the face turns towards the reference, turned by the angle about the face's axis, on the side of the pick; on a sphere it is parallel to the reference. The plane follows the reference when it moves. The Tangent Plane dialog gets a Reference plane input and, once one is picked, an Angle field; editing the plane brings both back. Saved as a pending format change. Co-Authored-By: Claude Opus 5.5 --------- Co-authored-by: Claude Opus 5.5 --- .../pending/tangent-plane-reference.json | 10 ++++ crates/doc/src/assembly.rs | 5 +- crates/doc/src/clipboard.rs | 7 ++- crates/doc/src/construct.rs | 60 ++++++++++++++++++- crates/doc/src/document.rs | 13 +++- .../format/pending/tangent-plane-reference.rs | 5 ++ crates/doc/src/walk.rs | 18 +++++- crates/engine/src/cmd/construct.rs | 17 +++++- crates/engine/src/construct_tests.rs | 51 ++++++++++++++++ crates/engine/src/schema/construct.rs | 10 +++- crates/engine/src/schema/construct_tests.rs | 2 +- crates/ui-egui/src/dialogs.rs | 55 ++++++++++++++++- crates/ui-egui/src/dialogs_dense.rs | 6 ++ crates/ui-egui/src/timeline.rs | 2 +- 14 files changed, 246 insertions(+), 15 deletions(-) create mode 100644 assets/locales/pending/tangent-plane-reference.json create mode 100644 crates/doc/src/format/pending/tangent-plane-reference.rs diff --git a/assets/locales/pending/tangent-plane-reference.json b/assets/locales/pending/tangent-plane-reference.json new file mode 100644 index 000000000..5676f1af0 --- /dev/null +++ b/assets/locales/pending/tangent-plane-reference.json @@ -0,0 +1,10 @@ +{ + "en": { "Reference plane": "Reference plane" }, + "ar": { "Reference plane": "المستوى المرجعي" }, + "de": { "Reference plane": "Referenzebene" }, + "he": { "Reference plane": "מישור ייחוס" }, + "ja": { "Reference plane": "参照平面" }, + "ko": { "Reference plane": "참조 평면" }, + "vi": { "Reference plane": "Mặt phẳng tham chiếu" }, + "zh": { "Reference plane": "参考平面" } +} diff --git a/crates/doc/src/assembly.rs b/crates/doc/src/assembly.rs index 1ac32ad43..72c9ee26a 100644 --- a/crates/doc/src/assembly.rs +++ b/crates/doc/src/assembly.rs @@ -202,9 +202,12 @@ fn map_plane_ref(m: &Mat, p: &mut PlaneRef) { } // Named origin planes and construction planes are the component's own. PlaneRef::Origin { .. } | PlaneRef::Construction { .. } => {} - PlaneRef::Tangent { face, at } => { + PlaneRef::Tangent { face, at, reference, .. } => { *at = apply_point(m, *at); map_geo_ref(m, face); + if let Some(r) = reference { + map_geo_ref(m, r); + } } PlaneRef::Midplane { .. } | PlaneRef::TwoEdges { .. } diff --git a/crates/doc/src/clipboard.rs b/crates/doc/src/clipboard.rs index aa99f21c3..8101620a5 100644 --- a/crates/doc/src/clipboard.rs +++ b/crates/doc/src/clipboard.rs @@ -333,7 +333,12 @@ fn checked_plane(p: &PlaneRef, depth: usize) -> crate::Result<()> { checked_geo(b, depth + 1)?; checked_geo(c, depth + 1)?; } - PlaneRef::Tangent { face, .. } => checked_geo(face, depth + 1)?, + PlaneRef::Tangent { face, reference, .. } => { + checked_geo(face, depth + 1)?; + if let Some(r) = reference { + checked_geo(r, depth + 1)?; + } + } PlaneRef::AlongPath { path, .. } => checked_geo(path, depth + 1)?, _ => {} } diff --git a/crates/doc/src/construct.rs b/crates/doc/src/construct.rs index 6957e4708..d7e73925b 100644 --- a/crates/doc/src/construct.rs +++ b/crates/doc/src/construct.rs @@ -497,6 +497,48 @@ fn surface_normal(s: FaceSurface, p: Vec3) -> Option<(Vec3, Vec3)> { } } +/// A point over `s` where the face turns towards `towards` (a reference plane's normal): around +/// the axis of a cylinder, cone or torus, turned `angle` about it; straight at it on a sphere. Of +/// the two sides, the one `at` is on (#8822). +fn facing(s: FaceSurface, at: Vec3, towards: Vec3, angle: f64) -> Result { + let side = |d: Vec3, w: Vec3| if d.dot(w) < 0.0 { d * -1.0 } else { d }; + let around = |axis: Vec3, w: Vec3| -> Result<(Vec3, f64)> { + let d = (towards - axis * towards.dot(axis)) + .normalized() + .ok_or_else(|| bad("the reference is square to the face's axis: every plane tangent to the face meets it at a right angle"))?; + let d = d * angle.cos() + axis.cross(d) * angle.sin(); + let r = w - axis * w.dot(axis); + Ok((side(d, r), r.len())) + }; + if !angle.is_finite() { + return Err(bad("the angle is not a number")); + } + Ok(match s { + FaceSurface::Plane { .. } => at, + FaceSurface::Cylinder { origin, axis, radius } => { + let w = at - origin; + let (d, _) = around(axis.into(), w)?; + origin + axis * w.dot(axis) + d * radius + } + FaceSurface::Cone { apex, axis, .. } => { + let w = at - apex; + let (d, r) = around(axis.into(), w)?; + apex + axis * w.dot(axis) + d * r.max(1e-6) + } + // On the outer equator, where the tube faces away from the axis. + FaceSurface::Torus { center, axis, major, minor } => { + let (d, _) = around(axis.into(), at - center)?; + center + d * (major + minor) + } + FaceSurface::Sphere { center, radius } => { + if angle.abs() > 1e-12 { + return Err(bad("a sphere has no axis to turn the plane about: leave the angle out")); + } + center + side(towards, at - center) * radius + } + }) +} + fn face_surface_at(ctx: &Ctx, g: &GeoRef) -> Result<(FaceSurface, Vec3)> { match g { GeoRef::Face { at, name } => { @@ -637,9 +679,17 @@ pub(crate) fn resolve_geo_plane(ctx: &Ctx, p: &PlaneRef) -> Result { } Plane::new(pa, pb - pa, pc - pa).ok_or_else(degenerate) } - PlaneRef::Tangent { face, at } => { + PlaneRef::Tangent { face, at, reference, angle } => { let (s, _) = face_surface_at(ctx, face)?; - let (on, n) = surface_normal(s, *at).ok_or_else(|| bad("the point is on the face's axis"))?; + let at = match reference { + Some(r) => { + let towards = plane(ctx, r)?.normal().normalized().ok_or_else(degenerate)?; + let angle = angle.as_ref().map(|a| Document::eval_in(ctx.vals, a, Kind::Angle)).transpose()?.unwrap_or(0.0); + facing(s, *at, towards, angle)? + } + None => *at, + }; + let (on, n) = surface_normal(s, at).ok_or_else(|| bad("the point is on the face's axis"))?; Plane::from_normal(on, n).ok_or_else(degenerate) } PlaneRef::Perpendicular { plane: base, line: l } => { @@ -781,7 +831,11 @@ impl PlaneRef { PlaneRef::Midplane { a, b } | PlaneRef::TwoEdges { a, b } | PlaneRef::AxisPoint { axis: a, point: b } => vec![a.as_mut(), b.as_mut()], PlaneRef::Perpendicular { plane, line } => vec![plane.as_mut(), line.as_mut()], PlaneRef::ThreePoints { a, b, c } => vec![a.as_mut(), b.as_mut(), c.as_mut()], - PlaneRef::Tangent { face, .. } => vec![face.as_mut()], + PlaneRef::Tangent { face, reference, .. } => { + let mut v = vec![face.as_mut()]; + v.extend(reference.as_deref_mut()); + v + } PlaneRef::AlongPath { path, .. } => vec![path.as_mut()], PlaneRef::ThroughPoint { base, point } => { let mut refs = base.refs_mut(); diff --git a/crates/doc/src/document.rs b/crates/doc/src/document.rs index f48f083b8..73ba17464 100644 --- a/crates/doc/src/document.rs +++ b/crates/doc/src/document.rs @@ -101,8 +101,17 @@ pub enum PlaneRef { AxisPoint { axis: Box, point: Box }, /// Through three points. ThreePoints { a: Box, b: Box, c: Box }, - /// Tangent to a face (cylinder, cone, sphere, torus) where the point `at` lies over it. - Tangent { face: Box, at: Vec3 }, + /// Tangent to a face (cylinder, cone, sphere, torus) where the point `at` lies over it. With + /// a `reference` (a plane or planar face), it touches where the face turns towards the + /// reference instead, turned by `angle` about the face's axis, on the side of `at` (#8822). + Tangent { + face: Box, + at: Vec3, + #[serde(default, skip_serializing_if = "Option::is_none")] + reference: Option>, + #[serde(default, skip_serializing_if = "Option::is_none")] + angle: Option, + }, /// Square to an edge or sketch curve, a fraction `t` (expression, 0…1) of the way along it. AlongPath { path: Box, t: Expr }, /// Through a straight edge (or line, axis) and square to a plane (or planar face). diff --git a/crates/doc/src/format/pending/tangent-plane-reference.rs b/crates/doc/src/format/pending/tangent-plane-reference.rs new file mode 100644 index 000000000..5f8ce101b --- /dev/null +++ b/crates/doc/src/format/pending/tangent-plane-reference.rs @@ -0,0 +1,5 @@ +//! A tangent construction plane can save a `reference` (a plane or planar face) and an `angle` +//! (#8822): the plane touches the face where it turns towards the reference, turned by the angle +//! about the face's axis. An older design has neither, and its tangent planes touch where they +//! were picked, as before. The last release refuses a design that has them (it asks for an +//! update) instead of opening those planes at the picked point, away from their reference. diff --git a/crates/doc/src/walk.rs b/crates/doc/src/walk.rs index 542195ddc..cf7252552 100644 --- a/crates/doc/src/walk.rs +++ b/crates/doc/src/walk.rs @@ -335,6 +335,7 @@ impl Walk for PlaneRef { p = base; } PlaneRef::AlongPath { t, .. } => return f(("Distance (0 to 1)", t, Kind::Unitless, ExprRole::Input)), + PlaneRef::Tangent { angle: Some(angle), .. } => return f(("Angle", angle, Kind::Angle, ExprRole::Input)), // A plane seen from behind keeps its base's expressions (a sketch made with // `flip` on an offset plane did not follow the offset's parameter). PlaneRef::Flipped { base, .. } | PlaneRef::ThroughPoint { base, .. } => p = base, @@ -361,6 +362,7 @@ impl Walk for PlaneRef { p = base; } PlaneRef::AlongPath { t, .. } => return f(("Distance (0 to 1)", t, Kind::Unitless, ExprRole::Input)), + PlaneRef::Tangent { angle: Some(angle), .. } => return f(("Angle", angle, Kind::Angle, ExprRole::Input)), // A plane seen from behind keeps its base's expressions (a sketch made with // `flip` on an offset plane did not follow the offset's parameter). PlaneRef::Flipped { base, .. } | PlaneRef::ThroughPoint { base, .. } => p = base, @@ -415,7 +417,13 @@ impl PlaneRef { b.walk_feature_refs(f, depth + 1); c.walk_feature_refs(f, depth + 1); } - Self::Tangent { face, .. } | Self::AlongPath { path: face, .. } => face.walk_feature_refs(f, depth + 1), + Self::Tangent { face, reference, .. } => { + face.walk_feature_refs(f, depth + 1); + if let Some(r) = reference { + r.walk_feature_refs(f, depth + 1); + } + } + Self::AlongPath { path, .. } => path.walk_feature_refs(f, depth + 1), Self::Origin { .. } | Self::Custom { .. } | Self::Placed { .. } | Self::Face { .. } => {} } } @@ -446,7 +454,13 @@ impl PlaneRef { b.walk_feature_refs_mut(f, depth + 1); c.walk_feature_refs_mut(f, depth + 1); } - Self::Tangent { face, .. } | Self::AlongPath { path: face, .. } => face.walk_feature_refs_mut(f, depth + 1), + Self::Tangent { face, reference, .. } => { + face.walk_feature_refs_mut(f, depth + 1); + if let Some(r) = reference { + r.walk_feature_refs_mut(f, depth + 1); + } + } + Self::AlongPath { path, .. } => path.walk_feature_refs_mut(f, depth + 1), Self::Origin { .. } | Self::Custom { .. } | Self::Placed { .. } | Self::Face { .. } => {} } } diff --git a/crates/engine/src/cmd/construct.rs b/crates/engine/src/cmd/construct.rs index 610de9e94..8bbb72f81 100644 --- a/crates/engine/src/cmd/construct.rs +++ b/crates/engine/src/cmd/construct.rs @@ -141,7 +141,7 @@ pub static COMMANDS: &[CommandSpec] = &[ .icon("plane_tangent") .contract(&PLANE_TANGENT_CONTRACT) .output(output_schema_for::) - .params("face: ref (cylinder, cone, sphere or torus); at?: [x,y,z] where it touches (default: the face pick); name?"), + .params("face: ref (cylinder, cone, sphere or torus); at?: [x,y,z] where it touches (default: the face pick); reference?: plane or planar face ref (the plane touches where the face turns towards it: parallel to it, on the side of `at`); angle?: expr (with reference, default 0 deg: turned that far about the face's axis; not on a sphere); name?"), CommandSpec::new("construct.plane.along_path", "Plane Along Path", crate::checked!("construct.plane.along_path", plane_along)) .at("SOLID", "CONSTRUCT") .icon("plane_along_path") @@ -408,7 +408,20 @@ fn plane_tangent(s: &mut Session, p: &Value) -> Result { (None, GeoRef::Face { at, .. }) => *at, _ => return Err(bad(cmd, "`at` is required: where the plane touches the face")), }; - plane(s, p, PlaneRef::Tangent { face: Box::new(face), at }) + let reference = match p.get("reference").filter(|v| !v.is_null()) { + Some(_) => Some(Box::new(geo(s, p.get("reference"), cmd, "reference")?)), + None => None, + }; + let angle = match p.get("angle").filter(|v| !v.is_null()) { + Some(_) if reference.is_none() => return Err(bad(cmd, "`angle` turns the plane from a `reference`: give one")), + Some(_) => { + let angle = req_expr(cmd, p, "angle")?; + check_expr(s, &angle, solidcraft_doc::expr::Kind::Angle, cmd, "angle")?; + Some(angle) + } + None => None, + }; + plane(s, p, PlaneRef::Tangent { face: Box::new(face), at, reference, angle }) } fn plane_along(s: &mut Session, p: &Value) -> Result { diff --git a/crates/engine/src/construct_tests.rs b/crates/engine/src/construct_tests.rs index f8fc8b401..8bea30fe2 100644 --- a/crates/engine/src/construct_tests.rs +++ b/crates/engine/src/construct_tests.rs @@ -392,6 +392,57 @@ fn plane_tangent_to_a_cylinder() { assert!(parallel(u.normal(), Vec3::Z) && on_plane(&u, Vec3::new(3.0, 3.0, 65.0)), "{u:?}"); } +/// #8822: with a reference plane or planar face, the plane touches where the face turns towards +/// the reference (parallel to it, on the side of the pick), turned by an angle about the axis, +/// and follows the reference when it moves. +#[test] +fn plane_tangent_lined_up_with_a_reference() { + let mut s = part(); + // Picked 60° round the cylinder (axis Z through (60, 0)); the XZ plane faces ±Y. + let pick = json!([62.5, 4.330127, 5]); + run(&mut s, "construct.plane.tangent", json!({"face": {"face": pick}, "reference": {"plane": "XZ"}, "name": "T"})); + let t = plane(&s, "T"); + assert!(parallel(t.normal(), Vec3::Y) && on_plane(&t, Vec3::new(60.0, 5.0, 9.0)), "{t:?}"); + // Turned 30° about the axis: still on the pick's side. + run(&mut s, "construct.plane.tangent", json!({"face": {"face": pick}, "reference": {"plane": "XZ"}, "angle": "30 deg", "name": "A"})); + let a = plane(&s, "A"); + let d = Vec3::new(-0.5, 3f64.sqrt() / 2.0, 0.0); + assert!(parallel(a.normal(), d) && on_plane(&a, Vec3::new(60.0, 0.0, 2.0) + d * 5.0), "{a:?}"); + // A planar face of the box (x = 40, facing X) as the reference. + run(&mut s, "construct.plane.tangent", json!({"face": {"face": pick}, "reference": {"face": [40, 15, 10]}, "name": "F"})); + let f = plane(&s, "F"); + assert!(parallel(f.normal(), Vec3::X) && on_plane(&f, Vec3::new(65.0, 1.0, 1.0)), "{f:?}"); + // The torus (axis Z through (0, 60)): on its outer equator. + run(&mut s, "construct.plane.tangent", json!({"face": {"face": [0, 72, 0]}, "reference": {"plane": "XZ"}, "name": "R"})); + let r = plane(&s, "R"); + assert!(parallel(r.normal(), Vec3::Y) && on_plane(&r, Vec3::new(3.0, 72.0, 1.0)), "{r:?}"); + // The sphere (centre (0, 0, 60)), picked below its middle: under it, parallel to XY. + run(&mut s, "construct.plane.tangent", json!({"face": {"face": [3, 0, 56]}, "reference": {"plane": "XY"}, "name": "U"})); + let u = plane(&s, "U"); + assert!(parallel(u.normal(), Vec3::Z) && on_plane(&u, Vec3::new(1.0, 1.0, 55.0)), "{u:?}"); + + // A reference built on a parameter: the tangent plane follows it. + run(&mut s, "parameters.add", json!({"name": "turn", "expression": "0 deg"})); + run(&mut s, "construct.plane.at_angle", json!({"base": "XZ", "axis": "Z", "angle": "turn", "name": "Ref"})); + run(&mut s, "construct.plane.tangent", json!({"face": {"face": pick}, "reference": {"plane": "Ref"}, "name": "M"})); + assert!(on_plane(&plane(&s, "M"), Vec3::new(60.0, 5.0, 1.0))); + run(&mut s, "parameters.change", json!({"name": "turn", "expression": "-90 deg"})); + let m = plane(&s, "M"); + assert!(parallel(m.normal(), Vec3::X) && on_plane(&m, Vec3::new(65.0, 2.0, 1.0)), "{m:?}"); + + // What has no answer is refused and adds nothing. + let count = construction_planes(&s).len(); + for (p, why) in [ + (json!({"face": {"face": pick}, "reference": {"plane": "XY"}}), "a reference square to the axis"), + (json!({"face": {"face": [3, 0, 56]}, "reference": {"plane": "XY"}, "angle": "10 deg"}), "an angle on a sphere"), + (json!({"face": {"face": pick}, "angle": "10 deg"}), "an angle without a reference"), + (json!({"face": {"face": pick}, "reference": {"plane": "XZ"}, "angle": "2 mm"}), "a length for the angle"), + ] { + assert!(s.execute("construct.plane.tangent", &p).is_err(), "{why}"); + } + assert_eq!(construction_planes(&s).len(), count); +} + #[test] fn plane_along_a_path() { let mut s = part(); diff --git a/crates/engine/src/schema/construct.rs b/crates/engine/src/schema/construct.rs index 0e138867c..a670fcf0e 100644 --- a/crates/engine/src/schema/construct.rs +++ b/crates/engine/src/schema/construct.rs @@ -8,7 +8,7 @@ use schemars::JsonSchema; use serde::Deserialize; -use super::types::{ConstructionCommon, GeoRefParam, NumberExpr, PlaneCommon, Point3, feature_common, present}; +use super::types::{AngleExpr, ConstructionCommon, GeoRefParam, NumberExpr, PlaneCommon, Point3, feature_common, present}; /// Add a construction axis along the axis of a cylinder, cone or torus face, or of a circular /// edge. @@ -214,6 +214,14 @@ pub struct PlaneTangentParams { /// otherwise. #[serde(default)] pub at: Option, + /// A plane or planar face to line the plane up with: it touches where the face turns + /// towards the reference, parallel to it, on the side of `at`. + #[serde(default)] + pub reference: Option, + /// With `reference`, how far the plane is turned from it about the face's axis. Not on a + /// sphere. Default: 0 deg. + #[serde(default)] + pub angle: Option, #[serde(flatten, deserialize_with = "feature_common")] pub common: PlaneCommon, } diff --git a/crates/engine/src/schema/construct_tests.rs b/crates/engine/src/schema/construct_tests.rs index 106b985f4..f2d7a56ba 100644 --- a/crates/engine/src/schema/construct_tests.rs +++ b/crates/engine/src/schema/construct_tests.rs @@ -27,7 +27,7 @@ fn schemas_and_the_key_check_agree() { assert_keys(input_schema_for::(), &["axis", "name", "participants", "point"]); assert_keys(input_schema_for::(), &["a", "b", "name", "participants"]); assert_keys(input_schema_for::(), &["a", "b", "c", "name", "participants"]); - assert_keys(input_schema_for::(), &["at", "face", "name", "participants"]); + assert_keys(input_schema_for::(), &["angle", "at", "face", "name", "participants", "reference"]); assert_keys(input_schema_for::(), &["name", "participants", "path", "t"]); assert_keys(input_schema_for::(), &["line", "name", "participants", "plane"]); } diff --git a/crates/ui-egui/src/dialogs.rs b/crates/ui-egui/src/dialogs.rs index a69fde8ad..755ae3bd5 100644 --- a/crates/ui-egui/src/dialogs.rs +++ b/crates/ui-egui/src/dialogs.rs @@ -125,6 +125,11 @@ crate::dialog_descriptor::dialog_descriptors! { FeatureKind::ConstructionPlane { plane: PlaneRef::AtAngle { angle, .. } }) { angle: String = "45 deg".into() => shown_expr(angle, ValueKind::Angle), } => json!({"angle": angle}); + // With a reference plane, the plane touches where the face turns towards it (#8822). + TangentPlane("construct.plane.tangent", "TANGENT PLANE", [("Face", FACES, false), ("Reference plane", PLANES | PLANAR_FACES, false)], + FeatureKind::ConstructionPlane { plane: PlaneRef::Tangent { angle, .. } }) { + angle: String = "0 deg".into() => angle.as_ref().map_or_else(|| "0 deg".to_string(), ToString::to_string), + } => json!({"angle": angle}); Scale("solid.scale", "SCALE", [("Bodies", BODIES, true)], FeatureKind::Scale { factor, .. }) { factor: String = "2".into() => factor.to_string(), @@ -1316,6 +1321,7 @@ impl Dialog { | Kind::Loft { .. } | Kind::Sweep { .. } | Kind::AnglePlane { .. } + | Kind::TangentPlane { .. } | Kind::PathPlane { .. } | Kind::Midplane | Kind::Construct { .. } @@ -4783,6 +4789,15 @@ fn dialog_commands(app: &SolveApp, d: &Dialog) -> Result, S }; ("construct.plane.along_path", json!({"path": path, "t": t})) } + Kind::TangentPlane { .. } => { + need(0, "a curved face")?; + let face = construct_ref(app, sels(d, 0).first().ok_or("pick a curved face")?)?; + match sels(d, 1).first() { + Some(reference) => ("construct.plane.tangent", json!({"face": face, "reference": plane_arg(app, Some(reference))?})), + // A null `angle` keeps the descriptor's angle out: it turns the plane from a reference. + None => ("construct.plane.tangent", json!({"face": face, "angle": null})), + } + } Kind::AnglePlane { .. } => { need(0, "a plane")?; let base = plane_arg(app, sels(d, 0).first())?; @@ -5647,7 +5662,6 @@ fn construct_picks(id: &str) -> Option<(&'static str, &'static str, &'static [(& "PLANE THROUGH THREE POINTS", &[("First point", POINT, "a"), ("Second point", POINT, "b"), ("Third point", POINT, "c")], ), - "construct.plane.tangent" => ("construct.plane.tangent", "TANGENT PLANE", &[("Face", FACES, "face")]), "construct.plane.perpendicular" => { ("construct.plane.perpendicular", "PERPENDICULAR PLANE", &[("Plane", PLANE, "plane"), ("Line", LINE, "line")]) } @@ -6504,6 +6518,17 @@ pub fn for_feature(app: &SolveApp, id: u64, marker: Option) -> Option { + let mut d = start("construct.plane.tangent")?; + d.kind = Kind::simple_restore(&f.kind)?; + if let Some(inp) = d.inputs.first_mut() { + inp.items = geo_sel(s, face).into_iter().collect(); + } + if let Some(inp) = d.inputs.get_mut(1) { + inp.items = reference.as_deref().and_then(|r| geo_sel(s, r)).into_iter().collect(); + } + d + } FeatureKind::ConstructionPlane { plane: PlaneRef::Midplane { a, b } } => { let mut d = start("construct.plane.midplane")?; if let Some(inp) = d.inputs.first_mut() { @@ -7360,6 +7385,34 @@ mod construct_ask_tests { assert!(status_error(&h.app).is_none(), "{:?}", status_error(&h.app)); assert!(!solidcraft_engine::view::construction_planes(&h.app.session).is_empty(), "the tangent plane is created"); } + + /// #8822: a reference plane and an angle place the tangent plane, and editing it brings both back. + #[test] + fn tangent_plane_lines_up_with_a_reference_plane() { + let mut h = Harness::new(); + cylinder(&mut h); + h.step(&json!({"click": {"world": [10, 0, 10]}})).unwrap(); // the outer cylindrical face + h.step(&json!({"start": "construct.plane.tangent"})).unwrap(); + h.frames(2); + let d = h.app.dialog.as_mut().expect("the dialog opens"); + assert_eq!(d.inputs.iter().map(|i| i.label).collect::>(), ["Face", "Reference plane"]); + d.inputs[1].items = vec![Sel::Plane { name: "XZ".into() }]; + if let Kind::TangentPlane { angle } = &mut d.kind { + *angle = "30 deg".into(); + } + h.step(&json!({"key": "Enter"})).unwrap(); + h.frames(2); + assert!(h.app.dialog.is_none(), "Create closes the dialog: {:?}", status_error(&h.app)); + let planes = solidcraft_engine::view::construction_planes(&h.app.session); + let (id, _, pl) = planes.last().expect("the tangent plane is created"); + // XZ faces ±Y; turned 30° about the axis (Z), on the side of the pick (+X). + let n = Vec3::new(0.5, -(3f64.sqrt()) / 2.0, 0.0); + assert!(pl.normal().cross(n).len() < 1e-6 && (pl.origin - n * 10.0).dot(n).abs() < 1e-6, "{pl:?}"); + let reopened = for_feature(&h.app, *id, None).expect("the plane opens for editing"); + assert!(matches!(&reopened.kind, Kind::TangentPlane { angle } if angle == "30 deg"), "{:?}", reopened.kind); + assert_eq!(reopened.inputs[1].items, vec![Sel::Plane { name: "XZ".into() }]); + assert_eq!(reopened.inputs[0].items.len(), 1, "the face is picked again"); + } } #[cfg(test)] diff --git a/crates/ui-egui/src/dialogs_dense.rs b/crates/ui-egui/src/dialogs_dense.rs index da8f7b572..df6d934bb 100644 --- a/crates/ui-egui/src/dialogs_dense.rs +++ b/crates/ui-egui/src/dialogs_dense.rs @@ -750,6 +750,12 @@ fn additional_rows(app: &mut SolveApp, d: &mut Dialog, ui: &mut egui::Ui) -> boo Kind::AnglePlane { angle } => { enter |= expr_row(ui, "Angle", angle); } + // The angle turns the plane from its reference, so it shows once one is picked. + Kind::TangentPlane { angle } => { + if d.inputs.get(1).is_some_and(|inp| !inp.items.is_empty()) { + enter |= expr_row(ui, "Angle", angle); + } + } Kind::PathPlane { t, .. } => { enter |= expr_row(ui, "Position (0 to 1)", t); } diff --git a/crates/ui-egui/src/timeline.rs b/crates/ui-egui/src/timeline.rs index bbfb75e7f..7ee3cfd7d 100644 --- a/crates/ui-egui/src/timeline.rs +++ b/crates/ui-egui/src/timeline.rs @@ -1406,7 +1406,7 @@ mod tests { ("axis_point", PlaneRef::AxisPoint { axis: reference(), point: reference() }), ("two_edges", PlaneRef::TwoEdges { a: reference(), b: reference() }), ("three_points", PlaneRef::ThreePoints { a: reference(), b: reference(), c: reference() }), - ("tangent", PlaneRef::Tangent { face: reference(), at: Vec3::ZERO }), + ("tangent", PlaneRef::Tangent { face: reference(), at: Vec3::ZERO, reference: None, angle: None }), ("along_path", PlaneRef::AlongPath { path: reference(), t: "0.5".into() }), ("perpendicular", PlaneRef::Perpendicular { plane: reference(), line: reference() }), ] {