4 Replies Latest reply on May 16, 2017 6:44 PM by Raghvendra Bhargava

    Macro to traverse assembly components and change configurations

    Patrick Keith

      Hello, I am new to the SolidWorks API and programming in general.  I use a "Simplified" configuration in most assemblies that take the hardware, adapters, e.t.c. and suppress them.  When putting a top level assembly with multiple subassemblies in Simplified config I have to manually select each subassembly and change it to Simplified in the tree as well.  I would like to write a macro that loops through all my subassemblies and changes them from Default to Simplified config.  The Simplified will be already created in these subassemblies so it only needs to be changed over.  Hope this was easy enough to understand thanks!


      This is just a short bit of code I have started on but know it is far from where I need to be.


        • Re: Macro to traverse assembly components and change configurations
          Raghvendra Bhargava

          Try This:


          Dim swApp As SldWorks.SldWorks

          Dim swModel As ModelDoc2

          Dim swAssm As AssemblyDoc

          Dim vComp  As Variant

          Dim Count As Integer

          Dim swComp As Component2

          Dim swDoc As ModelDoc2

          Dim swConf  As Configuration


          Sub main()


          Set swApp = Application.SldWorks

          Set swModel = swApp.ActiveDoc

          Set swAssm = swModel


          vComp = swAssm.GetComponents(True)


          For Count = 0 To UBound(vComp)


              Set swComp = vComp(Count)

              Set swDoc = swComp.GetModelDoc2


              Set swConf = swDoc.GetConfigurationByName("Simplified")


              If Not swConf Is Nothing Then

                  swComp.ReferencedConfiguration = "Simplified"



                  Debug.Print swComp.Name2 & " is not having Simplified configuration"

              End If


          Next Count


          End Sub

            • Re: Macro to traverse assembly components and change configurations
              Patrick Keith

              Worked great man , I like the .GetComponents better than the way I was trying to do it. Thank you very much !

              • Re: Macro to traverse assembly components and change configurations
                Vincent Piazza

                Hello Raghvendra,


                TL;DR: Instead of choosing configuration based off of exact match, how would I select config. based off of an InStr search for each config. name?


                     I have a similar task that I'm trying to accomplish with selecting configurations that I'm hoping you could help with as well.


                My problem:

                     I need to create a unique assembly and drawing that will vary in size as required by the customer. Each of the parts in this assembly have many configurations that can be selected depending on the required size. The problem that I am running into is that the configurations of the parts do not share the same naming convention, which means I cannot use an exact match to select the configurations. They do however share the same 'size' in their configuration names, which is what I want to use to select the parts' configurations.


                An example:

                     My assembly has 4 parts in it: a tee, 2 flanges, and a blind flange. Customer puts an order in for a Strainer of size NPS 12. So now I'd like pull that size requirement from an excel file (I already have the code for this) and select the configuration that has "NPS 12" in the configuration name for each of the parts, and then generate the drawing.


                     Of course a lot more code is required to generate the drawing. I'm just having trouble getting the traversal loop to work with my requirements. I'm thinking using GetConfigurationNames() and then using InStr to search the string for "NPS 12" could work, but the compiler is throwing errors when I try to implement that.


                Any help would be greatly appreciated!