批量导出方程式
程序逻辑:
遍历文件夹及子文件夹下所有模型文件–>打开模型文件–>获取所有方程式–>输出到文本文件
代码如下:
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
总代码
界面
代码
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