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.

2.9 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.

//! STEP → BinXCAF (`.xbf`) → build_tree must yield the SAME assembly tree as
//! STEP → build_tree: identical nodeIds, geometry hashes, volumes, and
//! component count. The xbf is the compacted retained-raw form; if it drifted
//! from the STEP, lazy planning off the xbf would produce different components
//! than the original. Hermetic: fixtures generated by the occt bridge.

use converter::convert::{build_tree, convert_xbf, step_to_xbf};
use converter::graph::AssemblyNode;
use serde_json::Value;

fn fixture_paths(boxes: u32) -> (String, String) {
    let dir = std::env::temp_dir().join(format!("carbon-xbf-parity-{boxes}"));
    std::fs::create_dir_all(&dir).unwrap();
    let step = dir.join("fixture.step");
    let xbf = dir.join("fixture.xbf");
    let step_str = step.to_str().unwrap().to_string();
    assert!(
        occt_bridge::write_test_step(&step_str, boxes),
        "failed to write test STEP"
    );
    assert!(
        step_to_xbf(&step_str, xbf.to_str().unwrap()).is_ok(),
        "step_to_xbf"
    );
    (step_str, xbf.to_str().unwrap().to_string())
}

fn flatten<'a>(node: &'a AssemblyNode, out: &mut Vec<&'a AssemblyNode>) {
    out.push(node);
    for c in &node.children {
        flatten(c, out);
    }
}

#[test]
fn xbf_tree_matches_step_tree() {
    let (step, xbf) = fixture_paths(3);

    let from_step = build_tree(&step, 0.1, 0.5).expect("build_tree step");
    let from_xbf = build_tree(&xbf, 0.1, 0.5).expect("build_tree xbf");

    let (mut a, mut b) = (Vec::new(), Vec::new());
    flatten(&from_step, &mut a);
    flatten(&from_xbf, &mut b);

    assert_eq!(a.len(), b.len(), "node count must match");
    for (x, y) in a.iter().zip(b.iter()) {
        assert_eq!(x.node_id, y.node_id, "nodeId parity");
        assert_eq!(x.is_assembly, y.is_assembly, "assembly flag parity");
        assert_eq!(
            x.mesh.as_ref().map(|m| m.geometry_hash.as_str()),
            y.mesh.as_ref().map(|m| m.geometry_hash.as_str()),
            "geometryHash parity"
        );
        assert_eq!(x.volume, y.volume, "volume parity");
    }
}

#[test]
fn convert_xbf_reports_mm_and_same_component_count() {
    let (step, xbf) = fixture_paths(2);
    let step_graph = {
        let text = std::fs::read_to_string(&step).unwrap();
        converter::convert::convert_step(&step, &text, 0.1, 0.5)
            .unwrap()
            .graph
    };
    let conv = convert_xbf(&xbf, 0.1, 0.5).expect("convert_xbf");
    assert_eq!(conv.component_count, 2);
    assert_eq!(conv.graph["componentCount"], step_graph["componentCount"]);
    // Geometry is OCCT-normalized to mm inside the xbf; no STEP header to detect.
    assert_eq!(conv.graph["sourceUnit"], Value::String("mm".into()));
}