Locate a string

Started by golijoe, August 20, 2019, 06:45:15 AM

Previous topic - Next topic

golijoe

I have a script that I wrote many years ago that would convert all Y moves into A moves in a G-Code program. Back then I came up with the same problem that I was unable to locate the Y with all the numbers belonging to it because they are always different so I Used Winbatch and TextPad regular expressions to locate the information to make the change. Now I want to somehow just use WinBatch. So has anyone tried something like this? Here is a sample G-Code.

Y.564F16.5
X-37.7825
Y-.614
G00Z.2
X-37.3425Y-.989
G01Z-.105F20.
Y.564F16.5
X-37.7825
Y-.614
Z-.0115F50.
X-37.4425Y0.
X-26.5875F16.5
G00Z.2
X-25.8425Y-.989
G01Z-.062F10.
Y.564F16.5
X-26.2825
Y-.564
G00Z.2
X-25.8425Y-.989
G01Z-.105F16.5
Y.564F16.5
X-26.2825
Y-.564
G00Z.5M09
G91Z0


jmburton2001

Question -> Are you trying to locate the lines that start with "Y" and then change the "Y" to an "A" leaving everything else intact?

Examples:

Change Y.564F16.5    to    A.564F16.5

or

Change Y-.614    to    A-.614

golijoe

Change Y-.614    to    A-.614

But I have to take the Y-.614 and convert it to A-74.86
I'm having trouble separating the Y move from the rest of the code.

jmburton2001

Are you referring to CNC or milling codes?

If so, is there a conversion calculation to convert a Y position (Y-.614) to an A position (A-74.86)?

I notice that the associated number changed when you gave your example.

golijoe

Yes that is what I'm trying to do.

jmburton2001

It would be trivial to extract the lines that begin with "Y"... The issue is what calculation converts the number portion?

How do you get from -.614 to -74.86?

golijoe

What I'm doing is extracting the Y movement in certain parts of the program that I select where we need the A axis to move and the Y is stationary. We've been doing this for probably 20 years now but I would select the section of the program that I want to convert and paste it into TetxPad. I then used WinBatch with the following code to go thru TextPad to find each Y movement. Then do the math for that movement to convert it to an A movement in degrees and replace it. It works but it's slow when you have hundreds of line of code to go thru plus you can't do anything on your computer while it's running. I'm just looking for something that I can find just the Y with the numbers so that I don't have to use TextPad anymore.

ClipPut("")
WinActivate("~TextPad")
SendKey("^{HOME}")
SendKey("^{a}")
SendKey("^{c}")
fn = ClipGet( )
SendKey("^{HOME}")
nt = ItemCount(fn,"Y")
nx=0
de=2
SendKey("^{HOME}")
While 1
Decimals(10)
nx=nx + 1


SendKey("{F8}")
ClipPut("")
ClipPut("Y\([0-9*]+\).\([0-9*]+\)") ; Finds the first Y movement

SendKey("^{V}")

SendKey("{TAB 6}")

SendKey("{ENTER}")
;
SendKey("{TAB 4}")
;
SendKey("{ENTER}")
ClipPut("")

SendKey("^{C}")
a = ClipGet( ) ; Now I take what is in the clipboard extract and do the math to convert to A movement in degrees

jmburton2001

QuoteThen do the math for that movement to convert it to an A movement in degrees and replace it.

Without knowing the math involved, the following script will stop at any line that starts with "Y" and allow you to manually replace it with a value of your choosing (like A-74.86).

I used the data you provided in your first post as "input.txt". The file "output.txt" will be your modified data.

Code (winbatch) Select
InFile = "input.txt"
OutFile = "output.txt"
InputFile = FileOpen(InFile, "READ")
OutputFile = FileOpen(OutFile, "WRITE")
StringOut = "Y"
while @TRUE
   TextLine = FileRead(InputFile)
   IsItY = StrSub (TextLine, 1, 1)
   If IsItY == StringOut Then TextLine = AskLine ("Enter Value", "Change %TextLine% to:", TextLine,0)
      ; Or have it do some math functions here to convert TextLine to your A value
   If TextLine == "*EOF*" Then Break
   FileWrite (OutputFile, TextLine)
