1

我想做的是 Autodesk Inventor。我正在编写一个程序,它遍历草图中的一堆线。它收集连接线组并将它们放在一个集合中。然后它使这些集合的集合被处理。

我试图通过将这些行添加到临时集合中,然后将此临时集合添加到循环集合中来做到这一点,这样我就不必为每个循环生成未知数量的集合。但是,一旦我使用 Clear 方法重置临时集合,它就会删除我刚刚推送到循环集合中的信息。有没有办法使循环集合中的信息独立于临时集合中的信息?

如您所见,问题是我永远不知道将连接多少条线路,因此我永远不知道会有多少子集合。

这是我的代码。

Dim oLoopColl As New Collection
Dim oSubColl As ObjectCollection
Set oSubColl = ThisApplication.TransientObjects.CreateObjectCollection

For j = 1 To oSLColl.Count
    oSubColl.Add (oSLColl.Item(j))
    'Check for last item to see if it is part of the first
    If j = oSLColl.Count Then
        If oSLColl.Item(j).EndSketchPoint Is oSLColl.Item(1).StartSketchPoint Then
            MsgBox ("Last SL is part of first coll!")
            oLoopColl.Item(1).Add (oSLColl.Item(j))
            oSubColl.Clear
        Else
            Call oLoopColl.Add(oSubColl, CStr(j))
        End If
    Else
        If Not oSLColl.Item(j).EndSketchPoint Is oSLColl.Item(j + 1).StartSketchPoint Then
            Call oLoopColl.Add(oSubColl, CStr(j))
            oSubColl.Clear
        End If
    End If
Next
oSubColl.Clear
Set oSubColl = Nothing
4

1 回答 1

1

我想在评论中说的是以下内容。在示例中,您可以看到不必知道items.container


当 newitem应该添加到container创建时:

Set item = New Collection

然后将项目添加到这个新的item

item.Add "Some-New-Item"

最后添加对这个新item的引用container

container.Add item

现在保存对 驻留container内存位置的引用。item因此可以添加下一项,然后添加下一项,依此类推。


Option Explicit

Private Const ColItem As String = "Col_Item_"

Sub Demo()
    Dim container As VBA.Collection
    Dim item As VBA.Collection

    Set container = New Collection

    Set item = New Collection
    item.Add ColItem & 1
    item.Add ColItem & 11
    item.Add ColItem & 111
    container.Add item

    Set item = New Collection
    item.Add ColItem & 2
    item.Add ColItem & 22
    item.Add ColItem & 222
    container.Add item

    ' Clear is not part of VBA-Collection so Remove-all could simulate it
    ' When Clear would be called here then all the items will be removed
    ' and the container will reference an empty collection
    item.Remove 2

    Dim outer, inner
    For Each outer In container
        For Each inner In outer
            Debug.Print inner
        Next inner
    Next outer
End Sub

输出:

Col_Item_1
Col_Item_11
Col_Item_111
Col_Item_2
Col_Item_222

看看为什么不使用 As New

于 2016-11-23T08:38:16.310 回答