' ' Export active drawing to dwg in directory C:\dwg\ ' Option Strict On Imports System Imports System.ComponentModel Imports System.Diagnostics Imports System.IO Imports System.Text Imports NXOpen Imports NXOpen.UF Imports NXOpen.Drawings Imports NXOpen.Part Imports Microsoft.Win32 Imports Microsoft.VisualBasic Module ExportDWG 'declare actual nx-session Dim theSession As Session = Session.GetSession() 'declare error flags Private ERROR_FILE_NOT_FOUND As Integer = 2 Private ERROR_ACCESS_DENIED As Integer = 5 'declare some variables for random string generator Dim KeyGen As RandomKeyGenerator = New RandomKeyGenerator 'generate random string Dim RandomKey As String = KeyGen.Generate() Sub Main If Not theSession.Parts.Work Is Nothing Then 'declare actual workpart Dim workPart As Part = theSession.Parts.Work 'declare actual drawingsheet Dim drawingSheet As DrawingSheet = workPart.DrawingSheets().CurrentDrawingSheet 'declare actual User-Function-Session Dim s As UFSession = UFSession.GetUFSession() Dim cgm As UFCgm = s.cgm 'declare static sheetname for later use Static sheetName As String = CType(drawingSheet.Name, String) 'declare static partname for later use Static dwgName As String = CType(workPart.Leaf, String) 'declare static full partname with path Static fullpath As String = CType(workPart.FullPath, String) Dim p As System.IO.Path 'declare path of partfile without partname and extension Dim partpath As String = CType(p.GetDirectoryName(fullpath), String) 'combine partname and sheetname to new partfile basename Dim basename As String = dwgName & "_" & sheetName 'declare temporary cgm-filename Dim temp_cgm As String = Environ("TEMP").ToString & "\" & RandomKey & ".cgm" 'declare temporary part-filename Dim temp_part_name As String = Environ("TEMP").ToString & "\" & RandomKey & ".prt" 'declare link to cgm export-options Dim export_options As UFCgm.ExportOptions 'declare link to cgm import-options Dim import_options As UFCgm.ImportOptions 'export temporary cgm-file 'get cgm export-options cgm.AskDefaultExportOptions(export_options) 'check if temporary cgm-file exists... If System.IO.File.Exists(temp_cgm) = True Then '...and delete if exists System.IO.File.Delete(temp_cgm) End If 'exporting temorary cgm-file cgm.ExportCgm(workPart.DrawingSheets().CurrentDrawingSheet.Tag, export_options, temp_cgm) 'create new temporary part-file 'check if temporary part-file exists... If System.IO.File.Exists(temp_part_name) = True Then '...and delete if exists System.IO.File.Delete(temp_part_name) End If 'creating new temporary part-file Dim temp_part As Part = theSession.Parts.NewDisplay(temp_part_name, Part.Units.Millimeters) 'set actual view top-view 'declare link to layout Dim layout1 As Layout = CType(theSession.Parts.Work.Layouts.FindObject("L1"), Layout) 'declare link to view Dim view1 As View = CType(theSession.Parts.Work.Views.FindObject("TOP"), View) 'replacing view to top-view layout1.ReplaceView(theSession.Parts.Work.Views.WorkView, view1, True) 'import cgm-file 'importing temporary cgm-file in actual workpart (temporary part-file) cgm.ImportCgm(temp_cgm, import_options) 'check if temporary cgm-file exists... If System.IO.File.Exists(temp_cgm) = True Then '...and delete if exists System.IO.File.Delete(temp_cgm) End If 'fitting actual View view1.Fit() 'save temporary part-file Dim partSaveStatus1 As PartSaveStatus 'save actual workpart (temporary part-file) theSession.Parts.Work.Save(Part.SaveComponents.True, Part.CloseAfterSave.False, partSaveStatus1) partSaveStatus1.Dispose() 'start conversion of temporary part-file to dwg with external translator 'check if destination save-directory NOT exists... If Not Directory.Exists("C:\dwg\" & Environment.UserName) Then '...if not create destination save-directory Directory.CreateDirectory("C:\dwg\" & Environment.UserName) End If 'declare executable of translator (dxfdwg.exe) Dim dxfdwg As String = """" & Environ("UGII_BASE_DIR").ToString & "\dxfdwg\dxfdwg.exe""" 'declare definition-filename of translator (.def) Dim deffile As String = """d=" & Environ("UGII_BASE_DIR").ToString & "\dxfdwg\dxfdwg.def""" 'declare input part filename of translator (.prt) Dim tempfile As String = """" & Environ("TEMP").ToString & "\" & RandomKey & ".prt""" 'declare log filename of translator (.log) Dim logfile As String = """l=" & Environ("TEMP").ToString & "\" & RandomKey & ".log""" 'declare output filename (.dwg) Dim outfile As String = """o=C:\dwg\" & Environment.UserName & "\" & basename & ".dwg""" 'sets mode of conversion. only "m=auto" is needed! Dim mode As String = """m=auto""" 'combine all parameters for commandline use Dim args As String = tempfile & " " & outfile & " " & logfile & " " & deffile & " " & mode 'declare link to Process() Dim convertcmd As New Process() 'error-handling while converting Try 'declare filename for Process() convertcmd.StartInfo.FileName = dxfdwg 'declare arguments for Process() convertcmd.StartInfo.Arguments = args '''convertcmd.StartInfo.CreateNoWindow = True 'declare windowstyles for Process() convertcmd.StartInfo.WindowStyle = ProcessWindowStyle.Normal 'start Process() convertcmd.Start() 'if error occurs, check for reasons... Catch e As Win32Exception '...if executable file is not found... If e.NativeErrorCode = ERROR_FILE_NOT_FOUND Then '...throw error-messagebox System.Windows.Forms.MessageBox.Show((e.Message + ". Check the path.")) Else '...permissions to execute the translator... If e.NativeErrorCode = ERROR_ACCESS_DENIED Then '...throw error-messagebox System.Windows.Forms.MessageBox.Show((e.Message + ". You do not have permission to save the destination file.")) End If End If End Try 'wait for exit of Process() convertcmd.WaitForExit() 'close actual workpart theSession.Parts.Work.Close(False, Part.CloseModified.CloseModified, Nothing) 'change worpart 'find old workpart in session Dim oldWorkPart As Part = CType(theSession.Parts.FindObject(dwgName), Part) 'set link to partloadstatus Dim partLoadStatus1 As PartLoadStatus Dim partCollection_SdpsStatus1 As PartCollection.SdpsStatus 'set workpart to old workpart partCollection_SdpsStatus1 = theSession.Parts.SetDisplay(oldWorkPart, True, True, partLoadStatus1) partLoadStatus1.Dispose() 'cleanup temp files If System.IO.File.Exists(Environ("TEMP").ToString & "\" & RandomKey & ".log") = True Then System.IO.File.Delete(Environ("TEMP").ToString & "\" & RandomKey & ".log") End If 'delete temporary part-file If System.IO.File.Exists(Environ("TEMP").ToString & "\" & RandomKey & ".prt") = True Then System.IO.File.Delete(Environ("TEMP").ToString & "\" & RandomKey & ".prt") End If 'delete ug-translator summary_log-file If System.IO.File.Exists("C:\dwg\" & Environment.UserName & "\" & basename & "_summary.log") = True Then System.IO.File.Delete("C:\dwg\" & Environment.UserName & "\" & basename & "_summary.log") End If 'delete ug-translator summary_log-file If System.IO.File.Exists(partpath & "\" & basename & "_summary.log") = True Then System.IO.File.Delete(partpath & "\" & basename & "_summary.log") End If Else 'Show Message if no active Part is loaded System.Windows.Forms.MessageBox.Show("You tried to export without a loaded Part. Please load a Part first!", "No Active Part Found", System.Windows.Forms.MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Error, System.Windows.Forms.MessageBoxDefaultButton.Button1) End If End Sub End Module 'function to generate random string <== from http://www.codeproject.com Public Class RandomKeyGenerator Dim Key_Letters As String = "abcdefghijklmnopqrstuvwxyz" Dim Key_Numbers As String = "0123456789" Dim Key_Chars As Integer = 8 Dim LettersArray As Char() Dim NumbersArray As Char() Protected Friend WriteOnly Property KeyLetters() As String Set(ByVal Value As String) Key_Letters = Value End Set End Property Protected Friend WriteOnly Property KeyNumbers() As String Set(ByVal Value As String) Key_Numbers = Value End Set End Property Protected Friend WriteOnly Property KeyChars() As Integer Set(ByVal Value As Integer) Key_Chars = Value End Set End Property Function Generate() As String Dim i_key As Integer Dim Random1 As Single Dim arrIndex As Int16 Dim sb As New StringBuilder Dim RandomLetter As String LettersArray = Key_Letters.ToCharArray NumbersArray = Key_Numbers.ToCharArray For i_key = 1 To Key_Chars Randomize() Random1 = Rnd() arrIndex = -1 If (CType(Random1 * 111, Integer)) Mod 2 = 0 Then Do While arrIndex < 0 arrIndex = _ Convert.ToInt16(LettersArray.GetUpperBound(0) * Random1) Loop RandomLetter = LettersArray(arrIndex) If (CType(arrIndex * Random1 * 99, Integer)) Mod 2 <> 0 Then RandomLetter = LettersArray(arrIndex).ToString RandomLetter = RandomLetter.ToUpper End If sb.Append(RandomLetter) Else Do While arrIndex < 0 arrIndex = _ Convert.ToInt16(NumbersArray.GetUpperBound(0) * Random1) Loop sb.Append(NumbersArray(arrIndex)) End If Next Return sb.ToString End Function End Class