Skip to content

Open-source manufacturing ERP, MES and QMS. Quoting, MRP, inventory, shop floor, quality and lot/serial traceability on one Postgres schema, with a REST API and MCP server. Self-host or use Carbon Cloud.

3.3 KB
Raw Blame
// SPDX-License-Identifier: AGPL-3.0-only
// Carbon (github.com/crbnos/carbon). Modified or adapted versions of this file,
// including ports, remain AGPLv3; serving them over a network requires releasing their source.

//! Flat multi-body STEP (one PRODUCT, N solids, no assembly tree — the common
//! Fusion/SolidWorks export shape) must split into per-solid components.
//! Hermetic: the STEP fixtures are generated by the occt bridge itself.

use converter::convert::convert_step;
use serde_json::Value;

fn convert(boxes: u32) -> Value {
    let dir = std::env::temp_dir().join(format!("carbon-multibody-{boxes}"));
    std::fs::create_dir_all(&dir).unwrap();
    let path = dir.join("fixture.step");
    let path_str = path.to_str().unwrap();
    assert!(
        occt_bridge::write_test_step(path_str, boxes),
        "failed to write test STEP"
    );
    let text = std::fs::read_to_string(path_str).unwrap();
    let conv = convert_step(path_str, &text, 0.1, 0.5).expect("convert");
    conv.graph
}

fn leaves(node: &Value, out: &mut Vec<Value>) {
    let children = node["children"].as_array().cloned().unwrap_or_default();
    if children.is_empty() && node["isAssembly"] == Value::Bool(false) {
        out.push(node.clone());
        return;
    }
    for child in &children {
        leaves(child, out);
    }
}

#[test]
fn splits_a_flat_two_solid_product_into_two_components() {
    let graph = convert(2);
    assert_eq!(graph["componentCount"], 2, "expected a per-solid split");

    let mut parts = Vec::new();
    leaves(&graph["root"], &mut parts);
    assert_eq!(parts.len(), 2);

    // Distinct identity per solid
    let ids: Vec<&str> = parts
        .iter()
        .map(|p| p["nodeId"].as_str().unwrap())
        .collect();
    assert_ne!(ids[0], ids[1], "solids must get distinct nodeIds");
    let hashes: Vec<&str> = parts
        .iter()
        .map(|p| p["geometryHash"].as_str().unwrap())
        .collect();
    // Identical boxes at different local positions: positions are baked into
    // the part-local mesh, so the hashes differ too
    assert_ne!(hashes[0], hashes[1]);

    for part in &parts {
        assert!(
            part["volume"].as_f64().unwrap() > 0.0,
            "per-solid volume expected"
        );
        assert_eq!(part["isAssembly"], Value::Bool(false));
    }

    // The synthetic parent is an assembly (no mesh identity, null volume)
    let mut parent = &graph["root"];
    // Root may BE the parent, or wrap it — find the node whose children are the parts
    if parent["children"].as_array().map(|c| c.len()) != Some(2) {
        parent = &parent["children"][0];
    }
    assert_eq!(parent["isAssembly"], Value::Bool(true));
    assert_eq!(parent["geometryHash"], Value::Null);
    assert_eq!(parent["volume"], Value::Null);

    // Display + id-path naming: deterministic per solid
    let names: Vec<&str> = parts.iter().map(|p| p["name"].as_str().unwrap()).collect();
    assert_ne!(names[0], names[1], "solids must get distinct display names");
}

#[test]
fn keeps_a_single_solid_product_as_one_leaf() {
    let graph = convert(1);
    assert_eq!(graph["componentCount"], 1, "single solid must not split");
    let mut parts = Vec::new();
    leaves(&graph["root"], &mut parts);
    assert_eq!(parts.len(), 1);
    assert!(parts[0]["volume"].as_f64().unwrap() > 0.0);
}