Array & Sorting

Array & Sorting

cadman777
Advisor Advisor
2,156 Views
6 Replies
Message 1 of 7

Array & Sorting

cadman777
Advisor
Advisor

Hello everyone,

 

I just spent about 8 hours combined btw yesterday and today trying to UNDERSTAND vb Arrays and Sorting. But I just can't wrap my mind around the meaning of things. Maybe someone in here can help me understand it?

 

What I'm trying to do is take some code I got out of the Inventor Help file and adapt it to my needs. I want to compile a list of one iProperty found in all the Parts in one Assembly file, and then sort it alpha-numerically. Then I want to run that list through a Sub that does other things to every file. This is what I have so far:

 

Option Explicit

Public Sub BOMQuery()
    ' Set a reference to the assembly document.
    ' This assumes an assembly document is active.
    Dim odoc As AssemblyDocument
    Set odoc = ThisApplication.ActiveDocument

    ' Set a reference to the BOM
    Dim oBOM As BOM
    Set oBOM = odoc.ComponentDefinition.BOM
    
    ' Make sure that the PartsOnly view is enabled.
    oBOM.PartsOnlyViewEnabled = True
    
    'Set a reference to the "Parts Only" BOMView
    Dim oBOMView As BOMView
    Set oBOMView = oBOM.BOMViews.Item("Parts Only")
        
    Debug.Print "Mark"
    Debug.Print "----"

    'Initialize the tab for ItemNumber
    Dim ItemTab As Long
    ItemTab = -3
    Call QueryBOMRowProperties(oBOMView.BOMRows, ItemTab)
End Sub

Private Sub QueryBOMRowProperties(oBOMRows As BOMRowsEnumerator, ItemTab As Long)
    ItemTab = ItemTab + 3
    ' Iterate through the contents of the BOM Rows.
    Dim i As Long
    For i = 1 To oBOMRows.count
        ' Get the current row.
        Dim oRow As BOMRow
        Set oRow = oBOMRows.Item(i)

        'Set a reference to the primary ComponentDefinition of the row
        Dim oCompDef As ComponentDefinition
        Set oCompDef = oRow.ComponentDefinitions.Item(1)

            'Get the file property that contains the "Stock Number"
            'The file property is obtained from the parent
            'document of the associated ComponentDefinition.
            Dim oSN_Value As Property
            Set oSN_Value = oCompDef.Document.PropertySets _
                .Item("Design Tracking Properties").Item("Stock Number")
            
        Debug.Print Tab(ItemTab); oSN_Value.Value
    Next
    ItemTab = ItemTab - 3
    Call SortMarkList
End Sub

'Below is my n'th attempt at sorting, but I have NO IDEA what I'm doing. It's embarrassing, really!
Private Sub SortMarkList()
    Dim oSN_Value As String
    OrderBy (oSN_Value)
End Sub

End Sub

 I abandoned iLogic for doing this kind of thing b/c it doesn't have any way to look at what's going on. So I'm tentatively doing all this in Inventor VB.

 

Thanx for the help!

... Chris
Win 7 Pro 64 bit + IV 2010 Suite
ASUS X79 Deluxe
Intel i7 3820 4.4 O/C
64 Gig ADATA RAM
Nvidia Quadro M5000 8 Gig
3d Connexion Space Navigator
0 Likes
2,157 Views
6 Replies
Replies (6)
Message 2 of 7

bradeneuropeArthur
Mentor
Mentor

Try this:

Public Sub BOMQuery()
    ' Set a reference to the assembly document.
    ' This assumes an assembly document is active.
    Dim odoc As AssemblyDocument
    Set odoc = ThisApplication.ActiveDocument

    ' Set a reference to the BOM
    Dim oBOM As BOM
    Set oBOM = odoc.ComponentDefinition.BOM
    
    ' Make sure that the PartsOnly view is enabled.
    oBOM.PartsOnlyViewEnabled = True
    
    'Set a reference to the "Parts Only" BOMView
    Dim oBOMView As BOMView
    Set oBOMView = oBOM.BOMViews.Item("Parts Only")
        
    Debug.Print "Mark"
    Debug.Print "----"

    'Initialize the tab for ItemNumber
    Dim ItemTab As Long
    ItemTab = -3
    Call QueryBOMRowProperties(oBOMView.BOMRows, ItemTab)
End Sub

