使用C#中的AutoCAD .NET API对CAD二次开发,获取块的属性
/// <summary>
/// 获得块对象的所有属性
/// </summary>
public void GetBlockAllAttr()
{
Document doc = Application.DocumentManager.MdiActiveDocument;
Database db = doc.Database;
Editor ed = doc.Editor;
// 获取选择对象
PromptSelectionResult psr = ed.GetSelection();
if (psr.Status != PromptStatus.OK)
{
ed.WriteMessage("没有选择任何块。");
return;
}
SelectionSet sSet = psr.Value;
ObjectId[] selectedIds = sSet.GetObjectIds(); // 获取选择集的ObjectiID.
ObjectId blockId = selectedIds[0]; // 假设只选择了一个块
using (Transaction tr = db.TransactionManager.StartTransaction())
{
BlockReference br = tr.GetObject(blockId, OpenMode.ForWrite) as BlockReference;
foreach (ObjectId item in br.AttributeCollection) // 遍历所有属性
{
AttributeReference attRef = (AttributeReference)item.GetObject(OpenMode.ForRead);
ed.WriteMessage("\n属性名和值:" + attRef.Tag.ToString() + " " + attRef.TextString.ToString());
}
}
}