MACRO LIBRARY

Is body created at angle / off-plane

Free public access. No account or subscription required.

Files

Is body created at angle / off-plane

Code

Module: mMain

'Demonstrates how to determine if a body was created at an "off" angle, meaning its primary faces
'   are not parallel or perpendicular to the standard part planes. If used within an assembly,
'   it could also be used to determine if a body is mated or positioned such that it is not
'   parallel or perpendicular to the standard assembly planes.
    
'Preconditions: Body is selected.
'Results: See immediate window, which will display True if body is at an off angle, otherwise False.
    
'Written by Keith Rice
'CADSharp LLC
'www.cadsharp.com
    
Sub main()
    Dim swApp As SldWorks.SldWorks
    Dim swModel As SldWorks.ModelDoc2
    Dim swSelMgr As SldWorks.SelectionMgr
    Dim swBody As SldWorks.Body2
    
    Set swApp = Application.SldWorks
    Set swModel = swApp.ActiveDoc
    Set swSelMgr = swModel.SelectionManager
    Set swBody = swSelMgr.GetSelectedObject6(1, -1)
    
    Debug.Print IsMemberAngled(swModel, swBody)
End Sub
    
Function IsMemberAngled(swModel As SldWorks.ModelDoc2, swBody As SldWorks.Body2) As Boolean
    Dim swFace As SldWorks.Face2
    Dim swSurf As SldWorks.Surface
    Dim swMeasure As SldWorks.Measure
    Dim swRefPlane As SldWorks.Feature
    Dim swEnt As SldWorks.Entity
    Dim vFaces As Variant
    Dim vRefPlanes As Variant
    Dim i As Integer, j As Integer
    
    vFaces = swBody.GetFaces
    
    If IsEmpty(vFaces) Then
        IsMemberAngled = False
        Exit Function
    End If
    
    For i = 0 To UBound(vFaces)
        Set swFace = vFaces(i)
        Set swSurf = swFace.GetSurface
        If swSurf.IsPlane Then
            Set swEnt = swFace
            Set swMeasure = swModel.Extension.CreateMeasure
            
            vRefPlanes = GetStandardPlanes(swModel)
            
            If IsEmpty(vRefPlanes) Then
                MsgBox "Failed to get refence plane features"
                Exit Function
            End If
            
            For j = 0 To 2
                swEnt.Select False
                Set swRefPlane = vRefPlanes(j)
                swRefPlane.Select True
                swMeasure.Calculate Nothing
                If swMeasure.IsParallel = False And swMeasure.IsPerpendicular = False Then
                    IsMemberAngled = True
                    swModel.ClearSelection2 False
                    Exit Function
                End If
            Next j
            
        End If
    Next i
    
    swModel.ClearSelection2 False
    IsMemberAngled = False
End Function
    
Function GetStandardPlanes(swModel As SldWorks.ModelDoc2) As Variant
    Dim swFeat As SldWorks.Feature
    Dim swRefPlaneFeats(2) As SldWorks.Feature
    Dim i As Integer
    i = 0
    
    Set swFeat = swModel.FirstFeature
    
    Do While Not swFeat Is Nothing
        If swFeat.GetTypeName = "RefPlane" Then
            Set swRefPlaneFeats(i) = swFeat
            i = i + 1
            If i = 3 Then
                GetStandardPlanes = swRefPlaneFeats
                Exit Function
            End If
        End If
        Set swFeat = swFeat.GetNextFeature
    Loop
End Function