Can you create Subfolders in the Feature Tree?

Programming and macros
User avatar
loeb
Posts: 81
Joined: Sun Jan 16, 2022 5:55 pm
Answers: 1
x 41
x 14

Can you create Subfolders in the Feature Tree?

Unread post 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,
User avatar
gupta9665
Posts: 448
Joined: Thu Mar 11, 2021 10:20 am
Answers: 28
Location: India
x 450
x 494

Re: Can you create Subfolders in the Feature Tree?

Unread post 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.
Deepak Gupta
SOLIDWORKS Consultant/Blogger
User avatar
loeb
Posts: 81
Joined: Sun Jan 16, 2022 5:55 pm
Answers: 1
x 41
x 14

Re: Can you create Subfolders in the Feature Tree?

Unread post by loeb »

Thank you, Deepak!
User avatar
Stefan Sterk
Posts: 45
Joined: Tue Aug 10, 2021 2:40 am
Answers: 4
x 67
x 85

Re: Can you create Subfolders in the Feature Tree?

Unread post 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

User avatar
loeb
Posts: 81
Joined: Sun Jan 16, 2022 5:55 pm
Answers: 1
x 41
x 14

Re: Can you create Subfolders in the Feature Tree?

Unread post 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?
Post Reply