MACRO LIBRARY

Select an entity by its name

Free public access. No account or subscription required.

Files

Select an entity by its name

Code

'Selecting using IModelDoc2::GetEntityByName
    
'Preconditions: part is open with face named "F1"
'Postconditions: face named "F1" is colored green
    
'Written by Keith Rice
'CADSharp LLC
'www.cadsharp.com
    
Option Explicit
    
Dim swApp As SldWorks.SldWorks
Dim swModel As SldWorks.ModelDoc2
Dim swPart As SldWorks.PartDoc
Dim swFace As SldWorks.Face2
Dim swEnt As SldWorks.Entity
Dim swSelectData As SldWorks.SelectData
Dim vFaceProp As Variant
    
Sub main()
    Set swApp = Application.SldWorks
    Set swModel = swApp.ActiveDoc
    Set swPart = swModel
    Set swFace = swPart.GetEntityByName("F1", 2)    '2 = swSelFACES
    
        If IsEmpty(vFaceProp) Then
            'Is empty if face-level colors were not specified, so get them from underlying model
            vFaceProp = swModel.MaterialPropertyValues
        End If
        
        Set swEnt = swFace
        swEnt.Select4 True, swSelectData
    
        'Change to green. RGB = 0, 255, 0
        swModel.SelectedFaceProperties RGB(0, 255, 0), vFaceProp(3), vFaceProp(4), vFaceProp(5), _
            vFaceProp(6), vFaceProp(7), vFaceProp(8), False, ""
    
        swModel.ClearSelection2 True
End Sub