gjliang 发表于 2005-11-14 11:06:00

[VBA]请教遍历图像对象

<PRE class=Code>Sub Example_Count()
    ' Use count to retrieve the number of objects in a collection
    ' You might use this value in a loop structure to iterate through the collection
   
    MsgBox "There are " &amp; ThisDrawing.Layers.count &amp; " layer(s) in the drawing."
    MsgBox "There are " &amp; ThisDrawing.ModelSpace.count &amp; " object(s) in ModelSpace."

    Dim objCount As Integer
<FONT color=#de1062>    Dim I As Integer
    objCount = ThisDrawing.ModelSpace.count</FONT>
   
    Dim mspaceObj As AcadObject
    For I = 0 To objCount - 1
   Set mspaceObj = ThisDrawing.ModelSpace.Item(I)
    MsgBox "The objects in ModelSpace include: " &amp; mspaceObj.ObjectName, vbInformation, "Count 示例"
    Next
   
End Sub</PRE><PRE class=Code>请问当modelspace中对象数目超过<FONT color=#de1062>Integer上限时该如何处理。</FONT></PRE>

sieben 发表于 2005-11-14 17:01:00

<P>&nbsp; dim i as double</P>
<P>For I = 0 To <FONT color=#de1062>ThisDrawing.ModelSpace.count </FONT>&nbsp;- 1 step 1<BR>&nbsp;&nbsp;&nbsp;&nbsp; Set mspaceObj = ThisDrawing.ModelSpace.Item(fix(I)) '取整<BR>&nbsp;&nbsp;&nbsp; MsgBox "The objects in ModelSpace include: " &amp; mspaceObj.ObjectName, vbInformation, "Count 示例"<BR>&nbsp;&nbsp;&nbsp; Next</P>
<P>1,你用什么版本的AutoCAD 和VB?</P>
<P>2,你可以使用长整数</P>
<P>3,For each Obj in ModelSpace 这样不用到整数,ModelSpace 是一个集合<BR></P>

gjliang 发表于 2005-11-14 22:48:00

<P>以前论坛里的一个例子:</P>
<P>Private Sub AcadDocument_BeginCommand(ByVal CommandName As String)<BR>'Dim entcount As Double<BR>entcount = ThisDrawing.ModelSpace.Count<BR>End Sub<BR>Private Sub AcadDocument_EndCommand(ByVal CommandName As String)<BR>&nbsp;&nbsp;&nbsp; If Left(UCase(CommandName), 3) = "DIM" Then<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 'Count = ThisDrawing.ModelSpace.Count<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Dim NewEnt As AcadEntity<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Dim i As Long<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Dim NewLayerName As String<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; '以下定义了要将标注对象移动到的图层名称<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; NewLayerName = "标注"<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Dim newlayer As AcadLayer<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Set newlayer = ThisDrawing.Layers.Add(NewLayerName)<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; newlayer.color = acGreen<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; For i = entcount To ThisDrawing.ModelSpace.Count - 1<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Set NewEnt = ThisDrawing.ModelSpace.Item(i)<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 'If TypeOf NewEnt Is AcadDimension Then<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; NewEnt.Layer = "标注"<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 'End If<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Next</P>
<P>end sub</P>
<P>当对象超过integer上限时就会出错,如果用For each Obj in ModelSpace 方法,每次遍历选择集就会很慢。</P>

兰州人 发表于 2006-9-8 15:45:00

<P>用For each Obj in ModelSpace 方法遍历选择集,与用dim as Inter 有何区别?</P>
<P>一张图纸的实体数是一定的,分实体遍历也是一样的.只不过是过滤了line,circle等实体.</P>
<P>&nbsp;</P>

palagon 发表于 2006-12-25 14:29:00

<p>稍微练习了一下,有点收获</p><p>不过还不知道怎么筛选</p>
页: [1]
查看完整版本: [VBA]请教遍历图像对象