Files
solidcraft/crates/doc/src/feature_refs.rs
T
d6d6c7d9ee Copy and paste bodies and components between designs (#3511) (#3512)
* 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>
2026-10-29 16:31:09 +00:00

112 lines
4.2 KiB
Rust

//! References from a feature to the rest of the design: the sketches it is built from (by id),
//! the features it names (patterned and mirrored features and the construction plane a plane is
//! built on, by id since #1134; construction axes and points, still by name) and the bodies it
//! works on (by name). The walk (`crate::walk`) is behind
//! `input_sketches`, `remap_refs` and `remap_bodies`.
use std::collections::BTreeMap;
use crate::walk::Walk;
use crate::{BodyName, FeatureId, FeatureKind, FeatureName, SketchId};
/// A reference a feature holds: `S` is `&SketchId` or `&mut SketchId`, and so on. `Feature`
/// carries a feature id (pattern and mirror seeds, construction planes); `FeatureName` carries
/// a feature name, for the references that are still keyed by name (construction axes and
/// points).
pub(crate) enum Ref<S, F, N, B> {
Sketch(S),
Feature(F),
FeatureName(N),
Body(B),
}
impl FeatureKind {
/// Sketches the feature is built from (profiles, paths, points): deleting one of them
/// deletes the feature.
pub fn input_sketches(&self) -> Vec<u64> {
let mut v = Vec::new();
self.walk_refs(&mut |r| {
if let Ref::Sketch(id) = r {
v.push(id.0);
}
});
v
}
/// Whether the feature refers to the feature `id` (by id: a patterned or mirrored feature,
/// the construction plane a plane is built on) or names it (`name`: a construction axis or
/// point it is built from). Used to refuse a feature built on itself (#3567).
pub fn names_feature(&self, id: u64, name: &str) -> bool {
let mut found = false;
self.walk_refs(&mut |r| match r {
Ref::Feature(x) => found |= x.0 == id,
Ref::FeatureName(n) => found |= n.as_ref() == name,
_ => {}
});
found
}
/// Bodies the feature works on (by name).
pub fn input_bodies(&self) -> Vec<String> {
let mut v = Vec::new();
self.walk_refs(&mut |r| {
if let Ref::Body(name) = r {
v.push(name.as_ref().to_string());
}
});
v
}
/// Point references to sketches and features (both by id) at their copies, and references
/// that are still keyed by name at a feature's new name (`timeline.rename`).
pub fn remap_refs(&mut self, ids: &BTreeMap<u64, u64>, names: &BTreeMap<String, String>) {
self.walk_refs_mut(&mut |r| match r {
Ref::Sketch(id) => {
if let Some(n) = ids.get(&id.0) {
*id = SketchId(*n);
}
}
Ref::Feature(id) => {
if let Some(n) = ids.get(&id.0) {
*id = FeatureId(*n);
}
}
Ref::FeatureName(name) => {
if let Some(n) = names.get(name.as_ref()) {
*name = FeatureName(n.clone());
}
}
Ref::Body(_) => {}
});
}
/// Point references to bodies (by name) at their copies.
pub fn remap_bodies(&mut self, bodies: &BTreeMap<String, String>) {
self.walk_refs_mut(&mut |r| {
if let Ref::Body(name) = r
&& let Some(n) = bodies.get(name.as_ref())
{
*name = BodyName(n.clone());
}
});
}
}
impl crate::Document {
/// Rename feature `old` to `new` where the design names it outside the features' own
/// references (`remap_refs`): sketch projections of a construction plane (#4353). The
/// features are the rename command's to change; feature references inside features are keyed
/// by id since #1134 and follow a rename on their own.
pub fn rename_feature_keys(&mut self, old: &str, new: &str) {
let names = BTreeMap::from([(old.to_string(), new.to_string())]);
let (ids, bodies) = (BTreeMap::new(), BTreeMap::new());
for f in &mut self.features {
if let FeatureKind::Sketch { sketch, .. } = &mut f.kind {
for link in &mut sketch.links {
crate::clipboard::remap_link(&mut link.source, &ids, &names, &bodies, 0);
}
}
}
}
}