Cancel
Showing results for 
Search instead for 
Did you mean: 

Export dxf or dwg with JOURNAL-MACRO

Solution Partner Genius Solution Partner Genius
Solution Partner Genius

Hi, is there a way to automate exporting of geometry from a .prt to a .dwg, filtering the selection by layers?

What I need to do is to export all geometry that is on layers 1,2 and 3 of a nx file into a dwg . And to repeat the operation for many other files.

Thank you for tips

Laurus

13 REPLIES

Re: JOURNAL-MACRO

Siemens Phenom Siemens Phenom
Siemens Phenom

Are you actually trying to MOVE geometry from a Part model file to a separate Drawing file?  Why?  In NX you have two choices, either you can create a Drawing in the existing file by simply entering the Drafting application and creating a drawing sheet and add the desired views.  While this is not the recommended way to work, it is supported none the less.

 

The preferred approach is to use what we call the Master Model approach which is where the Drawing is its own file and you add the part model or assembly as a top-level Component to the Drawing file and then add your views and such to the Drawing sheet.

 

In neither case are you actually moving the geometric objects themselves from the Part model to the Drawing file although the second approach described above is doing something close, but since you're working with adding Component parts into what is in essence a single-level Assembly, there is no actual movement of geometry from one file to another, only links between the Drawing file and the Part file(s).

John R. Baker, P.E. (ret)
EX-Product 'Evangelist'
Irvine, CA

Re: JOURNAL-MACRO

Solution Partner Genius Solution Partner Genius
Solution Partner Genius

thank you John, but maybe I you misunderstood my question,  my Engish is poor I am afraid ;-))  I need to export curves from a Nx part file to Autocad dwg format, and not from a model to drawing. I was wonderin if there is a way to automate the File Export to dxf-dwg procedure, since I have many file to convert.

Thank you for any tips

Laurus

Re: Export dxf or dwg in JOURNAL-MACRO

@Laurus I changed the subject of your message to include Export DXF or DWG to get some responses. I am sure this has been discussed before.

 

You might also try the Programming & Customization forum

Mark Rief
Retired Siemens

Re: Export dxf or dwg in JOURNAL-MACRO

Solution Partner Genius Solution Partner Genius
Solution Partner Genius

Thank you Mark, I will try the Programming and Customization Forum as you suggest

Re: Export dxf or dwg with JOURNAL-MACRO

Gears Esteemed Contributor Gears Esteemed Contributor
Gears Esteemed Contributor

Below is a journal that will export all the .prt files in a chosen folder to .dwg. It exports all the curves on layer 1-3; if there is a solid body, it will export the edges as curves. It assumes that you are running native NX, it will not work with Teamcenter.

 

Option Strict Off
Imports System
Imports System.windows.forms
Imports NXOpen
Imports NXOpen.UF

