找回密码
 立即注册

QQ登录

只需一步,快速开始

扫一扫,访问微社区

查看: 1225|回复: 0

[分享] Using .NET API to Add and Remove XData

[复制链接]

已领礼包: 593个

财富等级: 财运亨通

发表于 2013-5-25 03:28:18 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有账号?立即注册

×
Using .NET API to Add and Remove XData                                                         By Virupaksha Aithal
The read/write property XData of the Autodesk.AutoCAD.DatabaseServices.DBObject class is used to get and set  XData of any database resident object. This example demonstrates the same for an entity.
  1. [CommandMethod("ADDXDATA")]
  2. static public void AddXdata()
  3. {
  4.     Document doc =
  5.         Application.DocumentManager.MdiActiveDocument;
  6.     Database db =
  7.         doc.Database;
  8.     Transaction tr =
  9.         db.TransactionManager.StartTransaction();
  10.     using(tr)
  11.     {
  12.         Editor ed =
  13.             Application.DocumentManager.MdiActiveDocument.Editor;
  14.         // Prompt the user to select an entity
  15.         PromptEntityResult ers =
  16.            ed.GetEntity("Pick entity ");
  17.         // Open the entity
  18.         Entity ent =
  19.           (Entity)tr.GetObject(ers.ObjectId,
  20.                                         OpenMode.ForWrite);
  21.         // Get the registered application names table
  22.         RegAppTable regTable =
  23.            (RegAppTable)tr.GetObject(db.RegAppTableId,
  24.                                         OpenMode.ForRead);
  25.         if(!regTable.Has("ADS"))
  26.         {
  27.             regTable.UpgradeOpen();
  28.    // Add the application names that would be used to add Xdata
  29.             RegAppTableRecord app =
  30.                     new RegAppTableRecord();
  31.             app.Name = "ADS";
  32.             regTable.Add(app);
  33.             tr.AddNewlyCreatedDBObject(app, true);
  34.         }
  35.         // Append the Xdata to the entity - two different
  36.         // applications added.
  37.         ent.XData = new ResultBuffer(new TypedValue(1001, "ADS"),
  38.                                 new TypedValue(1070, 100));
  39.         tr.Commit();
  40.     }
  41. }

  42. [CommandMethod("REMXDATA")]
  43. static public void RemoveXdata() // This method can have any name
  44. {
  45.     Document doc =
  46.         Application.DocumentManager.MdiActiveDocument;
  47.     Database db =
  48.         doc.Database;
  49.     Transaction tr =
  50.         db.TransactionManager.StartTransaction();
  51.     using (tr)
  52.     {
  53.         Editor ed =
  54.             Application.DocumentManager.MdiActiveDocument.Editor;
  55.         try
  56.         {
  57.             // Prompt the user to select an entity
  58.             PromptEntityResult ers =
  59.                 ed.GetEntity("Pick entity ");
  60.             // Open the selected entity
  61.             Entity ent =
  62.                 (Entity)tr.GetObject(ers.ObjectId,
  63.                             OpenMode.ForRead);
  64.             ResultBuffer buffer =
  65.                 ent.GetXDataForApplication("ADS");
  66.             // This call would ensure that the
  67.             //Xdata of the entity associated with ADSK application
  68.             //name only would be removed
  69.             if (buffer != null)
  70.             {
  71.                 ent.UpgradeOpen();
  72.                 ent.XData =
  73.                     new ResultBuffer(new TypedValue(1001, "ADS"));
  74.                 buffer.Dispose();
  75.             }
  76.             tr.Commit();
  77.         }
  78.         catch
  79.         {
  80.             tr.Abort();
  81.         }
  82.     }
  83. }


评分

参与人数 1D豆 +2 收起 理由
ScmTools + 2 很给力!资料分享奖!

查看全部评分

论坛插件加载方法
发帖求助前要善用【论坛搜索】功能,那里可能会有你要找的答案;
如果你在论坛求助问题,并且已经从坛友或者管理的回复中解决了问题,请把帖子标题加上【已解决】;
如何回报帮助你解决问题的坛友,一个好办法就是给对方加【D豆】,加分不会扣除自己的积分,做一个热心并受欢迎的人!
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|申请友链|Archiver|手机版|小黑屋|辽公网安备|晓东CAD家园 ( 辽ICP备15016793号 )

GMT+8, 2024-5-4 05:30 , Processed in 0.550731 second(s), 31 queries , Gzip On.

Powered by Discuz! X3.5

© 2001-2024 Discuz! Team.

快速回复 返回顶部 返回列表