当前位置:   article > 正文

使用C#中的AutoCAD .NET API对CAD二次开发,获取为块添加属性_autocad 开发 .net api

autocad 开发 .net api

public void AddBlockAttr()
{
    Document doc = Application.DocumentManager.MdiActiveDocument;
    Database db = doc.Database;
    Editor ed = doc.Editor;

    using (Transaction tr = db.TransactionManager.StartTransaction())
    {
        // 获取选择对象
        PromptSelectionResult psr = ed.GetSelection();
        if (psr.Status != PromptStatus.OK)
        {
            ed.WriteMessage("没有选择任何块。");
            return;
        }

        SelectionSet ss = psr.Value;
        foreach (SelectedObject selObj in ss)
        {
            // 获取块参照对象
            BlockReference blockRef = tr.GetObject(selObj.ObjectId, OpenMode.ForWrite) as BlockReference;
            if (blockRef == null)
            {
                ed.WriteMessage("选择的对象不是块参照。");
                continue;
            }
            // 添加自定义属性
            AttributeDefinition attDef = new AttributeDefinition();
            attDef.Position = blockRef.Position;
            attDef.Tag = "MyCustomProperty";  //YourAttributeName"
            attDef.Prompt = "请输入自定义属性值:"; //"Enter a value for the attribute:";
            attDef.TextString = "12345";  //Default value
            attDef.Height = 2.5;
            attDef.Justify = AttachmentPoint.MiddleCenter;
            attDef.AlignmentPoint = blockRef.Position;
            attDef.Layer = "0";
            attDef.ColorIndex = 256;

            // 将属性添加到块参照对象的属性集合中
            AttributeReference attRef = new AttributeReference();
            attRef.SetAttributeFromBlock(attDef, blockRef.BlockTransform);
            blockRef.AttributeCollection.AppendAttribute(attRef);
            tr.AddNewlyCreatedDBObject(attRef, true);

        }

        tr.Commit();
    }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小丑西瓜9/article/detail/111817
推荐阅读
相关标签
  

闽ICP备14008679号