SolidCraft: an open-source parametric 3D CAD application built in pure Rust. Desktop, web, and automation through MCP.
component.list: group bodies, features and occurrences once (#6394) (#6395)
JK3Dcbcf1fe71652
diff --git a/crates/engine/src/cmd/component.rs b/crates/engine/src/cmd/component.rsindex 15e6ebd976..e14ec5c497 100644--- a/crates/engine/src/cmd/component.rs+++ b/crates/engine/src/cmd/component.rs@@ -3,8 +3,10 @@//! belong to their feature's component unless moved; occurrences place components (instances//! share contents, "paste new" copies them) and can be grounded and moved.+use std::collections::HashMap;+use serde_json::{Value, json};-use solidcraft_doc::{FeatureKind, Mat, Occurrence};+use solidcraft_doc::{Feature, FeatureKind, Mat, Occurrence};use solidcraft_geom::Vec3;use super::CommandSpec;@@ -204,24 +206,30 @@ fn list(s: &mut Session, p: &Value) -> Result<Value> {}v};+ // Each component's bodies, features, occurrences and children, grouped once rather than+ // searched for every component (thousands of each in a design with many components).+ let mut bodies_of: HashMap<u64, Vec<&str>> = HashMap::new();+ for b in &st.bodies {+ bodies_of.entry(doc.model_body_component(b)).or_default().push(b.name.as_str());+ }+ let mut features_of: HashMap<u64, Vec<&Feature>> = HashMap::new();+ for f in &doc.features {+ features_of.entry(f.component).or_default().push(f);+ }+ let (mut occurrences_of, mut children_of): (HashMap<u64, Vec<&Occurrence>>, HashMap<u64, Vec<&Occurrence>>) = Default::default();+ for o in &doc.occurrences {+ occurrences_of.entry(o.component).or_default().push(o);+ children_of.entry(o.parent).or_default().push(o);+ }let node = |id: u64, name: &str, parent: Option<u64>| {- let bodies: Vec<&str> = st.bodies.iter().filter(|b| doc.model_body_component(b) == id).map(|b| b.name.as_str()).collect();- let features: Vec<&str> = doc- .features- .iter()- .filter(|f| f.component == id && !matches!(f.kind, FeatureKind::Sketch { .. } | FeatureKind::ConstructionPlane { .. }))- .map(|f| f.name.as_str())- .collect();- let sketches: Vec<&str> =- doc.features.iter().filter(|f| f.component == id && matches!(f.kind, FeatureKind::Sketch { .. })).map(|f| f.name.as_str()).collect();- let construction: Vec<&str> = doc- .features- .iter()- .filter(|f| f.component == id && matches!(f.kind, FeatureKind::ConstructionPlane { .. }))- .map(|f| f.name.as_str())- .collect();- let occurrences: Vec<Value> = doc.occurrences.iter().filter(|o| o.component == id).map(placement).collect();- let children: Vec<Value> = doc.occurrences.iter().filter(|o| o.parent == id).map(|o| if compact { json!(o.id) } else { json!(o) }).collect();+ let bodies = bodies_of.get(&id).cloned().unwrap_or_default();+ let features = features_of.get(&id).map(Vec::as_slice).unwrap_or_default();+ let named = |keep: fn(&FeatureKind) -> bool| -> Vec<&str> { features.iter().filter(|f| keep(&f.kind)).map(|f| f.name.as_str()).collect() };+ let sketches = named(|k| matches!(k, FeatureKind::Sketch { .. }));+ let construction = named(|k| matches!(k, FeatureKind::ConstructionPlane { .. }));+ let features = named(|k| !matches!(k, FeatureKind::Sketch { .. } | FeatureKind::ConstructionPlane { .. }));+ let occurrences: Vec<Value> = occurrences_of.get(&id).into_iter().flatten().map(|o| placement(o)).collect();+ let children: Vec<Value> = children_of.get(&id).into_iter().flatten().map(|o| if compact { json!(o.id) } else { json!(o) }).collect();let bodies = if body_names { json!(bodies) } else { json!(bodies.len()) };json!({"id": id, "name": name, "parent": parent, "bodies": bodies, "features": names(features), "sketches": names(sketches),"construction": names(construction), "occurrences": occurrences, "children": children})diff --git a/crates/engine/src/cmd/occurrence_tests.rs b/crates/engine/src/cmd/occurrence_tests.rsindex 6d7c85c107..24da8dd817 100644--- a/crates/engine/src/cmd/occurrence_tests.rs+++ b/crates/engine/src/cmd/occurrence_tests.rs@@ -620,6 +620,37 @@ fn compact_component_list_counts_and_skips_identity_transforms() {assert_eq!(block(&run(&mut s, "component.list", json!({"compact": true, "bodies": true})))["bodies"], json!(["Cube"]));}+/// Each component lists only what it holds, in timeline order: its bodies, features and+/// sketches, its own occurrences and the occurrences inside it, through nested components.+#[test]+fn component_list_gives_each_component_only_its_own_contents() {+ let mut s = Session::default();+ run(&mut s, "solid.box", json!({"length": 5, "width": 5, "height": 5, "name": "Base", "body_name": "Base"}));+ let (arm, arm_first) = created(&mut s, "Arm");+ run(&mut s, "sketch.create", json!({"plane": "XY", "name": "Arm sketch"}));+ run(&mut s, "solid.box", json!({"corner": [0, 20, 0], "length": 2, "width": 2, "height": 2, "name": "Lug", "body_name": "Lug"}));+ let pin = run(&mut s, "component.create", json!({"name": "Pin", "parent": arm, "parent_path": [arm_first]}));+ let (pin, pin_first) = (pin["component"].as_u64().unwrap(), pin["occurrence"].as_u64().unwrap());+ run(&mut s, "solid.box", json!({"corner": [0, 40, 0], "length": 1, "width": 1, "height": 1, "name": "Shank", "body_name": "Shank"}));+ run(&mut s, "solid.box", json!({"corner": [0, 50, 0], "length": 1, "width": 1, "height": 1, "name": "Head", "body_name": "Head"}));+ run(&mut s, "component.activate", json!({"component": "root"}));+ let arm_second = run(&mut s, "occurrence.copy", json!({"component": arm, "translate": [30, 0, 0]}))["occurrence"].as_u64().unwrap();+ let list = run(&mut s, "component.list", json!({}));+ let of = |id: u64| list["components"].as_array().unwrap().iter().find(|c| c["id"] == id).unwrap().clone();+ let ids = |v: &Value| v.as_array().unwrap().iter().map(|o| o["id"].as_u64().unwrap()).collect::<Vec<_>>();+ let (root, arm_node, pin_node) = (of(0), of(arm), of(pin));+ assert_eq!((&root["bodies"], &root["features"], &root["sketches"]), (&json!(["Base"]), &json!(["Base"]), &json!([])), "{root}");+ assert_eq!(ids(&root["children"]), vec![arm_first, arm_second], "{root}");+ assert_eq!(+ (&arm_node["bodies"], &arm_node["features"], &arm_node["sketches"]),+ (&json!(["Lug"]), &json!(["Lug"]), &json!(["Arm sketch"])),+ "{arm_node}"+ );+ assert_eq!((ids(&arm_node["occurrences"]), ids(&arm_node["children"])), (vec![arm_first, arm_second], vec![pin_first]), "{arm_node}");+ assert_eq!((&pin_node["bodies"], &pin_node["features"]), (&json!(["Shank", "Head"]), &json!(["Shank", "Head"])), "{pin_node}");+ assert_eq!((ids(&pin_node["occurrences"]), ids(&pin_node["children"])), (vec![pin_first], vec![]), "{pin_node}");+}+/// A script's 3x4 placement matrix (rotation rows, then the translation column) moves an/// occurrence exactly as the same turn and move given as an axis, an angle and a translation.#[test]