跳到主要内容

使用SOLIDWORKS API为尺寸添加方程

该示例将使用SOLIDWORKS API修改所选尺寸的值,并将其值设置为等于方程:

sin(0.5) * 2 + (10 - 5)

尺寸中的方程{ width=320 height=200 }

应使用SOLIDWORKS API接口IEquationMgr来管理SOLIDWORKS文档中的方程。

Dim swApp As SldWorks.SldWorks
Dim swModel As SldWorks.ModelDoc2
Dim swSelMgr As SldWorks.SelectionMgr
Dim swEqMgr As SldWorks.EquationMgr

Const EQUATION = "sin(0.5) * 2 + (10 - 5)"

Sub main()

Set swApp = Application.SldWorks

Set swModel = swApp.ActiveDoc

If Not swModel Is Nothing Then

Set swSelMgr = swModel.SelectionManager

Dim swDispDim As SldWorks.DisplayDimension

Set swDispDim = swSelMgr.GetSelectedObject6(1, -1)

If Not swDispDim Is Nothing Then

Set swEqMgr = swModel.GetEquationMgr

Dim formula As String

formula = """" & swDispDim.GetNameForSelection & """ = " & EQUATION

swEqMgr.Add2 -1, formula, True

Else
MsgBox "请选择尺寸"
End If

Else
MsgBox "请打开模型"
End If

End Sub