遍历BlockTable?
本帖最后由 cdinten 于 2011-8-8 10:21 编辑请问如何遍历块表下的所有实体?我的项目中实体不多,就20个,烦请给段代码?我记得以前有人给过代码的,现在没找到了 <CommandMethod("T6")> Public Sub T6()
Dim db As Database = HostApplicationServices.WorkingDatabase
Dim ed As Editor = Application.DocumentManager.MdiActiveDocument.Editor
Using trans As Transaction = db.TransactionManager.StartTransaction
Dim bt As BlockTable = trans.GetObject(db.BlockTableId, OpenMode.ForRead)
Dim btr As BlockTableRecord = trans.GetObject(bt.Item(BlockTableRecord.ModelSpace), OpenMode.ForWrite)
For Each Id As ObjectId In btr
Dim obj As DBObject = trans.GetObject(Id, OpenMode.ForWrite)
If TypeOf (obj) Is Entity Then
ed.WriteMessage(vbCrLf & obj.GetType.ToString)
End If
Next
End Using
End Sub 回复 single-yu 的帖子
谢谢,虽然没怎么使用过VB.NET,不过有点VB的基础,然后你的程序写的清晰明了,所以看懂毫无问题,我试试。先谢谢了 找个网上的VB.NET和C#转换就行了,不过稍微有点基础的不难看懂,VB的代码就像用英语写作文一样好懂! 回复 single-yu 的帖子
还好吧,其实你试试C#就知道C#也很简单。就像口头交流一样,虽然没有VB随意,但是语法严谨而又灵活,而且有个好处是C#转VB没问题因为没有可变转换,即使是泛型也可以转化为VB中的类型,但是似乎VB中的Object类型转到C#中不能自动装箱,造成类型不匹配 来个C#代码:
public static void ListEntities()
{
Document acDoc = Application.DocumentManager.MdiActiveDocument;
Database acCurDb = acDoc.Database;
using (Transaction acTrans = acCurDb.TransactionManager.StartTransaction())
{
BlockTable acBlkTbl;
acBlkTbl = acTrans.GetObject(acCurDb.BlockTableId, OpenMode.ForRead) as BlockTable;
BlockTableRecord acBlkTblRec;
acBlkTblRec = acTrans.GetObject(acBlkTbl, OpenMode.ForRead) as BlockTableRecord;
int nCnt = 0;
acDoc.Editor.WriteMessage("\nModel space objects: ");
foreach (ObjectId acObjId in acBlkTblRec)
{
acDoc.Editor.WriteMessage("\n" + acObjId.ObjectClass.DxfName + acObjId.Handle.ToString());
nCnt = nCnt + 1;
}
if (nCnt == 0)
{
acDoc.Editor.WriteMessage("\nNo objects found.");
}
else
{
acDoc.Editor.WriteMessage("\nTotal {0} objects.", nCnt);
}
}
}
回复 sunchengbo2007 的帖子
谢谢~~~我已经改写过啦~~ 学过c语言,看c#久没有多大难度了
页:
[1]