Autocad map DisplayFeatures error

Autocad map DisplayFeatures error

Sgear
Advocate Advocate
622 Views
2 Replies
Message 1 of 3

Autocad map DisplayFeatures error

Sgear
Advocate
Advocate

 

Hi

 

when I check in drawing for classified entity and total and length I don't get error but somtime Autocad crash 

This is what I see in Autocad dumpdata

 

Clr Data:
at Autodesk.AutoCAD.DatabaseServices.Transaction.DeleteUnmanagedObject()
at Autodesk.AutoCAD.Runtime.DisposableWrapper.!DisposableWrapper()
at Autodesk.AutoCAD.Runtime.DisposableWrapper.Dispose(Boolean A_0)
GDI_Objects:1857 User_Objects:929 Process_Handles:1878]]>

 

Regards

 

 

       Public Sub DisplayFeatures(ByVal sl_tegind)


            Dim errorCode As Classification.FeatureClassErrorCode = Classification.FeatureClassErrorCode.OK
            Try
                Dim colIds As ObjectIdCollection = Nothing
                Dim total As String

                Dim PipelineLength As Double
                Dim dbObj As DBObject
                Dim ent As Entity = Nothing
                Dim trans As Transaction = Nothing
                Dim Classtype As String

                Try
                    Dim acDoc As Document = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument

                    Dim AcApp As Autodesk.AutoCAD.Interop.AcadApplication = DirectCast(Autodesk.AutoCAD.ApplicationServices.Application.AcadApplication, Autodesk.AutoCAD.Interop.AcadApplication)
                    Dim ThisDrawing As Autodesk.AutoCAD.Interop.AcadDocument = AcApp.ActiveDocument

                    Dim doc As Document = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument
                    Dim dd As Editor = doc.Editor

                    Dim ed As Autodesk.AutoCAD.EditorInput.Editor = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.Editor


                    Classtype = sl_tegind
                    colIds = m_ClassificationManager.GetClassifiedEntities(Classtype.ToString(), True)


                    total = colIds.Count.ToString

                Catch e As System.Exception
                    '  Utility.ShowMsg(vbNewLine & "No classified entity." & vbNewLine)
                 '   AddElementToStringArray(tegund & "," & "0" & "," & "0")
                    Return

                End Try


                Dim colId As ObjectId
                For Each colId In colIds

                    trans = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.TransactionManager.StartTransaction()
                    ent = trans.GetObject(colId, Autodesk.AutoCAD.DatabaseServices.OpenMode.ForRead)
                    dbObj = trans.GetObject(colId, OpenMode.ForWrite)

                    Dim l As Polyline = DirectCast(dbObj, Polyline)

                    PipelineLength = PipelineLength + l.Length.ToString

                Next colId

                AddElementToStringArray(Classtype & "," & total & "," & PipelineLength)

                Utility.ShowMsg(vbNewLine & Classtype)
                Utility.ShowMsg(vbNewLine & total)
                Utility.ShowMsg(vbNewLine & PipelineLength)

                trans.Commit()
                trans.Dispose()

            Catch err As MapFeatureClassException
                errorCode = err.ErrorCode
            Finally

            End Try

            If errorCode <> Classification.FeatureClassErrorCode.OK Then
                Utility.ShowMsg(vbNewLine & "Display ClassifiedEntities failed. Error code: ")
                Utility.ShowMsg(String.Concat(GetPrintErrorMessage(errorCode), vbNewLine))
            Else

   
       
            End If

        End Sub
0 Likes
Accepted solutions (1)
623 Views
2 Replies
Replies (2)
Message 2 of 3

norman.yuan
Mentor
Mentor
Accepted solution

If you take closer llok at this code snippet of yours:

 

                Dim colId As ObjectId
                For Each colId In colIds

                    trans = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.TransactionManager.StartTransaction()
                    ent = trans.GetObject(colId, Autodesk.AutoCAD.DatabaseServices.OpenMode.ForRead)
                    dbObj = trans.GetObject(colId, OpenMode.ForWrite)

                    Dim l As Polyline = DirectCast(dbObj, Polyline)

                    PipelineLength = PipelineLength + l.Length.ToString

                Next colId

                AddElementToStringArray(Classtype & "," & total & "," & PipelineLength)

                Utility.ShowMsg(vbNewLine & Classtype)
                Utility.ShowMsg(vbNewLine & total)
                Utility.ShowMsg(vbNewLine & PipelineLength)

                trans.Commit()
                trans.Dispose()

Your code starts a number of Transactions inside the For Each...Next. and you only Commit() and Dispose() the last Transaction when the For Each...Next loop is done.That means your code leave many Transactions not disposed. This explains the exception message you get. And depending on how many loops the For Each...Next has to go through, at certain point AutoCAD simply cannot handle too many Transactions left hanging on, so, your code crashes from time to time.

 

You should start a single Transaction with a Using...End Using block (or a Try..Catch...Finally with Transaction.Dispose() in the Finally clause) that wraps the For Each...Next loop inside.

 

 

Norman Yuan

Drive CAD With Code

EESignature

0 Likes
Message 3 of 3

Sgear
Advocate
Advocate

 

Thanks will go over this to day

 

Regards

 

0 Likes