1

What I am trying to do is for Autodesk Inventor. I'm writing a program that goes iterates through a bunch of lines in a sketch. It gathers the groups of connected lines and puts them in a collection. Then it makes a collection of these collections to be processed.

I'm trying to do this by adding the lines to a temporary collection, then adding this temporary collection to the collection of loops, so that I don't have to generate an unknown amount of collections for each loop. However, as soon as I reset the temporary collection using the Clear method, it erases the information that I had just pushed into the collection of loops. Is there any way to make the information in the collections of loops independent of what is in the temporary collection?

As you can see, the issue is that I never know how many lines will be connected, so thus I never know how many sub collections there will be.

Here is the code I have.

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
  • The code `oLoopColl.Add(oSubColl, CStr(j))` adds a `reference` to sub-collection to loop-collection. This reference just points to the same sub-collection and so when the sub-collection is cleared, the loop-collection points to this collection which is now empty. You can't add the sub-collection `by-value`. You could create new collection each time, copy the elements of sub-collection to this new collection and add this new collection to loop-collection. When you don't clear this new collection then the loop collection will persist it. – Daniel Dušek Nov 22 '16 at 16:12
  • But how do I deal with this with the variable number of sub-collections in the loop? As far as I know, I can't exactly define a variable based on a variable; ie oLoop & i = oLoop1, oLoop2 for i = 1 to 2 – MechMachineMan Nov 22 '16 at 16:44
  • Maybe I just need to rethink my approach and only store the oLoops as an ObjectCollection right before I make the call that requires them as an ObjectCollection... ie; store them in an array and convert the array to an ObjectCollection. – MechMachineMan Nov 22 '16 at 16:49

1 Answers1

1

What I was trying to say in the comment was the following. In the example you can see that it is not necessary to know the number of items in the container.


When new item should be added to the container create one:

Set item = New Collection

Then add items to this new item

item.Add "Some-New-Item"

And finally add reference to this new item to the container

container.Add item

The container saves now the reference to the memory place where the item resides. And so next item can be added and then next one and so on.


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

Output:

Col_Item_1
Col_Item_11
Col_Item_111
Col_Item_2
Col_Item_222

See why not use As New.

Community
  • 1
  • 1
Daniel Dušek
  • 13,683
  • 5
  • 36
  • 51