2 Replies Latest reply on Aug 22, 2013 9:50 AM by Yong Ning

    How to delete the equation at the specified index

    Yong Ning

      Hope delete select 1,2,4 Item Equation

      0.jpg

       

      record Macro ,the code is

       

        Set swApp = Application.SldWorks 

        Set Part = swApp.ActiveDoc

        Set SelMgr = Part.SelectionManager

        boolstatus = Part.Extension.SelectByID2("Equations", "EQNFOLDER", 0, 0, 0, False, 0, Nothing, 0)

        Part.DeleteAllRelations

      End Sub

       

      The Code result is

       

      1.jpg

      2013 SolidWorks API Help - Pack and Go Part and Linked Equation (VBA)

      http://help.solidworks.com/2013/English/api/sldworksapi/pack_and_go_part_and_linked_equation_example_vb.htm

       

       

      2013 SolidWorks API Help - Add Equation and Evaluate All Example (VBA)

      http://help.solidworks.com/2013/English/api/sldworksapi/add_equation_and_evaluate_all_example_vb.htm

       

      2013 SolidWorks API Help - Use IIf Function When Adding an Equation Example (VBA)

      http://help.solidworks.com/2013/English/api/sldworksapi/use_iif_function_when_adding_an_equation_example_vb.htm

       

       

      2013 SolidWorks API Help - Automatically Solve Equations in Sequence (VBA)

      http://help.solidworks.com/2013/English/api/sldworksapi/automatically_solve_equations_in_sequence_example_vb.htm

       

       

      SolidWorks Forums: Does SolidWorks have APIs that can modify the values of global variables in SolidWorks equations?

      https://forum.solidworks.com/message/330406#330406

        • Re: How to delete the equation at the specified index
          Artem Taturevych

          Use this method: http://help.solidworks.com/2013/English/api/sldworksapi/SolidWorks.Interop.sldworks~SolidWorks.Interop.sldworks.IEquationMgr~Delete.html

           

          ____________________________________________________

          Regards,

          Artem Taturevych, Application Engineer at Intercad (Australia)

           

          translationXpert – add-in to translate SolidWorks models

          myIntercad an integrated tool for SolidWorks Professionals

          LinkedIn SolidWorks API Education Group

            • Re: How to delete the equation at the specified index
              Yong Ning

              Thank you tips

               

              Sub ll()

                Dim SwModel As ModelDoc2

                Set SwModel = Application.SldWorks.ActiveDoc

                Dim SwEqgMgr As EquationMgr

                Set SwEqgMgr = SwModel.GetEquationMgr

               

                With SwEqgMgr

                  '.Equation(0) = "x=" & 2

                  For ii = 0 To .GetCount - 1

                    Debug.Print .Equation(ii)

                    .Delete (ii)     

                  Next ii

                End With

              End Sub

               

              ********************************************

              Use IIf Function When Adding an Equation Example (VB)

              This example shows how to use the Visual Basic IIf function when adding an equation.

               

              '----------------------------------------

              '

              ' Preconditions: Model document is open, feature dimensions are visible, and at least

              '                one equation already exists.

              '

              ' Postconditions: An equation that uses the Visual IIf function is added.

              '

              '-----------------------------------------

              Option Explicit

              Sub main()

                  Dim swApp                   As SldWorks.SldWorks

                  Dim swModel                 As SldWorks.ModelDoc2

                  Dim swEqnMgr                As SldWorks.EquationMgr

                  Dim i                       As Long

                  Dim nCount                  As Long

                  Dim bRet                    As Boolean

                  Set swApp = Application.SldWorks

                  Set swModel = swApp.ActiveDoc

                  Set swEqnMgr = swModel.GetEquationMgr

                  Debug.Print "File = " & swModel.GetPathName

                  Debug.Print "  Status = " & swEqnMgr.Status

                  nCount = swEqnMgr.GetCount

                  ' List the existing equations and their calculated values

                  For i = 0 To nCount - 1

                      Debug.Print "  Eqn(" & i & ")  = " & swEqnMgr.Equation(i)

                      Debug.Print "    Value  = " & swEqnMgr.Value(i)

                  Next i

               

                 ' Add an equation that uses the Visual Basic IIf function,

                 ' list it and its calculated value

                  swEqnMgr.Add i, ("""D1@Extrude2"" = (IIf(""D1@Extrude3"">20, 15, 6))+3")

                  Debug.Print "  Eqn(" & i & ")  = " & swEqnMgr.Equation(i)

                  Debug.Print "    Value  = " & swEqnMgr.Value(i)

              End Sub

              ***********************

              Sub ll()

                Dim oDic

                Set oDic = CreateObject("Scripting.Dictionary")

                Dim SwModel As ModelDoc2, SwRootComp As Component2, SwComp As Component2

                Dim SwRootConf As Configuration, vChildComp, vModel As ModelDoc2

                Set SwModel = Application.SldWorks.ActiveDoc

                Set SwRootConf = SwModel.GetActiveConfiguration

                Set SwRootComp = SwRootConf.GetRootComponent

                vChildComp = SwRootComp.GetChildren

               

                For ii = 0 To UBound(vChildComp)

                   Set SwComp = vChildComp(ii)

                   Set oDic(Left(SwComp.Name2, Len(SwComp.Name) - 2)) = SwComp.GetModelDoc    

                Next ii 

                Dim Arr

                Arr = oDic.Items

                For ii = 0 To UBound(Arr)

                  TravseEquition Arr(ii)

                Next ii 

              End Sub 

              Function TravseEquition(SwModel) ' As ModelDoc2)

                 Dim SwEqMgr As EquationMgr

                 Set SwEqMgr = SwModel.GetEquationMgr  

                 For ii = 0 To SwEqMgr.GetCount - 1

                   Debug.Print SwModel.GetTitle,

                   Debug.Print SwEqMgr.Equation(ii)

                 Next ii

                 Debug.Print "**************"   

              End Function

               

              *************************************

              ''

              Private Sub changeEquation()

               

                Dim oDic, ii, Arr, oArr, rArr

                Dim cArr(1, 1)

                cArr(0, 0) = "cccc": cArr(0, 1) = "aaaa"

                cArr(1, 0) = "bbbb": cArr(1, 1) = "aaaa"

                Set oDic = CreateObject("Scripting.Dictionary")

                Dim SwModel As ModelDoc2, swRootComp As Component2, swComp As Component2

                Dim SwRootConf As Configuration, vChildComp, vModel As ModelDoc2

                Set SwModel = Application.SldWorks.ActiveDoc

                ''

                Arr = ComponentArr(SwModel)

                nn = UBound(Arr)

                For ii = 0 To UBound(Arr)

                  'Debug.Print "*********"

                  If UCase(Arr(ii).GetTitle) Like "*SLDASM" Then

                    Set vModel = Arr(ii)

                    oArr = ComponentArr(vModel)

                    Debug.Print "***************"

                    If Not IsEmpty(oArr) Then

                       For jj = 0 To UBound(oArr)

                          'Debug.Print Space(6) & oArr(jj).GetTitle

                          If Not IsEmpty(oArr(jj)) Then

                            Set vModel = oArr(jj)

                            ChangeEquationArr vModel, cArr

                          End If

                       Next jj

                    End If

                  Else

                    'Debug.Print Space(2) & Arr(ii).GetTitle

                    Set vModel = Arr(ii)

                    ChangeEquationArr vModel, cArr

                  End If

                Next ii

                SwModel.ForceRebuild3 True

              End Sub

               

               

              Function ChangeEquationArr(SwModel As ModelDoc2, cArr)

               

                 Dim SwEqMgr As EquationMgr, Arr(), Arr1, Arr2

                 'Set oDic = CreateObject("Scripting.Dictionary")

                 Dim oDic As New Dictionary

                 ''

                 Set SwEqMgr = SwModel.GetEquationMgr

                 With SwEqMgr

                    For ii = 0 To .GetCount - 1

                       Debug.Print .Equation(ii)

                       For jj = 0 To UBound(cArr)

                          .Equation(ii) = Replace(.Equation(ii), cArr(jj, 0), cArr(jj, 1))

                          Debug.Print .Equation(ii)

                       Next jj

                    Next ii

                 End With

               

               

              End Function

               

               

               

               

              Function ComponentArr(SwModel As ModelDoc2)

                'Debug.Print SwModel.GetTitle

                Dim oDic

                Set oDic = CreateObject("Scripting.Dictionary")

                Dim swRootComp As Component2, swComp As Component2

                Dim SwRootConf As Configuration, vChildComp, vModel As ModelDoc2

                Set SwRootConf = SwModel.GetActiveConfiguration

                Set swRootComp = SwRootConf.GetRootComponent

                vChildComp = swRootComp.GetChildren

                For ii = 0 To UBound(vChildComp)

                   Set swComp = vChildComp(ii)

                   Set vModel = swComp.GetModelDoc

                    If swComp.GetSuppression() = swComponentSuppressionState_e.swComponentResolved _

                      Or swComp.GetSuppression() = swComponentSuppressionState_e.swComponentFullyResolved Then

                        Set oDic(vModel.GetTitle) = vModel

                    End If

                   'Set oDic(Left(SwComp.Name2, Len(SwComp.Name) - 2)) = SwComp.GetModelDoc

                Next ii

                ''

                ComponentArr = oDic.Items

              End Function