56 Replies Latest reply on Mar 23, 2017 10:36 AM by Tim Webb

    MACRO TO READ TXT FILE

    Korbi Anis

      Hi friends , I need a macro to read the txt file especially line beagan with "Vertex" ! I want to read the coodinates after every "vertex" word. in attachement you will find my TXT.file

        • Re: MACRO TO READ TXT FILE
          Christian Chu

          You can google this

          Here is the one

          • Re: MACRO TO READ TXT FILE
            Tim Webb

            Korbi,

            If my response provided the solution, I would appreciate you marking my response as the correct response.

            Thanks!

            Tim

            • Re: MACRO TO READ TXT FILE
              Korbi Anis

              Hi friends , emm Maybe I waant to do some changements on the macro . the coordinates are decalared like String . so I want to read them like double. So I like I saidd before I wanto to read stl file espacially lines began with Vertex (word) and read coordinates rint the right side like Double . can you help me ! thanks

                • Re: MACRO TO READ TXT FILE
                  Christian Chu

                  use cdbl function which will covert string to double

                    • Re: MACRO TO READ TXT FILE
                      Korbi Anis

                      Dear Christian , I changed the macro using cdbl function but it deas not work too !!

                      Sub main()

                       

                       

                      Set swApp = Application.SldWorks

                       

                       

                      Dim strFilename As String: strFilename = "C:\jk.stl"

                      Debug.Print strFilename

                      Dim strTextLine As String

                      Dim iFile As Integer: iFile = FreeFile

                      Open strFilename For Input As #iFile

                      Debug.Print strFilename & " Opened."

                      Do Until EOF(1)

                          Line Input #1, strTextLine

                          Dim TempString As String

                          TempString = UCase(Trim(strTextLine))

                          'Debug.Print "TempString = " & TempString

                          If Left(TempString, 6) = "VERTEX" Then

                              Dim myString As String

                              Dim coordinates As String            ----------------> ( I changed it to "Double")

                              coordinates = Right(TempString, Len(TempString) - Len("vertex"))     ---------> cdbl( coordinates = Right(TempString, Len(TempString) - Len("vertex"))   )

                              Debug.Print "Found vertex with coordinates: " & coordinates

                          End If

                      Loop

                      Close #iFile

                      MsgBox "Process complete."

                       

                       

                      End Sub

                        • Re: MACRO TO READ TXT FILE
                          Christian Chu

                          I don't see any line which you included the cdbl function

                          should be like this

                          dim dCorrd as double

                          dCoord = cdbl(StringofCharacter)

                            • Re: MACRO TO READ TXT FILE
                              Korbi Anis

                              I changed it ! but the same probleme it does not work I think thare are a mistake anywhere

                               

                               

                              Sub main()

                               

                               

                              Set swApp = Application.SldWorks

                               

                               

                              Dim strFilename As String: strFilename = "C:\jk.stl"

                              Debug.Print strFilename

                              Dim strTextLine As String

                              Dim iFile As Integer: iFile = FreeFile

                              Open strFilename For Input As #iFile

                              Debug.Print strFilename & " Opened."

                              Do Until EOF(1)

                                  Line Input #1, strTextLine

                                  Dim TempString As String

                                  TempString = UCase(Trim(strTextLine))

                                  'Debug.Print "TempString = " & TempString

                                  If Left(TempString, 6) = "VERTEX" Then

                                      Dim myString As String

                                      Dim coordinates As Double

                                      coordinates =  cdbl( Right(TempString, Len(TempString) - Len("vertex"))   )

                                      Debug.Print "Found vertex with coordinates: " & coordinates

                                  End If

                              Loop

                              Close #iFile

                              MsgBox "Process complete."

                               

                               

                              End Sub

                                • Re: MACRO TO READ TXT FILE
                                  Christian Chu

                                  Not sure what you mean it didn't work

                                  Please capture the debug.print and post here

                                  Can you break down

                                  dim sTemp as string

                                  sTemp = Right(TempString, Len(TempString) - Len("vertex"))

                                  and dim dTemp as double

                                  dTemp = cdbl (sTemp)

                                      • Re: MACRO TO READ TXT FILE
                                        Christian Chu

                                        Put  comment on 2 lines as shown on the image and capture the screen of debug of

                                        debug.print coordinates and post here, please

                                        I suspect, there comma between  the string ???

                                         

                                         

                                            • Re: MACRO TO READ TXT FILE
                                              Korbi Anis

                                              lI did some research on google and I found that CDBL function is used like this CDBL( expression)

                                              so the probleme I think that our expression is a coordinates for example like this  : "8.1012954711914062 -59.383426666259766 39.932231903076172" ....

                                                • Re: MACRO TO READ TXT FILE
                                                  Christian Chu

                                                  I got it now, it's space, not comma

                                                  The expression is ok, I just wanted you to break so I can see what it is

                                                  There are spaces between the numbers, you need to break them before you can convert each to double

                                                    • Re: MACRO TO READ TXT FILE
                                                      Korbi Anis

                                                      I think we have the same idea but how can I delete the space in the expression and after read each nupmber alone like a double ?

                                                        • Re: MACRO TO READ TXT FILE
                                                          Christian Chu

                                                          Try this to break x, y, and  z corrd - replace sTemp with  your coordinates

                                                           

                                                          Dim sTemp As String

                                                          Dim strArray() As String

                                                          sTemp = "-12.21548 12.35589 -15.232145"

                                                             strArray = Split(sTemp, " ") 

                                                             For i = LBound(strArray) To UBound(strArray)

                                                                Debug.Print Trim(strArray(i))

                                                             Next

                                                           

                                                          Here is what I got from this code

                                                              • Re: MACRO TO READ TXT FILE
                                                                Korbi Anis

                                                                now its ok I can read each coordinate separately but when I tried to use CDBL function its does not work

                                                                    • Re: MACRO TO READ TXT FILE
                                                                      Korbi Anis

                                                                      very strangr !!! for me the macro does not work offff !!

                                                                       

                                                                      Sub main()

                                                                       

                                                                       

                                                                      Set swApp = Application.SldWorks

                                                                       

                                                                       

                                                                      Dim strFilename As String: strFilename = "C:\jk.stl"

                                                                      Debug.Print strFilename

                                                                      Dim strTextLine As String

                                                                      Dim iFile As Integer: iFile = FreeFile

                                                                      Open strFilename For Input As #iFile

                                                                      Debug.Print strFilename & " Opened."

                                                                      Do Until EOF(1)

                                                                          Line Input #1, strTextLine

                                                                          Dim TempString As String

                                                                          TempString = UCase(Trim(strTextLine))

                                                                          'Debug.Print "TempString = " & TempString

                                                                          If Left(TempString, 6) = "VERTEX" Then

                                                                              Dim sTemp As String, stemp2 As String

                                                                              Dim strArray() As String

                                                                              Dim dTemp As Double

                                                                      sTemp = Right(TempString, Len(TempString) - Len("vertex"))

                                                                         strArray = Split(sTemp, " ")

                                                                         For i = LBound(strArray) To UBound(strArray)

                                                                            stemp2 = Trim(strArray(i))

                                                                            dTemp = CDbl(stemp2)

                                                                         Next

                                                                          End If

                                                                      Loop

                                                                      Close #iFile

                                                                      MsgBox "Process complete."

                                                                       

                                                                       

                                                                      End Sub

                                                                        • Re: MACRO TO READ TXT FILE
                                                                          Christian Chu

                                                                          Can you comment all the bottom part and show me what's sTemp in the debug.print

                                                                          or you can replace sTemp with something like "12.12354 -25.2325488 45.2325888" and run the macro

                                                                          • Re: MACRO TO READ TXT FILE
                                                                            Tim Webb

                                                                            Hi Korbi,

                                                                            I had a few minutes this afternoon to absorb your intent and determine what you needed...then wrote some code.

                                                                             

                                                                            You need a "structure" to store the coordinates so I've added one called "PartCoordinates" then Dim'd an array of that structure type to stuff all the coordinates into.

                                                                             

                                                                            Here's the complete code. It works well.

                                                                             

                                                                            Dim swApp As Object
                                                                            
                                                                            
                                                                            Private Type xyzCoordinates
                                                                                xcoordinate As Double
                                                                                ycoordinate As Double
                                                                                zcoordinate As Double
                                                                            End Type
                                                                            
                                                                            
                                                                            Sub main()
                                                                            Set swApp = Application.SldWorks
                                                                            
                                                                            
                                                                            Dim PartCoordinates() As xyzCoordinates
                                                                            Dim strFilename As String: strFilename = "<full file path to the STL file>"
                                                                            Dim strTextLine As String
                                                                            Dim iFile As Integer: iFile = FreeFile
                                                                            
                                                                            
                                                                            Debug.Print strFilename
                                                                            
                                                                            
                                                                            Open strFilename For Input As #iFile
                                                                            Debug.Print strFilename & " Opened."
                                                                            Dim i As Integer
                                                                            i = 0
                                                                            Do Until EOF(1)
                                                                                Line Input #1, strTextLine
                                                                                Dim TempString As String
                                                                                TempString = UCase(Trim(strTextLine))
                                                                                Debug.Print "TempString = " & TempString
                                                                                If Left(TempString, 6) = "VERTEX" Then
                                                                                    Dim myString As String
                                                                                    Dim coordinates As String
                                                                                    Dim finalcoords() As String
                                                                                    
                                                                                    coordinates = Trim(Right(TempString, Len(TempString) - Len("vertex")))
                                                                                    Debug.Print "Found vertex with coordinates: " & coordinates
                                                                                    
                                                                                    finalcoords = Split(coordinates, " ")
                                                                                    Debug.Print "finalcoords(0): " & finalcoords(0)
                                                                                    Debug.Print "finalcoords(1): " & finalcoords(1)
                                                                                    Debug.Print "finalcoords(2): " & finalcoords(2)
                                                                                    If i = 0 Then
                                                                                        ReDim PartCoordinates(0)
                                                                                    Else
                                                                                        ReDim Preserve PartCoordinates(UBound(PartCoordinates) + 1)
                                                                                    End If
                                                                                    PartCoordinates(i).xcoordinate = CDbl(finalcoords(0))
                                                                                    PartCoordinates(i).ycoordinate = CDbl(finalcoords(1))
                                                                                    PartCoordinates(i).zcoordinate = CDbl(finalcoords(2))
                                                                                    i = i + 1
                                                                                End If
                                                                            Loop
                                                                            
                                                                            
                                                                            Dim j As Integer
                                                                            For j = 0 To UBound(PartCoordinates)
                                                                                Debug.Print "x-coordinate: " & CStr(PartCoordinates(j).xcoordinate)
                                                                                Debug.Print "y-coordinate: " & CStr(PartCoordinates(j).ycoordinate)
                                                                                Debug.Print "z-coordinate: " & CStr(PartCoordinates(j).zcoordinate)
                                                                            Next j
                                                                            
                                                                            
                                                                            Close #iFile
                                                                            MsgBox "Process complete."
                                                                            
                                                                            
                                                                            End Sub
                                                                            

                                                                            I've highlighted the code I added today to get it to work.

                                                                            1.png

                                                                            Hope this is the solution to what you need.

                                                                            Tim CEPA

                                                                            Believe in The Q!

                                                                              • Re: MACRO TO READ TXT FILE
                                                                                Korbi Anis

                                                                                Hi dear Tim , sorry to be late , Thank you very much forThis code! Unfortunately I tried to run it but it does not work

                                                                                  • Re: MACRO TO READ TXT FILE
                                                                                    Tim Webb

                                                                                    Ok Korbi

                                                                                    Please paste ALL your code from this module. I have a feeling you left something out like declaring the PartCoordinates above like this:

                                                                                     

                                                                                    1.png

                                                                                      • Re: MACRO TO READ TXT FILE
                                                                                        Korbi Anis

                                                                                        Hi Tim , thanks for your aswer !    This is the code that I used but it does not work yet !

                                                                                         

                                                                                                                              *********************************************************

                                                                                         

                                                                                        Dim swApp As Object

                                                                                        Private Type xyzcoordinates

                                                                                        xcoordinate As Double

                                                                                        ycoordinate As Double

                                                                                        zcoordinate As Double

                                                                                        End Type

                                                                                        Sub main()

                                                                                        Set swApp = Application.SldWorks

                                                                                         

                                                                                         

                                                                                        Dim PartCoordinates() As xyzcoordinates

                                                                                        Dim strFilename As String: strFilename = "C:\jk.stl"

                                                                                        Dim strTextLine As String

                                                                                        Dim iFile As Integer: iFile = FreeFile

                                                                                        Debug.Print strFilename

                                                                                        Open strFilename For Input As #iFile

                                                                                        Debug.Print strFilename & "Opened."

                                                                                        Dim i As Integer

                                                                                        i = 0

                                                                                        Do Until EOF(1)

                                                                                        Line Input #1, strTextLine

                                                                                        Dim TempString As String

                                                                                        TempString = UCase(Trim(strTextLine))

                                                                                        Debug.Print "TempString=" & TempString

                                                                                        If Left(TempString, 6) = "vertex" Then

                                                                                        Dim myString As String

                                                                                        Dim coordinates As String

                                                                                        Dim finalcoords() As String

                                                                                        coordinates = Trim(Right(TempString, Len(TempString) - Len("vertex")))

                                                                                        Debug.Print "Found  vertex  with coordinates :" & coordinates

                                                                                        finalcoords = Split(coordinates, "")

                                                                                        Debug.Print "finalcoords(0): " & finalcoords(0)

                                                                                        Debug.Print "finalcoords(1): " & finalcoords(1)

                                                                                        Debug.Print "finalcoords(2): " & finalcoords(2)

                                                                                        If i = 0 Then

                                                                                        ReDim PartCoordinates(0)

                                                                                        Else

                                                                                        ReDim Preserve PartCoordinates(UBound(PartCoordinates) + 1)

                                                                                        End If

                                                                                        PartCoordinates(i).xcoordinate = CDbl(finalcoords(0))

                                                                                        PartCoordinates(i).ycoordinate = CDbl(finalcoords(1))

                                                                                        PartCoordinates(i).zcoordinate = CDbl(finalcoords(2))

                                                                                        i = i + 1

                                                                                        End If

                                                                                        Loop

                                                                                        Dim j As Integer

                                                                                        For j = 0 To UBound(PartCoordinates)

                                                                                        Debug.Print "x-coordinate: " & CStr(PartCoordinates(j).xcoordinate)

                                                                                        Debug.Print "y-coordinate: " & CStr(PartCoordinates(j).xcoordinate)

                                                                                        Debug.Print "z-coordinate: " & CStr(PartCoordinates(j).xcoordinate)

                                                                                        Next j

                                                                                        Close #iFile

                                                                                        MsgBox "Process complete."

                                                                                        End Sub

                                                                                        • Re: MACRO TO READ TXT FILE
                                                                                          Korbi Anis

                                                                                              Very strange I tried to evaluate to macto another time and it s the same probleme . I don't understand what's the mistake !

                                                                                            • Re: MACRO TO READ TXT FILE
                                                                                              Tim Webb

                                                                                              Hi again Korbi!

                                                                                              It's not very strange and yes it was a mistake you made, you didn't copy and paste the entire code! I ran your code and it doesn't work for me either and I found the error but I'm not going to tell you because I can see you did not copy the entire code block into your program.

                                                                                               

                                                                                              Please, if you are going to ask for assistance, copy and paste the entire code block provided by those who help you because it was a simple troubleshooting error where you left out a space. Do I have your word that you will copy and paste the whole code block?

                                                                                               

                                                                                              Here is the final code I am running which works! I added a little bit more code to tighten up the scan but I'm not going to tell you where it is because I don't want you to just try to copy one or two lines, I want you to copy this ENTIRE code block and REPLACE your ENTIRE code block with this block.

                                                                                               

                                                                                              Dim swApp As Object
                                                                                              
                                                                                              
                                                                                              Private Type xyzCoordinates
                                                                                                  xcoordinate As Double
                                                                                                  ycoordinate As Double
                                                                                                  zcoordinate As Double
                                                                                              End Type
                                                                                              Private Const ScanText As String = "VERTEX"
                                                                                              
                                                                                              
                                                                                              Sub main()
                                                                                              Set swApp = Application.SldWorks
                                                                                              
                                                                                              
                                                                                              Dim PartCoordinates() As xyzCoordinates
                                                                                              Dim strFilename As String: strFilename = "C:\Users\tim.webb\OneDrive\Business Development\EPDM add-ins\SW Read STL file coordinates\STL File\jk.stl"
                                                                                              Dim strTextLine As String
                                                                                              Dim iFile As Integer: iFile = FreeFile
                                                                                              
                                                                                              
                                                                                              'Debug.Print strFilename
                                                                                              
                                                                                              
                                                                                              Open strFilename For Input As #iFile
                                                                                              Debug.Print strFilename & " Opened."
                                                                                              Dim i As Integer
                                                                                              i = 0
                                                                                              Do Until EOF(1)
                                                                                                  Line Input #1, strTextLine
                                                                                                  Dim TempString As String
                                                                                                  TempString = UCase(Trim(strTextLine))
                                                                                              '    Debug.Print "TempString = " & TempString
                                                                                                  If UCase(Left(TempString, 6)) = ScanText Then
                                                                                                      Dim myString As String
                                                                                                      Dim coordinates As String
                                                                                                      Dim finalcoords() As String
                                                                                              
                                                                                              
                                                                                                      coordinates = Trim(Right(TempString, Len(TempString) - Len(ScanText)))
                                                                                              '        Debug.Print "Found vertex with coordinates: " & coordinates
                                                                                              
                                                                                              
                                                                                                      finalcoords = Split(coordinates, " ")
                                                                                              '        Debug.Print "finalcoords(0): " & finalcoords(0)
                                                                                              '        Debug.Print "finalcoords(1): " & finalcoords(1)
                                                                                              '        Debug.Print "finalcoords(2): " & finalcoords(2)
                                                                                                      If i = 0 Then
                                                                                                          ReDim PartCoordinates(0)
                                                                                                      Else
                                                                                                          ReDim Preserve PartCoordinates(UBound(PartCoordinates) + 1)
                                                                                                      End If
                                                                                                      PartCoordinates(i).xcoordinate = CDbl(finalcoords(0))
                                                                                                      PartCoordinates(i).ycoordinate = CDbl(finalcoords(1))
                                                                                                      PartCoordinates(i).zcoordinate = CDbl(finalcoords(2))
                                                                                                      i = i + 1
                                                                                                  End If
                                                                                                  DoEvents
                                                                                              Loop
                                                                                              
                                                                                              
                                                                                              Dim j As Integer
                                                                                              For j = 0 To UBound(PartCoordinates)
                                                                                                  Debug.Print "x-coordinate: " & CStr(PartCoordinates(j).xcoordinate)
                                                                                                  Debug.Print "y-coordinate: " & CStr(PartCoordinates(j).ycoordinate)
                                                                                                  Debug.Print "z-coordinate: " & CStr(PartCoordinates(j).zcoordinate)
                                                                                                  DoEvents
                                                                                              Next j
                                                                                              
                                                                                              
                                                                                              Close #iFile
                                                                                              MsgBox "Process complete."
                                                                                              
                                                                                              
                                                                                              End Sub
                                                                                              

                                                                                               

                                                                                              Tim CEPA

                                                                                              Believe in The Q!

                                                    • Re: MACRO TO READ TXT FILE
                                                      Korbi Anis

                                                      If we consider this macro for example , the result is the string in the right side for exemple : [ 0.88582742214202881 -64.497276306152344 39.931648254394531]   ok ? so the goal is to transform this side in Double but the probleme that if we transform it directly l As Double the macro wel read it like a same number not separately number  ( X=0.88582742214202881 , Y=64.497276306152344  , Z=39.931648254394531) . I need your help friends !

                                                       

                                                       

                                                       

                                                       

                                                       

                                                       

                                                      Sub main()

                                                       

                                                       

                                                      Set swApp = Application.SldWorks

                                                       

                                                       

                                                      Dim strFilename As String: strFilename = "C:\jk.stl"

                                                      Debug.Print strFilename

                                                      Dim strTextLine As String

                                                      Dim iFile As Integer: iFile = FreeFile

                                                      Open strFilename For Input As #iFile

                                                      Debug.Print strFilename & " Opened."

                                                      Do Until EOF(1)

                                                          Line Input #1, strTextLine

                                                          Dim TempString As String

                                                          TempString = UCase(Trim(strTextLine))

                                                          'Debug.Print "TempString = " & TempString

                                                          If Left(TempString, 6) = "VERTEX" Then

                                                              Dim coordinates As String

                                                              coordinates = Right(TempString, Len(TempString) - Len("vertex"))

                                                              Debug.Print coordinates

                                                          End If

                                                      Loop

                                                      Close #iFile

                                                      MsgBox "Process complete."

                                                       

                                                       

                                                      End Sub