MACRO LIBRARY

Get IEntity pointer for entity selected in drawing view

Free public access. No account or subscription required.

Files

Get IEntity pointer for entity selected in drawing view

Code

Module: mMain

'Demonstrates how to get the IEntity pointer to an entity selected _
    in a drawing view. (e.g., face, edge, vertex)
    
'Precondition: Face, edge, or vertex is selected in drawing view.
'Result: See immediate window.
    
'Notes:
'-When you select an entity in a drawing view, both the drawing view _
'   and the entity are considered selected. The drawing view is the _
'   first selected item and the entity is the second.
    
'Written by Keith Rice
'CADSharp LLC
'www.cadsharp.com
    
Sub main()
    Dim swApp As SldWorks.SldWorks
    Dim swModel As SldWorks.ModelDoc2
    Dim swSelMgr As SldWorks.SelectionMgr
    Dim swEnt As SldWorks.Entity
    Dim i As Integer
    
    Set swApp = Application.SldWorks
    Set swModel = swApp.ActiveDoc
    Set swSelMgr = swModel.SelectionManager
    
    If swSelMgr.GetSelectedObjectType3(1, -1) = swSelDRAWINGVIEWS And _
        swSelMgr.GetSelectedObjectCount2(-1) = 2 Then
    
        Select Case swSelMgr.GetSelectedObjectType3(2, -1)
            Case swSelVERTICES
                Set swEnt = swSelMgr.GetSelectedObject6(2, -1)
                Debug.Print "Vertex selected"
            Case swSelEDGES
                Set swEnt = swSelMgr.GetSelectedObject6(2, -1)
                Debug.Print "Edge selected"
            Case swSelFACES
                Set swEnt = swSelMgr.GetSelectedObject6(2, -1)
                Debug.Print "Face selected"
            Case Else
                Debug.Print "Vertex, edge, or face not selected."
        End Select
    
    End If
End Sub