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.
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
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
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.
Yeah I just found that out as well. You can just try it by printing every feature name. The name is actually the folder name plus ___EndTag___, so in my test it was Folder1___EndTag___
I implemented the changes, it runs a lot faster. Thanks again.
Two other things that I observed as I was working on it:
If TopLevelOnly is false, then this method gets all of the feature and child features in this document. It does not get features in components.
The features that are returned by this method can be in any order. You should not rely on the order to indicate anything about children or parents. If hierarchy and order information is needed, then use IPartDoc::FirstFeature or IPartDoc::IFirstFeature or IModelDoc2::FirstFeature or IModelDoc2::FirstFeature, IFeature::GetNextFeature or IFeatureIGetNextFeature, IFeature::GetFirstSubFeature or IFeature::IGetFirstSubFeature, and IFeature::GetNextSubFeature or IFeature::IGetNextSubFeature to retrieve your information.
The documentation claims that this method is not guaranteed to return features in the order that they appear on the tree. I should use the alternative method instead but it seems to behave as it is - this is using SW 2012.
2) The behavior of folder features is weird. I can rename a folder using swFeature.Name = "NewName" and it shows up properly on the design tree as "NewName". However, the folder terminator feature with form "FolderName___EndTag___" remains unchanged (i.e. "Folder1___EndTag___" instead of "NewName___EndTag___"). I'm not certain if the naming convention of the folder tags is important to the normal functioning of SolidWorks so it could be dangerous if the folder feature is renamed without updating the terminator to match. Again, so far, I haven't experienced problems.
TreeOrganizer_V02.swp.zip 20.3 KB
Regarding point 1: you could indeed also use:
set swFeat = swModel.FirstFeature
set swFeat = swFeat.GetNextFeature
Regarding 2: do you rename the feature within a loop? In that case you probably need to rebuild or re-get all features. It would probably be best to rename the folder after the loop where you move the files into folders, so to make a new loop for just renaming folders.