我正在尝试编写一个代码来匹配从不同循环中获取的两个句柄(字符串)。一个是从外环获得的,另一个是从嵌套环获得的,因为这两个是相关的。我正在比较并将从第二个循环获得的结果存储到变体中。但这个过程非常缓慢。因为最外面的循环循环超过800次,中间一次循环大约400次,最内循环每次运行外循环80次,依此类推。
'Cycling through groups, and getting handle for entities attached to the group
Dim acGroup As AcadGroup
Dim acGroupEnt As AcadEntity
Dim ghandle As String
Dim gehandle As String
Dim ehandle As String
'Dim group_entity_handle_array As Variant
Dim l As Integer
Dim selected_group_array As Variant
For Each acEnt In acSelSet
ehandle = acEnt.Handle
For Each acGroup In ThisDrawing.Groups
'Debug.Print ("Group Name: " & acGroup.Name)
ghandle = acGroup.Handle
For Each acGroupEnt In acGroup
'Debug.Print (" " & acGroupEnt.ObjectName & " ... " & acGroupEnt.Handle)
gehandle = acGroupEnt.Handle
If ehandle = gehandle Then
selected_group_array = ghandle
'Debug.Print "Group Handle: " & ghandle
End If
Next
Next
Next
有没有办法以最少的时间这样做!
答案 0 :(得分:1)
首先,您可以在找到论坛后立即退出循环(Goto
)。也许您也可以使用变量来保存组列表,以避免在ThisDrawing.Groups
中进行查找。也许你可以使用ObjectID
代替句柄。比较LongPtr
应该比字符串更快。
Public Sub Test()
Dim acSelSet As AcadSelectionSet
On Error Resume Next
Set acSelSet = ThisDrawing.SelectionSets.Add("SS1")
If Err <> 0 Then
Set acSelSet = ThisDrawing.SelectionSets("SS1")
End If
On Error GoTo 0
acSelSet.SelectOnScreen
Dim acGroup As AcadGroup
Dim acGroupEnt As AcadEntity
Dim gid As LongPtr
Dim geid As LongPtr
Dim eid As LongPtr
Dim group_entity_handle_array As Variant
Dim l As Integer
Dim selected_group_array As Variant
Dim groups As AcadGroups
Set groups = ThisDrawing.groups
For Each acEnt In acSelSet
eid = acEnt.ObjectID
For Each acGroup In groups
Debug.Print ("Group Name: " & acGroup.Name)
gid = acGroup.ObjectID
For Each acGroupEnt In acGroup
Debug.Print (" " & acGroupEnt.ObjectName & " ... " & acGroupEnt.Handle)
geid = acGroupEnt.ObjectID
If eid = geid Then
selected_group_array = gid
Debug.Print "Group ID: " & gid
GoTo selectedGroupArrayFound
End If
Next
Next
Next
selectedGroupArrayFound:
End Sub