.NET
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

AutoCAD crashes by using ObjectAppended only with some entities

5 REPLIES 5
Reply
Message 1 of 6
MGOMKD
626 Views, 5 Replies

AutoCAD crashes by using ObjectAppended only with some entities

Hi all,

 

I would like to re-encode my plug-in lighter, in order to optimize tasks.

 

So I would like to store dynamically, in an ArrayList or else, entities properties (ID, Layer, Bounds...etc.) by using reactors.

 

It works! but not with every entity! For instance AutoCAD crashes when I create a CIRCLE while ObjectAppended is invoking.

 

Here is a simply code to resume:

 

Imports Autodesk.AutoCAD.ApplicationServices
Imports Autodesk.AutoCAD.DatabaseServices
Imports Autodesk.AutoCAD.Runtime
Imports Autodesk.AutoCAD.EditorInput

Public Class Class1

    Friend acDoc As Document = Application.DocumentManager.MdiActiveDocument
    Friend acCurDb As Database = acDoc.Database
    Friend ed As Editor = acDoc.Editor

    <CommandMethod("Test")> _
    Public Sub Test()

        AddHandler acCurDb.ObjectAppended, AddressOf MyDelegate

    End Sub

    Public Sub MyDelegate(ByVal sender As Object, ByVal e As Autodesk.AutoCAD.DatabaseServices.ObjectEventArgs)

        On Error Resume Next

        Using acTrans As Transaction = acCurDb.TransactionManager.StartTransaction()

            Dim acblktbl As BlockTable = acTrans.GetObject(acCurDb.BlockTableId, OpenMode.ForRead)
            Dim acblkTblRec As BlockTableRecord = acTrans.GetObject(acblktbl(BlockTableRecord.ModelSpace), OpenMode.ForRead)

            For Each acObjId As ObjectId In acblkTblRec

                If acObjId = e.DBObject.ObjectId Then

                    Dim MyEnt As Entity = acTrans.GetObject(acObjId, OpenMode.ForRead)
                    Dim message As String = MyEnt.GetRXClass.DxfName.ToString & vbLf

                    Select Case MyEnt.GetRXClass.DxfName

                        Case Is = "LWPOLYLINE"
                            Dim Mypolyline As Polyline = CType(MyEnt, Entity)
                            message += "Area= " & Mypolyline.Area.ToString & vbLf
                            message += "Layer= " & Mypolyline.Layer.ToString & vbLf
                            message += "Min point= " & Mypolyline.Bounds.Value.MinPoint.ToString & vbLf
                            message += "Max point= " & Mypolyline.Bounds.Value.MaxPoint.ToString & vbLf

                        Case Is = "CIRCLE"
                            Dim Mycircle As Circle = CType(MyEnt, Circle)
                            message += "Area= " & Mycircle.Area.ToString & vbLf
                            message += "Calque= " & Mycircle.Layer.ToString & vbLf
                            message += "Radius= " & Mycircle.Radius.ToString & vbLf

                    End Select

                    MsgBox(message)

                End If

            Next

        End Using
    End Sub

End Class

 What can I do to solve that!?

5 REPLIES 5
Message 2 of 6
justshutup
in reply to: MGOMKD

I would imagine that this post would get more traction over in the customizations forums.

Message 3 of 6
MGOMKD
in reply to: justshutup

Sure! But how could I put it in the right section, I mean .NET section!?

 

Message 4 of 6
drjohn
in reply to: MGOMKD

Use the RIC (Report Inappropriate Content) selection on the left and ask and admin to move it for you.

 

Regards,

DJ

Message 5 of 6
MGOMKD
in reply to: drjohn

HI all,

 

I solved the problem by adding this line:

 

acTrans.Commit

 

AutoCAD now no longer crashes but another problem occurs when adding circles.

Specific properties (like center, circumference, radius, diameter) are initialized to 0, while those of other entities are well notified.

 

