.NET

Reply
Valued Contributor
92 Posts
0 Kudos
Registered: ‎06-04-2007
Post 1 of 10

retrieving block info using c#

549 Views, 9 Replies
08-08-2007 01:01 PM
does anybody know how to
1. reference all the block names in a document using c#
2. if a block name is a certain string, then pull info from that block's attributes
I've got it in VBA within AutoCad, but am now trying to convert it to C# and am running into problems. I've got the application and document open, no problem.
Thanks,
John
Mentor
2,504 Posts
3 Kudos
Registered: ‎02-17-2004
Post 2 of 10

Re: retrieving block info using c#

08-08-2007 01:52 PM in reply to: johnlap
How I would do it (not all the experienced)
Get the database
Get the layout dictionary id
Then step through all the layouts in the dictionary checking the objects to
make sure they are blocks, and match the name you are looking for.

--

Tim
"A blind man lets nothing block his vision."


wrote in message news:5684044@discussion.autodesk.com...
does anybody know how to
1. reference all the block names in a document using c#
2. if a block name is a certain string, then pull info from that block's
attributes
I've got it in VBA within AutoCad, but am now trying to convert it to C# and
am running into problems. I've got the application and document open, no
problem.
Thanks,
John
*Tony Tanzillo
Post 3 of 10

Re: retrieving block info using c#

08-08-2007 02:34 PM in reply to: johnlap
>> does anybody know how to

Plenty of folks here know how, but few if any are
going to make the significant effort required to
write a detailed example for you.

So, rather than ask if anyone knows how, just post
whatever code you have that you're having problems
with, and someone should be able to give you some
ideas on what's wrong and what you need to do.

If you're just starting out with C# and haven't done
much with it, then please make that clear as well.


--
http://www.caddzone.com

AcadXTabs: MDI Document Tabs for AutoCAD 2008
Supporting AutoCAD 2000 through 2008
http://www.acadxtabs.com

wrote in message news:5684044@discussion.autodesk.com...
does anybody know how to
1. reference all the block names in a document using c#
2. if a block name is a certain string, then pull info from that block's attributes
I've got it in VBA within AutoCad, but am now trying to convert it to C# and am running into problems. I've got the application and document open, no problem.
Thanks,
John
Valued Contributor
92 Posts
0 Kudos
Registered: ‎06-04-2007
Post 4 of 10

Re: retrieving block info using c#

08-08-2007 03:00 PM in reply to: johnlap
Thanks Tony and Tim. I am new to AutoCad, but not C#. Like I stated before, I have this working in a VBA form thru AutoCad, but just want to convert to C#. I'll check out the labs to see if I can get some leads there on which direction to go. I didn't really want a detailed tutorial with examples, but was just looking for some specific direction or ideas on some literature.
John
Distinguished Contributor
1,691 Posts
3 Kudos
Registered: ‎12-15-2003
Post 5 of 10

Re: retrieving block info using c#

08-08-2007 03:45 PM in reply to: johnlap
BlockRef & AttRef OR BlockDef & AttDef?
Active Member
6 Posts
0 Kudos
Registered: ‎07-16-2007
Post 6 of 10

Re: retrieving block info using c#

08-09-2007 11:31 AM in reply to: johnlap
Below is a similiar function to one where I update our drawing TitleBlock(block name "TITLEBLK"). this function updates the attributes "DWG_NO", "SHEET_NUMBER","REVISION_NUMBER"

-----
using Autodesk.AutoCAD.Runtime;
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.EditorInput;
using Autodesk.AutoCAD.Geometry;
using AcadApp = Autodesk.AutoCAD.ApplicationServices.Application;
---

private void somefunction()
{
Database db = AcadApp.DocumentManager.MdiActiveDocument.Database;

Editor ed = AcadApp.DocumentManager.MdiActiveDocument.Editor;

using (Transaction trans = db.TransactionManager.StartTransaction())
{
try
{
BlockTable bt = (BlockTable)trans.GetObject(db.BlockTableId, OpenMode.ForRead);
BlockTableRecord btr = (BlockTableRecord)trans.GetObject(bt[BlockTableRecord.PaperSpace], OpenMode.ForRead);
foreach (ObjectId bId in btr)
{
using (Entity ent = (Entity)trans.GetObject(bId, OpenMode.ForRead, false))
{

if (ent.GetRXClass().Name.ToString() == "AcDbBlockReference")
{

BlockReference br = (BlockReference)ent;

BlockTableRecord blkObj = (BlockTableRecord)trans.GetObject(br.BlockTableRecord, OpenMode.ForRead);

if ((blkObj.HasAttributeDefinitions) && (blkObj.Name == "TITLEBLK"))
{

Autodesk.AutoCAD.DatabaseServices.AttributeCollection attcol = br.AttributeCollection;
foreach (ObjectId attId in attcol)
{
AttributeReference attRef = (AttributeReference)trans.GetObject(attId, OpenMode.ForWrite);

switch (attRef.Tag)
{
case "DWG_NO":
attRef.TextString = "My Number";
break;
case "SHEET_NUMBER":
attRef.TextString = "My Sheet";
break;
case "REVISION_NUMBER":
attRef.TextString = "My Rev";
break;

}

string str = ("\n Updated Attribute Tag: " + attRef.Tag + " Attribute String: " + attRef.TextString + "\n");
ed.WriteMessage(str);
}

}
}
}
} trans.Commit();
}
catch (System.Exception ex)
{
ed.WriteMessage("Unexpected Error: " + ex.ToString());
}
}

}
*Tony Tanzillo
Post 7 of 10

