MACRO LIBRARY

Is body a 2D body

Free public access. No account or subscription required.

Files

Is body a 2D body Test model

Code

Module: mMain

'A "2D" body is a body that could be cut using only two axes.
    
'Preconditions: Body is selected.
'Results: See immediate window.
    
'Algorithm used:
'1. The two largest faces...
'   a. Are planar
'   a. Have equal surface area
'   b. Have opposite normals
'2. All other faces share an edge with two largest faces
    
'Written by Keith Rice
'CADSharp LLC
'www.cadsharp.com
    
Const ROUND_DECIMALS As Integer = 3
    
Sub main()
    Dim swApp As SldWorks.SldWorks
    Dim swModel As ModelDoc2
    Dim swSelMgr As SelectionMgr
    Dim swBody As Body2
    
    Set swApp = Application.SldWorks
    Set swModel = swApp.ActiveDoc
    Set swSelMgr = swModel.SelectionManager
    Set swBody = swSelMgr.GetSelectedObject6(1, -1)
    
    Dim strFalseReason As String
    Debug.Print "Is 2d: " & Is2dBody(swBody, strFalseReason)
    If strFalseReason <> Empty Then Debug.Print vbTab & "Reason: " & strFalseReason
End Sub
    
Private Function Is2dBody(swBody As Body2, ByRef strFalseReason As String) As Boolean
    Dim vMainFaces As Variant
    Dim vSideFaces As Variant
    Dim swFace1 As Face2
    Dim swFace2 As Face2
    
    'Get two largest faces
    vMainFaces = GetTwoLargestFaces(swBody)
    
    If IsEmpty(vMainFaces) Then
        strFalseReason = "Body does not contain at least two faces."
        Is2dBody = False
        Exit Function
    End If
    
    Set swFace1 = vMainFaces(0)
    Set swFace2 = vMainFaces(1)
    
    'Verify two largest faces are planar
    If AreFacesPlanar(vMainFaces) = False Then
        strFalseReason = "One or both largest faces are not planar."
        Is2dBody = False
        Exit Function
    End If
    
    'Verify two largest faces have equal surface area
    If IsEqualSurfaceArea(swFace1, swFace2) = False Then
        strFalseReason = "Two largest planar faces do not have equal surface area."
        Is2dBody = False
        Exit Function
    End If
    
    'Verify two largest faces have opposite normals
    If IsOppositeNormal(swFace1, swFace2) = False Then
        strFalseReason = "Two largest planar faces do not have opposite normals."
        Is2dBody = False
        Exit Function
    End If
    
    'Get side faces
    Dim colSideFaces As New Collection
    Set colSideFaces = GetSideFaces(swBody, swFace1, swFace2)
    
    'Verify that all side faces share an edge with two largest faces
    Dim i As Integer
    For i = 1 To colSideFaces.Count
        If DoesFaceShareAnEdgeWithTwoOtherFaces(colSideFaces.Item(i), swFace1, swFace2) = False Then
            strFalseReason = "Not all side faces share an edge with two largest faces."
            Is2dBody = False
            Exit Function
        End If
    Next i
    
    Is2dBody = True
End Function
    
Private Function GetTwoLargestFaces(swBody As Body2) As Variant
    Dim swFinalFaces(1) As Face2
    Dim swCurrentFace As Face2
    Dim vFaces As Variant
    Dim i As Integer
    
    vFaces = swBody.GetFaces
    
    If UBound(vFaces) < 2 Then
        GetTwoLargestFaces = Empty
        Exit Function
    End If
    
    Set swFinalFaces(0) = vFaces(0)
    
    For i = 1 To UBound(vFaces)
        Set swCurrentFace = vFaces(i)
        If swCurrentFace.GetArea > swFinalFaces(0).GetArea Then
            Set swFinalFaces(1) = swFinalFaces(0)
            Set swFinalFaces(0) = swCurrentFace
        ElseIf swFinalFaces(1) Is Nothing Then
            Set swFinalFaces(1) = swCurrentFace
        ElseIf swCurrentFace.GetArea > swFinalFaces(1).GetArea Then
            Set swFinalFaces(1) = swCurrentFace
        End If
    Next i
    
    GetTwoLargestFaces = swFinalFaces
