Hide Table of Contents

Select Component Face By Name Example (VBA)

This example shows how to find and select a face using its name.

'-----------------------------------------------------------------------------
' Preconditions:
' 1. Open install_dir\samples\tutorial\appearances\usb_flash_drive2.sldasm.
' 2. Open the Immediate window.
'
' Postconditions:
' 1. Selects a component.
' 2. Selects a face on that component and names it.
' 3. Traverses the component's body and gets each face
'    on that body.
' 4. When the selected face matching the named face is found,
'    prints the selected face's name to the Immediate window.
' 5. Examine the Immediate window.
'
' NOTE: Because the assembly is used elsewhere, do not save changes.
'-----------------------------------------------------------------------------
Option Explicit
Dim swApp As SldWorks.SldWorks
Dim swSelMgr As SldWorks.SelectionMgr
Dim swModel As SldWorks.ModelDoc2
Dim swComp As SldWorks.Component2
Dim swFace As SldWorks.Face2
Dim boolstatus As Long
Public Sub SelectComponentFaceByName(componentName As String, faceName As String)
    Dim swBody As SldWorks.Body2
    Dim swSelData As SldWorks.SelectData
    Dim currentFaceName As String    
    Set swSelData = swSelMgr.CreateSelectData
    ' Get the component body
    Set swBody = swComp.GetBody()
    If (swBody Is Nothing) Then
        swApp.SendMsgToUser "Component body unavailable."
        swApp.SendMsgToUser "Make sure component is not lightweight or suppressed."
        Exit Sub
    End If 
    Debug.Print "Traversing faces on component's body..." 
    Set swFace = swBody.GetFirstFace
    Do While Not swFace Is Nothing
        currentFaceName = swModel.GetEntityName(swFace)
        If (currentFaceName = faceName) Then
            ' Select the face
            swFace.Select4 False, swSelData
            Debug.Print "  Name of currently selected face; should match name of previously selected face: " & currentFaceName
            Exit Do
        End If
    Set swFace = swFace.GetNextFace
Loop
' Subsequent code might select a second face,
' edge, or feature, and then mate the two
' items using AssemblyDoc::AddMate5
End Sub
Sub main()
    Dim swModelDocExt As SldWorks.ModelDocExtension
    Dim componentName As String
    Dim faceName As String
   Set swApp = CreateObject("SldWorks.Application")
    ' Get active assembly document
    Set swModel = swApp.ActiveDoc()
    Set swModelDocExt = swModel.Extension
    Set swSelMgr = swModel.SelectionManager    
    ' Select a component and get its name
    boolstatus = swModelDocExt.SelectByID2("USB_connector_shell2-1@usb_flash_drive2", "COMPONENT", 0, 0, 0, False, 0, Nothing, 0)
    Set swComp = swSelMgr.GetSelectedObject6(1, -1)
    componentName = swComp.Name2
    Debug.Print "Name of selected component: " & componentName        
    swModel.ClearSelection2 True    
    ' Select a face on the component and
    ' name it
    boolstatus = swModelDocExt.SelectByID2("", "FACE", -6.97433509299117E-03, 8.5197071911125E-04, 5.99999999997181E-03, False, 0, Nothing, 0)
    Set swFace = swSelMgr.GetSelectedObject6(1, -1)
    boolstatus = swModel.SelectedFaceProperties(0, 0, 0, 0, 0, 0, 0, True, "SideFace")
    faceName = swModel.GetEntityName(swFace)
    Debug.Print "Name of selected face on selected component: " & faceName    
    SelectComponentFaceByName componentName, faceName
End Sub
 


Provide feedback on this topic

SOLIDWORKS welcomes your feedback concerning the presentation, accuracy, and thoroughness of the documentation. Use the form below to send your comments and suggestions about this topic directly to our documentation team. The documentation team cannot answer technical support questions. Click here for information about technical support.

* Required

 
*Email:  
Subject:   Feedback on Help Topics
Page:   Select Component Face By Name Example (VBA)
*Comment:  
*   I acknowledge I have read and I hereby accept the privacy policy under which my Personal Data will be used by Dassault Systèmes

Print Topic

Select the scope of content to print:

x

We have detected you are using a browser version older than Internet Explorer 7. For optimized display, we suggest upgrading your browser to Internet Explorer 7 or newer.

 Never show this message again
x

Web Help Content Version: API Help (English only) 2015 SP05

To disable Web help from within SOLIDWORKS and use local help instead, click Help > Use SOLIDWORKS Web Help.

To report problems encountered with the Web help interface and search, contact your local support representative. To provide feedback on individual help topics, use the “Feedback on this topic” link on the individual topic page.