Save file in a specificated folder

Hi guys,

I work in siemens NX and I need your support to write some script line.
I Need to save the file in the folder DXF and not in the 3D folder.

Example of folder stucture:

C:\dirwork1\3d\
In the 3D Folder i put all NX file

When I save DXF file the script save the DXF in the folder below.
If the folder doesn't exist I need to create it.

C:\dirwork1\DXF

Have you any solution for this script?
Thank for all
I Attached below the original script that i have to modify...

'
' This is intended to export .DXF files two different ways.
'
' First, it will create individual DXF files for each sheet, and it will
' name the exported files after the master model part, with the
' sheet name appended to each one.
'
' Then it will export all of the sheets in the displayed part to one DXF
' file, and name the exported file that contains all of the sheets after
' the master model part, with "_All_Sheets.dxf" appended to the name.
'
' It also deletes the .log files.
'
'
Option Strict Off
Imports System
Imports System.IO
Imports System.Windows.Forms
'Imports System.Windows.Forms.Application 'doevents
Imports System.Collections.Generic
Imports System.Threading
Imports System.Diagnostics
Imports System.Reflection

Imports NXOpen
Imports NXOpen.Drawings
Imports NXOpen.UF
Imports NXOpen.Utilities
Imports NXOpen.Assemblies

Module export_drawings_to_dxf

Const blnExportSingleDXF As Boolean = True
Const blnExportMergeDXF As Boolean = False
Const blnDeleteLogFiles As Boolean = True
Const blnShowListingWindow As Boolean = False
Const strDefaultOutputFolder As String = "D:\TEMP\" 'initial start folder for browser. Journal will read registery for last location

'==============DO NOT EDIT BELOW THIS LINE=================
Dim lstLogFilesToDelete As New List(Of String)

Dim theSession As NXOpen.Session = NXOpen.Session.GetSession()
Dim theUFSession As UFSession = UFSession.GetUFSession()
Dim lw As ListingWindow = theSession.ListingWindow

Sub Main(ByVal args() As String)

Dim theSession As NXOpen.Session = NXOpen.Session.GetSession()
Dim displayPart As NXOpen.Part = theSession.Parts.Display
Dim outputFileName As String = ""
Dim outputFilePath As String = ""
Dim outputLogFile As String = ""
Dim defName As String = Environment.GetEnvironmentVariable("UGII_BASE_DIR") & "\DXFDWG\dxfdwg.def"

PrintJournalStart()

Echo("Display Part: " & displayPart.FullPath(), 0, False)

outputFilePath = AskUserOutputPath()
If outputFilePath Is Nothing Then
Echo("No output path selected. EXIT!", 0, True)
Return
End If

'======================================================================
' Export each sheets to 1 DXF
If blnExportSingleDXF = True Then
Echo("Exporting Single DXF for each sheet...", 0, False)

Dim allSheets() As DrawingSheet = displayPart.DrawingSheets.ToArray()
For Each thisSheet As DrawingSheet In allSheets

Dim dxfCrtr_1 As NXOpen.DxfdwgCreator = theSession.DexManager.CreateDxfdwgCreator()
dxfCrtr_1.ExportData = NXOpen.DxfdwgCreator.ExportDataOption.Drawing
dxfCrtr_1.AutoCADRevision = NXOpen.DxfdwgCreator.AutoCADRevisionOptions.R2004
dxfCrtr_1.ViewEditMode = True
dxfCrtr_1.FlattenAssembly = True

dxfCrtr_1.SettingsFile = defName
'dxfCrtr_1.OutputTo = NXOpen.DxfdwgCreator.OutputToOption.Drafting
dxfCrtr_1.ObjectTypes.Curves = True
dxfCrtr_1.ObjectTypes.Annotations = True
dxfCrtr_1.ObjectTypes.Structures = True
dxfCrtr_1.FlattenAssembly = False
dxfCrtr_1.InputFile = displayPart.FullPath()
dxfCrtr_1.WidthFactorMode = NXOpen.DxfdwgCreator.WidthfactorMethodOptions.AutomaticCalculation
dxfCrtr_1.LayerMask = "1-256"

'set file name
Dim sheetName As String = thisSheet.Name()
Dim suffix As String = sheetName & ".dxf"
outputFileName = Path.Combine(outputFilePath, displayPart.Leaf & "_" & suffix)
outputLogFile = outputFileName.Replace(".dxf", ".log")
lstLogFilesToDelete.Add(outputLogFile)

dxfCrtr_1.OutputFile = outputFileName
Echo("Creating Output File: " & outputFileName, 1, False)
DeleteFile(outputFileName)

'Echo("The drawing part is: " & displayPart.FullPath())
'Echo("The Master Model Part is: " & masterModelName)
'Echo("The output filename is: " & outputFileName)

