Cancel
Showing results for 
Search instead for 
Did you mean: 

Export bodies to STL in Block Styler

Creator
Creator

Hi All,

 

I am using NX 11. In block styler i want to create an action button that will export to the -prt( prt bodies) to stl format.

Can anyone help me in how to create this with my program code?

 

PS: I tried to add and paste the journal code but did not succeed and did not work.

 

Thanks in advance for the information.

 

Mate

 

4 REPLIES

Re: Export bodies to STL in Block Styler

Phenom
Phenom

Hi @matejku92,

have you the license to export to STL ?

Thank you...

Using NX 11 / RuleDesigner PDM

Re: Export bodies to STL in Block Styler

Creator
Creator

Hi @cubalibre00,


Yes, I have the license.

 

 

Highlighted

Re: Export bodies to STL in Block Styler

Phenom
Phenom

Hi @matejku92,

here the 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
Thank you...

Using NX 11 / RuleDesigner PDM

Re: Export bodies to STL in Block Styler

Creator
Creator

Hi @cubalibre00

 

Thank you for your early reply! A big thanks for the code. It will help a lot for me in the future. 

 

Thank you for your help.