2 Replies Latest reply on Nov 26, 2012 1:39 PM by Adam Doerr

    Sub-Assembly Incrementing Macro Help

    Adam Doerr

      I'm hoping to create a macro that will let me manually select a sub-assembly in the tree and then run the macro through a hotkey to increment the sub-assembly to its next configuration. I've previously only created very simple macros or edited complex ones to slightly modify their function. Now I'm trying to use the online API help to try creating such a macro from scratch, but it seems quite difficult to find what I should even be searching for. I recorded a macro to start off with that basically does what I want, but it is too exact in it's funtion calls to allow me to run it again and again (ie; it keeps calling for the same configuration name). So far, it seems like CompConfigProperties4 is what I'll probably use according to my initial macro recording. I'm looking for some help/pointers on what functions I might want to use and any other tips you can give me. Any suggestions on other downloadable macros that might have pieces of code I an use would also be much appreciated.

        • Re: Sub-Assembly Incrementing Macro Help
          Keith Rice

          Hi Adam,


          Cool idea for a macro. You need to use ISelectionMgr to get the selected object, IComponent2::ReferencedConfiguration to get and set the component's configuration, IComponent2::GetModelDoc2 to get the underlying model of the component so that you can determine the names of the configurations, and IModelDoc2::GetConfigurationNames to get the names of the configs.


          This macro should accomplish what you need:




          Dim swApp As SldWorks.SldWorks

          Dim swModel As SldWorks.ModelDoc2

          Dim swSelMgr As SldWorks.SelectionMgr

          Dim swComp As SldWorks.Component2

          Dim swCompModel As SldWorks.ModelDoc2

          Dim strCurrentConfigName As String

          Dim strNewConfigName As String

          Dim vConfigNames As Variant

          Dim i As Integer


          Sub main()

              Set swApp = Application.SldWorks

              Set swModel = swApp.ActiveDoc

              Set swSelMgr = swModel.SelectionManager

              Set swComp = swSelMgr.GetSelectedObject6(1, -1)

              strCurrentConfigName = swComp.ReferencedConfiguration

              Set swCompModel = swComp.GetModelDoc2

              vConfigNames = swCompModel.GetConfigurationNames

              For i = 0 To UBound(vConfigNames)

                  If strCurrentConfigName = vConfigNames(i) Then

                      If i + 1 > UBound(vConfigNames) Then

                          strNewConfigName = vConfigNames(0)


                          strNewConfigName = vConfigNames(i + 1)

                      End If

                      swComp.ReferencedConfiguration = strNewConfigName

                      Exit For

                  End If

              Next i


          End Sub




          I would highly recommend not using the online API Help. It is a lot harder to navigate. Instead, uncheck Help-->Use SolidWorks web help" before going to Help-->API Help. This will open the local API Help. I cover this and a lot more in Lesson 2.2 at my web site.



          Video Tutorials for the SolidWorks API