如何遍历数据库的实体?
C#开发|冯美娟|2018-07-24 10:12
-
回答:12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
private void GetAllEntity()
{
try
{
MxDrawApplication app =
new
MxDrawApplication();
MxDrawUtility mxUtility =
new
MxDrawUtility();
// 得到当前图纸空间
MxDrawBlockTableRecord blkRec = app.WorkingDatabase().CurrentSpace();
// 创建一个用于遍历当前图纸空间的遍历器
MxDrawBlockTableRecordIterator iter = blkRec.NewIterator();
if
(iter ==
null
)
return
;
// 所有实体的id数组。
List<Int64> aryId =
new
List<Int64>();
int iLineNum = 0;
// 循环得到所有实体
for
(; !iter.Done(); iter.Step(
true
,
false
))
{
// 得到遍历器当前的实体
MxDrawEntity ent = iter.GetEntity();
if
(ent ==
null
)
continue
;
// 得到实体的id
aryId.Add(ent.ObjectID);
if
(ent is MxDrawLine)
{
// 当前实体是一个直线
MxDrawLine line = (MxDrawLine)ent;
iLineNum++;
}
else
if
(ent is MxDrawBlockReference)
{
// 当前实体是一个块引用
MxDrawBlockReference blkRef = (MxDrawBlockReference)ent;
for
(int j = 0; j < blkRef.AttributeCount; j++)
{
// 得到块引用中所有的属性
MxDrawAttribute attrib = blkRef.AttributeItem(j);
mxUtility.Prompt(
"\n Tag: "
+ attrib.Tag +
"Text:"
+ attrib.TextString);
}
}
else
if
(ent is MxDrawText)
{
MxDrawText text = (MxDrawText)ent;
// 是个文字实体,text.TextString是文字内容
//text.TextString;
}
else
if
(ent is MxDrawMText)
{
MxDrawMText text = (MxDrawMText)ent;
// 是个多行文字实体,text.Contents是文字内容
//text.Contents
}
// else if (ent is 其它类型)
//{
// ... ....
//}
}
String sT;
sT =
"发现"
+ aryId.Count +
"个实体,其中有"
+ iLineNum +
"个直线"
;
MessageBox.Show(sT);
}
catch
(Exception ex)
{
MessageBox.Show(ex.Message.ToString());
}
}