Creating a macro to add global equations

I am trying to add/edit global variables in Solidworks 2021 with a macro. When I use the Application.SldWorks.ActiveDoc.GetEquationMgr.Add2(0, “”“A”" = 2in", True) function this is possible, but for some reason I can’t get the get Add3 and SetEquationAndConfigurationOption to work. This is the code I’m using:

Dim SwApp As SldWorks.SldWorks
Dim swModel As SldWorks.ModelDoc2

Dim swEquationMgr As SldWorks.EquationMgr
Dim longEquation As Long

Sub main()

Set SwApp = Application.SldWorks

Set swModel = SwApp.ActiveDoc

Set swEquationMgr = swModel.GetEquationMgr
longEquation = swEquationMgr.Add2(0, “”“A”" = 2in", True)
If longEquation <> 0 Then Debug.Print “Failed to add2 a global variable assignment”

’ ^ Works

longEquation = swEquationMgr.Add3(1, “”“B”" = 2in", True, swAllConfiguration, Empty)
If longEquation <> 0 Then Debug.Print “Failed to add3 a global variable assignment”

’ ^ Doesn’t work

longEquation = swEquationMgr.SetEquationAndConfigurationOption(0, “”“A”" = 3in", swAllConfiguration, Empty)
If longEquation <> 0 Then Debug.Print “Failed to change a global variable assignment”

’ ^ Doesn’t work

End Sub

The API docs have this to say beneath the Add3 remarks. If your model only has 1 configuration, then this will fail. Otherwise, this works with a file that has multiple configurations in my testing.

image

So, if your file only has 1 configuration, it explains why Add2 works and Add3 does not. Add3 is used to specify configuration specific equations.

2 Likes

Thank you so much! Having one configurations was indeed the problem

1 Like