3个循环中实体句柄的迭代和比较

Iteration and comparison of entity handles in 3 loops

我正在尝试编写一个代码来匹配从不同循环中获得的两个句柄(字符串)。一个是从外层循环得到的,一个是从嵌套循环中得到的,因为这两个是关联的。我正在比较从第二个循环获得的结果并将其存储到一个变体中。但是这个过程非常缓慢。因为最外面的循环循环超过 800 次,中间循环大约 400 次,最里面的 运行s 循环 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

有没有什么方法可以用最少的时间做到这一点!

首先,您可以在找到您的群组后立即退出循环 (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