Select components associated with attributes using SOLIDWORKS API

Edit ArticleEdit Article

This example attaches to the selection SOLIDWORKS API events of the active assembly via NewSelectionNotify notification.

If the attribute is selected and there is a component associated with this attribute - this component will be selected in the tree.

Macro will stop once the active assembly is closed.

Macro module

Dim swApp As SldWorks.SldWorks
Dim swEventsListener As EventsListener

Sub main()

    Set swApp = Application.SldWorks

    Set swEventsListener = New EventsListener
        
    Dim swAssy As SldWorks.AssemblyDoc
    
    Set swAssy = swApp.ActiveDoc
    
    swEventsListener.SetAssembly swAssy
    
    While swApp.ActiveDoc Is swAssy
        DoEvents
    Wend
        
End Sub

EventsListener class

Dim swModel As SldWorks.ModelDoc2
Dim WithEvents swAssy As SldWorks.AssemblyDoc
Dim swSelMgr As SldWorks.SelectionMgr

Private Function swAssy_NewSelectionNotify() As Long
    
    Dim swFeat As SldWorks.Feature
    Dim swAtt As SldWorks.Attribute
    Dim swComp As SldWorks.Component2

    Dim i As Integer
    
    i = swSelMgr.GetSelectedObjectCount2(-1)
    
    If i > 0 Then
        
        On Error Resume Next
        
        Set swFeat = swSelMgr.GetSelectedObject6(i, -1)
        
        If Not swFeat Is Nothing Then
        
            If swFeat.GetTypeName2 = "Attribute" Then
            
                Set swAtt = swFeat.GetSpecificFeature2
            
                Set swComp = swAtt.GetComponent()
            
                swComp.Select4 True, Nothing, False
                
            End If
            
        End If
        
    End If
    
    Set swFeat = Nothing
    
End Function

Sub SetAssembly(assy As SldWorks.AssemblyDoc)
        
    Set swAssy = assy
    
    Set swModel = swAssy
        
    Set swSelMgr = swModel.SelectionManager
       
End Sub



Product of Xarial Product of Xarial