True length of sweep feature

Description

This sample demonstrates the calculation of the true length of a sweep feature. If the centroid of the sweep profile does not lie on the sweep path, the true length of the sweep feature is not the same as the sum of lengths of the path entities.

Code Samples

Sub TrueSweepLength()
    'Set a reference to the active part document
    Dim oDoc As PartDocument
    Set oDoc = ThisApplication.ActiveDocument
    
    Dim oDef As PartComponentDefinition
    Set oDef = oDoc.ComponentDefinition
    
    ' Check to make sure a sweep feature is selected.
    If Not TypeOf oDoc.SelectSet.Item(1) Is SweepFeature Then
        MsgBox "A sweep feature must be selected."
        Exit Sub
    End If

    ' Set a reference to the selected feature.
    Dim oSweep As SweepFeature
    Set oSweep = oDoc.SelectSet.Item(1)
    
    ' Get the centroid of the sweep profile in sketch space
    Dim oProfileOrigin As Point2d
    Set oProfileOrigin = oSweep.Profile.RegionProperties.Centroid
    
    ' Transform the centroid from sketch space to model space
    Dim oProfileOrigin3D As Point
    Set oProfileOrigin3D = oSweep.Profile.Parent.SketchToModelSpace(oProfileOrigin)
    
    ' Get the set of curves that represent the true path of the sweep
    Dim oCurves As ObjectsEnumerator
    Set oCurves = oDef.Features.SweepFeatures.GetTruePath(oSweep.Path, oProfileOrigin3D)
    
    Dim TotalLength As Double
    TotalLength = 0
    
    Dim oCurve As Object
    For Each oCurve In oCurves
        
        Dim oCurveEval As CurveEvaluator
        Set oCurveEval = oCurve.Evaluator
        
        Dim MinParam As Double
        Dim MaxParam As Double
        Dim Length As Double
        
        Call oCurveEval.GetParamExtents(MinParam, MaxParam)
        Call oCurveEval.GetLengthAtParam(MinParam, MaxParam, Length)
        
        TotalLength = TotalLength + Length
    Next
    
    ' Display total sweep length
    MsgBox "Total sweep length = " & ThisApplication.UnitsOfMeasure.GetStringFromValue(TotalLength, kInchLengthUnits)
End Sub