Files
Read, add, delete, and modify equations Associated part
Code
Module: mMain
'Demonstrates how to read, add, delete, and modify equations using IEquationMgr.
'Preconditions: EquationMgr.SLDPRT is open in SolidWorks 2013 or later.
'Result:
'1. Get equations and print them to immediate window
'2. Delete global variable named "angle"
'3. Modify the value of global variable "factor"
'4. Add global variable called "diameter"
'Notes:
'-Equation list is 0-based
'-Variable names and dimension names must be in quotes. You can use _
Chr(34) to include a double quote in a string.
'-You cannot rename a variable that is being used in other equations. Instead, _
you must first delete all equations that contain that variable, rename _
the global variable, and then re-add the other equations with the new name.
'-If you delete a variable that used in another equation, that equation will _
fail to solve. You can verify the status of every equation by calling _
IEquationMgr::Value & then examining the return value of IEquationMgr::Status.
'-In 2013 and earlier, IEquationMgr::Add2 returns -1 when the last argument is _
False, even if the equation is successfully added. As a workaround, use True _
for the last argument.
'-Adding an equation will fail if any other equations have errors or warnings.
Dim swApp As SldWorks.SldWorks
Dim swModel As SldWorks.ModelDoc2
Dim swEqnMgr As SldWorks.EquationMgr
Dim i As Integer
Dim vSplit As Variant
Sub main()
Set swApp = Application.SldWorks
Set swModel = swApp.ActiveDoc
Set swEqnMgr = swModel.GetEquationMgr
'1. Get equations and print them to immediate window
'Note: If you only want to get the equation value, use IEquationMgr::Value
For i = 0 To swEqnMgr.GetCount - 1
Debug.Print swEqnMgr.Equation(i)
Next i
'2. Delete global variable named "angle"
For i = 0 To swEqnMgr.GetCount - 1
vSplit = Split(swEqnMgr.Equation(i), "=")
If InStr(vSplit(0), "angle") <> 0 Then
swEqnMgr.Delete i
Exit For 'exit the for loop
End If
Next i
'Verify that each equation can still successfully execute, otherwise _
delete that equation.
For i = 0 To swEqnMgr.GetCount - 1
Debug.Print swEqnMgr.Value(i)
If swEqnMgr.Status = -1 Then
swApp.SendMsgToUser swEqnMgr.Equation(i) & vbCrLf & _
"This equation no longer evaluates and will be deleted."
swEqnMgr.Delete i
End If
Next i
'3. Modify global variable "factor".
For i = 0 To swEqnMgr.GetCount - 1
vSplit = Split(swEqnMgr.Equation(i), "=")
If InStr(vSplit(0), "factor") <> 0 Then
swEqnMgr.Equation(i) = Replace(swEqnMgr.Equation(i), vSplit(1), "1.2")
Exit For 'exit the for loop
End If
Next i
'4. Add global variable called "diameter"
If swEqnMgr.Add2(-1, Chr(34) & "diameter" & Chr(34) & " = " & _
Chr(34) & "D1@BaseSketch" & Chr(34), True) = -1 Then
swApp.SendMsgToUser "Failed to add equation."
End If
swModel.EditRebuild3
End Sub