Private Sub QueryBOMRowProperties(oBOMRows As BOMRowsEnumerator, ItemTab As Long)
    ItemTab = ItemTab + 3
    ' Iterate through the contents of the BOM Rows.
    Dim i As Long
    Dim x As Integer
    
    Dim a(100) As Variant
    For i = 1 To oBOMRows.Count
        ' Get the current row.
        Dim oRow As BOMRow
        Set oRow = oBOMRows.Item(i)

        'Set a reference to the primary ComponentDefinition of the row
        Dim oCompDef As ComponentDefinition
        Set oCompDef = oRow.ComponentDefinitions.Item(1)

            'Get the file property that contains the "Stock Number"
            'The file property is obtained from the parent
            'document of the associated ComponentDefinition.
            Dim oSN_Value As Property
            Set oSN_Value = oCompDef.Document.PropertySets _
                .Item("Design Tracking Properties").Item("Stock Number")
            a(i - 1) = oSN_Value.Value
            
        Debug.Print Tab(ItemTab); oSN_Value.Value
    Next
    ItemTab = ItemTab - 3
    Call Alphabetically_SortArray(a)
End Sub


Sub Alphabetically_SortArray(myArray() As Variant)

Dim x As Long, y As Long
Dim TempTxt1 As String
Dim TempTxt2 As String


'Alphabetize Sheet Names in Array List
  For x = LBound(myArray) To UBound(myArray)
    For y = x To UBound(myArray)
      If UCase(myArray(y)) < UCase(myArray(x)) Then
        TempTxt1 = myArray(x)
        TempTxt2 = myArray(y)
        myArray(x) = TempTxt2
        myArray(y) = TempTxt1
      End If
     Next y
  Next x
Dim k As Integer
k = 0

For k = 0 To UBound(myArray)
Debug.Print myArray(k)
Next
End Sub

Regards,

Arthur Knoors

Autodesk Affiliations & Links:
blue LinkedIn LogoSquare Youtube Logo Isolated on White Background


Autodesk Software:Inventor Professional 2025 | Vault Professional 2024 | Autocad Mechanical 2024
Programming Skills:Vba | Vb.net (Add ins Vault / Inventor, Applications) | I-logic
Programming Examples:
Drawing List!|
Toggle Drawing Sheet!|
Workplane Resize!|
Drawing View Locker!|
Multi Sheet to Mono Sheet!|
Drawing Weld Symbols!|
Drawing View Label Align!|
Open From Balloon!|
Model State Lock!
Posts and Ideas:
My Ideas|
Dimension Component!|
Partlist Export!|
Derive I-properties!|
Vault Prompts Via API!|
Vault Handbook/Manual!|
Drawing Toggle Sheets!|
Vault Defer Update!

! For administrative reasons, please mark a "Solution as solved" when the issue is solved !


 


EESignature

0 Likes
Message 3 of 7

cadman777
Advisor
Advisor

Hi,

Thanx for doing that for me.

I ran it and it didn't sort the numbers.

Do you have anything that explains to a dumdum what every part of the Array and Array.Sort means and does?
That's what I need to get a grip on this.

Like when I used to teach AutoCAD to engineers in the 90's, I had to take them in baby steps.

Now I'm that 'old guy' who needs baby steps to get there.

The thing that grates on me is Autodesk makes it seem like this stuff is easy and anybody can do it.

What nonsense!

... Chris
Win 7 Pro 64 bit + IV 2010 Suite
ASUS X79 Deluxe
Intel i7 3820 4.4 O/C
64 Gig ADATA RAM
Nvidia Quadro M5000 8 Gig
3d Connexion Space Navigator
0 Likes
Message 4 of 7

cadman777
Advisor
Advisor

After all the hours of reading on Arrays, this is what I took away from it:

1. An array is essentially a Variable that can hold more than one value.

2. There are 2 kinds of arrays: a) static, and b) dynamic

   a) a fixed set of values that don't change during program execution

   b) a set of values that can change during program execution.

3. There are 3 dimensions to arrays: a) Single, b) 2-dimensional, 3) milti-dimensional.

   a) one list of items

   b) a list of 2 columns

   c) example: a 3 dimensional data set

I want to create a dynamic array for a set of data that I don't know the quantity of when the program starts running. I found conflicting and confusing statements defining this, and also exemplifying it. I couldn't find any good sources of info on this online.

 

Anyone in here know a SIMPLE yet comprehensive TUTORIAL that I can get online for this (for free), so I can understand this an implement it?

 

... Chris
Win 7 Pro 64 bit + IV 2010 Suite
ASUS X79 Deluxe
Intel i7 3820 4.4 O/C
64 Gig ADATA RAM
Nvidia Quadro M5000 8 Gig
3d Connexion Space Navigator
0 Likes
Message 5 of 7

cadman777
Advisor
Advisor

I tried it again today and got it to work somewhat, but not correctly.

What I did was change the Array index value to 1000, due to the size of the 3dmodel.

So I upped your

Dim a(100) As Variant

to '1000'. And the macro ran all the way though the list and then sorted it. Before it ran and got stuck (threw a 'Debug' error), which I had no idea how to figure out.

 

The problem w/the macro is, it started at number 137 and either missed or cut off all numbers btw 001 and 136.

I'm trying to run it w/some of the code commented out to see if it even can find all the parts.

 

Also, I tried to understand what you did, but can't, b/c I don't understand how the Array works.

