Creating Drawing Dimensions Causes System.AccessViolationException

Hello-

I have been attempting to automate Sheet Metal drawings for a while. As I get ever closer, I come across more and more difficult problems.

I am opening up a drawing, and am cycling through each visible edge for each view.

In the below code I then apply a Dimension when I find two matching orientation edges. This works fine, the problem comes when I try and do this for a batch of drawings. When I loop through the code over multiple drawings I inevitably get the following error:

An exception of type 'System.AccessViolationException' occurred in PHOEN-IX.dll and wasn't handled before a managed/native boundary

Additional information: Attempted to read or write protected memory. This is often an indication that other memory is corrupt.a'' <<<<-------------- ERRORS AT THIS LINE

AT LINES:

122 & 157

I can not seem to locate any sort of solution to this problem. I am hoping that there is just a small mistake in my code that will solve the problem, but so far nothing seems to have solved the problem. I have even sprinkled GC.Collects into the code to try and free up all of the memory as I go.

And help would be greatly appreciated.

Thanks-

Code Sample

Sub Test1(ByRef swthatEdge As sldworks.Edge, ByRef swthatView As sldworks.View)

        GC.Collect()

        Const nTolerance As Double = 0.00000001

       Const nXoffset As Double = 0.01

        Const nYoffset As Double = 0.01

        Dim swthisApp As sldworks.SldWorks

        Dim swthisModel As sldworks.ModelDoc2

        Dim swthisSelMgr As sldworks.SelectionMgr

        Dim swthisEnt As sldworks.Entity

        Dim swCurve As sldworks.Curve

        Dim vCurveParam As Object

        Dim nDummy As Long

        Dim nIdentity As Long

        Dim nTag As Long

        Dim nSense As Long

        Dim swMathUtil As sldworks.MathUtility

        Dim nPtData(2) As Double

        Dim vPtData As Object

        Dim swModelStartPt As sldworks.MathPoint

        Dim swModelEndPt As sldworks.MathPoint

        Dim swViewStartPt As sldworks.MathPoint

        Dim swViewEndPt As sldworks.MathPoint

        Dim swthisView As sldworks.View

        Dim swViewXform As sldworks.MathTransform

        Dim vOutline As Object

        Dim swDispDim As sldworks.DisplayDimension

        Dim nXpos As Double

        Dim nYpos As Double

        Dim bRet2 As Boolean

        Dim bRet3 As Boolean

        swthisApp = CreateObject("SldWorks.Application")

        swthisModel = swthisApp.ActiveDoc

        swthisView = swthatView

        swthisSelMgr = swthisModel.SelectionManager

        swCurve = swthatEdge.GetCurve

        swthisEnt = swthatEdge

        vCurveParam = swthisEnt.GetCurveParams2

        DoubleIntConv.Unpack(vCurveParam(8), nDummy, nIdentity)

        DoubleIntConv.Unpack(vCurveParam(9), nDummy, nTag)

        DoubleIntConv.Unpack(vCurveParam(10), nDummy, nSense)

           ' Derived quantity

        Debug.Print("Length     = " & swCurve.GetLength2(vCurveParam(6), vCurveParam(7)) * 1000.0# & " mm ")

        Debug.Print("")

        ' Only makes sense for straight edges

        If LINE_TYPE <> nIdentity Then Exit Sub

        swMathUtil = swthisApp.GetMathUtility

        nPtData(0) = vCurveParam(0)

        nPtData(1) = vCurveParam(1)

        nPtData(2) = vCurveParam(2)

        vPtData = nPtData

        swModelStartPt = swMathUtil.CreatePoint(vPtData)

        nPtData(0) = vCurveParam(3)

        nPtData(1) = vCurveParam(4)

        nPtData(2) = vCurveParam(5)

        vPtData = nPtData

        swModelEndPt = swMathUtil.CreatePoint(vPtData)

        ' Start creating drawing of the model

        ' Select regardless

        swthisModel.ClearSelection()

        bRet2 = swthisView.SelectEntity(swthisEnt, True)

        'Debug.Assert(bRet)

        vOutline = swthisView.GetOutline

        swViewXform = swthisView.ModelToViewTransform

        swViewStartPt = swModelStartPt.MultiplyTransform(swViewXform)

        swViewEndPt = swModelEndPt.MultiplyTransform(swViewXform)

              If Math.Abs(swViewStartPt.ArrayData(0) - swViewEndPt.ArrayData(0)) < nTolerance Then

            ' Must be vertical

            'If Not entPreviouseHorEdge Is Nothing Then

            '    swthisModel.ClearSelection()

            '    entPreviouseVertEdge = swthisEnt

            '    GoTo nextone

            'End If

            ' Place dimension midway up edge and to the right of view

            nXpos = vOutline(0) - nXoffset

            nYpos = Math.Abs((swViewStartPt.ArrayData(1) + swViewEndPt.ArrayData(1)) / 2.0#)

            'If this is missing then next line fails for unknown reason

            'swthisModel.ForceRebuild3(False)

            If Not entPreviouseVertEdge Is Nothing Then

                GC.Collect()

                bRet3 = swthisView.SelectEntity(entPreviouseVertEdge, True) '' <<<<-------------- ERRORS AT THIS LINE

                entPreviouseVertEdge = Nothing

                swDispDim = swthisModel.AddVerticalDimension2(nXpos, nYpos, 0.0#)

                swthisModel.ClearSelection()

                swDispDim = Nothing

                bRet3 = Nothing

                swthisModel = Nothing

            Else

                entPreviouseVertEdge = swthisEnt

                swthisModel.ClearSelection()

            End If

            ' NULL if cannot convert edge in this view

        ElseIf Math.Abs(swViewStartPt.ArrayData(1) - swViewEndPt.ArrayData(1)) < nTolerance Then

            ' Must be horizontal

            'If Not entPreviouseVertEdge Is Nothing Then

            '    swthisModel.ClearSelection()

            '    entPreviouseHorEdge = swthisEnt

            '    GoTo nextone

            'End If

            ' Place dimension midway across edge and above view

            nXpos = Math.Abs((swViewStartPt.ArrayData(0) + swViewEndPt.ArrayData(0)) / 2.0#)

            nYpos = vOutline(3) + nYoffset

            ' NULL if cannot convert edge in this view

            'If this is missing then next line fails for unknown reason

            'swthisModel.ForceRebuild3(False)

            If Not entPreviouseHorEdge Is Nothing Then

                GC.Collect()

                bRet3 = swthisView.SelectEntity(entPreviouseHorEdge, True)'' <<<<-------------- ERRORS AT THIS LINE

                entPreviouseHorEdge = Nothing

                swDispDim = swthisModel.AddVerticalDimension2(nXpos, nYpos, 0.0#)

                swthisModel.ClearSelection()

            Else

                entPreviouseHorEdge = swthisEnt

                swthisModel.ClearSelection()

            End If

        Else

            ' Neither horizontal or vertical

            ' Place dimension near middle of edge

            nXpos = Math.Abs((swViewStartPt.ArrayData(0) + swViewEndPt.ArrayData(0)) / 2.0#) + nXoffset

            nYpos = Math.Abs((swViewStartPt.ArrayData(1) + swViewEndPt.ArrayData(1)) / 2.0#) + nYoffset

            ' Depends on the orientation of the entity in the drawing view,

            ' thus, could be NULL

            '

            ' Create the dimension even if the entity is not

            ' visible in the drawing view

            'If this is missing then next line fails for unknown reason

            'swthisModel.ForceRebuild3(False)

            swDispDim = swthisModel.AddDimension2(nXpos, nYpos, 0.0#)

            swthisModel.ClearSelection()

        End If

        swthisModel = Nothing

        swDispDim = Nothing

        bRet2 = Nothing

        bRet3 = Nothing

        swCurve = Nothing

        swthisEnt = Nothing

        vCurveParam = Nothing

        swthisApp = Nothing

        swthisView = Nothing

        swthisSelMgr = Nothing

        GC.Collect()

        GC.WaitForFullGCComplete()

    End Sub

SolidworksApi macros