Open/Edit Multiplt NX Files Inside Same "run_journal" Script

Hello all,

I'm currently working to automate some NX tasks but I need to work in multiple assemblies/file parts. I could do this by running multiple scripts using "run_journal()" multiple times, but this is not efficient and would take some time to run. Does anyone know if there is a way to open a separate NX part file while inside a session?

Currently I am using the following code.


theSession = NXOpen.Session.GetSession()

#Opens assembly part desired to modify
#Try to open Part; throw an error upon failure
try:
basepart1, partLoadStatus1 = theSession.Parts.OpenBaseDisplay(assemblyFile)
partLoadStatus1.Dispose()
except Exception:
msg.error(fullLogFile,' The specified file could not be opened.'
+' Ensure that the file name and path are correct.')

#get work part of assembly
workPart = theSession.Parts.Work

##------------------------------------------------------------
##------------------------------------------------------------
## CODE MODIFICATIONS GO HERE (not relevant to problem at hand)
##------------------------------------------------------------
##------------------------------------------------------------

#try to open new part file to edit (different than assemblyFile)
newPart = 'C:/my/directory/to/newPart/'

#use class method to open new session/workPart
newWorkPart,newDisplayPart = ModMass.SetNXFile(newPart,logFile)

##------------------------------------------------------------
##------------------------------------------------------------
## NEW PART MODIFICATIONS GO HERE
##------------------------------------------------------------
##------------------------------------------------------------

#now we need to get back to original assembly file.
workPart,displayPart = ModMass.SetNXFile(assemblyFile,logFile)

The class method is shown here: (this opens a new file)


import NXOpen
import MsgAppend as msg

#Configure message appending
msg = msg.MsgAppend()

class ModMassIndex():

def __init__(self):
super(ModMassIndex, self).__init__()

def SetNXFile(self,NXFile,logFile):
theSession = NXOpen.Session.GetSession()
#Opens assembly part desired to modify
#Try to open Part; throw an error upon failure
try:
basepart1, partLoadStatus1 = theSession.Parts.OpenBaseDisplay(NXFile)
partLoadStatus1.Dispose()
except Exception:
msg.error(logFile,' The specified file could not be opened.'
+' Ensure that the file name and path are correct.')

#Obtains needed NXOpen work class that give access to other objects
if theSession.Parts.Work == None:
msg.error(logFile,' NXOpen.Session.GetSession().Parts.Work is Null.')
else:
workPart = theSession.Parts.Work
displayPart = theSession.Parts.Display

return workPart, displayPart

This code successfully opens the new desired part, but when I need to access components of the original part later on, it cannot locate the assembly. If anyone has any insight on this issue, it would be greatly appreciated!

Thank you!
Sam

If I remember correctly, the .OpenBaseDisplay method will throw an exception if the file is already open in session. To check this, you could have your code write out the actual exception message, rather than a generic "file could not be opened" message that you have now.

To alleviate this issue, you could keep a reference to the part once you open it; if/when you need to access it later, use the reference that you already have instead of attempting to open it again. Alternately, you could search through the currently open parts in the session to see if the part is already open before attempting to open it again.

I'm not quite sure I am following you when you say "keep a reference to the part." For the alternative method, how do I cycle through open parts?

Thanks for the reply!

By "reference", I mean keeping a variable that references the part. In your code above, "basepart1" is a part object variable that references the assembly part. As long as this variable is still in scope and the value has not changed, you can reference "basepart1" when you want to work with the assembly part.

The session has a .Parts collection that contains the currently open parts. The following thread has some example code (in VB format, but the concept is the same in other languages):
http://nxjournaling.com/content/check-if-part-already-loaded-session