MACRO LIBRARY

Get diameter of largest circular edge in body

Free public access. No account or subscription required.

Files

Get diameter of largest circular edge in body

Code

Module: mMain

'Demonstrates how to get the diameter of the largest circular curve in a body. In this example, _
    the first body obtained is used.
    
'Preconditions: Part is open.
'Result: Diamter is printed in meters to immediate window. If a -1 is returned, then there _
    are no circular edges in the model.
    
'Written by Keith Rice
'CADSharp LLC
'www.cadsharp.com
    
Option Explicit
    
Sub main()
    Dim swApp As SldWorks.SldWorks
    Dim swModel As SldWorks.ModelDoc2
    Dim swPart As SldWorks.PartDoc
    Dim swBody As SldWorks.Body2
    Dim vBodies As Variant
    
    Set swApp = Application.SldWorks
    Set swModel = swApp.ActiveDoc
    Set swPart = swModel
    vBodies = swPart.GetBodies2(swAllBodies, False)
    Set swBody = vBodies(0)
    
    If swBody Is Nothing Then Exit Sub
    
    Debug.Print CStr(GetDiamOfLargestCircularEdgeInBody(swBody))
    
End Sub
    
Private Function GetDiamOfLargestCircularEdgeInBody(swBody As SldWorks.Body2) As Double
    Dim vEdges As Variant
    Dim swEdge As SldWorks.Edge
    Dim swCurve As SldWorks.Curve
    Dim swCurveParamData As SldWorks.CurveParamData
    Dim dblLargestCurveLength As Double
    Dim dblCurrentCurveLength As Double
    Dim i As Integer
    
    dblLargestCurveLength = -1
    
    vEdges = swBody.GetEdges
    For i = 0 To UBound(vEdges)
        Set swEdge = vEdges(i)
        Set swCurve = swEdge.GetCurve
        Set swCurveParamData = swEdge.GetCurveParams3
        If swCurveParamData.CurveType = CIRCLE_TYPE Then
            dblCurrentCurveLength = swCurve.GetLength3(swCurveParamData.UMinValue, swCurveParamData.UMaxValue)
            If dblCurrentCurveLength > dblLargestCurveLength Then dblLargestCurveLength = dblCurrentCurveLength
        End If
    Next i
    
    If dblLargestCurveLength = -1 Then
        GetDiamOfLargestCircularEdgeInBody = -1
    Else
        'pi is equal to 4 x the arctangent of 1
        GetDiamOfLargestCircularEdgeInBody = dblLargestCurveLength / (4 * Atn(1))
    End If
End Function