Hide Table of Contents

Use Safe Entity Example (VBA)

This example shows how to use IEntity::GetSafeEntity.




' Problem:

'       In general, an entity is something that can

'       be selected through the user interface.

'       Examples of entities include:

'               * vertex

'               * edge

'               * face

'               * feature


'       An entity is useful because you can add an attribute

'       to it to store arbitrary user information. Entities are

'       also useful because they support direct selection through

'       IEntity::Select4.


'       Unfortunately, under normal circumstances, an entity is

'       transient and is not valid if the model is rebuilt.

'       Thus, if a rebuild occurs, the entity must be reacquired.

'       This can be  time consuming and error prone. Furthermore,

'       it increases the programming complexity.


'       There is now the concept of a safe entity that is

'       valid across rebuilds of the model. However, this type of

'       entity is not valid across sessions of SOLIDWORKS.


'       This sample code shows how to correctly use the new

'       safe entity.


' Preconditions:

'       (1) Part, assembly or drawing is open.

'       (2) In the case of an assembly, the assembly is fully

'           resolved.

'       (3) One of the following is selected:

'               * vertex

'               * edge

'               * face

'               * feature


' Postconditions:

'       (1) Model is rebuilt.

'       (2) Entity is reselected.


' NOTE: This code does not work for a feature selected from the

'        FeatureManager design tree in a drawing.



Option Explicit


Sub main()

    Dim swApp                       As SldWorks.SldWorks

    Dim swModel                     As SldWorks.ModelDoc2

    Dim swSelMgr                    As SldWorks.SelectionMgr

    Dim swSelData                   As SldWorks.SelectData

    Dim swEnt                       As SldWorks.entity

    Dim swSafeEnt                   As SldWorks.entity

    Dim bRet                        As Boolean



    Set swApp = CreateObject("SldWorks.Application")

    Set swModel = swApp.ActiveDoc

    Set swSelMgr = swModel.SelectionManager

    Set swEnt = swSelMgr.GetSelectedObject5(1)

    Set swSafeEnt = swEnt.GetSafeEntity


    ' Clear any selections

    bRet = swModel.ForceRebuild3(False)


   ' Selection still works because entity is safe

    Set swSelData = swSelMgr.CreateSelectData

    bRet = swSafeEnt.Select4(True, swSelData)

    Debug.Assert bRet


    Debug.Print "SelCount = " & swSelMgr.GetSelectedObjectCount

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:   Use Safe Entity 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) 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.