Add equation to dimension using SOLIDWORKS API

Edit ArticleEdit Article

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
Equation in dimension

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
            MsgBox "Please select dimension"
        End If
        MsgBox "Please open model"
    End If
End Sub


All articles and code at CodeStack are now open-source and hosted on GitHub. If you want to contribute by modifying existing articles and code snippets, submitting new ones, reporting errors and bugs etc. please follow this blog post for more information. We appreciate any contribution.

Product of Xarial Product of Xarial