Hide Table of Contents

Use Safe Entity Example (VBA)

This example shows how to use the safe entity functionality.

NOTE: In general, an entity is something that can be selected through the user interface. Examples of safe entities include vertices, edges, faces, and features. 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. Under normal conditions, an entity is transient and is not valid when the model is rebuilt. Thus, if a rebuild occurs, then the entity must be reacquired. This can be time consuming and prone to errors. Furthermore, it increases the programming complexity. Safe entities are valid across rebuilds of the model. However, this type of entity is not valid across sessions of SOLIDWORKS. 

'------------------------------------------------------------------
' Preconditions:
' 1. Open a part, fully resolved assembly, or drawing.
' 2. Select one of these entities in the graphics area:
'    * vertex
'    * edge
'    * face
'    * feature
'
' Postconditions:
' 1. Rebuilds the model.
' 2. Reselects the entity.
'
' NOTE: This code does not work for a feature selected
' in 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    
    ' Select safe entity
    Set swSelMgr = swModel.SelectionManager
    Set swEnt = swSelMgr.GetSelectedObject6(1, -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.Print "Number of safe entities selected = " & 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

 
*Email:  
Subject:   Feedback on Help Topics
Page:   Use Safe Entity 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) 2017 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.