Submitted by rbarnthouse on Thu, 06/23/2016 - 08:47
Forums:
I'm creating a journal that will output 3 files and I want those output files to be named the same as the file that is active when running the journal. I'm looking for a code snippet that will do this. I believe this would a UF function but I've tried a couple of things that haven't given me what I need. Any help with this is greatly appreciated.
Thank you,
Ron
re: file name
If you are using a .net language, you can access information about the file name and path through the .net framework. The code below is in VB.net.
Option Strict Off
Imports System
Imports NXOpen
Module Module2
Sub Main()
Dim theSession As Session = Session.GetSession()
Dim workPart As Part = theSession.Parts.Work
Dim lw As ListingWindow = theSession.ListingWindow
lw.Open()
'get information about current work part
Dim fileName As String = IO.Path.GetFileName(workPart.FullPath)
Dim fileNameNoExt As String = IO.Path.GetFileNameWithoutExtension(workPart.FullPath)
Dim parentFolder As String = IO.Path.GetDirectoryName(workPart.FullPath)
Dim root As String = IO.Path.GetPathRoot(workPart.FullPath)
lw.WriteLine("full path: " & workPart.FullPath)
lw.WriteLine(New String("-", ("full path: " & workPart.FullPath).Length))
lw.WriteLine("file name: " & fileName)
lw.WriteLine("file name w/o extension: " & fileNameNoExt)
lw.WriteLine("parent folder: " & parentFolder)
lw.WriteLine("root folder: " & root)
lw.WriteLine("")
lw.Close()
End Sub
End Module
File Name
Dim fileNameNoExt As String = IO.Path.GetFileNameWithoutExtension(workPart.FullPath)
That was exactly what I was looking for!
Thank you!
How to Extract Last 2 letters from file name
Thanks for the code. I have extracted file name. E.g. in my case file name is DMC00050_db. Now I want to extract last 2 letters from this file name means db. Please help with the code.
re: last two letters
You can easily get the last two letters in a string with a combination of the .Substring and .Length string methods.
Option Strict Off
Imports System
Imports NXOpen
Module Module2
Sub Main()
Dim theSession As Session = Session.GetSession()
If IsNothing(theSession.Parts.BaseWork) Then
'active part required
Return
End If
Dim workPart As Part = theSession.Parts.Work
Dim lw As ListingWindow = theSession.ListingWindow
lw.Open()
Dim targetString As String = "DMC00050_db"
Dim lastTwo As String = targetString.Substring(targetString.Length - 2)
lw.WriteLine("initial string: " & targetString)
lw.WriteLine("last two characters: " & lastTwo)
lw.Close()
End Sub
Public Function GetUnloadOption(ByVal dummy As String) As Integer
'Unloads the image immediately after execution within NX
GetUnloadOption = NXOpen.Session.LibraryUnloadOption.Immediately
End Function
End Module