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,



Re: Assembly tree traversal

Gears Esteemed Contributor Gears Esteemed Contributor
Gears Esteemed 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")
    c = displayPart.ComponentAssembly
    if c.RootComponent is None:
        theLw.WriteLine("Part has no components")
        theLw.WriteLine("Assembly: " + c.RootComponent.DisplayName)
        theLw.WriteLine("  active arrangement: " + c.ActiveArrangement.Name)
        ReportComponentChildren(c.RootComponent, 0)
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__':