找回密码
 立即注册

QQ登录

只需一步,快速开始

扫一扫,访问微社区

查看: 958|回复: 0

[分享] What's the best way to iterate through an entire Database?

[复制链接]

已领礼包: 593个

财富等级: 财运亨通

发表于 2013-5-26 18:18:32 | 显示全部楼层 |阅读模式

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

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

×
What's the best way to iterate through an entire Database?                                                        By Philippe Leefsma
  The objects stored in the database get an handle value that gets incremented sequentially, so the fastest approach if one wants to iterate through the whole content of a Database, is to start from handle = 1 to the max handle existing in the Database. P/Invoking "acdbHandEnt" can then help to retrieve the ObjectId from an handle.
  The following C# code illustrates how to do that:
  1. public struct ads_name
  2. {
  3.     public IntPtr a;
  4.     public IntPtr b;
  5. };

  6. [DllImport("acdb19.dll",
  7. CharSet = CharSet.Unicode,
  8. CallingConvention = CallingConvention.Cdecl,
  9. EntryPoint = "acdbHandEnt")]
  10. public static extern int acdbHandEnt(
  11.     string handle, ref ads_name name);

  12. [CommandMethod("IterateDb")]
  13. static public void IterateDb()
  14. {
  15.     ads_name ename = new ads_name();
  16.     Document doc = Application.DocumentManager.MdiActiveDocument;
  17.     Database db = doc.Database;
  18.     Editor ed = doc.Editor;
  19.     ObjectIdCollection validIds = new ObjectIdCollection();
  20.     // now get the last handle in the db
  21.     Handle handseed = db.Handseed;
  22.     // copy the handseed total into an efficient raw datatype
  23.     long handseedTotal = handseed.Value;
  24.     // loop from 0 to the last handle (this could be a big loop)
  25.     for (long i = 1; i < handseedTotal; ++i)
  26.     {
  27.         string handle = Convert.ToString(i, 16);
  28.         // get the ename by converting
  29.         // the long to a hex value string
  30.         int res = acdbHandEnt(handle, ref ename);
  31.         if (res != 5100) // RTNORM
  32.             continue;
  33.         // convert the ename to an objectid
  34.         ObjectId id = new ObjectId(ename.a);
  35.         // check if it's a valid objectId
  36.         if (!id.IsValid)
  37.             continue;
  38.         try
  39.         {
  40.             // open if not erased
  41.             using (DBObject obj =
  42.                 id.Open(OpenMode.ForRead, false))
  43.             {
  44.                 validIds.Add(id);
  45.             }
  46.         }
  47.         catch
  48.         {

  49.         }
  50.     }
  51.     ed.WriteMessage("\nNumber of retrieved valid ObjectIds: "
  52.         + validIds.Count.ToString());
  53.     validIds.Clear();

  54. }

评分

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

查看全部评分

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

本版积分规则

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

GMT+8, 2024-5-22 04:39 , Processed in 0.455563 second(s), 35 queries , Gzip On.

Powered by Discuz! X3.5

© 2001-2024 Discuz! Team.

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