13 Replies Latest reply on Mar 22, 2017 9:39 AM by John Lega

    Need Macro To Remove All Global Variables in Part/Assembly

    John Lega

      We are having issues with some Global Variables (really slows down the model) and we would like a way to remove them with a single click. Ideally we could point to a folder and do them all but the active document would be great. Perhaps even open the top level assembly and run macro on all sub-assemblies is also ideal.

       

      Here are the variables in question. I guess it's safer to JUST delete those (2) rather than ALL in case some parts have others we need.

      Equations.jpg

      We are using Solidworks 2013 and plan to upgrade soon to 2015.

       

      Thanks so much!

       

      Regards,

       

      John Lega

        • Re: Need Macro To Remove All Global Variables in Part/Assembly
          Amen Allah Jlili

          Hello John,

           

          Here's something I've written but have not tested (on assemblies)

           

          I've tested on a part doc and it works fine. The macro asks you to enter the global variables separated by a comma.

           

          preview.png


          Normally this should do a drill down on assemblies. It may freeze SolidWorks if you're operating on large assemblies so hang on tight and wait for it to finish.

           

           

           

          Public swApp As SldWorks.SldWorks
          Public swModel As ModelDoc2
          Dim Variables
          
          
          Sub main()
          Set swApp = Application.SldWorks
          Set swModel = swApp.ActiveDoc
          If swModel Is Nothing Then
          swApp.SendMsgToUser ("Active model is empty. Macro will exist")
          Exit Sub
          End If
          Dim VariablesTobeDeleted As String
          VariablesTobeDeleted = InputBox("Please enter the equations to be deleted seperated by a comma", "Delete equations by jliliamen@gmail.com")
          If VariablesTobeDeleted = "" Then
          swApp.SendMsgToUser ("No variables entered. Macro will exist")
          Exit Sub
          End If
          Variables = Split(VariablesTobeDeleted, ",", -1, vbTextCompare)
          Select Case swModel.GetType()
          Case 1
          PartSub
          Case 2
          AssemblySub
          Case Else
          swApp.SendMsgToUser ("Active model type unknown to macro")
          End Select
          End Sub
            
            
          Sub AssemblySub()
          Dim swAssemblyDoc As AssemblyDoc
          'First step is to remove all equations from assembly doc
          DeleteModelDocEquations swModel
          'Remove from components2
          Set swAssemblyDoc = swModel
          Dim VComponents As Variant
          VComponents = swAssemblyDoc.GetComponents(False)
          If Not IsEmpty(VComponents) Then
          Dim TravObj
          For Each TravObj In VComponents
          Dim Comp As Component2
          Set Comp = TravObj
          Dim ModelComp As ModelDoc2
          Set ModelComp = Comp.GetModelDoc2
          DeleteModelDocEquations ModelComp
          Next TravObj
          End If
          swModel.Save
          End Sub
            
            
          Sub PartSub()
          DeleteModelDocEquations swModel
          End Sub
            
            
          Sub DeleteModelDocEquations(swModel As ModelDoc2)
          Dim swEquationManager As EquationMgr
          Set swEquationManager = swModel.GetEquationMgr
          Dim ReturnValue As Long
          Dim EquationIndex As Long
          For i = 0 To UBound(Variables)
          EquationIndex = GetEquationIndex(swEquationManager, (Variables(i)))
          If EquationIndex <> -1 Then
          ReturnValue = swEquationManager.Delete(EquationIndex)
          End If
          Next i
          swModel.Save
          End Sub
          
          
          Private Function GetEquationIndex(ByRef EquationManager As EquationMgr, Equation As String) As Long
          For i = 0 To EquationManager.GetCount - 1
          If Replace(Split(EquationManager.Equation(Counter), "=")(0), """", "") = Equation Then
          GetEquationIndex = i
          Exit Function
          End If
          Next
          GetEquationIndex = -1
          End Function
          
            • Re: Need Macro To Remove All Global Variables in Part/Assembly
              John Lega

              Thank you sir. It worked great.

               

              Question:

               

              1. Could this be modified to only delete the (2) Global Variables on my screenshot above. That's really all we need it to do.

               

              Thanks so much!

               

              Regards,

               

              John

                • Re: Need Macro To Remove All Global Variables in Part/Assembly
                  Amen Allah Jlili

                  Yes. Simply input the 2 global variables separated by comma. Global variables handled as equations by the equation manager.

                   

                  In your case,  you would enter "Weight-KG,Weight-LB" and it will remove just those two. The reason why I coded it to ask you for the global variables is to make it more general. I took into account that some other people in the SW community might use it to remove global variables different to Weight-KG and Weight-LB.

                    • Re: Need Macro To Remove All Global Variables in Part/Assembly
                      John Lega

                      Thanks again. No problem entering them but I was wondering how it can be written to JUST delete those (2) variables so that it will be a 1-click solution. Ideally I would have a separate macro to JUST  delete those (2) and use it as is to make it more flexible. I do not know how to code so I was unclear how to remove the dialog box portion and just add the (2) variables to the code.

                       

                      Thanks again for  your help!

                       

                      Regards,

                       

                      John

                        • Re: Need Macro To Remove All Global Variables in Part/Assembly
                          Amen Allah Jlili

                          As per your last comment, change this:

                          1. Dim VariablesTobeDeleted As String 
                          2. VariablesTobeDeleted = InputBox("Please enter the equations to be deleted seperated by a comma", "Delete equations by jliliamen@gmail.com") 
                          3. If VariablesTobeDeleted = "" Then 
                          4. swApp.SendMsgToUser ("No variables entered. Macro will exist") 
                          5. Exit Sub 
                          6. End If 
                          7. Variables = Split(VariablesTobeDeleted, ",", -1, vbTextCompare) 

                          to

                           

                          1. Variables = Array("Weight-LB","Weight-KG")

                           

                          and you're golden!

                          (if this post answers your question), please tick it as answer

                            • Re: Need Macro To Remove All Global Variables in Part/Assembly
                              John Lega

                              I'm so sorry. I did not see your reply. I replaced what you suggested but I get a runtime error on this line:

                               

                              Set swEquationManager = swModel.GetEquationMgr

                               

                              I'm on SW 2013 if that helps.

                               

                              Thanks so much!

                               

                              John

                              • Re: Need Macro To Remove All Global Variables in Part/Assembly
                                John Lega

                                Here is my current macro...

                                 

                                Public swApp As SldWorks.SldWorks

                                Public swModel As ModelDoc2

                                Dim Variables

                                 

                                 

                                Sub main()

                                Set swApp = Application.SldWorks

                                Set swModel = swApp.ActiveDoc

                                If swModel Is Nothing Then

                                swApp.SendMsgToUser ("Active model is empty. Macro will exist")

                                Exit Sub

                                End If

                                Variables = Array("Weight-LB", "Weight-KG")

                                Select Case swModel.GetType()

                                Case 1

                                PartSub

                                Case 2

                                AssemblySub

                                Case Else

                                swApp.SendMsgToUser ("Active model type unknown to macro")

                                End Select

                                End Sub

                                   

                                   

                                Sub AssemblySub()

                                Dim swAssemblyDoc As AssemblyDoc

                                'First step is to remove all equations from assembly doc

                                DeleteModelDocEquations swModel

                                'Remove from components2

                                Set swAssemblyDoc = swModel

                                Dim VComponents As Variant

                                VComponents = swAssemblyDoc.GetComponents(False)

                                If Not IsEmpty(VComponents) Then

                                Dim TravObj

                                For Each TravObj In VComponents

                                Dim Comp As Component2

                                Set Comp = TravObj

                                Dim ModelComp As ModelDoc2

                                Set ModelComp = Comp.GetModelDoc2

                                DeleteModelDocEquations ModelComp

                                Next TravObj

                                End If

                                swModel.Save

                                End Sub

                                   

                                   

                                Sub PartSub()

                                DeleteModelDocEquations swModel

                                End Sub

                                   

                                   

                                Sub DeleteModelDocEquations(swModel As ModelDoc2)

                                Dim swEquationManager As EquationMgr

                                Set swEquationMgr = swModel.GetEquationMgr

                                Dim ReturnValue As Long

                                Dim EquationIndex As Long

                                For i = 0 To UBound(Variables)

                                EquationIndex = GetEquationIndex(swEquationManager, (Variables(i)))

                                If EquationIndex <> -1 Then

                                ReturnValue = swEquationManager.Delete(EquationIndex)

                                End If

                                Next i

                                swModel.Save

                                End Sub

                                 

                                 

                                Private Function GetEquationIndex(ByRef EquationManager As EquationMgr, Equation As String) As Long

                                For i = 0 To EquationManager.GetCount - 1

                                If Replace(Split(EquationManager.Equation(Counter), "=")(0), """", "") = Equation Then

                                GetEquationIndex = i

                                Exit Function

                                End If

                                Next

                                GetEquationIndex = -1

                                End Function

                                 

                                Getting errors on these two lines:

                                Set swEquationMgr = swModel.GetEquationMgr

                                 

                                I'm using 2015... any ideas?

                                 

                                Thanks,

                                 

                                John

                                  • Re: Need Macro To Remove All Global Variables in Part/Assembly
                                    John Lega

                                    Now I get a ByRef Argument Type Mismatch on the following line:

                                     

                                    DeleteModelDocEquations swModel

                                     

                                    I'm trying to work it out myself but I know nothing about VBE... but wan to learn... Thanks!

                                      • Re: Need Macro To Remove All Global Variables in Part/Assembly
                                        Kevin Chisholm

                                        This code works for me:

                                         

                                        Added ByRef for AssemblySub & Added swModel.EditRebuild after the equation is deleted.

                                         

                                        Public swApp As SldWorks.SldWorks
                                        Public swModel As ModelDoc2
                                        Dim Variables
                                          
                                        Sub main()
                                        Set swApp = Application.SldWorks
                                        Set swModel = swApp.ActiveDoc
                                        If swModel Is Nothing Then
                                        swApp.SendMsgToUser ("Active model is empty. Macro will exist")
                                        Exit Sub
                                        End If
                                        Variables = Array("Weight-LB", "Weight-KG")
                                        Select Case swModel.GetType()
                                        Case 1
                                        PartSub
                                        Case 2
                                        AssemblySub swModel
                                        Case Else
                                        swApp.SendMsgToUser ("Active model type unknown to macro")
                                        End Select
                                        End Sub
                                            
                                            
                                        Sub AssemblySub(swModel As ModelDoc2)
                                        Dim swAssemblyDoc As AssemblyDoc
                                        'First step is to remove all equations from assembly doc
                                        DeleteModelDocEquations swModel
                                        'Remove from components2
                                        Set swAssemblyDoc = swModel
                                        Dim VComponents As Variant
                                        VComponents = swAssemblyDoc.GetComponents(False)
                                        If Not IsEmpty(VComponents) Then
                                        Dim TravObj
                                        For Each TravObj In VComponents
                                        Dim Comp As Component2
                                        Set Comp = TravObj
                                        Dim ModelComp As ModelDoc2
                                        Set ModelComp = Comp.GetModelDoc2
                                        DeleteModelDocEquations ModelComp
                                        Next TravObj
                                        End If
                                        swModel.Save
                                        End Sub
                                            
                                            
                                        Sub PartSub()
                                        DeleteModelDocEquations swModel
                                        End Sub
                                            
                                            
                                        Sub DeleteModelDocEquations(swModel As ModelDoc2)
                                        Dim swEquationManager As EquationMgr
                                        Set swEquationManager = swModel.GetEquationMgr
                                        Dim ReturnValue As Integer
                                        Dim EquationIndex As Integer
                                        For i = 0 To UBound(Variables)
                                        EquationIndex = GetEquationIndex(swEquationManager, (Variables(i)))
                                        If EquationIndex <> -1 Then
                                        ReturnValue = swEquationManager.Delete(EquationIndex)
                                        End If
                                        Next i
                                        swModel.EditRebuild3
                                        swModel.Save
                                        End Sub
                                          
                                          
                                        Private Function GetEquationIndex(ByRef EquationManager As EquationMgr, Equation As String) As Long
                                        For i = 0 To EquationManager.GetCount - 1
                                        If Replace(Split(EquationManager.Equation(Counter), "=")(0), """", "") = Equation Then
                                        GetEquationIndex = i
                                        Exit Function
                                        End If
                                        Next
                                        GetEquationIndex = -1
                                        End Function
                                        

                                         

                                        Let me know if it works for you!

                                          • Re: Need Macro To Remove All Global Variables in Part/Assembly
                                            John Lega

                                            Thank you. I get a Run-time error '91': Object variable or With block variable not set

                                            on this line:

                                             

                                            Set swEquationManager = swModel.GetEquationMgr

                                             

                                            What's odd is that it does actually work and delete the equations but it still errors out. I'm on 2015, btw.

                                             

                                            Thanks again so much - let me know if you know how to fix this.

                                             

                                            Regards,

                                             

                                            John

                                              • Re: Need Macro To Remove All Global Variables in Part/Assembly
                                                Kevin Chisholm

                                                Try replacing this line:

                                                Dim swEquationManager As EquationMgr

                                                To

                                                Dim swEquationManager As SldWorks.EquationMgr

                                                 

                                                Edit:

                                                Also what References do you have active here:

                                                  • Re: Need Macro To Remove All Global Variables in Part/Assembly
                                                    Kevin Chisholm

                                                    Let me know if this solves the problem...

                                                    I don't have SW2015 installed but I'll install it if you're still having problems!

                                                     

                                                    Public swApp As SldWorks.SldWorks  
                                                    Public swModel As ModelDoc2  
                                                    Dim Variables  
                                                        
                                                    Sub main()  
                                                    Set swApp = Application.SldWorks  
                                                    Set swModel = swApp.ActiveDoc  
                                                    If swModel Is Nothing Then  
                                                    swApp.SendMsgToUser ("Active model is empty. Macro will exist")  
                                                    Exit Sub  
                                                    End If  
                                                    Variables = Array("Weight-LB", "Weight-KG")  
                                                    Select Case swModel.GetType()  
                                                    Case 1  
                                                    PartSub swModel  
                                                    Case 2  
                                                    AssemblySub swModel  
                                                    Case Else  
                                                    swApp.SendMsgToUser ("Active model type unknown to macro")  
                                                    End Select  
                                                    End Sub  
                                                          
                                                          
                                                    Sub AssemblySub(swModel As ModelDoc2)  
                                                    Dim swAssemblyDoc As AssemblyDoc  
                                                    'First step is to remove all equations from assembly doc  
                                                    DeleteModelDocEquations swModel  
                                                    'Remove from components2  
                                                    Set swAssemblyDoc = swModel  
                                                    Dim VComponents As Variant  
                                                    VComponents = swAssemblyDoc.GetComponents(False)  
                                                    If Not IsEmpty(VComponents) Then  
                                                    Dim TravObj  
                                                    For Each TravObj In VComponents  
                                                    Dim Comp As Component2  
                                                    Set Comp = TravObj  
                                                    Dim ModelComp As ModelDoc2  
                                                    Set ModelComp = Comp.GetModelDoc2  
                                                    DeleteModelDocEquations ModelComp  
                                                    Next TravObj  
                                                    End If  
                                                    swModel.Save  
                                                    End Sub  
                                                          
                                                          
                                                    Sub PartSub(swModel As ModelDoc2)  
                                                    DeleteModelDocEquations swModel  
                                                    End Sub  
                                                          
                                                          
                                                    Sub DeleteModelDocEquations(swModel As ModelDoc2)  
                                                    Dim swEquationManager As EquationMgr  
                                                    Set swEquationManager = swModel.GetEquationMgr  
                                                    Dim ReturnValue As Integer  
                                                    Dim EquationIndex As Integer  
                                                    For i = 0 To UBound(Variables)  
                                                    EquationIndex = GetEquationIndex(swEquationManager, (Variables(i)))  
                                                    If EquationIndex <> -1 Then  
                                                    ReturnValue = swEquationManager.Delete(EquationIndex)  
                                                    End If  
                                                    Next i  
                                                    swModel.ForceRebuild3 False  
                                                    swModel.Save  
                                                    End Sub  
                                                        
                                                        
                                                    Private Function GetEquationIndex(ByRef EquationManager As EquationMgr, Equation As String) As Long  
                                                    For i = 0 To EquationManager.GetCount - 1  
                                                    If Replace(Split(EquationManager.Equation(Counter), "=")(0), """", "") = Equation Then  
                                                    GetEquationIndex = i  
                                                    Exit Function  
                                                    End If  
                                                    Next  
                                                    GetEquationIndex = -1  
                                                    End Function