MACRO LIBRARY

Get longest edge in a body

Free public access. No account or subscription required.

Files

Get longest edge in a body

Code

Module:

'Demonstrates how to get the longest edge in a body and its area.
    
'Preconditions: Body is selected.
'Results: Longest edge of specified type is selected and its length is _
    printed to the immediate window.
    
'Notes:
'-Use an argument of -1 for segmentType to search all curve types.
    
'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
    Dim swEdge As SldWorks.Edge
    Dim swEnt As SldWorks.Entity
    Dim dblLength As Double
    
    Set swApp = Application.SldWorks
    Set swModel = swApp.ActiveDoc
    Set swSelMgr = swModel.SelectionManager
    Set swBody = swSelMgr.GetSelectedObject6(1, -1)
    Set swEdge = GetLongestEdge(swBody, -1, dblLength)
    
    If swEdge Is Nothing Then
        Debug.Print "No edge found"
    Else
        Set swEnt = swEdge
        swEnt.Select False
        Debug.Print Round(dblLength, 6) & "m"
    End If
End Sub
    
Private Function GetLongestEdge(swBody As SldWorks.Body2, curveType As swCurveTypes_e, _
    ByRef length As Double) As SldWorks.Edge
    
    Dim vEdges As Variant
    Dim swEdge As SldWorks.Edge
    Dim swCurve As SldWorks.Curve
    Dim swCurveParamData As SldWorks.CurveParamData
    Dim dblLongestCurveLength As Double
    Dim dblCurrentCurveLength As Double
    Dim i As Integer
    
    vEdges = swBody.GetEdges
    
    If IsEmpty(vEdges) Then Exit Function
    
    length = 0
    
    For i = 0 To UBound(vEdges)
        Set swEdge = vEdges(i)
        Set swCurve = swEdge.GetCurve
        Set swCurveParamData = swEdge.GetCurveParams3
        
        If edgeType <> -1 And swCurveParamData.curveType <> curveType Then GoTo Skip
    
        dblCurrentCurveLength = swCurve.GetLength3(swCurveParamData.UMinValue, swCurveParamData.UMaxValue)
        If dblCurrentCurveLength > length Then
            length = dblCurrentCurveLength
            Set GetLongestEdge = swEdge
        End If
        
Skip:
    Next i
End Function