Showing results for 
Search instead for 
Did you mean: 

NX Journal: Export bodies to stl

Solution Partner Creator Solution Partner Creator
Solution Partner Creator



I managed to prepare simple journal based on codes from forum.

For now it is exporting all bodies to separate *.stl files with names of each body.

I failed to make jorunal working only for visibile or seleted bodies.


Any help would be appreciated






Option Strict Off
Imports System
Imports System.Collections.Generic
Imports NXOpen
Imports NXOpen.Features
Imports NXOpen.UF
Module Module2
    Dim theSession As Session = Session.GetSession()
    Dim theUfSession As UFSession = UFSession.GetUFSession()
    Dim lw As ListingWindow = theSession.ListingWindow
    Sub Main()
        If IsNothing(theSession.Parts.BaseWork) Then
        'active part required
        End If
    Dim displayPart As Part = theSession.Parts.Display
        Const undoMarkName As String = "export solids to STL"
        Dim markId1 As Session.UndoMarkId
        markId1 = theSession.SetUndoMark(Session.MarkVisibility.Visible, undoMarkName)
        Dim theSolids As New List(Of Body)
        'collect the solid bodies in the work part
        For Each temp As Body In displayPart.Bodies
            If temp.IsSolidBody Then
            End If
            ExportSTLFiles(displayPart.FullPath, theSolids, 0.003, 0.003)
        Catch ex As NXException
            lw.WriteLine("NX Error: " & ex.Message)
        Catch ex As Exception
            lw.WriteLine("Error: " & ex.Message)
        End Try

    End Sub
'export separate bodies
    Function ExportSTLFiles(ByVal FileName As String, ByVal theObjects As List(Of Body), ByVal triangleTolerance As Double, ByVal adjacencyTolerance As Double)
        Dim NumErrors As Integer
        Dim InfoError() As UFStd.StlError
        Dim Header, FileBaseName As String
        'Dim numNegated As Integer
        'Dim Negated() As Tag
        'Negated = Nothing
        InfoError = Nothing
        Dim parentFolder As String = IO.Path.GetDirectoryName(FileName)

        'FileName = IO.Path.ChangeExtension(FileName, ".stl")

        For Each temp As Body In theObjects
            Dim FileHandle As IntPtr
            Dim CurrentName As String = temp.Name()
            Dim finalName As String = CurrentName & ".stl"
            FileBaseName = IO.Path.GetFileName(FileName)
            Header = "Header: " & FileBaseName
            theUfSession.Std.OpenBinaryStlFile(IO.Path.Combine(parentFolder, finalName), False, Header, FileHandle)
            theUfSession.Ui.SetPrompt("Creating file ... " & FileBaseName & " ...")
            theUfSession.Std.PutSolidInStlFile(FileHandle, Tag.Null, temp.Tag, 0.0, 0.0, triangleTolerance, NumErrors, InfoError)
            theUfSession.Ui.SetStatus("File ... " & FileBaseName & " generated ...")
    End Function
    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: NX Journal: Export bodies to stl

Honored Contributor
Honored Contributor

Here is one method for getting the visible solid bodies (bodies that are not hidden or on a hidden layer).

Re: NX Journal: Export bodies to stl

Solution Partner Creator Solution Partner Creator
Solution Partner Creator

Works like a charm!


Thank You.