使用C#中的AutoCAD .NET API对CAD二次开发,获取块的属性

使用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());
        }
    }
}
  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
使用C#CAD2019进行二次开发获取属性,可以使用AutoCAD .NET API。以下是获取属性的示例代码: ```csharp // 获取当前文档 Document doc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument; // 获取当前文档的数据库 Database db = doc.Database; // 获取当前文档的编辑器 Editor ed = doc.Editor; // 提示用户选择 PromptEntityOptions peo = new PromptEntityOptions("\n请选择:"); peo.SetRejectMessage("\n只能选择!"); peo.AddAllowedClass(typeof(BlockReference), true); PromptEntityResult per = ed.GetEntity(peo); if (per.Status == PromptStatus.OK) { // 打开事务 using (Transaction trans = db.TransactionManager.StartTransaction()) { // 获取对象 BlockReference blkRef = trans.GetObject(per.ObjectId, OpenMode.ForRead) as BlockReference; if (blkRef != null) { // 获取定义对象 BlockTableRecord btr = trans.GetObject(blkRef.BlockTableRecord, OpenMode.ForRead) as BlockTableRecord; if (btr != null) { // 获取属性定义集合 AttributeDefinitionCollection attDefs = btr.AttributeCollection; foreach (ObjectId attDefId in attDefs) { AttributeDefinition attDef = trans.GetObject(attDefId, OpenMode.ForRead) as AttributeDefinition; if (attDef != null && !attDef.Constant) { // 获取属性值 AttributeReference attRef = new AttributeReference(); attRef.SetAttributeFromBlock(attDef, blkRef.BlockTransform); ed.WriteMessage($"\n{attDef.Tag}: {attRef.TextString}"); } } } } // 提交事务 trans.Commit(); } } ``` 以上代码通过选择获取定义对象,遍历属性定义集合,获取属性值,并在命令行输出属性名和属性值。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值