Option Strict Off
Imports System
Imports System.Collections.Generic
Imports NXOpen
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
lw.Open()
Const undoMarkName As String = "export solids to STL"
Dim markId1 As Session.UndoMarkId
markId1 = theSession.SetUndoMark(Session.MarkVisibility.Invisible, undoMarkName)
Dim theSolids As New List(Of Body)
'collect the solid bodies in the work part
For Each temp As Body In workPart.Bodies
If temp.IsSolidBody Then
theSolids.Add(temp)
End If
Next
Try
ExportSTLFiles(workPart.FullPath, theSolids, 0.01, 0.01)
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
Sub ExportSTL(ByVal FileName As String, ByVal theObjects As List(Of Body), ByVal triangleTolerance As Double, ByVal adjacencyTolerance As Double)
Dim NumErrors As Integer
Dim FileHandle As IntPtr
Dim InfoError() As UFStd.StlError
Dim Header, FileBaseName As String
'Dim numNegated As Integer
'Dim Negated() As Tag
'Negated = Nothing
InfoError = Nothing
FileName = IO.Path.ChangeExtension(FileName, ".stl")
FileBaseName = IO.Path.GetFileName(FileName)
Header = "Header: " & FileBaseName
theUfSession.Std.OpenBinaryStlFile(FileName, False, Header, FileHandle)
theUfSession.Ui.SetPrompt("Creating file ... " & FileBaseName & " ...")
For Each temp As Body In theObjects
If temp.IsSolidBody Then
theUfSession.Std.PutSolidInStlFile(FileHandle, Tag.Null, temp.Tag, 0.0, 0.0, triangleTolerance, NumErrors, InfoError)
End If
Next
theUfSession.Std.CloseStlFile(FileHandle)
theUfSession.Ui.SetStatus("File ... " & FileBaseName & " generated ...")
End Sub
Sub 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)
Dim fileNameNoExt As String = IO.Path.GetFileNameWithoutExtension(FileName)
'FileName = IO.Path.ChangeExtension(FileName, ".stl")
Dim suffix As Integer = 1
For Each temp As Body In theObjects
Dim FileHandle As IntPtr
Dim fileNameWithSuffix As String = fileNameNoExt & "_" & suffix.ToString & ".stl"
FileBaseName = IO.Path.GetFileName(FileName)
Header = "Header: " & FileBaseName
theUfSession.Std.OpenBinaryStlFile(IO.Path.Combine(parentFolder, fileNameWithSuffix), 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 ...")
suffix += 1
Next
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