zdm860114 发表于 2009-5-3 17:58:00

[以解决]CAD二次开发中遍历的问题(感谢mccad)

本帖最后由 作者 于 2009-5-4 11:46:55 编辑 <br /><br /> <p>想破了头,查了很多资料,都想不出个办法来解决这个遍历问题。</p><p>问题就是在我建立了选择集后,用FOR EACH遍历获得实体进行实时操作。 这里有个TEXT数组接受数据。</p><p>而</p><p>For i = 0 To Num - 1<br/>&nbsp;&nbsp;&nbsp; For Each entry In ssetobj<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; entHandle = entry.handle<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; entry.Highlight (True)<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; MsgBox "The handle of this object is " &amp; entHandle, vbInformation, "Handle数值"<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Set entry = acadapp.ActiveDocument.HandleToObject(entHandle)<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; entry.TextOverride =&nbsp;Text1(i).Text<br/>&nbsp;&nbsp;&nbsp; Next<br/>&nbsp;&nbsp;&nbsp; Next i</p><p>这段代码肯定是错误的,我的目的是要通过遍历获得实体,实时进行操作。不知道如何修改,请高手指教,不甚感激!</p><p></p><p></p><p>全代码:</p><p>Dim ssetobj As AcadSelectionSet<br/>On Error Resume Next<br/>&nbsp;&nbsp;&nbsp; '建立选择集<br/>&nbsp;&nbsp;&nbsp; acadapp.SelectionSets("test1").Delete<br/>&nbsp;&nbsp;&nbsp; Set ssetobj = acadapp.ActiveDocument.SelectionSets.Add("test1")<br/>&nbsp;&nbsp;&nbsp; '建立过滤器<br/>AppActivate acadapp.Caption<br/>Dim fType(0) As Integer<br/>Dim fData(0) As Variant<br/>fType(0) = 8<br/>fData(0) = "bz"<br/>Dim FilterType As Variant<br/>Dim FilterData As Variant<br/>FilterType = fType<br/>FilterData = fData<br/>ssetobj.Select acSelectionSetAll, , , fType, fData<br/>AppActivate Form1.Caption</p><p>Dim i As Integer<br/>&nbsp;&nbsp;&nbsp; Dim Num As Integer<br/>&nbsp;&nbsp;&nbsp; Dim entHandle As String<br/>&nbsp;&nbsp;&nbsp; Dim entry As AcadEntity<br/>&nbsp;&nbsp;&nbsp; Num = ssetobj.Count</p><p>For i = 0 To Num - 1<br/>&nbsp;&nbsp;&nbsp; For Each entry In ssetobj<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; entHandle = entry.handle<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; entry.Highlight (True)<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; MsgBox "The handle of this object is " &amp; entHandle, vbInformation, "Handle数值"<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Set entry = acadapp.ActiveDocument.HandleToObject(entHandle)<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; entry.TextOverride = Text1(i).Text<br/>&nbsp;&nbsp;&nbsp; Next<br/>&nbsp;&nbsp;&nbsp; Next i</p><p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; entry.Highlight (False)</p><p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; entry.Update<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ssetobj.Delete</p><p>end sub</p><p></p>

zdm860114 发表于 2009-5-3 18:21:00

<p>高手来帮忙解答下,谢谢!</p><p>好几天都没有想出一个好的方法去解决这个问题</p>

sailorcwx 发表于 2009-5-3 18:26:00

<p>不明白你既然已经用for each遍历了,还要For i = 0 To Num - 1循环干嘛</p>

qqfish128 发表于 2009-5-3 21:31:00

For i = 0 To Num - 1<br/><br/>MS如果for 后面的0 to N 如果这个N是一个变量的话会出错(初步测试过这个错误)。<br/>不知道LZ和其它高手有没有解决这个问题;<br/><br/>对于CAD VBA里面对像遍举真的很麻烦,也一直希望能有高手给个教程与在枚举过程当中 对    对像操作的示例。<br/><br/>这个思路,对于新手来说真不好弄。论坛里有一两个示例,但还是不能满足要求。<br/><br/><br/><br/>

mccad 发表于 2009-5-3 22:08:00

<p>你用For i = 0 To Num - 1为的是取得Text(i)的值,但你却用了两重循环。</p><p>如果一个图元需要对应对话框中一个Text的值,那只需要用里面那层循环,即For Each那一层,然后在循环中使用i=i+1来让i值递增。</p>

zdm860114 发表于 2009-5-4 10:30:00

本帖最后由 作者 于 2009-5-4 11:11:39 编辑 <br /><br /> <p>恩,你的这个思路我也试过,可是就是弄不出来,我的目的是要实现,在遍历获得一个对象的同时将TEXT1(i )里面的值赋给它。也就是遍历一个实体用TEXT赋值一个,一个TEXT对应一个实体。由于遍历获得的Handle值有多个,</p><p>恳请大大指教!</p>

zdm860114 发表于 2009-5-4 11:44:00

