Get Faces Associated with Feature Example (VB.NET)
In SolidWorks, a face is the result of evaluating a feature. A face can be
owned by several features.
IFeature::GetFaces returns all faces owned by a feature. This is different from
faces highlighted in the user interface when a feature is selected, because the user
interface filters out multiple feature faces. This filter is for display
purposes only.
An application must use IFace::GetFeature to filter out multiple feature faces.
This method returns only the oldest feature from face; that is, the first owning
feature in the FeatureManager design tree.
This example shows how to eliminate multiple feature faces.
'-----------------------------------------------
' Preconditions:
' 1. Open a part document.
' 2. Select a feature in the FeatureManager design
' tree.
' 3. Open the Immediate window.
' 4. Run the macro.
'
' Postconditions:
' 1. Prints the name of the feature and number
' of faces to the Immediate window. Examine
' the Immediate window.
' 2. Colors the faces of the feature blue. The
' faces are the same faces as if the you selected
' the feature via the user interface.
'-----------------------------------------------
Imports SolidWorks.Interop.sldworks
Imports SolidWorks.Interop.swconst
Imports System.Runtime.InteropServices
Imports System
Imports System.Diagnostics
Partial Class SolidWorksMacro
Public Sub Main()
Dim swModel As ModelDoc2
Dim swSelMgr As SelectionMgr
Dim swSelData As SelectData
Dim swFeat As Feature
Dim swFaceFeat As Feature
Dim faceArr As Object
Dim oneFace As Object
Dim featColors As Object
Dim swFace As Face2
Dim swEnt As Entity
Dim status As Boolean
swModel = swApp.ActiveDoc
swSelMgr = swModel.SelectionManager
swFeat = swSelMgr.GetSelectedObject6(1, -1)
swSelData = swSelMgr.CreateSelectData
Debug.Print("Feature = " + swFeat.Name + " [" + swFeat.GetTypeName + "]")
Debug.Print(" Face count = " & swFeat.GetFaceCount)
swModel.ClearSelection2(True)
featColors = swModel.MaterialPropertyValues
featColors(0) = 0 'R
featColors(1) = 0 'G
featColors(2) = 1 'B
faceArr = swFeat.GetFaces : If IsNothing(faceArr) Then Exit Sub
For Each oneFace In faceArr
swFace = oneFace
swEnt = swFace
swFaceFeat = swFace.GetFeature
' Check to see if face is owned by multiple features
If swFaceFeat Is swFeat Then
status = swEnt.Select4(True, swSelData) : Debug.Assert(status)
swFace.MaterialPropertyValues = featColors
Else
Debug.Print(" Other feature = " & swFaceFeat.Name + " [" + swFaceFeat.GetTypeName + "]")
End If
Next
End Sub
''' <summary>
''' The SldWorks swApp variable is pre-assigned for you.
''' </summary>
Public swApp As SldWorks
End Class