Skip to content

SolidCraft: an open-source parametric 3D CAD application built in pure Rust. Desktop, web, and automation through MCP.

Offset Plane: through a point (#3765)

Jesper Webbc3aa430138
diff --git a/crates/doc/src/assembly.rs b/crates/doc/src/assembly.rs
index 3826883ffd..b375eb4ae6 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 7945c3f7fc..ce3af71444 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<u64, u64>, bodies: &BTreeMap<Str
}
match p {
PlaneRef::Offset { base, .. } | PlaneRef::AtAngle { base, .. } | PlaneRef::Flipped { base, .. } => 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 b10e0eeb05..284bde93bf 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<Plane> {
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 25abbaa226..c157c1e526 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<PlaneRef>, 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<PlaneRef>, point: Box<GeoRef> },
/// Another plane rotated about a line (in world coordinates) by an angle expression.
AtAngle { base: Box<PlaneRef>, 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 a5b8f3ce84..794ffd82f5 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 38633d2cac..c321e0b6fe 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 0000000000..0b449bc8ce
--- /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 0c78b26492..fb96d63be3 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 b484b4206a..aa4fc74c54 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 cf924e89d6..5b5416c583 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 5762ce9338..26b6a80742 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<GeoRef> {
+pub(super) fn geo(s: &Session, v: Option<&Value>, cmd: &str, what: &str) -> Result<GeoRef> {
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 6159c30322..cf41441f9e 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<s
}
fn plane_offset(s: &mut Session, p: &Value) -> Result<Value> {
+ 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<Value> {
diff --git a/crates/engine/src/cmd/sketch.rs b/crates/engine/src/cmd/sketch.rs
index 85cf36e27d..a1385402a9 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 35d30953a0..f551598d28 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 e750458b5a..a124cd4b6d 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 786380067d..e763fec16f 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 c4438c5bdc..270cc56ba7 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<Vec3>)> {
}
// A corner picked into a sketch window ("<sketch id>:<point id>", 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 "<sketch id>:<point id>" (or a bare id of the active sketch) is.
+fn sketch_point_at(app: &SolveApp, st: &solidcraft_engine::doc::ModelState, id: &str) -> Option<Vec3> {
+ 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<Vec<Pos2>> {
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 2f8c34522e..49429e88c5 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<Vec<(String, Value)>, 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<Value, String> {
}
}
+/// 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<Sel> {
+ 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<Sel> {
match g {
@@ -5121,6 +5150,16 @@ pub fn for_feature(app: &SolveApp, id: u64, marker: Option<usize>) -> Option<Dia
}
d
}
+ FeatureKind::ConstructionPlane { plane: PlaneRef::ThroughPoint { base, point } } => {
+ 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 f22aefd8bf..1bef6ee6a3 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<String, Value>) -> 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 22e1e4f490..4a603bb986 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 b08d0fc6c3..023da7c0f1 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