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

* claim: #2175

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

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

An Import keeps its body names by the file's body index, and builds no body for the
file's later instances of a repeated part (`skip`). Renaming a body stored the names by
the body's place in the model instead, so every name after the first skipped instance
landed one slot early (and the new name could land on a skipped slot). The rename looked
right until the design was rebuilt; then those bodies took other names, dropped out of
their components and showed up loose in the import's top component.

`Feature::body_name_slots` maps the bodies a feature builds to their stored slots, and
`Feature::pin_body_names` stores current names through it. The rename, the pinning of
later features' names (rename and suppress) and `Document::body_rename_from` use them.

Fixes #2175

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:
yhamil04
2026-10-26 11:10:39 +00:00
committed by GitHub
parent d7252f2240
commit a3483ed451
4 changed files with 105 additions and 10 deletions
+11 -8
View File
@@ -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.
+1 -1
View File
@@ -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 {
@@ -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);
}