' ' Export active drawing to dwg in directory C:\Temp\dwg\ ' Option Strict Off 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 Dim theSession As Session = Session.GetSession() Private ERROR_FILE_NOT_FOUND As Integer = 2 Private ERROR_ACCESS_DENIED As Integer = 5 ' Dim KeyGen As RandomKeyGenerator = New RandomKeyGenerator ' Dim RandomKey As String = KeyGen.Generate() Dim K e y G e n As Random K e y G e n erator = New Random K e y G e n erator Dim RandomKey As String = K e y G e n .Generate() Sub Main If Not theSession.Parts.Work Is Nothing Then ' s.Ui.SetStatus("Setting up needed variables...") Dim workPart As Part = theSession.Parts.Work Dim displayPart As Part = theSession.Parts.Display Dim drawingSheet As DrawingSheet = workPart.DrawingSheets().CurrentDrawingSheet Dim s As UFSession = UFSession.GetUFSession() Dim cgm As UFCgm = s.cgm Static sheetName As String = CType(drawingSheet.Name, String) Static dwgName As String = CType(workPart.Leaf, String) Static fullpath As String = CType(workPart.FullPath, String) Dim p As System.IO.Path Dim partpath As String = CType(p.GetDirectoryName(fullpath), String) Dim basename As String = dwgName & "_" & sheetName Dim temp_cgm As String = Environ("TEMP").ToString & "\" & RandomKey & ".cgm" Dim temp_part_name As String = Environ("TEMP").ToString & "\" & RandomKey & ".prt" Dim export_options As UFCgm.ExportOptions Dim import_options As UFCgm.ImportOptions s.Ui.SetStatus("Exporting to temporary cgm-file...") cgm.AskSessionExportOptions(export_options) export_options.Colors = export_options.Colors.AsDisplayedColors 'export_options.Fonts = export_options.Fonts.NxFonts export_options.Fonts = export_options.Fonts.DefaultFileFonts export_options.Text_Mode = export_options.Text_Mode.TextAsCharacters export_options.Tolerance = 0.0001 export_options.Vdc_Mode = export_options.Vdc_Mode.RealVdc export_options.Widths = export_options.Widths.StandardWidths cgm.SetSessionExportOptions(export_options) If System.IO.File.Exists(temp_cgm) = True Then System.IO.File.Delete(temp_cgm) End If cgm.ExportCgm(workPart.DrawingSheets().CurrentDrawingSheet.Tag, export_options, temp_cgm) If System.IO.File.Exists(temp_part_name) = True Then System.IO.File.Delete(temp_part_name) End If s.Ui.SetStatus("Creating temporary Unigraphics part-file...") Dim basePart1 As BasePart basePart1 = theSession.Parts.NewBaseDisplay(temp_part_name, Part.Units.Millimeters) workPart = theSession.Parts.Work displayPart = theSession.Parts.Display Dim layout1 As Layout = CType(workPart.Layouts.FindObject("L1"), Layout) Dim view1 As ModelingView = CType(workPart.ModelingViews.FindObject("TOP"), ModelingView) layout1.ReplaceView(workPart.ModelingViews.WorkView, view1, True) s.Ui.SetStatus("Importing temporary cgm-file...") cgm.InitImportOptions(import_options) cgm.ImportCgm(temp_cgm, import_options) If System.IO.File.Exists(temp_cgm) = True Then System.IO.File.Delete(temp_cgm) End If workPart.ModelingViews.WorkView.Fit() s.Ui.SetStatus("Saving temporary Unigraphics part-file...") Dim partSaveStatus1 As PartSaveStatus theSession.Parts.Work.Save(Part.SaveComponents.True, Part.CloseAfterSave.False, partSaveStatus1) partSaveStatus1.Dispose() s.Ui.SetStatus("Starting conversion of temporary Unigraphics part-file...") If Not Directory.Exists("C:\Temp\dwg\" & Environment.UserName) Then Directory.CreateDirectory("C:\Temp\dwg\" & Environment.UserName) End If Dim dxfdwg As String = """" & Environ("UGII_BASE_DIR").ToString & "\dxfdwg\dxfdwg.exe""" Dim deffile As String = """d=" & Environ("UGII_BASE_DIR").ToString & "\dxfdwg\dxfdwg.def""" Dim tempfile As String = """" & Environ("TEMP").ToString & "\" & RandomKey & ".prt""" Dim logfile As String = """l=" & Environ("TEMP").ToString & "\" & RandomKey & ".log""" Dim outfile As String = """o=C:\Temp\dwg\" & Environment.UserName & "\" & basename & ".dwg""" Dim mode As String = """m=auto""" Dim args As String = tempfile & " " & outfile & " " & logfile & " " & deffile & " " & mode Dim convertcmd As New Process() Try convertcmd.StartInfo.FileName = dxfdwg convertcmd.StartInfo.Arguments = args convertcmd.StartInfo.WindowStyle = ProcessWindowStyle.Normal convertcmd.Start() Catch e As Win32Exception If e.NativeErrorCode = ERROR_FILE_NOT_FOUND Then System.Windows.Forms.MessageBox.Show((e.Message + ". Check the path.")) Else If e.NativeErrorCode = ERROR_ACCESS_DENIED Then System.Windows.Forms.MessageBox.Show((e.Message + ". You do not have permission to save the destination file.")) End If End If End Try convertcmd.WaitForExit() theSession.Parts.Work.Close(False, Part.CloseModified.CloseModified, Nothing) Dim oldWorkPart As Part = CType(theSession.Parts.FindObject(dwgName), Part) Dim partLoadStatus1 As PartLoadStatus Dim partCollection_SdpsStatus1 As PartCollection.SdpsStatus partCollection_SdpsStatus1 = theSession.Parts.SetDisplay(oldWorkPart, True, True, partLoadStatus1) partLoadStatus1.Dispose() If System.IO.File.Exists(Environ("TEMP").ToString & "\" & RandomKey & ".log") = True Then System.IO.File.Delete(Environ("TEMP").ToString & "\" & RandomKey & ".log") End If If System.IO.File.Exists(Environ("TEMP").ToString & "\" & RandomKey & ".prt") = True Then System.IO.File.Delete(Environ("TEMP").ToString & "\" & RandomKey & ".prt") End If If System.IO.File.Exists("C:\Temp\dwg\" & Environment.UserName & "\" & basename & "_summary.log") = True Then System.IO.File.Delete("C:\Temp\dwg\" & Environment.UserName & "\" & basename & "_summary.log") End If If System.IO.File.Exists(partpath & "\" & basename & "_summary.log") = True Then System.IO.File.Delete(partpath & "\" & basename & "_summary.log") End If Else 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 Random K e y G e n erator 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