* Components: copy and paste bodies and components between designs and components (#3390) Squashed for the rebase onto main. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * claim: #3511 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * Paste New: copy from a design other than the one pasted into `component_clone::paste` read the design it copies from through the session it pastes into. `build` now takes the source (document, evaluated model, unapplied moves) apart from the target session; Paste New within one design passes the session as both, as before. Names are numbered against the target's names. From another design (`Source::copied`, a `ForeignCopy` snapshot taken at copy time): - names are kept where the target doesn't have them, else numbered; - the user parameters the copy uses come along, `w_2` where the target has a `w`, with the copied expressions following; - a copy whose features use what isn't in it (bodies, sketches, faces, parameters outside it) is refused with a list of what; `FeatureKind::input_bodies` lists the bodies a feature uses; - configurations stay with their design, and the target's timeline marker must be at the end; - a body moved in from outside comes as an imported base named `BodyImport1`. Body transfers are pointed at the copy's components before they are added: `add_feature` checks that the components they name exist, which another design's don't. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * Engine: paste bodies and components copied in another design `component.paste_from_design` pastes what `Session::foreign_copy` holds, at the same world coordinates as in its design, into the active component or `component` (placed by `component_path`; a component placed more than once gets it in every instance), as one step to undo: - bodies as imported bodies, without their history, in one Import feature (named as any import is, "Import1"); a body seen in another placement ("Cube (B:2)") pastes as "Cube"; names the target has are numbered; - components as independent copies (Paste New from the snapshot): with their history, appearances, materials, joints and motion inside them; joints to the outside stay behind. Tests: bodies (position, naming, target component, undo; a body of another placement) and components (history, appearances, a part placed twice, editing independently, save and reopen, undo; user parameters renamed; refused when using a body outside it; bodies moved in). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * Clipboard: copy in one design, paste in another Copying or cutting bodies or components also keeps a snapshot of them (`ForeignCopy`); another tab pastes it with `component.paste_from_design`: bodies as imported bodies, components as independent copies (Paste and Paste New alike), folded and selected. Cut pastes as a copy there, the original staying; Esc on the cut leaves nothing to paste anywhere. The last copy in any design is what pastes: each copy or cut is numbered, and a tab whose own copy is older empties its clipboard when it is switched to (`drop_stale`). A failed copy anywhere leaves nothing to paste in any tab. Tests: bodies and components pasted in another tab, the last copy winning across tabs, cut as a copy. `copied_bodies_stay_with_their_design` gave way to the cross-design test. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * Clipboard: what is selected wins over a design row picked earlier Copy copied the whole design as a linked component whenever the design's browser row was still picked, even after a component, body or feature had been selected in the view or another tab, and failed in an unsaved design. The design row now gives way to anything selected, and switching tabs drops the picked components of the design left. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * Put the Paste from Another Design label in the pending locale file Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * Tests: unground the placement before moving it (a design's first part is grounded) Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * Drop locale keys main already folded into en.json Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * Pending locale: Paste from Another Design in the complete catalogs Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> --------- Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com> Co-authored-by: bherbruck <brennen.herbruck@gmail.com>
135 lines
7.3 KiB
Rust
135 lines
7.3 KiB
Rust
#![allow(clippy::unwrap_used)]
|
|
use crate::{ForeignCopy, Session};
|
|
use serde_json::{Value, json};
|
|
use std::sync::Arc;
|
|
|
|
fn run(s: &mut Session, cmd: &str, p: Value) -> Value {
|
|
s.execute(cmd, &p).unwrap()
|
|
}
|
|
fn created(s: &mut Session, name: &str) -> (u64, u64) {
|
|
let c = run(s, "component.create", json!({"name": name}));
|
|
(c["component"].as_u64().unwrap(), c["occurrence"].as_u64().unwrap())
|
|
}
|
|
fn volume(s: &mut Session, id: u64) -> f64 {
|
|
run(s, "component.properties", json!({"component": id}))["total"]["volume_mm3"].as_f64().unwrap()
|
|
}
|
|
fn design(name: &str) -> Session {
|
|
let mut s = Session::default();
|
|
s.doc_mut().name = name.into();
|
|
s
|
|
}
|
|
/// `s` copies its placement `path` for another design.
|
|
fn copied(s: &Session, path: Vec<u64>) -> Option<Arc<ForeignCopy>> {
|
|
Some(Arc::new(ForeignCopy::of(s, Vec::new(), vec![path])))
|
|
}
|
|
|
|
/// A component copied in another design pastes as an independent copy of it and what it holds:
|
|
/// its history, its appearances, a part placed twice in it still one component placed twice,
|
|
/// where it is in the world, named "(2)" where the name is taken, as one step to undo.
|
|
#[test]
|
|
fn a_component_from_another_design_pastes_with_its_history() {
|
|
let mut source = design("Bracket");
|
|
let (assembly, at) = created(&mut source, "Assembly");
|
|
let (child, _) = created(&mut source, "Child");
|
|
run(&mut source, "solid.box", json!({"length": 2, "width": 2, "height": 2, "body_name": "Cube"}));
|
|
run(&mut source, "appearance.assign", json!({"bodies": ["Cube"], "color": "#abcdef"}));
|
|
run(&mut source, "occurrence.copy", json!({"component": child}));
|
|
run(&mut source, "component.activate", json!({"component": 0}));
|
|
run(&mut source, "occurrence.ground", json!({"occurrence": at, "grounded": false}));
|
|
run(&mut source, "occurrence.move", json!({"occurrence": at, "translate": [0, 30, 0]}));
|
|
let was = source.world_state().body("Cube").unwrap().mesh().bounds();
|
|
let box_features = source.doc.features.len();
|
|
|
|
let mut s = design("Frame");
|
|
created(&mut s, "Assembly");
|
|
run(&mut s, "component.activate", json!({"component": 0}));
|
|
s.foreign_copy = copied(&source, vec![at]);
|
|
let undo = s.undo.len();
|
|
let out = run(&mut s, "component.paste_from_design", json!({}));
|
|
assert_eq!(s.undo.len(), undo + 1);
|
|
let new = s.doc.components.iter().find(|c| c.name == "Assembly (2)").expect("named (2)").id;
|
|
assert_eq!(out["paths"], json!([[s.doc.occurrence_of(new).unwrap().id]]));
|
|
let kids: Vec<_> = s.doc.occurrences.iter().filter(|o| o.parent == new).collect();
|
|
assert_eq!(kids.len(), 2);
|
|
assert_eq!(kids[0].component, kids[1].component, "still one component placed twice");
|
|
assert_eq!(s.doc.features.len(), box_features, "the box's history came along");
|
|
assert!((volume(&mut s, new) - 16.0).abs() < 1e-6);
|
|
assert_eq!(s.doc.appearances.bodies["Cube"].color, [0xab, 0xcd, 0xef]);
|
|
let is = s.world_state().body("Cube").unwrap().mesh().bounds();
|
|
assert!(was.min.dist(is.min) < 1e-6 && was.max.dist(is.max) < 1e-6, "{was:?} vs {is:?}");
|
|
// Its history is its own: changing it here leaves the original as it was.
|
|
let copy = s.doc.features.iter().find(|f| matches!(f.kind, solidcraft_doc::FeatureKind::Box { .. })).unwrap();
|
|
let parameter = copy.param_names[0].clone();
|
|
run(&mut s, "parameters.change", json!({"name": parameter, "expression": "3 mm"}));
|
|
assert!((volume(&mut s, new) - 24.0).abs() < 1e-6);
|
|
assert!((volume(&mut source, assembly) - 16.0).abs() < 1e-6);
|
|
let reopened = solidcraft_doc::Document::from_json(&s.doc.to_json()).unwrap();
|
|
assert_eq!(reopened.occurrences.iter().filter(|o| o.parent == new).count(), 2);
|
|
run(&mut s, "edit.undo", json!({}));
|
|
run(&mut s, "edit.undo", json!({}));
|
|
assert!(s.doc.components.iter().all(|c| c.name != "Assembly (2)"));
|
|
}
|
|
|
|
/// The user parameters a pasted component uses come along: as they were where the name is
|
|
/// free, else renamed `w_2`, its features following; this design's own `w` stays as it is.
|
|
#[test]
|
|
fn user_parameters_come_along_renamed_where_taken() {
|
|
let mut source = design("Bracket");
|
|
run(&mut source, "parameters.add", json!({"name": "w", "expression": "4 mm"}));
|
|
let (_, at) = created(&mut source, "Part");
|
|
let feature = run(&mut source, "solid.box", json!({"length": 2, "width": 2, "height": 2, "body_name": "Cube"}))["feature"].as_u64().unwrap();
|
|
let length = source.doc.feature(feature).unwrap().param_names[0].clone();
|
|
run(&mut source, "parameters.change", json!({"name": length, "expression": "w"}));
|
|
|
|
let mut s = design("Frame");
|
|
run(&mut s, "parameters.add", json!({"name": "w", "expression": "10 mm"}));
|
|
s.foreign_copy = copied(&source, vec![at]);
|
|
run(&mut s, "component.paste_from_design", json!({}));
|
|
let w2 = s.doc.params.iter().find(|p| p.name == "w_2").expect("w came along as w_2");
|
|
assert_eq!(w2.expr, "4 mm");
|
|
assert_eq!(s.doc.params.iter().find(|p| p.name == "w").unwrap().expr, "10 mm");
|
|
let part = s.doc.components.iter().find(|c| c.name == "Part").unwrap().id;
|
|
assert!((volume(&mut s, part) - 16.0).abs() < 1e-6);
|
|
run(&mut s, "parameters.change", json!({"name": "w_2", "expression": "1 mm"}));
|
|
assert!((volume(&mut s, part) - 4.0).abs() < 1e-6, "the copy follows w_2");
|
|
}
|
|
|
|
/// A component whose features use what isn't in it (here a body outside it) isn't pasted into
|
|
/// another design: it is refused, saying what, and the design is left as it was.
|
|
#[test]
|
|
fn a_component_using_what_is_outside_it_is_refused_saying_what() {
|
|
let mut source = design("Bracket");
|
|
run(&mut source, "solid.box", json!({"length": 2, "width": 2, "height": 2, "body_name": "Cube"}));
|
|
let (_, at) = created(&mut source, "Part");
|
|
run(&mut source, "solid.box", json!({"length": 1, "width": 1, "height": 1, "body_name": "Pin"}));
|
|
run(&mut source, "solid.move", json!({"bodies": ["Cube"], "translate": [10, 0, 0]}));
|
|
|
|
let mut s = design("Frame");
|
|
s.foreign_copy = copied(&source, vec![at]);
|
|
let saved = s.doc.to_json();
|
|
let error = s.execute("component.paste_from_design", &json!({})).unwrap_err().to_string();
|
|
assert!(error.contains("uses body Cube"), "{error}");
|
|
assert_eq!(s.doc.to_json(), saved);
|
|
}
|
|
|
|
/// A component that bodies were moved into (a body transfer, as Create Components from Bodies
|
|
/// makes) pastes into another design, which has none of the components the transfer names.
|
|
#[test]
|
|
fn a_component_bodies_were_moved_into_pastes_into_another_design() {
|
|
let mut source = design("Bracket");
|
|
run(&mut source, "solid.box", json!({"length": 2, "width": 2, "height": 2, "body_name": "Cube"}));
|
|
let (part, at) = created(&mut source, "Part");
|
|
run(&mut source, "component.move_bodies", json!({"bodies": ["Cube"], "component": part}));
|
|
run(&mut source, "solid.box", json!({"length": 1, "width": 1, "height": 1, "body_name": "Pin"}));
|
|
|
|
let mut s = design("Frame");
|
|
s.foreign_copy = copied(&source, vec![at]);
|
|
run(&mut s, "component.paste_from_design", json!({}));
|
|
let new = s.doc.components.iter().find(|c| c.name == "Part").unwrap().id;
|
|
let state = s.model.state();
|
|
assert_eq!(s.doc.model_body_component(state.body("Cube").unwrap()), new);
|
|
assert_eq!(s.doc.model_body_component(state.body("Pin").unwrap()), new);
|
|
// The body moved in from outside comes as an imported body.
|
|
assert!(s.doc.find_feature("BodyImport1").is_some_and(|f| f.component == new));
|
|
}
|