转:实体ToolTip显示XData

时间:2023-11-10 19:30:08

首先要说明,实现方式不是我的原创,其中ToolTip的实现转自这里,然后XData的设置和获取来自Kean的博客,当然我自己也做了点研究,在这里,然后把这两个技术结合起来就是你将要看到的东西。

先上效果吧。

转:实体ToolTip显示XData

下面是实现的代码:

public class Class1:IExtensionApplication

    {

        void IExtensionApplication.Initialize()

        {

            Mytooltip();

        }

        void IExtensionApplication.Terminate()

        {

        }

        public void Mytooltip()

        {

            Document doc =Application.DocumentManager.MdiActiveDocument;

            Editor ed = doc.Editor;

            ed.PointMonitor += newPointMonitorEventHandler(MytooltipOn);

        }

        private void MytooltipOn(object sender,PointMonitorEventArgs e)

        {

            Database db =Application.DocumentManager.MdiActiveDocument.Database;

            FullSubentityPath[] ePaths = e.Context.GetPickedEntities();

            if (ePaths.Length > 0)

            {

                FullSubentityPath ePath = ePaths[0];

                using (Transaction trans = db.TransactionManager.StartTransaction())

                {

                    ObjectId entId = ePath.GetObjectIds()[0];

                    Entity ent = (Entity)trans.GetObject(entId,OpenMode.ForRead);

                    e.AppendToolTipText(GetXData(ent));

                    trans.Commit();

                }

            }

            else

            {

                e.AppendToolTipText("\n未指向实体");

            }

        }

        static public string GetXData(Entity ent)

        {

            string ret = "";

            Document doc =Application.DocumentManager.MdiActiveDocument;

            Editor ed = doc.Editor;

            ResultBuffer rb = ent.XData;

            if (rb == null)

                return "该实体不包含XData.";

            else

            {

                int n = 0;

                foreach (TypedValue tv in rb)

                {

                    ret += string.Format("类型值{0} - 类型: {1},值: {2}\n", n, tv.TypeCode, tv.Value);

                    n++;

                }

                rb.Dispose();

                return ret;

            }

        }

        [CommandMethod("SetXD")]

        static public void SetXData()

        {

            Document doc =Application.DocumentManager.MdiActiveDocument;

            Editor ed = doc.Editor;

            PromptEntityOptions peo = newPromptEntityOptions("\n请选择实体:");

            PromptEntityResult per = ed.GetEntity(peo);

            if (per.Status == PromptStatus.OK)

            {

                Transaction trans = doc.TransactionManager.StartTransaction();

                DBObject obj = trans.GetObject(per.ObjectId,OpenMode.ForWrite);

                AddRegAppTableRecord("Test");

                ResultBuffer rb = new ResultBuffer();

                rb.Add(new TypedValue(1001, "Test"));

                rb.Add(new TypedValue(1000, "Greeting! I am the XData"));

                rb.Add(new TypedValue(1000, "Today is very Cold"));

                obj.XData = rb;

                rb.Dispose();

                trans.Commit();

                trans.Dispose();

            }

        }

        static void AddRegAppTableRecord(string regAppName)

        {

            Document doc =Application.DocumentManager.MdiActiveDocument;

            Editor ed = doc.Editor;

            Database db = doc.Database;

            Transaction trans = doc.TransactionManager.StartTransaction();

            RegAppTable rat = (RegAppTable)trans.GetObject(db.RegAppTableId,OpenMode.ForRead, false);

            if (!rat.Has(regAppName))

            {

                rat.UpgradeOpen();

                RegAppTableRecord ratr = newRegAppTableRecord();

                ratr.Name = regAppName;

                rat.Add(ratr);

                trans.AddNewlyCreatedDBObject(ratr, true);

            }

            trans.Commit();

            trans.Dispose();

        }

    }

命令SetXD用于设置XData,不然新建的实体是不包含XData的。指向一个实体后,将会自动显示该实体的XData

原文地址:http://blog.csdn.net/gisoracle/article/details/7276782