dxfCrtr_1.DrawingList = sheetName

dxfCrtr_1.ProcessHoldFlag = True

Dim nXObject1 As NXOpen.NXObject
nXObject1 = dxfCrtr_1.Commit()

dxfCrtr_1.Destroy()

Next
End If

'Echo("DONE: single DXF/sheet" & Now.ToString("dd/MM/yyyy") & " - " & Now.ToString("HH:mm:ss"), 0, False)

'======================================================================
' Merge all sheets to 1 DXF
If blnExportMergeDXF = True Then

Echo("Exporting Merged DXF with all sheets...", 0, False)

Dim dxfCrtr_1 As NXOpen.DxfdwgCreator = theSession.DexManager.CreateDxfdwgCreator()

dxfCrtr_1.ExportData = NXOpen.DxfdwgCreator.ExportDataOption.Drawing
dxfCrtr_1.AutoCADRevision = NXOpen.DxfdwgCreator.AutoCADRevisionOptions.R2004
dxfCrtr_1.ViewEditMode = True
dxfCrtr_1.FlattenAssembly = True

dxfCrtr_1.SettingsFile = defName
'dxfCrtr_1.OutputTo = NXOpen.DxfdwgCreator.OutputToOption.Drafting
dxfCrtr_1.ObjectTypes.Curves = True
dxfCrtr_1.ObjectTypes.Annotations = True
dxfCrtr_1.ObjectTypes.Structures = True
dxfCrtr_1.FlattenAssembly = False
dxfCrtr_1.InputFile = displayPart.FullPath()
dxfCrtr_1.WidthFactorMode = NXOpen.DxfdwgCreator.WidthfactorMethodOptions.AutomaticCalculation
dxfCrtr_1.LayerMask = "1-256"
dxfCrtr_1.DrawingList = "_ALL_"

outputFileName = Path.Combine(outputFilePath, displayPart.Leaf & "_All_Sheets.dxf")
outputLogFile = outputFileName.Replace(".dxf", ".log")
lstLogFilesToDelete.Add(outputLogFile)

dxfCrtr_1.OutputFile = outputFileName
Echo("Creating Output File: " & outputFileName, 1, False)
DeleteFile(outputFileName)

Dim nXObject2 As NXOpen.NXObject
nXObject2 = dxfCrtr_1.Commit()
dxfCrtr_1.Destroy()

End If

'Echo("DONE: Merge DXF all sheets" & Now.ToString("dd/MM/yyyy") & " - " & Now.ToString("HH:mm:ss"), 0, False)

'=============DELETE LOG-FILES=====================================
'detect when DXF translations are done
If blnDeleteLogFiles Then
Echo("", 0, False)
Echo("Deleting translation LOG-file(s)...", 0, False)
For ii As Integer = 0 To 100 'max wait time in seconds
If CheckIfRunning("ugto2d") = False And CheckIfRunning("dxfdwg") = False Then
Echo("Translations finished, process not detected", 1, False)
Exit For
Else
Echo("Translation running...", 1, False)
System.Threading.Thread.Sleep(1000) ' Pause for xx seconds...
End If
Next

'Delete LOG files
For Each strLogFile As String In lstLogFilesToDelete
DeleteFile(strLogFile)
Next
End If
'Echo("DONE: deleting log-files" & Now.ToString("dd/MM/yyyy") & " - " & Now.ToString("HH:mm:ss"), 0, False)

PrintJournalEnd()

End Sub

Sub DeleteFile(outputFileName As String)
' If that file exists, delete it.
Try
If My.Computer.FileSystem.FileExists(outputFileName).Equals(True) Then
Echo("Deleting: " & outputFileName, 0, False)
My.Computer.FileSystem.DeleteFile(outputFileName)
End If
Catch ex As Exception
Echo("Exception: " & ex.Message(), 0, True)
End Try

End Sub

'*************************************************************************************************************************************

Function AskUserOutputPath() As String

Dim strLastPath As String = ""
Dim strOutputPath As String = ""

'Key will show up in HKEY_CURRENT_USER\Software\VB and VBA Program Settings
Try
'Get the last path used from the registry
strLastPath = GetSetting("NX journal", "Export DXF", "ExportPath")
'msgbox("Last Path: " & strLastPath)
Catch e As ArgumentException
Catch e As Exception
MsgBox(e.GetType.ToString)
Finally
End Try

Dim FolderBrowserDialog1 As New FolderBrowserDialog

