Showing results for 
Search instead for 
Do you mean 
Reply
Solved! Go to solution

NX Journal: Export bodies to stl

Hello!

 

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

 

Regards,

Sebastian

 

Code:


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
        Return
        End If
 
    Dim displayPart As Part = theSession.Parts.Display
    lw.Open()
 
        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
                theSolids.Add(temp)
            End If
        Next
 
        Try
            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

        lw.Close()
 
    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.Std.CloseStlFile(FileHandle)
 
            theUfSession.Ui.SetStatus("File ... " & FileBaseName & " generated ...")
 
            
 
        Next
 
    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

2 REPLIES
Solution
Solution
Accepted by topic author SebatianP
3 weeks ago

Re: NX Journal: Export bodies to stl

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

Works like a charm!

 

Thank You.

Sebastian