// Define Command "LayerReport"
[CommandMethod("LayerReport")]
static public void LayerReportCommand() {
// Get a pointer to the currently active document...
Document currentDocument = acadApp.DocumentManager.MdiActiveDocument;
//...and a pointer to the currently active documents database...
Database currentDatabase = currentDocument.Database;
//...and the editor whilst we're at it...
Editor ed = currentDocument.Editor;
PromptEntityOptions promptEntOpts = new PromptEntityOptions("Select an entity: ");
// Ask the question...
PromptEntityResult promptEntRes = ed.GetEntity(promptEntOpts);
// Did we succeed?
if (promptEntRes.Status != PromptStatus.OK)
return; // Nope - bailski!
// Kick off a transaction on the dbase...
using (Transaction tr = currentDocument.TransactionManager.StartTransaction()) {
// Open up the selected entity for a read op only...
Entity selectedEntity = tr.GetObject(promptEntRes.ObjectId, OpenMode.ForRead, false) as Entity;
// Did we get it?
if (selectedEntity == null)
return;
// Get the layer of the entity...
string selectedEntityLayerName = selectedEntity.Layer;
// Now we loop the block table looking for entities that have the same layer.
// Open the block table for read...
BlockTable blkTbl = tr.GetObject(currentDatabase.BlockTableId, OpenMode.ForRead, false) as BlockTable;
// Did we get it?
if (blkTbl == null)
return; // Nope - out we go!
// Declare our hashtable for storing out stuff
Hashtable layerReportTable = null;
// Loop the table...
foreach (ObjectId blkTblRcdId in blkTbl) {
BlockTableRecord blkTblRcd = tr.GetObject(blkTblRcdId, OpenMode.ForRead, false) as BlockTableRecord;
if (blkTblRcd == null)
continue; // Failed to open it for some reason
// Loop all the entities in the block table record.
// This approach will inherently get ALL entities in the dbase...
foreach (ObjectId entityId in blkTblRcd) {
Entity blkTblRcdEntity = tr.GetObject(entityId, OpenMode.ForRead, false) as Entity;
if (blkTblRcdEntity == null)
continue; // Failed to get entity in block table record for some reason.
// Is it on the same layer as the originally selected entity?
if (blkTblRcdEntity.Layer == selectedEntityLayerName) {
if (layerReportTable == null)
layerReportTable = new Hashtable();
// Yep - got a match! Print up the entity type
Type entityType = blkTblRcdEntity.GetType();
// Does our table contain the object type already?
if (!layerReportTable.ContainsKey(entityType))
layerReportTable.Add(entityType, 1); // No, so add it in.
else // Yes, so increment the count.
layerReportTable[entityType] = (int)layerReportTable[entityType] + 1;
}//if
}//foreach
}//foreach
// Did we get anything?
if (layerReportTable != null && layerReportTable.Count > 0)
// print out the results...
ed.WriteMessage("\nEntities on layer {0}: ", selectedEntityLayerName);
foreach (DictionaryEntry de in layerReportTable)
ed.WriteMessage("\n{0} : {1}", de.Key, de.Value);
// Last off commit the transaction, although not strictly necessary.
tr.Commit();
}//using
}
Cheers,
Glenn.