3 Replies Latest reply on Mar 28, 2018 6:21 PM by Rob Edwards

    Deleting All Dangling Equations:  Macro Isn't Looping

    David Mandl

      I'm piggy-backing off of a previous discussion here:  https://forum.solidworks.com/thread/80102

       

      It looked like a good template for a situation that I'm dealing with:

      • Have equations in a model
      • Delete some features that reference equations (a sketch dimension linked to another value by an equation, for example)
      • SW doesn't delete the "dangling" equations, leaves them as invalid
      • I want to use a macro to cycle through all equations in a file, delete any that are invalid.

       

      Based on what I saw there, I thought that this macro would work:

      Sub main()

       

          Dim swApp As SldWorks.SldWorks

          Dim swModel As SldWorks.ModelDoc2

          Dim swEqnMgr As SldWorks.EquationMgr

          Dim i As Long

         

          Set swApp = Application.SldWorks

          Set swModel = swApp.ActiveDoc

          Set swEqnMgr = swModel.GetEquationMgr

       

          For i = i To swEqnMgr.GetCount - 1

              Debug.Print swEqnMgr.Equation(i)

              Debug.Print swEqnMgr.Value(i)

              If swEqnMgr.Status = -1 Then

                  Debug.Print swEqnMgr.Delete(i)

              End If

          Next i

       

      End Sub

       

      And it did.  Sort of.  I tested it on a file that had these equations:

      When I run the above macro, only the first equation gets deleted.  If I run the macro again, the second equation gets deleted.

       

      So it works.  But my "For i..." loop isn't functioning as I thought it would.  I'm guessing that it's because the id numbers of the equations adjust after I delete one of them (i=1 becomes i=0, etc.).

       

      Any thoughts on how best to combat this?