Get Faces Affected by Scale Feature Example (VBA)
This example shows how to get the faces affected by a scale feature.
'--------------------------------------------------
'
' Problem:
' In
SolidWorks, a feature is a collection of faces.
' The
faces created by a feature can be retrieved through
' IFeature::GetFaces.
'
' However,
certain types of features only affect existing
' faces
and do not create any new faces. Examples of
' these
types of features are a draft or a scale feature.
' For
these features, IFeature::GetFaces returns an
' empty
array (no faces).
'
' To
get the faces affected by a scale feature,
' first
get the solid bodies to which the
' scale
feature is applied. Each body is composed of
' a
number of faces, which you can use to determine where
' these
faces are located after the scale feature has
' been
applied to the body.
'
' The
interesting issue is how to locate a face after
' a
rebuild because, after a rebuild, a face may no longer
' exist.
To address this, SolidWorks has safe entities,
' which
are entities that survive rebuilds; thus, are
' valid
across rebuilds.
'
' This
sample code shows how to use IScaleFeatureData::Bodies
' and
IEntity::GetSafeEntity to get the faces affected
' by
a scale feature.
'
' Preconditions:
' (1)
Part or assembly is open.
' (2)
Assembly is fully resolved.
' (3)
Scale feature is selected.
'
' Postconditions:
' (1)
Scale feature is deselected.
' (2)
Faces affected by scale feature are selected.
'
'--------------------------------------------------
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
swFeat As
SldWorks.feature
Dim
swScaleFeat As
SldWorks.ScaleFeatureData
Dim
vBody As
Variant
Dim
vBodyArr As
Variant
Dim
swBody As
SldWorks.body2
Dim
swFace As
SldWorks.face2
Dim
swEnt As
SldWorks.Entity
Dim
swSafeEnt As
SldWorks.Entity
Dim
vSafeEnt As
Variant
Dim
swSafeEntColl As
New Collection
Dim
i As
Long
Dim
bRet As
Boolean
Set
swApp = CreateObject("SldWorks.Application")
Set
swModel = swApp.ActiveDoc
Set
swSelMgr = swModel.SelectionManager
Set
swFeat = swSelMgr.GetSelectedObject5(1)
Set
swScaleFeat = swFeat.GetDefinition
Debug.Assert
0 = swFeat.GetFaceCount
bRet
= swScaleFeat.AccessSelections(swModel,
Nothing)
vBodyArr
= swScaleFeat.Bodies
For
Each vBody In vBodyArr
Set
swBody = vBody
Set
swFace = swBody.GetFirstFace
Do
While Not swFace Is Nothing
'
Face reference is only valid in rolled back
'
state, so get a more persistent face reference
Set
swEnt = swFace
Set
swSafeEnt = swEnt.GetSafeEntity
swSafeEntColl.Add swSafeEnt
Set
swFace = swFace.GetNextFace
Loop
Next
swScaleFeat.ReleaseSelectionAccess
For
Each vSafeEnt In swSafeEntColl
Set
swSafeEnt = vSafeEnt
bRet
= swSafeEnt.Select4(True, swSelData)
Debug.Assert
bRet
Next
End Sub
'--------------------------------------------------