使用Linq在块表记录中获取同一类型的实体
下面的例子在当前空间中遍历,并改变直线和圆的颜色,注意Cad版本2009及以上相关的讨论帖
http://www.theswamp.org/index.php?topic=31489.0
public static void Test8()
{
Document doc = Application.DocumentManager.MdiActiveDocument;
Database db = doc.Database;
using (Transaction tr = db.TransactionManager.StartTransaction())
{
BlockTableRecord btr = (BlockTableRecord)tr.GetObject(db.CurrentSpaceId, OpenMode.ForRead);
var dict =
from ObjectId id in btr
group id by id.ObjectClass.DxfName;
foreach (var ids in dict)
{
switch (ids.Key)
{
case "LINE":
foreach (var id in ids)
{
Entity ent = id.GetObject(OpenMode.ForWrite) as Entity;
ent.ColorIndex = 1;
}
break;
case "CIRCLE":
foreach (var id in ids)
{
Entity ent = id.GetObject(OpenMode.ForWrite) as Entity;
ent.ColorIndex = 3;
}
break;
}
}
tr.Commit();
}
}
本帖最后由 作者 于 2010-1-13 23:53:42 编辑
直接写成函数调用,:)
public static IEnumerable<ObjectId> GetObjectIds<T>(this BlockTableRecord btr) where T : Entity
{
string dxfName = RXClass.GetClass(typeof(T)).DxfName;
return
from ObjectId id in btr
where id.ObjectClass.DxfName == dxfName
select id;
}
public static IEnumerable<IGrouping<string, ObjectId>> GetObjectIds(this BlockTableRecord btr)
{
return
from ObjectId id in btr
group id by id.ObjectClass.DxfName;
}
收到。顶起来 <p>RXClass.GetClass(typeof(T)).DxfName; 这句对我相当有用。</p><p></p> <p>提示btr发生错误:</p><p>无法为源类型“Autodesk.AutoCAD.DatabaseServices.BlockTableRecord”找到查询模式的实现。找不到“Cast”。是否缺少对“System.Core.dll”的引用或未使用“System.Linq”的指令? G:\Prog\VS2008\C#\AzElectricity\AzElectricity\Form_AutoNumber.cs 37 41 AzElectricity<br/></p> using System.Linq;<br/> 很好的基础贴...
页:
[1]