Skip to main content

Add equation to dimension using SOLIDWORKS API

This example will modify the value of the selected dimension and sets its value to be equal to the equation using SOLIDWORKS API:

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

Equation in dimension{ width=320 height=200 }

IEquationMgr SOLIDWORKS API interface should be used to manage equations in SOLIDWORKS document.

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 "Please select dimension"
End If

Else
MsgBox "Please open model"
End If

End Sub