MACRO LIBRARY

Get the length, start point, and end point of an edge

Free public access. No account or subscription required.

Files

Get the length, start point, and end point of an edge

Code

'Length and start/end locations of preselected edges is displayed.
'Based off of exercise 12 in API Fundamentals manual
    
'Written by Keith Rice
'CADSharp LLC
'www.cadsharp.com
    
Option Explicit
    
Dim swApp As SldWorks.SldWorks
Dim swModel As SldWorks.ModelDoc2
Dim swSelMgr As SldWorks.SelectionMgr
Dim swEdge As SldWorks.Edge
Dim swCurve As SldWorks.Curve
Dim swCurveParamData As SldWorks.CurveParamData
Dim vStartPt, vEndPt As Variant
Dim i As Integer
    
Sub main()
    Set swApp = Application.SldWorks
    Set swModel = swApp.ActiveDoc
    
    If swModel Is Nothing Then
        swApp.SendMsgToUser "Open a part."
        End
    End If
    
    'get selected edges
    Set swSelMgr = swModel.SelectionManager
    For i = 1 To swSelMgr.GetSelectedObjectCount2(-1)
        If swSelMgr.GetSelectedObjectType3(i, -1) = swSelectType_e.swSelEDGES Then
            Set swEdge = swSelMgr.GetSelectedObject6(i, -1)
            'get the curve
            Set swCurve = swEdge.GetCurve
            Set swCurveParamData = swEdge.GetCurveParams3
            'get the curve length; this returns the arc length in the case of an arc _
                or the circumference in the case of a circle
            Debug.Print "Length: " & Round(swCurve.GetLength3(swCurveParamData.UMinValue, _
                swCurveParamData.UMaxValue), 5) * 1000 & "mm"
            'get start and end points
            vStartPt = swCurveParamData.StartPoint
            vEndPt = swCurveParamData.EndPoint
            Debug.Print "Start point: " & vStartPt(0) & ", " & vStartPt(1) & ", " & vStartPt(2)
            Debug.Print "End point: " & Round(vEndPt(0), 3) & ", " & vEndPt(1) & ", " & vEndPt(2)
        
            'reselect using midpoint
            'for use on 5x5x5mm cube
            'select bottom X edge
            swModel.ClearSelection2 True
            swModel.Extension.SelectByID2 "", swSelectType_e.swSelEDGES, _
                vStartPt(0) + vEndPt(0) / 2, vStartPt(1), vStartPt(2), False, 0, Nothing, 0
        End If
    Next i
End Sub