I needed to list the level of nesting in a block and wrote something that seems to work ok, but is there a nice recursive way of doing this or a better way.
public static bool Nesting(string Blockname, out string sNested)
{
Document doc = acadApp.DocumentManager.MdiActiveDocument;
Database db = doc.Database;
sNested = "";
List<string> Nested=new List<string>();
using (Transaction tr = db.TransactionManager.StartTransaction())
{
BlockTable bt = tr.GetObject(db.BlockTableId, OpenMode.ForRead) as BlockTable;
if (!bt.Has(Blockname)) return false;
Nested.Add(Blockname);
for (int i = 0; i < Nested.Count; i++)
{
Blockname = Nested[i];
BlockTableRecord btr = tr.GetObject(bt[Blockname], OpenMode.ForRead) as BlockTableRecord;
foreach (ObjectId id in btr)
{
if (id.ObjectClass.DxfName == "INSERT")
{
BlockReference br = tr.GetObject(id, OpenMode.ForRead) as BlockReference;
if (!Nested.Contains(br.Name))
Nested.Add(br.Name);
}
}
}//next i
tr.Commit();
Nested.Sort();
for (int i = 0; i < Nested.Count; i++)
sNested+=Nested[i]+",";
sNested = sNested.Remove(sNested.Length - 1);
}
return Nested.Count>0;
} //end Nesting