Surface Trim problems

I'm trying to automate a surface trim using a fit spline. What I have here is not working - when the AddTrimmingLoop2 function is called, Solidworks crashes. No debugging error, just a hard crash. The spline values are found using the 'Get BCurve Spline Points' Example, and is a single, closed spline describing the trim I want to occur. Does this function require more that one curve? Or does the surface need to be created within the scope of the function in order to access the TrimmingLoops?

Is there a better way of going about this?

'Function takes the cut curves data and trims the main surface (in the form of persistent sketch splines)

'

Function TrimCurves(swModel As SldWorks.ModelDoc2, swSelMgr As SldWorks.SelectionMgr, CutArr() As Variant, persistID() As Variant) As Boolean

Dim swFeat                   As SldWorks.feature

Dim swSketch               As SldWorks.Sketch

Dim bret                        As Boolean

Dim swSelData              As SldWorks.SelectData

Dim i                             As Integer

Dim j                             As Integer

Dim lastFeat                  As SldWorks.feature

Dim typeName               As String

Dim vFaceArr                 As Variant

Dim uFace                     As SldWorks.Face2

Dim cutSurface              As SldWorks.Surface

Dim mainSurf                 As SldWorks.Surface

Dim vBody                     As Variant

Dim swBody                  As SldWorks.Body2

Dim sBodySelStr            As String

Dim sPadStr                  As String

Dim sBodyTypeSelStr     As String

Dim vSketchSeg             As Variant

Dim swSketchSeg          As SldWorks.SketchSegment

Dim noSpline                 As Boolean

Dim segString                As String

Dim isGood                    As Boolean

'Spline parameters

Dim numCurves           As Long

Dim Order                   As Long

Dim Dimen                  As Long

Dim Periodic                As Long

Dim NumKnots            As Long

Dim NumCtrlPoints       As Long

Dim Knots()                 As Double

Dim CtrlPointsDbl()       As Double

Dim UVRange(3)          As Double

    'Force Rebuild

    bret = swModel.ForceRebuild3(True)

   

    'Set data

    Set swSelData = swSelMgr.CreateSelectData

   

    'Find surface body

    vBody = swModel.GetBodies2(swSheetBody, True)

    For j = 0 To UBound(vBody)

        Set swBody = vBody(j)

        sBodySelStr = swBody.GetSelectionId

        Debug.Print "  " & sPadStr & sBodySelStr

       

        Select Case swBody.GetType

            Case swSolidBody

                sBodyTypeSelStr = "SOLIDBODY"

               

            Case swSheetBody

                sBodyTypeSelStr = "SURFACEBODY"

                'get body faces

                vFaceArr = swBody.GetFaces

                Set mainSurf = vFaceArr(0).GetSurface

                'Select surface body

                'bret = swModel.Extension.SelectByID2(sBodySelStr, sBodyTypeSelStr, 0#, 0#, 0#, True, 0, Nothing, swSelectOptionDefault): Debug.Assert bret

               

            Case Else

                Debug.Assert False

        End Select

    Next j

   

For i = 0 To UBound(persistID)

    'Set swSelData = swSelMgr.CreateSelectData

    Set swSketchSeg = swModel.Extension.GetObjectByPersistReference3(persistID(i), Empty)

    bret = swSketchSeg.Select4(False, swSelData)

    segString = swSketchSeg.GetType

   

'Ensure spline inputed as a sketch segment

    If swSketchSeg.GetType = 3 Then

        'Find the spline parameters to pass to the trimming loop

        bret = SplineParams1.splineParameters(swSketchSeg, Order, Dimen, Periodic, NumKnots, NumCtrlPoints, Knots, CtrlPointsDbl, UVRange)

    Else

        noSpline = True

    End If

   

    numCurves = 1

    UVRange(0) = 0

    UVRange(1) = 1

    UVRange(2) = 0

    UVRange(3) = 1

   

    'Create the trimming loop on the Surface

     isGood = mainSurf.AddTrimmingLoop2(numCurves, Order, Dimen, Periodic, NumKnots, NumCtrlPoints, Knots, CtrlPointsDbl, UVRange)

Next i

End Function

SolidworksApi macros