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
'--------------------------------------------