Skip to content

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

Rigid joint saved with the wrong occurrence when two occurrences share a component (#3857) (#3939)

Loohi145c6ad60d56
diff --git a/crates/ui-egui/src/dialogs_assembly.rs b/crates/ui-egui/src/dialogs_assembly.rs
index 4f06c1c25f..11df42f5fd 100644
--- a/crates/ui-egui/src/dialogs_assembly.rs
+++ b/crates/ui-egui/src/dialogs_assembly.rs
@@ -386,12 +386,17 @@ pub struct SnapPt {
pub param: Value,
}
-/// The occurrence placing a body (0: the root design), when the body is its primary instance.
+/// The occurrence placing a world body (0: the root design), the instance its name says.
pub(crate) fn occurrence_of_body(s: &Session, body: &str) -> Option<u64> {
+ // The world name tells the instance: a further occurrence of a component is not its first.
let st = s.world_state();
let b = st.body(body)?;
- let comp = s.doc.model_body_component(b);
- if comp == 0 { Some(0) } else { s.doc.occurrence_of(comp).map(|o| o.id) }
+ if s.doc.model_body_component(b) == 0 {
+ return Some(0);
+ }
+ let model = s.model.state();
+ let (_, _, _, path, _) = s.doc.body_frames_with_source(&model).into_iter().find(|(world, ..)| world == body)?;
+ path.last().copied()
}
/// Centre and unit normal of the circle through a polyline's points, if it lies on one.
diff --git a/crates/ui-egui/src/dialogs_assembly_tests.rs b/crates/ui-egui/src/dialogs_assembly_tests.rs
index e78310bd08..dd6c9102fe 100644
--- a/crates/ui-egui/src/dialogs_assembly_tests.rs
+++ b/crates/ui-egui/src/dialogs_assembly_tests.rs
@@ -215,6 +215,19 @@ fn component_picks_become_occurrences() {
assert_eq!(c[0].0, "component.create");
}
+/// A body of a further instance of a component belongs to that instance, not the first (#3857).
+#[test]
+fn body_of_a_further_instance_is_its_own_occurrence() {
+ let mut app = two_boxes();
+ let copy = app.session.execute("occurrence.copy", &json!({"component": "B", "translate": [0, 40, 0]})).unwrap()["occurrence"].as_u64().unwrap();
+ let first = occ(&app, "B");
+ assert_ne!(copy, first);
+ let frames = app.session.doc.body_frames(&app.session.model.state());
+ let of = |o: u64| frames.iter().find(|f| f.2.last() == Some(&o)).map(|f| f.0.clone()).unwrap();
+ assert_eq!(occurrence_of_body(&app.session, &of(first)), Some(first));
+ assert_eq!(occurrence_of_body(&app.session, &of(copy)), Some(copy));
+}
+
/// Round edges snap to their centre; straight ones are not circles.
#[test]
fn circles_from_edge_polylines() {