Module Module1

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

    Dim nTotalPartFiles As Integer = 0
    Dim DxfdwgDir As String

    Sub Main()

        If IsNothing(theSession.Parts.BaseWork) Then
            'active part required
            Return
        End If

        Dim workPart As Part = theSession.Parts.Work
        lw.Open()

        Const undoMarkName As String = "export to DWG"
        Dim markId1 As Session.UndoMarkId
        markId1 = theSession.SetUndoMark(Session.MarkVisibility.Visible, undoMarkName)


        DxfdwgDir = theSession.GetEnvironmentVariableValue("DXFDWG_DIR")

        If String.IsNullOrEmpty(DxfdwgDir) Then
            MsgBox("Dxfdwg settings directory not found; journal exiting.", MsgBoxStyle.Critical, "Dxfdwg settings error")
            Return
        End If

        Dim strOutputFolder As String = ""

        Try

            Dim FolderBrowserDialog1 As New FolderBrowserDialog
            ' Change the .SelectedPath property to the default location
            With FolderBrowserDialog1
                ' Desktop is the root folder in the dialog.
                .RootFolder = Environment.SpecialFolder.Desktop
                ' Change the following line to default to a given path
                .SelectedPath = "C:\"
                ' Prompt the user with a custom message.
                .Description = "Select the directory to scan"
                If .ShowDialog = DialogResult.OK Then
                    ' Display the selected folder if the user clicked on the OK button.
                    'msgbox(.SelectedPath)
                    strOutputFolder = .SelectedPath
                Else
                    'user pressed "cancel", exit the journal
                    Return
                End If
            End With

            lw.WriteLine("Export NX parts in folder to DWG")
            lw.WriteLine("Start Time: " & CType(TimeOfDay(), String))
            lw.WriteLine("")

            processParts(strOutputFolder, False)

            lw.WriteLine("")
            lw.WriteLine("Total Part Files Scanned: " & nTotalPartFiles)
            lw.WriteLine("Stop Time: " & CType(TimeOfDay(), String))

        Catch ex As NXException
            lw.WriteLine("Cycle Files and Folders Error: " & ex.Message)
            Exit Sub
        End Try
        lw.Close()

    End Sub

    Sub processParts(ByVal directoryPath As String, ByVal includeSubDirs As Boolean)

        Dim nPartFiles As Integer = 0
        Dim files() As String

        If includeSubDirs Then
            files = IO.Directory.GetFiles(directoryPath, "*.prt", IO.SearchOption.AllDirectories)
        Else
            files = IO.Directory.GetFiles(directoryPath, "*.prt", IO.SearchOption.TopDirectoryOnly)
        End If

        For Each fileName As String In files
            nPartFiles += 1
            nTotalPartFiles += 1
            lw.WriteLine("   " & nPartFiles & " " & IO.Path.GetFileName(fileName))

            Try

                ExportDwg(fileName)

            Catch ex As NXException
                lw.WriteLine("NX exception: " & ex.Message)
            Catch ex As Exception
                lw.WriteLine("Part Scan Error: " & ex.Message)
            End Try

        Next fileName

    End Sub

    Sub ExportDwg(inputPart As String)

        Dim outputDwg As String = inputPart.Substring(0, inputPart.Length - 3) & "dwg"

        Dim dxfdwgCreator1 As DxfdwgCreator
        dxfdwgCreator1 = theSession.DexManager.CreateDxfdwgCreator()

        With dxfdwgCreator1
            .SettingsFile = IO.Path.Combine(DxfdwgDir, "dxfdwg.def")
            .ProcessHoldFlag = True
            .ExportFrom = DxfdwgCreator.ExportFromOption.ExistingPart
            .InputFile = inputPart
            .OutputFileType = DxfdwgCreator.OutputFileTypeOption.Dwg
            .OutputFile = outputDwg
            .ViewEditMode = True
            .ExportData = DxfdwgCreator.ExportDataOption.Modeling
            .ObjectTypes.Curves = True
            .ObjectTypes.Annotations = True
            .AutoCADRevision = DxfdwgCreator.AutoCADRevisionOptions.R2010
            .FlattenAssembly = True
            .WidthFactorMode = DxfdwgCreator.WidthfactorMethodOptions.AutomaticCalculation
            .LayerMask = "1-3"
            .ViewList = "Top"
        End With

        Dim nXObject1 As NXObject
        nXObject1 = dxfdwgCreator1.Commit()

        dxfdwgCreator1.Destroy()

    End Sub

    Public Function GetUnloadOption(ByVal dummy As String) As Integer

        'Unloads the image immediately after execution within NX
        GetUnloadOption = NXOpen.Session.LibraryUnloadOption.Immediately

    End Function

End Module

Re: Export dxf or dwg with JOURNAL-MACRO

Solution Partner Genius Solution Partner Genius
Solution Partner Genius

thank you very much, it seems perfect

Re: Export dxf or dwg with JOURNAL-MACRO

Genius
Genius

Here is a python version that worked for me. Note: There might be some additional attributes or options that you have to set but this basic pattern.

def exportDXF(self):
    
        global dxf_cntr
        bodies = workPart.Bodies
        your_directoryname="C:\\yourdirectory"
        dxf_list=[]
        
        dxf_cntr=0

        #get the bodies to export to dxf and append to a list
        for body in bodies:
            
            #get the Journal Identifier for each body
            dxflst.append(body.JournalIdentifier)
            
            dxf_cntr+=1

        #call the DXF method
        for i in range(0, dxf_cntr):

             
            dxfdwgCreator1 = theSession.DexManager.CreateDxfdwgCreator()
            dxfdwgCreator1.ExportData = NXOpen.DxfdwgCreator.ExportDataOption.Drawing
           
    
            dxfdwgCreator1.OutputFile = str(your_directoryname)
    
            dxfdwgCreator1.ExportData = NXOpen.DxfdwgCreator.ExportDataOption.Modeling
    
            dxfdwgCreator1.ExportSelectionBlock.SelectionScope = NXOpen.ObjectSelector.Scope.SelectedObjects
    
            
            #input file name
            dxfdwgCreator1.InputFile = "C:\\Yourinputfile"
            # output file name
            dxfdwgCreator1.OutputFile = str(save_dir)+".dxf"
            
            # ----------------------------------------------
            #   Dialog Begin AutoCAD DXF/DWG Export Wizard
            # ----------------------------------------------

            dxf_body=workPartBodies.FindObject(str(dxf_list[i]))
            
            dxf_objects[0] = dxf_body
    
            dxfdwgCreator1.Commit()
    
            dxfdwgCreator1.Destroy()

Re: Export dxf or dwg with JOURNAL-MACRO

Solution Partner Genius Solution Partner Genius
Solution Partner Genius

Smiley Happy thank you

 

Re: Export dxf or dwg with JOURNAL-MACRO

Builder
Builder
Hi Guys. I need help related to this discussion and i was hoping someone has a solution. I need to export a large number of draft files to AutoCAD .dwg. Does someone have an automated method to do this a batch file or a journal? I would really apreciate any feedback.
NX12.0.2.9 Win10 64bit, Tecnomatix PS V14.1.