Select Features of Selected Bodies

Library for macros
User avatar
josh
Posts: 253
Joined: Thu Mar 11, 2021 1:05 pm
Answers: 11
x 19
x 444

Select Features of Selected Bodies

Unread post by josh »

I was recently sent a SW file that was an entire machine assembly saved as a part, which just created thousands of solid and surface body features. I needed to clean it up significantly to make it even close to usable, but not knowing the structure or any of the components, it was hard for me to select body features from the tree rather than selecting the solid bodies in the graphics view. The problem with graphics view selection is that if you select a bunch of bodies and then hit the "delete" key, it creates a "body delete" feature rather than deleting the body features from the tree.

When run, this macro will examine the current selection set. For every solid or surface body that is selected, it will find the body feature in the feature tree. It will then de-select all the bodies and select the features instead. You can then delete the features from the tree. It will ignore any selection that's not a solid body or surface body, so activate the Solid and/or Surface body filters to select things from the graphics area.

Code: Select all

Sub main()

    Dim swApp As SldWorks.SldWorks
    Dim swDoc As SldWorks.ModelDoc2
    Dim swSels As SldWorks.SelectionMgr
    Dim swFeat As SldWorks.Feature
    Dim cFeats As Collection
    Dim swFace As SldWorks.Face2
    Dim swBod As SldWorks.Body2
    Dim i As Long
    Dim j As Long
    Dim bFound As Boolean
    
    Set swApp = Application.SldWorks
    Set swDoc = swApp.ActiveDoc
    Set swSels = swDoc.SelectionManager
    Set cFeats = New Collection
    For i = 1 To swSels.GetSelectedObjectCount2(-1)
        If (swSels.GetSelectedObjectType3(i, -1) = swSelSOLIDBODIES) Or (swSels.GetSelectedObjectType3(i, -1) = swSelSURFACEBODIES) Then
            Set swBod = swSels.GetSelectedObject6(i, -1)
            Set swFace = swBod.GetFirstFace
            Set swFeat = swFace.GetFeature
            bFound = False
            For j = 1 To cFeats.Count
                If swFeat Is cFeats(j) Then
                    bFound = True
                    Exit For
                End If
            Next j
            If Not bFound Then cFeats.Add swFeat
        End If
    Next i
    
    swDoc.ClearSelection2 True
    For i = 1 To cFeats.Count
        cFeats(i).Select True
    Next i
        
End Sub
Post Reply