0

I have uploded an inventor file using autodesk forge (API) which have a structure of assembly, subassembly and parts. I want to count total number of part in the assembly without counting assembly and subassembly.

I am using follwing method to count totalelements = getAllLeafIdsOfParentId(viewer.model.getData().instanceTree.getRootId()) to get all the nodes in the model. But it counts assembly and subassembly also.

2nd part of question is that if I get only the parts node then I would like to push only the part nodes to an array and block the assembly and subassembly nodes. If the user select an assembly by mistake instead of the parts, it will not allow to push that assembly to percular array of objects. Hope I have put a clear question to undertand. Thank you.

kaushik
  • 15
  • 2

2 Answers2

0

I solve the issue by modifying "getAllLeafIdsOfParentId" function as below.

    function getAllLeafIdsOfParentId(id) {
let allIds = [];
let partIds=[];   
const instanceTree = viewer.model.getData().instanceTree;  
if (instanceTree.getChildCount(id) > 0 ) {            
    allIds.push(id);      
    instanceTree.enumNodeChildren(id, function (child) {
    
    allIds = allIds.concat(getAllLeafIdsOfParentId(child));
    partIds = partIds.concat(getAllLeafIdsOfParentId(child));       
    }, false);
}
else if(instanceTree.getChildCount(id) < 1) {
    
    partIds.push(id);
   // console.log(partIds.length);      
           
}
else {
    allIds.push(id);
    //console.log(allIds.length);       
}

return partIds;

}

kaushik
  • 15
  • 2
0

Adding to the first answer, the instance tree is a generally useful structure that can be used to traverse the logical hierarchy, inspect the different elements of the hierarchy, etc.

For example, when using the instanceTree.enumNodeChildren method, your callback (the function that is called for each child) can return a boolean that affects the traversal logic - if your function returns true, the recursive traversal stops at this node, otherwise it continues further down the tree.

There's also a method called instanceTree.getNodeType that returns the Autodesk.Viewing.Private.NODE_TYPE enum which can have the following values:

{
    NODE_TYPE_ASSEMBLY   : 0x0,
    NODE_TYPE_INSERT     : 0x1,
    NODE_TYPE_LAYER      : 0x2,
    NODE_TYPE_COLLECTION : 0x3,
    NODE_TYPE_COMPOSITE  : 0x4,
    NODE_TYPE_MODEL      : 0x5,
    NODE_TYPE_GEOMETRY   : 0x6,
    NODE_TYPE_BITS       : 0x7
};

So you could use this as well to find out what type of node you're working with.

Petr Broz
  • 8,891
  • 2
  • 15
  • 24