New Member
Posts: 1
Registered: ‎04-28-2010
Message 1 of 2 (1,227 Views)

How to extract attributes(text) from blocks with C# .NET

1227 Views, 1 Replies
04-28-2010 10:42 AM
Hi dear friends

I'm trying to write a simple program which it can open the DWG file and read the text inside a block,

until now the software can open the AutoCAD software but I dont know how to access through the block attributes and text,

Autodesk.AutoCAD.Interop.AcadApplication app = null;
Autodesk.AutoCAD.Interop.AcadDocument doc;
Autodesk.AutoCAD.Interop.Common.AcadModelSpace modelspace;

string inFileName = "C:\\Users\\Paul\\Desktop\\drawing1.dwg";
app = new Autodesk.AutoCAD.Interop.AcadApplication();
doc = app.Documents.Open(inFileName, true, string.Empty);
modelspace = doc.ModelSpace;

foreach (Autodesk.AutoCAD.Interop.Common.AcadEntity myCadEntity in doc.ModelSpace) {
if (myCadEntity is AcadBlockReference)
if (myCadEntity.EntityType.ToString() == "7")
MessageBox.Show("this is a block, now its time to read the atrributes inside");




Any help is greatly appreciated,

*Expert Elite*
Posts: 753
Registered: ‎02-13-2008
Message 2 of 2 (1,226 Views)

Re: How to extract attributes(text) from blocks with C# .NET

04-28-2010 03:15 PM in reply to: maziar24
You'll need to cast myCadEntity into an AcadBlockReference. Then you can check the myBref.HasAttributes property, and call the myBref.GetAttributes Function, which returns a Variant(Object) array of AcadAttributeReference's, which you can loop through.

You could also save some trouble by making a selection set which is filtered to only have block references with attributes.
Then you could do:
dim atts() as object
For Each Bref as AcadBlockReference in mySelectionSet
atts = Bref.GetAttributes
For Each AtRef as AcadAttributeReference in atts
'Do whatever you need to do
Next AtRef
Next Bref

Sorry, I use VB, but you should get the idea.
Dave O.                                                                  Sig-Logos32.png
Post to the Community

Have questions about Autodesk products? Ask the community.

New Post
Do you have 60 seconds to spare? The Autodesk Community Team is revamping our site ranking system and we want your feedback! Please click here to launch the 5 question survey. As always your input is greatly appreciated.