简体   繁体   中英

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.

It works fine, but test is static. I need to keep tracking the area of the circle 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.

[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;

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 .

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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