<p>已经解决</p><p>在<strong><font color="#ff0000">mccad </font><font color="#000000">大大的指导下,获得了突破性的进展。在此感谢他的不卷教诲!谢谢!</font></strong></p><p><strong>还有谢谢每个热心关注的人!</strong></p><p><strong></strong></p><p>Dim ssetobj As AcadSelectionSet<br/>On Error Resume Next<br/>&nbsp;&nbsp;&nbsp; '建立选择集<br/>&nbsp;&nbsp;&nbsp; acadapp.SelectionSets("test1").Delete<br/>&nbsp;&nbsp;&nbsp; Set ssetobj = acadapp.ActiveDocument.SelectionSets.Add("test1")<br/>&nbsp;&nbsp;&nbsp; '建立过滤器<br/>AppActivate acadapp.Caption<br/>Dim fType(0) As Integer<br/>Dim fData(0) As Variant<br/>fType(0) = 8<br/>fData(0) = "bz"<br/>Dim FilterType As Variant<br/>Dim FilterData As Variant<br/>FilterType = fType<br/>FilterData = fData<br/>ssetobj.Select acSelectionSetAll, , , fType, fData</p><p>AppActivate Form1.Caption</p><p>'获得HANDLE<br/>&nbsp;&nbsp;&nbsp; Dim i As Integer<br/>&nbsp;&nbsp;&nbsp; Dim Num As Integer<br/>&nbsp;&nbsp;&nbsp; Dim entHandle() As String<br/>&nbsp;&nbsp;&nbsp; Dim entry As AcadEntity<br/>&nbsp;&nbsp;&nbsp; Num = ssetobj.Count - 1<br/>&nbsp;&nbsp;&nbsp; ReDim entHandle(Num)<br/>&nbsp;&nbsp;&nbsp; <font color="#ff0000">i = 0<br/></font>&nbsp;&nbsp;&nbsp; For Each entry In ssetobj<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; entHandle(i) = entry.Handle<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; entry.Highlight (True)<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; MsgBox "the number is:" &amp; i &amp; ",and the handle of this object is " &amp; entHandle(i), vbInformation, "Handle数值"<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Set entry = acadapp.ActiveDocument.HandleToObject(entHandle(i))<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; entry.TextOverride = Text1(i).Text<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <font color="#ff0000">i = i + 1<br/></font>&nbsp;&nbsp;&nbsp; Next<br/>&nbsp;&nbsp;&nbsp; entry.Highlight (False)<br/>&nbsp;&nbsp;&nbsp; entry.Update<br/>&nbsp;&nbsp;&nbsp; ssetobj.Delete<br/>End Sub</p><p>用了数组的办法,再用<strong><font color="#ff0000">mccad</font><font color="#000000">的办法解决了!</font></strong></p>

mccad 发表于 2009-5-4 12:24:00

<p>问题是解决了,但感觉你的程序太复杂。</p><p>entry.Handle是获取句柄,而HandleToObject(entHandle(i))又按照句柄转成图元,就不知是为什么,直接使用entry不行吗?</p><p>把句柄值做成数组也没有必要,因为此值只是用于循环过程中显示一下,数组在后续并不需要再次使用。</p><p>中间那段可试着这样改(不要的代码我都用引号注释掉了,还有图元的更新应该放在循环中去,不然就只更新最后一个图元了):<br/>(没有调试过,自己试试吧)</p><p>'获得HANDLE<br/>&nbsp;&nbsp;&nbsp; Dim i As Integer<br/>&nbsp;&nbsp;&nbsp; <font color="#ff00cc">'Dim Num As Integer<br/>&nbsp;&nbsp;&nbsp; 'Dim entHandle() As String<br/>&nbsp;&nbsp;&nbsp; </font><font color="#000000">Dim entry As AcadEntity</font><br/><font color="#ff00cc">&nbsp;&nbsp;&nbsp; 'Num = ssetobj.Count - 1<br/>&nbsp;&nbsp;&nbsp; 'ReDim entHandle(Num)<br/></font>&nbsp;&nbsp;&nbsp; <font color="#ff0000">i = 0<br/></font>&nbsp;&nbsp;&nbsp; For Each entry In ssetobj<br/><font color="#ff00cc">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 'entHandle(i) = entry.Handle<br/></font>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; entry.Highlight (True)<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; MsgBox "the number is:" &amp; i &amp; ",and the handle of this object is " &amp; <font color="#ff00cc">entry.Handle</font>, vbInformation, "Handle数值"<br/><font color="#ff00cc">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 'Set entry = acadapp.ActiveDocument.HandleToObject(entHandle(i))<br/></font>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; entry.TextOverride = Text1(i).Text<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <font color="#ff0000">i = i + 1<br/></font><font color="#ff00cc">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; entry.Highlight (False)<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; entry.Update<br/></font>&nbsp;&nbsp;&nbsp; Next<br/>&nbsp;&nbsp;&nbsp; ssetobj.Delete<br/></p>

zdm860114 发表于 2009-5-7 17:41:00

mccad发表于2009-5-4 12:24:00static/image/common/back.gif问题是解决了,但感觉你的程序太复杂。entry.Handle是获取句柄,而HandleToObject(entHandle(i))又按照句柄转成图元,就不知是为什么,直接使用entry不行吗?把句柄值做成数组也没有必要,因为

<p>十分感谢!!</p><p></p>
页: [1]
查看完整版本: [以解决]CAD二次开发中遍历的问题(感谢mccad)