From bbc3aa4301380f4d6180ccbb12c35162d1fd98a1 Mon Sep 17 00:00:00 2001 From: Jesper We Date: Sun, 25 Oct 2026 06:02:14 +0000 Subject: [PATCH] Offset Plane: through a point (#3765) * claim: Offset Plane through a point * Offset Plane: through a point instead of at a distance --- crates/doc/src/assembly.rs | 4 ++ crates/doc/src/clipboard.rs | 8 ++++ crates/doc/src/construct.rs | 10 +++++ crates/doc/src/document.rs | 6 ++- crates/doc/src/eval.rs | 6 ++- crates/doc/src/face_anchor.rs | 15 ++++--- .../src/format/pending/plane-through-point.rs | 4 ++ crates/doc/src/sheet.rs | 14 +++--- crates/doc/src/walk.rs | 12 +++++- crates/engine/src/cmd/clipboard.rs | 4 +- crates/engine/src/cmd/construct.rs | 2 +- crates/engine/src/cmd/features.rs | 28 +++++++++--- crates/engine/src/cmd/sketch.rs | 5 ++- crates/engine/src/cmd/sketch_face.rs | 8 +++- crates/engine/src/construct_tests.rs | 38 ++++++++++++++++ crates/engine/src/occurrences.rs | 5 ++- crates/ui-egui/src/canvas.rs | 28 +++++++----- crates/ui-egui/src/dialogs.rs | 43 ++++++++++++++++++- crates/ui-egui/src/dialogs_dense.rs | 5 ++- crates/ui-egui/src/preselect_tests.rs | 24 +++++++++++ docs/book/src/guide/features.md | 2 +- 21 files changed, 230 insertions(+), 41 deletions(-) create mode 100644 crates/doc/src/format/pending/plane-through-point.rs diff --git a/crates/doc/src/assembly.rs b/crates/doc/src/assembly.rs index 3826883ff..b375eb4ae 100644 --- a/crates/doc/src/assembly.rs +++ b/crates/doc/src/assembly.rs @@ -146,6 +146,10 @@ fn map_plane_ref(m: &Mat, p: &mut PlaneRef) { *at = apply_point(m, *at); } PlaneRef::Offset { base, .. } | PlaneRef::Flipped { base, .. } => map_plane_ref(m, base), + PlaneRef::ThroughPoint { base, point } => { + map_plane_ref(m, base); + map_geo_ref(m, point); + } PlaneRef::AtAngle { base, axis_origin, axis_dir, .. } => { map_plane_ref(m, base); *axis_origin = apply_point(m, *axis_origin); diff --git a/crates/doc/src/clipboard.rs b/crates/doc/src/clipboard.rs index 7945c3f7f..ce3af7144 100644 --- a/crates/doc/src/clipboard.rs +++ b/crates/doc/src/clipboard.rs @@ -115,6 +115,10 @@ fn remap_plane(p: &mut PlaneRef, ids: &BTreeMap, bodies: &BTreeMap remap_plane(base, ids, bodies, depth + 1)?, + PlaneRef::ThroughPoint { base, point } => { + remap_plane(base, ids, bodies, depth + 1)?; + remap_geo(point, ids, bodies, depth + 1)?; + } PlaneRef::Face { name, anchor, .. } => { if let Some(name) = name { *name = remap_topology(name, ids, bodies); @@ -294,6 +298,10 @@ fn checked_plane(p: &PlaneRef, depth: usize) -> crate::Result<()> { checked_geo(plane, depth + 1)?; checked_geo(line, depth + 1)?; } + PlaneRef::ThroughPoint { base, point } => { + checked_plane(base, depth + 1)?; + checked_geo(point, depth + 1)?; + } PlaneRef::ThreePoints { a, b, c } => { checked_geo(a, depth + 1)?; checked_geo(b, depth + 1)?; diff --git a/crates/doc/src/construct.rs b/crates/doc/src/construct.rs index b10e0eeb0..284bde93b 100644 --- a/crates/doc/src/construct.rs +++ b/crates/doc/src/construct.rs @@ -592,6 +592,11 @@ pub(crate) fn resolve_geo_plane(ctx: &Ctx, p: &PlaneRef) -> Result { let (p, tan) = along(&path_points(ctx, path)?, t).ok_or_else(|| bad("the path has no length"))?; Plane::from_normal(p, tan).ok_or_else(degenerate) } + PlaneRef::ThroughPoint { base, point: at } => { + let b = ctx.doc.resolve_plane_in(ctx.vals, base, ctx.depth + 1, Some(ctx.st))?; + let n = b.normal().normalized().ok_or_else(degenerate)?; + Ok(Plane { origin: b.origin + n * (point(ctx, at)? - b.origin).dot(n), ..b }) + } _ => ctx.doc.resolve_plane_in(ctx.vals, p, ctx.depth + 1, Some(ctx.st)), } } @@ -700,6 +705,11 @@ impl PlaneRef { PlaneRef::ThreePoints { a, b, c } => vec![a.as_mut(), b.as_mut(), c.as_mut()], PlaneRef::Tangent { face, .. } => vec![face.as_mut()], PlaneRef::AlongPath { path, .. } => vec![path.as_mut()], + PlaneRef::ThroughPoint { base, point } => { + let mut refs = base.refs_mut(); + refs.push(point.as_mut()); + refs + } PlaneRef::Offset { base, .. } | PlaneRef::AtAngle { base, .. } | PlaneRef::Flipped { base, .. } => base.refs_mut(), _ => Vec::new(), } diff --git a/crates/doc/src/document.rs b/crates/doc/src/document.rs index 25abbaa22..c157c1e52 100644 --- a/crates/doc/src/document.rs +++ b/crates/doc/src/document.rs @@ -54,6 +54,9 @@ pub enum PlaneRef { }, /// Another plane offset along its normal by an expression. Offset { base: Box, distance: Expr }, + /// Another plane moved along its normal until it goes through a point (a vertex, sketch + /// point or construction point), so it follows the point. + ThroughPoint { base: Box, point: Box }, /// Another plane rotated about a line (in world coordinates) by an angle expression. AtAngle { base: Box, axis_origin: Vec3, axis_dir: Vec3, angle: Expr }, /// A construction plane feature (by name). @@ -3590,7 +3593,8 @@ impl Document { | PlaneRef::ThreePoints { .. } | PlaneRef::Tangent { .. } | PlaneRef::AlongPath { .. } - | PlaneRef::Perpendicular { .. } => { + | PlaneRef::Perpendicular { .. } + | PlaneRef::ThroughPoint { .. } => { let st = st.ok_or_else(|| DocError::Invalid("this plane is built from the model: evaluate first".into()))?; crate::construct::resolve_geo_plane(&crate::construct::Ctx { doc: self, vals, st, depth }, p) } diff --git a/crates/doc/src/eval.rs b/crates/doc/src/eval.rs index a5b8f3ce8..794ffd82f 100644 --- a/crates/doc/src/eval.rs +++ b/crates/doc/src/eval.rs @@ -6532,6 +6532,9 @@ pub(crate) fn sheet_face_plane_in_frame(doc: &Document, st: &ModelState, comp: u PlaneRef::Flipped { base, about_y } => { PlaneRef::Flipped { base: Box::new(sheet_face_plane_in_frame(doc, st, comp, base)?), about_y: *about_y } } + PlaneRef::ThroughPoint { base, point } => { + PlaneRef::ThroughPoint { base: Box::new(sheet_face_plane_in_frame(doc, st, comp, base)?), point: point.clone() } + } other => other.clone(), }) } @@ -7527,7 +7530,8 @@ fn plane_reads_geometry(plane: &PlaneRef) -> bool { match plane { PlaneRef::Origin { .. } | PlaneRef::Custom { .. } | PlaneRef::Placed { .. } | PlaneRef::Construction { .. } => false, PlaneRef::Offset { base, .. } | PlaneRef::AtAngle { base, .. } | PlaneRef::Flipped { base, .. } => plane_reads_geometry(base), - PlaneRef::Midplane { .. } + PlaneRef::ThroughPoint { .. } + | PlaneRef::Midplane { .. } | PlaneRef::TwoEdges { .. } | PlaneRef::ThreePoints { .. } | PlaneRef::Tangent { .. } diff --git a/crates/doc/src/face_anchor.rs b/crates/doc/src/face_anchor.rs index 38633d2ca..c321e0b6f 100644 --- a/crates/doc/src/face_anchor.rs +++ b/crates/doc/src/face_anchor.rs @@ -83,7 +83,9 @@ impl FaceAnchor { pub fn plane_face_anchor(plane: &PlaneRef) -> Option<&FaceAnchor> { match plane { PlaneRef::Face { anchor, .. } => anchor.as_deref(), - PlaneRef::Offset { base, .. } | PlaneRef::AtAngle { base, .. } | PlaneRef::Flipped { base, .. } => plane_face_anchor(base), + PlaneRef::Offset { base, .. } | PlaneRef::ThroughPoint { base, .. } | PlaneRef::AtAngle { base, .. } | PlaneRef::Flipped { base, .. } => { + plane_face_anchor(base) + } _ => None, } } @@ -91,7 +93,9 @@ pub fn plane_face_anchor(plane: &PlaneRef) -> Option<&FaceAnchor> { fn has_face_reference(plane: &PlaneRef) -> bool { match plane { PlaneRef::Face { .. } => true, - PlaneRef::Offset { base, .. } | PlaneRef::AtAngle { base, .. } | PlaneRef::Flipped { base, .. } => has_face_reference(base), + PlaneRef::Offset { base, .. } | PlaneRef::ThroughPoint { base, .. } | PlaneRef::AtAngle { base, .. } | PlaneRef::Flipped { base, .. } => { + has_face_reference(base) + } _ => false, } } @@ -128,9 +132,10 @@ impl Document { *plane = PlaneRef::Custom { plane: resolved }; Ok(note) } - PlaneRef::Offset { base, .. } | PlaneRef::AtAngle { base, .. } | PlaneRef::Flipped { base, .. } => { - replace(doc, base, author, state, depth + 1) - } + PlaneRef::Offset { base, .. } + | PlaneRef::ThroughPoint { base, .. } + | PlaneRef::AtAngle { base, .. } + | PlaneRef::Flipped { base, .. } => replace(doc, base, author, state, depth + 1), _ => Ok(None), } } diff --git a/crates/doc/src/format/pending/plane-through-point.rs b/crates/doc/src/format/pending/plane-through-point.rs new file mode 100644 index 000000000..0b449bc8c --- /dev/null +++ b/crates/doc/src/format/pending/plane-through-point.rs @@ -0,0 +1,4 @@ +//! A construction plane can be another plane moved along its normal until it goes through a +//! point (`through_point`: `base` and the `point` reference), so it follows the point. Older +//! designs need no transformation: they have no such planes. The last release asks for an update +//! instead of meeting a plane type it can't read. diff --git a/crates/doc/src/sheet.rs b/crates/doc/src/sheet.rs index 0c78b2649..fb96d63be 100644 --- a/crates/doc/src/sheet.rs +++ b/crates/doc/src/sheet.rs @@ -4706,9 +4706,10 @@ pub fn rename_plane_sheet_body(plane: &mut crate::PlaneRef, old: &str, new: &str *name = renamed; } } - crate::PlaneRef::Offset { base, .. } | crate::PlaneRef::AtAngle { base, .. } | crate::PlaneRef::Flipped { base, .. } => { - rename_plane_sheet_body(base, old, new) - } + crate::PlaneRef::Offset { base, .. } + | crate::PlaneRef::ThroughPoint { base, .. } + | crate::PlaneRef::AtAngle { base, .. } + | crate::PlaneRef::Flipped { base, .. } => rename_plane_sheet_body(base, old, new), _ => {} } } @@ -4718,9 +4719,10 @@ pub fn rename_plane_sheet_body(plane: &mut crate::PlaneRef, old: &str, new: &str pub fn plane_sheet_face(plane: &crate::PlaneRef) -> Option<&str> { match plane { crate::PlaneRef::Face { name: Some(name), anchor: None, .. } if split_sheet_face_name(name).is_some() => Some(name), - crate::PlaneRef::Offset { base, .. } | crate::PlaneRef::AtAngle { base, .. } | crate::PlaneRef::Flipped { base, .. } => { - plane_sheet_face(base) - } + crate::PlaneRef::Offset { base, .. } + | crate::PlaneRef::ThroughPoint { base, .. } + | crate::PlaneRef::AtAngle { base, .. } + | crate::PlaneRef::Flipped { base, .. } => plane_sheet_face(base), _ => None, } } diff --git a/crates/doc/src/walk.rs b/crates/doc/src/walk.rs index b484b4206..aa4fc74c5 100644 --- a/crates/doc/src/walk.rs +++ b/crates/doc/src/walk.rs @@ -336,7 +336,7 @@ impl Walk for PlaneRef { PlaneRef::AlongPath { t, .. } => return f(("Distance (0 to 1)", t, Kind::Unitless, 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, .. } => p = base, + PlaneRef::Flipped { base, .. } | PlaneRef::ThroughPoint { base, .. } => p = base, PlaneRef::Placed { origin, .. } => { for (label, e) in ["X", "Y", "Z"].into_iter().zip(origin) { f((label, e, Kind::Length, ExprRole::Placement)); @@ -362,7 +362,7 @@ impl Walk for PlaneRef { PlaneRef::AlongPath { t, .. } => return f(("Distance (0 to 1)", t, Kind::Unitless, 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, .. } => p = base, + PlaneRef::Flipped { base, .. } | PlaneRef::ThroughPoint { base, .. } => p = base, PlaneRef::Placed { origin, .. } => { for (label, e) in ["X", "Y", "Z"].into_iter().zip(origin) { f((label, e, Kind::Length, ExprRole::Placement)); @@ -395,6 +395,10 @@ impl PlaneRef { match self { Self::Construction { name } => f(Ref::Feature(name)), Self::Offset { base, .. } | Self::AtAngle { base, .. } | Self::Flipped { base, .. } => base.walk_feature_refs(f, depth + 1), + Self::ThroughPoint { base, point } => { + base.walk_feature_refs(f, depth + 1); + point.walk_feature_refs(f, depth + 1); + } Self::Midplane { a, b } | Self::TwoEdges { a, b } | Self::Perpendicular { plane: a, line: b } => { a.walk_feature_refs(f, depth + 1); b.walk_feature_refs(f, depth + 1); @@ -416,6 +420,10 @@ impl PlaneRef { match self { Self::Construction { name } => f(Ref::Feature(name)), Self::Offset { base, .. } | Self::AtAngle { base, .. } | Self::Flipped { base, .. } => base.walk_feature_refs_mut(f, depth + 1), + Self::ThroughPoint { base, point } => { + base.walk_feature_refs_mut(f, depth + 1); + point.walk_feature_refs_mut(f, depth + 1); + } Self::Midplane { a, b } | Self::TwoEdges { a, b } | Self::Perpendicular { plane: a, line: b } => { a.walk_feature_refs_mut(f, depth + 1); b.walk_feature_refs_mut(f, depth + 1); diff --git a/crates/engine/src/cmd/clipboard.rs b/crates/engine/src/cmd/clipboard.rs index cf924e89d..5b5416c58 100644 --- a/crates/engine/src/cmd/clipboard.rs +++ b/crates/engine/src/cmd/clipboard.rs @@ -46,7 +46,9 @@ fn explicit_planes(p: &mut PlaneRef) { *p = PlaneRef::Custom { plane }; } } - PlaneRef::Offset { base, .. } | PlaneRef::AtAngle { base, .. } | PlaneRef::Flipped { base, .. } => explicit_planes(base), + PlaneRef::Offset { base, .. } | PlaneRef::ThroughPoint { base, .. } | PlaneRef::AtAngle { base, .. } | PlaneRef::Flipped { base, .. } => { + explicit_planes(base) + } _ => {} } } diff --git a/crates/engine/src/cmd/construct.rs b/crates/engine/src/cmd/construct.rs index 5762ce933..26b6a8074 100644 --- a/crates/engine/src/cmd/construct.rs +++ b/crates/engine/src/cmd/construct.rs @@ -89,7 +89,7 @@ pub static COMMANDS: &[CommandSpec] = &[ ]; /// A reference from its JSON form. -fn geo(s: &Session, v: Option<&Value>, cmd: &str, what: &str) -> Result { +pub(super) fn geo(s: &Session, v: Option<&Value>, cmd: &str, what: &str) -> Result { let v = v.ok_or_else(|| bad(cmd, format!("`{what}` is required: {REF}")))?; let p3 = |k: &str| v.get(k).and_then(vec3); let mut g = if v.get("type").is_some() { diff --git a/crates/engine/src/cmd/features.rs b/crates/engine/src/cmd/features.rs index 6159c3032..cf41441f9 100644 --- a/crates/engine/src/cmd/features.rs +++ b/crates/engine/src/cmd/features.rs @@ -104,7 +104,7 @@ pub static COMMANDS: &[CommandSpec] = &[ CommandSpec::new("construct.plane.offset", "Offset Plane", plane_offset) .at("SOLID", "CONSTRUCT") .icon("plane") - .params("base: XY|XZ|YZ|plane name, or a planar face {face: [x,y,z], body?, index?, path?}; offset: expr; name?"), + .params("base: XY|XZ|YZ|plane name, or a planar face {face: [x,y,z], body?, index?, path?}; offset: expr, or through: a point ref (vertex, sketch point, construction point) the plane goes through, parallel to base; name?"), CommandSpec::new("construct.plane.at_angle", "Plane at Angle", plane_angle) .at("SOLID", "CONSTRUCT") .icon("plane_angle") @@ -1811,19 +1811,35 @@ pub(super) fn plane_param(s: &Session, v: Option<&Value>, cmd: &str) -> Result Result { + use solidcraft_doc::PlaneRef; let cmd = "construct.plane.offset"; - let d = req_expr(cmd, p, "offset")?; - check_expr(s, &d, Kind::Length, cmd, "offset")?; + // Through a point, or at a distance along the base's normal. + let through = match p.get("through").filter(|t| !t.is_null()) { + Some(_) if p.get("offset").is_some_and(|o| !o.is_null()) => return Err(bad(cmd, "give `offset` or `through`, not both")), + Some(t) => Some(super::construct::geo(s, Some(t), cmd, "through")?), + None => None, + }; + let distance = match &through { + Some(_) => Default::default(), + None => { + let d = req_expr(cmd, p, "offset")?; + check_expr(s, &d, Kind::Length, cmd, "offset")?; + d + } + }; + let offset = move |base: PlaneRef| match through { + Some(point) => PlaneRef::ThroughPoint { base: Box::new(base), point: Box::new(point) }, + None => PlaneRef::Offset { base: Box::new(base), distance }, + }; // 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::Offset { base: Box::new(base), distance: d } }; // The capture is already in the authoring frame. - return add_feature_in(s, p, kind, false); + return add_feature_in(s, p, FeatureKind::ConstructionPlane { plane: offset(base) }, false); } let base = plane_param(s, p.get("base"), cmd)?; - add_feature(s, p, FeatureKind::ConstructionPlane { plane: solidcraft_doc::PlaneRef::Offset { base: Box::new(base), distance: d } }) + add_feature(s, p, FeatureKind::ConstructionPlane { plane: offset(base) }) } fn plane_angle(s: &mut Session, p: &Value) -> Result { diff --git a/crates/engine/src/cmd/sketch.rs b/crates/engine/src/cmd/sketch.rs index 85cf36e27..a1385402a 100644 --- a/crates/engine/src/cmd/sketch.rs +++ b/crates/engine/src/cmd/sketch.rs @@ -601,7 +601,10 @@ pub(crate) fn author_path_for_rebind(s: &Session, feature: &solidcraft_doc::Feat let mut plane = plane; loop { match plane { - PlaneRef::Offset { base, .. } | PlaneRef::AtAngle { base, .. } | PlaneRef::Flipped { base, .. } => plane = base, + PlaneRef::Offset { base, .. } + | PlaneRef::ThroughPoint { base, .. } + | PlaneRef::AtAngle { base, .. } + | PlaneRef::Flipped { base, .. } => plane = base, PlaneRef::Face { anchor: Some(anchor), .. } => { saved = Some(anchor.author_path.clone()); break; diff --git a/crates/engine/src/cmd/sketch_face.rs b/crates/engine/src/cmd/sketch_face.rs index 35d30953a..f551598d2 100644 --- a/crates/engine/src/cmd/sketch_face.rs +++ b/crates/engine/src/cmd/sketch_face.rs @@ -206,7 +206,9 @@ fn capture_impl(s: &Session, value: &Value, cmd: &str, internal: bool) -> Result pub(in crate::cmd) fn anchor(mut plane: &PlaneRef) -> Option<&solidcraft_doc::FaceAnchor> { loop { match plane { - PlaneRef::Offset { base, .. } | PlaneRef::AtAngle { base, .. } | PlaneRef::Flipped { base, .. } => plane = base, + PlaneRef::Offset { base, .. } | PlaneRef::ThroughPoint { base, .. } | PlaneRef::AtAngle { base, .. } | PlaneRef::Flipped { base, .. } => { + plane = base + } PlaneRef::Face { anchor, .. } => return anchor.as_deref(), _ => return None, } @@ -216,7 +218,9 @@ pub(in crate::cmd) fn anchor(mut plane: &PlaneRef) -> Option<&solidcraft_doc::Fa pub(in crate::cmd) fn anchor_mut(mut plane: &mut PlaneRef) -> Option<&mut solidcraft_doc::FaceAnchor> { loop { match plane { - PlaneRef::Offset { base, .. } | PlaneRef::AtAngle { base, .. } | PlaneRef::Flipped { base, .. } => plane = base, + PlaneRef::Offset { base, .. } | PlaneRef::ThroughPoint { base, .. } | PlaneRef::AtAngle { base, .. } | PlaneRef::Flipped { base, .. } => { + plane = base + } PlaneRef::Face { anchor, .. } => return anchor.as_deref_mut(), _ => return None, } diff --git a/crates/engine/src/construct_tests.rs b/crates/engine/src/construct_tests.rs index e750458b5..a124cd4b6 100644 --- a/crates/engine/src/construct_tests.rs +++ b/crates/engine/src/construct_tests.rs @@ -524,3 +524,41 @@ fn ambiguity_error_names_the_other_origin_alias() { assert_eq!(inspected["plane"]["origin"], json!([0.0, 0.0, 0.0])); } } + +/// An offset plane through a point instead of at a distance: parallel to its base, through a +/// vertex, a sketch point or a construction point, and moving with the point. +#[test] +fn offset_plane_through_a_point_follows_the_point() { + let mut s = Session::default(); + run(&mut s, "parameters.add", json!({"name": "h", "expression": "20 mm"})); + run(&mut s, "solid.box", json!({"length": 40, "width": 30, "height": "h", "body_name": "Box"})); + // Through the box's top corner, parallel to XY: the top face's plane. + run(&mut s, "construct.plane.offset", json!({"base": "XY", "through": {"vertex": [40, 30, 20]}, "name": "Lid"})); + let lid = plane(&s, "Lid"); + assert!(parallel(lid.normal(), Vec3::Z) && near(lid.origin, Vec3::new(0.0, 0.0, 20.0)), "{lid:?}"); + // Through a point of a sketch on XZ, parallel to the box's side face at x = 40. + let sketch = run(&mut s, "sketch.create", json!({"plane": "XZ"}))["sketch"].as_u64().unwrap(); + run(&mut s, "sketch.point", json!({"point": [70, 5], "id": "mark"})); + run(&mut s, "sketch.finish", json!({})); + run(&mut s, "construct.plane.offset", json!({"base": {"face": [40, 15, 10]}, "through": {"sketch": sketch, "point": "mark"}, "name": "Side"})); + let side = plane(&s, "Side"); + assert!(parallel(side.normal(), Vec3::X) && on_plane(&side, Vec3::new(70.0, 9.0, 3.0)), "{side:?}"); + // Through a construction point, parallel to another construction plane. + run(&mut s, "construct.point.vertex", json!({"point": {"vertex": [0, 30, 20]}, "name": "Corner"})); + run(&mut s, "construct.plane.offset", json!({"base": "XZ", "offset": 5, "name": "Fore"})); + run(&mut s, "construct.plane.offset", json!({"base": "Fore", "through": {"construct_point": "Corner"}, "name": "Back"})); + let back = plane(&s, "Back"); + assert!(parallel(back.normal(), Vec3::Y) && on_plane(&back, Vec3::new(3.0, 30.0, 7.0)), "{back:?}"); + // The box gets taller: the planes through its corners follow. + run(&mut s, "parameters.change", json!({"name": "h", "expression": "50 mm"})); + assert!(on_plane(&plane(&s, "Lid"), Vec3::new(1.0, 2.0, 50.0)), "{:?}", plane(&s, "Lid")); + assert!(on_plane(&plane(&s, "Back"), Vec3::new(1.0, 30.0, 2.0)), "{:?}", plane(&s, "Back")); + // Saved and opened again, the planes come back where they were. + let restored = solidcraft_doc::format::read(&s.doc.to_json()).unwrap(); + let mut cold = Session { doc: restored.into(), ..Default::default() }; + cold.refresh(); + assert!(near(plane(&cold, "Lid").origin, plane(&s, "Lid").origin)); + assert!(near(plane(&cold, "Side").origin, plane(&s, "Side").origin)); + // A distance and a point at once is refused. + assert!(s.execute("construct.plane.offset", &json!({"base": "XY", "offset": 5, "through": {"vertex": [0, 0, 0]}})).is_err()); +} diff --git a/crates/engine/src/occurrences.rs b/crates/engine/src/occurrences.rs index 786380067..e763fec16 100644 --- a/crates/engine/src/occurrences.rs +++ b/crates/engine/src/occurrences.rs @@ -387,7 +387,10 @@ impl Session { let mut plane = plane; loop { match plane { - PlaneRef::Offset { base, .. } | PlaneRef::AtAngle { base, .. } | PlaneRef::Flipped { base, .. } => plane = base, + PlaneRef::Offset { base, .. } + | PlaneRef::ThroughPoint { base, .. } + | PlaneRef::AtAngle { base, .. } + | PlaneRef::Flipped { base, .. } => plane = base, // A face attachment that names no exact source: which body it sits on is unknown, // so none is taken from the view. PlaneRef::Face { anchor: None, .. } => return none, diff --git a/crates/ui-egui/src/canvas.rs b/crates/ui-egui/src/canvas.rs index c4438c5bd..270cc56ba 100644 --- a/crates/ui-egui/src/canvas.rs +++ b/crates/ui-egui/src/canvas.rs @@ -68,14 +68,7 @@ pub fn anchor(app: &SolveApp, d: &Dialog) -> Option<(Vec3, Option)> { } // A corner picked into a sketch window (":", or a bare id when it // was selected outside one): the value box sits on the corner itself. - Sel::SketchPoint { id } => { - let (sketch, point) = match id.split_once(':') { - Some((sketch, point)) => (sketch.parse().ok()?, point), - None => (app.session.active_sketch?, id.as_str()), - }; - let ss = st.sketch(sketch)?; - (ss.plane.to_world(ss.sketch.point(ss.sketch.resolve_point(point)?)?), None) - } + Sel::SketchPoint { id } => (sketch_point_at(app, &st, id)?, None), Sel::Body { name } => (st.body(name)?.mesh().bounds().center(), None), // A feature picked as a whole (a pattern or mirror of features): the middle of what it // made, as a body sits at the middle of its box. @@ -391,6 +384,16 @@ pub(crate) fn format_len(v: f64) -> String { format!("{} mm", if s == "-0" { "0" } else { s }) } +/// Where a sketch point picked as ":" (or a bare id of the active sketch) is. +fn sketch_point_at(app: &SolveApp, st: &solidcraft_engine::doc::ModelState, id: &str) -> Option { + let (sketch, point) = match id.split_once(':') { + Some((sketch, point)) => (sketch.parse().ok()?, point), + None => (app.session.active_sketch?, id), + }; + let ss = st.sketch(sketch)?; + Some(ss.plane.to_world(ss.sketch.point(ss.sketch.resolve_point(point)?)?)) +} + /// The offset plane preview: a square of half side `h` around `c`, square to `n`. fn offset_plane_quad(proj: &Proj, c: Vec3, n: Vec3, h: f64) -> Option> { let u = n.cross(if n.z.abs() < 0.9 { Vec3::Z } else { Vec3::X }).normalized()?; @@ -490,9 +493,14 @@ fn draw(app: &SolveApp, ui: &mut egui::Ui, painter: &egui::Painter, proj: &Proj, drag_arrow(ui, painter, proj, base, bs, n2, l2, false, egui::Id::new("sc_manipulator2"), half_height, spacing2); } } - // An offset plane shows where it will be. + // An offset plane shows where it will be: at the distance, or through the picked point. + let through = crate::dialogs::offset_through_point(d).and_then(|p| match p { + Sel::Vertex { point, .. } => Some(*point), + Sel::SketchPoint { id } => sketch_point_at(app, &app.session.world_state(), id), + _ => None, + }); if let (Kind::OffsetPlane { offset }, Some(n)) = (&d.kind, dir) - && let Ok(off) = app.session.doc.eval(offset, ValueKind::Length) + && let Some(off) = through.map(|p| (p - base).dot(n)).or_else(|| app.session.doc.eval(offset, ValueKind::Length).ok()) && let Some(q) = offset_plane_quad(proj, base + n * off, n, half_height * 0.25) { painter.extend(projected_polygon(&q, t.construction_plane, Stroke::new(1.2, t.origin_plane_edge))); diff --git a/crates/ui-egui/src/dialogs.rs b/crates/ui-egui/src/dialogs.rs index 2f8c34522..49429e88c 100644 --- a/crates/ui-egui/src/dialogs.rs +++ b/crates/ui-egui/src/dialogs.rs @@ -105,7 +105,8 @@ crate::dialog_descriptor::dialog_descriptors! { FeatureKind::Draft { angle, .. }) { angle: String = "5 deg".into() => angle.to_string(), } => json!({"angle": angle}); - OffsetPlane("construct.plane.offset", "OFFSET PLANE", [("Plane", PLANES | PLANAR_FACES, false)], + // A point picked as well puts the plane through it instead of at the distance. + OffsetPlane("construct.plane.offset", "OFFSET PLANE", [("Plane", PLANES | PLANAR_FACES, false), ("Through point", selection::VERTICES | selection::POINTS, false)], FeatureKind::ConstructionPlane { plane: PlaneRef::Offset { distance, .. } }) { offset: String = "0 mm".into() => distance.to_string(), } => json!({"offset": offset}); @@ -1048,6 +1049,7 @@ impl Dialog { /// The main value (shown on the canvas too): label, kind and the expression. pub fn primary(&mut self) -> Option<(&'static str, ValueKind, &mut String)> { let move_field = crate::dialogs_move::value_field(self); + let through_point = offset_through_point(self).is_some(); Some(match &mut self.kind { Kind::Extrude { to_offset, .. } if to_object_on(&self.inputs) => ("To Offset", ValueKind::Length, to_offset), Kind::Extrude { distance, .. } => ("Distance", ValueKind::Length, distance), @@ -1088,6 +1090,7 @@ impl Dialog { Kind::PatternRect { spacing, .. } => ("Spacing", ValueKind::Length, spacing), Kind::PatternCirc { angle, .. } => ("Angle", ValueKind::Angle, angle), Kind::Section { offset, .. } => ("Distance", ValueKind::Length, offset), + Kind::OffsetPlane { .. } if through_point => return None, Kind::OffsetPlane { offset } => ("Distance", ValueKind::Length, offset), Kind::PathPlane { t, .. } => ("Position (0 to 1)", ValueKind::Unitless, t), Kind::Rib { thickness, .. } => ("Thickness", ValueKind::Length, thickness), @@ -2044,6 +2047,7 @@ fn selection_label(s: &Session, item: &Sel) -> String { let p = s.active_sketch.and_then(|id| state.sketch(id)).map(|sk| sk.plane.to_local(*point)).unwrap_or_default(); format!("({:.2}, {:.2}) mm", p.x, p.y) } + Sel::Vertex { body, .. } => format!("{body} · vertex"), _ => format!("{item:?}"), } } @@ -3675,7 +3679,11 @@ fn dialog_commands(app: &SolveApp, d: &Dialog) -> Result, S Kind::OffsetPlane { .. } => { need(0, "a plane")?; let base = plane_arg(app, sels(d, 0).first())?; - ("construct.plane.offset", json!({"base": base})) + match offset_through_point(d) { + // A null `offset` keeps the descriptor's distance out: the plane goes through the point. + Some(point) => ("construct.plane.offset", json!({"base": base, "through": construct_ref(app, point)?, "offset": null})), + None => ("construct.plane.offset", json!({"base": base})), + } } Kind::MeshConvert { tolerance } => { need(0, "the mesh body")?; @@ -4481,6 +4489,27 @@ fn plane_arg(app: &SolveApp, sel: Option<&Sel>) -> Result { } } +/// The point an Offset Plane dialog puts its plane through, when one is picked. +pub(crate) fn offset_through_point(d: &Dialog) -> Option<&Sel> { + if !matches!(d.kind, Kind::OffsetPlane { .. }) { + return None; + } + sels(d, 1).first() +} + +/// A stored point reference back into a dialog pick: a body vertex or a sketch point. +fn point_sel(s: &Session, g: &solidcraft_engine::doc::construct::GeoRef) -> Option { + use solidcraft_engine::doc::construct::GeoRef; + match g { + GeoRef::Vertex { at, .. } => match face_sel(s, *at)? { + Sel::Face { body, .. } => Some(Sel::Vertex { body, point: *at }), + _ => None, + }, + GeoRef::SketchPoint { sketch, point } => Some(Sel::SketchPoint { id: format!("{sketch}:{point}") }), + _ => None, + } +} + /// A stored construction reference back into a dialog pick. fn geo_sel(s: &Session, g: &solidcraft_engine::doc::construct::GeoRef) -> Option { match g { @@ -5121,6 +5150,16 @@ pub fn for_feature(app: &SolveApp, id: u64, marker: Option) -> Option { + let mut d = start("construct.plane.offset")?; + if let Some(inp) = d.inputs.first_mut() { + inp.items = plane_sel(s, base).into_iter().collect(); + } + if let Some(inp) = d.inputs.get_mut(1) { + inp.items = point_sel(s, point).into_iter().collect(); + } + d + } FeatureKind::ConstructionPlane { plane: PlaneRef::AlongPath { path, t } } => { let mut d = start("construct.plane.along_path")?; let (item, sketch) = match path.as_ref() { diff --git a/crates/ui-egui/src/dialogs_dense.rs b/crates/ui-egui/src/dialogs_dense.rs index f22aefd8b..1bef6ee6a 100644 --- a/crates/ui-egui/src/dialogs_dense.rs +++ b/crates/ui-egui/src/dialogs_dense.rs @@ -607,6 +607,7 @@ fn coil_rows(ui: &mut egui::Ui, extra: &mut serde_json::Map) -> b fn additional_rows(app: &mut SolveApp, d: &mut Dialog, ui: &mut egui::Ui) -> bool { let mut enter = false; + let through_point = offset_through_point(d).is_some(); match &mut d.kind { Kind::Revolve { angle, operation, .. } => { let cur = usize::from(to_object_on(&d.inputs)); @@ -673,7 +674,9 @@ fn additional_rows(app: &mut SolveApp, d: &mut Dialog, ui: &mut egui::Ui) -> boo } } Kind::OffsetPlane { offset } => { - enter |= expr_row(ui, "Distance", offset); + if !through_point { + enter |= expr_row(ui, "Distance", offset); + } } Kind::AnglePlane { angle } => { enter |= expr_row(ui, "Angle", angle); diff --git a/crates/ui-egui/src/preselect_tests.rs b/crates/ui-egui/src/preselect_tests.rs index 22e1e4f49..4a603bb98 100644 --- a/crates/ui-egui/src/preselect_tests.rs +++ b/crates/ui-egui/src/preselect_tests.rs @@ -2131,6 +2131,30 @@ fn midplane_asks_for_two_planes_then_builds_the_one_between_them() { assert!((pl.origin.z - 10.0).abs() < 1e-6, "{pl:?}"); } +/// Offset Plane through a point: a vertex picked as well as the plane puts the plane through +/// it, the distance row goes away, and editing the plane shows both picks again. +#[test] +fn offset_plane_through_a_picked_vertex() { + let mut h = crate::scenario::Harness::new(); + h.app.run("solid.box", json!({"length":40,"width":30,"height":20,"body_name":"Box"})).unwrap(); + h.app.start("construct.plane.offset"); + h.frames(1); + let d = h.app.dialog.as_mut().unwrap(); + assert_eq!(d.inputs.len(), 2); + d.inputs[0].items = vec![Sel::Plane { name: "XY".into() }]; + d.inputs[1].items = vec![Sel::Vertex { body: "Box".into(), point: Vec3::new(40.0, 30.0, 20.0) }]; + assert!(d.primary().is_none(), "no distance to type or drag"); + let commands = apply_commands(&h.app, h.app.dialog.as_ref().unwrap()).unwrap(); + assert_eq!(commands.first().map(|c| c.1.clone()), Some(json!({"base":"XY","through":{"vertex":[40.0,30.0,20.0]},"offset":null}))); + h.step(&json!({"key":"Enter"})).unwrap(); + assert!(h.app.dialog.is_none(), "{:?}", h.app.dialog.as_ref().and_then(|d| d.error.clone())); + let (id, _, pl) = solidcraft_engine::view::construction_planes(&h.app.session).into_iter().next().expect("the plane"); + assert!(pl.normal().cross(Vec3::Z).len() < 1e-6 && (pl.origin.z - 20.0).abs() < 1e-6, "{pl:?}"); + let reopened = crate::dialogs::for_feature(&h.app, id, None).unwrap(); + assert_eq!(reopened.inputs[0].items, vec![Sel::Plane { name: "XY".into() }]); + assert!(matches!(reopened.inputs[1].items.as_slice(), [Sel::Vertex { body, .. }] if body == "Box"), "{:?}", reopened.inputs[1].items); +} + /// #2474, #2494: every construct tool asks for what it needs. Started from the toolbar with /// nothing selected, none of them runs its command and reports that `a` (or `face`, `edge`…) /// is required; a construct command added later without a dialog fails here too. diff --git a/docs/book/src/guide/features.md b/docs/book/src/guide/features.md index b08d0fc6c..023da7c0f 100644 --- a/docs/book/src/guide/features.md +++ b/docs/book/src/guide/features.md @@ -9,7 +9,7 @@ it. |---|---| | Create | extrude (one side, two sides, symmetric, taper, start offset, to a distance or through all; new body, join, cut, intersect; thin walls along curves), revolve, sweep, loft, box, cylinder, sphere, torus, coil, pipe, rib, web, emboss, hole (simple, counterbore, countersink, tapped; on faces, at distances from edges, or at sketch points), thread, rectangular/circular/path patterns, mirror | | Modify | press pull, fillet, chamfer, shell, draft, scale, combine, split body, offset face, replace face, move/copy, align, remove | -| Construct | offset plane, plane at angle, plane along path | +| Construct | offset plane (at a distance or through a point), plane at angle, plane along path | | Inspect | measure, section analysis, interference, compare bodies, curvature comb and map, zebra, draft, minimum radius, isocurves, centre of mass | Extrude automatically selects **Join** for additions when the active component already has a