Skip to content

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

Components: a component named like the design is reachable by name (#8358) (#8423)

Colin Faulkingham5221103369b4
diff --git a/crates/doc/src/document.rs b/crates/doc/src/document.rs
index 58030c4c21..bd30616659 100644
--- a/crates/doc/src/document.rs
+++ b/crates/doc/src/document.rs
@@ -3351,12 +3351,13 @@ impl Document {
Ok(id)
}
- /// A component by id or name (0 / "root" is the design itself).
+ /// A component by id or name (0 / "root" is the design itself). A component that carries the
+ /// design's own name is found by that name: the design is still named by 0 / "root".
pub fn find_component(&self, key: &str) -> Option<u64> {
- if key == "0" || key.eq_ignore_ascii_case("root") || key == self.name {
+ if key == "0" || key.eq_ignore_ascii_case("root") {
return Some(0);
}
- self.components.iter().find(|c| c.id.to_string() == key || c.name == key).map(|c| c.id)
+ self.components.iter().find(|c| c.id.to_string() == key || c.name == key).map(|c| c.id).or_else(|| (key == self.name).then_some(0))
}
/// Read a design file's JSON (any format this build knows; older ones are upgraded).
diff --git a/crates/engine/src/cmd/pbr_appearance_tests.rs b/crates/engine/src/cmd/pbr_appearance_tests.rs
index 1673876b2c..81bb8005f8 100644
--- a/crates/engine/src/cmd/pbr_appearance_tests.rs
+++ b/crates/engine/src/cmd/pbr_appearance_tests.rs
@@ -183,6 +183,21 @@ fn independent_component_copy_keeps_its_opaque_response_after_source_edits_and_r
assert_eq!(reopened.appearances.components[&copied], original);
}
+/// A component that carries the design's own name is still reachable by that name (#8358):
+/// `appearance.assign` resolves the name to the component, not to the design's root row.
+#[test]
+fn a_component_named_like_the_design_takes_its_appearance_by_name() {
+ let mut s = Session::default();
+ s.doc_mut().name = "CurtainRailPlug".into();
+ let component = s.execute("component.create", &json!({"name":"CurtainRailPlug","activate":false})).unwrap()["component"].as_u64().unwrap();
+ let assigned =
+ s.execute("appearance.assign", &json!({"components":["CurtainRailPlug"],"color":"#349650","name":"ABS - Green","opacity":1.0})).unwrap();
+ assert_eq!(assigned["components"], json!([component]), "the name resolves to the component, not the design");
+ let look = &s.doc.appearances.components[&component];
+ assert_eq!(look.name, "ABS - Green");
+ assert_eq!(look.color, [0x34, 0x96, 0x50]);
+}
+
#[test]
fn named_opaque_finishes_reach_the_scene_and_survive_undo_and_reopen_without_changing_mass() {
let mut s = fixture();