EaBIM一直以来积极响应国家“十二五”推进建筑业信息化的号召,对建筑领域的信息技术开展深入技术交流和探讨!致力于打造“BIM-建筑师-生态技术”三位一体综合资源交流共享平台,希望为BIM与可持续设计理念及技术的普及做出微小的贡献!!!

EaBIM

 找回密码
 注册

QQ登录

只需一步,快速开始

扫一扫,访问微社区

搜索
查看: 623|回复: 0
打印 上一主题 下一主题

[实体对象] 使用LINQ过滤实体

[复制链接]

1514

主题

7465

帖子

1万

积分

admin

Rank: 10Rank: 10Rank: 10Rank: 10Rank: 10Rank: 10Rank: 10Rank: 10Rank: 10Rank: 10

积分
12408

社区QQ达人

跳转到指定楼层
楼主
发表于 2014-1-8 14:57:49 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
Let us look at a cool AutoCAD .NET coding gadget in this post. It uses the cool modern LINQ technology to filter AutoCAD entities with various criteria, e.g. finding all circles with red color and dashed line type on a specific layer.
In the old days without the LINQ technology, this is a not so straightforward and actually an error prone task. We generally had to use many out-of-dated ADS stuffs such as result buffer, DXF codes, selection set, and different sets of relational and logical operators which are not familiar to C# or C++ programmers at all, though they have been wrapped a bit and renamed in the AutoCAD .NET as such, TypedValue, DxfCode, SelectionFilter, etc.
With the wonderful LINQ technology handy, we do not have to care about these unfriendly legacy stuffs anymore. We can make the same work done more nicely, naturally and efficiently with less code using the LINQ technology. Here we go.


  1. private static ObjectId[] FindCircles(short color, string layer, string linetype)
  2. {
  3.     ObjectId[] ret;
  4.     using (Transaction tr = HostApplicationServices.WorkingDatabase.TransactionManager.StartTransaction())
  5.     {
  6.         BlockTable bt = tr.GetObject(HostApplicationServices.WorkingDatabase.BlockTableId, OpenMode.ForRead) as BlockTable;
  7.         BlockTableRecord btr = tr.GetObject(bt[BlockTableRecord.ModelSpace], OpenMode.ForRead) as BlockTableRecord;
  8.         var v = from ObjectId id in btr
  9.                 where OpenObject(tr, id) is Circle &&
  10.                         OpenObject(tr, id).ColorIndex == color &&
  11.                         OpenObject(tr, id).Layer == layer &&
  12.                         OpenObject(tr, id).Linetype == linetype
  13.                 select id;
  14.         ret = v.ToArray();
  15.         tr.Commit();
  16.     }
  17.     return ret;
  18. }
  19. private static Entity cachedEnt = null;
  20. private static ObjectId cachedId = ObjectId.Null;
  21. private static Entity OpenObject(Transaction tr, ObjectId id)
  22. {
  23.     if (cachedId != id || cachedEnt == null)
  24.         cachedEnt = tr.GetObject(id, OpenMode.ForRead) as Entity;
  25.     return cachedEnt;
  26. }
  27. [CommandMethod("TestLinqInAutoCAD")]
  28. public static void TestLinqInAutoCAD_Method()
  29. {
  30.     Editor ed = MgdAcApplication.DocumentManager.MdiActiveDocument.Editor;
  31.     try
  32.     {
  33.         ObjectId[] ids = FindCircles(1, "TEST", "DASHED");
  34.         ed.WriteMessage(string.Format("There are {0} circles with red color and dashed line type on the layer 'test'.", ids.Length));
  35.     }
  36.     catch (System.Exception ex)
  37.     {
  38.         ed.WriteMessage(ex.ToString());
  39.     }
  40. }
  41. In case there are some circles meeting the filtering criteria in the current drawing, the command may print out something onto the AutoCAD command line window as follows:
  42. Command: TestLinqInAutoCAD
  43. There are 76 circles with red color and dashed line type on the layer 'test'.
  44. Enjoy it! More coding gadgets will be created and demonstrated in the future. Please stay tuned.
  45. The leading edge AutoCAD .NET Addin Wizard (AcadNetAddinWizard) provides various project wizards, item wizards, coders and widgets to help program AutoCAD .NET addins.
复制代码

分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友 微信微信
收藏收藏 转播转播 分享分享 分享淘帖 支持支持 反对反对

相关帖子

工作时间:工作日的9:00-12:00/13:30-18:00,节假日不在线,请勿留言
*滑块验证:
您需要登录后才可以回帖 登录 | 注册

本版积分规则

QQ|EaBIM网 ( 苏ICP备2020058923号-1  苏公网安备32011502011255号

GMT+8, 2024-12-4 02:00

Powered by Discuz! X3.2 Licensed

© 2001-2013 Comsenz Inc.

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