Skip to content

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

BOM: group purchased children under inseparable parts (#9744) (#9794)

Colin Faulkingham8311ae5c5dad
diff --git a/crates/engine/src/cmd/bom.rs b/crates/engine/src/cmd/bom.rs
index e1e5ff7347..eb3c745926 100644
--- a/crates/engine/src/cmd/bom.rs
+++ b/crates/engine/src/cmd/bom.rs
@@ -41,8 +41,8 @@ pub static COMMANDS: &[CommandSpec] = &[
revision, each, count, definition}], not as rows of their own); path?: write a .csv or .json file (children as items 1.1, \
1.2…); format?: csv|json (default from the path). Each component's bom_type counts: phantom has no row (what it holds \
counts here), reference counts nowhere, purchased and inseparable are one row (in flat, an inseparable one's purchased \
- parts are listed too) → rows: [{item, part_number, description, revision, bom_type, count, definition, components, \
- properties, children?}] sorted by part number, total_occurrences (children included)",
+ parts are listed too, under it with group_children and a part number) → rows: [{item, part_number, description, revision, \
+ bom_type, count, definition, components, properties, children?}] sorted by part number, total_occurrences (children included)",
),
];
@@ -280,11 +280,12 @@ fn group_key(g: &str, cmd: &str) -> Result<Group> {
/// `Top`, the direct children; in `Flat`, the parts at any depth (a part in a subassembly placed
/// twice counts twice), not below a purchased part, nor below a numbered part when `fold`, and of
/// what an inseparable part holds only its purchased parts. A phantom's contents count where the
-/// phantom is placed; a reference counts nowhere, and nothing inside it does.
-fn counts(ix: &Index, root: u64, levels: Levels, fold: bool, cmd: &str) -> Result<BTreeMap<u64, u64>> {
+/// phantom is placed; a reference counts nowhere, and nothing inside it does. `walk` chooses
+/// whether the root's contents are listed normally or as an inseparable part's purchased children.
+fn counts(ix: &Index, root: u64, levels: Levels, fold: bool, walk: Walk, cmd: &str) -> Result<BTreeMap<u64, u64>> {
let mut out: BTreeMap<u64, u64> = BTreeMap::new();
let mut placed = 0usize;
- let mut stack: Vec<(u64, usize, Walk)> = vec![(root, 0, Walk::Listed)];
+ let mut stack: Vec<(u64, usize, Walk)> = vec![(root, 0, walk)];
while let Some((comp, depth, walk)) = stack.pop() {
for child in ix.children.get(&comp).into_iter().flatten() {
placed += 1;
@@ -303,7 +304,7 @@ fn counts(ix: &Index, root: u64, levels: Levels, fold: bool, cmd: &str) -> Resul
}
(BomType::Inseparable, Walk::Listed) => {
*out.entry(*child).or_default() += 1;
- if levels == Levels::Flat && inside {
+ if levels == Levels::Flat && inside && !(fold && ix.numbered.contains(child)) {
stack.push((*child, depth + 1, Walk::Purchased));
}
}
@@ -415,13 +416,18 @@ fn bom(s: &mut Session, p: &Value) -> Result<Value> {
let root = if p.get("component").is_some_and(|v| !v.is_null()) { component_id(s, p, cmd)? } else { 0 };
let o = options(p, cmd)?;
let ix = Index::new(s);
- let counts = counts(&ix, root, o.levels, o.fold, cmd)?;
- // Folding: what one of each numbered part holds, as parts at any depth. A purchased or
- // inseparable part already keeps what it holds out of the list.
+ let counts = counts(&ix, root, o.levels, o.fold, Walk::Listed, cmd)?;
+ // Folding: what one of each numbered part holds, as parts at any depth. Inseparable parts
+ // list only purchased descendants; purchased parts keep all their contents out of the list.
let mut kits: BTreeMap<u64, BTreeMap<u64, u64>> = BTreeMap::new();
if o.fold {
- for id in counts.keys().filter(|id| ix.numbered.contains(id) && ix.bom_type(**id) == BomType::Normal) {
- let kit = self::counts(&ix, *id, Levels::Flat, false, cmd)?;
+ for id in counts.keys().filter(|id| ix.numbered.contains(id)) {
+ let walk = match ix.bom_type(*id) {
+ BomType::Normal => Walk::Listed,
+ BomType::Inseparable => Walk::Purchased,
+ _ => continue,
+ };
+ let kit = self::counts(&ix, *id, Levels::Flat, false, walk, cmd)?;
if !kit.is_empty() {
kits.insert(*id, kit);
}
diff --git a/crates/engine/src/cmd/bom_tests.rs b/crates/engine/src/cmd/bom_tests.rs
index 8dc9f2b150..6b0a5b2e22 100644
--- a/crates/engine/src/cmd/bom_tests.rs
+++ b/crates/engine/src/cmd/bom_tests.rs
@@ -481,6 +481,91 @@ fn bom_types_decide_what_is_a_row_and_what_counts_inside_it() {
assert_eq!(run(&mut s, "component.metadata", json!({"component": "Gearbox"}))["bom_type"], "normal");
}
+#[test]
+fn group_children_keeps_an_inseparable_parts_purchased_children_in_its_row() {
+ let mut s = Session::default();
+ for (name, parent, part_number, bom_type) in
+ [("Sub", "root", "SUB-1", "inseparable"), ("Screw", "Sub", "M2x10", "purchased"), ("Pin", "Sub", "PIN-1", "normal")]
+ {
+ run(&mut s, "component.create", json!({"name": name, "parent": parent, "activate": false}));
+ run(&mut s, "component.metadata.set", json!({"component": name, "part_number": part_number, "bom_type": bom_type}));
+ }
+ run(&mut s, "occurrence.copy", json!({"component": "Screw", "parent": "Sub"}));
+ run(&mut s, "occurrence.copy", json!({"component": "Sub"}));
+ let plain = run(&mut s, "assembly.bom", json!({}));
+ assert_eq!(typed(&plain), [row("M2x10", "purchased", 4), row("SUB-1", "inseparable", 2)]);
+ for levels in ["flat", "top"] {
+ for group in ["part_number", "definition"] {
+ let r = run(&mut s, "assembly.bom", json!({"levels": levels, "group_by": [group], "group_children": true}));
+ assert_eq!(typed(&r), [row("SUB-1", "inseparable", 2)], "{r}");
+ assert_eq!(r["total_occurrences"], plain["total_occurrences"]);
+ let children = r["rows"][0]["children"].as_array().unwrap();
+ assert_eq!(children.len(), 1, "the made Pin stays inside the inseparable part: {r}");
+ assert_eq!(children[0]["item"], "1.1");
+ assert_eq!(children[0]["part_number"], "M2x10");
+ assert_eq!(children[0]["bom_type"], "purchased");
+ assert_eq!(children[0]["each"], 2);
+ assert_eq!(children[0]["count"], 4);
+ }
+ }
+ let path = scratch("inseparable-kit").join("bom.csv");
+ run(&mut s, "assembly.bom", json!({"group_children": true, "path": path}));
+ let text = std::fs::read_to_string(path).unwrap();
+ let rows: Vec<&str> = text.lines().skip(1).collect();
+ assert_eq!(rows, ["\"1\",\"SUB-1\",\"\",\"\",\"2\",\"Sub\"", "\"1.1\",\"M2x10\",\"\",\"\",\"4\",\"Screw\""]);
+
+ // In its own context the component still lists all its parts, regardless of its type.
+ let own = run(&mut s, "assembly.bom", json!({"component": "Sub", "group_children": true}));
+ assert_eq!(typed(&own), [row("M2x10", "purchased", 2), row("PIN-1", "normal", 1)]);
+ // Only numbered parts fold; turning the option off or removing the number keeps the flat list.
+ run(&mut s, "component.metadata.set", json!({"component": "Sub", "part_number": ""}));
+ assert_eq!(run(&mut s, "assembly.bom", json!({"group_children": true}))["rows"], run(&mut s, "assembly.bom", json!({}))["rows"]);
+}
+
+#[test]
+fn an_inseparable_kit_finds_nested_purchased_parts_without_opening_purchased_or_reference_parts() {
+ let mut s = Session::default();
+ for (name, parent, bom_type) in [
+ ("Sub", "root", "inseparable"),
+ ("Carrier", "Sub", "normal"),
+ ("Nested", "Carrier", "inseparable"),
+ ("Bolt", "Nested", "purchased"),
+ ("Phantom", "Sub", "phantom"),
+ ("Reference", "Sub", "reference"),
+ ("Bought", "Sub", "purchased"),
+ ] {
+ run(&mut s, "component.create", json!({"name": name, "parent": parent, "activate": false}));
+ run(&mut s, "component.metadata.set", json!({"component": name, "part_number": name, "bom_type": bom_type}));
+ }
+ for parent in ["Phantom", "Reference", "Bought"] {
+ run(&mut s, "occurrence.copy", json!({"component": "Bolt", "parent": parent}));
+ }
+ let plain = run(&mut s, "assembly.bom", json!({}));
+ assert_eq!(typed(&plain), [row("Bolt", "purchased", 2), row("Bought", "purchased", 1), row("Sub", "inseparable", 1)]);
+ let folded = run(&mut s, "assembly.bom", json!({"group_children": true}));
+ assert_eq!(typed(&folded), [row("Sub", "inseparable", 1)]);
+ assert_eq!(folded["total_occurrences"], plain["total_occurrences"]);
+ let children = &folded["rows"][0]["children"];
+ assert_eq!(children.as_array().unwrap().len(), 2, "{folded}");
+ assert_eq!(children[0]["part_number"], "Bolt");
+ assert_eq!(children[0]["each"], 2);
+ assert_eq!(children[1]["part_number"], "Bought");
+ assert_eq!(children[1]["each"], 1);
+
+ // Two definitions with the same number but different purchased contents stay separate kits.
+ run(&mut s, "component.create", json!({"name": "Other", "activate": false}));
+ run(&mut s, "component.metadata.set", json!({"component": "Other", "part_number": "Sub", "bom_type": "inseparable"}));
+ run(&mut s, "occurrence.copy", json!({"component": "Bolt", "parent": "Other"}));
+ let different = run(&mut s, "assembly.bom", json!({"group_children": true}));
+ assert_eq!(typed(&different), [row("Sub", "inseparable", 1), row("Sub", "inseparable", 1)]);
+ assert_eq!(different["total_occurrences"], 6);
+ // Buying the whole parent makes one purchased row, with no children listed.
+ set_type(&mut s, "Sub", "purchased");
+ let bought = run(&mut s, "assembly.bom", json!({"group_children": true}));
+ let sub = bought["rows"].as_array().unwrap().iter().find(|r| r["definition"] == "Sub").unwrap();
+ assert!(sub.get("children").is_none(), "{bought}");
+}
+
#[test]
fn a_phantom_counts_what_it_holds_in_its_parent_merged_with_the_same_parts() {
// A fastener set (no bodies) of two screws, placed three times, and one more screw placed directly.
diff --git a/docs/book/src/guide/assemblies.md b/docs/book/src/guide/assemblies.md
index 06b80eca79..04027a98eb 100644
--- a/docs/book/src/guide/assemblies.md
+++ b/docs/book/src/guide/assemblies.md
@@ -465,6 +465,8 @@ description; **Change Part Size** renumbers them, keeping a field you changed yo
under it as `children` (items 1.1, 1.2…, with `each` per bolt and `count` in all) instead of
rows of their own. Bolts with different contents (one with a nut, one without) are different
rows. `total_occurrences` counts the children too, so it is the same either way.
+ For an inseparable part, only its purchased descendants become children; a purchased part
+ still has no children listed. Parts without a part number are not grouped this way.
- `component` lists one subassembly's parts instead of the design's.
A component's **bill of materials type** (`bom_type`, a part property set with