Trying to start posting a little more quality than quantity.
Sorry first intentions were to show how the code works by MgdDbg, then to explain the code, but it is taking a longer then expected and do want to spend any more time if no one cares or will not look at it.
I would be more than happy to go in more detail on any part, any line, or the whole thing if anyone wants but if not I will not waste the time.
I do not know if this will help but hopefully it will help a little on looking at things with MgdDbg and docs.
Just did it for an example not to show best practices
Your 3 best friends1. Swamp
2. MgdDbg
3. Docs .chm help files from ObjectArx SDK
Recorded while tired, trying not to be loud so not to wake the kid, & the redneck accent might make it hard to understand.
Setup:Have a drawing with three blocks
1. C -- Contains the tag we want
2. AnotherBlockWithSameTag -- Contains Block we want
3. Rec -- Does not contain the block we want
The tag we are after is
ONEWEWANTHere are 2 possible options to do it1. Iterate current space to find blocks
2. Iterate BlockTable to find blocks
This link
FirstOptionMgdDbg shows MdgDbg where we would iterate all entities in current space and check to see if it is a BlockReference and if it is get the AttributeCollection and check the AttributesReference's Tag property.
This link
SecondOptionMgdDbg shows MgdDbg where would iterate through the BlockTable if the BlockTableRecord's IsLayout property is true then skip it(IsLayout is true if it is model space or any paper space) or,
If HasAttributeDefinitions property is false we will skip it, else we will look at the Attribute definition and see if the Tag property contains same value as the one we want.
If it does we will get the GetBlockReferenceIds and make sure to add the ones in the current space.
[CommandMethod("AttributeTags")]
public void AttributeTags()
{
Document doc = Application.DocumentManager.MdiActiveDocument;//Assign the Current Document to doc
Database db = doc.Database;// Assign doc's(Current Active Document) database to db
Editor ed = doc.Editor;// Assign Editor to doc
using (Transaction trx = db.TransactionManager.StartTransaction())// Start Transaction
{
BlockTable blocktable = trx.GetObject(db.BlockTableId, OpenMode.ForRead) as BlockTable;//Assign BlockTable
ed.WriteMessage("\n*******************\nItterate CurrentSpace\n****************\n");
// Using Database.CurrentSpaceId --- Accesses the ObjectId of the BlockTableRecord of the current space
// (e.g., model space or paper space.)
BlockTableRecord currentSpace = trx.GetObject(db.CurrentSpaceId, OpenMode.ForRead) as BlockTableRecord;
// Iterate all the entites in the current space
foreach (ObjectId objId in currentSpace)
{
// This is quicker than opening the actual object and checking the type
// When a object is created or loaded AutoCAD creates the Object and a ObjectId---(look @ ObjectId structure in docs)
// The value of the ObjectId is it's memory address and it contains the memory address or pointer to actual object returned by GetObject()
if (!(objId.ObjectClass.Name == "AcDbBlockReference"))// If not a BlockReference then skip it
{// objId.ObjectClass.DxfName == "BLOCKREFERENCE" could have been used
continue;
}
BlockReference blockreference = trx.GetObject(objId, OpenMode.ForRead) as BlockReference;
AttributeCollection attributeCollection = blockreference.AttributeCollection;
foreach (ObjectId attId in attributeCollection)
{
AttributeReference attributeReference = (AttributeReference)trx.GetObject(attId, OpenMode.ForRead);
if (attributeReference.Tag.ToUpper() == "ONEWEWANT")// If we do not care about casing make it all upper so not to worry
{
BlockTableRecord btr = (BlockTableRecord)trx.GetObject(blockreference.BlockTableRecord, OpenMode.ForRead);
ed.WriteMessage("{0} with Attribute Value {1}\n", btr.Name, attributeReference.TextString);
}
}
}
ed.WriteMessage("\n*******************\nItterate BlockTable\n****************\n");
// Itterating BlockTable
foreach (ObjectId objId in blocktable)
{
BlockTableRecord blkTbleRecord = trx.GetObject(objId, OpenMode.ForRead) as BlockTableRecord;
if (blkTbleRecord.IsLayout || !blkTbleRecord.HasAttributeDefinitions)
{
continue;
}
foreach (ObjectId entityId in blkTbleRecord)
{
if (!(entityId.ObjectClass.Name == "AcDbAttributeDefinition"))
{
continue;
}
AttributeDefinition attributeDef = trx.GetObject(entityId, OpenMode.ForRead) as AttributeDefinition;
if (!(attributeDef.Tag.ToUpper() == "ONEWEWANT"))
{
continue;
}
// Get the BlockReferenceIds
ObjectIdCollection blockreferenceIds = blkTbleRecord.GetBlockReferenceIds(true, false);
foreach (ObjectId brefId in blockreferenceIds)
{
BlockReference blockreference = trx.GetObject(brefId, OpenMode.ForRead) as BlockReference;
AttributeCollection attributeCollection = blockreference.AttributeCollection;
// Make Sure It is in Current Space
if (!(blockreference.BlockId == currentSpace.ObjectId))
{
continue;
}
foreach (ObjectId attId in attributeCollection)
{
AttributeReference attributeReference = (AttributeReference)trx.GetObject(attId, OpenMode.ForRead);
// Make Sure It is right Tag
if (attributeReference.Tag.ToUpper() == "ONEWEWANT")
{
ed.WriteMessage("{0} with Attribute Value {1}\n", blkTbleRecord.Name, attributeReference.TextString);
}
}
}
}
}
trx.Commit();
}
}