End Function
    
Private Function AreFacesPlanar(vFaces As Variant) As Boolean
    Dim swFace As Face2
    Dim swSurf As Surface
    Dim i As Integer
    
    For i = 0 To UBound(vFaces)
        Set swFace = vFaces(i)
        Set swSurf = swFace.GetSurface
        If swSurf.IsPlane = False Then
            AreFacesPlanar = False
            Exit Function
        End If
    Next i
    
    AreFacesPlanar = True
End Function
    
Private Function IsEqualSurfaceArea(swFace1 As Face2, swFace2 As Face2) As Boolean
    If Round(swFace1.GetArea, ROUND_DECIMALS) = Round(swFace2.GetArea, ROUND_DECIMALS) Then
        IsEqualSurfaceArea = True
    Else
        IsEqualSurfaceArea = False
    End If
End Function
    
Private Function IsOppositeNormal(swFace1 As Face2, swFace2 As Face2) As Boolean
    Dim vNormal1 As Variant
    Dim vNormal2 As Variant
    
    vNormal1 = swFace1.Normal
    vNormal2 = swFace2.Normal
    
    Dim i As Integer
    Dim dblSum As Double
    For i = 0 To 2
        dblSum = vNormal1(i) + vNormal2(i)
    Next i
    
    If Round(dblSum, ROUND_DECIMALS) = 0 Then
        IsOppositeNormal = True
    Else
        IsOppositeNormal = False
    End If
End Function
    
Private Function GetSideFaces(swBody As Body2, swMainFace1 As Face2, swMainFace2 As Face2) As Collection
    Dim colSideFaces As New Collection
    Dim vFaces As Variant
    Dim i As Integer
    
    vFaces = swBody.GetFaces
    
    For i = 0 To UBound(vFaces)
        If swMainFace1.IsSame(vFaces(i)) = False And swMainFace2.IsSame(vFaces(i)) = False Then
            colSideFaces.Add vFaces(i)
        End If
    Next i
    
    Set GetSideFaces = colSideFaces
End Function
    
Private Function DoesFaceShareAnEdgeWithTwoOtherFaces(swMainFace As Face2, swFace1 As Face2, swFace2 As Face2) As Boolean
    Dim swApp As SldWorks.SldWorks
    Set swApp = Application.SldWorks
    
    Dim vMainFaceEdges As Variant
    Dim vFace1Edges As Variant
    Dim vFace2Edges As Variant
    vMainFaceEdges = swMainFace.GetEdges
    vFace1Edges = swFace1.GetEdges
    vFace2Edges = swFace2.GetEdges
    
    Dim swEdge As Edge
    Dim swEdge1 As Edge
    Dim swEdge2 As Edge
    
    Dim i As Integer, j As Integer
    Dim blnFound1 As Boolean, blnFound2 As Boolean
    
    For i = 0 To UBound(vMainFaceEdges)
        Set swEdge = vMainFaceEdges(i)
        For j = 0 To UBound(vFace1Edges)
            Set swEdge1 = vFace1Edges(j)
            If swApp.IsSame(swEdge, swEdge1) = swObjectSame Then
                blnFound1 = True
                Exit For
            End If
        Next j
        For j = 0 To UBound(vFace2Edges)
            Set swEdge2 = vFace2Edges(j)
            If swApp.IsSame(swEdge, swEdge2) = swObjectSame Then
                blnFound2 = True
                Exit For
            End If
        Next j
    Next i
    
    If blnFound1 And blnFound2 Then
        DoesFaceShareAnEdgeWithTwoOtherFaces = True
    Else
        DoesFaceShareAnEdgeWithTwoOtherFaces = False
    End If
End Function