NX二次开发之VB.NET(指定图层)批量导出PART为STL

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
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值