How to automatically discern if 0-base or 1-base obj collection?

How to automatically discern if 0-base or 1-base obj collection?

josh.nieman
Advocate Advocate
327 Views
2 Replies
Message 1 of 3

How to automatically discern if 0-base or 1-base obj collection?

josh.nieman
Advocate
Advocate

Apologies if this is a duplicative question, but I found it difficult to search for, and get good results, with inputs I could think of.

I feel like Inventor's object collections are not always consistent on whether they're 0-base or 1-base collections (meaning the first item is index-1 or index-0) 

Is there an underlying 'rule' or 'guideline' being followed that I'm ignorant of?

Is there a way, when the collection has no indexes on the collection itself to query, I can discern what the item indexes are, without just attempting an item(0) on it and seeing if it errors?

What I normally do is: Debug.Print(objcol.item(0).Name) '//or whatever string property or .toString//

Just be nice if this was something I could just 'know' without doing that.

Example:
https://help.autodesk.com/view/INVNTOR/2022/ENU/?guid=ThemeManager
Methods: GetComponentThemeColor
Properties: ActiveTheme, Application, Themes, Type, Accessed From

The linked 'sample' gives me a clue, because it uses an "i=0" followed by a For Loop, so I kind of "infer" it is 1-base.  But not all examples are even that explicit (or have an example)

0 Likes
Accepted solutions (1)
328 Views
2 Replies
Replies (2)
Message 2 of 3

JelteDeJong
Mentor
Mentor
Accepted solution

As far as I know, all Inventor APi use 1-based object collection. But if you cast it to or use a .net collection it will be converted to a 0-based collection. Have a look at this example:

Dim doc As PartDocument = ThisApplication.ActiveDocument

' this collection is 1-based
Dim features = doc.ComponentDefinition.Features

' this collection is 0-based
Dim featuresList As IEnumerable(Of PartFeature) = features.Cast(Of PartFeature).ToList()



' get 3e item from 1-based collection
Dim f3 = features.Item(3)

' get 3e item from 0-based collection
Dim fl3 = featuresList(2)

Jelte de Jong
Did you find this post helpful? Feel free to Like this post.
Did your question get successfully answered? Then click on the ACCEPT SOLUTION button.

EESignature


Blog: hjalte.nl - github.com

0 Likes
Message 3 of 3

josh.nieman
Advocate
Advocate

@JelteDeJong - thanks for that.  You may be right - Inventor collections may all be 1-based.  I work in a few different environments and maybe was confused.  I had thought there was some inconsistency, depending on what Inventor object it was giving me.  Maybe there were Inventor functions returning generic system collections (even if ofObject of an Inventor type) and that was it.  However, yes, it seems any Inventor enumerated collection or ObjectCollection type seems to be 1-base... you may be correct that it's all Inventor types.

If that's the case, maybe I should just internalize that expectation and only worry about the times it errors on me 😄

I suppose I was hoping there was an "easy to reference" answer someone had that I just didn't know, like some value or intellisense method I was not aware of.  

Thanks

0 Likes