Skip to content

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

Assemblies: a construction axis follows its component when the component rotates (#10127) (#10212)

kaltenbrunnermarkus073bbc3f00c6
diff --git a/crates/doc/src/construct.rs b/crates/doc/src/construct.rs
index d7e73925b8..2da42d0ef8 100644
--- a/crates/doc/src/construct.rs
+++ b/crates/doc/src/construct.rs
@@ -116,6 +116,11 @@ pub struct Construct {
pub feature: u64,
pub name: String,
pub geom: ConstructGeom,
+ /// The bodies it was built from, when every pick it reads lies on a body
+ /// ([`source_bodies`]): a later Move of all of them carries it along (#10127). Empty when
+ /// it also reads something fixed (a point, a plane, a sketch), which keeps it in place.
+ #[serde(default, skip_serializing_if = "Vec::is_empty")]
+ pub bodies: Vec<String>,
}
/// Everything a reference needs to be resolved.
@@ -580,6 +585,46 @@ fn line_line(a: (Vec3, Vec3), b: (Vec3, Vec3)) -> Option<Vec3> {
Some(((p + u * s) + (q + v * t)) * 0.5)
}
+/// The bodies a construction built from `refs` lies on: each face, edge and vertex pick's body,
+/// and the bodies of the constructions it is built from. Empty when any pick is not on a body
+/// (a fixed point, the origin, a plane, a world axis, a sketch) or is not found: such a
+/// construction is held by something a move of the bodies leaves where it is, so it stays.
+pub(crate) fn source_bodies(ctx: &Ctx, refs: &[&GeoRef]) -> Vec<String> {
+ let mut out: Vec<String> = Vec::new();
+ for g in refs {
+ let found: Vec<String> = match g {
+ GeoRef::Face { at, name } => face_of(ctx.st, *at, name).map(|(b, ..)| vec![b.definition_name().to_string()]).unwrap_or_default(),
+ GeoRef::Edge { at, name } => edge_pick(ctx.st, *at, name).map(|(b, ..)| vec![b.definition_name().to_string()]).unwrap_or_default(),
+ GeoRef::Vertex { .. } => {
+ point(ctx, g).ok().and_then(|p| vertex_body(ctx.st, p)).map(|b| vec![b.definition_name().to_string()]).unwrap_or_default()
+ }
+ GeoRef::Axis { name } | GeoRef::ConstructPoint { name } => {
+ ctx.st.construct.iter().find(|c| c.name.as_str() == AsRef::<str>::as_ref(name)).map(|c| c.bodies.clone()).unwrap_or_default()
+ }
+ _ => Vec::new(),
+ };
+ if found.is_empty() {
+ return Vec::new();
+ }
+ for b in found {
+ if !out.contains(&b) {
+ out.push(b);
+ }
+ }
+ }
+ out
+}
+
+/// The body with a vertex (an end of one of its edges) at `p`.
+fn vertex_body(st: &ModelState, p: Vec3) -> Option<&ModelBody> {
+ bodies(st).find(|b| {
+ let tol = (b.body.size() * 1e-3).max(1e-3);
+ b.body
+ .edges(tol)
+ .is_ok_and(|edges| edges.iter().flat_map(|e| [e.points.first(), e.points.last()]).flatten().any(|v| v.dist(p) <= 1e-6 * (1.0 + p.len())))
+ })
+}
+
pub(crate) fn resolve_axis(ctx: &Ctx, d: &AxisDef) -> Result<(Vec3, Vec3)> {
match d {
AxisDef::ThroughFace { face } => line(ctx, face),
diff --git a/crates/doc/src/eval.rs b/crates/doc/src/eval.rs
index 6e50a9a982..f304b13fd9 100644
--- a/crates/doc/src/eval.rs
+++ b/crates/doc/src/eval.rs
@@ -7759,23 +7759,38 @@ fn eval_feature(doc: &Document, vals: &BTreeMap<String, Value>, f: &Feature, st:
// Picks on bodies of other (moved) components are where those bodies are shown.
None => doc.resolve_plane_in(vals, plane, 0, Some(&construction_state(doc, st, f.component)))?,
};
- st.construct.push(crate::construct::Construct { feature: f.id, name: f.name.clone(), geom: crate::construct::ConstructGeom::Plane(pl) });
+ st.construct.push(crate::construct::Construct {
+ feature: f.id,
+ name: f.name.clone(),
+ geom: crate::construct::ConstructGeom::Plane(pl),
+ bodies: Vec::new(),
+ });
Ok(())
}
FeatureKind::ConstructionAxis { def } => {
- let (origin, dir) =
- crate::construct::resolve_axis(&crate::construct::Ctx { doc, vals, st: &construction_state(doc, st, f.component), depth: 0 }, def)?;
+ let cst = construction_state(doc, st, f.component);
+ let ctx = crate::construct::Ctx { doc, vals, st: &cst, depth: 0 };
+ let (origin, dir) = crate::construct::resolve_axis(&ctx, def)?;
+ let bodies = crate::construct::source_bodies(&ctx, &def.refs());
st.construct.push(crate::construct::Construct {
feature: f.id,
name: f.name.clone(),
geom: crate::construct::ConstructGeom::Axis { origin, dir },
+ bodies,
});
Ok(())
}
FeatureKind::ConstructionPoint { def } => {
- let p =
- crate::construct::resolve_point(&crate::construct::Ctx { doc, vals, st: &construction_state(doc, st, f.component), depth: 0 }, def)?;
- st.construct.push(crate::construct::Construct { feature: f.id, name: f.name.clone(), geom: crate::construct::ConstructGeom::Point(p) });
+ let cst = construction_state(doc, st, f.component);
+ let ctx = crate::construct::Ctx { doc, vals, st: &cst, depth: 0 };
+ let p = crate::construct::resolve_point(&ctx, def)?;
+ let bodies = crate::construct::source_bodies(&ctx, &def.refs());
+ st.construct.push(crate::construct::Construct {
+ feature: f.id,
+ name: f.name.clone(),
+ geom: crate::construct::ConstructGeom::Point(p),
+ bodies,
+ });
Ok(())
}
FeatureKind::BoundaryFill { tools, cells, operation, remove_tools } => {
@@ -8255,6 +8270,9 @@ fn eval_feature(doc: &Document, vals: &BTreeMap<String, Value>, f: &Feature, st:
*slot = ModelBody::traced_new(mb.name.clone(), Traced::step(nb, &[&mb.traced()]), mb.feature);
}
}
+ if !*copy {
+ move_constructions_with(doc, st, bodies, &crate::rigid(t, Vec3::ZERO, axis, ang));
+ }
Ok(())
}
FeatureKind::MeshConvert { body, tolerance } => {
@@ -8390,6 +8408,36 @@ fn construction_state<'a>(doc: &Document, st: &'a ModelState, comp: u64) -> std:
state_in_frame(doc, st, comp)
}
+/// Construction axes and points built on bodies a Move moves go with them (#10127): an axis
+/// through a cylinder turns with the cylinder, as its faces do. Only those built from the moved
+/// bodies alone (`Construct::bodies`) follow; one also held by a fixed point, a plane or another
+/// body stays. `m` is the move in the bodies' own frame; each construction is kept in its
+/// component's frame (`construction_state`), so the move is carried over into that frame.
+fn move_constructions_with(doc: &Document, st: &mut ModelState, moved: &[crate::BodyName], m: &Mat) {
+ let frame_of = |feature: u64| {
+ let comp = doc.feature(feature).map(|f| f.component).unwrap_or(0);
+ if comp == 0 && !doc.occurrences.is_empty() { crate::IDENTITY } else { doc.component_transform(comp) }
+ };
+ let world: BTreeMap<&str, Mat> =
+ st.bodies.iter().filter(|b| moved.iter().any(|n| *n == b.name)).map(|b| (b.name.as_str(), body_world(doc, b))).collect();
+ let mut placed = Vec::new();
+ for (k, c) in st.construct.iter().enumerate() {
+ let Some(body) = c.bodies.first().and_then(|b| world.get(b.as_str())) else { continue };
+ if !c.bodies.iter().all(|b| world.contains_key(b.as_str())) {
+ continue;
+ }
+ let frame = frame_of(c.feature);
+ let (Some(body_inv), Some(frame_inv)) = (crate::mat_inverse(body), crate::mat_inverse(&frame)) else { continue };
+ let in_frame = crate::mat_mul(&crate::mat_mul(&frame_inv, body), &crate::mat_mul(m, &crate::mat_mul(&body_inv, &frame)));
+ placed.push((k, c.geom.placed(&in_frame)));
+ }
+ for (k, geom) in placed {
+ if let Some(c) = st.construct.get_mut(k) {
+ c.geom = geom;
+ }
+ }
+}
+
/// Whether `plane` is given by the origin planes and numbers alone.
fn plane_is_fixed(plane: &PlaneRef) -> bool {
match plane {
diff --git a/crates/engine/src/construct_tests.rs b/crates/engine/src/construct_tests.rs
index 8bea30fe2a..bb3601296b 100644
--- a/crates/engine/src/construct_tests.rs
+++ b/crates/engine/src/construct_tests.rs
@@ -789,3 +789,27 @@ fn hiding_an_occurrence_hides_its_construction_geometry() {
assert!(!s.construction_visible(name), "{name} shows in a hidden placement");
}
}
+
+/// #10127: an axis and a point built on a component's cylinder turn with it when Move/Copy
+/// turns the cylinder, as its faces do. One also pinned to a fixed point stays where it is.
+#[test]
+fn construction_on_a_moved_body_moves_with_it() {
+ let mut s = Session::default();
+ run(&mut s, "solid.cylinder", json!({"radius": 5, "height": 20, "body_name": "Pin"}));
+ run(&mut s, "component.create", json!({"name": "Part", "parent": "root", "activate": false}));
+ run(&mut s, "component.move_bodies", json!({"bodies": ["Pin"], "component": "Part"}));
+ run(&mut s, "construct.axis.through_cylinder", json!({"face": {"face": [5, 0, 10]}, "name": "PinAxis"}));
+ run(&mut s, "construct.point.center", json!({"of": {"edge": [5, 0, 20]}, "name": "PinTop"}));
+ run(&mut s, "construct.axis.two_points", json!({"a": {"construct_point": "PinTop"}, "b": {"point": [0, 50, 20]}, "name": "Tether"}));
+ let body = s.model.state().bodies.first().map(|b| b.name.clone()).unwrap();
+ // A quarter turn about Y through the origin, then 10 mm along X: the top centre (0, 0, 20)
+ // goes to (30, 0, 0), and the cylinder's axis lies along X.
+ run(&mut s, "solid.move", json!({"bodies": [body], "translate": [10, 0, 0], "axis": [0, 1, 0], "angle": "90 deg"}));
+ let errors: Vec<String> = s.model.results.iter().filter_map(|r| r.error.clone()).collect();
+ assert!(errors.is_empty(), "{errors:?}");
+ let (o, d) = axis(&s, "PinAxis");
+ assert!(parallel(d, Vec3::X) && on_line(o, d, Vec3::new(30.0, 0.0, 0.0)), "{o:?} {d:?}");
+ assert!(near(point(&s, "PinTop"), Vec3::new(30.0, 0.0, 0.0)), "{:?}", point(&s, "PinTop"));
+ let (o, d) = axis(&s, "Tether");
+ assert!(on_line(o, d, Vec3::new(0.0, 0.0, 20.0)) && on_line(o, d, Vec3::new(0.0, 50.0, 20.0)), "{o:?} {d:?}");
+}