Skip to content

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

Paste and Move Sketches: geometry pasted while an exploded view is shown stays put when the view closes (#12580)

Daniel304aba2a49fc
diff --git a/crates/engine/src/cmd/clipboard.rs b/crates/engine/src/cmd/clipboard.rs
index 5667564185..7fc75dbb2e 100644
--- a/crates/engine/src/cmd/clipboard.rs
+++ b/crates/engine/src/cmd/clipboard.rs
@@ -102,13 +102,14 @@ fn paste(s: &mut Session, p: &Value) -> Result<Value> {
let author_path = match given_path {
Some(path) => path,
None if comp == s.active_component => s.active_occurrence_path.clone().unwrap_or_else(|| s.doc.first_path(comp)),
- None => super::component::placing::placement_of(s, &s.placed_document(), comp, None, "component_path", cmd)?.0,
+ None => super::component::placing::placement_of(s, &s.design_document(), comp, None, "component_path", cmd)?.0,
};
- solidcraft_doc::EmbossFace::component_frame(&s.placed_document(), comp, &author_path)?;
+ solidcraft_doc::EmbossFace::component_frame(&s.design_document(), comp, &author_path)?;
// A copy from another component goes where its original is in the world: its geometry moves
// from its component's frame (its first placement, where its bodies have their own names)
- // into the target's.
- let placed = s.placed_document();
+ // into the target's. The design's placements: an exploded view shown is left out, or the copy
+ // would keep its offset once the view is closed.
+ let placed = s.design_document();
let target_frame = placed.path_transform(comp, &author_path).ok_or_else(|| bad(cmd, "that path is not a placement of the target"))?;
let to_target = solidcraft_doc::mat_inverse(&target_frame).ok_or_else(|| bad(cmd, "the target's frame is not invertible"))?;
let frame_change: BTreeMap<u64, solidcraft_doc::Mat> = feats
@@ -373,6 +374,33 @@ mod tests {
}
}
+ /// A sketch pasted from another design while an exploded view moves the target is where the
+ /// original is once the target is shown assembled again.
+ #[test]
+ fn foreign_sketch_pasted_while_an_exploded_view_is_shown_keeps_its_world_position() {
+ let mut source = dimensioned_sketch();
+ run(&mut source, "timeline.copy", json!({"features": ["S"]}));
+ let mut target = Session::default();
+ let made = run(&mut target, "component.create", json!({"name": "Target", "activate": false}));
+ let component = made["component"].as_u64().unwrap();
+ let occurrence = made["occurrence"].as_u64().unwrap();
+ run(&mut target, "occurrence.ground", json!({"occurrence": occurrence, "grounded": false}));
+ run(&mut target, "occurrence.move", json!({"occurrence": occurrence, "translate": [100, 20, 30]}));
+ run(&mut target, "component.create", json!({"name": "B", "parent": 0, "activate": false}));
+ let view =
+ run(&mut target, "explode.create", json!({"moves": [{"occurrence": occurrence, "translate": [0, 0, 40]}]}))["view"].as_u64().unwrap();
+ run(&mut target, "explode.show", json!({"view": view}));
+ target.foreign_copy = source.foreign_copy.clone();
+ let id = run(&mut target, "timeline.paste", json!({"from_design": true, "component": component, "component_path": [occurrence]}))["pasted"]
+ [0]["id"]
+ .as_u64()
+ .unwrap();
+ run(&mut target, "explode.show", json!({}));
+ let original = source.world_state().unwrap().sketch(1).unwrap().plane;
+ let pasted = target.world_state().unwrap().sketch(id).unwrap().plane;
+ assert!(pasted.origin.dist(original.origin) < 1e-6, "{original:?} vs {pasted:?}");
+ }
+
#[test]
fn foreign_sketch_paste_refuses_external_references_and_bad_parameters_atomically() {
let mut source = dimensioned_sketch();
@@ -496,6 +524,32 @@ mod tests {
assert_eq!(s.doc.model_body_component(state.body("Block (2)").unwrap()), a);
}
+ /// An exploded view shown while pasting is display only: once the design is shown assembled
+ /// again, the copy is where the original is, as it is without the view.
+ #[test]
+ fn features_pasted_while_an_exploded_view_is_shown_stay_where_they_were() {
+ let mut s = Session::default();
+ run(&mut s, "sketch.create", json!({"plane": "XY", "name": "S"}));
+ run(&mut s, "sketch.rectangle.two_point", json!({"p0": [0, 0], "p1": [10, 10]}));
+ run(&mut s, "sketch.finish", json!({}));
+ run(&mut s, "solid.extrude", json!({"sketch": "S", "distance": 5, "body_name": "Block"}));
+ let made = run(&mut s, "component.create", json!({"name": "A", "activate": false}));
+ let (a, at) = (made["component"].as_u64().unwrap(), made["occurrence"].as_u64().unwrap());
+ run(&mut s, "occurrence.ground", json!({"occurrence": at, "grounded": false}));
+ run(&mut s, "occurrence.move", json!({"occurrence": at, "translate": [100, 0, 0], "axis": [0, 0, 1], "angle": "90 deg"}));
+ // An exploded view needs two placements: B stays put.
+ run(&mut s, "component.create", json!({"name": "B", "parent": 0, "activate": false}));
+ let view = run(&mut s, "explode.create", json!({"moves": [{"occurrence": at, "translate": [0, 50, 0]}]}))["view"].as_u64().unwrap();
+ run(&mut s, "explode.show", json!({"view": view}));
+ run(&mut s, "timeline.copy", json!({"features": ["S", "Extrude1"]}));
+ let r = run(&mut s, "timeline.paste", json!({"component": a, "component_path": [at]}));
+ assert_eq!(r["errors"].as_array().map(Vec::len), Some(0), "{r}");
+ run(&mut s, "explode.show", json!({}));
+ let world = s.world_state().unwrap();
+ let (original, copy) = (world.body("Block").unwrap().mesh().bounds(), world.body("Block (2)").unwrap().mesh().bounds());
+ assert!(copy.min.dist(original.min) < 1e-6 && copy.max.dist(original.max) < 1e-6, "{original:?} vs {copy:?}");
+ }
+
/// A sketch attached to a face of a body that isn't copied with it can't be carried into a
/// component placed elsewhere: the paste is refused, saying to make its plane fixed first.
#[test]
diff --git a/crates/engine/src/cmd/clipboard_foreign.rs b/crates/engine/src/cmd/clipboard_foreign.rs
index afdc96c786..c69124a9fc 100644
--- a/crates/engine/src/cmd/clipboard_foreign.rs
+++ b/crates/engine/src/cmd/clipboard_foreign.rs
@@ -192,9 +192,10 @@ pub(super) fn paste(s: &mut Session, p: &Value) -> Result<Value> {
.and_then(|a| a.iter().map(Value::as_u64).collect::<Option<Vec<_>>>())
.ok_or_else(|| bad(CMD, "`component_path` must list 0..64 occurrence ids"))?,
None if component == s.active_component => s.active_occurrence_path.clone().unwrap_or_else(|| s.doc.first_path(component)),
- None => super::component::placing::placement_of(s, &s.placed_document(), component, None, "component_path", CMD)?.0,
+ None => super::component::placing::placement_of(s, &s.design_document(), component, None, "component_path", CMD)?.0,
};
- let placed = s.placed_document();
+ // The target as the design places it, without an exploded view shown.
+ let placed = s.design_document();
let frame = placed.path_transform(component, &path).ok_or_else(|| bad(CMD, "that path is not a placement of the target"))?;
let into_target = solidcraft_doc::mat_inverse(&frame).ok_or_else(|| bad(CMD, "the target's frame is not invertible"))?;
drop(placed);
diff --git a/crates/engine/src/cmd/component_foreign.rs b/crates/engine/src/cmd/component_foreign.rs
index 38916848bd..8020ea0be6 100644
--- a/crates/engine/src/cmd/component_foreign.rs
+++ b/crates/engine/src/cmd/component_foreign.rs
@@ -25,9 +25,10 @@ pub(super) fn paste_from_design(s: &mut Session, p: &Value) -> Result<Value> {
let path = match given {
Some(path) => path,
None if to == s.active_component => s.active_occurrence_path.clone().unwrap_or_else(|| s.doc.first_path(to)),
- None => placing::placement_of(s, &s.placed_document(), to, None, "component_path", cmd)?.0,
+ None => placing::placement_of(s, &s.design_document(), to, None, "component_path", cmd)?.0,
};
- let frame = s.placed_document().path_transform(to, &path).ok_or_else(|| bad(cmd, "that path is not a placement of the target"))?;
+ // The target as the design places it, without an exploded view shown.
+ let frame = s.design_document().path_transform(to, &path).ok_or_else(|| bad(cmd, "that path is not a placement of the target"))?;
let into_target = solidcraft_doc::mat_inverse(&frame).ok_or_else(|| bad(cmd, "the target's frame is not invertible"))?;
if copy.bodies.is_empty() {
return paste_components(s, &copy, to, path, into_target, p);
diff --git a/crates/engine/src/cmd/component_sketches_to_parts.rs b/crates/engine/src/cmd/component_sketches_to_parts.rs
index f2e2dae2dc..ecd33ceec2 100644
--- a/crates/engine/src/cmd/component_sketches_to_parts.rs
+++ b/crates/engine/src/cmd/component_sketches_to_parts.rs
@@ -36,7 +36,9 @@ pub(super) fn reauthor(s: &mut Session, id: u64, to: u64, cmd: &str) -> Result<(
if from == to {
return Ok(());
}
- let doc = s.placed_document();
+ // Both placements as the design has them: an exploded view shown would move the sketch by
+ // the difference of its offsets.
+ let doc = s.design_document();
let path = if to == anchor.component {
anchor.path.clone()
} else if to == 0 {
diff --git a/crates/engine/src/cmd/occurrence_tests.rs b/crates/engine/src/cmd/occurrence_tests.rs
index b2c8552f90..bbbe01b0cd 100644
--- a/crates/engine/src/cmd/occurrence_tests.rs
+++ b/crates/engine/src/cmd/occurrence_tests.rs
@@ -1830,6 +1830,31 @@ fn a_body_copied_in_another_design_pastes_as_an_imported_body() {
assert!(s.model.state().body("Cube (2)").is_none());
}
+/// A body pasted from another design while an exploded view moves the target component is at
+/// the world coordinates it had there once the target is shown assembled again.
+#[test]
+fn a_body_pasted_from_another_design_while_an_exploded_view_is_shown_keeps_its_world_position() {
+ let mut source = Session::default();
+ source.doc_mut().name = "Bracket".into();
+ run(&mut source, "solid.box", json!({"length": 2, "width": 2, "height": 2, "body_name": "Cube"}));
+ let was = source.world_state().unwrap().body("Cube").unwrap().mesh().bounds();
+ let copy = crate::ForeignCopy::of(&source, vec!["Cube".into()], Vec::new()).unwrap();
+
+ let mut s = Session::default();
+ run(&mut s, "solid.box", json!({"length": 1, "width": 1, "height": 1, "body_name": "Block"}));
+ let (a, a_at) = created(&mut s, "A");
+ run(&mut s, "occurrence.move", json!({"occurrence": a_at, "translate": [100,0,0]}));
+ // An exploded view needs two placements: B stays put.
+ run(&mut s, "component.create", json!({"name": "B", "parent": 0, "activate": false}));
+ let view = run(&mut s, "explode.create", json!({"moves": [{"occurrence": a_at, "translate": [0, 60, 0]}]}))["view"].as_u64().unwrap();
+ run(&mut s, "explode.show", json!({"view": view}));
+ s.foreign_copy = Some(std::sync::Arc::new(copy));
+ run(&mut s, "component.paste_from_design", json!({"component": a, "component_path": [a_at]}));
+ run(&mut s, "explode.show", json!({}));
+ let is = s.world_state().unwrap().body("Cube").unwrap().mesh().bounds();
+ assert!(was.min.dist(is.min) < 1e-6 && was.max.dist(is.max) < 1e-6, "{was:?} vs {is:?}");
+}
+
/// A body seen in another placement of its component ("Cube (Holder:2)") pastes into another
/// design under its own name, at that placement's position.
#[test]
diff --git a/crates/engine/src/schema/component_tests.rs b/crates/engine/src/schema/component_tests.rs
index 983c8705bc..c03d84f302 100644
--- a/crates/engine/src/schema/component_tests.rs
+++ b/crates/engine/src/schema/component_tests.rs
@@ -134,3 +134,42 @@ fn sketches_move_to_the_part_whose_face_they_are_on() {
let again = run(&mut s, "component.move_sketches.to_parts", json!({"sketches":["OnFace"]}));
assert_eq!((again["moved"].as_u64(), again["kept"][0]["reason"].as_str()), (Some(0), Some("already in its part")));
}
+
+/// A sketch moved to its part while an exploded view moves the part saves the same picked point
+/// and guide plane as without the view: the view is display only.
+#[test]
+fn sketches_moved_to_a_part_while_an_exploded_view_is_shown_save_what_they_save_without_it() {
+ let run = |s: &mut Session, c: &str, p: Value| s.execute(c, &p).unwrap_or_else(|e| panic!("{c}: {e}"));
+ let moved = |exploded: bool| {
+ let mut s = Session::default();
+ let part = run(&mut s, "component.create", json!({"name":"Part","parent":0,"grounded":false}));
+ let occurrence = part["occurrence"].as_u64().unwrap();
+ run(&mut s, "solid.box", json!({"length":10,"width":10,"height":10,"body_name":"Base"}));
+ run(&mut s, "occurrence.move", json!({"occurrence":occurrence,"translate":[30,0,0]}));
+ run(&mut s, "component.activate", json!({"component":0}));
+ let world = s.world_state().unwrap();
+ let base = world.body("Base").unwrap();
+ let top = base.body.faces(0.01).unwrap().into_iter().max_by(|a, b| a.centroid.z.total_cmp(&b.centroid.z)).unwrap();
+ let pick = json!({"body": base.name, "index": top.index, "face": [top.centroid.x, top.centroid.y, top.centroid.z], "path": [occurrence]});
+ let on_face = run(&mut s, "sketch.create", json!({"name":"OnFace","plane":pick,"project_edges":false}))["sketch"].as_u64().unwrap();
+ sketch_square(&mut s);
+ // An exploded view needs two placements: B stays put, in both runs.
+ run(&mut s, "component.create", json!({"name":"B","parent":0,"activate":false}));
+ if exploded {
+ let view =
+ run(&mut s, "explode.create", json!({"moves": [{"occurrence": occurrence, "translate": [0, 25, 0]}]}))["view"].as_u64().unwrap();
+ run(&mut s, "explode.show", json!({"view": view}));
+ }
+ run(&mut s, "component.move_sketches.to_parts", json!({}));
+ run(&mut s, "explode.show", json!({}));
+ let solidcraft_doc::FeatureKind::Sketch { plane: solidcraft_doc::PlaneRef::Face { plane, at, .. }, .. } =
+ &s.doc.feature(on_face).unwrap().kind
+ else {
+ panic!("not a face sketch")
+ };
+ (*plane, *at)
+ };
+ let ((plane, at), (assembled_plane, assembled_at)) = (moved(true), moved(false));
+ assert!(at.dist(assembled_at) < 1e-9, "{at:?} vs {assembled_at:?}");
+ assert!(plane.origin.dist(assembled_plane.origin) < 1e-9, "{plane:?} vs {assembled_plane:?}");
+}