5 Replies Latest reply on Mar 28, 2018 5:11 PM by David Mandl

    Delete equation

    Chris Johnson

      Hi All,

      Im trying to delete an equation but cant get it working.

       

      does anyonwe see where im going wrong?

       

      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)

           ' This returns: "D1@Sketch1" = 7

          

                 If swEqnMgr.Equation(i) = """D1@Sketch1""=" & "=" & "7" Then

       

                      swEqnMgr.Suppression(i) = True 'suppress To delete the Equation

                      swEqnMgr.Delete

                 

                  End If

          Next i

      End Sub

       

      Thank you!

      Chris

        • Re: Delete equation
          Keith Rice

          Chris,

           

          First of all, I'm not sure what version of SolidWorks you're using, but IEquationMgr::Suppression is not longer available as of 2014 since they removed the ability to suppress equations since equations are now configurable.

           

          Anyway you shouldn't need to supress to delete. Aside from removing the suppression line, I used Chr(34) to represent a quote since it makes the code more readable IMO. Comparing our versions, it looks like you are trying to delete a equation that doesn't exist given where you have placed the quotes.

           

          =====

           

          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.Equation(i) = Chr(34) & "D1@Sketch1" & Chr(34) & "= 7" Then

                      Debug.Print swEqnMgr.delete(i)

                  End If

              Next i

          End Sub

           

          =====

           

          Hope this helps.

           

          Keith

          SolidWorks API Training

            • Re: Delete equation
              Chris Johnson

              Thanks Keith,

               

              additional tweaking has this working:

               

              Sub main()

                 

                  Dim swApp                   As SldWorks.SldWorks

                  Dim swModel                 As SldWorks.ModelDoc2

                  Dim swEqnMgr                As SldWorks.EquationMgr

                  Dim i                       As Long

                  Dim EQ                      As Variant

                 

                 

                  Set swApp = Application.SldWorks

                  Set swModel = swApp.ActiveDoc

                  Set swEqnMgr = swModel.GetEquationMgr

               

                  For i = i To swEqnMgr.GetCount - 1

               

                     Debug.Print EQ

                      EQ = swEqnMgr.Equation(i)

                      If EQ = """D1@Sketch1"" = 7" Then

                      msgbox "found it"

                          'swEqnMgr.Delete(i)

                          End If

                  Next i

              End Sub

               

              but i cannot get this one working...

               

              here is the line that i used to add the EQ

               

              MyEquation = swEquationMgr.Add2(0, """D3@ID Chamfer1""  = " & "((""WALL@LAYOUT""" & " / 2) " & " - .03)", True)

               

               

              Also,  I pasted your version with the Chr(34) into my test project but it didnt work.

               

              Thank you!

              Chris

              • Re: Delete equation
                David Mandl

                This is excellent.

                 

                Next step... applying this to a loop to delete all "dangling" equations...