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:

<p><strong>An exception of type 'System.AccessViolationException' occurred in PHOEN-IX.dll and wasn't handled before a managed/native boundary</strong></p><p><strong>Additional information: Attempted to read or write protected memory. This is often an indication that other memory is corrupt.</strong>a<strong style="color: rgba(0, 0, 0, 0); font-family: Consolas, 'Courier New', Courier, mono, serif; font-size: 12px; background-position: repeat;">'' <<<<-------------- ERRORS AT THIS LINE</strong></p>

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
<p>Sub Test1(ByRef swthatEdge As sldworks.Edge, ByRef swthatView As sldworks.View)</p><p>        GC.Collect()</p><p>        Const nTolerance As Double = 0.00000001</p><p></p><p></p><p>       Const nXoffset As Double = 0.01</p><p>        Const nYoffset As Double = 0.01</p><p></p><p></p><p>        Dim swthisApp As sldworks.SldWorks</p><p>        Dim swthisModel As sldworks.ModelDoc2</p><p>        Dim swthisSelMgr As sldworks.SelectionMgr</p><p>        Dim swthisEnt As sldworks.Entity</p><p>        Dim swCurve As sldworks.Curve</p><p>        Dim vCurveParam As Object</p><p>        Dim nDummy As Long</p><p>        Dim nIdentity As Long</p><p>        Dim nTag As Long</p><p>        Dim nSense As Long</p><p></p><p></p><p>        Dim swMathUtil As sldworks.MathUtility</p><p>        Dim nPtData(2) As Double</p><p>        Dim vPtData As Object</p><p>        Dim swModelStartPt As sldworks.MathPoint</p><p>        Dim swModelEndPt As sldworks.MathPoint</p><p>        Dim swViewStartPt As sldworks.MathPoint</p><p>        Dim swViewEndPt As sldworks.MathPoint</p><p></p><p></p><p>        Dim swthisView As sldworks.View</p><p>        Dim swViewXform As sldworks.MathTransform</p><p>        Dim vOutline As Object</p><p>        Dim swDispDim As sldworks.DisplayDimension</p><p></p><p></p><p>        Dim nXpos As Double</p><p>        Dim nYpos As Double</p><p>        Dim bRet2 As Boolean</p><p>        Dim bRet3 As Boolean</p><p></p><p></p><p>        swthisApp = CreateObject("SldWorks.Application")</p><p>        swthisModel = swthisApp.ActiveDoc</p><p>        swthisView = swthatView</p><p></p><p></p><p>        swthisSelMgr = swthisModel.SelectionManager</p><p></p><p></p><p>        swCurve = swthatEdge.GetCurve</p><p>        swthisEnt = swthatEdge</p><p></p><p></p><p>        vCurveParam = swthisEnt.GetCurveParams2</p><p></p><p></p><p>        DoubleIntConv.Unpack(vCurveParam(8), nDummy, nIdentity)</p><p>        DoubleIntConv.Unpack(vCurveParam(9), nDummy, nTag)</p><p>        DoubleIntConv.Unpack(vCurveParam(10), nDummy, nSense)</p><p></p><p></p><p>           ' Derived quantity</p><p>        Debug.Print("Length     = " & swCurve.GetLength2(vCurveParam(6), vCurveParam(7)) * 1000.0# & " mm ")</p><p>        Debug.Print("")</p><p></p><p></p><p>        ' Only makes sense for straight edges</p><p>        If LINE_TYPE <> nIdentity Then Exit Sub</p><p></p><p></p><p>        swMathUtil = swthisApp.GetMathUtility</p><p></p><p></p><p>        nPtData(0) = vCurveParam(0)</p><p>        nPtData(1) = vCurveParam(1)</p><p>        nPtData(2) = vCurveParam(2)</p><p>        vPtData = nPtData</p><p>        swModelStartPt = swMathUtil.CreatePoint(vPtData)</p><p></p><p></p><p>        nPtData(0) = vCurveParam(3)</p><p>        nPtData(1) = vCurveParam(4)</p><p>        nPtData(2) = vCurveParam(5)</p><p>        vPtData = nPtData</p><p>        swModelEndPt = swMathUtil.CreatePoint(vPtData)</p><p></p><p></p><p>        ' Start creating drawing of the model</p><p></p><p></p><p>        ' Select regardless</p><p>        swthisModel.ClearSelection()</p><p>        bRet2 = swthisView.SelectEntity(swthisEnt, True)</p><p>        'Debug.Assert(bRet)</p><p></p><p></p><p>        vOutline = swthisView.GetOutline</p><p></p><p></p><p>        swViewXform = swthisView.ModelToViewTransform</p><p>        swViewStartPt = swModelStartPt.MultiplyTransform(swViewXform)</p><p>        swViewEndPt = swModelEndPt.MultiplyTransform(swViewXform)</p><p></p><p></p><p>              If Math.Abs(swViewStartPt.ArrayData(0) - swViewEndPt.ArrayData(0)) < nTolerance Then</p><p>            ' Must be vertical</p><p>            'If Not entPreviouseHorEdge Is Nothing Then</p><p>            '    swthisModel.ClearSelection()</p><p>            '    entPreviouseVertEdge = swthisEnt</p><p>            '    GoTo nextone</p><p>            'End If</p><p></p><p></p><p>            ' Place dimension midway up edge and to the right of view</p><p>            nXpos = vOutline(0) - nXoffset</p><p>            nYpos = Math.Abs((swViewStartPt.ArrayData(1) + swViewEndPt.ArrayData(1)) / 2.0#)</p><p>            'If this is missing then next line fails for unknown reason</p><p>            'swthisModel.ForceRebuild3(False)</p><p>            If Not entPreviouseVertEdge Is Nothing Then</p><p>                GC.Collect()</p><p>                bRet3 = swthisView.SelectEntity(entPreviouseVertEdge, True) <span style="text-decoration: underline;"><strong>'' <<<<-------------- ERRORS AT THIS LINE</strong></span></p><p>                entPreviouseVertEdge = Nothing</p><p></p><p></p><p>                swDispDim = swthisModel.AddVerticalDimension2(nXpos, nYpos, 0.0#)</p><p>                swthisModel.ClearSelection()</p><p>                swDispDim = Nothing</p><p>                bRet3 = Nothing</p><p>                swthisModel = Nothing</p><p>            Else</p><p>                entPreviouseVertEdge = swthisEnt</p><p>                swthisModel.ClearSelection()</p><p>            End If</p><p></p><p></p><p>            ' NULL if cannot convert edge in this view</p><p>        ElseIf Math.Abs(swViewStartPt.ArrayData(1) - swViewEndPt.ArrayData(1)) < nTolerance Then</p><p>            ' Must be horizontal</p><p>            'If Not entPreviouseVertEdge Is Nothing Then</p><p>            '    swthisModel.ClearSelection()</p><p>            '    entPreviouseHorEdge = swthisEnt</p><p>            '    GoTo nextone</p><p>            'End If</p><p></p><p></p><p>            ' Place dimension midway across edge and above view</p><p>            nXpos = Math.Abs((swViewStartPt.ArrayData(0) + swViewEndPt.ArrayData(0)) / 2.0#)</p><p>            nYpos = vOutline(3) + nYoffset</p><p></p><p></p><p>            ' NULL if cannot convert edge in this view</p><p>            'If this is missing then next line fails for unknown reason</p><p>            'swthisModel.ForceRebuild3(False)</p><p>            If Not entPreviouseHorEdge Is Nothing Then</p><p>                GC.Collect()</p><p>                bRet3 = swthisView.SelectEntity(entPreviouseHorEdge, True)<strong style="color: rgba(0, 0, 0, 0); font-family: Consolas, 'Courier New', Courier, mono, serif; font-size: 12px; background-position: repeat;">'' <<<<-------------- ERRORS AT THIS LINE</strong></p><p>                entPreviouseHorEdge = Nothing</p><p>                swDispDim = swthisModel.AddVerticalDimension2(nXpos, nYpos, 0.0#)</p><p>                swthisModel.ClearSelection()</p><p>            Else</p><p>                entPreviouseHorEdge = swthisEnt</p><p>                swthisModel.ClearSelection()</p><p>            End If</p><p>        Else</p><p>            ' Neither horizontal or vertical</p><p>            ' Place dimension near middle of edge</p><p>            nXpos = Math.Abs((swViewStartPt.ArrayData(0) + swViewEndPt.ArrayData(0)) / 2.0#) + nXoffset</p><p>            nYpos = Math.Abs((swViewStartPt.ArrayData(1) + swViewEndPt.ArrayData(1)) / 2.0#) + nYoffset</p><p></p><p></p><p>            ' Depends on the orientation of the entity in the drawing view,</p><p>            ' thus, could be NULL</p><p>            '</p><p>            ' Create the dimension even if the entity is not</p><p>            ' visible in the drawing view</p><p>            'If this is missing then next line fails for unknown reason</p><p>            'swthisModel.ForceRebuild3(False)</p><p>            swDispDim = swthisModel.AddDimension2(nXpos, nYpos, 0.0#)</p><p>            swthisModel.ClearSelection()</p><p>        End If</p><p></p><p></p><p>        swthisModel = Nothing</p><p>        swDispDim = Nothing</p><p>        bRet2 = Nothing</p><p>        bRet3 = Nothing</p><p>        swCurve = Nothing</p><p>        swthisEnt = Nothing</p><p>        vCurveParam = Nothing</p><p>        swthisApp = Nothing</p><p>        swthisView = Nothing</p><p>        swthisSelMgr = Nothing</p><p>        GC.Collect()</p><p>        GC.WaitForFullGCComplete()</p><p>    End Sub</p>
SolidworksApi macros