以下代碼展示如何周遊 AutoCAD 文檔中的組。
Document doc = Application.DocumentManager.MdiActiveDocument;
Database db = doc.Database;
using (Transaction tr = db.TransactionManager.StartTransaction())
{
DBDictionary groups = tr.GetObject(db.GroupDictionaryId,
OpenMode.ForRead) as DBDictionary;
// 周遊所有的組
foreach (DBDictionaryEntry entry in groups)
{
Group group = tr.GetObject(entry.Value, OpenMode.ForRead) as Group;
ObjectId[] ids = group.GetAllEntityIds();
// 周遊組下的圖元
foreach (ObjectId oid in ids)
{
Entity entity = tr.GetObject(oid, OpenMode.ForRead) as Entity;
}
doc.Editor.WriteMessage("Group: {0} 包含 {1} 個圖元\n", group.Name, ids.Length);
}
tr.Commit();
}
參考文章:
Iterating through the group dictionary