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

How to get all blocks from current drawing using .NET

2 REPLIES 2
Reply
Message 1 of 3
Anonymous
1155 Views, 2 Replies

How to get all blocks from current drawing using .NET

I'm new in ObjectARX. Please give me simple code and some explanations on how to get all blocks and their attributes from current drawing using .NET (in my case C#). Great thanks!
2 REPLIES 2
Message 2 of 3
Arrowstick
in reply to: Anonymous

I'm not sure, if you still need it, but this is a possible way:

// get the working database
Database db = HostApplicationServices.WorkingDatabase;
// Start a transaction.
using (Transaction trans = db.TransactionManager.StartTransaction())
{
// Get Block Table and Block Table Record
BlockTable bt = (BlockTable)trans.GetObject(db.BlockTableId, OpenMode.ForRead);
BlockTableRecord btrMS = (BlockTableRecord)trans.GetObject(bt[BlockTableRecord.ModelSpace], OpenMode.ForRead);
// Now iterate through ModelSpace (means through btrMS)
foreach (ObjectId id in btrMS)
{
// Fetch the entity
Entity ent = (Entity)trans.GetObject(id, OpenMode.ForRead, false);
// Test if the entity is a BlockReference
if (ent.GetType() == typeof(BlockReference))
{
// It's a BlockReference... cast it:
BlockReference blRef = (BlockReference)ent;
// Now you can acces attributes via the AttributeCollection
// Do Something with:
blRef.AttributeCollection;
}
}
// commit Transaction
trans.Commit();
} Edited by: Arrowstick on Nov 12, 2008 3:24 PM
Message 3 of 3
Anonymous
in reply to: Anonymous


Thanks for posting a commented example! I see a lot
of code posted here with no supporting comments.

 

John


style="PADDING-RIGHT: 0px; PADDING-LEFT: 5px; MARGIN-LEFT: 5px; BORDER-LEFT: #000000 2px solid; MARGIN-RIGHT: 0px">
I'm
not sure, if you still need it, but this is a possible way:

// get the
working database
Database db = HostApplicationServices.WorkingDatabase;

// Start a transaction.
using (Transaction trans =
db.TransactionManager.StartTransaction())
{

// Get Block Table and Block Table Record
BlockTable bt =
(BlockTable)trans.GetObject(db.BlockTableId, OpenMode.ForRead);

BlockTableRecord btrMS =
(BlockTableRecord)trans.GetObject(bt[BlockTableRecord.ModelSpace],
OpenMode.ForRead);
// Now iterate through ModelSpace (means through
btrMS)
foreach (ObjectId id in btrMS)
{
// Fetch the entity

Entity ent = (Entity)trans.GetObject(id, OpenMode.ForRead, false);
//
Test if the entity is a BlockReference
if (ent.GetType() ==
typeof(BlockReference))
{
// It's a BlockReference... cast it:

BlockReference blRef = (BlockReference)ent;
// Now you can acces
attributes via the AttributeCollection
// Do Something with:

blRef.AttributeCollection;
}
}
// commit Transaction

trans.Commit();
} Edited by: Arrowstick on Nov 12, 2008
3:24 PM

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

Post to forums  

Autodesk Design & Make Report

”Boost