5 Replies Latest reply on Apr 5, 2017 10:53 AM by Peter Brinkhuis

    Detect if Assembly Component is in a folder

    John Alexander

      I'm trying to sort assembly components into folders according to the custom property "vendor". I'm just traversing the assembly using AssemblyDoc::GetComponents and checking for the existence of the folder before trying to create one or move the current component.


      The problem with this approach is that it wants to move components that are already in folders. The current macro is functional but it would run much faster if I could skip components that are already in folders. I can't seem to figure out how to do that.


      I have tried traversing the getFeatures array via the featuremanager instead of the getComponents array via the AssemblyDoc but this got messy.


      I attached the macro as-is. If anyone has a suggestion for improvement, I will post the latest version when I incorporate those changes.




      John Alexander

        • Re: Detect if Assembly Component is in a folder
          Peter Brinkhuis

          I took a look at the macro, my first impression is that it looks well put together.


          I first though you were talking about file folders, but I see you are talking about folders in the assembly itself. This code works with one folder deep. You'll need to do some work to get it to work with nested folders, but I think you can figure out a way to do that.


          Option Explicit


          Sub main()

              Dim swApp As SldWorks.SldWorks

              Dim swModel As ModelDoc2

              Dim swFeaturefolder As FeatureFolder

              Dim swFeatureManager  As FeatureManager

              Dim vFeature  As Variant

              Dim swFeature   As Feature

              Dim withinFolder As Boolean

              Dim folderName As String


              Set swApp = Application.SldWorks

              Set swModel = swApp.ActiveDoc

              Set swFeatureManager = swModel.FeatureManager


              For Each vFeature In swFeatureManager.GetFeatures(True)

                  Set swFeature = vFeature


                  If withinFolder Then

                      Debug.Print "Skipped: " & swFeature.Name


                      'Do your magic here

                      Debug.Print "Processed : " & swFeature.Name

                  End If


                  If swFeature.GetTypeName2 = "FtrFolder" Then

                      If swFeature.Name = folderName & "___EndTag___" Then

                          Debug.Print "Going out of folder"

                          withinFolder = False

                          folderName = ""


                          Debug.Print "Going into folder"

                          withinFolder = True

                          folderName = swFeature.Name

                      End If

                  End If

              Next vFeature

          End Sub

          • Re: Detect if Assembly Component is in a folder
            John Alexander



            Thanks for the help. If I'm reading that right, folders always terminate as a feature in the FeatureManager with name "___EndTag____"?


            I don't think I'll need to handle nested folders.