Preserve plastic rules and hidden bodies through renames (#4421) (#4425)

* claim: #4421

* Fix body metadata and visibility across renames (#4421)
This commit is contained in:
Colin Faulkingham
2026-10-26 11:15:31 +00:00
committed by GitHub
parent 484f0cf8ae
commit cd66495e14
5 changed files with 237 additions and 18 deletions
+89 -3
View File
@@ -367,7 +367,10 @@ fn visibility(s: &mut Session, p: &Value) -> Result<Value> {
}
let v = &mut s.visibility;
match str_(p, "folder") {
Some("bodies" | "all") if visible => v.hidden_occurrences.clear(),
Some("bodies" | "all") if visible => {
v.hidden_bodies.clear();
v.hidden_occurrences.clear();
}
// Showing a whole folder shows the folder too.
Some("origin") if visible => v.origin_hidden = Some(false),
_ => {}
@@ -760,6 +763,7 @@ fn rename_body(s: &mut Session, p: &Value) -> Result<Value> {
if name == old {
return Ok(json!({"body": old, "name": name}));
}
let previous = s.doc.clone();
let st = s.model.state();
let b = st.body(&old).ok_or_else(|| bad(cmd, format!("no body `{old}`")))?;
if st.body(name).is_some() {
@@ -878,10 +882,10 @@ fn rename_body(s: &mut Session, p: &Value) -> Result<Value> {
return Err(bad(cmd, e));
}
d.rename_body_keys(&old, name);
let v = &mut s.visibility;
for b in v.hidden_bodies.iter_mut().chain(v.locked_bodies.iter_mut()).filter(|b| **b == old) {
for b in s.visibility.locked_bodies.iter_mut().filter(|b| **b == old) {
*b = name.to_string();
}
s.remember_body_rename(&previous, &old, name);
Ok(json!({"body": old, "name": name}))
}
@@ -1011,6 +1015,88 @@ mod tests {
s.model.state().bodies.iter().map(|b| b.name.clone()).collect()
}
#[test]
fn body_rename_preserves_plastic_rules_and_current_visibility_through_history() {
let mut s = Session::default();
run(&mut s, "solid.box", json!({"length": 10, "width": 10, "height": 10, "body_name": "Plate"}));
run(&mut s, "plastic.manage_rules", json!({"name": "Housing", "draft": "3 deg"}));
run(&mut s, "plastic.assign_rule", json!({"bodies": ["Plate"], "rule": "Housing"}));
run(&mut s, "browser.visibility", json!({"items": ["Plate"], "visible": false}));
run(&mut s, "body.rename", json!({"body": "Plate", "name": "Base"}));
assert_eq!(run(&mut s, "plastic.rules", json!({}))["assigned"], json!({"Base": "Housing"}));
assert!(!s.body_visible("Base"));
run(&mut s, "edit.undo", json!({}));
assert_eq!(run(&mut s, "plastic.rules", json!({}))["assigned"], json!({"Plate": "Housing"}));
assert!(!s.body_visible("Plate"), "undo must keep the body hidden");
run(&mut s, "edit.redo", json!({}));
assert!(!s.body_visible("Base"));
// Showing a body is its own undo step; the rename keeps the correct hidden name on
// either side of that step, and neither operation loses the assigned rule.
run(&mut s, "browser.visibility", json!({"items": ["Base"], "visible": true}));
run(&mut s, "edit.undo", json!({}));
assert_eq!(names(&s), ["Base"]);
assert!(!s.body_visible("Base"));
run(&mut s, "edit.undo", json!({}));
assert_eq!(names(&s), ["Plate"]);
assert!(!s.body_visible("Plate"));
run(&mut s, "edit.redo", json!({}));
assert!(!s.body_visible("Base"));
run(&mut s, "edit.redo", json!({}));
assert!(s.body_visible("Base"));
run(&mut s, "browser.visibility", json!({"items": ["Base"], "visible": false}));
let mut saved = (*s.doc).clone();
saved.visibility = s.visibility.clone();
let reopened = Session::new(serde_json::from_str(&serde_json::to_string(&saved).unwrap()).unwrap());
assert_eq!(reopened.doc.plastic.assigned.get("Base").map(String::as_str), Some("Housing"));
assert!(!reopened.body_visible("Base"));
}
#[test]
fn body_rename_undo_keeps_hidden_bodies_with_downstream_users_hidden() {
let mut s = Session::default();
run(&mut s, "solid.box", json!({"length": 10, "width": 10, "height": 10, "body_name": "Peg"}));
run(&mut s, "solid.move", json!({"bodies": ["Peg"], "translate": [0, 0, 5]}));
run(&mut s, "browser.visibility", json!({"items": ["Peg"], "visible": false}));
run(&mut s, "body.rename", json!({"body": "Peg", "name": "Stud"}));
run(&mut s, "edit.undo", json!({}));
assert!(!s.body_visible("Peg"));
assert_eq!(s.visibility.hidden_bodies, ["Peg"]);
run(&mut s, "edit.redo", json!({}));
assert!(!s.body_visible("Stud"));
}
#[test]
fn show_all_bodies_clears_stale_hidden_names() {
for folder in ["bodies", "all"] {
let mut s = Session::default();
s.visibility.hidden_bodies.push("Old body name".into());
run(&mut s, "browser.visibility", json!({"folder": folder, "visible": true}));
assert!(s.visibility.hidden_bodies.is_empty(), "{folder}");
}
}
#[test]
fn computed_body_rename_keeps_the_latest_visibility_and_undo_mapping() {
let mut s = Session::default();
run(&mut s, "solid.box", json!({"length": 10, "width": 10, "height": 10, "body_name": "Peg"}));
run(&mut s, "solid.move", json!({"bodies": ["Peg"], "translate": [0, 0, 5]}));
let mut computed = s.scratch();
run(&mut computed, "body.rename", json!({"body": "Peg", "name": "Stud"}));
// The user hides the body while the rename is being evaluated.
run(&mut s, "browser.visibility", json!({"items": ["Peg"], "visible": false}));
s.accept_computed(computed);
assert!(!s.body_visible("Stud"));
assert_eq!(s.visibility.hidden_bodies, ["Stud"]);
run(&mut s, "edit.undo", json!({}));
assert!(!s.body_visible("Peg"));
run(&mut s, "edit.undo", json!({}));
assert!(s.body_visible("Peg"), "the concurrent hide remains its own undo step");
run(&mut s, "edit.redo", json!({}));
assert!(!s.body_visible("Peg"));
run(&mut s, "edit.redo", json!({}));
assert!(!s.body_visible("Stud"));
}
#[test]
fn renamed_body_keeps_its_users() {
let mut s = Session::default();
+49
View File
@@ -52,6 +52,55 @@ fn component_create_rejects_unknown_keys_and_accepts_documented_keys() {
assert_eq!(s.active_occurrence_path, Some(vec![occurrence]));
}
#[test]
fn body_and_component_renames_keep_hidden_copies_through_history() {
let mut s = Session::default();
let (component, first) = created(&mut s, "Knob");
run(&mut s, "solid.box", json!({"length": 10, "width": 10, "height": 10, "body_name": "Knob"}));
let second = run(&mut s, "occurrence.copy", json!({"component": component, "translate": [20, 0, 0]}))["occurrence"].as_u64().unwrap();
let name = |s: &Session, path: &[u64]| s.occurrence_placement(path).unwrap().bodies[0].world.clone();
let hidden = name(&s, &[second]);
run(&mut s, "browser.visibility", json!({"items": [hidden], "visible": false}));
for (command, params) in [
("body.rename", json!({"body": "Knob", "name": "Grip"})),
("component.rename", json!({"component": component, "name": "Dial"})),
("edit.undo", json!({})),
("edit.undo", json!({})),
("edit.redo", json!({})),
("edit.redo", json!({})),
] {
run(&mut s, command, params);
assert!(s.body_visible(&name(&s, &[first])), "{command}: the first copy stays visible");
assert!(!s.body_visible(&name(&s, &[second])), "{command}: the hidden copy stays hidden");
assert_eq!(s.visibility.hidden_bodies, [name(&s, &[second])], "no stale names after {command}");
}
}
#[test]
fn renaming_a_parent_preserves_hidden_nested_copies_with_ambiguous_labels() {
let mut s = Session::default();
let (parent, first_parent) = created(&mut s, "Assembly");
let (_, child) = created(&mut s, "Knob");
run(&mut s, "solid.box", json!({"length": 10, "width": 10, "height": 10, "body_name": "Knob"}));
let second_parent = run(&mut s, "occurrence.copy", json!({"component": parent, "translate": [20, 0, 0]}))["occurrence"].as_u64().unwrap();
let name = |s: &Session, path: &[u64]| s.occurrence_placement(path).unwrap().bodies[0].world.clone();
let hidden = name(&s, &[second_parent, child]);
run(&mut s, "browser.visibility", json!({"items": [hidden], "visible": false}));
for (command, params) in [
("component.rename", json!({"component": parent, "name": "Assembly/Other"})),
("body.rename", json!({"body": "Knob", "name": "Grip (@occ[custom])"})),
("edit.undo", json!({})),
("edit.undo", json!({})),
("edit.redo", json!({})),
("edit.redo", json!({})),
] {
run(&mut s, command, params);
assert!(s.body_visible(&name(&s, &[first_parent, child])), "{command}");
assert!(!s.body_visible(&name(&s, &[second_parent, child])), "{command}");
assert_eq!(s.visibility.hidden_bodies, [name(&s, &[second_parent, child])]);
}
}
#[test]
fn instance_copy_uses_selected_source_transform() {
let mut s = Session::default();