- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
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
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
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)
NextThis 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.
Blog: hjalte.nl - github.com
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
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.
Blog: hjalte.nl - github.com
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
Hallo @JelteDeJong
it works great for me. Thanks a lot for help

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