Submitted by shaberberger on Tue, 03/21/2017 - 12:50
Forums:
Hello all,
I have a quick question. Does anyone know how to construct an array of all components loaded into an assembly in NXOpen's Python API? Everything I have tried leads to the same error:
"TypeError: 'NXOpen.Assemblies.Component' object is not iterable"
I've tried code such as:
import NXOpen
theSession = NXOpen.Session.GetSession()
for x in theSession.Parts.Work.Bodies:
if x.IsSolidBody:
theBodyTags.append(x.Tag)
but this only gives me one component tag for some reason. Any help or insight on this issue would be greatly appreciated!
Thanks so much for your help!
Sam
re: listing components
The code above looks for solid bodies in the current work part; the tag it returns is NOT a component tag.
The code below will write the display name of each top level component contained in the current display part.
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, "body feature group")
theLw.Open()
# initialize list to hold components
comps = theSession.Parts.Display.ComponentAssembly.RootComponent.GetChildren()
for x in comps:
theLw.WriteLine(x.DisplayName)
theLw.Close()
if __name__ == '__main__':
main()
If you need sub components, you will need to write a recursive function to process all the assembly levels. The code in the link below is Visual Basic, but the overall strategy also applies to Python.
http://nxjournaling.com/content/creating-subroutine-process-all-componen...