cancel
Showing results for 
Search instead for 
Did you mean: 

Assembly tree traversal

Solution Partner Pioneer Solution Partner Pioneer
Solution Partner Pioneer

How to do assembly tree traversal? I want to do some activities on each part of assembly.

 

I use Python.

 

Best regards,

Anton

1 REPLY

Re: Assembly tree traversal

Honored Contributor
Honored Contributor

The journal below is basically a port of the VB code found here. The code just reports the assembly structure of the current display part. You can add code to the "ReportComponentChildren" function to perform other actions, or, if the actions are more involved, write a separate function(s) to do the work that is called from the ReportComponentChildren function.

 

import NXOpen
import NXOpen.UF

theSession = NXOpen.Session.GetSession()
theLw = theSession.ListingWindow
theUfSession = NXOpen.UF.UFSession.GetUFSession()    
    
def main(): 

    workPart = theSession.Parts.Work
    displayPart = theSession.Parts.Display
    
    markId1 = theSession.SetUndoMark(NXOpen.Session.MarkVisibility.Visible, "report assembly")
    theLw.Open()
    
    c = displayPart.ComponentAssembly
    
    if c.RootComponent is None:
        theLw.WriteLine("Part has no components")
    else:
        theLw.WriteLine("Assembly: " + c.RootComponent.DisplayName)
        theLw.WriteLine("  active arrangement: " + c.ActiveArrangement.Name)
        ReportComponentChildren(c.RootComponent, 0)
    
        
    theLw.Close()
    
def ReportComponentChildren(comp, indent):
    
    for child in comp.GetChildren():
        padding = " " * indent
        theLw.WriteLine(padding + child.DisplayName)
        grandkids = child.GetChildren()
        if len(grandkids) > 0:
            theLw.WriteLine(padding + " * subassembly with " + str(len(grandkids)) + " components")
            theLw.WriteLine(padding + " + Active Arrangement: " + child.OwningPart.ComponentAssembly.ActiveArrangement.Name)
            
        ReportComponentChildren(child, indent + 1)
    
if __name__ == '__main__':
    main()