So, I once again tried using this code.
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.Runtime;
using System.Windows.Forms;
using System;
namespace Anonymous_Blocks
{
public class PropertyDebug
{
[CommandMethod("ShowProperties")]
public void ShowProperties()
{
int countAnonymous = 0;
int countReference = 0;
Autodesk.AutoCAD.ApplicationServices.Document currentDoc =
Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;
Database currentDatabase = currentDoc.Database;
string message = "Properties of all blocks in the drawing:";
using(Transaction trans = currentDatabase.TransactionManager.StartTransaction())
{
BlockTable blockTable = trans.GetObject(currentDatabase.BlockTableId, OpenMode.ForRead) as BlockTable;
foreach(ObjectId recordId in blockTable)
{
BlockTableRecord blockTableRecord = trans.GetObject(recordId, OpenMode.ForRead) as BlockTableRecord;
// Check if the block table record is dynamic block and NOT null
if (blockTableRecord != null && blockTableRecord.IsDynamicBlock)
{
foreach (ObjectId blockId in blockTableRecord.GetAnonymousBlockIds())
{
countAnonymous++;
message += GetBlockProps(blockId, trans);
}
foreach (ObjectId referenceId in blockTableRecord.GetBlockReferenceIds(true, true))
{
countReference++;
message += GetBlockProps(referenceId, trans);
}
}
}
trans.Commit();
}
MessageBox.Show(string.Format("There are {0} anonymous and {1} reference blocks in the drawing", countAnonymous, countReference));
MessageBox.Show(message);
}
private string GetBlockProps(ObjectId blockId, Transaction transaction)
{
string message = "";
try
{
BlockReference blockRef = transaction.GetObject(blockId, OpenMode.ForRead) as BlockReference;
DynamicBlockReferencePropertyCollection propCollection = blockRef.DynamicBlockReferencePropertyCollection;
message += "\nBlock: " + blockRef.Name;
foreach (DynamicBlockReferenceProperty property in propCollection)
{
message += "\n\t" + property.PropertyName + ": " + property.Value;
}
}
catch (System.Exception ex)
{
message += "\n" + ex.Message;
}
return message;
}
}
}
I also attached a file with 8 top level block references, 4 of them being clones of the block table record and 4 of them being anonymous due to modified "length" property.
At the GetBlockProps() Method I try to get the blockReference corresponded to the Object ID (line 57).
For all anonymous blocks it results in null.
Unfortunately the official AutoCAD API reference is not very clear to me, raising more questions in general than answering.