end while
FileClose(InputFile)
FileClose(OutputFile)

golijoe

Thanks but I'm trying to find a way to just find the Y with the numbers not the whole line.

golijoe

When you have hundreds of lines you don't want to do it manually because then you have a chance of errors.

JTaylor

jmburton wasn't suggesting you do them manually.  He just did that since you wouldn't say how the conversion was done.   From the data you offered the only thing in a line is the letters and numbers.  Is there more to the line than what you posted?  If not, and you want all the non-numbers gone then take a look at StrClean().   If there is you will need to provide enough info for someone to help you, as jmburton keeps requesting.

Jim

jmburton2001

Thanks JTaylor!

Yesterday I had pretty much the whole day to help this person. Unfortunately he didn't provide any useful information to help in that regard. Even his last two responses yielded no additional useful information.

If he would provide the raw file he's working with and the formula he uses to convert them, I could put together a script that would plow through 20,000 lines of the document (with the conversions and output) in less than a minute and he only has "hundreds" of lines! I hate to see a "SendKey" script when there are faster, more accurate ways to do simple jobs like this.

Sadly real life has come crashing back so I don't have time to work on it today.

PS - His "Thanks but..." post is a bit disheartening. If he would provide useful information, finding the "Y with the numbers" is trivial too.

td

Welcome to the wonderful world of Tech Support.   All you can do is try your best to lead a horse to water but you...   
"No one who sees a peregrine falcon fly can ever forget the beauty and thrill of that flight."
  - Dr. Tom Cade

golijoe

All I was asking is how can I find the Y with the numbers behind it. Not the X, G or F. I can write the rest and do the math once I have it. I just haven't found a way to efficiently locate it that's why we still use the old script with the sendkeys in it. I wasn't asking you to write it, just show me a better way of finding it. The math is not the issue. Here is the current script we are using.

;ARGUMENTS FOR MACRO CONVERTION OF Y MOVE TO A MOVEMENT
;This will use regular & replacement expressions through Texpad

disc=StrCat("For Y-(negative) moves change all Y's to positive now.Then change the A's back to negative after you have converted them.")
dia=IniReadPvt("con", "dia", "4.0", "C:\convertYtoA.dgn")
offs=IniReadPvt("con", "offs", "2.0", "C:\convertYtoA.dgn")

MyDialogFormat=`WWWDLGED,6.1`

MyDialogCaption=`Convert Y to A`
MyDialogX=-01
MyDialogY=-01
MyDialogWidth=254
MyDialogHeight=077
MyDialogNumControls=007
MyDialogProcedure=`DEFAULT`
MyDialogFont=`DEFAULT`
MyDialogTextColor=`DEFAULT`
MyDialogBackground=`DEFAULT,255|128|64`
MyDialogConfig=0

MyDialog001=`003,057,036,012,PUSHBUTTON,DEFAULT,"OK",1,1,32,DEFAULT,DEFAULT,DEFAULT`
MyDialog002=`045,057,036,012,PUSHBUTTON,DEFAULT,"Cancel",0,2,DEFAULT,DEFAULT,DEFAULT,DEFAULT`
MyDialog003=`015,015,062,012,EDITBOX,dia,DEFAULT,DEFAULT,3,DEFAULT,DEFAULT,DEFAULT,DEFAULT`
MyDialog004=`001,007,092,012,STATICTEXT,DEFAULT,"OD or ID of part layout",DEFAULT,4,512,DEFAULT,DEFAULT,DEFAULT`
MyDialog005=`015,037,062,012,EDITBOX,offs,DEFAULT,DEFAULT,5,DEFAULT,DEFAULT,DEFAULT,DEFAULT`
MyDialog006=`001,029,092,012,STATICTEXT,DEFAULT,"Offset amount of tool",DEFAULT,6,512,DEFAULT,DEFAULT,DEFAULT`
MyDialog007=`111,007,126,064,VARYTEXT,disc,DEFAULT,DEFAULT,7,512,DEFAULT,DEFAULT,DEFAULT`

