Ilogic read csv file in as variables

Ilogic read csv file in as variables

Anonymous
Not applicable
2,428 Views
6 Replies
Message 1 of 7

Ilogic read csv file in as variables

Anonymous
Not applicable

I am reading in a 2 line csv file. The first line contains strings representing variables and the second line, the values.

 

my question is, how do you read these in creating iLogic variables of each pair?

 

ie csv contains:

 

length. Width

1000.     500

 

 

i want ilogic variables of length and width created with the relevant values.

 

 

many thanks in advance

 

Dave

0 Likes
Accepted solutions (2)
2,429 Views
6 Replies
Replies (6)
Message 2 of 7

JelteDeJong
Mentor
Mentor
Accepted solution

you could try the following:

Dim splitChar As String = ","
Dim keyLine As String
Dim valLine As String

Using reader As New IO.StreamReader("c:\temp\test.csv", Text.Encoding.Default)
    keyLine = reader.ReadLine()
    valLine = reader.ReadLine()
End Using

Dim keys As String() = keyLine.Split(splitChar)
Dim vals As String() = valLine.Split(splitChar)

Dim csvData As Dictionary(Of String, String) = New Dictionary(Of String, String)()
For i = 0 To keys.Count - 1
    csvData.Add(keys(i), vals(i))
Next

For Each item As KeyValuePair(Of String, String) In csvData
    MsgBox(item.Key & ": " & item.Value)
Next

This will fail if the number of key and values are not the same. If you have control over how the csv file is made i would advice you to setup different. its better tou put a key/value pair on each line. like this:

length,1000

Width,500

Its more easy to read and the following iLogic rule will not fail enterly when a line is not correct.

Dim splitChar As String = ","
        Dim csvData As Dictionary(Of String, String) = New Dictionary(Of String, String)()

        Using reader As New IO.StreamReader("c:\temp\test.csv", Text.Encoding.Default)
            Dim line As String = reader.ReadLine

            Do While (Not line Is Nothing)
                Try
                    Dim splittedLine As String() = line.Split(splitChar)
                    csvData.Add(splittedLine(0), splittedLine(1))
                Catch ex As Exception
                    MsgBox("Could not add data from line: " & line)
                End Try
                line = reader.ReadLine
            Loop
        End Using

        For Each item As KeyValuePair(Of String, String) In csvData
            MsgBox(item.Key & ": " & item.Value)
        Next

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

Message 3 of 7

stijnkimpe
Observer
Observer

@JelteDeJong 

Thanks for this usefull code!

 

Is it also possible to create an InputListBox  with an array of values read from a CSV file?

I want to fill in 2 custom Props; material and number by select the correct material from an inputListbox.

 

my csv file looks like this:

Number, Material

1000, Steel

2000, Wood

I know this is possible with an XLS file, but this has a slower read performance because ilogic has to start Excel in the background.

 

 

0 Likes
Message 4 of 7

j_weber
Mentor
Mentor

Hallo, 

 

by searching for a way to read values from a csv to create Inventor parameters I found this posting. 

 

I have a csv file that contains the parameter names and the values. 

I try the second code from @JelteDeJong

A Messegebox show me the pairs but not parameters are created. 

 

What do I wrong or is it not possible to create Inventor Parameter from a csv-file




Jörg Weber
CAD Systemtechniker für AutoCAD, Inventor, Vault





0 Likes
Message 5 of 7

JelteDeJong
Mentor
Mentor
Accepted solution

That rule was only to show how you can read csv files not to set them as parameters. The following rule will read the csv and also set the parameters.

 

Sub Main()
    Dim doc As PartDocument = ThisDoc.Document
    Dim userParameters As UserParameters = doc.
        ComponentDefinition.Parameters.UserParameters

    Dim csvData As Dictionary(Of String, String) = ReadCSV("c:\temp\test.csv")
    For Each item As KeyValuePair(Of String, String) In csvData
        SetParameter(userParameters, item.Key, item.Value)
    Next

End Sub

Public Function ReadCSV(fileName As String)
    Dim splitChar As String = ","
    Dim csvData As Dictionary(Of String, String) = New Dictionary(Of String, String)()

    Using reader As New IO.StreamReader("c:\temp\test.csv", Text.Encoding.Default)
        Dim line As String = reader.ReadLine

        Do While (Not line Is Nothing)
            Try
                Dim splittedLine As String() = line.Split(splitChar)
                csvData.Add(splittedLine(0), splittedLine(1))
            Catch ex As Exception
                MsgBox("Could not add data from line: " & line)
            End Try
            line = reader.ReadLine
        Loop
    End Using
    Return csvData
End Function

Public Sub SetParameter(userParameters As UserParameters, name As String, expresion As String)
    Dim foundPara As UserParameter = userParameters.Cast(Of UserParameter).
        Where(Function(p)
                  Return p.Name.Equals(name)
              End Function).FirstOrDefault()

    If (foundPara Is Nothing) Then
        userParameters.AddByExpression(name, expresion, "mm")
    Else
        foundPara.Expression = expresion
    End If
End Sub

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

Message 6 of 7

j_weber
Mentor
Mentor

Thanks @JelteDeJong for your answer and the code

 

I try it as fast as I can. 

 

by




Jörg Weber
CAD Systemtechniker für AutoCAD, Inventor, Vault





0 Likes
Message 7 of 7

j_weber
Mentor
Mentor

Hallo @JelteDeJong 

 

it works great for me. Thanks a lot for help




Jörg Weber
CAD Systemtechniker für AutoCAD, Inventor, Vault





0 Likes