Re: retrieving block info using c#

08-09-2007 02:09 PM in reply to: johnlap
You said you wanted to convert to C#, but say
which API you wanted to use.

You can use the same ActiveX API used in VBA
with C# or, you can use the managed ObjectARX
API (a much bigger leap).

So, if you meant to use the managed API,
the basic direction would be:

1. Get a filtered selection set containing
the block references you're interested in.
You can specify one or more block names
for the filter criteria (SelectionSetFilter
class).

2. Iterate through the selection set, and
open each BlockReference object, and
then examine its AttributesCollection
looking for the attributes of interest,
and operate on them.

That's the general direction, but there's still
a lot of knowledge required to do it using the
managed API, that can only be gained by
following the examples and using whatever
learning materials you can find.

When you actually attempt to do this with
the managed API, if you run into problems,
then just post the code you have and then,
we can more easily give you more specific
direction.

--
http://www.caddzone.com

AcadXTabs: MDI Document Tabs for AutoCAD 2008
Supporting AutoCAD 2000 through 2008
http://www.acadxtabs.com

wrote in message news:5684250@discussion.autodesk.com...
Thanks Tony and Tim. I am new to AutoCad, but not C#. Like I stated before, I have this working in a VBA form thru AutoCad, but just want to convert to C#. I'll check out the labs to see if I can get some leads there on which direction to go. I didn't really want a detailed tutorial with examples, but was just looking for some specific direction or ideas on some literature.
John
Distinguished Contributor
172 Posts
1 Kudo
Registered: ‎11-19-2003
Post 8 of 10

Re: retrieving block info using c#

08-13-2007 10:57 PM in reply to: johnlap
Look at this blog, maybe you can find something you will need. And if you
want to get the name of a block, don't forget dynamic blocks.
http://through-the-interface.typepad.com/

schrieb im Newsbeitrag news:5684044@discussion.autodesk.com...
does anybody know how to
1. reference all the block names in a document using c#
2. if a block name is a certain string, then pull info from that block's
attributes
I've got it in VBA within AutoCad, but am now trying to convert it to C# and
am running into problems. I've got the application and document open, no
problem.
Thanks,
John
Distinguished Contributor
1,222 Posts
0 Kudos
Registered: ‎10-14-2004
Post 9 of 10

Re: retrieving block info using c#

08-14-2007 10:35 AM in reply to: johnlap
Great blog. I loved the self-portraits! LOL
New Member
1 Posts
0 Kudos
Registered: ‎05-12-2010
Post 10 of 10

Re: retrieving block info using c#

05-26-2010 01:39 PM in reply to: johnlap

Hello All, When I use the code as posted I get the attributes repeated for every instance of the block. I only want them once. I tried changing the way I retrieve the instance to btr by indexing into the blockTable as such; bt[blockName], however that approach never gets me an AcDbBlockReference. I have years of c# under my belt, but I'm new to AutoCAD.NET. Thanks in advance.

try

{

string BName = lstBlockName.SelectedItem.ToString();

BlockTable bt = (BlockTable)tr.GetObject(db.BlockTableId, OpenMode.ForRead);

//using the following with a foreach repeats the attributes for each instance of the block

// BlockTableRecord btr = (BlockTableRecord)tr.GetObject(bt[BlockTableRecord.ModelSpace], OpenMode.ForRead);

BlockTableRecord btr = GetBtr(tr, BName, bt);

foreach (ObjectId bId in btr)

{

using (Entity ent = (Entity)tr.GetObject(bId, OpenMode.ForRead, false))

{

if (ent.GetRXClass().Name.ToString() == "AcDbBlockReference")

{

BlockReference br = (BlockReference)ent;

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

if ((blkObj.HasAttributeDefinitions) && (blkObj.Name == BName))

{

Post to the Community

Have questions about Autodesk products? Ask the community.

New Post
Announcements
Are you interested in helping shape the future of the Autodesk Community? To participate in this brief usability study, please click here. Your time and input is greatly appreciated!