Get polyline entities crash from BlockReference in release mode

Get polyline entities crash from BlockReference in release mode

Anonymous
Not applicable
1,478 Views
3 Replies
Message 1 of 4

Get polyline entities crash from BlockReference in release mode

Anonymous
Not applicable

Hi 

 

I am using the realdwg sdk, i found a problem with getting polyline entities inside a new BlockReference

 

So if it's the block from the model space, it's like this

 

BlockTable bt = (BlockTable)tr.GetObject(db.BlockTableId, OpenMode.ForRead);
BlockTableRecord blockrecord = (BlockTableRecord)tr.GetObject(bt[BlockTableRecord.ModelSpace], OpenMode.ForRead);

 

Then i can iterate all the entities to get the polyline like this 

 

void getPolyline(BlockTableRecord bt, Transaction tr) {

    foreach (ObjectId id in bt)
    {
        Entity ent = (Entity)tr.GetObject(id, OpenMode.ForRead);

         if (ent is Polyline) {

                getDATA((Polyline)ent)

         }

         if (ent is BlockReference) {

              //recursive calling

             getPolyline((BlockReference)ent, tr)

         }

    }

}

 

 

void getDATA(Polyline p) {

    for (int i = 0; i < p.NumberOfVertices; i++)
    {
        SegmentType t = p.GetSegmentType(i);
        Console.WriteLine(t);
        if (t == SegmentType.Line)
        {
                 LineSegment2d l = p.GetLineSegment2dAt(i);

                 //Save to my own data structure
         }
         else if (t == SegmentType.Arc)
         {
                CircularArc2d arc = p.GetArcSegment2dAt(i);

                //Save to my another data structure
          }
     }

}

 

The problem is that if the ent is another BlockReference; I get crash error.

 

Should I start a new Transaction for each new BlockReference?

 

If I just get the Polyline of first the BlockReference(The model space one), not calling the recursive function, it works fine without crash. If I call the recursive part. I can the error:

 

System.AccessViolationException was unhandled
Source=Acdbmgd
StackTrace:
 Autodesk.AutoCAD.DatabaseServices.Transaction.DeleteUnmanagedObject()
 Autodesk.AutoCAD.Runtime.DisposableWrapper.!DisposableWrapper()
 Autodesk.AutoCAD.Runtime.DisposableWrapper.Dispose(Boolean A_0)

 Autodesk.AutoCAD.Runtime.DisposableWrapper.Dispose()

 

 

What did I do wrong?

 

Thx

 

0 Likes
1,479 Views
3 Replies
Replies (3)
Message 2 of 4

Anonymous
Not applicable

The file I use is attached

0 Likes
Message 3 of 4

norman.yuan
Mentor
Mentor

Firstly, in spite this forum is called "Visual Basic Customization", "Visual Basic" is really meant for AutoCAD VBA for historical reason, and the discussion is focused on AutoCAD VBA and AutoCAD COM API. Since your code deals with AutoCAD .NET API, it would be better to post here.

 

Now regarding your code, it looks like you are confusing BlockTableRecord and BlockReference: the method

 

getPolyline(BlockTableRecord bt, ...)

 

takes a parameter of BlockTableRecord type. However, in your recursive calling the same method, you pass it a BlockReference type, thus the crash. BlockReference is just a REFERENCE to a BlockTableRecord. It DOES NOT contain any entity (except for AttributeReferences).

 

So, your code should always crash, regardless the code being debug mode or release mode.

 

Norman Yuan

Drive CAD With Code

EESignature

0 Likes
Message 4 of 4

Anonymous
Not applicable

@norman.yuan

Sorry post this in wrong place, I do have the code to fetch the block actually.

 

if (ent is BlockReference)
{
BlockReference br = (BlockReference)ent;

BlockTableRecord blockRecord = (BlockTableRecord)tr.GetObject(br.DynamicBlockTableRecord, OpenMode.ForRead);

 

// Then do the recursive part.

}

 

Sorry

0 Likes