SW批量导出方程式

1 篇文章 0 订阅

批量导出方程式

程序逻辑:

遍历文件夹及子文件夹下所有模型文件–>打开模型文件–>获取所有方程式–>输出到文本文件

代码如下:

1.遍历文件夹及子文件夹

    Private Sub GetAllFiles(ByVal strDirect As String)  '搜索所有目录下的文件
If Not (strDirect Is Nothing) Then
            Dim mFileInfo As System.IO.FileInfo
            Dim mDir As System.IO.DirectoryInfo
            Dim mDirInfo As New System.IO.DirectoryInfo(strDirect)
  	    Try
                For Each mFileInfo In mDirInfo.GetFiles("*.SLDPRT")
                    If Microsoft.VisualBasic.Left(Path.GetFileName(mFileInfo.FullName), 2) <> "~$" Then
                        ExportEquation(mFileInfo.FullName, 1)
                    End If
                Next
                For Each mFileInfo In mDirInfo.GetFiles("*.SLDASM")
                    If Microsoft.VisualBasic.Left(Path.GetFileName(mFileInfo.FullName), 2) <> "~$" Then
                        ExportEquation(mFileInfo.FullName, 2)
                    End If
                Next
                For Each mDir In mDirInfo.GetDirectories
                    GetAllFiles(mDir.FullName)
                Next
            Catch ex As System.IO.DirectoryNotFoundException
                Debug.Print("目录没找到:" + ex.Message)
            End Try
        End If
    End Sub

2.导出所有方程式

    Private Sub ExportEquation(filename As String, type As Integer) '导出方程式
        Dim txtFilePath As String
        Dim Openerror As Integer
        Dim equationTxt As String
        txtFilePath = Path.GetDirectoryName(filename) + "\" + Path.GetFileNameWithoutExtension(filename) + "-equation.txt"
        swApp.Visible = False
        swModel = swApp.OpenDoc2(filename, type, False, False, False, Openerror)
        If Not swModel Is Nothing Then
            swModel.ClearSelection2(True)
            Dim swEqnMgr As EquationMgr
            Dim i As Integer
            Dim nCount As Integer
            swEqnMgr = swModel.GetEquationMgr
            nCount = swEqnMgr.GetCount
            equationTxt = ""
            For i = 0 To nCount - 1
                Dim t As String
                If swEqnMgr.GlobalVariable(i) Then
                    t = swEqnMgr.Equation(i)
                    equationTxt = equationTxt + t + vbCrLf
                End If
            Next i
            WriteEquationOut(txtFilePath, equationTxt)
            LinktoEquation(txtFilePath)
            swModel.Save2(True)
            swApp.CloseDoc(swModel.GetTitle)
        End If
        swApp.Visible = True
    End Sub

写入文本文件

    Private Sub WriteEquationOut(txtFilePath As String, equationTxt As String)
        Dim fs As New System.IO.FileStream(txtFilePath, IO.FileMode.Create, IO.FileAccess.Write)
        Dim sw As New System.IO.StreamWriter(fs)
        sw.Write(equationTxt)
        sw.Close()
    End Sub

总代码

界面

文本框 ->textbox1

代码

