NX9 JOURNAL
NX9 JOURNAL
(OP)
Can someone help me get this journal to work in NX9.
It works fine with NX8, but they've changed some code. see image for error code.
The routine exports a dxf with 2d exchange then adds a rev letter.(from attributes)
I did manage to get rid of some of the errors, by commenting out those lines, I've even copied the NXopen, and NXTo2d folders into NX9 but that didn't fix it either, there were some parts in NXopen which looked incomplete compared to the NX8 version.
any help appreciated......
'NXJournaling.com
'November 11, 2014
'
'http://www.eng-tips.com/viewthread.cfm?qid=374170
'Export DXF files using 2D exchange.
'NX 8
'due to API changes, will NOT work in 8.5 or higher
'update November 18, 2014
' converted code to class
' added folder picker
Option Strict Off
Imports System
Imports System.Collections.Generic
Imports System.Windows.Forms
Imports NXOpen
Imports NXOpen.UF
Module Module2
Dim theSession As Session = Session.GetSession()
Sub Main()
If IsNothing(theSession.Parts.Work) Then
'active part required
Return
End If
Dim workPart As Part = theSession.Parts.Work
Dim lw As ListingWindow = theSession.ListingWindow
lw.Open()
Const undoMarkName As String = "NXJ journal"
Dim markId1 As Session.UndoMarkId
markId1 = theSession.SetUndoMark(Session.MarkVisibility.Visible, undoMarkName)
Dim myDxfExporter As New NXJExportDXF2DExchange(workPart)
myDxfExporter.PickExportFolder()
myDxfExporter.RevisionAttributeTitle = "REV"
myDxfExporter.Commit()
lw.Close()
End Sub
Public Function GetUnloadOption(ByVal dummy As String) As Integer
'Unloads the image when the NX session terminates
GetUnloadOption = NXOpen.Session.LibraryUnloadOption.AtTermination
'----Other unload options-------
'Unloads the image immediately after execution within NX
'GetUnloadOption = NXOpen.Session.LibraryUnloadOption.Immediately
'Unloads the image explicitly, via an unload dialog
'GetUnloadOption = NXOpen.Session.LibraryUnloadOption.Explicitly
'-------------------------------
End Function
End Module
Class NXJExportDXF2DExchange
#Region "Properties"
Private _theSession As Session = Session.GetSession
Private _theUfSession As UFSession = UFSession.GetUFSession
Private lg As LogFile = _theSession.LogFile
Private _drawingSheets As New List(Of Drawings.DrawingSheet)
Private _exportFile As String = ""
Private _partUnits As Integer
Private _outputFolder As String = ""
Public Property OutputFolder() As String
Get
Return _outputFolder
End Get
Set(ByVal value As String)
lg.WriteLine("Set Property OutputFolder")
If Not IO.Directory.Exists(value) Then
Try
lg.WriteLine(" specified directory does not exist, trying to create it...")
IO.Directory.CreateDirectory(value)
lg.WriteLine(" directory created: " & value)
Catch ex As Exception
lg.WriteLine(" ** error while creating directory: " & value)
lg.WriteLine(" " & ex.GetType.ToString & " : " & ex.Message)
lg.WriteLine(" defaulting to: " & My.Computer.FileSystem.SpecialDirectories.MyDocuments)
value = My.Computer.FileSystem.SpecialDirectories.MyDocuments
End Try
End If
_outputFolder = value
_outputDxfFile = IO.Path.Combine(_outputFolder, _exportFile & ".dxf")
lg.WriteLine(" outputFolder: " & _outputFolder)
lg.WriteLine(" outputDxfFile: " & _outputDxfFile)
lg.WriteLine("exiting Set Property OutputFolder")
lg.WriteLine("")
End Set
End Property
Private _outputDxfFile As String = ""
Public Property OutputDxfFileName() As String
Get
Return _outputDxfFile
End Get
Set(ByVal value As String)
lg.WriteLine("Set Property OutputDxfFileName")
_outputDxfFile = value
lg.WriteLine(" outputDxfFile: " & value)
'update _outputFolder
Me.OutputFolder = Me.GetParentPath(_outputDxfFile)
lg.WriteLine("exiting Set Property OutputDxfFileName")
lg.WriteLine("")
End Set
End Property
Private _overwriteDxf As Boolean = True
Public Property OverwriteDxf() As Boolean
Get
Return _overwriteDxf
End Get
Set(ByVal value As Boolean)
lg.WriteLine("Set Property OverwriteDxf")
_overwriteDxf = value
lg.WriteLine(" overwriteDxf: " & _overwriteDxf.ToString)
lg.WriteLine("exiting Set Property OverwriteDxf")
lg.WriteLine("")
End Set
End Property
Private _thePart As Part = Nothing
Public Property Part() As Part
Get
Return _thePart
End Get
Set(ByVal value As Part)
lg.WriteLine("Set Property Part")
_thePart = value
_partUnits = _thePart.PartUnits
Me.GetPartInfo()
Me.GetDrawingSheets()
lg.WriteLine("exiting Set Property Part")
lg.WriteLine("")
End Set
End Property
Private _partNumber As String
Public ReadOnly Property PartNumber() As String
Get
Return _partNumber
End Get
End Property
Private _partRevision As String = ""
Public ReadOnly Property PartRevision() As String
Get
Return _partRevision
End Get
End Property
Private _isTCRunning As Boolean
Public ReadOnly Property IsTCRunning() As Boolean
Get
Return _isTCRunning
End Get
End Property
Private _partFilePath As String
Public ReadOnly Property PartFilePath() As String
Get
Return _partFilePath
End Get
End Property
Private _revisionAttributeTitle As String = "REVISION"
Public Property RevisionAttributeTitle() As String
Get
Return _revisionAttributeTitle
End Get
Set(ByVal value As String)
lg.WriteLine("Set Property: RevisionAttributeTitle")
_revisionAttributeTitle = value
Me.GetPartInfo()
lg.WriteLine("exiting Set Property: RevisionAttributeTitle")
lg.WriteLine("")
End Set
End Property
#End Region
#Region "Public Methods"
Public Sub New()
Me.StartLog()
End Sub
Public Sub New(ByVal thePart As NXOpen.Part)
Me.StartLog()
Me.Part = thePart
End Sub
Public Sub PickExportFolder()
'Requires:
' Imports System.Windows.Forms
'if the user presses OK on the dialog box, the chosen path is returned
'if the user presses cancel on the dialog box, 0 is returned
lg.WriteLine("Sub PickExportFolder")
Dim strLastPath 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
lg.WriteLine(" attempting to retrieve last export path from registry...")
strLastPath = GetSetting("NX journal", "Export dxf", "ExportPath")
'msgbox("Last Path: " & strLastPath)
Catch e As ArgumentException
lg.WriteLine(" ** Argument Exception: " & e.Message)
Catch e As Exception
lg.WriteLine(" ** Exception type: " & e.GetType.ToString)
lg.WriteLine(" ** Exception message: " & e.Message)
'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 D:\home directory on entry.
If IO.Directory.Exists(strLastPath) Then
.SelectedPath = strLastPath
Else
.SelectedPath = My.Computer.FileSystem.SpecialDirectories.MyDocuments
End If
' Prompt the user with a custom message.
.Description = "Select the directory to export .dxf file(s)"
If .ShowDialog = DialogResult.OK Then
' Display the selected folder if the user clicked on the OK button.
Me.OutputFolder = .SelectedPath
lg.WriteLine(" selected output path: " & .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', keep original value of output folder
lg.WriteLine(" folder browser dialog cancel button pressed")
lg.WriteLine(" current output path: " & Me.OutputFolder)
End If
End With
lg.WriteLine("exiting Sub PickExportFolder")
lg.WriteLine("")
End Sub
Public Sub Commit()
lg.WriteLine("Sub Commit")
lg.WriteLine(" number of drawing sheets in part file: " & _drawingSheets.Count.ToString)
If _drawingSheets.Count = 0 Then
lg.WriteLine(" no drawing sheets to export")
Return
End If
Dim numPlists As Integer = 0
Dim myPlists() As Tag
_theUfSession.Plist.AskTags(myPlists, numPlists)
lg.WriteLine(" " & numPlists.ToString & " parts list(s) found, updating...")
For i As Integer = 0 To numPlists - 1
_theUfSession.Plist.Update(myPlists(i))
Next
'update views
Dim sheetCount As Integer = 0
For Each tempSheet As Drawings.DrawingSheet In _drawingSheets
sheetCount += 1
lg.WriteLine(" working on sheet: " & tempSheet.Name)
lg.WriteLine(" sheetCount: " & sheetCount.ToString)
'update any views that are out of date
lg.WriteLine(" updating OutOfDate views on sheet: " & tempSheet.Name)
Me.Part.DraftingViews.UpdateViews(Drawings.DraftingViewCollection.ViewUpdateOption.OutOfDate, tempSheet)
Next
'ready for export
For Each tempDwg As Drawings.DrawingSheet In _drawingSheets
_outputDxfFile = IO.Path.Combine(Me.OutputFolder, _exportFile & "_" & tempDwg.Name & ".dxf")
If Me.DeleteExistingDxfFile(_outputDxfFile) Then
'OK to output
Me.ExportDxf2DExchange(tempDwg, _outputDxfFile)
Else
'user chose not to overwrite or file cannot be deleted
If Me.OverwriteDxf Then
MessageBox.Show("The file: " & _outputDxfFile & " could not be overwritten, skipping file", "Error writing file: " & _outputDxfFile, MessageBoxButtons.OK, MessageBoxIcon.Error)
Else
MessageBox.Show("The file: " & _outputDxfFile & " exists and will not be overwritten, skipping file", "File exists: " & _outputDxfFile, MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
End If
End If
Next
End Sub
#End Region
#Region "Private Methods"
Private Sub GetPartInfo()
lg.WriteLine("Sub GetPartInfo")
If Me.IsTCRunning Then
_partNumber = _thePart.GetStringAttribute("DB_PART_NO")
_partRevision = _thePart.GetStringAttribute("DB_PART_REV")
lg.WriteLine(" TC running")
lg.WriteLine(" partNumber: " & _partNumber)
lg.WriteLine(" partRevision: " & _partRevision)
Else 'running in native mode
_partNumber = IO.Path.GetFileNameWithoutExtension(_thePart.FullPath)
_partFilePath = IO.Directory.GetParent(_thePart.FullPath).ToString
lg.WriteLine(" Native NX")
lg.WriteLine(" partNumber: " & _partNumber)
lg.WriteLine(" partFilePath: " & _partFilePath)
Try
_partRevision = _thePart.GetStringAttribute(Me.RevisionAttributeTitle)
_partRevision = _partRevision.Trim
Catch ex As Exception
_partRevision = ""
End Try
lg.WriteLine(" partRevision: " & _partRevision)
End If
If String.IsNullOrEmpty(_partRevision) Then
_exportFile = _partNumber
Else
_exportFile = _partNumber & "_" & _partRevision
End If
lg.WriteLine("")
Me.GetOutputName()
lg.WriteLine(" exportFile: " & _exportFile)
lg.WriteLine(" outputDxfFile: " & _outputDxfFile)
lg.WriteLine(" exiting Sub GetPartInfo")
lg.WriteLine("")
End Sub
Private Sub GetOutputName()
lg.WriteLine("Sub GetOutputName")
If IsNothing(Me.Part) Then
lg.WriteLine(" Me.Part is Nothing")
lg.WriteLine(" exiting Sub GetOutputName")
lg.WriteLine("")
Return
End If
If Me.IsTCRunning Then
lg.WriteLine(" TC is running")
'exporting from TC to filesystem, no part folder to default to
'default to "MyDocuments" folder
If Me.OutputFolder = "" Then
_outputDxfFile = IO.Path.Combine(My.Computer.FileSystem.SpecialDirectories.MyDocuments, _exportFile & ".dxf")
Else
'if output folder has been specified, use it
_outputDxfFile = IO.Path.Combine(Me.OutputFolder, _exportFile & ".dxf")
End If
Else
lg.WriteLine(" native NX")
'exporting from native to file system
'use part folder as default output folder
If _outputFolder = "" Then
_outputFolder = _partFilePath
End If
_outputDxfFile = IO.Path.Combine(_outputFolder, _exportFile & ".dxf")
End If
lg.WriteLine(" exiting Sub GetOutputName")
lg.WriteLine("")
End Sub
Private Sub GetDrawingSheets()
_drawingSheets.Clear()
For Each tempSheet As Drawings.DrawingSheet In _thePart.DrawingSheets
_drawingSheets.Add(tempSheet)
Next
End Sub
Private Sub StartLog()
lg.WriteLine("")
lg.WriteLine("~ NXJournaling.com: Start of drawing to DXF journal ~")
lg.WriteLine(" ~~ Timestamp of run: " & DateTime.Now.ToString & " ~~")
lg.WriteLine("DXF Exporter Sub StartLog()")
'determine if we are running under TC or native
_theUfSession.UF.IsUgmanagerActive(_isTCRunning)
lg.WriteLine("IsTcRunning: " & _isTCRunning.ToString)
lg.WriteLine("exiting Sub StartLog")
lg.WriteLine("")
End Sub
Private Function DeleteExistingDxfFile(ByVal theDxfFile As String) As Boolean
lg.WriteLine("Function DeleteExistingDxfFile(" & theDxfFile & ")")
If IO.File.Exists(theDxfFile) Then
lg.WriteLine(" specified DXF file already exists")
If Me.OverwriteDxf Then
Try
lg.WriteLine(" user chose to overwrite existing DXF file")
IO.File.Delete(theDxfFile)
lg.WriteLine(" file deleted")
lg.WriteLine(" returning: True")
lg.WriteLine(" exiting Function DeleteExistingDxfFile")
lg.WriteLine("")
Return True
Catch ex As Exception
'rethrow error?
lg.WriteLine(" ** error while attempting to delete existing dxf file")
lg.WriteLine(" " & ex.GetType.ToString & " : " & ex.Message)
lg.WriteLine(" returning: False")
lg.WriteLine(" exiting Function DeleteExistingDxfFile")
lg.WriteLine("")
Return False
End Try
Else
'file exists, overwrite option is set to false - do nothing
lg.WriteLine(" specified dxf file exists, user chose not to overwrite")
lg.WriteLine(" returning: False")
lg.WriteLine(" exiting Function DeleteExistingDxfFile")
lg.WriteLine("")
Return False
End If
Else
'file does not exist
Return True
End If
End Function
Private Function GetParentPath(ByVal thePath As String) As String
lg.WriteLine("Function GetParentPath(" & thePath & ")")
Try
Dim directoryInfo As System.IO.DirectoryInfo
directoryInfo = System.IO.Directory.GetParent(thePath)
lg.WriteLine(" returning: " & directoryInfo.FullName)
lg.WriteLine("exiting Function GetParentPath")
lg.WriteLine("")
Return directoryInfo.FullName
Catch ex As ArgumentNullException
lg.WriteLine(" Path is a null reference.")
Throw ex
Catch ex As ArgumentException
lg.WriteLine(" Path is an empty string, contains only white space, or contains invalid characters")
Throw ex
End Try
lg.WriteLine("exiting Function GetParentPath")
lg.WriteLine("")
End Function
Private Sub ExportDxf2DExchange(ByVal inputSheet As Drawings.DrawingSheet, ByVal outputFileName As String)
'If Not IO.Directory.Exists(outputFolder) Then
' MsgBox("Output folder, " & outputFolder & ", not found; journal exiting.", MsgBoxStyle.Critical, "Output folder error")
' Return
'End If
Dim dxfSettingsDir As String = _theSession.GetEnvironmentVariableValue("DXFDWG_DIR")
Dim ugTo2dDir As String = _theSession.GetEnvironmentVariableValue("UGTO2D_DIR")
If String.IsNullOrEmpty(dxfSettingsDir) Then
MsgBox("DXF settings directory not found; journal exiting.", MsgBoxStyle.Critical, "DXF settings error")
Return
End If
If String.IsNullOrEmpty(ugTo2dDir) Then
MsgBox("UGto2D settings directory not found; journal exiting.", MsgBoxStyle.Critical, "UGto2D settings error")
Return
End If
Dim nXTo2dCreator1 As NXTo2dCreator
nXTo2dCreator1 = _theSession.DexManager.CreateNxto2dCreator()
'Dim outputFileName As String = IO.Path.Combine(outputFolder, _exportFile & "_" & inputSheet.Name & ".dxf")
With nXTo2dCreator1
.Nxto2dSettingsFile = IO.Path.Combine(ugTo2dDir, "ugto2d.def")
.DxfSettingsFile = IO.Path.Combine(dxfSettingsDir, "dxfdwg.def")
.InputFile = Me.Part.FullPath
.OutputFile = outputFileName
.DrawingName = inputSheet.Name
.MaxSystem3DModelSpace = True
.MaxSystemPointRes = True
.FacetBodies = True
.AutoCADRevision = NXTo2dCreator.AutoCADRevisionOption.R14
.ExportData = NXTo2dCreator.ExportDataOption.Drawing
.OutputFileType = NXTo2dCreator.OutputAsOption.DXFFile
End With
Dim nXObject1 As NXObject
nXObject1 = nXTo2dCreator1.Commit()
nXTo2dCreator1.Destroy()
End Sub
#End Region
End Class
It works fine with NX8, but they've changed some code. see image for error code.
The routine exports a dxf with 2d exchange then adds a rev letter.(from attributes)
I did manage to get rid of some of the errors, by commenting out those lines, I've even copied the NXopen, and NXTo2d folders into NX9 but that didn't fix it either, there were some parts in NXopen which looked incomplete compared to the NX8 version.
any help appreciated......

'NXJournaling.com
'November 11, 2014
'
'http://www.eng-tips.com/viewthread.cfm?qid=374170
'Export DXF files using 2D exchange.
'NX 8
'due to API changes, will NOT work in 8.5 or higher
'update November 18, 2014
' converted code to class
' added folder picker
Option Strict Off
Imports System
Imports System.Collections.Generic
Imports System.Windows.Forms
Imports NXOpen
Imports NXOpen.UF
Module Module2
Dim theSession As Session = Session.GetSession()
Sub Main()
If IsNothing(theSession.Parts.Work) Then
'active part required
Return
End If
Dim workPart As Part = theSession.Parts.Work
Dim lw As ListingWindow = theSession.ListingWindow
lw.Open()
Const undoMarkName As String = "NXJ journal"
Dim markId1 As Session.UndoMarkId
markId1 = theSession.SetUndoMark(Session.MarkVisibility.Visible, undoMarkName)
Dim myDxfExporter As New NXJExportDXF2DExchange(workPart)
myDxfExporter.PickExportFolder()
myDxfExporter.RevisionAttributeTitle = "REV"
myDxfExporter.Commit()
lw.Close()
End Sub
Public Function GetUnloadOption(ByVal dummy As String) As Integer
'Unloads the image when the NX session terminates
GetUnloadOption = NXOpen.Session.LibraryUnloadOption.AtTermination
'----Other unload options-------
'Unloads the image immediately after execution within NX
'GetUnloadOption = NXOpen.Session.LibraryUnloadOption.Immediately
'Unloads the image explicitly, via an unload dialog
'GetUnloadOption = NXOpen.Session.LibraryUnloadOption.Explicitly
'-------------------------------
End Function
End Module
Class NXJExportDXF2DExchange
#Region "Properties"
Private _theSession As Session = Session.GetSession
Private _theUfSession As UFSession = UFSession.GetUFSession
Private lg As LogFile = _theSession.LogFile
Private _drawingSheets As New List(Of Drawings.DrawingSheet)
Private _exportFile As String = ""
Private _partUnits As Integer
Private _outputFolder As String = ""
Public Property OutputFolder() As String
Get
Return _outputFolder
End Get
Set(ByVal value As String)
lg.WriteLine("Set Property OutputFolder")
If Not IO.Directory.Exists(value) Then
Try
lg.WriteLine(" specified directory does not exist, trying to create it...")
IO.Directory.CreateDirectory(value)
lg.WriteLine(" directory created: " & value)
Catch ex As Exception
lg.WriteLine(" ** error while creating directory: " & value)
lg.WriteLine(" " & ex.GetType.ToString & " : " & ex.Message)
lg.WriteLine(" defaulting to: " & My.Computer.FileSystem.SpecialDirectories.MyDocuments)
value = My.Computer.FileSystem.SpecialDirectories.MyDocuments
End Try
End If
_outputFolder = value
_outputDxfFile = IO.Path.Combine(_outputFolder, _exportFile & ".dxf")
lg.WriteLine(" outputFolder: " & _outputFolder)
lg.WriteLine(" outputDxfFile: " & _outputDxfFile)
lg.WriteLine("exiting Set Property OutputFolder")
lg.WriteLine("")
End Set
End Property
Private _outputDxfFile As String = ""
Public Property OutputDxfFileName() As String
Get
Return _outputDxfFile
End Get
Set(ByVal value As String)
lg.WriteLine("Set Property OutputDxfFileName")
_outputDxfFile = value
lg.WriteLine(" outputDxfFile: " & value)
'update _outputFolder
Me.OutputFolder = Me.GetParentPath(_outputDxfFile)
lg.WriteLine("exiting Set Property OutputDxfFileName")
lg.WriteLine("")
End Set
End Property
Private _overwriteDxf As Boolean = True
Public Property OverwriteDxf() As Boolean
Get
Return _overwriteDxf
End Get
Set(ByVal value As Boolean)
lg.WriteLine("Set Property OverwriteDxf")
_overwriteDxf = value
lg.WriteLine(" overwriteDxf: " & _overwriteDxf.ToString)
lg.WriteLine("exiting Set Property OverwriteDxf")
lg.WriteLine("")
End Set
End Property
Private _thePart As Part = Nothing
Public Property Part() As Part
Get
Return _thePart
End Get
Set(ByVal value As Part)
lg.WriteLine("Set Property Part")
_thePart = value
_partUnits = _thePart.PartUnits
Me.GetPartInfo()
Me.GetDrawingSheets()
lg.WriteLine("exiting Set Property Part")
lg.WriteLine("")
End Set
End Property
Private _partNumber As String
Public ReadOnly Property PartNumber() As String
Get
Return _partNumber
End Get
End Property
Private _partRevision As String = ""
Public ReadOnly Property PartRevision() As String
Get
Return _partRevision
End Get
End Property
Private _isTCRunning As Boolean
Public ReadOnly Property IsTCRunning() As Boolean
Get
Return _isTCRunning
End Get
End Property
Private _partFilePath As String
Public ReadOnly Property PartFilePath() As String
Get
Return _partFilePath
End Get
End Property
Private _revisionAttributeTitle As String = "REVISION"
Public Property RevisionAttributeTitle() As String
Get
Return _revisionAttributeTitle
End Get
Set(ByVal value As String)
lg.WriteLine("Set Property: RevisionAttributeTitle")
_revisionAttributeTitle = value
Me.GetPartInfo()
lg.WriteLine("exiting Set Property: RevisionAttributeTitle")
lg.WriteLine("")
End Set
End Property
#End Region
#Region "Public Methods"
Public Sub New()
Me.StartLog()
End Sub
Public Sub New(ByVal thePart As NXOpen.Part)
Me.StartLog()
Me.Part = thePart
End Sub
Public Sub PickExportFolder()
'Requires:
' Imports System.Windows.Forms
'if the user presses OK on the dialog box, the chosen path is returned
'if the user presses cancel on the dialog box, 0 is returned
lg.WriteLine("Sub PickExportFolder")
Dim strLastPath 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
lg.WriteLine(" attempting to retrieve last export path from registry...")
strLastPath = GetSetting("NX journal", "Export dxf", "ExportPath")
'msgbox("Last Path: " & strLastPath)
Catch e As ArgumentException
lg.WriteLine(" ** Argument Exception: " & e.Message)
Catch e As Exception
lg.WriteLine(" ** Exception type: " & e.GetType.ToString)
lg.WriteLine(" ** Exception message: " & e.Message)
'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 D:\home directory on entry.
If IO.Directory.Exists(strLastPath) Then
.SelectedPath = strLastPath
Else
.SelectedPath = My.Computer.FileSystem.SpecialDirectories.MyDocuments
End If
' Prompt the user with a custom message.
.Description = "Select the directory to export .dxf file(s)"
If .ShowDialog = DialogResult.OK Then
' Display the selected folder if the user clicked on the OK button.
Me.OutputFolder = .SelectedPath
lg.WriteLine(" selected output path: " & .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', keep original value of output folder
lg.WriteLine(" folder browser dialog cancel button pressed")
lg.WriteLine(" current output path: " & Me.OutputFolder)
End If
End With
lg.WriteLine("exiting Sub PickExportFolder")
lg.WriteLine("")
End Sub
Public Sub Commit()
lg.WriteLine("Sub Commit")
lg.WriteLine(" number of drawing sheets in part file: " & _drawingSheets.Count.ToString)
If _drawingSheets.Count = 0 Then
lg.WriteLine(" no drawing sheets to export")
Return
End If
Dim numPlists As Integer = 0
Dim myPlists() As Tag
_theUfSession.Plist.AskTags(myPlists, numPlists)
lg.WriteLine(" " & numPlists.ToString & " parts list(s) found, updating...")
For i As Integer = 0 To numPlists - 1
_theUfSession.Plist.Update(myPlists(i))
Next
'update views
Dim sheetCount As Integer = 0
For Each tempSheet As Drawings.DrawingSheet In _drawingSheets
sheetCount += 1
lg.WriteLine(" working on sheet: " & tempSheet.Name)
lg.WriteLine(" sheetCount: " & sheetCount.ToString)
'update any views that are out of date
lg.WriteLine(" updating OutOfDate views on sheet: " & tempSheet.Name)
Me.Part.DraftingViews.UpdateViews(Drawings.DraftingViewCollection.ViewUpdateOption.OutOfDate, tempSheet)
Next
'ready for export
For Each tempDwg As Drawings.DrawingSheet In _drawingSheets
_outputDxfFile = IO.Path.Combine(Me.OutputFolder, _exportFile & "_" & tempDwg.Name & ".dxf")
If Me.DeleteExistingDxfFile(_outputDxfFile) Then
'OK to output
Me.ExportDxf2DExchange(tempDwg, _outputDxfFile)
Else
'user chose not to overwrite or file cannot be deleted
If Me.OverwriteDxf Then
MessageBox.Show("The file: " & _outputDxfFile & " could not be overwritten, skipping file", "Error writing file: " & _outputDxfFile, MessageBoxButtons.OK, MessageBoxIcon.Error)
Else
MessageBox.Show("The file: " & _outputDxfFile & " exists and will not be overwritten, skipping file", "File exists: " & _outputDxfFile, MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
End If
End If
Next
End Sub
#End Region
#Region "Private Methods"
Private Sub GetPartInfo()
lg.WriteLine("Sub GetPartInfo")
If Me.IsTCRunning Then
_partNumber = _thePart.GetStringAttribute("DB_PART_NO")
_partRevision = _thePart.GetStringAttribute("DB_PART_REV")
lg.WriteLine(" TC running")
lg.WriteLine(" partNumber: " & _partNumber)
lg.WriteLine(" partRevision: " & _partRevision)
Else 'running in native mode
_partNumber = IO.Path.GetFileNameWithoutExtension(_thePart.FullPath)
_partFilePath = IO.Directory.GetParent(_thePart.FullPath).ToString
lg.WriteLine(" Native NX")
lg.WriteLine(" partNumber: " & _partNumber)
lg.WriteLine(" partFilePath: " & _partFilePath)
Try
_partRevision = _thePart.GetStringAttribute(Me.RevisionAttributeTitle)
_partRevision = _partRevision.Trim
Catch ex As Exception
_partRevision = ""
End Try
lg.WriteLine(" partRevision: " & _partRevision)
End If
If String.IsNullOrEmpty(_partRevision) Then
_exportFile = _partNumber
Else
_exportFile = _partNumber & "_" & _partRevision
End If
lg.WriteLine("")
Me.GetOutputName()
lg.WriteLine(" exportFile: " & _exportFile)
lg.WriteLine(" outputDxfFile: " & _outputDxfFile)
lg.WriteLine(" exiting Sub GetPartInfo")
lg.WriteLine("")
End Sub
Private Sub GetOutputName()
lg.WriteLine("Sub GetOutputName")
If IsNothing(Me.Part) Then
lg.WriteLine(" Me.Part is Nothing")
lg.WriteLine(" exiting Sub GetOutputName")
lg.WriteLine("")
Return
End If
If Me.IsTCRunning Then
lg.WriteLine(" TC is running")
'exporting from TC to filesystem, no part folder to default to
'default to "MyDocuments" folder
If Me.OutputFolder = "" Then
_outputDxfFile = IO.Path.Combine(My.Computer.FileSystem.SpecialDirectories.MyDocuments, _exportFile & ".dxf")
Else
'if output folder has been specified, use it
_outputDxfFile = IO.Path.Combine(Me.OutputFolder, _exportFile & ".dxf")
End If
Else
lg.WriteLine(" native NX")
'exporting from native to file system
'use part folder as default output folder
If _outputFolder = "" Then
_outputFolder = _partFilePath
End If
_outputDxfFile = IO.Path.Combine(_outputFolder, _exportFile & ".dxf")
End If
lg.WriteLine(" exiting Sub GetOutputName")
lg.WriteLine("")
End Sub
Private Sub GetDrawingSheets()
_drawingSheets.Clear()
For Each tempSheet As Drawings.DrawingSheet In _thePart.DrawingSheets
_drawingSheets.Add(tempSheet)
Next
End Sub
Private Sub StartLog()
lg.WriteLine("")
lg.WriteLine("~ NXJournaling.com: Start of drawing to DXF journal ~")
lg.WriteLine(" ~~ Timestamp of run: " & DateTime.Now.ToString & " ~~")
lg.WriteLine("DXF Exporter Sub StartLog()")
'determine if we are running under TC or native
_theUfSession.UF.IsUgmanagerActive(_isTCRunning)
lg.WriteLine("IsTcRunning: " & _isTCRunning.ToString)
lg.WriteLine("exiting Sub StartLog")
lg.WriteLine("")
End Sub
Private Function DeleteExistingDxfFile(ByVal theDxfFile As String) As Boolean
lg.WriteLine("Function DeleteExistingDxfFile(" & theDxfFile & ")")
If IO.File.Exists(theDxfFile) Then
lg.WriteLine(" specified DXF file already exists")
If Me.OverwriteDxf Then
Try
lg.WriteLine(" user chose to overwrite existing DXF file")
IO.File.Delete(theDxfFile)
lg.WriteLine(" file deleted")
lg.WriteLine(" returning: True")
lg.WriteLine(" exiting Function DeleteExistingDxfFile")
lg.WriteLine("")
Return True
Catch ex As Exception
'rethrow error?
lg.WriteLine(" ** error while attempting to delete existing dxf file")
lg.WriteLine(" " & ex.GetType.ToString & " : " & ex.Message)
lg.WriteLine(" returning: False")
lg.WriteLine(" exiting Function DeleteExistingDxfFile")
lg.WriteLine("")
Return False
End Try
Else
'file exists, overwrite option is set to false - do nothing
lg.WriteLine(" specified dxf file exists, user chose not to overwrite")
lg.WriteLine(" returning: False")
lg.WriteLine(" exiting Function DeleteExistingDxfFile")
lg.WriteLine("")
Return False
End If
Else
'file does not exist
Return True
End If
End Function
Private Function GetParentPath(ByVal thePath As String) As String
lg.WriteLine("Function GetParentPath(" & thePath & ")")
Try
Dim directoryInfo As System.IO.DirectoryInfo
directoryInfo = System.IO.Directory.GetParent(thePath)
lg.WriteLine(" returning: " & directoryInfo.FullName)
lg.WriteLine("exiting Function GetParentPath")
lg.WriteLine("")
Return directoryInfo.FullName
Catch ex As ArgumentNullException
lg.WriteLine(" Path is a null reference.")
Throw ex
Catch ex As ArgumentException
lg.WriteLine(" Path is an empty string, contains only white space, or contains invalid characters")
Throw ex
End Try
lg.WriteLine("exiting Function GetParentPath")
lg.WriteLine("")
End Function
Private Sub ExportDxf2DExchange(ByVal inputSheet As Drawings.DrawingSheet, ByVal outputFileName As String)
'If Not IO.Directory.Exists(outputFolder) Then
' MsgBox("Output folder, " & outputFolder & ", not found; journal exiting.", MsgBoxStyle.Critical, "Output folder error")
' Return
'End If
Dim dxfSettingsDir As String = _theSession.GetEnvironmentVariableValue("DXFDWG_DIR")
Dim ugTo2dDir As String = _theSession.GetEnvironmentVariableValue("UGTO2D_DIR")
If String.IsNullOrEmpty(dxfSettingsDir) Then
MsgBox("DXF settings directory not found; journal exiting.", MsgBoxStyle.Critical, "DXF settings error")
Return
End If
If String.IsNullOrEmpty(ugTo2dDir) Then
MsgBox("UGto2D settings directory not found; journal exiting.", MsgBoxStyle.Critical, "UGto2D settings error")
Return
End If
Dim nXTo2dCreator1 As NXTo2dCreator
nXTo2dCreator1 = _theSession.DexManager.CreateNxto2dCreator()
'Dim outputFileName As String = IO.Path.Combine(outputFolder, _exportFile & "_" & inputSheet.Name & ".dxf")
With nXTo2dCreator1
.Nxto2dSettingsFile = IO.Path.Combine(ugTo2dDir, "ugto2d.def")
.DxfSettingsFile = IO.Path.Combine(dxfSettingsDir, "dxfdwg.def")
.InputFile = Me.Part.FullPath
.OutputFile = outputFileName
.DrawingName = inputSheet.Name
.MaxSystem3DModelSpace = True
.MaxSystemPointRes = True
.FacetBodies = True
.AutoCADRevision = NXTo2dCreator.AutoCADRevisionOption.R14
.ExportData = NXTo2dCreator.ExportDataOption.Drawing
.OutputFileType = NXTo2dCreator.OutputAsOption.DXFFile
End With
Dim nXObject1 As NXObject
nXObject1 = nXTo2dCreator1.Commit()
nXTo2dCreator1.Destroy()
End Sub
#End Region
End Class





RE: NX9 JOURNAL
I seem to remember that you specifically wanted to use 2D exchange, but I don't remember exactly why?
www.nxjournaling.com
RE: NX9 JOURNAL
Thanks for the reply.
Yea we used the 2D exchange because some suppliers complained about problems with other exports we did, but were happy with the 2D exchange.
I did an export using the NX9 autobad export wizard and it looks good, I've sent a sample to the suppliers for feedback, and they seem to think it's alright.
I created a journal and tried to splice in some of your routines, but I'm just not good enough.
I'd appreciate it if you could you please add in the browse-to and the "REV" in the name, to this macro for me..
' NX 9.0.2.5
' Journal created by paulhorton on Wed Aug 19 09:48:15 2015 E. Australia Standard Time
'
Option Strict Off
Imports System
Imports NXOpen
Module NXJournal
Sub Main (ByVal args() As String)
Dim theSession As Session = Session.GetSession()
Dim workPart As Part = theSession.Parts.Work
Dim displayPart As Part = theSession.Parts.Display
' ----------------------------------------------
' Menu: File->Export->AutoCAD DXF/DWG...
' ----------------------------------------------
Dim markId1 As Session.UndoMarkId
markId1 = theSession.SetUndoMark(Session.MarkVisibility.Visible, "Start")
Dim dxfdwgCreator1 As DxfdwgCreator
dxfdwgCreator1 = theSession.DexManager.CreateDxfdwgCreator()
dxfdwgCreator1.ExportData = DxfdwgCreator.ExportDataOption.Drawing
dxfdwgCreator1.AutoCADRevision = DxfdwgCreator.AutoCADRevisionOptions.R2004
dxfdwgCreator1.ViewEditMode = True
dxfdwgCreator1.FlattenAssembly = True
dxfdwgCreator1.SettingsFile = "C:\Program Files\Siemens\NX 9.0\dxfdwg\dxfdwg.def"
dxfdwgCreator1.OutputFile = "C:\TEMP\FTCD-BK951_dwg.dxf"
dxfdwgCreator1.ObjectTypes.Curves = True
dxfdwgCreator1.ObjectTypes.Annotations = True
dxfdwgCreator1.ObjectTypes.Structures = True
dxfdwgCreator1.InputFile = "X:\Paul\NX9-TEST PARTS\FTCD-BK951_dwg.prt"
theSession.SetUndoMarkName(markId1, "AutoCAD DXF/DWG Export Wizard Dialog")
Dim markId2 As Session.UndoMarkId
markId2 = theSession.SetUndoMark(Session.MarkVisibility.Invisible, "AutoCAD DXF/DWG Export Wizard ")
theSession.DeleteUndoMark(markId2, Nothing)
Dim markId3 As Session.UndoMarkId
markId3 = theSession.SetUndoMark(Session.MarkVisibility.Invisible, "AutoCAD DXF/DWG Export Wizard ")
dxfdwgCreator1.WidthFactorMode = DxfdwgCreator.WidthfactorMethodOptions.AutomaticCalculation
dxfdwgCreator1.LayerMask = "1-256"
dxfdwgCreator1.DrawingList = "SH1"
Dim nXObject1 As NXObject
nXObject1 = dxfdwgCreator1.Commit()
theSession.DeleteUndoMark(markId3, Nothing)
theSession.SetUndoMarkName(markId1, "AutoCAD DXF/DWG Export Wizard ")
dxfdwgCreator1.Destroy()
' ----------------------------------------------
' Menu: Tools->Journal->Stop Recording
' ----------------------------------------------
End Sub
End Module
RE: NX9 JOURNAL
It exports the number of sheets but they're all of sheet 1, its not cycling thru them.
Cowski, Could you please have a look at it and see if you can fix it..
'NXJournaling.com
'august 27, 2015
'
'http://www.eng-tips.com/viewthread.
'Export DXF files using dxfdwg.
'NX 9
' added folder picker
Option Strict Off
Imports System
Imports System.Collections.Generic
Imports System.Windows.Forms
Imports NXOpen
Imports NXOpen.UF
Module Module2
Dim theSession As Session = Session.GetSession()
Sub Main()
If IsNothing(theSession.Parts.Work) Then
'active part required
Return
End If
Dim workPart As Part = theSession.Parts.Work
Dim lw As ListingWindow = theSession.ListingWindow
lw.Open()
Const undoMarkName As String = "NXJ journal"
Dim markId1 As Session.UndoMarkId
markId1 = theSession.SetUndoMark(Session.MarkVisibility.Visible, undoMarkName)
Dim myDxfExporter As New NXJExportDXFDWG(workPart)
myDxfExporter.PickExportFolder()
myDxfExporter.RevisionAttributeTitle = "REV"
myDxfExporter.Commit()
lw.Close()
End Sub
Public Function GetUnloadOption(ByVal dummy As String) As Integer
'Unloads the image when the NX session terminates
GetUnloadOption = NXOpen.Session.LibraryUnloadOption.AtTermination
'----Other unload options-------
'Unloads the image immediately after execution within NX
'GetUnloadOption = NXOpen.Session.LibraryUnloadOption.Immediately
'Unloads the image explicitly, via an unload dialog
'GetUnloadOption = NXOpen.Session.LibraryUnloadOption.Explicitly
'-------------------------------
End Function
End Module
Class NXJExportDXFDWG
#Region "Properties"
Private _theSession As Session = Session.GetSession
Private _theUfSession As UFSession = UFSession.GetUFSession
Private lg As LogFile = _theSession.LogFile
Private _drawingSheets As New List(Of Drawings.DrawingSheet)
Private _exportFile As String = ""
Private _partUnits As Integer
Private _outputFolder As String = ""
Public Property OutputFolder() As String
Get
Return _outputFolder
End Get
Set(ByVal value As String)
lg.WriteLine("Set Property OutputFolder")
If Not IO.Directory.Exists(value) Then
Try
lg.WriteLine(" specified directory does not exist, trying to create it...")
IO.Directory.CreateDirectory(value)
lg.WriteLine(" directory created: " & value)
Catch ex As Exception
lg.WriteLine(" ** error while creating directory: " & value)
lg.WriteLine(" " & ex.GetType.ToString & " : " & ex.Message)
lg.WriteLine(" defaulting to: " & My.Computer.FileSystem.SpecialDirectories.MyDocuments)
value = My.Computer.FileSystem.SpecialDirectories.MyDocuments
End Try
End If
_outputFolder = value
_outputDxfFile = IO.Path.Combine(_outputFolder, _exportFile & ".dxf")
lg.WriteLine(" outputFolder: " & _outputFolder)
lg.WriteLine(" outputDxfFile: " & _outputDxfFile)
lg.WriteLine("exiting Set Property OutputFolder")
lg.WriteLine("")
End Set
End Property
Private _outputDxfFile As String = ""
Public Property OutputDxfFileName() As String
Get
Return _outputDxfFile
End Get
Set(ByVal value As String)
lg.WriteLine("Set Property OutputDxfFileName")
_outputDxfFile = value
lg.WriteLine(" outputDxfFile: " & value)
'update _outputFolder
Me.OutputFolder = Me.GetParentPath(_outputDxfFile)
lg.WriteLine("exiting Set Property OutputDxfFileName")
lg.WriteLine("")
End Set
End Property
Private _overwriteDxf As Boolean = True
Public Property OverwriteDxf() As Boolean
Get
Return _overwriteDxf
End Get
Set(ByVal value As Boolean)
lg.WriteLine("Set Property OverwriteDxf")
_overwriteDxf = value
lg.WriteLine(" overwriteDxf: " & _overwriteDxf.ToString)
lg.WriteLine("exiting Set Property OverwriteDxf")
lg.WriteLine("")
End Set
End Property
Private _thePart As Part = Nothing
Public Property Part() As Part
Get
Return _thePart
End Get
Set(ByVal value As Part)
lg.WriteLine("Set Property Part")
_thePart = value
_partUnits = _thePart.PartUnits
Me.GetPartInfo()
Me.GetDrawingSheets()
lg.WriteLine("exiting Set Property Part")
lg.WriteLine("")
End Set
End Property
Private _partNumber As String
Public ReadOnly Property PartNumber() As String
Get
Return _partNumber
End Get
End Property
Private _partRevision As String = ""
Public ReadOnly Property PartRevision() As String
Get
Return _partRevision
End Get
End Property
Private _isTCRunning As Boolean
Public ReadOnly Property IsTCRunning() As Boolean
Get
Return _isTCRunning
End Get
End Property
Private _partFilePath As String
Public ReadOnly Property PartFilePath() As String
Get
Return _partFilePath
End Get
End Property
Private _revisionAttributeTitle As String = "REVISION"
Public Property RevisionAttributeTitle() As String
Get
Return _revisionAttributeTitle
End Get
Set(ByVal value As String)
lg.WriteLine("Set Property: RevisionAttributeTitle")
_revisionAttributeTitle = value
Me.GetPartInfo()
lg.WriteLine("exiting Set Property: RevisionAttributeTitle")
lg.WriteLine("")
End Set
End Property
#End Region
#Region "Public Methods"
Public Sub New()
Me.StartLog()
End Sub
Public Sub New(ByVal thePart As NXOpen.Part)
Me.StartLog()
Me.Part = thePart
End Sub
Public Sub PickExportFolder()
'Requires:
' Imports System.Windows.Forms
'if the user presses OK on the dialog box, the chosen path is returned
'if the user presses cancel on the dialog box, 0 is returned
lg.WriteLine("Sub PickExportFolder")
Dim strLastPath 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
lg.WriteLine(" attempting to retrieve last export path from registry...")
strLastPath = GetSetting("NX journal", "Export dxf", "ExportPath")
'msgbox("Last Path: " & strLastPath)
Catch e As ArgumentException
lg.WriteLine(" ** Argument Exception: " & e.Message)
Catch e As Exception
lg.WriteLine(" ** Exception type: " & e.GetType.ToString)
lg.WriteLine(" ** Exception message: " & e.Message)
'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 D:\home directory on entry.
If IO.Directory.Exists(strLastPath) Then
.SelectedPath = strLastPath
Else
.SelectedPath = My.Computer.FileSystem.SpecialDirectories.MyDocuments
End If
' Prompt the user with a custom message.
.Description = "Select the directory to export .dxf file(s)"
If .ShowDialog = DialogResult.OK Then
' Display the selected folder if the user clicked on the OK button.
Me.OutputFolder = .SelectedPath
lg.WriteLine(" selected output path: " & .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', keep original value of output folder
lg.WriteLine(" folder browser dialog cancel button pressed")
lg.WriteLine(" current output path: " & Me.OutputFolder)
End If
End With
lg.WriteLine("exiting Sub PickExportFolder")
lg.WriteLine("")
End Sub
Public Sub Commit()
lg.WriteLine("Sub Commit")
lg.WriteLine(" number of drawing sheets in part file: " & _drawingSheets.Count.ToString)
If _drawingSheets.Count = 0 Then
lg.WriteLine(" no drawing sheets to export")
Return
End If
Dim numPlists As Integer = 0
Dim myPlists() As Tag
_theUfSession.Plist.AskTags(myPlists, numPlists)
lg.WriteLine(" " & numPlists.ToString & " parts list(s) found, updating...")
For i As Integer = 0 To numPlists - 1
_theUfSession.Plist.Update(myPlists(i))
Next
'update views
Dim sheetCount As Integer = 0
For Each tempSheet As Drawings.DrawingSheet In _drawingSheets
sheetCount += 1
lg.WriteLine(" working on sheet: " & tempSheet.Name)
lg.WriteLine(" sheetCount: " & sheetCount.ToString)
'update any views that are out of date
lg.WriteLine(" updating OutOfDate views on sheet: " & tempSheet.Name)
Me.Part.DraftingViews.UpdateViews(Drawings.DraftingViewCollection.ViewUpdateOption.OutOfDate, tempSheet)
Next
'ready for export
For Each tempDwg As Drawings.DrawingSheet In _drawingSheets
_outputDxfFile = IO.Path.Combine(Me.OutputFolder, _exportFile & "_" & tempDwg.Name & ".dxf")
If Me.DeleteExistingDxfFile(_outputDxfFile) Then
'OK to output
Me.ExportDxfdwg(tempDwg, _outputDxfFile)
Else
'user chose not to overwrite or file cannot be deleted
If Me.OverwriteDxf Then
MessageBox.Show("The file: " & _outputDxfFile & " could not be overwritten, skipping file", "Error writing file: " & _outputDxfFile, MessageBoxButtons.OK, MessageBoxIcon.Error)
Else
MessageBox.Show("The file: " & _outputDxfFile & " exists and will not be overwritten, skipping file", "File exists: " & _outputDxfFile, MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
End If
End If
Next
End Sub
#End Region
#Region "Private Methods"
Private Sub GetPartInfo()
lg.WriteLine("Sub GetPartInfo")
If Me.IsTCRunning Then
_partNumber = _thePart.GetStringAttribute("DB_PART_NO")
_partRevision = _thePart.GetStringAttribute("DB_PART_REV")
lg.WriteLine(" TC running")
lg.WriteLine(" partNumber: " & _partNumber)
lg.WriteLine(" partRevision: " & _partRevision)
Else 'running in native mode
_partNumber = IO.Path.GetFileNameWithoutExtension(_thePart.FullPath)
_partFilePath = IO.Directory.GetParent(_thePart.FullPath).ToString
lg.WriteLine(" Native NX")
lg.WriteLine(" partNumber: " & _partNumber)
lg.WriteLine(" partFilePath: " & _partFilePath)
Try
_partRevision = _thePart.GetStringAttribute(Me.RevisionAttributeTitle)
_partRevision = _partRevision.Trim
Catch ex As Exception
_partRevision = ""
End Try
lg.WriteLine(" partRevision: " & _partRevision)
End If
If String.IsNullOrEmpty(_partRevision) Then
_exportFile = _partNumber
Else
_exportFile = _partNumber & "_" & _partRevision
End If
lg.WriteLine("")
Me.GetOutputName()
lg.WriteLine(" exportFile: " & _exportFile)
lg.WriteLine(" outputDxfFile: " & _outputDxfFile)
lg.WriteLine(" exiting Sub GetPartInfo")
lg.WriteLine("")
End Sub
Private Sub GetOutputName()
lg.WriteLine("Sub GetOutputName")
If IsNothing(Me.Part) Then
lg.WriteLine(" Me.Part is Nothing")
lg.WriteLine(" exiting Sub GetOutputName")
lg.WriteLine("")
Return
End If
If Me.IsTCRunning Then
lg.WriteLine(" TC is running")
'exporting from TC to filesystem, no part folder to default to
'default to "MyDocuments" folder
If Me.OutputFolder = "" Then
_outputDxfFile = IO.Path.Combine(My.Computer.FileSystem.SpecialDirectories.MyDocuments, _exportFile & ".dxf")
Else
'if output folder has been specified, use it
_outputDxfFile = IO.Path.Combine(Me.OutputFolder, _exportFile & ".dxf")
End If
Else
lg.WriteLine(" native NX")
'exporting from native to file system
'use part folder as default output folder
If _outputFolder = "" Then
_outputFolder = _partFilePath
End If
_outputDxfFile = IO.Path.Combine(_outputFolder, _exportFile & ".dxf")
End If
lg.WriteLine(" exiting Sub GetOutputName")
lg.WriteLine("")
End Sub
Private Sub GetDrawingSheets()
_drawingSheets.Clear()
For Each tempSheet As Drawings.DrawingSheet In _thePart.DrawingSheets
_drawingSheets.Add(tempSheet)
Next
End Sub
Private Sub StartLog()
lg.WriteLine("")
lg.WriteLine("~ NXJournaling.com: Start of drawing to DXF journal ~")
lg.WriteLine(" ~~ Timestamp of run: " & DateTime.Now.ToString & " ~~")
lg.WriteLine("DXF Exporter Sub StartLog()")
'determine if we are running under TC or native
_theUfSession.UF.IsUgmanagerActive(_isTCRunning)
lg.WriteLine("IsTcRunning: " & _isTCRunning.ToString)
lg.WriteLine("exiting Sub StartLog")
lg.WriteLine("")
End Sub
Private Function DeleteExistingDxfFile(ByVal theDxfFile As String) As Boolean
lg.WriteLine("Function DeleteExistingDxfFile(" & theDxfFile & ")")
If IO.File.Exists(theDxfFile) Then
lg.WriteLine(" specified DXF file already exists")
If Me.OverwriteDxf Then
Try
lg.WriteLine(" user chose to overwrite existing DXF file")
IO.File.Delete(theDxfFile)
lg.WriteLine(" file deleted")
lg.WriteLine(" returning: True")
lg.WriteLine(" exiting Function DeleteExistingDxfFile")
lg.WriteLine("")
Return True
Catch ex As Exception
'rethrow error?
lg.WriteLine(" ** error while attempting to delete existing dxf file")
lg.WriteLine(" " & ex.GetType.ToString & " : " & ex.Message)
lg.WriteLine(" returning: False")
lg.WriteLine(" exiting Function DeleteExistingDxfFile")
lg.WriteLine("")
Return False
End Try
Else
'file exists, overwrite option is set to false - do nothing
lg.WriteLine(" specified dxf file exists, user chose not to overwrite")
lg.WriteLine(" returning: False")
lg.WriteLine(" exiting Function DeleteExistingDxfFile")
lg.WriteLine("")
Return False
End If
Else
'file does not exist
Return True
End If
End Function
Private Function GetParentPath(ByVal thePath As String) As String
lg.WriteLine("Function GetParentPath(" & thePath & ")")
Try
Dim directoryInfo As System.IO.DirectoryInfo
directoryInfo = System.IO.Directory.GetParent(thePath)
lg.WriteLine(" returning: " & directoryInfo.FullName)
lg.WriteLine("exiting Function GetParentPath")
lg.WriteLine("")
Return directoryInfo.FullName
Catch ex As ArgumentNullException
lg.WriteLine(" Path is a null reference.")
Throw ex
Catch ex As ArgumentException
lg.WriteLine(" Path is an empty string, contains only white space, or contains invalid characters")
Throw ex
End Try
lg.WriteLine("exiting Function GetParentPath")
lg.WriteLine("")
End Function
Private Sub ExportDxfdwg(ByVal inputSheet As Drawings.DrawingSheet, ByVal outputFileName As String)
'If Not IO.Directory.Exists(outputFolder) Then
' MsgBox("Output folder, " & outputFolder & ", not found; journal exiting.", MsgBoxStyle.Critical, "Output folder error")
' Return
'End If
Dim dxfSettingsDir As String = _theSession.GetEnvironmentVariableValue("DXFDWG_DIR")
Dim DxfdwgDir As String = _theSession.GetEnvironmentVariableValue("DXFDWG_DIR")
If String.IsNullOrEmpty(dxfSettingsDir) Then
MsgBox("DXF settings directory not found; journal exiting.", MsgBoxStyle.Critical, "DXF settings error")
Return
End If
If String.IsNullOrEmpty(DxfdwgDir) Then
MsgBox("Dxfdwg settings directory not found; journal exiting.", MsgBoxStyle.Critical, "Dxfdwg settings error")
Return
End If
Dim DxfdwgCreator1 As DxfdwgCreator
DxfdwgCreator1 = _theSession.DexManager.CreateDxfdwgCreator()
'Dim outputFileName As String = IO.Path.Combine(outputFolder, _exportFile & "_" & inputSheet.Name & ".dxf")
With DxfdwgCreator1
' .DxfdwgSettingsFile = IO.Path.Combine(Dxfdwg.def, "Dxfdwg.def")
' .DxfSettingsFile = IO.Path.Combine(dxfSettingsDir, "dxfdwg.def")
.InputFile = Me.Part.FullPath
.OutputFile = outputFileName
' .DrawingName = inputSheet.Name
' .MaxSystem3DModelSpace = True
' .MaxSystemPointRes = True
' .FacetBodies = True
' .AutoCADRevision = DxfdwgCreator.AutoCADRevisionOption.R14
.ExportData = DxfdwgCreator.ExportDataOption.Drawing
' .OutputFileType = DxfdwgCreator.OutputAsOption.DXFFile
.ObjectTypes.Curves = True
.ObjectTypes.Annotations = True
.ObjectTypes.Structures = True
.FlattenAssembly = False
.ViewEditMode = False
.FileSaveFlag = False
.LayerMask = "1-256"
.DrawingList = ""
.ViewList = "TOP,FRONT,RIGHT,BACK,BOTTOM,LEFT,TFR-ISO,TFR-TRI"
End With
Dim nXObject1 As NXObject
nXObject1 = DxfdwgCreator1.Commit()
DxfdwgCreator1.Destroy()
End Sub
#End Region
End Class
RE: NX9 JOURNAL
CODE
'NXJournaling.com 'august 27, 2015 ' 'http://www.eng-tips.com/viewthread. 'Export DXF files using dxfdwg. 'NX 9 ' added folder picker Option Strict Off Imports System Imports System.Collections.Generic Imports System.Windows.Forms Imports NXOpen Imports NXOpen.UF Module Module1 Dim theSession As Session = Session.GetSession() Sub Main() If IsNothing(theSession.Parts.Work) Then 'active part required Return End If Dim workPart As Part = theSession.Parts.Work Dim lw As ListingWindow = theSession.ListingWindow lw.Open() Const undoMarkName As String = "NXJ journal" Dim markId1 As Session.UndoMarkId markId1 = theSession.SetUndoMark(Session.MarkVisibility.Visible, undoMarkName) Dim myDxfExporter As New NXJExportDXFDWG(workPart) myDxfExporter.PickExportFolder() myDxfExporter.RevisionAttributeTitle = "REV" myDxfExporter.Commit() lw.Close() End Sub Public Function GetUnloadOption(ByVal dummy As String) As Integer 'Unloads the image when the NX session terminates GetUnloadOption = NXOpen.Session.LibraryUnloadOption.AtTermination '----Other unload options------- 'Unloads the image immediately after execution within NX 'GetUnloadOption = NXOpen.Session.LibraryUnloadOption.Immediately 'Unloads the image explicitly, via an unload dialog 'GetUnloadOption = NXOpen.Session.LibraryUnloadOption.Explicitly '------------------------------- End Function End Module Class NXJExportDXFDWG #Region "Properties" Private _theSession As Session = Session.GetSession Private _theUfSession As UFSession = UFSession.GetUFSession Private lg As LogFile = _theSession.LogFile Private _drawingSheets As New List(Of Drawings.DrawingSheet) Private _exportFile As String = "" Private _partUnits As Integer Private _outputFolder As String = "" Public Property OutputFolder() As String Get Return _outputFolder End Get Set(ByVal value As String) lg.WriteLine("Set Property OutputFolder") If Not IO.Directory.Exists(value) Then Try lg.WriteLine(" specified directory does not exist, trying to create it...") IO.Directory.CreateDirectory(value) lg.WriteLine(" directory created: " & value) Catch ex As Exception lg.WriteLine(" ** error while creating directory: " & value) lg.WriteLine(" " & ex.GetType.ToString & " : " & ex.Message) lg.WriteLine(" defaulting to: " & My.Computer.FileSystem.SpecialDirectories.MyDocuments) value = My.Computer.FileSystem.SpecialDirectories.MyDocuments End Try End If _outputFolder = value _outputDxfFile = IO.Path.Combine(_outputFolder, _exportFile & ".dxf") lg.WriteLine(" outputFolder: " & _outputFolder) lg.WriteLine(" outputDxfFile: " & _outputDxfFile) lg.WriteLine("exiting Set Property OutputFolder") lg.WriteLine("") End Set End Property Private _outputDxfFile As String = "" Public Property OutputDxfFileName() As String Get Return _outputDxfFile End Get Set(ByVal value As String) lg.WriteLine("Set Property OutputDxfFileName") _outputDxfFile = value lg.WriteLine(" outputDxfFile: " & value) 'update _outputFolder Me.OutputFolder = Me.GetParentPath(_outputDxfFile) lg.WriteLine("exiting Set Property OutputDxfFileName") lg.WriteLine("") End Set End Property Private _overwriteDxf As Boolean = True Public Property OverwriteDxf() As Boolean Get Return _overwriteDxf End Get Set(ByVal value As Boolean) lg.WriteLine("Set Property OverwriteDxf") _overwriteDxf = value lg.WriteLine(" overwriteDxf: " & _overwriteDxf.ToString) lg.WriteLine("exiting Set Property OverwriteDxf") lg.WriteLine("") End Set End Property Private _thePart As Part = Nothing Public Property Part() As Part Get Return _thePart End Get Set(ByVal value As Part) lg.WriteLine("Set Property Part") _thePart = value _partUnits = _thePart.PartUnits Me.GetPartInfo() Me.GetDrawingSheets() lg.WriteLine("exiting Set Property Part") lg.WriteLine("") End Set End Property Private _partNumber As String Public ReadOnly Property PartNumber() As String Get Return _partNumber End Get End Property Private _partRevision As String = "" Public ReadOnly Property PartRevision() As String Get Return _partRevision End Get End Property Private _isTCRunning As Boolean Public ReadOnly Property IsTCRunning() As Boolean Get Return _isTCRunning End Get End Property Private _partFilePath As String Public ReadOnly Property PartFilePath() As String Get Return _partFilePath End Get End Property Private _revisionAttributeTitle As String = "REVISION" Public Property RevisionAttributeTitle() As String Get Return _revisionAttributeTitle End Get Set(ByVal value As String) lg.WriteLine("Set Property: RevisionAttributeTitle") _revisionAttributeTitle = value Me.GetPartInfo() lg.WriteLine("exiting Set Property: RevisionAttributeTitle") lg.WriteLine("") End Set End Property #End Region #Region "Public Methods" Public Sub New() Me.StartLog() End Sub Public Sub New(ByVal thePart As NXOpen.Part) Me.StartLog() Me.Part = thePart End Sub Public Sub PickExportFolder() 'Requires: ' Imports System.Windows.Forms 'if the user presses OK on the dialog box, the chosen path is returned 'if the user presses cancel on the dialog box, 0 is returned lg.WriteLine("Sub PickExportFolder") Dim strLastPath 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 lg.WriteLine(" attempting to retrieve last export path from registry...") strLastPath = GetSetting("NX journal", "Export dxf", "ExportPath") 'msgbox("Last Path: " & strLastPath) Catch e As ArgumentException lg.WriteLine(" ** Argument Exception: " & e.Message) Catch e As Exception lg.WriteLine(" ** Exception type: " & e.GetType.ToString) lg.WriteLine(" ** Exception message: " & e.Message) '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 D:\home directory on entry. If IO.Directory.Exists(strLastPath) Then .SelectedPath = strLastPath Else .SelectedPath = My.Computer.FileSystem.SpecialDirectories.MyDocuments End If ' Prompt the user with a custom message. .Description = "Select the directory to export .dxf file(s)" If .ShowDialog = DialogResult.OK Then ' Display the selected folder if the user clicked on the OK button. Me.OutputFolder = .SelectedPath lg.WriteLine(" selected output path: " & .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', keep original value of output folder lg.WriteLine(" folder browser dialog cancel button pressed") lg.WriteLine(" current output path: " & Me.OutputFolder) End If End With lg.WriteLine("exiting Sub PickExportFolder") lg.WriteLine("") End Sub Public Sub Commit() lg.WriteLine("Sub Commit") lg.WriteLine(" number of drawing sheets in part file: " & _drawingSheets.Count.ToString) If _drawingSheets.Count = 0 Then lg.WriteLine(" no drawing sheets to export") Return End If Dim numPlists As Integer = 0 Dim myPlists() As Tag _theUfSession.Plist.AskTags(myPlists, numPlists) lg.WriteLine(" " & numPlists.ToString & " parts list(s) found, updating...") For i As Integer = 0 To numPlists - 1 _theUfSession.Plist.Update(myPlists(i)) Next 'update views Dim sheetCount As Integer = 0 For Each tempSheet As Drawings.DrawingSheet In _drawingSheets sheetCount += 1 lg.WriteLine(" working on sheet: " & tempSheet.Name) lg.WriteLine(" sheetCount: " & sheetCount.ToString) 'update any views that are out of date lg.WriteLine(" updating OutOfDate views on sheet: " & tempSheet.Name) Me.Part.DraftingViews.UpdateViews(Drawings.DraftingViewCollection.ViewUpdateOption.OutOfDate, tempSheet) Next 'ready for export For Each tempDwg As Drawings.DrawingSheet In _drawingSheets tempDwg.Open() _outputDxfFile = IO.Path.Combine(Me.OutputFolder, _exportFile & "_" & tempDwg.Name & ".dxf") If Me.DeleteExistingDxfFile(_outputDxfFile) Then 'OK to output Me.ExportDxfdwg(tempDwg, _outputDxfFile) Else 'user chose not to overwrite or file cannot be deleted If Me.OverwriteDxf Then MessageBox.Show("The file: " & _outputDxfFile & " could not be overwritten, skipping file", "Error writing file: " & _outputDxfFile, MessageBoxButtons.OK, MessageBoxIcon.Error) Else MessageBox.Show("The file: " & _outputDxfFile & " exists and will not be overwritten, skipping file", "File exists: " & _outputDxfFile, MessageBoxButtons.OK, MessageBoxIcon.Exclamation) End If End If Next End Sub #End Region #Region "Private Methods" Private Sub GetPartInfo() lg.WriteLine("Sub GetPartInfo") If Me.IsTCRunning Then _partNumber = _thePart.GetStringAttribute("DB_PART_NO") _partRevision = _thePart.GetStringAttribute("DB_PART_REV") lg.WriteLine(" TC running") lg.WriteLine(" partNumber: " & _partNumber) lg.WriteLine(" partRevision: " & _partRevision) Else 'running in native mode _partNumber = IO.Path.GetFileNameWithoutExtension(_thePart.FullPath) _partFilePath = IO.Directory.GetParent(_thePart.FullPath).ToString lg.WriteLine(" Native NX") lg.WriteLine(" partNumber: " & _partNumber) lg.WriteLine(" partFilePath: " & _partFilePath) Try _partRevision = _thePart.GetStringAttribute(Me.RevisionAttributeTitle) _partRevision = _partRevision.Trim Catch ex As Exception _partRevision = "" End Try lg.WriteLine(" partRevision: " & _partRevision) End If If String.IsNullOrEmpty(_partRevision) Then _exportFile = _partNumber Else _exportFile = _partNumber & "_" & _partRevision End If lg.WriteLine("") Me.GetOutputName() lg.WriteLine(" exportFile: " & _exportFile) lg.WriteLine(" outputDxfFile: " & _outputDxfFile) lg.WriteLine(" exiting Sub GetPartInfo") lg.WriteLine("") End Sub Private Sub GetOutputName() lg.WriteLine("Sub GetOutputName") If IsNothing(Me.Part) Then lg.WriteLine(" Me.Part is Nothing") lg.WriteLine(" exiting Sub GetOutputName") lg.WriteLine("") Return End If If Me.IsTCRunning Then lg.WriteLine(" TC is running") 'exporting from TC to filesystem, no part folder to default to 'default to "MyDocuments" folder If Me.OutputFolder = "" Then _outputDxfFile = IO.Path.Combine(My.Computer.FileSystem.SpecialDirectories.MyDocuments, _exportFile & ".dxf") Else 'if output folder has been specified, use it _outputDxfFile = IO.Path.Combine(Me.OutputFolder, _exportFile & ".dxf") End If Else lg.WriteLine(" native NX") 'exporting from native to file system 'use part folder as default output folder If _outputFolder = "" Then _outputFolder = _partFilePath End If _outputDxfFile = IO.Path.Combine(_outputFolder, _exportFile & ".dxf") End If lg.WriteLine(" exiting Sub GetOutputName") lg.WriteLine("") End Sub Private Sub GetDrawingSheets() _drawingSheets.Clear() For Each tempSheet As Drawings.DrawingSheet In _thePart.DrawingSheets _drawingSheets.Add(tempSheet) Next End Sub Private Sub StartLog() lg.WriteLine("") lg.WriteLine("~ NXJournaling.com: Start of drawing to DXF journal ~") lg.WriteLine(" ~~ Timestamp of run: " & DateTime.Now.ToString & " ~~") lg.WriteLine("DXF Exporter Sub StartLog()") 'determine if we are running under TC or native _theUfSession.UF.IsUgmanagerActive(_isTCRunning) lg.WriteLine("IsTcRunning: " & _isTCRunning.ToString) lg.WriteLine("exiting Sub StartLog") lg.WriteLine("") End Sub Private Function DeleteExistingDxfFile(ByVal theDxfFile As String) As Boolean lg.WriteLine("Function DeleteExistingDxfFile(" & theDxfFile & ")") If IO.File.Exists(theDxfFile) Then lg.WriteLine(" specified DXF file already exists") If Me.OverwriteDxf Then Try lg.WriteLine(" user chose to overwrite existing DXF file") IO.File.Delete(theDxfFile) lg.WriteLine(" file deleted") lg.WriteLine(" returning: True") lg.WriteLine(" exiting Function DeleteExistingDxfFile") lg.WriteLine("") Return True Catch ex As Exception 'rethrow error? lg.WriteLine(" ** error while attempting to delete existing dxf file") lg.WriteLine(" " & ex.GetType.ToString & " : " & ex.Message) lg.WriteLine(" returning: False") lg.WriteLine(" exiting Function DeleteExistingDxfFile") lg.WriteLine("") Return False End Try Else 'file exists, overwrite option is set to false - do nothing lg.WriteLine(" specified dxf file exists, user chose not to overwrite") lg.WriteLine(" returning: False") lg.WriteLine(" exiting Function DeleteExistingDxfFile") lg.WriteLine("") Return False End If Else 'file does not exist Return True End If End Function Private Function GetParentPath(ByVal thePath As String) As String lg.WriteLine("Function GetParentPath(" & thePath & ")") Try Dim directoryInfo As System.IO.DirectoryInfo directoryInfo = System.IO.Directory.GetParent(thePath) lg.WriteLine(" returning: " & directoryInfo.FullName) lg.WriteLine("exiting Function GetParentPath") lg.WriteLine("") Return directoryInfo.FullName Catch ex As ArgumentNullException lg.WriteLine(" Path is a null reference.") Throw ex Catch ex As ArgumentException lg.WriteLine(" Path is an empty string, contains only white space, or contains invalid characters") Throw ex End Try lg.WriteLine("exiting Function GetParentPath") lg.WriteLine("") End Function Private Sub ExportDxfdwg(ByVal inputSheet As Drawings.DrawingSheet, ByVal outputFileName As String) 'If Not IO.Directory.Exists(outputFolder) Then ' MsgBox("Output folder, " & outputFolder & ", not found; journal exiting.", MsgBoxStyle.Critical, "Output folder error") ' Return 'End If Dim dxfSettingsDir As String = _theSession.GetEnvironmentVariableValue("DXFDWG_DIR") Dim DxfdwgDir As String = _theSession.GetEnvironmentVariableValue("DXFDWG_DIR") If String.IsNullOrEmpty(dxfSettingsDir) Then MsgBox("DXF settings directory not found; journal exiting.", MsgBoxStyle.Critical, "DXF settings error") Return End If If String.IsNullOrEmpty(DxfdwgDir) Then MsgBox("Dxfdwg settings directory not found; journal exiting.", MsgBoxStyle.Critical, "Dxfdwg settings error") Return End If Dim DxfdwgCreator1 As DxfdwgCreator DxfdwgCreator1 = _theSession.DexManager.CreateDxfdwgCreator() 'Dim outputFileName As String = IO.Path.Combine(OutputFolder, _exportFile & "_" & inputSheet.Name & ".dxf") With DxfdwgCreator1 .SettingsFile = IO.Path.Combine(dxfSettingsDir, "dxfdwg.def") .InputFile = Me.Part.FullPath .OutputFile = outputFileName .DrawingList = inputSheet.Name .AutoCADRevision = DxfdwgCreator.AutoCADRevisionOptions.R2007 .ExportData = DxfdwgCreator.ExportDataOption.Drawing .OutputFileType = DxfdwgCreator.OutputFileTypeOption.Dxf .ObjectTypes.Curves = True .ObjectTypes.Annotations = True .ObjectTypes.Structures = True .FlattenAssembly = False .ViewEditMode = False .FileSaveFlag = False .LayerMask = "1-256" .DrawingList = "" .ViewList = "TOP,FRONT,RIGHT,BACK,BOTTOM,LEFT,TFR-ISO,TFR-TRI" End With Dim nXObject1 As NXObject nXObject1 = DxfdwgCreator1.Commit() DxfdwgCreator1.Destroy() End Sub #End Region End Classwww.nxjournaling.com
RE: NX9 JOURNAL
Thanks so much for your time and expertise, that works great, it looks like the main thing it needed was tempDwg.Open()
much appreciated..
I saw some other threads where you got the journal to scale the output back up to 1:1.
I found the code but it was all to do with CGM code.
I realise the scale would not be fool proof.
Would it be possible to incorporate that feature into this journal
RE: NX9 JOURNAL
Sorry to keep asking favours...
RE: NX9 JOURNAL
www.nxjournaling.com
RE: NX9 JOURNAL
They've managed ok so far scaling it themselves.
Thanks..