Hide Table of Contents

Select All Faces of Component Example (VBA)

This example shows how to select all of the faces of the selected component.



' Preconditions:

'       (1) Assembly is open.

'       (2) Assembly is fully resolved.

'       (3) Something is selected.

'       (4) Selected item belongs to a component part.


' Postconditions:

'       (1) Initial entity is deselected.

'       (2) All faces in component part are selected.



Option Explicit

Public Enum swBodyType_e

    swSolidBody = 0

    swSheetBody = 1

    swWireBody = 2

    swMinimumBody = 3

    swGeneralBody = 4

    swEmptyBody = 5

End Enum

Sub main()

    Dim swApp                   As SldWorks.SldWorks

    Dim swModel                 As SldWorks.ModelDoc2

    Dim swSelMgr                As SldWorks.SelectionMgr

    Dim swSelData               As SldWorks.SelectData

    Dim swComp                  As SldWorks.Component2

    Dim swBody                  As SldWorks.body2

    Dim vBody                   As Variant

    Dim vBodyArr                As Variant

    Dim swFace                  As SldWorks.face2

    Dim swEnt                   As SldWorks.entity

    Dim bRet                    As Boolean


    Set swApp = CreateObject("SldWorks.Application")

    Set swModel = swApp.ActiveDoc

    Set swSelMgr = swModel.SelectionManager

    Set swSelData = swSelMgr.CreateSelectData

    Set swComp = swSelMgr.GetSelectedObjectsComponent(1)

    ' Return number of bodies

    vBodyArr = swComp.GetBodies2(swSolidBody)


    ' Probably a subassembly selected

    If IsEmpty(vBodyArr) Then Exit Sub


    ' Debugging only

    Debug.Print "Assy = " & swModel.GetPathName

    Debug.Print "  Comp = " & swComp.Name2

    Debug.Print "    Path    = " & swComp.GetPathName

    Debug.Print "    Bodies  = " & UBound(vBodyArr) + 1


    swModel.ClearSelection2 True


    For Each vBody In vBodyArr

        Set swBody = vBody

        Set swFace = swBody.GetFirstFace


        Do While Not swFace Is Nothing

            Set swEnt = swFace


            bRet = swEnt.Select4(True, swSelData): Debug.Assert bRet

            Set swFace = swFace.GetNextFace



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

Subject:   Feedback on Help Topics
Page:   Select All Faces of Component Example (VBA)
*   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:


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

Web Help Content Version: API Help (English only) 2012 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.