cancel
Showing results for 
Search instead for 
Did you mean: 

Python NXOpen.Session.GetSession().Parts not iterable

Creator
Creator

Hello again everyone,

 

I have one problem and one question:

self.ses = NXOpen.Session.GetSession()

and then later:

for part in self.ses.Parts:
self.parts.update(part.Tag, Part(part, self.settings, self))

Raises >>> SystemError: create iterator on bad object

so basically PartCollection is not iterable

 

Am I doing this wrong? And if not:

Is this solved in the latest versions of NX or should I work around it with UFSession.Part.AskNthPart() over all of the parts and then PartCollection.FindObject()

 

 

I also have a question: Can I assume, that if I select and save externally a tag of a face, for certain operation over a given solid, the tag will stay reliably the same every time I load and edit the prt file?

 

 

 

6 REPLIES

Re: Python NXOpen.Session.GetSession().Parts not iterable

Siemens Phenom Siemens Phenom
Siemens Phenom

Try getting the PartCollection as a separate object first.  This worked for me:

 

import NXOpen

theSession = NXOpen.Session.GetSession()

def echo(what) :
theSession.LogFile.WriteLine(what)
theSession.ListingWindow.Open()
theSession.ListingWindow.WriteFullline(what)
theSession.LogFile.WriteLine(what)

def main() :

# workPart = theSession.Parts.Work

theParts = theSession.Parts

for thisPart in theParts:

output = "Part: " + thisPart.Leaf
echo(output)

if __name__ == '__main__':
main()

 

[ Sorry about the lost indenting. ]

 

Tags are not persistent across sessions.

 

Re: Python NXOpen.Session.GetSession().Parts not iterable

Creator
Creator

Unfortunately did not work for me.

 

If not the Tags, is there something else, that can be used?

 

 

 

Re: Python NXOpen.Session.GetSession().Parts not iterable

Siemens Phenom Siemens Phenom
Siemens Phenom

 

I am surprised to hear that did not work.  What did the failure look like?  What release of NX10 are you using?  I tested in NX10.0.3 MP4, I think.

 

The simple method to find a face of interest in a later session is to give it a custom name.

 

The other method is to use the handle.  Handles were created to allow access to entities across sessions.

 

To see detailed information, look in the Open C Reference Guide, in the UF chapter, and see the various UF_TAG... functions, starting with UF_TAG_ask_handle_from_tag()

Re: Python NXOpen.Session.GetSession().Parts not iterable

Creator
Creator

I am using 10.0.1.something

I will update and see what happens - then report back. I really hope that this Python API gets fixed for NX11 Smiley Sad It is a nightmare of bad documentation and "things" that don't work currently ...

 

Thank you for the information about the handles!

Re: Python NXOpen.Session.GetSession().Parts not iterable

Creator
Creator

I forgot this little bit: The failure is absolutely the same as the one in the first post, just this time it is about "parts" insterad of self.ses.Parts - it is attached as a picture.

Re: Python NXOpen.Session.GetSession().Parts not iterable

Genius
Genius

what type of information are you trying to obtain? below is a variation of Steve's code. I have a lot of python documentation for NX10. I just need to find the time to organize and post it somewhere

 

https://docs.plm.automation.siemens.com/data_services/resources/nx/10/nx_api/en_US/custom/nxopen_pyt...

import NXOpen
import NXOpen.BlockStyler
import NXOpen.Features
import NXOpen.UF
import NXOpen.GeometricAnalysis
import NXOpen.Facet
import random
import math




class generic_class:
        # Constructor for NX Styler class
    # ------------------------------------------------------------------------------
    def __init__(self):
        
        # class members
        self.theSession = None
        self.theUI = None
        self.theLw= None
        self.theDialogName = ""
        self.theDialog = None
        #Get the UF session
        self.theUfSession = None

       

        try:
            self.theSession = NXOpen.Session.GetSession()
            self.theUI = NXOpen.UI.GetUI()
            self.theLw = self.theSession.ListingWindow #create print window
            self.theUfSession = NXOpen.UF.UFSession.GetUFSession() 
            
        except Exception as ex:
            # ---- Enter your exception handling code here -----
            raise ex
    
    def hello_world(self):
        global sesh
        self.theLw.Open()
        
        self.theLw.WriteLine("Hello NX World!")
       
        workPart = self.theSession.Parts.Work

        theParts = self.theSession.Parts

        for thisPart in theParts:
        
            self.theLw.WriteLine("The leaf is "+str(thisPart.Leaf))
            self.theLw.WriteLine("The tag is "+str(thisPart.Tag))
def main():
    try:

        #create instance of class
        myclass = generic_class()
        myclass.hello_world()
    except Exception as ex:
        # ---- Enter your exception handling code here -----
        NXOpen.UI.GetUI().NXMessageBox.Show("Block Styler", NXOpen.NXMessageBox.DialogType.Error, str(ex))
   
if __name__ == "__main__":
    main() 


 Below is a variation of Steve Labout's code