Community
Inventor Programming - iLogic, Macros, AddIns & Apprentice
Inventor iLogic, Macros, AddIns & Apprentice Forum. Share your knowledge, ask questions, and explore popular Inventor topics related to programming, creating add-ins, macros, working with the API or creating iLogic tools.
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

CSV Filename not working

3 REPLIES 3
SOLVED
Reply
Message 1 of 4
VGonsalves
443 Views, 3 Replies

CSV Filename not working

Hi

I am trying to write code to collect standard info from a csv file to be added to iProperties.

 

Following is the code I have so far.

 

Dim Separators() As Char = {"\"c} Words = ThisDoc.Path.Split(Separators) FilName = "C:\Vault\Designs\" & Words(3) & "\Spec_Sheet.csv"

Dim ReadCSV As New System.IO.StreamReader(FilName) Dim WordSets As New ArrayList()

Do While ReadCSV.Peek <> -1 WordSets.Add(ReadCSV.ReadLine.Split(",")) Loop

i = 0 SName = iProperties.Value("Project", "Stock Number")

For Each wrd In WordSets  If WordSets.item(i)(0) = SName Then  iProperties.Value("Project", "Description") = WordSets.item(i)(1)  iProperties.Value("Custom", "Finish") = WordSets.item(i)(2)  End If i += 1 Next

 

 

The only problem I have is that this line crashes. I am not competent enough to understand why. Can anyone help me

 

Dim ReadCSV As New System.IO.StreamReader(FilName)

 

Anyone......

3 REPLIES 3
Message 2 of 4
Mike.Wohletz
in reply to: VGonsalves

I think a little error handling could help to solve your problem. Please see the included on a little more eligant way to handle this. 

 

   Private Sub test()
        Dim Thisdoc As Document = ThisAppliocation.ActiveDocument
        Dim FilName As String = GetDirFromPath(Thisdoc.FullDocumentName) & "Spec_Sheet.csv"
        If My.Computer.FileSystem.FileExists(FilName) Then
            Dim WordSets As New ArrayList()
            Using ReadCSV As System.IO.StreamReader = New System.IO.StreamReader(FilName)
                Do While ReadCSV.Peek <> -1
                    WordSets.Add(ReadCSV.ReadLine.Split(","))
                Loop
                ' work with your word sets in this area

            End Using
        Else
            'FilName file did not exist. deal with this in this section or ignor it.
            MsgBox(String.Format("{0} does not exist", FilName), MsgBoxStyle.Information, "Error Reading File")
        End If

    End Sub


    Public Function GetDirFromPath(ByVal path As String) _
      As String

        Try
            Return path.Substring(0, path.LastIndexOf("\") + 1)

        Catch ex As Exception
            ' error
            Return ""
        End Try

    End Function

 

 

Message 3 of 4
VGonsalves
in reply to: VGonsalves

Hi Mike
Yes I think you have a elegant solution here. I will not be able to test it until I get back to work on Monday. I have studied the code and I think the directory structure is not what I am looking for... I didn't explain that in my previous post though
The csv file is located at C:\Vault\Designs\Project1\Spec_Sheet.csv but the reference part can be in the following locations
C:\Vault\Designs\Project1\Dir1\ipt or
C:\Vault\Designs\Project1\Dir1\Dir2\ipt or even further
So I am trying to track down to find the right project name directory. The only constant here is that from the ipt file path the third directory under C: is the location where the file is located. That is why I split the file name and rebuilt it. But then couldn't use the rebuilt name with StreamReader. I hope I have been able to explain myself. Hope you can help me with this one.
On a side note I am struggling to get to the help file for iLogic. Can you tell me if any exists? Or suggest a good place to find iLogic command help
Thank you for your help this far
Message 4 of 4
VGonsalves
in reply to: VGonsalves

Hi Mike

 

I have updated my code with your help. It looks like this and works.

DimSeparators()AsChar= {"\"c}  Words=ThisDoc.Path.Split(Separators)DimFilNameAsString="C:\Vault\Designs\"&Words(3)&"\Spec_Sheet.csv"
IfMy.Computer.FileSystem.FileExists(FilName)ThenDimWordSetsAsNewArrayList()UsingReadCSVAsSystem.IO.StreamReader=NewSystem.IO.StreamReader(FilName)DoWhileReadCSV.Peek<>-1WordSets.Add(ReadCSV.ReadLine.Split(","))Loop
i=0SName=iProperties.Value("Project", "Stock Number")ForEachwrdInWordSetsIfWordSets.item(i)(0)=SNameTheniProperties.Value("Project", "Description")=WordSets.item(i)(1)iProperties.Value("Custom", "Finish")=WordSets.item(i)(2)EndIfi+=1NextEndUsingElse'FilName file did not exist. deal with this in this section or ignor it.MsgBox(String.Format("{0} does not exist", FilName), MsgBoxStyle.Information, "Error Reading File")EndIf

But I am still open to your suggestion of directory handling as outlined in my first reply to you.

 

Thank you

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Autodesk Design & Make Report