使用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());
}
}
}
该代码示例展示了如何利用C#编程和AutoCAD.NETAPI来获取用户选择的CAD块的所有属性。首先,它获取当前文档和数据库,然后从编辑器中选择一个块。接着,在事务管理器中开始一个事务,通过块引用访问并遍历属性集合,打印出每个属性标签及其对应的文本值。
712

被折叠的 条评论
为什么被折叠?



