Forums:
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