System.AccessViolationException while trying to get layername

System.AccessViolationException while trying to get layername

r00teniy
Contributor Contributor
395 Views
4 Replies
Message 1 of 5

System.AccessViolationException while trying to get layername

r00teniy
Contributor
Contributor

Hello, I am getting "System.AccessViolationException: Attempted to read or write protected memory." while trying go get layer of blockref in array.

Here is part of the code that give exeption, it always gives it then i try to get (obj as BlockReference).Layer, if i remove it, it works fine:

                    if (AssocArray.IsAssociativeArray(objectId)) 
                    {
                        using (BlockReference br = objectId.Open(OpenMode.ForRead) as BlockReference)
                        {
                            using (DBObjectCollection brs = new DBObjectCollection())
                            {
                                br.Explode(brs);
                                if (brs != null && brs.Count > 0)
                                {
                                    foreach (DBObject obj in brs)
                                    {
                                        for (int j = 0; j < laylistBL.Length; j++)
                                        {
                                            if (obj is BlockReference && (obj as BlockReference).Layer == laylistBL[j])
                                            {
                                                TableValues[12 + j] += 1;
                                            }
                                            obj.Dispose();
                                        }
                                    }
                                }
                            }
                        }
                    }

I found similar code and modified it for myself here.

Can you please point out what I'm doing wrong?

0 Likes
Accepted solutions (1)
396 Views
4 Replies
Replies (4)
Message 2 of 5

_gile
Consultant
Consultant
Accepted solution

hi,

 

What if you iterate through the BlockTableRecord entities of the BlockReference instead of exploding it?

Something like this (not tested).

 

if (AssocArray.IsAssociativeArray(objectId)) 
{
    using (BlockReference br = (BlockReference)objectId.Open(OpenMode.ForRead))
    {
        using (BlockTableRecord btr = (BlockTableRecord)br.BlockTableRecord.Open(OpenMode.ForRead))
        {
            foreach (objectId id in btr)
            {
                if (id.ObjectClass.Name = "AcDbBlockReference")
                {
                    using(BlockReference bRef = (BlockReference)id.Open(OpenMode.ForRead))
                    {
                        for (int j = 0; j < laylistBL.Length; j++)
                        {
                            if (bRef.Layer == laylistBL[j])
                            {
                                TableValues[12 + j] += 1;
                            }
                        }
                    }
                }
            }
        }
    }
}

 



Gilles Chanteau
Programmation AutoCAD LISP/.NET
GileCAD
GitHub

0 Likes
Message 3 of 5

r00teniy
Contributor
Contributor

Thank you, will try to work in this direction.

0 Likes
Message 4 of 5

_gile
Consultant
Consultant

Oops!...

I forgot casting the BlockTableRecord.

I corrected the upper code replacing:

using (BlockTableRecord btr = br.BlockTableRecord.Open(OpenMode.ForRead))

with:

using (BlockTableRecord btr = (BlockTableRecord)br.BlockTableRecord.Open(OpenMode.ForRead))


Gilles Chanteau
Programmation AutoCAD LISP/.NET
GileCAD
GitHub

0 Likes
Message 5 of 5

r00teniy
Contributor
Contributor

For me this line did it, thank you:
using (BlockTableRecord btr = br.DynamicBlockTableRecord.Open(OpenMode.ForRead) as BlockTableRecord)

0 Likes