Assembly: default names skip names in use (part of #9738) (#9898)

* Claim part of #9738

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

* Assembly: default names skip names in use (part of #9738)

Contact sets, motion studies and exploded views were named by how many
there were, and a component's placements by how many it had, so after a
delete the next default name repeated one still in use, and commands
given that name acted on the first. They now take the first number no
item has, as joints' and the app's dialogs' default names already do; a
placement keeps the next number when it is free.

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

---------

Co-authored-by: Claude <noreply@anthropic.com>
This commit is contained in:
austin62jake
2026-10-31 08:28:50 +00:00
committed by GitHub
parent 8986a670e5
commit b5fe8776b5
3 changed files with 61 additions and 4 deletions
+8 -1
View File
@@ -456,7 +456,14 @@ impl Document {
let id = self.next_id.max(1);
self.next_id = id.checked_add(1).ok_or_else(|| DocError::Invalid("too many items in the design".into()))?;
let cname = self.components.iter().find(|c| c.id == component).map(|c| c.name.clone()).unwrap_or_default();
let k = self.occurrences.iter().filter(|o| o.component == component).count() + 1;
// The next number, or, when an occurrence already has that name (one before it was
// deleted, or of another component of that name), the first number none has (#9738).
let next = self.occurrences.iter().filter(|o| o.component == component).count() + 1;
let free = |k: &usize| {
let name = format!("{cname}:{k}");
!self.occurrences.iter().any(|o| o.name == name)
};
let k = std::iter::once(next).chain(1..).find(free).unwrap_or(next);
self.occurrences.push(crate::Occurrence { id, component, parent, name: format!("{cname}:{k}"), transform, grounded: false });
Ok(id)
}
+13 -3
View File
@@ -117,6 +117,14 @@ pub static COMMANDS: &[CommandSpec] = &[
const MAX_ITEMS: usize = 10_000;
/// `prefix` and the first number from 1 that no name in `taken` has: a default name never repeats
/// one in use, after a delete either, as joints' and the app's dialogs' default names (#9738).
fn free_name<'a>(prefix: &str, taken: impl Iterator<Item = &'a str>) -> String {
let taken: BTreeSet<&str> = taken.collect();
let n = (1..=taken.len() + 1).find(|n| !taken.contains(format!("{prefix}{n}").as_str())).unwrap_or(1);
format!("{prefix}{n}")
}
fn occurrences(s: &Session, p: &Value, cmd: &str) -> Result<Vec<u64>> {
let list = p.get("occurrences").and_then(Value::as_array).ok_or_else(|| bad(cmd, "`occurrences` must list occurrences"))?;
if list.len() > MAX_ITEMS {
@@ -496,7 +504,8 @@ fn new_set(s: &mut Session, p: &Value) -> Result<Value> {
return Err(bad(cmd, "too many contact sets"));
}
let id = s.doc_mut().allocate_id()?;
let name = str_(p, "name").map(str::to_string).unwrap_or_else(|| format!("Contact Set{}", s.doc.assembly.contact_sets.len() + 1));
let name =
str_(p, "name").map(str::to_string).unwrap_or_else(|| free_name("Contact Set", s.doc.assembly.contact_sets.iter().map(|c| c.name.as_str())));
let a = &mut s.doc_mut().assembly;
a.contact_sets.push(ContactSet { id, name: name.clone(), occurrences: occ, suppressed: false });
a.contact_enabled = true;
@@ -629,7 +638,7 @@ fn study(s: &mut Session, p: &Value) -> Result<Value> {
}
let m = MotionStudy {
id,
name: str_(p, "name").map(str::to_string).unwrap_or_else(|| format!("Motion Study{}", count + 1)),
name: str_(p, "name").map(str::to_string).unwrap_or_else(|| free_name("Motion Study", a.studies.iter().map(|m| m.name.as_str()))),
steps: steps.unwrap_or(100),
keys,
};
@@ -1087,7 +1096,8 @@ fn explode_create(s: &mut Session, p: &Value) -> Result<Value> {
return Err(bad(cmd, "too many exploded views"));
}
let id = s.doc_mut().allocate_id()?;
let name = str_(p, "name").map(str::to_string).unwrap_or_else(|| format!("Exploded View{}", s.doc.assembly.exploded.len() + 1));
let name =
str_(p, "name").map(str::to_string).unwrap_or_else(|| free_name("Exploded View", s.doc.assembly.exploded.iter().map(|v| v.name.as_str())));
s.doc_mut().assembly.exploded.push(ExplodedView { id, name: name.clone(), moves: moves.clone() });
Ok(json!({"view": id, "name": name, "moves": moves.iter().map(|(o, t)| json!({"occurrence": o, "translate": t})).collect::<Vec<_>>()}))
}
+40
View File
@@ -38,6 +38,46 @@ fn deleted_motion_item_ids_are_not_reused_after_reopening() {
}
}
/// #9738: a default name skips the names in use, after a delete too: contact sets, motion
/// studies, exploded views, and the placements of a component.
#[test]
fn default_names_skip_names_in_use() {
for (create, delete, key, params, prefix) in [
("contact.create", "contact.edit", "set", json!({"occurrences": ["A", "B"]}), "Contact Set"),
("motion.study", "motion.delete", "study", json!({"steps": 10}), "Motion Study"),
("explode.create", "explode.delete", "view", json!({"moves": [{"occurrence": "B", "translate": [20, 0, 0]}]}), "Exploded View"),
] {
let mut s = Session::default();
for name in ["A", "B"] {
run(&mut s, "component.activate", json!({"component": "root"}));
run(&mut s, "component.create", json!({"name": name}));
}
let made = |s: &mut Session| {
let r = run(s, create, params.clone());
(r[key].as_u64().unwrap(), r["name"].as_str().unwrap().to_string())
};
let (first, one) = made(&mut s);
let (_, two) = made(&mut s);
assert_eq!([one, two], [format!("{prefix}1"), format!("{prefix}2")]);
run(&mut s, delete, json!({key: first, "delete": true}));
assert_eq!(made(&mut s).1, format!("{prefix}1"), "{create}: the free name, not {prefix}2 again");
}
// Placements: with the first deleted, a copy takes a name none has.
let mut s = Session::default();
run(&mut s, "component.create", json!({"name": "Arm"}));
run(&mut s, "solid.box", json!({"length": 10, "width": 10, "height": 10}));
run(&mut s, "component.activate", json!({"component": "root"}));
let arm = occ(&s, "Arm");
for _ in 0..2 {
run(&mut s, "occurrence.copy", json!({"occurrence": arm, "translate": [20, 0, 0]}));
}
let names = |s: &Session| s.doc.occurrences.iter().map(|o| o.name.clone()).collect::<Vec<_>>();
assert_eq!(names(&s), ["Arm:1", "Arm:2", "Arm:3"]);
run(&mut s, "selection.delete", json!({"occurrences": ["Arm:1"]}));
run(&mut s, "occurrence.copy", json!({"occurrence": "Arm:3", "translate": [20, 0, 0]}));
assert_eq!(names(&s), ["Arm:2", "Arm:3", "Arm:1"]);
}
/// Where B's corner (20, 0, 0) is now.
fn b_x(s: &Session) -> f64 {
super::world_point(s, occ(s, "B"), Vec3::new(20.0, 0.0, 0.0)).x