2 Replies Latest reply on Jun 8, 2017 4:20 PM by John Alexander

    How to find out the folder/file names in the assembly tree using macro?

    Marco Wu

      I have four folders

      AA

      BB

      CC

      DD

       

      There are different parts in those folders. I need to sort the parts in different folder in different ways.

      Is there any way that I can know what part is in which folder.

      Thanks

        • Re: How to find out the folder/file names in the assembly tree using macro?
          Peter Brinkhuis

          Sure. You can traverse the feature tree with something like this. Mostly from the top of my head:

           

          Sub PrintFeatureTypes()

          dim swApp as SldWorks.SldWorks

          dim swModel as ModelDoc2

          Dim swFeat as Feature

          Set swApp = Application.SldWorks

          Set swModel = swApp.ActiveDoc

          Set swFeat = swModel.FirstFeature

          While Not swFeat Is Nothing

             Debug.Print swFeat.GetTypeName2

             Set swFeat = swFeat.GetNextFeature

          Wend

          End Sub

           

          You'll see that it will print a 'folder' type and an 'end folder' type. I don't remember the exact phrasing, but it's pretty clear when you see it.

          • Re: How to find out the folder/file names in the assembly tree using macro?
            John Alexander

            I was working on a similar task a couple weeks ago (sorting components in a folder). The conclusion that I came to was that it is easier to  dissolve (delete) all folders, re-create them, and then insert the components. I found this approach easier to implement because one of the folder insertion methods takes an array of swComponents and arranges in the same order that they appear in that array.

             

            Detect if Assembly Component is in a folder

             

            In my case, the folders were instantiated by a "vendor" property and then some other characteristics like reference models or in-house custom parts. Some of those things were cut out of the attached TreeOrganizer_v04a for public consumption. It isn't well commented but the function names are pretty descriptive so you should get an idea for what to change to get the behavior you want.

             

            When I was working on the TreeOrganizer, I came across the sortPartNumbers macro (also attached). It takes a different approach but appears to be faster than my method and more direct. Unfortunately, I don't know who to credit that macro to. I'm pretty sure I found it on this forum.