|
-
Jun 16th, 2006, 01:04 PM
#1
Thread Starter
Lively Member
Text file loop
Hi everyone
Hey i could use some help with a text file problem. I want to open a very large text (3000lines) file and write "G04 P1" after every "Z" line that is already in the file. Any help?
This is the start of the cnc gcode program
%
O0000
(PROGRAM NAME - EAD )
(DATE=DD-MM-YY - 16-06-06 TIME=HH:MM - 10:44 )
N100 G20
N102 G0 G17 G40 G49 G80 G90
N104 G91 G28 Z0.
N106 G91 G28 X0. Y0.
N108 G92 X0. ? Y0. ? Z0. ?
N110 G0 G90 G52 X2.847 Y0. S764 M3
N112 Z.25
N114 Z.1
N116 G1 Z0. F6.1
N118 X2.852 F12.2
N120 X2.857
N122 Z-.0002 #### Start below this line ######
N124 X2.862
N126 Z-.0004
N128 X2.867
N130 Z-.0007
N132 X2.872
N134 Z-.0011
N136 X2.8769
N138 Z-.0016
N140 X2.8819
N142 Z-.0022
N144 X2.8869
Thanks Reston
-
Jun 16th, 2006, 01:12 PM
#2
Re: Text file loop
You can't edit a text file in place. The best you can do is open the file for read, open another file for write, read each line into a string variable, test if it's a Z line, add "G04 P1" to the end if it is, then write the string variable to the write file regardless.
The most difficult part of developing a program is understanding the problem.
The second most difficult part is deciding how you're going to solve the problem.
Actually writing the program (translating your solution into some computer language) is the easiest part.
Please indent your code and use [HIGHLIGHT="VB"] [/HIGHLIGHT] tags around it to make it easier to read.
Please Help Us To Save Ana
-
Jun 16th, 2006, 01:22 PM
#3
Thread Starter
Lively Member
Re: Text file loop
I have been writing to a log file for awhile, but have never opened a file and wrote to another like this. Any starter type code would be a great help.
Reston
-
Jun 16th, 2006, 11:10 PM
#4
Thread Starter
Lively Member
Re: Text file loop
Can anyone help with this?
-
Jun 17th, 2006, 12:58 AM
#5
Re: Text file loop
i hope this code will help you
VB Code:
Dim sContent As String
Dim iInputFile As Integer
Dim iOutputFile As Integer
iInputFile = FreeFile()
Open "E:\MC\sometext.txt" For Input As #iInputFile
iOutputFile = FreeFile()
Open "E:\MC\anotherFile.txt" For Output As #iOutputFile
Do While Not EOF(iInputFile)
Line Input #iInputFile, sContent
'If InStr(sContent, "Z") > 0 Then ' If you just want to check for Z
If UCase(Mid(sContent, 6, 1)) = "Z" Then ' If you just want to check for Z in a specific pos
Print #iOutputFile, sContent
Print #iOutputFile, "G04 P1"
Else
Print #iOutputFile, sContent
End If
Loop
Close #iInputFile
Close #iOutputFile
If an answer to your question has been helpful, then please, Rate it!
Have done Projects in Access and Member management systems using BioMetric devices, Smart cards and BarCodes.
-
Jun 17th, 2006, 11:29 PM
#6
Thread Starter
Lively Member
Re: Text file loop
Hey that works killer. Thank you soo much.
I did have one more question about this. I there a way to number the new line that we are putting in. If you look at the G Code file you will see that the file is numbered,But does skip a number. The new line needs to be the next number in sequence.
Another thing that i just noticed is the if the file numbers reaches 1000 it stops adding the "G04 P1"
N988 X2.8106
N990 Z-.7197
G04 P1
N992 X2.8142
N994 Z-.7232
G04 P1
N996 X2.8178
N998 Z-.7267
G04 P1
N1000 X2.8213
N1002 Z-.7302
N1004 X2.8249
N1006 Z-.7337
N1008 X2.8285
N1010 Z-.7371
N1012 X2.8322
N1014 Z-.7406
N1016 X2.8358
N1018 Z-.7441
N1020 X2.8394
N1022 Z-.7475
N1024 X2.843
N1026 Z-.7509
I think If UCase(Mid(sContent, 6, 1)) may be looking at the sixth charactor.
Thanks again
Reston
-
Jun 17th, 2006, 11:38 PM
#7
Thread Starter
Lively Member
Re: Text file loop
Hi all
I think i figured out part of it.
Vb Code ______________________________________
Private Sub Command1_Click()
Dim sContent As String
Dim iInputFile As Integer
Dim iOutputFile As Integer
iInputFile = FreeFile()
Open "C:\Documents and Settings\n\Desktop\EAD.t" For Input As #iInputFile
iOutputFile = FreeFile()
Open "C:\Documents and Settings\n\Desktop\EAD2.t" For Output As #iOutputFile
Do While Not EOF(iInputFile)
Line Input #iInputFile, sContent
'If InStr(sContent, "Z") > 0 Then ' If you just want to check for Z
If UCase(Mid(sContent, 6, 2)) = "Z-" Or UCase(Mid(sContent, 7, 2)) = "Z-" Then ' If you just want to check for Z in a specific pos
Print #iOutputFile, sContent
Print #iOutputFile, "G04 P1"
Else
Print #iOutputFile, sContent
End If
Loop
Close #iInputFile
Close #iOutputFile
End Sub
-
Jun 18th, 2006, 12:30 AM
#8
Re: Text file loop
Try this:
VB Code:
Dim sContent As String
Dim iInputFile As Integer
Dim iOutputFile As Integer
Dim iFirstSpace As Integer
Dim iLineNumber As Integer
iInputFile = FreeFile()
Open "E:\MC\sometext.txt" For Input As #iInputFile
iOutputFile = FreeFile()
Open "E:\MC\anotherFile.txt" For Output As #iOutputFile
Do While Not EOF(iInputFile)
Line Input #iInputFile, sContent
Print #iOutputFile, sContent
iFirstSpace = InStr(sContent, " ") ' Find the position of the first space
If Mid$(sContent, iFirstSpace + 1, 1) = "Z" Then ' Check the first character after the first space
iLineNumber = CInt(Mid$(sContent, 2, iFirstSpace - 2)) + 1 ' Get the line number, then add 1
Print #iOutputFile, "N" & iLineNumber & " G04 P1" ' Create and print the new line
End If
Loop
Close #iInputFile
Close #iOutputFile
-
Jun 18th, 2006, 04:27 AM
#9
PowerPoster
Re: Text file loop
Why not just open the file, get the text, close it, work on that text, then replace the file with that updated text ..??
-
Jun 18th, 2006, 05:04 AM
#10
PowerPoster
Re: Text file loop
VB Code:
Private Sub Command1_Click()
Dim sContent As String
Dim sText As String
Dim sArray() As String
Dim iFile As Integer
Dim sTemp As String
Dim iTemp As Integer
Dim i As Integer
iFile = FreeFile
Open "C:\Documents and Settings\n\Desktop\EAD.t" For Input As #iFile
sContent = Trim$(Input$(LOF(iFile), #iFile))
Close iFile
If Len(sContent) Then
sArray = Split(sContent, vbCrLf)
For i = 0 To UBound(sArray)
sText = sText & sArray(i) & vbCrLf
iTemp = InStr(sArray(i), " ")
sTemp = Mid$(sArray(i), iTemp + 1, 1)
If UCase$(sTemp) = "Z" And iTemp > 2 Then
sTemp = Mid$(sArray(i), 2, iTemp - 2)
If IsNumeric(sTemp) Then
sText = sText & "N" & CInt(sTemp) + 1 & " G04 P1" & vbCrLf
End If
End If
Next i
Open "C:\Documents and Settings\n\Desktop\EAD.t" For Output As #iFile
Print #iFile, sText;
Close iFile
Debug.Print "Finished"
End If
End Sub
Last edited by rory; Jun 18th, 2006 at 06:06 AM.
-
Jun 19th, 2006, 03:04 PM
#11
Thread Starter
Lively Member
Re: Text file loop
Hey Logophobic
Thanks for all the help on this by the way.
It looks like my work mates are making this harder on me. the file that i got from them was not the same as what they wanted.
what you helped me with is awesome but if i may ask you to help with one last thing that would be great.
This is the problem.
I can use what you helped me with to add the line number and after X a G04 P1 but i need to add The line number and the G04 P1 after Z line that is on it's own line also.
%
O0000
(PROGRAM NAME - U-CUT2 )
(DATE=DD-MM-YY - 19-06-06 TIME=HH:MM - 12:40 )
N100 G20
N102 G0 G17 G40 G49 G80 G90
N104 G91 G28 Z0.
N106 G91 G28 X0. Y0.
N108 G92 X0. ? Y0. ? Z0. ?
N110 G0 G90 G52 X2.847 Y0. S2000 M3
N112 Z.25
N114 Z.1
N116 G1 Z0. F10.
N118 X2.848
N120 X2.849
N122 X2.85
N124 X2.851
N126 X2.852
N128 X2.853 Z-.0001
N130 X2.854
N132 X2.855
N134 X2.856
N136 X2.857 Z-.0002
N138 X2.858
N140 X2.859 Z-.0003
N142 X2.86
N144 X2.861 Z-.0004
N146 X2.862
N148 X2.863
N150 Z-.0005 #### Here ###
N152 X2.864
N154 X2.865
N156 Z-.0006
N158 X2.866
N160 X2.867
N162 Z-.0007 ######Here ######
N164 X2.868
N166 Z-.0008 ######Here ######
N168 X2.869
N170 Z-.0009 ######Here ######
N172 X2.87
N174 Z-.001
N176 X2.871
N178 X2.872
N180 Z-.0011
N182 X2.873
N184 Z-.0012
Thenks for all the help
Reston
-
Jun 19th, 2006, 06:39 PM
#12
Re: Text file loop
You want to add line number and "G04 P1" on the same line, or just add "G04 P1"? I'm guessing you don't want two line numbers on one line...
VB Code:
Dim sContent As String
Dim iInputFile As Integer
Dim iOutputFile As Integer
Dim iFirstSpace As Integer
Dim iLineNumber As Integer
iInputFile = FreeFile()
Open "E:\MC\sometext.txt" For Input As #iInputFile
iOutputFile = FreeFile()
Open "E:\MC\anotherFile.txt" For Output As #iOutputFile
Do While Not EOF(iInputFile)
Line Input #iInputFile, sContent
iFirstSpace = InStr(sContent, " ") ' Find the position of the first space
If Mid$(sContent, iFirstSpace + 1, 1) = "Z" Then ' Check the first character after the first space
sContent = sContent & " G04 P1" ' Add to the line
End If
Print #iOutputFile, sContent
Loop
Close #iInputFile
Close #iOutputFile
-
Jun 19th, 2006, 08:18 PM
#13
Thread Starter
Lively Member
Re: Text file loop
Hi Look at my last post at line number 150 and see how that line is a Z move. I want a numberline and Go4 P1 just past that line also.
This is what i came up with from your post.
visual basic code:
-----------------------------------------
Private Sub Command1_Click()
Dim sContent As String
Dim iInputFile As Integer
Dim iOutputFile As Integer
Dim iFirstSpace As Integer
Dim iLineNumber As Integer
iInputFile = FreeFile()
Open "C:\Documents and Settings\n\Desktop\U-CUT2.t" For Input As #iInputFile
iOutputFile = FreeFile()
Open "C:\Documents and Settings\n\Desktop\U-CUT3.t" For Output As #iOutputFile
Do While Not EOF(iInputFile)
Line Input #iInputFile, sContent
Print #iOutputFile, sContent
iFirstSpace = InStr(sContent, " ") ' Find the position of the first space
If Mid$(sContent, iFirstSpace + 1, 1) = "X" Then ' Check the first character after the first space
iLineNumber = CInt(Mid$(sContent, 2, iFirstSpace - 2)) + 1 ' Get the line number, then add 1
Print #iOutputFile, "N" & iLineNumber & " G04 P1" ' Create and print the new line
End If
iFirstSpace = InStr(sContent, " ") ' Find the position of the first space
If Mid$(sContent, iFirstSpace + 1, 1) = "Z" Then ' Check the first character after the first space
iLineNumber = CInt(Mid$(sContent, 2, iFirstSpace - 2)) + 1 ' Get the line number, then add 1
Print #iOutputFile, "N" & iLineNumber & " G04 P1" ' Create and print the new line
End If
Loop
Close #iInputFile
Close #iOutputFile
End Sub
-----------------------------------------
This what the G-Code looks like before i use the code you helped me out with.
%
O0000
(PROGRAM NAME - U-CUT2 )
(DATE=DD-MM-YY - 19-06-06 TIME=HH:MM - 12:40 )
N100 G20
N102 G0 G17 G40 G49 G80 G90
N104 G91 G28 Z0.
N106 G91 G28 X0. Y0.
N108 G92 X0. ? Y0. ? Z0. ?
N110 G0 G90 G52 X2.847 Y0. S2000 M3
N112 Z.25
N114 Z.1
N116 G1 Z0. F10.
N118 X2.848
N120 X2.849
N122 X2.85
N124 X2.851
N126 X2.852
N128 X2.853 Z-.0001
N130 X2.854
N132 X2.855
N134 X2.856
N136 X2.857 Z-.0002
N138 X2.858
N140 X2.859 Z-.0003
N142 X2.86
N144 X2.861 Z-.0004
N146 X2.862
N148 X2.863
N150 Z-.0005
N152 X2.864
N154 X2.865
N156 Z-.0006
N158 X2.866
N160 X2.867
N162 Z-.0007
N164 X2.868
N166 Z-.0008
N168 X2.869
N170 Z-.0009
N172 X2.87
N174 Z-.001
N176 X2.871
N178 X2.872
N180 Z-.0011
N182 X2.873
N184 Z-.0012
N186 X2.874
N188 Z-.0013
N190 X2.875
N192 Z-.0014
N194 X2.8759
N196 Z-.0015
Hey so just wondering. What do you do that you know about VB?
Thanks Reston
-
Jun 19th, 2006, 08:24 PM
#14
Thread Starter
Lively Member
Re: Text file loop
And after the code has been run.
%
O0000
(PROGRAM NAME - U-CUT2 )
(DATE=DD-MM-YY - 19-06-06 TIME=HH:MM - 12:40 )
N100 G20
N102 G0 G17 G40 G49 G80 G90
N104 G91 G28 Z0.
N106 G91 G28 X0. Y0.
N108 G92 X0. ? Y0. ? Z0. ?
N110 G0 G90 G52 X2.847 Y0. S2000 M3
N112 Z.25
N113 G04 P1
N114 Z.1
N115 G04 P1
N116 G1 Z0. F10.
N118 X2.848
N119 G04 P1
N120 X2.849
N121 G04 P1
N122 X2.85
N123 G04 P1
N124 X2.851
N125 G04 P1
N126 X2.852
N127 G04 P1
N128 X2.853 Z-.0001
N129 G04 P1
N130 X2.854
N131 G04 P1
N132 X2.855
N133 G04 P1
N134 X2.856
N135 G04 P1
N136 X2.857 Z-.0002
N137 G04 P1
N138 X2.858
N139 G04 P1
N140 X2.859 Z-.0003
N141 G04 P1
N142 X2.86
N143 G04 P1
N144 X2.861 Z-.0004
N145 G04 P1
N146 X2.862
N147 G04 P1
N148 X2.863
N149 G04 P1
N150 Z-.0005
N151 G04 P1
N152 X2.864
N153 G04 P1
N154 X2.865
N155 G04 P1
N156 Z-.0006
N157 G04 P1
N158 X2.866
N159 G04 P1
N160 X2.867
N161 G04 P1
N162 Z-.0007
N163 G04 P1
N164 X2.868
N165 G04 P1
N166 Z-.0008
N167 G04 P1
N168 X2.869
N169 G04 P1
N170 Z-.0009
N171 G04 P1
N172 X2.87
N173 G04 P1
N174 Z-.001
N175 G04 P1
N176 X2.871
N177 G04 P1
N178 X2.872
N179 G04 P1
N180 Z-.0011
N181 G04 P1
N182 X2.873
N183 G04 P1
N184 Z-.0012
N185 G04 P1
N186 X2.874
-
Jun 21st, 2006, 01:42 PM
#15
Thread Starter
Lively Member
Re: Text file loop
Hi All
I had some help with this bit of code. the problem i'm having now is that my work mates want this to work on a file that is 150000 line of g-code. I'm getting an "RUN TIME ERROR 6"
overflow
This loops through 32766 line before i get this error.
visual basic code:
-----------------------------------------
Private Sub Command1_Click()
Dim sContent As String
Dim iInputFile As Integer
Dim iOutputFile As Integer
Dim iFirstSpace As Integer
Dim iLineNumber As Integer
iInputFile = FreeFile()
Open "C:\Documents and Settings\n\Desktop\U-CUT2.t" For Input As #iInputFile
iOutputFile = FreeFile()
Open "C:\Documents and Settings\n\Desktop\U-CUT3.t" For Output As #iOutputFile
Do While Not EOF(iInputFile)
Line Input #iInputFile, sContent
Print #iOutputFile, sContent
iFirstSpace = InStr(sContent, " ") ' Find the position of the first space
If Mid$(sContent, iFirstSpace + 1, 1) = "X" Then ' Check the first character after the first space
iLineNumber = CInt(Mid$(sContent, 2, iFirstSpace - 2)) + 1 ' Get the line number, then add 1
Print #iOutputFile, "N" & iLineNumber & " G04 P1" ' Create and print the new line
End If
iFirstSpace = InStr(sContent, " ") ' Find the position of the first space
If Mid$(sContent, iFirstSpace + 1, 1) = "Z" Then ' Check the first character after the first space
iLineNumber = CInt(Mid$(sContent, 2, iFirstSpace - 2)) + 1 ' Get the line number, then add 1
Print #iOutputFile, "N" & iLineNumber & " G04 P1" ' Create and print the new line
End If
Loop
Close #iInputFile
Close #iOutputFile
End Sub
-----------------------------------------
-
Jun 21st, 2006, 01:43 PM
#16
Re: Text file loop
 Originally Posted by tiguy
This loops through 32766 line before i get this error.
You have hit the max an Integer can hold. Change all of your Integers to Longs.
-
Jun 21st, 2006, 01:50 PM
#17
Lively Member
Re: Text file loop
no need to change iInputFile and iOutputFile to Long
-
Jun 21st, 2006, 01:55 PM
#18
Re: Text file loop
 Originally Posted by Agilaz
no need to change iInputFile and iOutputFile to Long 
I never use Integers for anything out of habit because I have been bitten by this situation one too many times.
Even if I need to loop through 5 items, I still use a Long.
-
Jun 21st, 2006, 02:00 PM
#19
Thread Starter
Lively Member
Re: Text file loop
Hey that did not work. i'm still getting the same error.
Any help?
-
Jun 21st, 2006, 02:07 PM
#20
Lively Member
Re: Text file loop
 Originally Posted by Hack
I never use Integers for anything out of habit because I have been bitten by this situation one too many times.
Even if I need to loop through 5 items, I still use a Long. 
but freefile() always returns a Integer so there's no need to let VB convert it to Long
@tiguy
change all cInt() to cLng()
-
Jun 21st, 2006, 02:20 PM
#21
Thread Starter
Lively Member
Re: Text file loop
You guy's are killer. that work like a charm.
Thank you
Reston
-
Dec 6th, 2006, 11:22 PM
#22
Thread Starter
Lively Member
Re: Text file loop
Hey everyone
I've been using this code for awhile now and I'm trying to mod it. I'm having no luck thus far.
I want to remove the empty space between the "N" and the number that follows it.
Any help would be nice
%
O0000
(PROGRAM NAME - U-CUT2 )
(DATE=DD-MM-YY - 19-06-06 TIME=HH:MM - 12:40 )
N 100 G20
N 102 G0 G17 G40 G49 G80 G90
N 104 G91 G28 Z0.
N 106 G91 G28 X0. Y0.
N 108 G92 X0. ? Y0. ? Z0. ?
N 110 G0 G90 G52 X2.847 Y0. S2000 M3
N 112 Z.25
N 113 G04 P1
N 114 Z.1
N 115 G04 P1
N 116 G1 Z0. F10.
N 118 X2.848
N 119 G04 P1
N 120 X2.849
N 121 G04 P1
N 122 X2.85
N 123 G04 P1
N 124 X2.851
N 125 G04 P1
VB Code ________________________________
Private Sub Command1_Click()
Dim sContent As String
Dim iInputFile As Integer
Dim iOutputFile As Integer
Dim iFirstSpace As Integer
Dim iLineNumber As Integer
iInputFile = FreeFile()
Open "C:\Ni\Nc\Nc.T" For Input As #iInputFile
iOutputFile = FreeFile()
Open "C:\Ni\Nc\Nc2.T" For Output As #iOutputFile
Do While Not EOF(iInputFile)
Line Input #iInputFile, sContent
Print #iOutputFile, sContent
iFirstSpace = InStr(sContent, " ") ' Find the position of the first space
If Mid$(sContent, iFirstSpace, 1) = " " Then ' Check the first character after the first space
'iLineNumber = CInt(Mid$(sContent, 2, iFirstSpace - 2)) + 1 ' Get the line number, then add 1
Print #iOutputFile, "" '& iLineNumber & " G04 P1" ' Create and print the new line
End If
Loop
Close #iInputFile
Close #iOutputFile
End Sub
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|