 |
AutoHotkey Community Let's help each other out
|
| View previous topic :: View next topic |
| Author |
Message |
laserman
Joined: 21 Jan 2008 Posts: 9
|
Posted: Mon Feb 18, 2008 3:57 pm Post subject: Request: Help with variable and loop |
|
|
Hey everybody, I pretty new to writing scripts, the ones I've written all all pretty basic, so if anybody can help it would be great. I am trying to write a script to edit a text files ( a numerical code file for a CNC machine.) I need to make sure that the line "G91G00Z1.000" is inserted before every line that begins with N#. Where # is any number.
Here's the fun part in the original file some times the line "G91G00Z1.000" is already there, in this case the script would need to skip to the next "N" variable. Also, the number of lines between each variable is not always the same and the number of variable can go as high as 1000.
Here is an example of my text file.
| Code: |
%
O2
(CUSTOMER NAME : john doe)
(PART FILE NAME : part)
(NC FILE NAME : C:\FabriWIN\FC2001\anyfile.nc)
(SHEET FILE NAME : sheet 3)
(MATERIAL : 26ga)
(THICKNESS : 0.062)
(BLANK SIZE : 60.000 X 48.000)
(SHEET USAGE : 76.91 PERCENT)
(PART SIZE : 25.670 X 22.670)
(OFFSET : 0.300 X 23.800)
(SPACE BETWEEN : 0 X 0)
(YIELD PER BLANK : 11 PARTS)
(CLAMPS : 12.00 36.00 60.00 84.00)
(ESTIMATED TIME : 22 MIN. 41 SEC.)
(PROGRAMMED BY : ME)
(DATE : 2-14-2008)
(TIME : 14:41)
(COMMENTS)
G91G28Z2.000
G22J0.062
G90G54X0.Y0.
N1G90G00X48.7250Y44.0310
M98P8040
G61G42G01Y43.9655D1
G02J0.0655
M21
M23
G40
M31
M09
G91G00Z1.000
N2G90G00X48.7250Y43.4090
M98P8040
G42G01Y43.3435D1
G02J0.0655
M21
M23
G40
M31
M09
G91G00Z1.000
N3G90G00X48.1000Y42.1105
M98P8040
G42G01Y42.0105D1
G02J0.1875
M21
M23
G40
M31
M09
G91G00Z1.000
N4G90G00X0.3410Y10.9200
M98P8040
G01Y13.5660
X1.6540
Y13.5300
X1.6550
Y14.3130
X3.0350
Y13.5660
X3.1150
Y15.7920
X23.0310
Y13.5660
X23.1110
Y14.3130
X24.4910
Y13.5300
X24.4920
Y13.5660
X25.8050
Y10.6700
X24.4920
Y10.7060
X24.4910
Y9.9230
X23.1110
Y10.6700
X23.0310
Y8.4440
M21
M23
M31
M09
N5G90G00X0.2410Y3.3220
M98P8040
G01X0.3410
Y3.8220
Y3.3220
X1.6540
Y3.3580
X1.6550
Y2.5750
M21
M23
M31
M09
N6G90G00X0.3410Y3.5720
M98P8040
G01Y6.2180
X1.6540
Y6.1820
X1.6550
Y6.9650
X3.0350
Y6.2180
X3.1150
Y8.4440
X23.0310
Y6.2180
X23.1110
Y6.9650
X24.4910
Y6.1820
X24.4920
Y6.2180
X25.8050
Y3.3220
X24.4920
Y3.3580
X24.4910
Y2.5750
M21
M23
M31
M09
N7G90G00X1.5550Y2.5750
M98P8040
G01X1.6550
X3.0350
Y3.3220
X3.1150
Y1.0960
X23.0310
Y3.3220
X23.1110
Y2.5750
X24.4910
M21
M23
M31
M09
M98P351
G00X100.Y12.
M30
%
|
I think I need to set the script up to use a variable and also a loop.
I haven't worked with variables or loops before and am hitting a brick wall.
Any help would be greatly appreciated.  |
|
| Back to top |
|
 |
razlin1 Guest
|
Posted: Mon Feb 18, 2008 4:03 pm Post subject: |
|
|
look up
FileReadLine
in help
should send you on a good path to get most done. |
|
| Back to top |
|
 |
grupo Guest
|
Posted: Mon Feb 18, 2008 5:48 pm Post subject: |
|
|
The easy way is using a loop to read the file, store the lines in a variable (for example: prev_line).
Check if the current "a_loopreadline" is a "N#" line and "prev_line" is = "g91g00z1.000", if not insert it between "prev_line" and "a_loopreadline" |
|
| Back to top |
|
 |
