cad.net 图元反应器,图元事件,要加在提交数据库之后哟

直接看代码

using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.EditorInput;
using Autodesk.AutoCAD.Runtime;
using Acap = Autodesk.AutoCAD.ApplicationServices.Application;
using Autodesk.AutoCAD.Geometry;
using System;

namespace JoinBox
{
#if NET48
    public class Command_Test_MTextModified
    {
        [CommandMethod("Test_MTextModified", CommandFlags.Modal | CommandFlags.UsePickSet | CommandFlags.DocExclusiveLock)]
        public void Test_MTextModified()
        {
            Database db = HostApplicationServices.WorkingDatabase;//当前的数据库   
            //using (Acap.DocumentManager.MdiActiveDocument.LockDocument())//锁文档
            {
                using (Transaction tr = db.TransactionManager.StartTransaction())
                {
                    for (int i = 0; i < 10; i++)
                    {
                        var acBlkTblRec = tr.GetObject(db.CurrentSpaceId, OpenMode.ForWrite) as BlockTableRecord;

                        var mText = new MText
                        {
                            Contents = "文字" + i.ToString(),
                            TextHeight = 3,
                            TextStyleId = db.Textstyle,
                            Location = new Point3d(i, i, 0),
                        }; 
                        acBlkTblRec.AppendEntity(mText);
                        tr.AddNewlyCreatedDBObject(mText, true);

                        mText.Modified += MText_Modified; //要加在这里,如果加在提交之前,所有的事件将指向第一个图元..
                    }
                    tr.Commit();
                }  
            }
        }

        private void MText_Modified(object sender, EventArgs e)
        {
            Editor ed = Acap.DocumentManager.MdiActiveDocument.Editor;
            ed.WriteMessage("
测试" + ((MText)sender).Text);
        }

    }
#endif
}
原文地址:https://www.cnblogs.com/JJBox/p/13811012.html