4 Replies Latest reply on Jan 29, 2015 8:28 AM by Keith Rice

    Traverse assembly at component level

    Damon Frashure

      I'm trying to make this code segment from the Knowledge Base (below) work in SW2010.  When I paste it in the VBA window I get an error because of an invalid number of arguments.  Can someone explain this?  Basically I am trying to write a macro that will let me traverse an assembly and save each part file with a new {sequential} file name and update the assembly at the end.  Thoughts?

       

       

      '------------------------------------------------------------------

      Option Explicit

      Sub TraverseComponent _

      ( _

          swComp As SldWorks.Component2, _

          nLevel As Long _

      )

          Dim vChildComp                  As Variant

          Dim swChildComp                 As SldWorks.Component2

          Dim swCompConfig                As SldWorks.Configuration

          Dim sPadStr                     As String

          Dim i                           As Long

         

          For i = 0 To nLevel - 1

              sPadStr = sPadStr + "  "

          Next i

         

          vChildComp = swComp.GetChildren

          For i = 0 To UBound(vChildComp)

              Set swChildComp = vChildComp(i)

             

              TraverseComponent swChildComp, nLevel + 1

             

              Debug.Print sPadStr & swChildComp.Name2 & " <" & swChildComp.ReferencedConfiguration & ">"

          Next i

      End Sub

      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

          Dim bRet                        As Boolean

         

          Set swApp = CreateObject("SldWorks.Application")

          Set swModel = swApp.ActiveDoc

          Set swConf = swModel.GetActiveConfiguration

          Set swRootComp = swConf.GetRootComponent3(true)

         

          Debug.Print "File = " & swModel.GetPathName

         

          TraverseComponent swRootComp, 1

      End Sub

      '---------------------------------------

        • Re: Traverse assembly at component level
          Attilio Colangelo

          Seems like the line continuation character "_" formatting doesn't survive cut and paste. It happens often. Change the first few lines to this:

           

          Sub TraverseComponent(swComp As SldWorks.Component2, nLevel As Long)

          Dim vChildComp                  As Variant

          .

          .

          .

           

           

          Here is some exciting information on how the line continuation should work

          https://msdn.microsoft.com/en-us/library/ba9sxbw4.aspx

          • Re: Traverse assembly at component level
            Keith Rice

            Damon,

             

            As Attilio pointed out, when the code its pasted in the VB Editor, it brings in some additional characters that cause problems. See the yellow highlight:

             

            Removing those should be sufficient. However I would also encourage you to check out this blog post, "How to Copy, Clean, & Use API Help Examples" for additional tips related to using API Help examples.

             

            BUT WAIT!

             

            If you need to perform a particular task on every component in an assembly (or every component that meets a specific criterion), there is no reason to use that code you pulled from the API Help. There are faster and simpler ways to accomplish the same thing.

             

            If you don't care about the order in which you traverse, simply use the free macro on my web site "Run code on every part in an assembly" (technically a misnomer -- should read "every component" not "every part").

             

            If you need to traverse the components in the exact order they are in the feature tree, I can supply you some other code that will do that. The current code you are using won't traverse in order.

             

            Keith

            SolidWorks API Tutorials

              • Re: Traverse assembly at component level
                Damon Frashure

                I'm not worried about the traverse order.  Really all I'm trying to do is write a macro that will take each part in an assembly and change the name while updating the assembly reference (basically just a SaveAs without the save as copy box ticked).  I haven't played with your code yet, but the first one create4d a new part file for each instance it showed up in the assembly.  How will your macro respond to this situation?

                  • Re: Traverse assembly at component level
                    Keith Rice

                    Damon,

                     

                    The macro I referenced simply demonstrates how to properly traverse an assembly without regard to order. Whatever other functionality or behavior you want will need to be programmed in. You could program it to save every component as a new file or save every unique component as a new file. If you wanted to do the latter, you'd simply need to keep track of the names of the components that have already been saved, minus their instance number.

                     

                    Also, if your only goal is to rename components while preserving references, then the Document Manager API would be more appropriate, though that is a serious step up in complexity and perhaps not worth it if this is just a hobbyist project.

                     

                    Hope this helps! Good luck.

                     

                    Keith

                    SolidWorks API Tutorials