5 Replies Latest reply on Jun 21, 2018 8:07 AM by Franz Dahinden

    Combine 2 macros--> delete propertys and equations

    Franz Dahinden

      I have this 2 macros and would like to combine them;

      1.delete propertys

      2. delete equations

       

      I do not have enThanks a lot

      I do not have enough experience to do this, At the moment I make 2 klicks on my macros.

       

      Option Explicit

       

      Dim swApp As SldWorks.SldWorks

      Dim swModel As ModelDoc2

      Dim cpm As CustomPropertyManager

       

      Sub DeleteProps()

       

      Set swApp = Application.SldWorks

      Set swModel = swApp.ActiveDoc

       

      Dim names() As String

       

      Set cpm = swModel.Extension.CustomPropertyManager("")

      names = cpm.GetNames

       

      Dim i As Integer

      For i = 0 To UBound(names)

      cpm.Delete names(i)

      Next

       

      End Sub

      _______________________________________________________________________________________________________________________________________________________________________________________________________________________________________________

      _________________________________________________________________________________________________________________________

      Const DELETE_BROKEN_ONLY As Boolean = False 'if this flag is True than only broken equations are deleted, otherwise all equations are deleted

       

      Dim swApp As SldWorks.SldWorks

      Dim swModel As SldWorks.ModelDoc2

      Dim swEqMgr As SldWorks.EquationMgr

       

      Sub main()

       

          Set swApp = Application.SldWorks

         

          Set swModel = swApp.ActiveDoc

         

          Dim hasDeleted As Boolean

         

          DeleteEquationsFromModel swModel, hasDeleted

         

          If swModel.GetType = swDocumentTypes_e.swDocASSEMBLY Then

             

              If MsgBox("Do you want to delete equations in all components of the assembly?", vbYesNo) = vbYes Then

                 

                  Dim swAssy As SldWorks.AssemblyDoc

                  Set swAssy = swModel

                 

                  'component needs to be loaded in memory in order to process it's equations

      swAssy.ResolveAllLightWeightComponents True

                 

                  Dim vComps As Variant

                  vComps = swAssy.GetComponents(False)

                 

                  Dim i As Integer

                 

                  For i = 0 To UBound(vComps)

                     

                      Dim swComp As SldWorks.Component2

                      Set swComp = vComps(i)

                     

                      Dim swCompModel As SldWorks.ModelDoc2

                     

                      Set swCompModel = swComp.GetModelDoc2

                     

                      If Not swCompModel Is Nothing Then

                         

                          Dim hasCompEqDeleted As Boolean

                          DeleteEquationsFromModel swCompModel, hasCompEqDeleted

                         

                          If hasCompEqDeleted Then

                              hasDeleted = True

                          End If

                         

                      End If

                     

                  Next

                 

              End If

             

          End If

         

          If hasDeleted Then

              swModel.ForceRebuild3 False

          End If

         

      End Sub

       

      Sub DeleteEquationsFromModel(model As SldWorks.ModelDoc2, ByRef hasDeleted As Boolean)

         

          Set swEqMgr = model.GetEquationMgr()

         

          Dim i As Integer

         

          hasDeleted = False

         

          'iterate in the reverse direction as the equation index will change once previous equation is deleted

          For i = swEqMgr.GetCount - 1 To 0 Step -1

             

              If Not DELETE_BROKEN_ONLY Or IsEquationBroken(swEqMgr, i) Then

                  swEqMgr.Delete i

                  hasDeleted = True

              End If

          Next

         

          If hasDeleted Then

              'deleting equation doesn't make the model dirty

              model.SetSaveFlag

          End If

         

      End Sub

       

      Function IsEquationBroken(eqMgr As SldWorks.EquationMgr, index As Integer) As Boolean

         

          Const STATUS_BROKEN As Integer = -1

         

          Dim val As String

          val = eqMgr.Value(index) 'evaluate to get the status

         

          IsEquationBroken = (eqMgr.Status = STATUS_BROKEN)

         

      End Function

        • Re: Combine 2 macros--> delete propertys and equations
          Artem Taturevych

          Const DELETE_BROKEN_ONLY As Boolean = False 'if this flag is True than only broken equations are deleted, otherwise all equations are deleted

           

          Dim swApp As SldWorks.SldWorks

          Dim swModel As SldWorks.ModelDoc2

          Dim swEqMgr As SldWorks.EquationMgr

           

          Sub main()

           

              Set swApp = Application.SldWorks

            

              Set swModel = swApp.ActiveDoc

            

          DeleteProps

           

              Dim hasDeleted As Boolean

            

              DeleteEquationsFromModel swModel, hasDeleted

            

              If swModel.GetType = swDocumentTypes_e.swDocASSEMBLY Then

                

                  If MsgBox("Do you want to delete equations in all components of the assembly?", vbYesNo) = vbYes Then

                    

                      Dim swAssy As SldWorks.AssemblyDoc

                      Set swAssy = swModel

                    

                      'component needs to be loaded in memory in order to process it's equations

          swAssy.ResolveAllLightWeightComponents True

                    

                      Dim vComps As Variant

                      vComps = swAssy.GetComponents(False)

                    

                      Dim i As Integer

                    

                      For i = 0 To UBound(vComps)

                        

                          Dim swComp As SldWorks.Component2

                          Set swComp = vComps(i)

                        

                          Dim swCompModel As SldWorks.ModelDoc2

                        

                          Set swCompModel = swComp.GetModelDoc2

                        

                          If Not swCompModel Is Nothing Then

                            

                              Dim hasCompEqDeleted As Boolean

                              DeleteEquationsFromModel swCompModel, hasCompEqDeleted

                            

                              If hasCompEqDeleted Then

                                  hasDeleted = True

                              End If

                            

                          End If

                        

                      Next

                    

                  End If

                

              End If

            

              If hasDeleted Then

                  swModel.ForceRebuild3 False

              End If

            

          End Sub

           

          Sub DeleteProps()

           

           

          Dim cpm As CustomPropertyManager

           

           

          Dim names() As String

           

           

          Set cpm = swModel.Extension.CustomPropertyManager("")

           

           

          names = cpm.GetNames

           

           

          Dim i As Integer

           

           

          For i = 0 To UBound(names)

           

           

          cpm.Delete names(i)

           

           

          Next

           

           

          End Sub

           

          Sub DeleteEquationsFromModel(model As SldWorks.ModelDoc2, ByRef hasDeleted As Boolean)

            

              Set swEqMgr = model.GetEquationMgr()

            

              Dim i As Integer

            

              hasDeleted = False

            

              'iterate in the reverse direction as the equation index will change once previous equation is deleted

              For i = swEqMgr.GetCount - 1 To 0 Step -1

                

                  If Not DELETE_BROKEN_ONLY Or IsEquationBroken(swEqMgr, i) Then

                      swEqMgr.Delete i

                      hasDeleted = True

                  End If

              Next

            

              If hasDeleted Then

                  'deleting equation doesn't make the model dirty

                  model.SetSaveFlag

              End If

            

          End Sub

           

          Function IsEquationBroken(eqMgr As SldWorks.EquationMgr, index As Integer) As Boolean

            

              Const STATUS_BROKEN As Integer = -1

            

              Dim val As String

              val = eqMgr.Value(index) 'evaluate to get the status

            

              IsEquationBroken = (eqMgr.Status = STATUS_BROKEN)

            

          End Function