19 Replies Latest reply on Feb 19, 2018 7:59 AM by Radomir Holusa

    I need a macro that will delete all unshown configurations in an assembly

    Barry Setzer

      I am using driveworks for an assembly containing about twenty components. After outputting the new model, I would like to use a macro to delete all unshown configurations in the assembly. It would be nice if it could wipe out all unshown configurations at part level as well.....but I'd be happy if it at least worked at assembly level..........any help?

        • Re: I need a macro that will delete all unshown configurations in an assembly
          Adam Hoffman

          This should get you started:

           

          Dim swApp          As SldWorks.SldWorks
          Dim swModel        As SldWorks.ModelDoc2
          Dim swConFig       As SldWorks.Configuration
          Dim sConFigName    As String
          Dim vConFigNames   As Variant
          Dim i              As Integer
          Dim bRet           As Boolean

           

          Sub main()

           

          Set swApp = _
          Application.SldWorks

           

          If swApp.GetDocumentCount() = 0 Then Exit Sub

           

          Set swModel = swApp.ActiveDoc

           

          If swModel.GetType() = swDocumentTypes_e.swDocDRAWING Then Exit Sub

           

          If swModel.GetConfigurationCount() = 1 Then Exit Sub

           

          Set swConFig = swModel.GetActiveConfiguration

           

          vConFigNames = swModel.GetConfigurationNames

           

          For i = 0 To UBound(vConFigNames)

           

              sConFigName = vConFigNames(i)
             
              If Not sConFigName = swConFig.Name Then
             
                 bRet = swModel.DeleteConfiguration2(sConFigName)
                
              End If
             
          Next i

           

          swModel.ForceRebuild3 (False)

           

          MsgBox ("Deletion Macro Complete")

           

          End Sub

          • Re: I need a macro that will delete all unshown configurations in an assembly
            Keith Rice

            To have it work at the assembly level, you could combine Adam's macro with the macro in my Macro Library called "Run code on every part in an assembly". (Requires a free membership)

             

            Keith

            SolidWorks API Tutorials

              • Re: I need a macro that will delete all unshown configurations in an assembly
                Barry Setzer

                Keith, I posted this as a new question, but since I'm having trouble getting the "all components" part to work, I figured that you are likely the most qualified to help me out..........By the way, nice macros!!!

                So I have 3 operations here. The first deletes suppressed components from my assembly. Then it calls the "allcomponents" sub. Allcomponents calls the "delcon" Sub.........this deletes all configurations from the assembly but not the individual components.......what am I missing?

                 

                Option Explicit
                Dim swApp As SldWorks.SldWorks
                Public iCountDelete As Integer
                Public sNameDelete As String

                 

                Sub main()

                    Dim swApp                 As SldWorks.SldWorks
                    Dim swModel               As SldWorks.ModelDoc2
                    Dim swAssy                As SldWorks.AssemblyDoc
                    Dim swConf                As SldWorks.Configuration
                    Dim swRootComp            As SldWorks.Component2
                   
                    Set swApp = CreateObject("SldWorks.Application")
                    Set swModel = swApp.ActiveDoc
                    Set swConf = swModel.GetActiveConfiguration
                    Set swRootComp = swConf.GetRootComponent

                    TraverseComponent swRootComp, swModel, 0
                   
                MsgBox ("Barry Has Cleaned Your File. You're Welcome!")
                End Sub

                 

                Sub TraverseComponent(swComp As SldWorks.Component2, RootModel As SldWorks.ModelDoc2, ByVal iLev As Integer)

                    Dim vChildComp            As Variant
                    Dim swChildComp           As SldWorks.Component2
                    Dim swCompConfig          As SldWorks.Configuration
                    Dim i                     As Long
                    Dim iRueck                As Double
                    Dim oModel, oModComp      As ModelDoc2
                    Dim iSel                  As Object
                    Dim b                     As Boolean
                    Dim o                     As Object
                    Dim iLevel                As Integer
                  
                    iLevel = iLev + 1   ' This variable is meant to show the sub-level the macro currently works in
                    Debug.Print iLevel & "|" & swComp.Name

                    vChildComp = swComp.GetChildren
                  
                    For i = 0 To UBound(vChildComp)
                        Set swChildComp = vChildComp(i)
                       
                        If swChildComp.IsSuppressed Then   ' Identified a suppressed part - Aim is to kill this one, no matter if it is assembly or part
                            Debug.Print "Suppr.: " & swChildComp.Name
                            swComp.Select4 False, Nothing, False
                            RootModel.EditAssembly
                            b = swChildComp.Select4(False, iSel, False)
                            b = RootModel.Extension.DeleteSelection2(1)
                            iCountDelete = iCountDelete + 1
                            RootModel.ClearSelection2 (True)
                            RootModel.EditAssembly

                            If b = False Then i = swChildComp.SetSuppression2(0)
                         End If

                        TraverseComponent swChildComp, RootModel, iLevel
                    Next i

                Call AllComponents

                End Sub

                 

                 

                Public Sub AllComponents()

                Dim swApp As SldWorks.SldWorks
                Dim swModel As SldWorks.ModelDoc2
                Dim swAssy As SldWorks.AssemblyDoc
                Dim swComp As SldWorks.Component2
                Dim swCompModel As SldWorks.ModelDoc2
                Dim i As Integer
                Dim vComps As Variant
                    Set swApp = Application.SldWorks
                    Set swModel = swApp.ActiveDoc
                    Set swAssy = swModel
                   
                    vComps = swAssy.GetComponents(True)
                   
                    If IsEmpty(vComps) Then Exit Sub
                   
                    For i = 0 To UBound(vComps)
                        Set swComp = vComps(i)
                        Set swCompModel = swComp.GetModelDoc2
                       
                         Call Delcon
                      
                    Next i

                End Sub

                Public Sub Delcon()
                Dim swApp          As SldWorks.SldWorks
                Dim swModel        As SldWorks.ModelDoc2
                Dim swConFig       As SldWorks.Configuration
                Dim sConFigName    As String
                Dim vConFigNames   As Variant
                Dim j              As Integer
                Dim bRet           As Boolean


                Set swApp = _
                Application.SldWorks
                If swApp.GetDocumentCount() = 0 Then Exit Sub
                Set swModel = swApp.ActiveDoc
                If swModel.GetType() = swDocumentTypes_e.swDocDRAWING Then Exit Sub
                If swModel.GetConfigurationCount() = 1 Then Exit Sub
                Set swConFig = swModel.GetActiveConfiguration
                vConFigNames = swModel.GetConfigurationNames
                For j = 0 To UBound(vConFigNames)
                    sConFigName = vConFigNames(j)
                    If Not sConFigName = swConFig.Name Then
                    bRet = swModel.DeleteConfiguration2(sConFigName)
                    End If
                Next j
                swModel.ForceRebuild3 (False)
                End Sub

              • Re: I need a macro that will delete all unshown configurations in an assembly
                Adam Hoffman

                Barry,

                 

                Try this macro here. The problem that took some extra time working around was: What if you had parts in your assembly that came from the same part file? The same file in the assembly, multiple times, but was using different configurations from that file. So after cleaning the configs in the top level, delete all suppressed components, then build a list of all used configuration names in this assembly. After that you can traverse the assembly deleting unused configs keeping all the referenced configs. (And if it's a sheet metal part, the flat-pattern for that config) I'm still a beginner, and I usually do all my programming in VB.Net, not Solidworks VBA. So this macro is not a pretty one, but I ran it through a couple of tests and it seems to work on my end. I haven't done extensive testing with it, so there still might be certain situations that need tweaking. Let me know if you have to make changes to it, I'd be curious about the mistakes I made. That way I might not make them in the future. Hope this helps.

                • Re: I need a macro that will delete all unshown configurations in an assembly
                  Cuinn Herrick

                  For anyone interested, I have added a function to avoid cleaning design library parts or components.

                    • Re: I need a macro that will delete all unshown configurations in an assembly
                      Nick Tzallas

                      Ok so this is a great help! Thank you Adam Hoffman Cuinn Herrick

                      I do not mean to resurrect a relatively old thread but I am in a bit of a bind

                       

                      Seeing as my macro fluency/comprehension is non-existent, is there any link/literature that could teach me how I could modify that particular Macro so I could simply select a folder with hundreds of files and just run it on that?

                      i.e. it would open each file one by one, perform "clean inactive configs", save and then close file. move onto the next one and so on.

                      Having to open each file and apply the macro manually will take a toll on me rather fast seeing as it would need to be applied to 1000's of parts (building fasteners library, sans toolbox)

                       

                      There might be an obvious answer and/or link here, but I am ever so slightly clueless

                        • Re: I need a macro that will delete all unshown configurations in an assembly
                          Dennis Dohogne

                          Nick Tzallas wrote:

                           

                          Ok so this is a great help! Thank you Adam Hoffman Cuinn Herrick

                          I do not mean to resurrect a relatively old thread but I am in a bit of a bind

                           

                          Seeing as my macro fluency/comprehension is non-existent, is there any link/literature that could teach me how I could modify that particular Macro so I could simply select a folder with hundreds of files and just run it on that?

                          i.e. it would open each file one by one, perform "clean inactive configs", save and then close file. move onto the next one and so on.

                          Having to open each file and apply the macro manually will take a toll on me rather fast seeing as it would need to be applied to 1000's of parts (building fasteners library, sans toolbox)

                           

                          There might be an obvious answer and/or link here, but I am ever so slightly clueless

                          Nick,

                          This sounds like it is right up the alley of #TASK #TASK

                          If it doesn't do exactly as you want then follow the information here Setting up a custom task with macro

                      • Re: I need a macro that will delete all unshown configurations in an assembly
                        Dennis Dohogne

                        For your desire to delete unused configurations in a part file you can use Purge introduced in SWX2016.  For the assembly you can bookmark this and vote when the time comes:  Have Purge function first do Where Used and extend it to assemblies