' Then use the following code to create the Dialog window
' Change the .SelectedPath property to the default location
With FolderBrowserDialog1
' Desktop is the root folder in the dialog.
.RootFolder = Environment.SpecialFolder.Desktop
' Select the strDefaultOutputFolder directory on entry.
If Directory.Exists(strLastPath) Then
.SelectedPath = strLastPath
Else
.SelectedPath = strDefaultOutputFolder
End If
' Prompt the user with a custom message.
.Description = "Select the directory to export .pdf file"
If .ShowDialog = DialogResult.OK Then
' Display the selected folder if the user clicked on the OK button.
AskUserOutputPath = .SelectedPath
' save the output folder path in the registry for use on next run
SaveSetting("NX journal", "Export DXF", "ExportPath", .SelectedPath)
Else
'user pressed 'cancel', exit the subroutine
AskUserOutputPath = Nothing
'exit sub
End If
End With

End Function

'*************************************************************************************************************************************

Function CheckIfRunning(strProcessName As String) As Boolean

Dim p() As Process
p = Process.GetProcessesByName(strProcessName) 'automatically adds (".exe")
If p.Length > 0 Then
' Process is running
Return True
Else
' Process is not running
Return False
End If
End Function

'Sub Echo(ByVal output As String)

' theSession.ListingWindow.Open()
' theSession.ListingWindow.WriteLine(output)
' theSession.LogFile.WriteLine(output)

'End Sub
Sub Echo(ByVal output As String, indent As Integer, blnError As Boolean)
Dim strSpace As String = Space(indent * 4)

'lw.WriteLine only takes 264char max (= 2 Lines), without any error the string is trimmed
Dim strTrimWarning As String = strSpace & "!!! Message below probably truncated (max lenght of lw.writeline = 264) !!!"

If blnShowListingWindow = True Or blnError = True Then
lw.Open()
If output.Length > 263 Then lw.WriteLine(strTrimWarning)
lw.WriteLine(strSpace & output)
End If

'always write everything to LogFile
If output.Length > 263 Then theSession.LogFile.WriteLine(strTrimWarning)
theSession.LogFile.WriteLine(strSpace & output)

End Sub

' This may be usefule to print custom debug messages
Sub PrintJournalStart()

Echo("", 0, False)
Echo("*********** START ***************************************************", 0, False)
Echo(Now.ToString("dd/MM/yyyy") & " - " & Now.ToString("HH:mm:ss"), 0, False)
Echo("Executing Journal: " & vbCrLf & theSession.ExecutingJournal, 0, False)
Echo("", 0, False)

' Name of journal file executed in journal editor

'Dim st As New StackTrace(New StackFrame(True))
'' Name, line and column of source code in compiled DLL
'Echo("Source: " & st.GetFrame(0).GetFileName())
'Echo("Line: " & st.GetFrame(0).GetFileLineNumber())
'Echo("Column: " & st.GetFrame(0).GetFileColumnNumber())

'' Name of executing assembly
'Echo("GetExecutingAssembly: " + [Assembly].GetExecutingAssembly().GetName().FullName)
'Echo("Loaded file: " + [Assembly].GetExecutingAssembly().Location)

End Sub

Sub PrintJournalEnd()

Echo("", 0, False)
Echo(Now.ToString("dd/MM/yyyy") & " - " & Now.ToString("HH:mm:ss"), 0, False)

' Name of journal file executed in journal editor
Echo("Done Executing Journal: " & vbCrLf & theSession.ExecutingJournal, 0, False)
Echo("============ END =====================================================", 0, False)
Echo("", 0, False)

End Sub

Public Function GetUnloadOption(ByVal dummy As String) As Integer
Return Session.LibraryUnloadOption.Immediately
End Function

End Module

The code below shows one way to get what you want. When run, the code will examine the path information of the current work part, get the parent directory, create a new directory named "DXF" (if it doesn't already exist), and create an export file name based on the DXF path and current file name.

Option Strict Off

Imports System
Imports NXOpen

Module Module155

Public theSession As Session = Session.GetSession()
Public lw As ListingWindow = theSession.ListingWindow

Sub Main()

lw.Open()

Dim currentFilePath As String = theSession.Parts.Work.FullPath
lw.WriteLine("currentFilePath: " & currentFilePath)
Dim currentFileName As String = theSession.Parts.Work.Leaf
lw.WriteLine("currentFileName: " & currentFileName)

Dim parentDir As String
parentDir = System.IO.Directory.GetParent(currentFilePath).FullName
lw.WriteLine("parentDir: " & Parentdir)

Dim exportPath As String
exportPath = IO.Path.Combine(IO.Directory.GetParent(parentDir).FullName, "DXF")
lw.WriteLine("exportPath: " & exportPath)

If (Not System.IO.Directory.Exists(exportPath)) Then
System.IO.Directory.CreateDirectory(exportPath)
End If

Dim exportFileName As String = IO.Path.Combine(exportPath, currentFileName & ".dxf")
lw.WriteLine("exportFileName: " & exportFileName)

lw.Close()

End Sub

Public Function GetUnloadOption(ByVal dummy As String) As Integer
Return Session.LibraryUnloadOption.Immediately
End Function

End Module