添加扩展数据XDATA

转载:http://blog.csdn.net/yuandingmao/article/details/5563452

使用ResultBuffer类 
        在ObjectARX中,resbuf结构给AutoCAD专门数据提供了一个灵活的存储器。在托管API中,这个结构被映射在Autodesk.AutoCAD.DatabaseServices.ResultBuffer类中。
     一个ResultBuffer类对象使用的方法与resbuf数据链相同。你要定义一个ResultBuffer并使它产生一系列的数据对。每一对数据由一个数据类型描述和一个值组成。在托管API中,
        这些数据对是Autodesk.AutoCAD.DatabaseServices.TypedValue类的实例。这个类实现的功能与resbuf结构的restype和resval成员一样。 
     TypedValue.TypeCode属性是一个16位的整数值,它表示TypedValue.Value属性的数据类型。可以使用的TypeCode值取决于ResultBuffer实例的具体使用情况。
        例如,适合于xrecord定义的TypeCode的值就不一定适用于xdata。Autodesk.AutoCAD.DatabaseServices.DxfCode枚举定义的代码精确地描述了所有可能的ResultBuffer数据类型的范围。
    因为TypedValue.Value属性与System.Object的一个实例相对应,所以它可以包含任何数据类型。但是,Value数据必须与TypeCode所表示的类一致,以保证结果的可用性。
    你可以通过给ResultBuffer的构造函数传递一个TypedValue对象数组来提前产生一个ResultBuffer,或者可以先构造一个空的ResultBuffer,
        然后通过调用ResultBuffer.Add()方法来加入新的TypedValue对象。

 #region 添加扩展数据XDATA
        /// <summary>
        /// 添加扩展数据xdata
        /// </summary>
        /// <param name=”ent”>被填充的实体</param>
        /// <param name=”obj”>填充的数据</param>
        /// <param name=”regapptablename”>regapptabl名称</param>
        /// <returns></returns>
        public Autodesk.AutoCAD.DatabaseServices.Entity AddXData(Autodesk.AutoCAD.DatabaseServices.Entity ent,Object obj,string regapptablename)
        {
           
            Autodesk.AutoCAD.DatabaseServices.ObjectId objId = ent.ObjectId;
            Autodesk.AutoCAD.DatabaseServices.Database db = Autodesk.AutoCAD.DatabaseServices.HostApplicationServices.WorkingDatabase;
             Autodesk.AutoCAD.DatabaseServices.Entity retEntity ;
            using (Autodesk.AutoCAD.DatabaseServices.Transaction trans = db.TransactionManager.StartTransaction())
            {
                retEntity = trans.GetObject(ent.ObjectId, Autodesk.AutoCAD.DatabaseServices.OpenMode.ForWrite) as Autodesk.AutoCAD.DatabaseServices.Entity;
                
                Autodesk.AutoCAD.DatabaseServices.RegAppTable appTbl = trans.GetObject(db.RegAppTableId, Autodesk.AutoCAD.DatabaseServices.OpenMode.ForWrite) as Autodesk.AutoCAD.DatabaseServices.RegAppTable;
                if (!appTbl.Has(regapptablename))
                {
                    Autodesk.AutoCAD.DatabaseServices.RegAppTableRecord appTblRcd = new Autodesk.AutoCAD.DatabaseServices.RegAppTableRecord();
                    appTblRcd.Name = regapptablename;
                    appTbl.Add(appTblRcd);
                    trans.AddNewlyCreatedDBObject(appTblRcd, true);
                }
                Autodesk.AutoCAD.DatabaseServices.ResultBuffer resBuf = new Autodesk.AutoCAD.DatabaseServices.ResultBuffer();

                
                // 先必须要将注册表添加到buffer里
                resBuf.Add(new Autodesk.AutoCAD.DatabaseServices.TypedValue((int)Autodesk.AutoCAD.DatabaseServices.DxfCode.ExtendedDataRegAppName, regapptablename));
                // 再将数据或对象加到buffer里 注意typecode 要是1000-1071里
                resBuf.Add(new Autodesk.AutoCAD.DatabaseServices.TypedValue((int)Autodesk.AutoCAD.DatabaseServices.DxfCode.ExtendedDataAsciiString, obj));//将某个对象添加到扩展数据中

                ent.XData = resBuf;
                trans.Commit();
            }

            return retEntity;

        }
        #endregion

        #region 获取扩展数据
        /// <summary>
        /// 获取扩展数据集合
        /// </summary>
        /// <param name=”ent”>实体</param>
        /// <returns>返回System.Collections.IEnumerator</returns>
        public System.Collections.IEnumerator GetXData(Autodesk.AutoCAD.DatabaseServices.Entity ent)
        {
            Autodesk.AutoCAD.EditorInput.Editor ed = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.Editor;

            System.Collections.IEnumerator itor;
            Autodesk.AutoCAD.DatabaseServices.Database db = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.Database;
            using (Autodesk.AutoCAD.DatabaseServices.Transaction trans = db.TransactionManager.StartTransaction())
            {
                ent = (Autodesk.AutoCAD.DatabaseServices.Entity)trans.GetObject(ent.ObjectId, Autodesk.AutoCAD.DatabaseServices.OpenMode.ForRead);
                Autodesk.AutoCAD.DatabaseServices.ResultBuffer resBuf = ent.XData;
                if (resBuf != null)
                {
                    //获取IEnumerator
                    itor = resBuf.GetEnumerator();

                }
                else
                    itor = null;
            }
            return itor;
        }

        /// <summary>
        /// 获取实体的扩展数据
        /// </summary>
        /// <param name=”ent”>实体</param>
        /// <returns>返回泛型的对象集</returns>
        public List<System.Object> GetXDataList(Autodesk.AutoCAD.DatabaseServices.Entity ent)
        {
            Autodesk.AutoCAD.EditorInput.Editor ed = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.Editor;

            List<System.Object> objs = new List<System.Object>();
            Autodesk.AutoCAD.DatabaseServices.Database db = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.Database;
            using (Autodesk.AutoCAD.DatabaseServices.Transaction trans = db.TransactionManager.StartTransaction())
            {
               ent = (Autodesk.AutoCAD.DatabaseServices.Entity)trans.GetObject(ent.ObjectId, Autodesk.AutoCAD.DatabaseServices.OpenMode.ForRead);
              
                Autodesk.AutoCAD.DatabaseServices.ResultBuffer resBuf = ent.XData;
                if (resBuf != null)
                {                   
                    System.Collections.IEnumerator itor = resBuf.GetEnumerator();
                    while (itor.MoveNext())
                    {
                        Autodesk.AutoCAD.DatabaseServices.TypedValue tmpVal = (Autodesk.AutoCAD.DatabaseServices.TypedValue)itor.Current;
                        objs.Add(tmpVal.Value);
                    }
                }
                trans.Commit();
            }
            return objs;
        }
        #endregion

此条目发表在ObjectArx分类目录。将固定链接加入收藏夹。

发表评论