在cad开发过程中,经常会遇到这样的需求,就是需要导出把框选区域的属性块的属性批量导出,下述代码已封装好。
#region 根据块引用id获取动态块属性
public static Dictionary<string, string> GetBlockRefAttDic(this ObjectId blockRefId)
{
Dictionary<string, string> attDic = new Dictionary<string, string>();
Database db = HostApplicationServices.WorkingDatabase;
using (Transaction trans = db.TransactionManager.StartTransaction())
{
BlockReference bkRef = (BlockReference)blockRefId.GetObject(OpenMode.ForRead);
BlockTable bt = (BlockTable)trans.GetObject(db.BlockTableId, OpenMode.ForRead);
string bkRefName = bkRef.Name;
BlockTableRecord btr = (BlockTableRecord)trans.GetObject(bt[bkRefName], OpenMode.ForRead);
if (btr.HasAttributeDefinitions)
{
foreach (ObjectId objid in bkRef.AttributeCollection)
{
AttributeReference attr = (AttributeReference)objid.GetObject(OpenMode.ForRead);
attDic[attr.Tag.ToString()] = attr.TextString;
}
}
}
return attDic;
}
#endregion