Delete ghost cutlist folders that prevent renaming

Sometimes editing weldments cutlists causes some deleted folder to become invisible and their folder name cannot be used to rename the other cutlist folders, since it is already in use.

One reddit user posted the macro below, he got close to make it work, but missed the correct way to delete the multiple selection of invisible folders.

I tested the macro a couple of times with files that had like a dozen of “ghost folders” inside.

Option Explicit
Sub Main()
    Dim SwApp As SldWorks.SldWorks
    Dim swModel As SldWorks.ModelDoc2
    Dim swFeat As SldWorks.Feature
    Dim swSubFeat As SldWorks.Feature

    Set SwApp = Application.SldWorks
    Set swModel = SwApp.ActiveDoc
    If swModel Is Nothing Then MsgBox ("Open a part"): Exit Sub
    If swModel.GetType <> swDocumentTypes_e.swDocPART Then MsgBox ("Open a part"): Exit Sub

    swModel.ClearSelection2 True
    Set swFeat = swModel.FirstFeature
    While Not swFeat Is Nothing
        Set swSubFeat = swFeat.GetFirstSubFeature
        While Not swSubFeat Is Nothing
            CleanFolderName swModel, swSubFeat
            Set swSubFeat = swSubFeat.GetNextSubFeature
        Wend
        Set swFeat = swFeat.GetNextFeature
    Wend

    'edited this line to make it work. 
    'You may add a counter to delete only if the selection is >0 elements
    swmodel.extension.deleteselection2(0)


End Sub

Sub CleanFolderName(swModel As SldWorks.ModelDoc2, swFeat As SldWorks.Feature)
    Dim swCutFolder As SldWorks.BodyFolder
    If swFeat.GetTypeName2 <> "CutListFolder" Then Exit Sub
    Set swCutFolder = swFeat.GetSpecificFeature2
    If swCutFolder Is Nothing Then Exit Sub
    If swCutFolder.GetBodyCount > 0 Then Exit Sub
    Debug.Print "'" & swFeat.Name & "' CutListFolder Deleted"
    swFeat.Select2 True, -1
End Sub

Never encountered this issue, but thanks for sharing. Might come handy sometime :slight_smile:

You are welcome. It easily goes unnoticed, unless you have to rename cutlist folders.

We use very big cutlists with 100s folders and easily over 300 bodies. Sometimes when the file is edited, deleting features, cutlist folders are just hidden in the tree and an error comes up when trying to recycle the previously used folder name. Traversing the tree with SW API, we can list all the folders, including the “deleted” ones that are still there, empty and unselectable, causing the issue.

It is not the first time that SW uses to play the “hide trick” with some feature in the modelling tree. Depending on SW version and SP assy configurations, exploded views and move/copy had some “hidden feature” popping up here and there iirc.