Here is the code:

Imports Autodesk.AutoCAD.ApplicationServices
Imports Autodesk.AutoCAD.DatabaseServices
Imports Autodesk.AutoCAD.Runtime
Imports Autodesk.AutoCAD.EditorInput

Public Class Class1

    Friend acDoc As Document = Application.DocumentManager.MdiActiveDocument
    Friend acCurDb As Database = acDoc.Database
    Friend ed As Editor = acDoc.Editor

    <CommandMethod("Test")> _
    Public Sub Test()
        AddHandler acCurDb.ObjectAppended, AddressOf MyDelegate
    End Sub

    Public Sub MyDelegate(ByVal sender As Object, ByVal e As Autodesk.AutoCAD.DatabaseServices.ObjectEventArgs)

        Using acTrans As Transaction = acCurDb.TransactionManager.StartTransaction()

            Dim acblktbl As BlockTable = acTrans.GetObject(acCurDb.BlockTableId, OpenMode.ForRead)
            Dim acblkTblRec As BlockTableRecord = acTrans.GetObject(acblktbl(BlockTableRecord.ModelSpace), OpenMode.ForRead)

            For Each acObjId As ObjectId In acblkTblRec

                If acObjId = e.DBObject.ObjectId Then

                    Dim MyEnt As Entity = acTrans.GetObject(acObjId, OpenMode.ForRead)
                    Dim message As String = "Object: " & MyEnt.GetRXClass.DxfName.ToString & vbLf
                    message += "Layer= " & MyEnt.Layer.ToString & vbLf
                    message += "Color Index= " & MyEnt.ColorIndex.ToString & vbLf

                    Select Case MyEnt.GetRXClass.DxfName
                        Case Is = "ARC"
                            Dim Myarc As Arc = CType(MyEnt, Arc)
                            message += "Bounds Min point= " & Myarc.Bounds.Value.MinPoint.ToString & vbLf
                            message += "Bounds Max point= " & Myarc.Bounds.Value.MaxPoint.ToString & vbLf
                            message += "Center= " & Myarc.Center.ToString & vbLf
                            message += "Radius= " & Myarc.Radius.ToString

                        Case Is = "CIRCLE"
                            Dim Mycircle As Circle = CType(MyEnt, Circle)
                            message += "Bounds Min point= " & Mycircle.Bounds.Value.MinPoint.ToString & vbLf
                            message += "Bounds Max point= " & Mycircle.Bounds.Value.MaxPoint.ToString & vbLf
                            message += "Center= " & Mycircle.Center.ToString & vbLf
                            message += "Radius= " & Mycircle.Radius.ToString & vbLf
                            message += "Diameter= " & Mycircle.Diameter.ToString & vbLf
                            message += "Circumference= " & Mycircle.Circumference

                        Case Is = "LWPOLYLINE"
                            Dim Mypolyline As Polyline = CType(MyEnt, Polyline)
                            message += "Bounds Min point= " & Mypolyline.Bounds.Value.MinPoint.ToString & vbLf
                            message += "Bounds Max point= " & Mypolyline.Bounds.Value.MaxPoint.ToString & vbLf
                            message += "Area= " & Mypolyline.Area.ToString & vbLf
                            message += "Length= " & Mypolyline.Length.ToString

                        Case Is = "XLINE"
                            Dim Myxline As Xline = TryCast(MyEnt, Xline)
                            message += "Handle= " & Myxline.Handle.ToString

                    End Select

                    MsgBox(message)

                End If

            Next

            acTrans.Commit()

        End Using

    End Sub

End Class

 

What's wrong with my code?

 

 

Message 6 of 6
MGOMKD
in reply to: MGOMKD

Hi!

 

By looking in my code, does anyone knows why, while adding "Circle" in model space, it returns null specifics properties!?

 

 

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Autodesk DevCon in Munich May 28-29th


Autodesk Design & Make Report

”Boost