VBA - force execution of equations

Hello everyone,

I've been looking for a snippet of VBA code for solidworks 2024 that does the following:
Click on " Tools " → " Equations " → " OK " in the equations window, the goal being to force the execution of my equations that does not update correctly with the rebuild but with this method, it is to integrate it into a larger code triggered from one more assembly at the level.

Thank you in advance if you have the info.
Guillaume.

I have this code that coresponds to a deep reconstruction (Shift + CTR + Q).
Reconstruction that is not identical if you run a traditional VBA function (seen with Visiativ).
Now on SW 2023 it worked very well.

'Lance l'ID pour le racourcis CTRL+SHIFT+Q = reconstruction profonde
Sub ForceRebuildSD()
    Dim swApp As SldWorks.SldWorks
    Dim swModel As SldWorks.ModelDoc2
    
    Set swApp = Application.SldWorks
    Set swModel = swApp.ActiveDoc
    
    If swModel Is Nothing Then
        MsgBox "Aucun document ouvert.", vbExclamation
        Exit Sub
    End If
    
    ' Ctrl+Shift+Q via ID de commande exact (SolidWorks 2023)
    swApp.RunCommand 3311, ""
    
End Sub

If non-functional, it is possible to find the exact order ID for equation and then validate.

Bonjour;

Les API de Solidworks proposent ceci:

'-----------------------------------------------------
' Preconditions:
' 1. Verify that the specified part to open exists.
' 2. Open the Immediate window.
'
' Postconditions:
' 1. Sets the equation's Automatic solve order option
'    to true, which results in the model's
'    independent equations executing before
'    dependent equations, if any.
' 2. Examine the Immediate window.
'
' NOTE: Because this model is used elsewhere,
' do save changes.
'-----------------------------------------------------
Option Explicit

Dim swApp As SldWorks.SldWorks
Dim swModel As SldWorks.ModelDoc2
Dim swEquationMgr As SldWorks.EquationMgr
Dim fileName As String
Dim errors As Long, warnings As Long

Sub main()

    Set swApp = Application.SldWorks
    fileName = "C:\Users\Public\Documents\SOLIDWORKS\SOLIDWORKS 2018\samples\tutorial\api\partEquations.sldprt"
    Set swModel = swApp.OpenDoc6(fileName, swDocumentTypes_e.swDocPART, swOpenDocOptions_e.swOpenDocOptions_Silent, "", errors, warnings)
    Set swEquationMgr = swModel.GetEquationMgr
    'Make sure that this model includes equations
    'by getting the number of equations
    Debug.Print "Number of equations: " & swEquationMgr.GetCount
        If swEquationMgr.GetCount >= 1 Then
            'Get whether equations are automatically
            'solved in sequence
            Debug.Print "  Are equations automatically solved in sequence? " & swEquationMgr.AutomaticSolveOrder
            If swEquationMgr.AutomaticSolveOrder Then
                Exit Sub
            Else
                'Automatically solve equations in sequence
                swEquationMgr.AutomaticSolveOrder = True
                Debug.Print "  Are equations automatically solved in sequence? " & swEquationMgr.AutomaticSolveOrder
            End If
        Else
            Debug.Print "No equations included with this model."
        End If

End Sub
1 Like

Thank you! For the rebuild I tried everything but it doesn't work the way I want. I'm rebuilding from a head assembly and at a certain level of tree structure it doesn't behave the way I want. On the other hand, as soon as I open the equation module of the element concerned and validate everything becomes correct. And I'm looking for this operation to be carried out not like that but by an operator who must use my configurator without asking these kinds of questions. He will not even necessarily be able to identify the parts that have not been recalculated. And when I try with macro recording, it doesn't get any code out of my operations...

Thank you! I did indeed come across this page too. It's not necessarily what I'm looking for, but I'm thinking about whether I can use it in a roundabout way. Will he necessarily run the equations he finds by using it? I can't be sure.

Otherwise here is attached a macro to know the ID of an order. (Thanks CodeStack)
Capturing commands.swp (39.5 KB)

Just throw the hand into Capturing_commands1.
Then press on your equation menu to discover the hidden ID of the function, here the ID=51 for your menu (sw2023).
Then you run the code to launch the ID in LaunchCommand:

Sub LancerCommande()
    Dim swApp As SldWorks.SldWorks
    Dim swModel As SldWorks.ModelDoc2
    
    Set swApp = Application.SldWorks
    Set swModel = swApp.ActiveDoc
    
    If swModel Is Nothing Then
        MsgBox "Aucun document ouvert.", vbExclamation
        Exit Sub
    End If
    
    ' Ctrl+Shift+Q via ID de commande exact (SolidWorks 2023)
    swApp.RunCommand 51, ""
    
End Sub

Tested on SW2023, the equation window opens and closes.

Thank you very much for your tool. I admit that I am not an expert in the field or am sure I understand what I should do.
I ran your macro in my room, it opens the equation window but it doesn't close it. And I can't get the ID of the operation that clicks on OK.

Indeed I just tried again and the window is not validated.
And no existing order to do ok...
You can test this, by adding:

swApp.RunCommand 961, "" 'A la suite de swApp.RunCommand 51...

This makes an ESC (maybe enough for your case because it is usually enough to open this window to solve this known bug of the equations (even by doing canceled, like here)

If it's not functional, test with:

DoEvents
SendKeys "~"   ' ENTER = OK

Do not test from the editor otherwise the entry is done in the editor and not in the SW window... Be careful, very average solution for the 2nd (but probably functional)

Be careful, I don't know if it does it at home, but, for me, the " SendKeys " command, in vba, has the annoying habit of disabling the Num-Lock key (and therefore no more numeric keypad ...