Delete: deleting one instance of a component takes its joints with it (#7749) (#7782)

* claim: #7749

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* Delete: deleting one instance of a component takes its joints with it (#7749)

Deleting an occurrence while its component had other instances dropped
only the occurrence: the joints on it, their motion links, joint origins,
contact set places and exploded moves stayed, so a joint stayed in the
Browser naming an occurrence that was gone. The clean-up that
remove_component_tree did is now Document::remove_occurrences, used by
both.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

---------

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
semmelrinde
2026-10-27 18:44:44 +00:00
committed by GitHub
parent ee6ec09b0a
commit 98658142c0
2 changed files with 46 additions and 10 deletions
+17 -9
View File
@@ -58,6 +58,22 @@ impl Document {
let occurrences: BTreeSet<u64> =
self.occurrences.iter().filter(|o| inside.contains(&o.component) || inside.contains(&o.parent)).map(|o| o.id).collect();
let joints = self.remove_occurrences(&occurrences);
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() })
}
/// Remove these occurrences and every assembly record that refers to them: the joints with
/// either side on one (with those joints' motion links and motion keys), the joint origins
/// on them, their places in contact sets and their exploded moves. Returns the joints removed.
pub fn remove_occurrences(&mut self, occurrences: &BTreeSet<u64>) -> BTreeSet<u64> {
let joints: BTreeSet<u64> = self
.assembly
.joints
@@ -79,14 +95,6 @@ impl Document {
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() })
joints
}
}
+29 -1
View File
@@ -160,7 +160,11 @@ fn apply(s: &mut Session, p: &Value) -> Result<Value> {
return Err(bad(cmd, format!("no occurrence `{k}`")));
};
if s.doc.occurrences.iter().filter(|x| x.component == o.component).count() > 1 {
s.doc_mut().occurrences.retain(|x| x.id != o.id);
// The joints on it and the other assembly records naming it go with it: left behind,
// a joint stays in the Browser joined to nothing (#7749).
let gone = s.doc_mut().remove_occurrences(&std::collections::BTreeSet::from([o.id]));
s.visibility.hidden_joints.retain(|h| !gone.contains(h));
joints.extend(gone);
s.pending_moves.remove(&o.id);
done = true;
} else {
@@ -406,6 +410,30 @@ mod tests {
}
}
/// An instance deleted while its component has others goes alone, and the joints on it go
/// with it, with their motion links (#7749): left behind, a joint names an occurrence that is
/// gone, and every solve reports it.
#[test]
fn an_instance_deleted_alone_takes_its_joints_with_it() {
let mut s = joint_assembly();
let b1 = s.doc.occurrences.iter().find(|o| o.name == "B:1").unwrap().id;
let b2 = run(&mut s, "occurrence.copy", json!({"occurrence": "B:1", "translate": [0, 40, 0]}))["occurrence"].as_u64().unwrap();
let dry = run(&mut s, "selection.delete", json!({"occurrences": [b1], "dry_run": true}));
assert_eq!(s.doc.assembly.joints.len(), 2, "a dry run changes nothing");
let deleted = run(&mut s, "selection.delete", json!({"occurrences": [b1]}));
assert_eq!(deleted, dry);
let left: Vec<&str> = s.doc.assembly.joints.iter().map(|j| j.name.as_str()).collect();
assert_eq!(left, ["Glue"], "the joint on the deleted instance goes with it");
assert!(s.doc.assembly.links.is_empty(), "and so does its motion link");
assert_eq!(deleted["deleted_joints"].as_array().map(Vec::len), Some(1), "the reply names it: {deleted}");
assert!(s.doc.occurrences.iter().any(|o| o.id == b2), "the other instance stays");
let solved = run(&mut s, "joint.solve", json!({}));
assert_eq!(solved["errors"], json!([]), "no joint is left pointing at the deleted instance");
run(&mut s, "edit.undo", json!({}));
assert_eq!(s.doc.assembly.joints.len(), 2);
assert_eq!(s.doc.assembly.links.len(), 1);
}
#[test]
fn occurrences_components_and_sketch_entities() {
let (mut s, _) = part();