Skip to content

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

solid.move: an inserted part moves in world space beside another body (#9660) (#9683)

rugbymauri059119b345e0
diff --git a/crates/engine/src/cmd/features.rs b/crates/engine/src/cmd/features.rs
index 18be03cd04..4f1e70c5f4 100644
--- a/crates/engine/src/cmd/features.rs
+++ b/crates/engine/src/cmd/features.rs
@@ -1807,9 +1807,44 @@ fn move_bodies(s: &mut Session, p: &Value) -> Result<Value> {
if let Some(plan) = crate::frames::placements_moved(s, p) {
return move_placements(s, p, plan);
}
+ // Bodies in differently placed frames: one Move feature per frame, each moving its bodies
+ // by the world vector and turn mapped into that frame (#9660).
+ if let Some(moves) = crate::frames::split_move(s, p) {
+ return move_split(s, &moves);
+ }
move_body_feature(s, p)
}
+/// The moves of `frames::split_move`, in one step, with one reply: the features made, and the
+/// bodies made, changed and removed by any of them.
+fn move_split(s: &mut Session, moves: &[Value]) -> Result<Value> {
+ let mut features = Vec::new();
+ let mut lists: [(&str, Vec<Value>); 3] = [("created_bodies", Vec::new()), ("changed_bodies", Vec::new()), ("removed_bodies", Vec::new())];
+ let mut last = Value::Null;
+ for p in moves {
+ let reply = move_body_feature(s, p)?;
+ features.push(reply.get("feature").cloned().unwrap_or(Value::Null));
+ for (key, list) in &mut lists {
+ for name in reply.get(*key).and_then(Value::as_array).into_iter().flatten() {
+ if !list.contains(name) {
+ list.push(name.clone());
+ }
+ }
+ }
+ last = reply;
+ }
+ if let Some(out) = last.as_object_mut() {
+ if let Some(first) = features.first() {
+ out.insert("feature".into(), first.clone());
+ }
+ out.insert("features".into(), Value::Array(features));
+ for (key, list) in lists {
+ out.insert(key.into(), Value::Array(list));
+ }
+ }
+ Ok(last)
+}
+
/// A move of several placements of one part (`frames::placements_moved`, #4768): each placement
/// moved in the world by the move's translation and turn (about the world origin, as the Move
/// feature turns bodies), and the root component's bodies listed moved as bodies, in one step.
diff --git a/crates/engine/src/frames.rs b/crates/engine/src/frames.rs
index afe7de3867..313f13e1d5 100644
--- a/crates/engine/src/frames.rs
+++ b/crates/engine/src/frames.rs
@@ -341,6 +341,9 @@ thread_local! {
static DEPTH: std::cell::Cell<u32> = const { std::cell::Cell::new(0) };
/// The outermost command's coordinates were mapped here (its table lists them).
static MAPPED: std::cell::Cell<bool> = const { std::cell::Cell::new(false) };
+ /// The outermost command is `solid.move`, whose parameters stay in the world when its bodies
+ /// lie in differently turned frames (`split_move`).
+ static OUTER_MOVE: std::cell::Cell<bool> = const { std::cell::Cell::new(false) };
/// The picks of the running command: the point it sees and the bodies whose faces meet
/// there (the first is the body under it; more than one means the pick is ambiguous).
static PICKS: std::cell::RefCell<Vec<(Vec3, Vec<String>)>> = const { std::cell::RefCell::new(Vec::new()) };
@@ -407,6 +410,7 @@ impl Nested {
if outer {
let inner = if id == "timeline.redefine" { None } else { Some(id) };
MAPPED.with(|m| m.set(inner.is_none_or(|i| !table(i).is_empty() || i == "construct.plane.axis_point")));
+ OUTER_MOVE.with(|m| m.set(id == "solid.move"));
}
(Nested, outer)
}
@@ -420,11 +424,72 @@ impl Drop for Nested {
/// `params` with world picks mapped into component frames (`None`: nothing to change).
pub fn to_local(s: &Session, id: &str, params: &Value) -> Option<Value> {
+ // A move of bodies in differently turned frames keeps its world vectors: `solid.move` splits
+ // it by frame and maps each part itself (`split_move`).
+ if id == "solid.move" && split_move(s, params).is_some() {
+ return None;
+ }
+ map_params(s, id, params)
+}
+
+/// A `solid.move` of bodies placed in different frames, as one move per frame (#9660). A Move
+/// feature moves each of its bodies in the body's own frame, by one vector and one turn: given
+/// in the world, they can be mapped into one frame only, so bodies in a frame turned otherwise
+/// (a screw inserted lying down, moved with a block of the root) moved along their own axes.
+/// Bodies whose frames turn alike share a move (and for a turn, frames that are also in the
+/// same place, as its pivot is a point). `None`: the bodies share one frame, or are moved as
+/// placements (`placements_moved`), or the move is not the outermost command (run by another,
+/// such as Edit Feature, its parameters are already in component frames). `to_local` leaves the
+/// parameters of a split move in the world, and `solid.move` asks for the parts.
+pub(crate) fn split_move(s: &Session, params: &Value) -> Option<Vec<Value>> {
+ let outermost_move = DEPTH.with(std::cell::Cell::get) == 1 && OUTER_MOVE.with(std::cell::Cell::get);
+ if !outermost_move || !placed(s) || placements_moved(s, params).is_some() {
+ return None;
+ }
+ let names = params.get("bodies")?.as_array()?;
+ let turns = params.get("axis").is_some() || params.get("angle").is_some();
+ let alike = |a: &Mat, b: &Mat| {
+ let columns = if turns { 4 } else { 3 };
+ a.iter().zip(b).take(columns).all(|(x, y)| x.iter().zip(y).take(3).all(|(u, v)| (u - v).abs() <= 1e-9 * (1.0 + u.abs().max(v.abs()))))
+ };
+ let frames = s.body_frames();
+ let mut groups: Vec<(Mat, Vec<Value>)> = Vec::new();
+ for name in names {
+ // An unknown body is the command's to refuse.
+ let b = name.as_str()?;
+ let m = frames.iter().find(|(world, own, _)| world == b || own == b).map(|(.., m)| *m)?;
+ match groups.iter_mut().find(|(g, _)| alike(g, &m)) {
+ Some((_, bodies)) => bodies.push(name.clone()),
+ None => groups.push((m, vec![name.clone()])),
+ }
+ }
+ if groups.len() < 2 {
+ return None;
+ }
+ let given = params.get("name").and_then(Value::as_str).map(str::trim).filter(|n| !n.is_empty());
+ let moves = groups
+ .into_iter()
+ .enumerate()
+ .map(|(k, (_, bodies))| {
+ let mut p = params.clone();
+ p["bodies"] = Value::Array(bodies);
+ // Each move is its own feature: a name given is numbered after the first.
+ if let Some(name) = given.filter(|_| k > 0) {
+ p["name"] = Value::String(format!("{name} ({})", k + 1));
+ }
+ map_params(s, "solid.move", &p).unwrap_or(p)
+ })
+ .collect();
+ Some(moves)
+}
+
+/// [`to_local`] for one command.
+fn map_params(s: &Session, id: &str, params: &Value) -> Option<Value> {
if id == "library.apply" {
// Library targets are the same world selections as appearance.assign,
// nested one level down. Convert at the outer boundary exactly once;
// run_nested subsequently receives the component-local picks and names.
- let targets = to_local(s, "appearance.assign", params.get("targets")?)?;
+ let targets = map_params(s, "appearance.assign", params.get("targets")?)?;
let mut out = params.clone();
out["targets"] = targets;
return Some(out);
@@ -445,7 +510,7 @@ pub fn to_local(s: &Session, id: &str, params: &Value) -> Option<Value> {
if let solidcraft_doc::FeatureKind::Emboss { face: Some(face), .. } = &feature.kind {
context.active_occurrence_path = (!face.author_path.is_empty()).then(|| face.author_path.clone());
}
- let mut inner = to_local(&context, cmd, params.get("params")?)?;
+ let mut inner = map_params(&context, cmd, params.get("params")?)?;
// An untouched explicit axis is displayed in world coordinates by Edit
// Feature. Keep its exact saved values instead of an affine round trip.
if cmd == "solid.revolve"
diff --git a/crates/engine/src/frames_tests.rs b/crates/engine/src/frames_tests.rs
index b614f8e4a5..245179a4dd 100644
--- a/crates/engine/src/frames_tests.rs
+++ b/crates/engine/src/frames_tests.rs
@@ -263,6 +263,65 @@ fn move_expression_distances_keep_parameters_through_rebuild_and_reopen() {
}
}
+/// One `solid.move` of bodies placed in differently turned frames (a block in the root and a
+/// screw inserted lying down) moves and turns each of them in the world, whichever is listed
+/// first (#9660).
+#[test]
+fn one_move_of_bodies_in_differently_turned_frames_moves_each_in_the_world() {
+ let corners = |(lo, hi): (Vec3, Vec3)| {
+ let mut out = Vec::new();
+ for x in [lo.x, hi.x] {
+ for y in [lo.y, hi.y] {
+ for z in [lo.z, hi.z] {
+ out.push(Vec3::new(x, y, z));
+ }
+ }
+ }
+ out
+ };
+ let bounds = |points: Vec<Vec3>| {
+ let mut lo = Vec3::new(f64::INFINITY, f64::INFINITY, f64::INFINITY);
+ let mut hi = Vec3::new(f64::NEG_INFINITY, f64::NEG_INFINITY, f64::NEG_INFINITY);
+ for p in points {
+ lo = Vec3::new(lo.x.min(p.x), lo.y.min(p.y), lo.z.min(p.z));
+ hi = Vec3::new(hi.x.max(p.x), hi.y.max(p.y), hi.z.max(p.z));
+ }
+ (lo, hi)
+ };
+ let close = |a: (Vec3, Vec3), b: (Vec3, Vec3)| a.0.dist(b.0) < 1e-4 && a.1.dist(b.1) < 1e-4;
+ // The quarter turn is about the world z axis, which passes through the screw's own origin
+ // (where it was inserted): a Move feature turns about that origin.
+ let moves: [(Value, &dyn Fn(Vec3) -> Vec3); 3] = [
+ (json!({"translate": [0, 0, 3]}), &|p| p + Vec3::new(0.0, 0.0, 3.0)),
+ (json!({"translate": [0, "2 mm", 0]}), &|p| p + Vec3::new(0.0, 2.0, 0.0)),
+ (json!({"axis": [0, 0, 1], "angle": "90 deg"}), &|p| Vec3::new(-p.y, p.x, p.z)),
+ ];
+ for direction in [[1, 0, 0], [0, 0, -1], [0, 1, 0]] {
+ for order in [["Block", "Body1"], ["Body1", "Block"]] {
+ for (params, expected) in &moves {
+ let mut s = Session::default();
+ run(&mut s, "solid.box", json!({"length": 30, "width": 30, "height": 10, "center": [0, 0, 5], "body_name": "Block"}));
+ let screw = json!({"family": "socket_head_cap_screw", "size": "M3", "length": 12, "point": [0, 0, 10], "direction": direction, "name": "screw"});
+ run(&mut s, "parts.insert", screw);
+ let before: Vec<_> = order.iter().map(|b| world_bbox(&s, b)).collect();
+ let mut p = params.clone();
+ p["bodies"] = json!(order);
+ run(&mut s, "solid.move", p.clone());
+ for (body, was) in order.iter().zip(&before) {
+ let want = bounds(corners(*was).into_iter().map(expected).collect());
+ let got = world_bbox(&s, body);
+ assert!(close(got, want), "{body} after {p} (screw along {direction:?}): {got:?}, want {want:?}");
+ }
+ // One step: a single undo puts both back.
+ run(&mut s, "edit.undo", json!({}));
+ for (body, was) in order.iter().zip(&before) {
+ assert!(close(world_bbox(&s, body), *was), "{body} after undoing {p}");
+ }
+ }
+ }
+ }
+}
+
/// Edit Feature passes a primitive's placement through as saved, so changing the radius of
/// Leaf B's rod in the Hinge Assembly leaves the leaf where it was (#2926).
#[test]