Continue to Site

Eng-Tips is the largest engineering community on the Internet

Intelligent Work Forums for Engineering Professionals

  • Congratulations waross on being selected by the Eng-Tips community for having the most helpful posts in the forums last week. Way to Go!

Using Current Path in a Macro

Status
Not open for further replies.

claforet

Mechanical
Apr 8, 2010
54
I'm trying to create a macro (in NX5) to export a step file from a part file. When I manually export the step file, the current path and filename is automatically put in the export dialogue, but when I run a recorded macro, it inserts the path and filename of the file I wrote the macro with. Does anyone know if there is a "current_path" and/or "current_filename" type tag that I can use replace the string that gets recorded in the macro. I tried the "User Input on Dialog Boxes" option, but for some reason, it doesn't work when I open the save dialogue box in the export window.

Thanks
 
Replies continue below

Recommended for you

I have a couple of functions that I use to get the current path and file name.

Code:
'***********************************************************************

Function GetFileName()
	Dim strPath as String
	Dim strPart as String
	Dim pos as Integer
	
	'get the full file path
	strPath = displayPart.fullpath
	'get the part file name
	pos = InStrRev(strPath, "\")
	strPart = Mid(strPath, pos + 1)
	
	strPath = Left(strPath, pos)
	'strip off the ".prt" extension
	strPart = Left(strPart, Len(strPart) - 4)
	
	GetFileName = strPart
End Function
'***********************************************************************

Function GetFilePath()
	Dim strPath as String
	Dim strPart as String
	Dim pos as Integer
	
	'get the full file path
	strPath = displayPart.fullpath
	'get the part file name
	pos = InStrRev(strPath, "\")
	strPart = Mid(strPath, pos + 1)
	
	strPath = Left(strPath, pos)
	'strip off the ".prt" extension
	strPart = Left(strPart, Len(strPart) - 4)
	
	GetFilePath = strPath
End Function
'***********************************************************************
You can use them within a function or use variables to save the contents like this:
Code:
Dim currentPath as string
Dim currentFile as string

'full path and file name of current file
'currentFile = GetFilePath() & GetFileName() & ".prt"

'path of current file
currentPath = GetFilePath()

'file name (without '.prt') of current file
currentFile = GetFileName()
 
Thanks for the reply. That looks great, but I assume that code would need to be run using journaling, and I can't record the export of a step file in journaling, only using basic macro.
 
Sorry, I read macro in your post and thought journaling.

Have you tried using journaling? I know it can be done in NX6, it is worth a shot in NX5.
 
I've tried journaling, but step file export is not supported (at least, when I record a journal, I doesn't recognize step file exporting. I don't know if there is a way to manually code it).
 
I tried to use your code listed but I'm getting an error that "displayPart" is not declared. Are you importing a specific library or do you define "displayPart" somewhere else?
 
The code listed previously is a snippet from a larger program. When you create a journal (in NX6) you get this much automatically:
Code:
' NX 6.0.3.6
'
Option Strict Off
Imports System
Imports NXOpen

Module NXJournal
Sub Main

Dim theSession As Session = Session.GetSession()
Dim workPart As Part = theSession.Parts.Work

Dim displayPart As Part = theSession.Parts.Display

' ----------------------------------------------
'   Menu: Tools->Journal->Stop Recording
' ----------------------------------------------

End Sub
End Module

The above code was generated simply by starting to record a journal then stopping the record operation. You can see it automatically creates some useful objects for you to use.
 
That's how I create the initial macro, using the Journaling function, but its still throwing the error.
 
Code:
' NX 7.0.1.7
' Journal created by adminuser on Mon Jul 12 14:52:17 2010 Central Daylight Time
'
Option Strict Off
Imports System
Imports NXOpen

Module NXJournal

Function GetFilePath()
    Dim strPath as String
    Dim strPart as String
    Dim pos as Integer
    
    'get the full file path
    strPath = displayPart.fullpath
    'get the part file name
    pos = InStrRev(strPath, "\")
    strPart = Mid(strPath, pos + 1)
    
    strPath = Left(strPath, pos)
    'strip off the ".prt" extension
    strPart = Left(strPart, Len(strPart) - 4)
    
    GetFilePath = strPath
End Function

Function GetFileName()
    Dim strPath as String
    Dim strPart as String
    Dim pos as Integer
    
    'get the full file path
    strPath = displayPart.fullpath
    'get the part file name
    pos = InStrRev(strPath, "\")
    strPart = Mid(strPath, pos + 1)
    
    strPath = Left(strPath, pos)
    'strip off the ".prt" extension
    strPart = Left(strPart, Len(strPart) - 4)
    
    GetFileName = strPart
End Function

Sub Main

For Each fileName As String In My.Computer.FileSystem.GetFiles("C:\Documents and Settings\adminuser\Desktop\CATIA_translated files - Node 2\n2_eclss")

	Dim theSession As Session = Session.GetSession()
' ----------------------------------------------
'   Menu: File->Open...
' ----------------------------------------------
	Dim basePart1 As BasePart
	Dim partLoadStatus1 As PartLoadStatus
	basePart1 = theSession.Parts.OpenBaseDisplay(fileName, partLoadStatus1)

	Dim workPart As Part = theSession.Parts.Work
	Dim displayPart As Part = theSession.Parts.Display

	partLoadStatus1.Dispose()
	Dim markId1 As Session.UndoMarkId
	markId1 = theSession.SetUndoMark(Session.MarkVisibility.Visible, "Enter Gateway")

' ----------------------------------------------
'   Menu: File->Save As...
' ---------------------------------------------
	Dim partSaveStatus1 As PartSaveStatus
	Dim fileSavepath As String = "C:\Documents and Settings\adminuser\Desktop\Node 2 STP\"
	Dim currentFile As String = GetFileName()

	partSaveStatus1 = workPart.SaveAs(fileSavePath & currentFile & ".stp" )
	partSaveStatus1.Dispose()
' ----------------------------------------------
'   Menu: File->Close->All Parts
' ----------------------------------------------
	theSession.Parts.CloseAll(BasePart.CloseModified.CloseModified, Nothing)

	workPart = Nothing
	displayPart = Nothing
' ----------------------------------------------
'   Menu: Tools->Journal->Stop Recording
' ----------------------------------------------

Next
End Sub
End Module
 
Move the declarations out of 'Sub Main' to right under the line: 'Module NXJournal'. This will essentially make the variables global (available to all parts of your code).
Code:
Module NXJournal

Dim theSession As Session = Session.GetSession()
Dim workPart As Part = theSession.Parts.Work
Dim displayPart As Part = theSession.Parts.Display

Sub Main...

Alternately, you could declare displayPart in each of the functions that use it. This would avoid the use of global variables and their potential problems.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor