简体   繁体   English

c#,autocad 插件,更新 object 属性的文本

[英]c#, autocad plugins, Updating text of object properties

I need to write AutoCAD plugin to display the area of the object. Below is my code.我需要编写 AutoCAD 插件来显示 object 的区域。下面是我的代码。

It works fine, but test is static. I need to keep tracking the area of the circle cir.Area.ToString() ;.它工作正常,但测试是 static。我需要继续跟踪圆cir.Area.ToString()的面积;。 Currently, If I change the size of the circle latter on, the text does not change anymore.目前,如果我稍后更改圆圈的大小,文本将不再更改。 For example, the area of my circle is 10. I run code, it displays 10. But if I change the radius of circle, the text remains 10. How can I make it working.例如,我的圆的面积是 10。我运行代码,它显示 10。但是如果我改变圆的半径,文本仍然是 10。我怎样才能让它工作。

[CommandMethod("displayarea")]
public static void Displayarea()
{
    var doc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;
    var db = doc.Database;
    var ed = doc.Editor;
    var filter = new SelectionFilter(new[] { new TypedValue(0, "Circle") });
    var selection = ed.GetSelection(filter);


    if (selection.Status != PromptStatus.OK)
        return;
    using (var tr = db.TransactionManager.StartTransaction())
    {
        var curSpace = (BlockTableRecord)tr.GetObject(db.CurrentSpaceId, OpenMode.ForWrite);
        foreach (var id in selection.Value.GetObjectIds())
        {
            var ids = new ObjectIdCollection(new[] { id });

            Circle cir = (Circle)tr.GetObject(id, OpenMode.ForRead) as Circle;
            var _centerPosition = cir.Center;

                using (DBText acText = new DBText())
                {
                    acText.Position = _centerPosition;
                    acText.TextString = cir.Area.ToString();
                    acText.Height = 0.5;
                    curSpace.AppendEntity(acText);
                    tr.AddNewlyCreatedDBObject(acText, true);
                }

        }
        tr.Commit();
    }
}

Also you can use oEntity.Modified += OEntity_Modified;您也可以使用oEntity.Modified += OEntity_Modified;

Find this: Find which properties changed on modified event查找: 查找在修改事件时更改了哪些属性

It works by replacing它通过更换工作

cir.Area.ToString();

to

string circarea = "%<\\AcObjProp Object(%<\\_ObjId "
                                    + CircleId
                                    + ">%).Area \\f \"%lu2\">%";

You need to use fields .您需要使用fields

Find this: https://www.keanw.com/2007/07/accessing-the-a.html找到这个: https://www.keanw.com/2007/07/accessing-the-a.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM