明经CAD社区

 找回密码
 注册

QQ登录

只需一步,快速开始

搜索
查看: 4360|回复: 6

[符号表] 使用Linq在块表记录中获取同一类型的实体

[复制链接]
发表于 2010-1-13 18:50 | 显示全部楼层 |阅读模式
下面的例子在当前空间中遍历,并改变直线和圆的颜色,注意Cad版本2009及以上
相关的讨论帖
http://www.theswamp.org/index.php?topic=31489.0
  1.         [CommandMethod("t8")]
  2.         public static void Test8()
  3.         {
  4.             Document doc = Application.DocumentManager.MdiActiveDocument;
  5.             Database db = doc.Database;
  6.             using (Transaction tr = db.TransactionManager.StartTransaction())
  7.             {
  8.                 BlockTableRecord btr = (BlockTableRecord)tr.GetObject(db.CurrentSpaceId, OpenMode.ForRead);
  9.                 var dict =
  10.                     from ObjectId id in btr
  11.                     group id by id.ObjectClass.DxfName;
  12.                 foreach (var ids in dict)
  13.                 {
  14.                     switch (ids.Key)
  15.                     {
  16.                         case "LINE":
  17.                             foreach (var id in ids)
  18.                             {
  19.                                 Entity ent = id.GetObject(OpenMode.ForWrite) as Entity;
  20.                                 ent.ColorIndex = 1;
  21.                             }
  22.                             break;
  23.                         case "CIRCLE":
  24.                             foreach (var id in ids)
  25.                             {
  26.                                 Entity ent = id.GetObject(OpenMode.ForWrite) as Entity;
  27.                                 ent.ColorIndex = 3;
  28.                             }
  29.                             break;
  30.                     }
  31.                 }
  32.                 tr.Commit();
  33.             }
  34.         }

评分

参与人数 1威望 +1 明经币 +2 金钱 +20 贡献 +5 激情 +5 收起 理由
ahlzl + 1 + 2 + 20 + 5 + 5 【精华】好程序

查看全部评分

 楼主| 发表于 2010-1-13 18:54 | 显示全部楼层
本帖最后由 作者 于 2010-1-13 23:53:42 编辑

直接写成函数调用,:)
  1.         public static IEnumerable<ObjectId> GetObjectIds<T>(this BlockTableRecord btr) where T : Entity
  2.         {
  3.             string dxfName = RXClass.GetClass(typeof(T)).DxfName;
  4.             return
  5.                 from ObjectId id in btr
  6.                 where id.ObjectClass.DxfName == dxfName
  7.                 select id;
  8.         }
  9.         public static IEnumerable<IGrouping<string, ObjectId>> GetObjectIds(this BlockTableRecord btr)
  10.         {
  11.             return
  12.                 from ObjectId id in btr
  13.                 group id by id.ObjectClass.DxfName;
  14.         }
复制代码
发表于 2010-1-13 22:09 | 显示全部楼层
收到。顶起来
发表于 2010-1-13 23:18 | 显示全部楼层

RXClass.GetClass(typeof(T)).DxfName; 这句对我相当有用。

发表于 2010-1-17 15:25 | 显示全部楼层

提示btr发生错误:

无法为源类型“Autodesk.AutoCAD.DatabaseServices.BlockTableRecord”找到查询模式的实现。找不到“Cast”。是否缺少对“System.Core.dll”的引用或未使用“System.Linq”的指令? G:\Prog\VS2008\C#\AzElectricity\AzElectricity\Form_AutoNumber.cs 37 41 AzElectricity

 楼主| 发表于 2010-1-17 19:00 | 显示全部楼层
using System.Linq;
发表于 2012-6-26 21:05 | 显示全部楼层
很好的基础贴...
您需要登录后才可以回帖 登录 | 注册

本版积分规则

小黑屋|手机版|CAD论坛|CAD教程|CAD下载|联系我们|关于明经|明经通道 ( 粤ICP备05003914号 )  
©2000-2023 明经通道 版权所有 本站代码,在未取得本站及作者授权的情况下,不得用于商业用途

GMT+8, 2024-4-24 21:05 , Processed in 1.416511 second(s), 23 queries , Gzip On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

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