//添加扩展数据
 private void AddXData_Button_Click(object sender, EventArgs e)
 {
     using (AddEntityXDataForm addentityxdataform = new AddEntityXDataForm())
     {
         Autodesk.AutoCAD.ApplicationServices.Application.ShowModalDialog(addentityxdataform);
     }
 }
        //用户添加扩展记录
        public static void UserAddXData()
        {
            Editor ed = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.Editor;

            ed.WriteMessage("添加扩充数据 XDATA\n");
            PromptEntityOptions entOps = new PromptEntityOptions("选择实体对象");
            PromptEntityResult  entRes;

            entRes = ed.GetEntity(entOps);
            if (entRes.Status != PromptStatus.OK)
            {
                ed.WriteMessage("选择对象失败,退出");
                return;
            }
            //出现添加扩展数据面板
            AddEntityXDataForm addentityxdataform = new AddEntityXDataForm();

            addentityxdataform.Show();

            ObjectId objId = entRes.ObjectId;
            Database db    = HostApplicationServices.WorkingDatabase;

            using (Transaction trans = db.TransactionManager.StartTransaction())
            {
                RegAppTable appTbl     = trans.GetObject(db.RegAppTableId, OpenMode.ForWrite) as RegAppTable;
                string      DateStr    = DateTime.Now.ToString("yyyyMMddHHmmssms"); //日期
                Random      rd         = new Random();                              //用于生成随机数
                string      AppNamestr = DateStr + rd.Next(10, 99);                 //带日期的随机数
                Entity      ent        = trans.GetObject(objId, OpenMode.ForWrite) as Entity;

                if (!appTbl.Has(AppNamestr))
                {
                    RegAppTableRecord appTblRcd = new RegAppTableRecord();
                    appTblRcd.Name = AppNamestr;
                    appTbl.Add(appTblRcd);
                    trans.AddNewlyCreatedDBObject(appTblRcd, true);
                }

                ResultBuffer resBuf = new ResultBuffer(
                    new TypedValue((int)DxfCode.ExtendedDataRegAppName, AppNamestr),
                    new TypedValue((int)DxfCode.ExtendedDataLayerName, "0"),
                    new TypedValue((int)DxfCode.ExtendedDataReal, 1.23479137438413E+40),
                    new TypedValue((int)DxfCode.ExtendedDataInteger16, 32767),
                    new TypedValue((int)DxfCode.ExtendedDataInteger32, 32767),
                    new TypedValue((int)DxfCode.ExtendedDataScale, 10),
                    new TypedValue((int)DxfCode.ExtendedDataWorldXCoordinate, new Point3d(10, 10, 0)));
                resBuf.Add(new TypedValue((int)DxfCode.ExtendedDataAsciiString, "这是追加的数据"));
                resBuf.Add(new TypedValue((int)DxfCode.ExtendedDataAsciiString, "我又一次追加了数据"));
                resBuf.Add(new TypedValue((int)DxfCode.ExtendedDataInteger16, 32767));

                if (ent.XData != null)
                {
                    ed.WriteMessage("该对象已有扩展记录,不需要再次添加,只需在原有记录进行修改");
                    return;
                }
                ent.XData = resBuf;
                trans.Commit();
            }
        }