I see you modified the code to have a 2 dimensional array instead of a single list.

Why did you do that?
Can't you get and then sort a single list?

 

Also, how can I make the Array sort an unknown quantity of parts without having to jack the number up to 10,000?

Is this how Array works?

I read on one web site that it can have empty parens and it'll update the size automatically if it's a Variant type, but couldn't understand how to do that. Learning this stuff is like learning Arabic writing!

... Chris
Win 7 Pro 64 bit + IV 2010 Suite
ASUS X79 Deluxe
Intel i7 3820 4.4 O/C
64 Gig ADATA RAM
Nvidia Quadro M5000 8 Gig
3d Connexion Space Navigator
0 Likes
Message 6 of 7

pball
Mentor
Mentor

Looks like you are using Inventor VBA. If you ever start using iLogic or an addin where you can use vb.net I'd suggest using Lists, they are much easier to use than arrays. For VBA you could try using the Scripting Dictionary feature. I'm not super familiar with it, but I've made a few simple scripts for others and have my example code below. It doesn't have a built in sort function but someone on the internet generously shared one which I have below. I don't recall exactly how the sort function works since I haven't used it in a while but some googling could help if you go this route.

 

VBA example of Scripting Dictionary

    Dim Colors As Object
    Set Colors = CreateObject("Scripting.Dictionary")

    Colors(oFace.Appearance.DisplayName) = Colors(oFace.Appearance.DisplayName) + oFace.Evaluator.Area * oRow.ItemQuantity * SubQty

    For Each item In Colors
        Debug.Print "Paint: " & item & " Area: "; Colors(item) & " cm^2"
    Next

-------------------------------------------------------------------

    Set dict = CreateObject("Scripting.Dictionary")
    dict.CompareMode = vbTextCompare          'non-case sensitive compare model
    
    'populate the dictionary
    dict.Add Key:="Red", Item:="Balloon"
    dict.Add Key:="Green", Item:="Balloon"
    dict.Add Key:="Blue", Item:="Balloon"

    'iterate through the keys
    For Each k In dict.Keys
        Debug.Print k & " - " & dict.Item(k)
    Next k

    'locate the Item for Green
    Debug.Print dict.Item("Green")
    
    'remove key/item pairs from the dictionary
    dict.Remove "blue"      'remove individual key/item pair by key
    dict.RemoveAll          'remove all remaining key/item pairs

-------------------------------------------------------------------

Function SortDictionary(objDict, intSort)
    ' declare our variables
    Dim strDict()
    Dim objKey
    Dim strKey, strItem
    Dim X, Y, Z

    ' get the dictionary count
    Z = objDict.Count

    ' we need more than one item to warrant sorting
    If Z > 1 Then
        ' create an array to store dictionary information
        ReDim strDict(Z, 2)
        X = 0
        ' populate the string array
        For Each objKey In objDict
            strDict(X, dictKey) = CStr(objKey)
            strDict(X, dictItem) = CStr(objDict(objKey))
            X = X + 1
        Next

        ' perform a a shell sort of the string array
        For X = 0 To (Z - 2)
            For Y = X To (Z - 1)
                If StrComp(strDict(X, intSort), strDict(Y, intSort), vbTextCompare) > 0 Then
                    strKey = strDict(X, dictKey)
                    strItem = strDict(X, dictItem)
                    strDict(X, dictKey) = strDict(Y, dictKey)
                    strDict(X, dictItem) = strDict(Y, dictItem)
                    strDict(Y, dictKey) = strKey
                    strDict(Y, dictItem) = strItem
                End If
            Next
        Next

        ' erase the contents of the dictionary object
        objDict.RemoveAll

        ' repopulate the dictionary with the sorted information
        For X = 0 To (Z - 1)
        objDict.Add strDict(X, dictKey), strDict(X, dictItem)
        Next
    End If
End Function

 

iLogic / VB.net example of List

Dim test As New List(Of String)
test.Add("stuff")
test.Add("junk")
test.Sort
For Each item In test
	MsgBox(item)
Next

 

 

Check out my style edits for the Autodesk forums
pball's Autodesk Forum Style
0 Likes
Message 7 of 7

cadman777
Advisor
Advisor

Thanx for this input.

I'll try working with it and see how it works for me.

I've been trying to work in VB.Net b/c it has an amazing UI.

But it's so doggone slow in connecting to Inventor that I keep returning to Inventor VBA.

But a lot of my rules are in a mix of iLogic + VBA 'by others', so that's what I'm working with until I can learn how to make macros in VB.Net.

Your examples look simple enough! 😂

... Chris
Win 7 Pro 64 bit + IV 2010 Suite
ASUS X79 Deluxe
Intel i7 3820 4.4 O/C
64 Gig ADATA RAM
Nvidia Quadro M5000 8 Gig
3d Connexion Space Navigator
0 Likes