.NET
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

retrieving block info using c#

10 REPLIES 10
Reply
Message 1 of 11
johnlap
2370 Views, 10 Replies

retrieving block info using c#

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
10 REPLIES 10
Message 2 of 11
t.willey
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
Message 3 of 11
Anonymous
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
Message 4 of 11
johnlap
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
Message 5 of 11
NathTay
in reply to: johnlap

BlockRef & AttRef OR BlockDef & AttDef?
Message 6 of 11
josephgarrett
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());
}
}

}
Message 7 of 11
Anonymous
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
Message 8 of 11
Anonymous
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
Message 9 of 11
Rudedog
in reply to: johnlap

Great blog. I loved the self-portraits! LOL
Message 10 of 11
tom.paolini
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))

{

Message 11 of 11
BKSpurgeon
in reply to: johnlap

For anyone looking at this in the future, searching for answers: this was inordinately helpful

http://adndevblog.typepad.com/autocad/2012/06/identify-the-number-of-reference-to-a-block.html

 

just incase the link gets broken in a few  years, here is the money, the code below. What a Godsend it is!

 

 

[CommandMethod("BlockRefCount")]

static public void BlockRefCount()

{

 Document doc = Application.DocumentManager.MdiActiveDocument;

 Database db = doc.Database;

 Editor ed = doc.Editor;

 

 PromptStringOptions opts =

    new PromptStringOptions("Enter block name");

    opts.AllowSpaces = true;

 

 PromptResult blockName = ed.GetString(opts);

 

 if (blockName.Status != PromptStatus.OK)

     return;

 

 using (Transaction tx =

                     db.TransactionManager.StartTransaction())

 {

     BlockTable blockTable = tx.GetObject(db.BlockTableId,

                                OpenMode.ForRead) as BlockTable;

 

     if (blockTable.Has(blockName.StringResult))

     {

         BlockTableRecord block = tx.GetObject(

                               blockTable[blockName.StringResult],

                               OpenMode.ForRead) as BlockTableRecord;

         //only direct reference

         ObjectIdCollection ids =

                            block.GetBlockReferenceIds(true, true);

 

         ed.WriteMessage("Number of reference is "

                                + ids.Count.ToString() + "\n");

     }

     tx.Commit();

 }

}

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Autodesk DevCon in Munich May 28-29th


Autodesk Design & Make Report

”Boost