MACRO LIBRARY

Isolate parent sub-assemblies of selected components

Free public access. No account or subscription required.

Instructions

Preconditions
Assembly is open and components are selected.
Result
Sub-assemblies containing selected components are isolated.

Files

Isolate parent sub-assemblies of selected components

Code

'Precondition: assembly is open and components are selected
'Results: sub-assemblies containing selected components are isolated
    
'Written by Keith Rice
'CADSharp LLC
'www.cadsharp.com
    
Dim swApp As SldWorks.SldWorks
Dim swModel As SldWorks.ModelDoc2
Dim swSelMgr As SldWorks.SelectionMgr
Dim swComp As SldWorks.Component2
Dim colSubAssys As New Collection
Dim i As Integer
    
Sub main()
    Set swApp = Application.SldWorks
    Set swModel = swApp.ActiveDoc
    Set swSelMgr = swModel.SelectionManager
    For i = 1 To swSelMgr.GetSelectedObjectCount2(-1)
        Set swComp = swSelMgr.GetSelectedObjectsComponent3(i, -1)
        If Not swComp Is Nothing Then
            Set swComp = swComp.GetParent
            If Not swComp Is Nothing Then
                colSubAssys.Add swComp
                Debug.Print "Added: " & swComp.Name2
            End If
        End If
    Next i
    swModel.ClearSelection2 True
    Debug.Print colSubAssys.Count
    For i = 1 To colSubAssys.Count
        colSubAssys.Item(i).Select4 True, Nothing, False
    Next i
    If colSubAssys.Count > 0 Then swModel.Extension.RunCommand swCommands_Comp_Isolate, Empty
    Set colSubAssys = Nothing
End Sub