ButtonPushed=Dialog("MyDialog")




offs =0%offs%
IniWritePvt("con", "dia", "%dia%", "C:\convertYtoA.dgn")
IniWritePvt("con", "offs", "%offs%", "C:\convertYtoA.dgn")

ClipPut("")
WinActivate("~TextPad")
SendKey("^{HOME}")
SendKey("^{a}")
SendKey("^{c}")
fn = ClipGet( )
SendKey("^{HOME}")
nt = ItemCount(fn,"Y")
nx=0
de=2
SendKey("^{HOME}")
While 1
   Decimals(10)
   nx=nx + 1
   SendKey("{F8}")
   ClipPut("")
   ClipPut("Y\([0-9*]+\).\([0-9*]+\)")
   SendKey("^{V}")
   SendKey("{TAB 6}")
   SendKey("{ENTER}")
   SendKey("{TAB 4}")
   SendKey("{ENTER}")
   ClipPut("")
   SendKey("^{C}")
   a = ClipGet( )
   rslt = StrCnt(a, "-", 1, -1, 0)
   yy = StrClean(a, "Y", "", @FALSE, 1)
   yy = StrClean(yy, "-", "", @FALSE, 1)
   
   yt = %yy% + %offs%
   yx = (57.296/(%dia%/2))
   yt = %yx% * %yt%
   yt = yt + 0.0
   if rslt==1
   yt="-"yt
   endif
   Decimals(3)
   SendKey("{F8}")
   SendKey("{TAB 1}")
   ClipPut("")
   ClipPut("A%yt%")
   SendKey("^{V}")
   SendKey("{TAB 6}")
   SendKey("{ENTER}")
   SendKey("{ESC}")
   SendKey("{ESC}")
   if nx == nt then break
endwhile
exit

golijoe

Here is probably a better example of what I'm doing.
BEFORE                  AFTER
X-1.575 F7.9         X-1.575 F7.9
X-1.7237 F22.9         X-1.7237 F22.9
X-1.7672 Y.8796      X-1.7672 A35.029
X-1.8105 Y.8853      X-1.8105 A35.256
X-1.8531 Y.8947      X-1.8531 A35.630
X-1.8947 Y.9078      X-1.8947 A36.152
X-1.935 Y.9245         X-1.935 A36.817
X-1.9737 Y.9447      X-1.9737 A37.621
X-2.0104 Y.9681      X-2.0104 A38.553
X-2.0451 Y.9946      X-2.0451 A39.608
X-2.0772 Y1.0241      X-2.0772 A40.783
X-4.8 Y3.7469         X-4.8 A149.215
X-4.4 Y3.3469 F31.2      X-4.4 A133.285 F31.2
Y.8777                 A34.953
X-4.0122                 X-4.0122
Y2.9591                 A117.842
X-3.6252 Y2.5721      X-3.6252 A102.430
Y.8777                 A34.953
X-3.2382                 X-3.2382
Y2.1851                 A87.018
X-2.8512 Y1.7981           X-2.8512 A71.607
Y.8777                 A34.953
X-2.4642                 X-2.4642
Y1.4111                 A56.195
X-2.0772 Y1.0241           X-2.0772 A40.783
Y.8777                 A34.953
G00 Z.2                 G00 Z.2

JTaylor

That is helpful and the data appears to be space delimited in the last post but not in the first so that makes a difference...assuming that is the case.   Assuming it is...just off the top of my head and I am sure it can be done a little more efficiently but maybe something like...

Code (winbatch) Select

........
line = FileRead(or)
new_line = ""

For x = 1 to ItemCount(line," ")
   code = ItemExtract(x,line," ")
   If StrSub(code,1,1) == "Y" Then
          code = StrSub(code,2,StrLen(code))
      ....DO CONVERSION
          code = "A":code
          new_line = new_line:" ":code
   Else
      new_line = new_line:" ":code
   EndIf
Next
   new_line = StrTrim(new_line)
   FileWrite(ow,new_line)
....................

golijoe