I am looking into a way to iterate over a model loaded into a Viewer to get items that are equivalent to the categories objects seen in Model Browser Panel in the ACC Docs Viewer:
Reading the documentation for current Viewer version 7, I see that there are getInstanceTree() and getObjectTree() methods. GetInstanceTree() method returns InstanceTree object that is documented but I don't seem to find info about object returned by getObjectTree() method. So what is the difference between them? And what is the recommended way to traverse objects in the model and get those categories?
Here is a way to build the tree view for the model structure:
function buildModelTree( model ) {
//builds model tree recursively
function _buildModelTreeRec( node ) {
it.enumNodeChildren( node.dbId, function(childId) {
node.children = node.children || [];
let childNode = {
dbId: childId,
name: it.getNodeName( childId )
};
node.children.push( childNode );
_buildModelTreeRec( childNode );
});
}
//get model instance tree and root component
let it = model.getInstanceTree();
let rootId = it.getRootId();
let rootNode = {
dbId: rootId,
name: it.getNodeName( rootId )
};
_buildModelTreeRec( rootNode );
return rootNode;
}
buildModelTree(viewer.model)
If you just want to get objects at the first tree level, then you can simplify it to the following:
let it = model.getInstanceTree();
let rootId = it.getRootId();
it.enumNodeChildren( rootId, function(childId) {
console.log({
dbId: childId,
name: it.getNodeName( childId)
});
});