use std::borrow::Cow;
use std::collections::HashMap;
use super::Graph;
use crate::p21::{Exchange, Instance, Param, Record};
use crate::policy::MAX_ASSEMBLY_DEPTH;
use crate::record::{self, primary_record, reference, upper};
const NAUO: &str = "NEXT_ASSEMBLY_USAGE_OCCURRENCE";
const QACU: &str = "QUANTIFIED_ASSEMBLY_COMPONENT_USAGE";
#[derive(Debug, Clone, PartialEq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize))]
pub struct ProductStructure {
definitions: Vec<Definition>,
usages: Vec<Usage>,
roots: Vec<usize>,
#[cfg_attr(feature = "serde", serde(skip))]
children: Vec<Vec<usize>>,
#[cfg_attr(feature = "serde", serde(skip))]
shape_index: super::shape::ShapeIndex,
}
#[derive(Debug, Clone, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize))]
#[non_exhaustive]
pub struct Definition {
pub instance: u64,
pub entity: String,
pub id: Option<String>,
pub description: Option<String>,
pub version: Option<String>,
pub product: Option<Product>,
pub shape_representations: Vec<u64>,
}
#[derive(Debug, Clone, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize))]
#[non_exhaustive]
pub struct Product {
pub instance: u64,
pub id: Option<String>,
pub name: Option<String>,
pub description: Option<String>,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize))]
#[cfg_attr(feature = "serde", serde(rename_all = "snake_case"))]
#[non_exhaustive]
pub enum UsageKind {
NextAssemblyUsageOccurrence,
QuantifiedAssemblyComponentUsage,
}
#[derive(Debug, Clone, PartialEq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize))]
#[non_exhaustive]
pub struct Usage {
pub instance: u64,
pub kind: UsageKind,
pub id: Option<String>,
pub name: Option<String>,
pub description: Option<String>,
pub reference_designator: Option<String>,
pub parent: usize,
pub child: usize,
pub quantity: Option<f64>,
pub placement: Option<Placement>,
}
#[derive(Debug, Clone, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize))]
#[cfg_attr(feature = "serde", serde(tag = "kind", rename_all = "snake_case"))]
#[non_exhaustive]
pub enum Placement {
ShapeRelationship {
context_dependent_shape_representation: u64,
relationship: u64,
transformation: Option<u64>,
reversed: Option<bool>,
},
MappedItem {
mapped_item: u64,
representation_map: u64,
origin: Option<u64>,
target: Option<u64>,
},
}
impl Placement {
pub fn reversed(&self) -> Option<bool> {
match self {
Self::ShapeRelationship { reversed, .. } => *reversed,
Self::MappedItem { .. } => None,
}
}
}
#[derive(Debug, Clone, Copy, PartialEq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize))]
#[non_exhaustive]
pub struct BomLine {
pub definition: usize,
pub quantity: f64,
pub is_assembly: bool,
}
#[derive(Debug, Clone, PartialEq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize))]
#[non_exhaustive]
pub struct BomNode {
pub definition: usize,
pub quantity: f64,
pub total: f64,
pub is_assembly: bool,
pub repeated: bool,
pub truncated: bool,
pub components: Vec<BomNode>,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[non_exhaustive]
pub struct BomTreeOptions {
pub depth: Option<usize>,
pub dedupe: bool,
}
impl Default for BomTreeOptions {
fn default() -> Self {
Self {
depth: None,
dedupe: true,
}
}
}
impl BomTreeOptions {
pub fn new(depth: Option<usize>, dedupe: bool) -> Self {
Self { depth, dedupe }
}
}
const MAX_BOM_NODES: usize = 1_000_000;
impl ProductStructure {
pub fn new(graph: &Graph<'_>) -> Self {
let exchange = graph.exchange();
let mut builder = Builder {
graph,
exchange,
definitions: Vec::new(),
by_node: HashMap::new(),
};
for node in exchange.instances_of("PRODUCT_DEFINITION") {
builder.definition(node);
}
let mut usage_nodes: Vec<usize> = exchange
.instances_of(NAUO)
.chain(exchange.instances_of(QACU))
.collect();
usage_nodes.sort_unstable();
usage_nodes.dedup();
let mut usages = Vec::new();
let mut placed_by = Vec::new();
for node in usage_nodes {
let instance = graph.instance(node);
let Some(fields) = usage_fields(exchange, instance) else {
continue;
};
let (Some(parent), Some(child)) =
(graph.node(fields.relating), graph.node(fields.related))
else {
continue;
};
let parent = builder.definition(parent);
let child = builder.definition(child);
usages.push(Usage {
instance: instance.id,
kind: fields.kind,
id: fields.id,
name: fields.name,
description: fields.description,
reference_designator: fields.reference_designator,
parent,
child,
quantity: fields.quantity.and_then(|id| measure_value(graph, id)),
placement: None,
});
placed_by.push(node);
}
let definitions = builder.definitions;
for (usage, &node) in usages.iter_mut().zip(&placed_by) {
usage.placement = shape_relationship(
graph,
node,
&definitions[usage.parent].shape_representations,
&definitions[usage.child].shape_representations,
);
}
let mut unplaced: HashMap<(usize, usize), Vec<usize>> = HashMap::new();
for (index, usage) in usages.iter().enumerate() {
if usage.placement.is_none() {
unplaced
.entry((usage.parent, usage.child))
.or_default()
.push(index);
}
}
for ((parent, child), indices) in unplaced {
let items = mapped_items(
graph,
&definitions[parent].shape_representations,
&definitions[child].shape_representations,
);
if items.len() == indices.len() {
for (index, placement) in indices.into_iter().zip(items) {
usages[index].placement = Some(placement);
}
}
}
let mut children = vec![Vec::new(); definitions.len()];
let mut is_child = vec![false; definitions.len()];
for (index, usage) in usages.iter().enumerate() {
children[usage.parent].push(index);
is_child[usage.child] = true;
}
let roots = (0..definitions.len()).filter(|&d| !is_child[d]).collect();
let shape_index = super::shape::index(graph, &definitions, &usages);
Self {
definitions,
usages,
roots,
children,
shape_index,
}
}
pub(super) fn shape_index(&self) -> &super::shape::ShapeIndex {
&self.shape_index
}
pub fn definitions(&self) -> &[Definition] {
&self.definitions
}
pub fn usages(&self) -> &[Usage] {
&self.usages
}
pub fn roots(&self) -> &[usize] {
&self.roots
}
pub fn children(&self, definition: usize) -> impl ExactSizeIterator<Item = &Usage> {
self.children[definition]
.iter()
.map(|&index| &self.usages[index])
}
pub(crate) fn child_usages(&self, definition: usize) -> &[usize] {
&self.children[definition]
}
pub fn components(&self, definition: usize) -> Vec<(usize, Vec<&Usage>)> {
let mut components: Vec<(usize, Vec<&Usage>)> = Vec::new();
for usage in self.children(definition) {
match components
.iter_mut()
.find(|(child, _)| *child == usage.child)
{
Some((_, usages)) => usages.push(usage),
None => components.push((usage.child, vec![usage])),
}
}
components
}
pub fn bill_of_materials(&self, root: usize) -> Vec<BomLine> {
let mut memo = vec![None; self.definitions.len()];
let mut visiting = vec![false; self.definitions.len()];
self.totals(root, &mut memo, &mut visiting, 0)
.into_iter()
.map(|(definition, quantity)| BomLine {
definition,
quantity,
is_assembly: !self.children[definition].is_empty(),
})
.collect()
}
pub fn bom_tree(&self, root: usize, options: BomTreeOptions) -> BomNode {
let mut state = BomWalk {
options,
expanded: vec![false; self.definitions.len()],
path: vec![false; self.definitions.len()],
budget: MAX_BOM_NODES,
};
self.bom_node(root, 1.0, 1.0, 0, &mut state)
}
fn bom_node(
&self,
definition: usize,
quantity: f64,
total: f64,
level: usize,
state: &mut BomWalk,
) -> BomNode {
state.budget = state.budget.saturating_sub(1);
let mut node = BomNode {
definition,
quantity,
total,
is_assembly: !self.children[definition].is_empty(),
repeated: false,
truncated: false,
components: Vec::new(),
};
if !node.is_assembly {
return node;
}
if state.options.dedupe && state.expanded[definition] {
node.repeated = true;
return node;
}
let too_deep = state.options.depth.is_some_and(|depth| level >= depth);
if too_deep || state.path[definition] || level >= MAX_ASSEMBLY_DEPTH || state.budget == 0 {
node.truncated = true;
return node;
}
state.expanded[definition] = true;
state.path[definition] = true;
for (child, usages) in self.components(definition) {
if state.budget == 0 {
node.truncated = true;
break;
}
let each: f64 = usages.iter().map(|u| u.quantity.unwrap_or(1.0)).sum();
let component = self.bom_node(child, each, total * each, level + 1, state);
node.components.push(component);
}
state.path[definition] = false;
node
}
fn totals(
&self,
definition: usize,
memo: &mut [Option<Vec<(usize, f64)>>],
visiting: &mut [bool],
depth: usize,
) -> Vec<(usize, f64)> {
if let Some(totals) = &memo[definition] {
return totals.clone();
}
let mut totals: Vec<(usize, f64)> = Vec::new();
let mut positions: HashMap<usize, usize> = HashMap::new();
let mut add = |definition: usize, quantity: f64| {
if let Some(&at) = positions.get(&definition) {
totals[at].1 += quantity;
} else {
positions.insert(definition, totals.len());
totals.push((definition, quantity));
}
};
if depth < MAX_ASSEMBLY_DEPTH {
visiting[definition] = true;
for &index in &self.children[definition] {
let usage = &self.usages[index];
if visiting[usage.child] {
continue;
}
let quantity = usage.quantity.unwrap_or(1.0);
add(usage.child, quantity);
for (descendant, each) in self.totals(usage.child, memo, visiting, depth + 1) {
add(descendant, quantity * each);
}
}
visiting[definition] = false;
}
memo[definition] = Some(totals.clone());
totals
}
}
struct BomWalk {
options: BomTreeOptions,
expanded: Vec<bool>,
path: Vec<bool>,
budget: usize,
}
struct Builder<'g, 'a> {
graph: &'g Graph<'a>,
exchange: &'g Exchange<'a>,
definitions: Vec<Definition>,
by_node: HashMap<usize, usize>,
}
impl Builder<'_, '_> {
fn definition(&mut self, node: usize) -> usize {
if let Some(&index) = self.by_node.get(&node) {
return index;
}
let graph = self.graph;
let exchange = self.exchange;
let instance = graph.instance(node);
let record = primary_record(exchange, instance, "PRODUCT_DEFINITION");
let entity = record.map(|r| upper(r.name())).unwrap_or_default();
let standard =
entity == "PRODUCT_DEFINITION" || entity.starts_with("PRODUCT_DEFINITION_WITH_");
let mut definition = Definition {
instance: instance.id,
entity,
id: None,
description: None,
version: None,
product: None,
shape_representations: shape_representations(graph, node),
};
if let (true, Some(record)) = (standard, record) {
definition.id = text(record.param(0).as_ref());
definition.description = text(record.param(1).as_ref());
let formation = reference(record.param(2).as_ref())
.and_then(|id| graph.node(id))
.and_then(|node| exchange.records(graph.instance(node)).next());
if let Some(formation) = formation {
definition.version = text(formation.param(0).as_ref());
definition.product = reference(formation.param(2).as_ref())
.and_then(|id| graph.node(id))
.and_then(|node| {
let product = graph.instance(node);
let record = primary_record(exchange, product, "PRODUCT")?;
Some(Product {
instance: product.id,
id: text(record.param(0).as_ref()),
name: text(record.param(1).as_ref()),
description: text(record.param(2).as_ref()),
})
});
}
}
let index = self.definitions.len();
self.definitions.push(definition);
self.by_node.insert(node, index);
index
}
}
struct UsageFields {
kind: UsageKind,
id: Option<String>,
name: Option<String>,
description: Option<String>,
relating: u64,
related: u64,
reference_designator: Option<String>,
quantity: Option<u64>,
}
fn usage_fields(exchange: &Exchange<'_>, instance: &Instance) -> Option<UsageFields> {
let records: Vec<Record<'_>> = exchange.records(instance).collect();
let find = |name: &str| records.iter().copied().find(|r| r.is(name));
let quantified = find(QACU).is_some();
let kind = if quantified {
UsageKind::QuantifiedAssemblyComponentUsage
} else {
UsageKind::NextAssemblyUsageOccurrence
};
let (relationship, designator, quantity) = if instance.is_complex() {
(
find("PRODUCT_DEFINITION_RELATIONSHIP")?,
find("ASSEMBLY_COMPONENT_USAGE").and_then(|r| r.param(0)),
find(QACU).and_then(|r| r.param(0)),
)
} else {
let record = *records.first()?;
(
record,
record.param(5),
if quantified { record.param(6) } else { None },
)
};
Some(UsageFields {
kind,
id: text(relationship.param(0).as_ref()),
name: text(relationship.param(1).as_ref()),
description: text(relationship.param(2).as_ref()),
relating: reference(relationship.param(3).as_ref())?,
related: reference(relationship.param(4).as_ref())?,
reference_designator: text(designator.as_ref()),
quantity: reference(quantity.as_ref()),
})
}
fn shape_representations(graph: &Graph<'_>, definition: usize) -> Vec<u64> {
let id = graph.instance(definition).id;
let mut representations = Vec::new();
for &shape in graph.referenced_by(definition) {
if !record_references(graph, shape, "PRODUCT_DEFINITION_SHAPE", 2, id) {
continue;
}
let shape_id = graph.instance(shape).id;
for &link in graph.referenced_by(shape) {
if record_references(graph, link, "SHAPE_DEFINITION_REPRESENTATION", 0, shape_id) {
if let Some(representation) = first_reference(graph, link, 1) {
representations.push(representation);
}
}
}
}
representations
}
fn shape_relationship(
graph: &Graph<'_>,
usage: usize,
parent_shapes: &[u64],
child_shapes: &[u64],
) -> Option<Placement> {
let exchange = graph.exchange();
let usage_id = graph.instance(usage).id;
for &shape in graph.referenced_by(usage) {
if !record_references(graph, shape, "PRODUCT_DEFINITION_SHAPE", 2, usage_id) {
continue;
}
let shape_id = graph.instance(shape).id;
for &link in graph.referenced_by(shape) {
if !record_references(
graph,
link,
"CONTEXT_DEPENDENT_SHAPE_REPRESENTATION",
1,
shape_id,
) {
continue;
}
let Some(relationship) = first_reference(graph, link, 0) else {
continue;
};
let Some(node) = graph.node(relationship) else {
continue;
};
let instance = graph.instance(node);
let records: Vec<Record<'_>> = exchange.records(instance).collect();
let (base, transformation) = if instance.is_complex() {
(
records
.iter()
.copied()
.find(|r| r.is("REPRESENTATION_RELATIONSHIP")),
records
.iter()
.find(|r| r.is("REPRESENTATION_RELATIONSHIP_WITH_TRANSFORMATION"))
.and_then(|r| reference(r.param(0).as_ref())),
)
} else {
let record = records.first().copied();
let transformation = record
.filter(|r| upper(r.name()).ends_with("WITH_TRANSFORMATION"))
.and_then(|r| reference(r.param(4).as_ref()));
(record, transformation)
};
let rep_1 = base.and_then(|r| reference(r.param(2).as_ref()));
let rep_2 = base.and_then(|r| reference(r.param(3).as_ref()));
let is_child = |rep: Option<u64>| rep.is_some_and(|rep| child_shapes.contains(&rep));
let is_parent = |rep: Option<u64>| rep.is_some_and(|rep| parent_shapes.contains(&rep));
let reversed = if is_child(rep_1) || is_parent(rep_2) {
Some(false)
} else if is_child(rep_2) || is_parent(rep_1) {
Some(true)
} else {
None
};
return Some(Placement::ShapeRelationship {
context_dependent_shape_representation: graph.instance(link).id,
relationship,
transformation,
reversed,
});
}
}
None
}
fn mapped_items(graph: &Graph<'_>, parent_shapes: &[u64], child_shapes: &[u64]) -> Vec<Placement> {
let exchange = graph.exchange();
let mut placements = Vec::new();
for &shape in parent_shapes {
let items = record::representation(exchange, shape)
.map(|representation| representation.items)
.unwrap_or_default();
for id in items {
let Some(node) = graph.node(id) else {
continue;
};
let instance = graph.instance(node);
let Some(item) =
primary_record(exchange, instance, "MAPPED_ITEM").filter(|r| r.is("MAPPED_ITEM"))
else {
continue;
};
let first = usize::from(!instance.is_complex());
let Some(map_id) = reference(item.param(first).as_ref()) else {
continue;
};
let Some(map) = graph
.node(map_id)
.and_then(|node| exchange.records(graph.instance(node)).next())
.filter(|r| r.is("REPRESENTATION_MAP"))
else {
continue;
};
if reference(map.param(1).as_ref()).is_some_and(|rep| child_shapes.contains(&rep)) {
placements.push(Placement::MappedItem {
mapped_item: id,
representation_map: map_id,
origin: reference(map.param(0).as_ref()),
target: reference(item.param(first + 1).as_ref()),
});
}
}
}
placements
}
pub(super) fn context_dependent_shape_representations(graph: &Graph<'_>, usage: usize) -> Vec<u64> {
let usage_id = graph.instance(usage).id;
let mut found = Vec::new();
for &shape in graph.referenced_by(usage) {
if !record_references(graph, shape, "PRODUCT_DEFINITION_SHAPE", 2, usage_id) {
continue;
}
let shape_id = graph.instance(shape).id;
for &link in graph.referenced_by(shape) {
if record_references(
graph,
link,
"CONTEXT_DEPENDENT_SHAPE_REPRESENTATION",
1,
shape_id,
) {
found.push(graph.instance(link).id);
}
}
}
found
}
fn measure_value(graph: &Graph<'_>, id: u64) -> Option<f64> {
let node = graph.node(id)?;
let exchange = graph.exchange();
let record = primary_record(exchange, graph.instance(node), "MEASURE_WITH_UNIT")?;
let value = record.param(0)?;
let literal = match value.typed() {
Some(typed) => typed.param(0)?.literal()?,
None => value.literal()?,
};
literal.to_f64()
}
fn record_references(
graph: &Graph<'_>,
node: usize,
entity: &str,
index: usize,
target: u64,
) -> bool {
graph
.exchange()
.records(graph.instance(node))
.next()
.is_some_and(|r| r.is(entity) && reference(r.param(index).as_ref()) == Some(target))
}
fn first_reference(graph: &Graph<'_>, node: usize, index: usize) -> Option<u64> {
let record = graph.exchange().records(graph.instance(node)).next()?;
reference(record.param(index).as_ref())
}
fn text(param: Option<&Param<'_>>) -> Option<String> {
let Some(Param::String(literal)) = param else {
return None;
};
let value = literal.decode().map_or_else(
|_| String::from_utf8_lossy(literal.text()).into_owned(),
Cow::into_owned,
);
(!value.trim().is_empty()).then_some(value)
}
#[cfg(test)]
mod tests {
use super::*;
use crate::p21::parse;
const SAMPLE: &str = "ISO-10303-21;HEADER;FILE_SCHEMA(('AP242'));ENDSEC;DATA;
#1=APPLICATION_CONTEXT('design');
#2=PRODUCT_CONTEXT('',#1,'mechanical');
#3=PRODUCT_DEFINITION_CONTEXT('part definition',#1,'design');
#11=PRODUCT('A','assembly','',(#2));
#12=PRODUCT_DEFINITION_FORMATION('1','',#11);
#10=PRODUCT_DEFINITION('a-def','',#12,#3);
#21=PRODUCT('B','bracket assembly','',(#2));
#22=PRODUCT_DEFINITION_FORMATION_WITH_SPECIFIED_SOURCE('2','',#21,.MADE.);
#20=PRODUCT_DEFINITION('b-def','',#22,#3);
#31=PRODUCT('C','bolt','',(#2));
#32=PRODUCT_DEFINITION_FORMATION('1','',#31);
#30=PRODUCT_DEFINITION('c-def','',#32,#3);
#41=PRODUCT('D','plate','',(#2));
#42=PRODUCT_DEFINITION_FORMATION('1','',#41);
#40=PRODUCT_DEFINITION('d-def','',#42,#3);
#50=SHAPE_REPRESENTATION('a',(#60),#1);
#51=SHAPE_REPRESENTATION('b',(#60),#1);
#52=SHAPE_REPRESENTATION('c',(#60),#1);
#60=AXIS2_PLACEMENT_3D('',#61,$,$);
#61=CARTESIAN_POINT('',(0.,0.,0.));
#70=PRODUCT_DEFINITION_SHAPE('','',#10);
#71=PRODUCT_DEFINITION_SHAPE('','',#20);
#72=PRODUCT_DEFINITION_SHAPE('','',#30);
#80=SHAPE_DEFINITION_REPRESENTATION(#70,#50);
#81=SHAPE_DEFINITION_REPRESENTATION(#71,#51);
#82=SHAPE_DEFINITION_REPRESENTATION(#72,#52);
#100=NEXT_ASSEMBLY_USAGE_OCCURRENCE('u1','B1','',#10,#20,$);
#101=NEXT_ASSEMBLY_USAGE_OCCURRENCE('u2','B2','',#10,#20,$);
#102=NEXT_ASSEMBLY_USAGE_OCCURRENCE('u3','C1','',#20,#30,'X1');
#103=NEXT_ASSEMBLY_USAGE_OCCURRENCE('u4','C2','',#20,#30,'X2');
#104=NEXT_ASSEMBLY_USAGE_OCCURRENCE('u5','C3','',#20,#30,'X3');
#105=(ASSEMBLY_COMPONENT_USAGE($)NEXT_ASSEMBLY_USAGE_OCCURRENCE()PRODUCT_DEFINITION_RELATIONSHIP('u6','D','',#10,#40)PRODUCT_DEFINITION_USAGE()QUANTIFIED_ASSEMBLY_COMPONENT_USAGE(#106));
#106=MEASURE_WITH_UNIT(COUNT_MEASURE(4.),#1);
#90=PRODUCT_DEFINITION_SHAPE('','',#100);
#91=(REPRESENTATION_RELATIONSHIP('','',#51,#50)REPRESENTATION_RELATIONSHIP_WITH_TRANSFORMATION(#92)SHAPE_REPRESENTATION_RELATIONSHIP());
#92=ITEM_DEFINED_TRANSFORMATION('','',#60,#60);
#93=CONTEXT_DEPENDENT_SHAPE_REPRESENTATION(#91,#90);
#94=PRODUCT_DEFINITION_SHAPE('','',#102);
#95=REPRESENTATION_RELATIONSHIP_WITH_TRANSFORMATION('','',#51,#52,#92);
#96=CONTEXT_DEPENDENT_SHAPE_REPRESENTATION(#95,#94);
ENDSEC;END-ISO-10303-21;";
fn mapped_sample(items: &str) -> String {
format!(
"ISO-10303-21;HEADER;FILE_SCHEMA(('X'));ENDSEC;DATA;
#1=APPLICATION_CONTEXT('design');
#10=PRODUCT_DEFINITION('a','',$,$);
#30=PRODUCT_DEFINITION('c','',$,$);
#50=ADVANCED_BREP_SHAPE_REPRESENTATION('a',({items}),#1);
#52=ADVANCED_BREP_SHAPE_REPRESENTATION('c',(#60),#1);
#60=AXIS2_PLACEMENT_3D('',#61,$,$);
#61=CARTESIAN_POINT('',(0.,0.,0.));
#62=AXIS2_PLACEMENT_3D('',#61,$,$);
#70=PRODUCT_DEFINITION_SHAPE('','',#10);
#72=PRODUCT_DEFINITION_SHAPE('','',#30);
#80=SHAPE_DEFINITION_REPRESENTATION(#70,#50);
#82=SHAPE_DEFINITION_REPRESENTATION(#72,#52);
#100=NEXT_ASSEMBLY_USAGE_OCCURRENCE('1','','',#10,#30,$);
#101=NEXT_ASSEMBLY_USAGE_OCCURRENCE('2','','',#10,#30,$);
#150=REPRESENTATION_MAP(#60,#52);
#200=MAPPED_ITEM('',#150,#60);
#201=MAPPED_ITEM('',#150,#62);
ENDSEC;END-ISO-10303-21;"
)
}
fn structure_of(src: &str) -> ProductStructure {
ProductStructure::new(&Graph::new(parse(src.as_bytes()).unwrap()).unwrap())
}
fn structure() -> ProductStructure {
structure_of(SAMPLE)
}
#[test]
fn definitions_and_products() {
let structure = structure();
let instances: Vec<u64> = structure.definitions().iter().map(|d| d.instance).collect();
assert_eq!(instances, [10, 20, 30, 40]);
let b = &structure.definitions()[1];
assert_eq!(b.id.as_deref(), Some("b-def"));
assert_eq!(b.version.as_deref(), Some("2"));
let product = b.product.as_ref().unwrap();
assert_eq!(
(
product.instance,
product.id.as_deref(),
product.name.as_deref()
),
(21, Some("B"), Some("bracket assembly"))
);
assert_eq!(product.description, None, "blank strings are omitted");
assert_eq!(structure.definitions()[0].shape_representations, [50]);
let shapes = &structure.definitions()[3].shape_representations;
assert!(shapes.is_empty(), "{shapes:?}");
assert_eq!(structure.roots(), [0]);
}
#[test]
fn usages_simple_and_complex() {
let structure = structure();
let usages = structure.usages();
assert_eq!(usages.len(), 6);
assert_eq!(
usages
.iter()
.map(|u| (u.instance, u.parent, u.child))
.collect::<Vec<_>>(),
[
(100, 0, 1),
(101, 0, 1),
(102, 1, 2),
(103, 1, 2),
(104, 1, 2),
(105, 0, 3)
]
);
assert_eq!(usages[2].reference_designator.as_deref(), Some("X1"));
assert_eq!(usages[0].kind, UsageKind::NextAssemblyUsageOccurrence);
let quantified = &usages[5];
assert_eq!(quantified.kind, UsageKind::QuantifiedAssemblyComponentUsage);
assert_eq!(quantified.id.as_deref(), Some("u6"));
assert_eq!(
quantified.quantity.map(|q| q.to_string()).as_deref(),
Some("4")
);
assert_eq!(quantified.reference_designator, None);
}
#[test]
fn shape_relationship_placements_and_reversal() {
let structure = structure();
let usages = structure.usages();
assert_eq!(
usages[0].placement,
Some(Placement::ShapeRelationship {
context_dependent_shape_representation: 93,
relationship: 91,
transformation: Some(92),
reversed: Some(false),
})
);
assert_eq!(
usages[2].placement,
Some(Placement::ShapeRelationship {
context_dependent_shape_representation: 96,
relationship: 95,
transformation: Some(92),
reversed: Some(true),
})
);
assert_eq!(usages[2].placement.as_ref().unwrap().reversed(), Some(true));
assert_eq!(usages[1].placement, None);
}
#[test]
fn mapped_items_place_usages_in_order() {
let structure = structure_of(&mapped_sample("#200,#61,#201"));
let placements: Vec<Option<Placement>> = structure
.usages()
.iter()
.map(|u| u.placement.clone())
.collect();
assert_eq!(
placements,
[
Some(Placement::MappedItem {
mapped_item: 200,
representation_map: 150,
origin: Some(60),
target: Some(60),
}),
Some(Placement::MappedItem {
mapped_item: 201,
representation_map: 150,
origin: Some(60),
target: Some(62),
}),
]
);
assert_eq!(placements[0].as_ref().unwrap().reversed(), None);
}
#[test]
fn complex_mapped_items_and_representations_place_usages() {
let src = mapped_sample("#200,#201")
.replace(
"#201=MAPPED_ITEM('',#150,#62);",
"#201=(GEOMETRIC_REPRESENTATION_ITEM()MAPPED_ITEM(#150,#62)\
REPRESENTATION_ITEM(''));",
)
.replace(
"#50=ADVANCED_BREP_SHAPE_REPRESENTATION('a',(#200,#201),#1);",
"#50=(ADVANCED_BREP_SHAPE_REPRESENTATION()REPRESENTATION('a',(#200,#201),#1)\
SHAPE_REPRESENTATION());",
);
let structure = structure_of(&src);
let placements: Vec<Option<Placement>> = structure
.usages()
.iter()
.map(|u| u.placement.clone())
.collect();
assert_eq!(
placements,
[
Some(Placement::MappedItem {
mapped_item: 200,
representation_map: 150,
origin: Some(60),
target: Some(60),
}),
Some(Placement::MappedItem {
mapped_item: 201,
representation_map: 150,
origin: Some(60),
target: Some(62),
}),
]
);
}
#[test]
fn mapped_items_are_not_guessed_when_counts_differ() {
let structure = structure_of(&mapped_sample("#200"));
assert!(structure.usages().iter().all(|u| u.placement.is_none()));
}
#[test]
fn components_group_repeated_usages() {
let structure = structure();
let grouped: Vec<(usize, Vec<u64>)> = structure
.components(0)
.into_iter()
.map(|(child, usages)| (child, usages.iter().map(|u| u.instance).collect()))
.collect();
assert_eq!(grouped, [(1, vec![100, 101]), (3, vec![105])]);
assert_eq!(structure.children(1).len(), 3);
assert_eq!(structure.children(2).len(), 0);
}
#[test]
fn bill_of_materials_multiplies_down_and_adds_across() {
let structure = structure();
let lines: Vec<(usize, String, bool)> = structure
.bill_of_materials(0)
.iter()
.map(|l| (l.definition, l.quantity.to_string(), l.is_assembly))
.collect();
assert_eq!(
lines,
[
(1, "2".to_owned(), true),
(2, "6".to_owned(), false),
(3, "4".to_owned(), false)
]
);
let bom = structure.bill_of_materials(3);
assert!(bom.is_empty(), "{bom:?}");
}
#[test]
fn cycles_do_not_loop() {
let src = "ISO-10303-21;HEADER;FILE_SCHEMA(('X'));ENDSEC;DATA;
#1=PRODUCT_DEFINITION('a','',$,$);
#2=PRODUCT_DEFINITION('b','',$,$);
#3=NEXT_ASSEMBLY_USAGE_OCCURRENCE('1','','',#1,#2,$);
#4=NEXT_ASSEMBLY_USAGE_OCCURRENCE('2','','',#2,#1,$);
#5=NEXT_ASSEMBLY_USAGE_OCCURRENCE('3','','',#9,#1,$);
ENDSEC;END-ISO-10303-21;";
let structure = ProductStructure::new(&Graph::build(parse(src.as_bytes()).unwrap()));
assert_eq!(
structure.usages().len(),
2,
"a usage with an undefined parent is skipped"
);
assert!(
structure.roots().is_empty(),
"every definition is someone's child"
);
let lines = structure.bill_of_materials(0);
assert_eq!(lines.len(), 1);
assert_eq!(lines[0].definition, 1);
let tree = structure.bom_tree(0, BomTreeOptions::default());
assert_eq!(
flatten(&tree),
[
(0, 0, "1".into(), "1".into(), ""),
(1, 1, "1".into(), "1".into(), ""),
(2, 0, "1".into(), "1".into(), "repeated")
]
);
let tree = structure.bom_tree(0, BomTreeOptions::new(None, false));
assert_eq!(flatten(&tree)[2].4, "truncated", "cycles are cut off");
}
fn flatten(node: &BomNode) -> Vec<(usize, usize, String, String, &'static str)> {
fn walk(
node: &BomNode,
level: usize,
out: &mut Vec<(usize, usize, String, String, &'static str)>,
) {
let mark = if node.repeated {
"repeated"
} else if node.truncated {
"truncated"
} else {
""
};
out.push((
level,
node.definition,
node.quantity.to_string(),
node.total.to_string(),
mark,
));
for component in &node.components {
walk(component, level + 1, out);
}
}
let mut out = Vec::new();
walk(node, 0, &mut out);
out
}
#[test]
fn bom_tree_groups_components_and_multiplies_totals() {
let structure = structure();
let tree = structure.bom_tree(0, BomTreeOptions::default());
assert_eq!(
flatten(&tree),
[
(0, 0, "1".into(), "1".into(), ""),
(1, 1, "2".into(), "2".into(), ""),
(2, 2, "3".into(), "6".into(), ""),
(1, 3, "4".into(), "4".into(), "")
]
);
assert!(tree.is_assembly && tree.components[0].is_assembly);
assert!(!tree.components[1].is_assembly);
}
#[test]
fn bom_tree_depth_limits_the_listing() {
let structure = structure();
let tree = structure.bom_tree(0, BomTreeOptions::new(Some(1), true));
assert_eq!(
flatten(&tree),
[
(0, 0, "1".into(), "1".into(), ""),
(1, 1, "2".into(), "2".into(), "truncated"),
(1, 3, "4".into(), "4".into(), "")
]
);
let root_only = structure.bom_tree(0, BomTreeOptions::new(Some(0), true));
assert!(root_only.components.is_empty() && root_only.truncated);
}
#[test]
fn bom_tree_expands_repeated_sub_assemblies_once() {
let src = "ISO-10303-21;HEADER;FILE_SCHEMA(('X'));ENDSEC;DATA;
#1=PRODUCT_DEFINITION('a','',$,$);
#2=PRODUCT_DEFINITION('b','',$,$);
#3=PRODUCT_DEFINITION('e','',$,$);
#4=PRODUCT_DEFINITION('c','',$,$);
#10=NEXT_ASSEMBLY_USAGE_OCCURRENCE('1','','',#1,#2,$);
#11=NEXT_ASSEMBLY_USAGE_OCCURRENCE('2','','',#1,#3,$);
#12=NEXT_ASSEMBLY_USAGE_OCCURRENCE('3','','',#3,#2,$);
#13=NEXT_ASSEMBLY_USAGE_OCCURRENCE('4','','',#2,#4,$);
ENDSEC;END-ISO-10303-21;";
let structure = structure_of(src);
let marks = |dedupe| {
flatten(&structure.bom_tree(0, BomTreeOptions::new(None, dedupe)))
.into_iter()
.map(|(level, definition, _, _, mark)| (level, definition, mark))
.collect::<Vec<_>>()
};
assert_eq!(
marks(true),
[
(0, 0, ""),
(1, 1, ""),
(2, 3, ""),
(1, 2, ""),
(2, 1, "repeated")
]
);
assert_eq!(
marks(false),
[
(0, 0, ""),
(1, 1, ""),
(2, 3, ""),
(1, 2, ""),
(2, 1, ""),
(3, 3, "")
]
);
}
}