Page 1 of 1

Can you create Subfolders in the Feature Tree?

Posted: Wed Feb 12, 2025 7:19 am
by loeb
I am trying to create folders and subfolders in the feature tree. I have found that with the InsertFeatureTreeFolder2 method, you can create empty folders or folders that contain existing features, but the API help doesn't clarify if it can contain existing folder features, thereby turning the existing folders into subfolders of the new folder.

The help for the InsertSubFolder method explicitly states that it can move existing body subfolders into a new folder. I am hoping the same is true of 'regular' folders per InsertFeatureTreeFolder2.

I have not been able to find any other discussions or examples that answer my question and can't afford to put in a lot of effort only to discover that there might a 'no subfolders' limitation to the InsertFeatureTreeFolder2 method.

Thank You,

Re: Can you create Subfolders in the Feature Tree?

Posted: Wed Feb 12, 2025 12:50 pm
by gupta9665
I have used InsertFeatureTreeFolder2 to create empty folders but it should work to add folders (making them sub folders) as well.

You will have to have the desired folders selected.

I just tested it, and it works OK, and moved the selected folders into the new folder.

Re: Can you create Subfolders in the Feature Tree?

Posted: Wed Feb 12, 2025 3:35 pm
by loeb
Thank you, Deepak!

Re: Can you create Subfolders in the Feature Tree?

Posted: Wed Feb 12, 2025 4:36 pm
by Stefan Sterk
Hi Loeb,

You could use ReorderFeature' to place a new 'sub' folder within a folder. As shown in the code below.

Code: Select all

Sub main()
    
    Set swApp = Application.SldWorks
    Set swDoc = swApp.ActiveDoc
    
    If swDoc Is Nothing Then End
    If swDoc.GetType <> 1 And swDoc.GetType <> 2 Then End
    
    Set swDocExt = swDoc.Extension
    Set swSelMgr = swDoc.SelectionManager
    Set swFeatMgr = swDoc.FeatureManager
    
    If swSelMgr.GetSelectedObjectType3(1, -1) <> 94 Then MsgBox "Please select a folder": End
    
    ' Get selected folder
    Set swFeatFolder = swSelMgr.GetSelectedObject6(1, -1)
    
    ' Create new empty 'sub'  folder
    Set swFeatSubFolder = swFeatMgr.InsertFeatureTreeFolder2(swFeatureTreeFolderType_e.swFeatureTreeFolder_EmptyBefore)
    
    ' Move 'sub' folder inside selected folder
    bRet = swDocExt.ReorderFeature(swFeatSubFolder.Name, swFeatFolder.Name, swMoveLocation_e.swMoveToFolder)
    If bRet Then MsgBox "Created subfolder '" & swFeatSubFolder.Name & "' inside folder '" & swFeatFolder.Name & "'."
    
End Sub


Re: Can you create Subfolders in the Feature Tree?

Posted: Wed Feb 12, 2025 5:37 pm
by loeb
Stefan Sterk wrote: Wed Feb 12, 2025 4:36 pm Hi Loeb,

You could use ReorderFeature' to place a new 'sub' folder within a folder. As shown in the code below.
Stefan,

Thanks, that's exactly what I'm looking for. I had already found ReorderComponent, but didn't realize that ReorderFeature was an available method.

Since a folder is actually more than one feature (FtrFolder, ___EndTag___, and any contents that might be in the folder), I'll have to figure out how that works. If you move the FtrFolder feature, does all the rest automatically go with it?

Re: Can you create Subfolders in the Feature Tree?

Posted: Sat Mar 01, 2025 9:08 pm
by loeb
loeb wrote: Wed Feb 12, 2025 5:37 pm If you move the FtrFolder feature, does all the rest automatically go with it?
FYI, I confirmed that if you move an assembly feature folder, its contents moves with it (subfolders, assemblies, parts).