[VxE]
Joined: 07 Oct 2006 Posts: 1494
|
Posted: Tue Feb 19, 2008 1:52 am Post subject: |
|
|
| Code: | MyFilePath = c:\YourFullFilePath.txt
FileRead, file, %MyFile%
StringToInsert = G91G00Z1.000
IfExist %MyFilePath%.Fixed.txt
FileDelete, %MyFilePath%.Fixed.txt
Loop, parse, file, `n, `r
{
If ( Asc("N") = Asc(SubStr(A_LoopField, 1, 1)) )
Num := SubStr(A_LoopField, 2, Abs(InStr(A_LoopField, "G") - 2))
If (num+1) && !InStr(PrevLine, StringToInsert)
StringReplace, file, file, %A_LoopField%, %StringToInsert%`n%A_LoopField%
PrevLine := num := A_LoopField
}
FileAppend, %file%, %MyFilePath%.Fixed.txt | untested _________________ My Home Thread
More Common Answers: [1]. It's in the FAQ [2]. Ternary ( a ? b : c ) guide [3]. Post code inside [code][/code] tags ! |
|
| Back to top |
|
 |
laserman
Joined: 21 Jan 2008 Posts: 9
|
Posted: Tue Feb 19, 2008 6:29 pm Post subject: |
|
|
| Thanks everyone!! I made one or two small changes and it works like a dream. Now I can sleep better at night and my boss will be happy that our machine is working properly. |
|
| Back to top |
|
 |
engunneer
Joined: 30 Aug 2005 Posts: 6847 Location: Pacific Northwest, US
|
Posted: Tue Feb 19, 2008 6:52 pm Post subject: |
|
|
I happen to know CNC code, and it's pretty strange to have a request like that. Are you sure you can rely on there being an N command only in certain places? N is used for generic line numbering, and depending on what wrote the G code for you, it may be place in some cases on every line. Maybe for your current task, you can rely on the N lines, but I wouldn't expect to in a a general case. _________________
Unless otherwise noted, all code is untested.
Common Answers: 1.(Loops, Viruses, etc.) 2. Search 3.RTFM |
|
| Back to top |
|
 |
laserman
Joined: 21 Jan 2008 Posts: 9
|
Posted: Tue Feb 19, 2008 7:53 pm Post subject: |
|
|
| This is for a sheet metal laser. The only time it uses an "N" is for a new cut. I just needed the cutting head to move up before every cut. The program I use to write the nc code has a bug in it and will not do it in certain situations. |
|
| Back to top |
|
 |
engunneer
Joined: 30 Aug 2005 Posts: 6847 Location: Pacific Northwest, US
|
Posted: Tue Feb 19, 2008 8:51 pm Post subject: |
|
|
I work at a place that makes waterjets. We can cut thin sheet metal VERY quickly.
I just wanted to mention the thing with N, since that's an odd thing about your NC Post processor, and isn't common to most NC PPs. _________________
Unless otherwise noted, all code is untested.
Common Answers: 1.(Loops, Viruses, etc.) 2. Search 3.RTFM |
|
| Back to top |
|
 |
laserman
Joined: 21 Jan 2008 Posts: 9
|
Posted: Wed Feb 20, 2008 11:21 am Post subject: |
|
|
| Thanks for the info, I am always trying to learn more about NC programming. I've seen some pieces cut on a waterjet, seems that they do a nice job. Btw, engunneer do you know anywhere I can get info on writing NC code "books, web, etc." Thanks for the help. |
|
| Back to top |
|
 |
engunneer
Joined: 30 Aug 2005 Posts: 6847 Location: Pacific Northwest, US
|
Posted: Wed Feb 20, 2008 4:40 pm Post subject: |
|
|
I can't help you too much on learning to write NC. The waterjet we sell does all that in the background. You as the user never sees the NC code. We can, however, import the NC code from your laser and turn it into a waterjet file directly, so you don't have to re-draw it. If you have simple questions about writing NC, you could continue this thread or start a new thread in general chat. I can help you with questions, but since each machine is different, only certain things you find on the web will apply to you. _________________
Unless otherwise noted, all code is untested.
Common Answers: 1.(Loops, Viruses, etc.) 2. Search 3.RTFM |
|
| Back to top |
|
 |
|
|
You can post new topics in this forum You can reply to topics in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|