Model Traversal
This lesson walks through the SampleModelTraversal.java tutorial, which demonstrates how to navigate the AP 210 entity graph programmatically.
The Sample Model Traversal
The SampleModelTraversal.java class provides a practical example of how to:
Open an AP 210 STEP file
Find the top-level assembly
Traverse the product structure
Access component placement data
Read shape and property information
Opening and Initializing
public class SampleModelTraversal {
private Ap210_schema schema;
private SdaiModel model;
public void open(String filename) throws SdaiException {
SdaiRepository repo = SdaiSession.getSession().createRepository();
model = repo.openModel("ap210_data", filename);
schema = (Ap210_schema) model.getSchema();
}
}Finding the Top-Level Product
public E_product findTopLevelProduct() throws SdaiException {
A_product products = new A_product();
schema.getInstances(schema.product, products);
// Find the product that is not used as a component
// (i.e., has no NAUO referencing it as the related definition)
SdaiIterator it = products.createIterator();
while (it.next()) {
E_product product = products.getCurrentMember(it);
if (isTopLevel(product)) {
return product;
}
}
return null;
}Traversing the Assembly
public void traverseAssembly(E_product product, int depth) throws SdaiException {
String indent = " ".repeat(depth);
System.out.println(indent + product.getId(null) + ": " + product.getName(null));
// Find all component usages
A_next_assembly_usage_occurrence naus = getComponentUsages(product);
SdaiIterator it = naus.createIterator();
while (it.next()) {
E_next_assembly_usage_occurrence nauo = naus.getCurrentMember(it);
E_product component = getComponentProduct(nauo);
traverseAssembly(component, depth + 1);
}
}Accessing Placement Data
public double[] getComponentLocation(E_next_assembly_usage_occurrence nauo)
throws SdaiException {
E_item_defined_transformation idt = getPlacement(nauo);
E_axis2_placement_3d placement = (E_axis2_placement_3d) idt Transform_item_2(null);
E_cartesian_point location = placement.getLocation(null);
return location.getCoordinates(null);
}Key Traversal Patterns
The traversal follows a consistent pattern:
Get instances of a type -
schema.getInstances(schema.type, aggregate)Iterate -
aggregate.createIterator()Access attributes -
entity.getAttributeName(null)Navigate references - Follow attribute values to related entities
Repeat recursively - For hierarchical structures
Practical Tips
Always handle
SdaiException- JSDAI throws on invalid data accessUse
nullparameter fornullcontext - Most JSDAI methods take a context parameterClose models when done - Call
model.close()to free resourcesUse the schema-specific constants - Access entity types through
schema.type_name