×
INTELLIGENT WORK FORUMS
FOR ENGINEERING PROFESSIONALS

Log In

Come Join Us!

Are you an
Engineering professional?
Join Eng-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!
  • Students Click Here

*Eng-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Jobs

Using Current Path in a Macro

Using Current Path in a Macro

Using Current Path in a Macro

(OP)
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

RE: Using Current Path in a Macro

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()
 

RE: Using Current Path in a Macro

(OP)
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.

RE: Using Current Path in a 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.

RE: Using Current Path in a Macro

(OP)
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).

RE: Using Current Path in a Macro

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?  

RE: Using Current Path in a Macro

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.

RE: Using Current Path in a Macro

That's how I create the initial macro, using the Journaling function, but its still throwing the error.

RE: Using Current Path in a Macro

What version of NX?

RE: Using Current Path in a Macro

NX 7.0

RE: Using Current Path in a Macro

Are you willing/able to post your code?

RE: Using Current Path in a Macro

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

RE: Using Current Path in a Macro

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.

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Eng-Tips Forums free from inappropriate posts.
The Eng-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Eng-Tips forums is a member-only feature.

Click Here to join Eng-Tips and talk with other members!


Resources