gpt4 book ai didi

vba - vb.net如何退出递归循环

转载 作者:行者123 更新时间:2023-12-05 06:07:40 25 4
gpt4 key购买 nike

我正在为 3D 程序 CATIA 编写代码,该代码通过递归循环遍历所有树。我想在找到特定产品后退出递归循环。但是我的代码一直在运行,即使他找到了。我大致写了meins。我在那里犯了什么错误?

Private Sub TestMain
.....
call TestMainChildren
.....
End Sub

Private Sub TestMainChildren
For Each item In product
.....
If itemName = "SearchName" then
MsgBox("Found!")
Exit Sub
End if
......
Call TestMainChildren
Next
End Sub

enter image description here

最佳答案

要使此递归,您需要将起始对象传递给函数,在本例中为 products。然后,当您查看子对象时,仅递归传递产品集合。

Private Sub TestMain    
Dim searchName As String
searchName = "SearchName"

' Start with the selected object
Dim doc As Document
Set doc = CATIA.ActiveDocument
Dim prod As Product
Set prod = doc.Product

Dim foundItem As Object
foundItem = TestMainChildren(doc.Selection.Item(1).Value, searchName)

MsgBox "Found: " & foundItem.Name
End Sub

Private Function TestMainChildren(ByRef catiaObject As Object, ByVal searchName As String) As Object
Dim item As Object
For Each item In catiaObject.Items
If item.Name = searchName Then
Set TestMainChildren = item
Exit For
End if

Dim catiaType As String
catiaType = TypeName(item)
If catiaType = "Product" Then
Set TestMainChildren = TestMainChildren(item, searchName)
End If
Next item
End Sub

警告:这是完全未经测试的汽化器。您将必须修改它以适应您的环境和要求。然后进行适当的测试和调试。

关于vba - vb.net如何退出递归循环,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65362629/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com