Files
Code
Module:
'Demonstrates how to get the largest face in a body and its area.
'Preconditions: Body is selected.
'Results: Largest face of specified type is selected and its area is _
printed to the immediate window.
'Written by Keith Rice
'CADSharp LLC
'www.cadsharp.com
Private Enum FaceTypeEnum
Planar
Cylindrical
All
End Enum
Sub main()
Dim swApp As SldWorks.SldWorks
Dim swModel As SldWorks.ModelDoc2
Dim swSelMgr As SldWorks.SelectionMgr
Dim swBody As SldWorks.Body2
Dim swFace As SldWorks.Face2
Dim swEnt As SldWorks.Entity
Dim dblArea As Double
Set swApp = Application.SldWorks
Set swModel = swApp.ActiveDoc
Set swSelMgr = swModel.SelectionManager
Set swBody = swSelMgr.GetSelectedObject6(1, -1)
Set swFace = GetLargestFace(swBody, FaceTypeEnum.All, dblArea)
If swFace Is Nothing Then
Debug.Print "No face found"
Else
Set swEnt = swFace
swEnt.Select False
Debug.Print Round(dblArea, 6) & "m^2"
End If
End Sub
Private Function GetLargestFace(swBody As SldWorks.Body2, faceType As FaceTypeEnum, _
ByRef area As Double) As SldWorks.Face2
Dim swFace As SldWorks.Face2
Dim swSurf As SldWorks.Surface
Dim vFaces As Variant
Dim i As Integer
vFaces = swBody.GetFaces
If IsEmpty(vFaces) Then Exit Function
area = 0
For i = 0 To UBound(vFaces)
Set swFace = vFaces(i)
Set swSurf = swFace.GetSurface
If faceType = Planar And swSurf.IsPlane And swFace.GetArea > area Then
Set GetLargestFace = swFace
area = swFace.GetArea
ElseIf faceType = Cylindrical And swSurf.IsCylinder And swFace.GetArea > area Then
Set GetLargestFace = swFace
area = swFace.GetArea
ElseIf swFace.GetArea > area Then
Set GetLargestFace = swFace
area = swFace.GetArea
End If
Next i
End Function