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
Return
End If
Dim workPart As Part = theSession.Parts.Work
lw.Open()
Dim theSolids As New List(Of Body)
For Each temp As Body In workPart.Bodies
If temp.IsSolidBody andalso temp.Layer = 1 Then
theSolids.Add(temp) '添加实体
End If
Next
Try
ExportSTLFiles(workPart.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
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
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 & " ...") 'UI界面显示创建信息
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 ...") 'UI界面显示生成信息
suffix += 1
Next
End Sub
Public Function GetUnloadOption(ByVal dummy As String) As Integer
GetUnloadOption = NXOpen.Session.LibraryUnloadOption.Immediately
End Function
End Module
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
Return
End If
Dim workPart As Part = theSession.Parts.Work
lw.Open()
Dim theSolids As New List(Of Body)
For Each temp As Body In workPart.Bodies
If temp.IsSolidBody andalso temp.Layer = 1 Then
theSolids.Add(temp) '添加实体
End If
Next
Try
ExportSTLFiles(workPart.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
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
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 & " ...") 'UI界面显示创建信息
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 ...") 'UI界面显示生成信息
suffix += 1
Next
End Sub
Public Function GetUnloadOption(ByVal dummy As String) As Integer
GetUnloadOption = NXOpen.Session.LibraryUnloadOption.Immediately
End Function
End Module