Tuesday, December 20, 2011

Use iLogic Code to Emulate a Table Driven Part or Assembly

 
Here is an interesting method for creating a "lookup table" using iLogic code without having to worry about managing or embedding external spreadsheets or dealing with iParts or iAssemblies.
This is a snapshot of the table we want to emulate. By selecting a number from the "Size" column a Minor Diameter and Major Diameter are determined and a Part Number is changed.





Most people would attack this with a Case Statement that would look something like this:

Select Case Size
Case 14 
MinorDia = 0.065 
MajorDia = 0.072
iProperties.Value("Project", "Part Number") = "11-1111"
Case 16
MinorDia = 0.051
MajorDia = 0.056
iProperties.Value("Project", "Part Number") = "11-1112"
Case 18…….
While this is completely viable code, it could get really long and confusing making modifications difficult, so it may be easier to emulate table functionality with in a little more efficient code. Try this little iLogic exercise and let me know what you think!
Start a new IPT or IAM file and create three numeric parameters. Make sure the "Size" parameter is a multivalue list that contains all of the size values you wish to utilize and is set to "unitless". Don't worry about the values of MajorDia and MinorDia, we'll set those with the iLogic code.


Create a new iLogic Rule called "Table" and add the following code:
Sub Main
    MultiValue.SetList("Size", 14, 16, 18)'''add more sizes to this sequence as needed
    InitializeTable()
    Dim row As TableValues
    Dim found As Boolean = table.TryGetValue(Size, row)
    If (found) Then
        MinorDia = row.MinorDiam
        MajorDia = row.MajorDiam
        iProperties.Value("Project", "Part Number") = row.PartNumber
    End If
End Sub
Public table As New Dictionary(Of Double, TableValues)
Sub InitializeTable()
    '''table starts below
    table(14) = New TableValues(0.065, 0.072, "11-1111")
    table(16) = New TableValues(0.051, 0.056, "11-1112")
    table(18) = New TableValues(0.040, 0.044, "11-1113")
    '''finish the table by adding lines above
End Sub
Class TableValues
    Public MinorDiam as Double
    Public MajorDiam as Double
    Public PartNumber As String
    Public Sub New( MinorDiam as Double, MajorDiam as Double, PartNumber As String)
        Me.MinorDiam = MinorDiam
        Me.MajorDiam = MajorDiam
        Me.PartNumber = PartNumber
    End Sub
End Class 
That should do it. You have the makings of a table-driven part or assembly without attaching or referencing a spreadsheet!
Finish the table by adding additional lines for the sizes you need between my comments. Now selecting a new size from your parameter list will cause the diameter parameters to update as well as update the part number. Special thanks to thanks to Mike Deck at Autodesk for the idea!

What do you think? Would automation like this work for you?

Download a sample file HERE and have fun!

3 comments:

  1. How could I get it to read the values from the largest "Size" in the table?

    ReplyDelete
  2. Hi Chase, could you clarify what it is you're looking for? I'm afraid I don't follow your question.

    ReplyDelete
  3. Useless. Doesn't work at all.

    ReplyDelete