How to create dynamic variables to enter in a dictionary

How to create dynamic variables to enter in a dictionary

roy7BB24
Advocate Advocate
375 Views
2 Replies
Message 1 of 3

How to create dynamic variables to enter in a dictionary

roy7BB24
Advocate
Advocate

Hi All, 

Currently working on an ilogic script to classify frame members to number them, and avoid the "section size - length" type part number. 

The loose structure of the code is as follows. 

Everything is more or less working until I try to add values into the dictionary. I've confirmed I'm pulling the correct data from each part; however it seems like its not possible to dynamically create variables in VB. So, what's currently happening in the code is I only have the one variable in the dictionary, and it's just being overwritten with each new BOM Row (part), or the variable is just being changed everytime it moves on to the next part. In any case, right now I can only enter one line into my dictionary, anyone got any idea how to fix this?

I've attached the full code below, if you create an assembly with 2-3 frame parts and one pair of frames to be identical, you should be able to run the code as intended. 

 

Kind Regards

 

Roydon Mackay

 

Open doc, set up BOM all the usual

set assembly counter at 01

Part Numbering Subroutine

set part counter at 01

'For each Bom Row, determine if row is a part/ assembly/ frame assembly

If part assign part number  = P - "partcounter" ; part counter += partcounter + 1

If assembly assign part number = A - "Assembly counter"; Assembly counter += 1; run Part numbering subroutine on Child rows

If frame assembly assign part number = A-Frame-"Assembly counter"; Run FrameProcessor subroutine

Frame Processor subroutine;

Create dictionary "Frame dictionary" (framedata, partnumber)

Public Class framedata (create a class to store all variable's to determine if frame is unique)

For each row in bom

populate framedata class from part information;

For each frame data in framedictionary, check if it matches the new part (this is actually done as a function in a few steps but simplified here)

If part is unique, create a new entry into the dictionary and assign a new part number to the frame part

If part is matched, assign the part number of the matched part

 

 

 

 

 

 

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

Michael.Navara
Advisor
Advisor
Accepted solution

I don't test your code, but the reason of this behavior is incorrect usage of variable frame in method FrameProcessor

You declare and initialize the variable frame at the top of the method. In this case this variable is the same instance for each BOM row and you rewrite values in it.

Private Sub FrameProcessor(BOMRows As BOMRowsEnumerator, AssemblyPNo As String, Counter As String)
    'Interate through the contents of the Frame
    Dim frame As New Frame_Data
    '...
    Dim FrameDictionary As New Dictionary(Of Frame_Data, String)
    '...
    For i As Integer = 1 To BOMRows.Count
        '...
        FrameDictionary.Add(frame, Compdef.Document.PropertySets.Item("Design Tracking Properties").Item("Part Number").Value)
        '...
    Next
End Sub

 You need to initialize the variable inside the For loop and it will work much better.

Private Sub FrameProcessor(BOMRows As BOMRowsEnumerator, AssemblyPNo As String, Counter As String)
    'Interate through the contents of the Frame
    '...
    Dim FrameDictionary As New Dictionary(Of Frame_Data, String)
    '...
    For i As Integer = 1 To BOMRows.Count
        Dim frame As New Frame_Data
        '...
        FrameDictionary.Add(frame, Compdef.Document.PropertySets.Item("Design Tracking Properties").Item("Part Number").Value)
        '...
    Next
End Sub

 

0 Likes
Message 3 of 3

roy7BB24
Advocate
Advocate

@Michael.Navara Thanks for the help, can confirm this section of the script is working as intended

0 Likes