Imports SldWorks
Imports SwConst
Imports System.IO
Public Class Form2
    Dim swModel As ModelDoc2
    Dim pointArray As Object
    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click '点击导出
        Dim strPath As String
        If TextBox1.Text = "" Then
            MsgBox("请输入根目录")
            Exit Sub
        End If
        strPath = TextBox1.Text + "\"
        getSW()
        If swApp Is Nothing Then
            MsgBox("请打开一个SoliWorks应用程序")
            Exit Sub
        End If
        GetAllFiles(strPath)
        MsgBox("批量导出方程式完成!")
    End Sub
    Private Sub GetAllFiles(ByVal strDirect As String)  '搜索所有目录下的文件
        If Not (strDirect Is Nothing) Then
            Dim mFileInfo As System.IO.FileInfo
            Dim mDir As System.IO.DirectoryInfo
            Dim mDirInfo As New System.IO.DirectoryInfo(strDirect)
            Try
                For Each mFileInfo In mDirInfo.GetFiles("*.SLDPRT")
                    If Microsoft.VisualBasic.Left(Path.GetFileName(mFileInfo.FullName), 2) <> "~$" Then
                        ExportEquation(mFileInfo.FullName, 1)
                    End If
                Next
                For Each mFileInfo In mDirInfo.GetFiles("*.SLDASM")
                    If Microsoft.VisualBasic.Left(Path.GetFileName(mFileInfo.FullName), 2) <> "~$" Then
                        ExportEquation(mFileInfo.FullName, 2)
                    End If
                Next
                For Each mDir In mDirInfo.GetDirectories
                    GetAllFiles(mDir.FullName)
                Next
            Catch ex As System.IO.DirectoryNotFoundException
                Debug.Print("目录没找到:" + ex.Message)
            End Try
        End If
    End Sub
    Private Sub ExportEquation(filename As String, type As Integer) '导出方程式
        Dim txtFilePath As String
        Dim Openerror As Integer
        Dim equationTxt As String
        txtFilePath = Path.GetDirectoryName(filename) + "\" + Path.GetFileNameWithoutExtension(filename) + "-equation.txt"
        swApp.Visible = False
        swModel = swApp.OpenDoc2(filename, type, False, False, False, Openerror)
        If Not swModel Is Nothing Then
            swModel.ClearSelection2(True)
            Dim swEqnMgr As EquationMgr
            Dim i As Integer
            Dim nCount As Integer
            swEqnMgr = swModel.GetEquationMgr
            nCount = swEqnMgr.GetCount
            equationTxt = ""
            For i = 0 To nCount - 1
                Dim t As String
                If swEqnMgr.GlobalVariable(i) Then
                    t = swEqnMgr.Equation(i)
                    equationTxt = equationTxt + t + vbCrLf
                End If
            Next i
            WriteEquationOut(txtFilePath, equationTxt)
            LinktoEquation(txtFilePath)
            swModel.Save2(True)
            swApp.CloseDoc(swModel.GetTitle)
        End If
        swApp.Visible = True
    End Sub
    Private Sub LinktoEquation(txtFilePath As String) '设置模型链接外部文件
        getSW()
        swModel = swApp.ActiveDoc
        Dim swEqnMgr As EquationMgr
        Dim i As Integer
        Dim nCount As Integer
        swEqnMgr = swModel.GetEquationMgr
        nCount = swEqnMgr.GetCount
        swEqnMgr.LinkToFile = True
        swEqnMgr.FilePath = txtFilePath
        For index = 0 To nCount - 1
            swEqnMgr.LinkToFile = True
            swEqnMgr.UpdateValuesFromExternalEquationFile()
        Next
    End Sub
    Private Sub WriteEquationOut(txtFilePath As String, equationTxt As String) '写入文本文件
        Dim fs As New System.IO.FileStream(txtFilePath, IO.FileMode.Create, IO.FileAccess.Write)
        Dim sw As New System.IO.StreamWriter(fs)
        sw.Write(equationTxt)
        sw.Close()
    End Sub
    Private Sub Button4_Click(sender As Object, e As EventArgs) Handles Button4.Click '浏览
        Dim dialog As New System.Windows.Forms.FolderBrowserDialog()
        dialog.Description = "请选择文件夹"
        If dialog.ShowDialog() = DialogResult.OK Then
            TextBox1.Text = dialog.SelectedPath
        End If
    End Sub
    Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click '关闭
        End
    End Sub
End Class
SW零件批量转STEP插件是一种用于SolidWorks软件的工具,它可以帮助用户将多个SW零件快速转换为STEP文件格式。 SolidWorks是一种常用的三维计算机辅助设计软件,用于创建和编辑零件和装配图。然而,在某些情况下,用户可能需要将SW零件转换为其他文件格式,例如STEP(Standard for the Exchange of Product data)格式,以便与其他CAD软件进行交互或共享设计文件。这时候,SW零件批量转STEP插件就发挥了重要作用。 SW零件批量转STEP插件提供了一种快速、高效的方法,可以同时处理多个SW零件并将其转换为STEP文件。用户只需将待转换的SW零件添加到插件中的列表中,然后设置转换选项,如输出文件路径和文件命名规则等。一旦设置完成,插件将自动执行转换过程,将每个SW零件转换为相应的STEP文件。 这个插件的好处是它可以大大节省用户的时间和精力。相比手动逐个进行零件转换,批量转换的功能可以一次性处理多个零件,提高工作效率。此外,使用插件进行批量转换还能减少因人为疏忽而导致的错误,确保转换结果的准确性和一致性。 总之,SW零件批量转STEP插件是一款功能强大的工具,它提供了高效快速的方法将多个SW零件转换为STEP文件。它为用户节省时间和精力,提高工作效率,同时确保转换结果的准确性。对于需要与其他CAD软件进行交互或共享设计文件的用户来说,这个插件无疑是一项宝贵的资源。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值