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

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

1174 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: 752
Registered: ‎02-13-2008
Message 2 of 2 (1,173 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

You are not logged in.

Log into access your profile, ask and answer questions, share ideas and more. Haven't signed up yet? Register

Are you familiar with the Autodesk Expert Elites? The Expert Elite program is made up of customers that help other customers by sharing knowledge and exemplifying an engaging style of collaboration. To learn more, please visit our Expert Elite website.

Need installation help?

Start with some of our most frequented solutions to get help installing your software.

Ask the Community