MACRO LIBRARY

Test whether edges on a body are straight or curved

Free public access. No account or subscription required.

Files

Test whether edges on a body are straight or curved

Code

'Tests whether the edges on a face or body are straight or curved
    
'Written by Keith Rice
'CADSharp LLC
'www.cadsharp.com
    
'Preconditions: part is open and a face is selected
'Results: see immediate window
    
Option Explicit
    
Dim swApp As SldWorks.SldWorks
Dim swModel As SldWorks.ModelDoc2
Dim swSelMgr As SldWorks.SelectionMgr
Dim swBody As SldWorks.Body2
Dim swLoop As SldWorks.Loop2
Dim vFaceArray As Variant
Dim vFace As Variant
Dim swFace As SldWorks.Face2
Dim vEdgeArray As Variant
Dim vEdge As Variant
Dim swEdge As SldWorks.Edge
Dim swCurve As SldWorks.Curve
    
    
Sub main()
    Set swApp = Application.SldWorks
    Set swModel = swApp.ActiveDoc
    Set swSelMgr = swModel.SelectionManager
    
    'Make sure that user is in a part.
    If swModel.GetType <> swDocPART And swModel.GetType <> swDocASSEMBLY Then
        swApp.SendMsgToUser "Open on a part or assembly."
    Else
        'Make sure that a single face is selected.
        If swSelMgr.GetSelectedObjectType3(1, -1) <> _
            swSelFACES Or swSelMgr.GetSelectedObjectCount2(-1) <> 1 Then
            swApp.SendMsgToUser "Select a single face."
        Else
            'Get the face.
            Set swFace = swSelMgr.GetSelectedObject6(1, -1)
            'Prompt whether user wants to test just the selected face or the whole body.
            If MsgBox("Get the edges for the whole body and not just the face?", vbYesNo, _
                "SolidWorks") = vbYes Then
                Set swBody = swFace.GetBody
                vFaceArray = swBody.GetFaces
                Debug.Print "Face count = " & swBody.GetFaceCount
                'Determine number of loops in each face.
                For Each vFace In vFaceArray
                    Set swFace = vFace
                    Debug.Print "Loop count = " & swFace.GetLoopCount
                Next
                vEdgeArray = swBody.GetEdges
                Debug.Print "Edges count = " & swBody.GetEdgeCount
                Call TestStraightness
            Else
                Debug.Print "Loop count = " & swFace.GetLoopCount
                vEdgeArray = swFace.GetEdges
                Debug.Print "Edges count = " & swFace.GetEdgeCount
                Call TestStraightness
            End If
        End If
    End If
End Sub
    
Sub TestStraightness()
    'Look at each edge in the edge array and test whether straight or curved.
    For Each vEdge In vEdgeArray
        Set swEdge = vEdge
        Set swCurve = swEdge.GetCurve
        If swCurve.IsLine Then
            Debug.Print "This is a line."
        ElseIf swCurve.IsCircle Then
            Debug.Print "This is a circle."
        End If
    Next
End Sub
    
'Notes:
'A line is basically a straight edge.
'A circle is basically a curved edge.
'For whatever reason, a edge must be set equal to a curve before its _
    straightness can be tested.
    
'When getting the array of faces or edges, do not use set or you will get 'type mismatch'.
    
'Object model hierarchy:
'Model-->Body-->Edges-->Curves
'Face-->Body