Skip to content

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

Body rename: an imported assembly's other bodies keep their names and components (#2175) (#6615)

yhamil04a3483ed451d9
diff --git a/crates/doc/src/document.rs b/crates/doc/src/document.rs
index 4d17108021..4f92f66daf 100644
--- a/crates/doc/src/document.rs
+++ b/crates/doc/src/document.rs
@@ -2464,6 +2464,33 @@ pub struct Feature {
pub kind: FeatureKind,
}
+impl Feature {
+ /// Where in `body_names` the names of the first `count` bodies the feature builds are kept,
+ /// in the order it builds them. The same index for most features. An Import keeps its names
+ /// by the file's body index, and builds no body for the file bodies it skips, so its built
+ /// bodies pass their slots by (#2175).
+ pub fn body_name_slots(&self, count: usize) -> Vec<usize> {
+ let skip: std::collections::HashSet<u64> = match &self.kind {
+ FeatureKind::Import { skip, .. } => skip.iter().copied().collect(),
+ _ => Default::default(),
+ };
+ (0..count.saturating_add(skip.len())).filter(|k| !skip.contains(&(*k as u64))).take(count).collect()
+ }
+
+ /// Keep `made`, the names the feature's bodies have now (in the order it builds them), as
+ /// its stored names, so they stay as they are whatever comes and goes before it. An Import's
+ /// skipped file bodies keep the names stored for them.
+ pub fn pin_body_names(&mut self, made: &[String]) {
+ let slots = self.body_name_slots(made.len());
+ self.body_names.resize(slots.last().map_or(0, |s| s + 1), String::new());
+ for (slot, name) in slots.iter().zip(made) {
+ if let Some(stored) = self.body_names.get_mut(*slot) {
+ stored.clone_from(name);
+ }
+ }
+ }
+}
+
fn inside_str() -> String {
"inside".into()
}
@@ -2907,8 +2934,10 @@ impl Document {
continue;
}
let now = made(a.id);
+ // The stored slot of each body built now (an Import's pass its skipped bodies by).
+ let at: std::collections::HashMap<usize, &String> = b.body_name_slots(now.len()).into_iter().zip(&now).collect();
for (k, name) in b.body_names.iter().enumerate() {
- let current = now.get(k);
+ let current = at.get(&k).copied();
// Slots the rename left alone, and the ones it set to the names they have now.
if a.body_names.get(k) == Some(name) || name.trim().is_empty() || current == Some(name) {
continue;
diff --git a/crates/engine/src/cmd/browser.rs b/crates/engine/src/cmd/browser.rs
index d072715b58..42a47c168f 100644
--- a/crates/engine/src/cmd/browser.rs
+++ b/crates/engine/src/cmd/browser.rs
@@ -819,7 +819,7 @@ fn rename_body(s: &mut Session, p: &Value) -> Result<Value> {
*names = pins;
f.body_names.clear();
} else {
- f.body_names.clone_from(made);
+ f.pin_body_names(made);
}
}
}
@@ -828,15 +828,18 @@ fn rename_body(s: &mut Session, p: &Value) -> Result<Value> {
*names = pins;
f.body_names.clear();
} else {
- while f.body_names.len() < siblings.len().max(k + 1) {
- let i = f.body_names.len();
- f.body_names.push(siblings.get(i).cloned().unwrap_or_default());
+ // An Import keeps its names by the file's body index, its skipped bodies' too (#2175).
+ let slots = f.body_name_slots(siblings.len().max(k + 1));
+ if let Some(end) = slots.last().map(|s| s + 1).filter(|end| f.body_names.len() < *end) {
+ f.body_names.resize(end, String::new());
}
- for (slot, current) in f.body_names.iter_mut().zip(&siblings) {
- slot.clone_from(current);
+ for (slot, current) in slots.iter().zip(&siblings) {
+ if let Some(stored) = f.body_names.get_mut(*slot) {
+ stored.clone_from(current);
+ }
}
- if let Some(slot) = f.body_names.get_mut(k) {
- *slot = name.to_string();
+ if let Some(stored) = slots.get(k).and_then(|slot| f.body_names.get_mut(*slot)) {
+ *stored = name.to_string();
}
}
// Later features refer to bodies by name.
diff --git a/crates/engine/src/cmd/edit.rs b/crates/engine/src/cmd/edit.rs
index 481901874c..9eb99fc295 100644
--- a/crates/engine/src/cmd/edit.rs
+++ b/crates/engine/src/cmd/edit.rs
@@ -632,7 +632,7 @@ fn suppress(s: &mut Session, p: &Value) -> Result<Value> {
let doc = s.doc_mut();
for (id, made) in &keep {
if let Some(f) = doc.feature_mut(*id) {
- f.body_names.clone_from(made);
+ f.pin_body_names(made);
}
}
for id in &ids {
diff --git a/crates/engine/src/tests/import_component.rs b/crates/engine/src/tests/import_component.rs
index 5a02b15748..826cb92fe2 100644
--- a/crates/engine/src/tests/import_component.rs
+++ b/crates/engine/src/tests/import_component.rs
@@ -415,3 +415,66 @@ fn a_joint_on_an_imported_part_takes_its_own_hole() {
let pin_at = solidcraft_doc::apply_point(&solidcraft_doc::joints::occurrence_world(&s.doc, pin), Vec3::new(0.0, -40.0, 0.0));
assert!(Vec3::new(pin_at.x, pin_at.y, 0.0).dist(Vec3::new(70.0, 10.0, 0.0)) < 1e-6, "the pin's base is at {pin_at:?}");
}
+
+/// Each component's bodies, by component name.
+fn bodies_by_component(s: &mut Session) -> Vec<(String, Vec<String>)> {
+ components(s)
+ .iter()
+ .map(|c| (c["name"].as_str().unwrap().to_string(), c["bodies"].as_array().unwrap().iter().map(|b| b.as_str().unwrap().to_string()).collect()))
+ .collect()
+}
+
+/// Renaming one body of an imported assembly renames that body and nothing else (#2175). An
+/// Import keeps its bodies' names by their place in the file, and builds no body for the file's
+/// later instances of a repeated part: the rename stored the names by their place in the model,
+/// so once the design was rebuilt the bodies after those instances took the wrong names and left
+/// their components.
+#[test]
+fn renaming_an_imported_body_after_a_repeated_part_keeps_every_body_in_its_component() {
+ let dir = temp_dir("rename-after-repeat");
+ // Bracket and Pin placed twice, then Plate and Knob: the file's bodies are Bracket, Pin,
+ // Bracket, Pin, Plate, Knob, and the Import builds Bracket, Pin, Plate, Knob.
+ let mut a = Session::default();
+ run(&mut a, "solid.box", json!({"length": 20, "width": 10, "height": 10, "body_name": "Bracket"}));
+ run(&mut a, "solid.box", json!({"length": 4, "width": 4, "height": 16, "corner": [8, 3, 10], "operation": "new_body", "body_name": "Pin"}));
+ run(&mut a, "component.from_bodies", json!({"bodies": ["Bracket", "Pin"]}));
+ for part in ["Bracket", "Pin"] {
+ run(&mut a, "occurrence.copy", json!({"component": part, "translate": [0, 25, 0]}));
+ }
+ run(
+ &mut a,
+ "solid.box",
+ json!({"length": 60, "width": 50, "height": 3, "corner": [-20, -10, -3], "operation": "new_body", "body_name": "Plate"}),
+ );
+ run(&mut a, "solid.box", json!({"length": 6, "width": 6, "height": 6, "corner": [30, 30, 0], "operation": "new_body", "body_name": "Knob"}));
+ run(&mut a, "component.from_bodies", json!({"bodies": ["Plate", "Knob"]}));
+ let path = dir.join("fixture.step").to_string_lossy().to_string();
+ run(&mut a, "file.export", json!({"path": path}));
+
+ let mut s = Session::default();
+ run(&mut s, "file.insert_step", json!({"path": path}));
+ let f = &s.doc.features[0];
+ assert!(matches!(&f.kind, solidcraft_doc::FeatureKind::Import { skip, .. } if *skip == [2, 3]), "{:?}", f.body_names);
+ let names = |s: &Session| s.model.state().bodies.iter().map(|b| b.name.clone()).collect::<Vec<_>>();
+ assert_eq!(names(&s), ["Bracket", "Pin", "Plate", "Knob"]);
+ let mut expected = bodies_by_component(&mut s);
+ let boxes = shown_boxes(&s);
+
+ let mut rename = |s: &mut Session, old: &str, new: &str| {
+ run(s, "body.rename", json!({"body": old, "name": new}));
+ for (_, bodies) in &mut expected {
+ for b in bodies.iter_mut().filter(|b| *b == old) {
+ *b = new.to_string();
+ }
+ }
+ // Rebuilt from the stored names, the design is the same.
+ run(s, "timeline.compute_all", json!({}));
+ assert_eq!(bodies_by_component(s), expected);
+ assert_eq!(shown_boxes(s), boxes, "every body is where it was");
+ };
+ rename(&mut s, "Plate", "Base plate");
+ rename(&mut s, "Pin", "Peg");
+ rename(&mut s, "Knob", "Handle");
+ assert_eq!(names(&s), ["Bracket", "Peg", "Base plate", "Handle"]);
+ let _ = std::fs::remove_dir_all(&dir);
+}