Files
solidcraft/crates/doc/src/component_removal.rs
T
raykholo e1bf784014 Deleting an inserted file removes its components, placements and joints (#4100) (#4113)
* claim: #4100

* Deleting an inserted file removes its components, placements and joints (#4100)

A STEP or mesh file inserted into a design makes a component tree. Deleting
its feature left that tree behind: the components, their occurrences, the
rigid joints that hold them and the bodies' component assignments. A later
body that took an imported body's name was then placed in the stale component.

Document::remove_component_tree removes a component subtree and everything
that refers to it: the features made inside it, its occurrences, the joints
on them with their motion links, joint origins, contact set members, motion
study keys and exploded moves, body component and offset entries, standard
part and link records, and configuration columns. Deleting an inserted
file's feature and component.delete both use it; component.delete now also
removes joints left pointing at a deleted occurrence. The body transfer
refusal moves with it, unchanged.

* Only an inserted file's feature takes its component tree when deleted

Mark the feature a STEP, IGES or mesh insert makes (`inserted`), and remove
the component tree on delete only for those and for a linked design's
feature. Deleting a copied component's independent base deletes just that
feature, as before. Older designs get the mark as they open (pending format
change), except copied components' independent bases.
2026-10-23 14:31:08 +00:00

93 lines
4.8 KiB
Rust

//! Removing a component tree: its components, the features made in them, their placements, and
//! every assembly record that refers to them.
use std::collections::BTreeSet;
use crate::{DocError, Document, FeatureKind, Result};
/// What [`Document::remove_component_tree`] removed.
#[derive(Clone, Debug, Default, PartialEq, Eq)]
pub struct RemovedTree {
/// The removed components: the root and everything inside it.
pub components: Vec<u64>,
/// The features that were inside them, in timeline order.
pub features: Vec<u64>,
/// Every deleted feature: `features`, then features elsewhere built on their sketches.
pub deleted: Vec<u64>,
/// The removed occurrences: placements of the components and placements inside them.
pub occurrences: Vec<u64>,
/// The removed joints: those with either side on a removed occurrence.
pub joints: Vec<u64>,
}
impl Document {
/// Did feature `f` make the component tree it sits at the top of: an inserted file
/// (`Feature::inserted`) or a linked design's bodies? Deleting it then removes that tree.
pub fn feature_owns_component(&self, f: &crate::Feature) -> bool {
f.component != 0
&& matches!(f.kind, FeatureKind::Import { .. } | FeatureKind::MeshImport { .. })
&& (f.inserted || self.links.get(&f.component).is_some_and(|link| link.feature == f.id))
}
/// Remove component `root` with every component inside it, the features made in them, their
/// occurrences, the joints, motion links, joint origins, contact sets, motion keys and
/// exploded moves that refer to those occurrences, the bodies' component assignments, and the
/// components' standard-part, link, appearance and configuration records.
///
/// Refused (nothing changes) for the design itself, an unknown component, or a tree that
/// supplies a body transfer recorded outside it.
pub fn remove_component_tree(&mut self, root: u64) -> Result<RemovedTree> {
if root == 0 {
return Err(DocError::Invalid("the design itself can't be deleted".into()));
}
if !self.components.iter().any(|c| c.id == root) {
return Err(DocError::Unknown(format!("component {root}")));
}
let components: Vec<u64> = self.components.iter().filter(|c| self.component_within(c.id, root)).map(|c| c.id).collect();
let inside: BTreeSet<u64> = components.iter().copied().collect();
if self.features.iter().any(|feature| {
!inside.contains(&feature.component)
&& matches!(&feature.kind, FeatureKind::BodyTransfer { from, to, .. } if inside.contains(from) || inside.contains(to))
}) {
return Err(DocError::Invalid(
"component supplies a recorded body transfer outside this subtree; remove that transfer history before deleting its source".into(),
));
}
let features: Vec<u64> = self.features.iter().filter(|f| inside.contains(&f.component)).map(|f| f.id).collect();
let deleted = if features.is_empty() { Vec::new() } else { self.delete_features(&features)? };
let occurrences: BTreeSet<u64> =
self.occurrences.iter().filter(|o| inside.contains(&o.component) || inside.contains(&o.parent)).map(|o| o.id).collect();
let joints: BTreeSet<u64> = self
.assembly
.joints
.iter()
.filter(|j| occurrences.contains(&j.a.occurrence) || occurrences.contains(&j.b.occurrence))
.map(|j| j.id)
.collect();
let assembly = &mut self.assembly;
assembly.joints.retain(|j| !joints.contains(&j.id));
assembly.links.retain(|l| !joints.contains(&l.a) && !joints.contains(&l.b));
assembly.origins.retain(|o| !occurrences.contains(&o.origin.occurrence));
for set in &mut assembly.contact_sets {
set.occurrences.retain(|o| !occurrences.contains(o));
}
for study in &mut assembly.studies {
study.keys.retain(|k| !joints.contains(&k.joint));
}
for view in &mut assembly.exploded {
view.moves.retain(|(o, _)| !occurrences.contains(o));
}
self.occurrences.retain(|o| !occurrences.contains(&o.id));
let bodies: BTreeSet<String> = self.body_components.iter().filter(|(_, c)| inside.contains(*c)).map(|(name, _)| name.clone()).collect();
self.body_components.retain(|_, c| !inside.contains(c));
self.body_offsets.retain(|name, _| !bodies.contains(name));
self.components.retain(|c| !inside.contains(&c.id));
self.parts.retain(|c, _| !inside.contains(c));
self.links.retain(|c, _| !inside.contains(c));
self.remove_configuration_components(&components);
Ok(RemovedTree { components, features, deleted, occurrences: occurrences.into_iter().collect(), joints: joints.into_iter().collect() })
}
}