shilvyan  

本来想清晰的描述扩展数据的数据结构、存储,读取等,发现真正写的时候,却不是那么回事,先列上几个关键的函数,后续在整理吧。  

 

定义一个设置图元属性的命令:sxd

 1       /// <summary>
 2         /// 设置实体属性
 3         /// </summary>
 4         /// <param name="objid"></param>
 5         /// <param name="appName"></param>
 6         /// <param name="rb"></param>
 7         /// <returns></returns>
 8         [CommandMethod("SXD")]
 9         public void SetXData()
10         {
11             Document doc = Application.DocumentManager.MdiActiveDocument;
12             Editor ed = doc.Editor;
13             PromptEntityOptions opt = new PromptEntityOptions("\n 请选择实体:");
14             PromptEntityResult res = ed.GetEntity(opt);
15             if (res.Status == PromptStatus.OK)
16             {
17                 Transaction tran = doc.TransactionManager.StartTransaction();
18                 using (tran)
19                 {
20                     DBObject obj = tran.GetObject(res.ObjectId, OpenMode.ForWrite);
21                     AddRegAppTableRecord("sly");
22                     //ResultBuffer rb = new ResultBuffer(
23                     //    new TypedValue(1001,"sly"),
24                     //    new TypedValue(1000,"这是我的名字")
25                     //    );
26 
27                     ResultBuffer rb = new ResultBuffer(
28                          new TypedValue(1001, "sly"),
29                       new TypedValue((int)DxfCode.ExtendedDataAsciiString, "这是我的名字")
30                        );
31                     obj.XData = rb;
32                     rb.Dispose();
33                     tran.Commit();
34                 }
35             }
36         }
View Code

 

获取图元属性:

 [CommandMethod("GXD")]
        public void GetXData()
        {
            Document doc = Application.DocumentManager.MdiActiveDocument;
            Editor ed = doc.Editor;
            PromptEntityOptions opt = new PromptEntityOptions("\n 请选择实体:");
            PromptEntityResult res = ed.GetEntity(opt);
            if (res.Status == PromptStatus.OK)
            {
                Transaction tran = doc.TransactionManager.StartTransaction();
                using (tran)
                {
                    DBObject obj = tran.GetObject(res.ObjectId, OpenMode.ForRead);
                    ResultBuffer rb = obj.XData;
                    if (rb == null)
                    {
                        ed.WriteMessage("\n 该图元没有实体附加!");

                    }
                    else
                    {
                        int n = 0;
                        foreach (TypedValue tv in rb)
                        {
                            ed.WriteMessage("\n TypeValue{0} - type:{1},{2}", n++,
                                tv.TypeCode,
                                tv.Value
                                );
                        }
                        rb.Dispose();

                    }


                }

            }
        }
View Code

 

 

定义一个添加扩展属性的函数:

/// <summary>
        /// 对一个实体进行写属性
        /// </summary>
        /// <param name="objid"></param>
        /// <param name="appName"></param>
        /// <param name="rb"></param>
        /// <returns></returns>
        public bool AddXdata(ObjectId objid, string appName, ResultBuffer rb)
        {
            bool returnValue = false;
            try
            {
                using (Database db = HostApplicationServices.WorkingDatabase)
                {
                    using (Transaction tran = db.TransactionManager.StartTransaction())
                    {
                        RegAppTable rat = (RegAppTable)tran.GetObject(db.RegAppTableId, OpenMode.ForWrite);
                        RegAppTableRecord ratr;
                        ObjectId ratrid = ObjectId.Null;
                        if (rat.Has(appName))
                        {
                            ratrid = rat[appName];
                        }
                        else
                        {
                            ratr = new RegAppTableRecord();
                            ratr.Name = appName;
                            ratrid = rat.Add(ratr);
                            tran.AddNewlyCreatedDBObject(ratr, true);
                        }
                        Entity en;
                        en = (Entity)tran.GetObject(objid, OpenMode.ForWrite);
                        en.XData = rb;
                        tran.Commit();
                        returnValue = true;
                    }

                }
            }
            catch
            {
                returnValue = false;
            }
            return returnValue;
        }
View Code

 

 

 

 

 

 

posted on 2014-03-04 10:34  shilvyan  阅读(2657)  评论(0编辑  收藏  举报