Variable replacement

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
Frak89
Posts: 4
Joined: 06 May 2014 07:06

Variable replacement

#1 Post by Frak89 » 06 May 2014 07:27

Hello, I am trying to create a script which read from a file for example composition.gcode and I want to extract values that starts with X, Y and Z.
For example in this file there is a structure like this:

Code: Select all

G21 ; set units to millimeters
M104 S200 ; set temperature
G28 ; home all axes
G1 Z5 F5000 ; lift nozzle
M109 S200 ; wait for temperature to be reached
G90 ; use absolute coordinates
G92 E0
M82 ; use absolute distances for extrusion
G1 F1800.000 E-1.00000
G92 E0
G1 Z15.350 F7800.000
G1 X152.500 Y152.500 F7800.000
G1 E1.00000 F1800.000
G1 X347.500 Y152.500 E18.37972 F378.000
G1 X347.500 Y347.500 E35.75944
...
G1 X343.831 Y156.169 E733.08114
G1 F1800.000 E732.08114
G92 E0
G1 Z20.350 F7800.000
G1 X347.500 Y152.500 F7800.000
G1 E1.00000 F1800.000
G1 X347.500 Y347.500 E249.28171 F1260
G1 X152.500 Y347.500 E497.56342
...


I want to delete or avoid the lines with ";" and replace in sequence the values for each X, Y and Z like this:

Code: Select all

CONST robtarget P1:=[[15.350,152.500,15.350],.....
CONST robtarget P2:=[[347.500,152.500,15.350],.....
CONST robtarget P3:=[[347.500,347.500,15.350],.....
...
CONST robtarget Pm:=[[347.500,152.500,20.350],......
...
CONST robtarget Pn:=[[X,Y,Z],.....
...


but I tried a lot of attempts, but no result made... Can someone help me?

I also have to generate 2 files (robtarget.txt and movel.txt) that will be included in Mod_%modnum%.mod to generate ABB rapid instructions...!
Like this:

Code: Select all

MODULE Mod_1234 
 
CONST robtarget P1:=[[x,y,z],.....;
...

PROC Routine()
ConfL\Off;
MoveL P1,v60,z5,mytool\Wobj:=mywobj;
...
ENDPROC

PROC MainMove()
Routine;
ENDPROC

ENDMODULE


The portion of the code I used is:

Code: Select all

@echo off
set nomefile=composition.gcode
set /a x=0
set /a riga=0
set modnum=1234
set wobj=mywobj
set tool=mytool
set vel=v60

IF EXIST Mod_%modnum%.mod DEL /f Mod_%modnum%.mod
IF EXIST movel.txt DEL /f movel.txt
IF EXIST robtarget.txt DEL /f robtarget.txt
IF EXIST xresult.txt DEL /f xresult.txt
IF EXIST yresult.txt DEL /f yresult.txt
IF EXIST zresult.txt DEL /f zresult.txt

findstr /n "Z" "%nomefile%" >> zresult.txt
findstr /n "X" "%nomefile%" >> xresult.txt
findstr /n "Y" "%nomefile%" >> yresult.txt

  set xval=%xvalnew%
  set yval=%yvalnew%
  set zval=%zvalnew%

:while
if %x% lss 20 (
  set /a riga+=1
echo CONST robtarget P%x%:=[[%xval%,%yval%,%zval%],[0.00223,-0.994008,-0.109258,-0.002251],[1,0,0,0],[9E+09,9E+09,9E+09,9E+09,9E+09,9E+09]]; >> robtarget.txt
echo MoveL P%x%,%vel%,z5,%tool%\Wobj:=%wobj%; >> movel.txt
::  pause>nul
  set /a x+=1
  set /a riga+=1
  goto :while
)

echo MODULE Mod_%modnum% >> Mod_%modnum%.mod
echo. >> Mod_%modnum%.mod
copy robtarget.txt >> Mod_%modnum%.mod
:: del robtarget.txt
echo PROC Routine() >> Mod_%modnum%.mod
echo ConfL\Off; >> Mod_%modnum%.mod
echo ENDPROC>> Mod_%modnum%.mod
echo.>> Mod_%modnum%.mod
echo PROC MainMove()>> Mod_%modnum%.mod
echo Routine;>> Mod_%modnum%.mod
echo ENDPROC>> Mod_%modnum%.mod
echo.>> Mod_%modnum%.mod
echo ENDMODULE>> Mod_%modnum%.mod


Thanks and have a good time! :)

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Variable replacement

#2 Post by foxidrive » 06 May 2014 07:40


penpen
Expert
Posts: 2009
Joined: 23 Jun 2013 06:15
Location: Germany

Re: Variable replacement

#3 Post by penpen » 06 May 2014 08:25

Frak89 wrote:

Code: Select all

(...)
G1 Z5 F5000 ; lift nozzle
(...)
I want to delete or avoid the lines with ";" and replace in sequence the values for each X, Y and Z like this:
I'm not sure what you want to do, as for example the fourth line contains a Z value ("Z5"), too (*).
Do you want to process only lines starting with "G1".
What to do, in cases where for example X, and Y are there, but no Z value.
Should the other values (E, F) be ignored?

I assume instead of this line

Code: Select all

copy robtarget.txt >> Mod_%modnum%.mod
you wanted to do something like that:

Code: Select all

>nul copy /B "Mod_%modnum%.mod" + /B robtarget.txt /B "Mod_%modnum%.mod"


(*): So do you wnat to process "this" "or that" (or something different?):

Code: Select all

@echo off
set "nomefile=composition.gcode"

echo this
for /F "tokens=1*" %%a in ('findstr /LV ";" "%nomefile%" ^| findstr /L "X Y Z" ') do (
   echo #%%a   # %%b
)

echo(
echo/or that
for /F "delims=;" %%A in ('findstr /L "X Y Z" "%nomefile%"') do (
   for /F "tokens=1*" %%a in ("%%A") do (
      echo #%%a   # %%b
   )
)

pause

penpen

Edit: Added (*).

Frak89
Posts: 4
Joined: 06 May 2014 07:06

Re: Variable replacement

#4 Post by Frak89 » 06 May 2014 08:43

penpen wrote:
Frak89 wrote:

Code: Select all

(...)
G1 Z5 F5000 ; lift nozzle
(...)
I want to delete or avoid the lines with ";" and replace in sequence the values for each X, Y and Z like this:
I'm not sure what you want to do, as for example the fourth line contains a Z value ("Z5"), too.
Do you want to process only lines starting with "G1".
What to do, in cases where for example X, and Y are there, but no Z value.
Should the other values (E, F) be ignored?

I assume instead of this line

Code: Select all

copy robtarget.txt >> Mod_%modnum%.mod
you wanted to do something like that:

Code: Select all

>nul copy /B "Mod_%modnum%.mod" + /B robtarget.txt /B "Mod_%modnum%.mod"

penpen


Thanks for the reply!
Yes, I would like to skip the lines with ";" also like the line where is shown the Z5 variable...
I want the code store the Z value, then at the next line store X and Y variables to put into the "robtarget point" and then do it again the procedure maintaining the Z value fixed until is find the next Z value, but continues to change the X and Y to store in the new point.
Like:

... G1 Z5 F5000 ; lift nozzle -- skip storing of the variable, because the point is too close to 0 and is not necessary and there is ";"
... Z1 -- Z found and store
... X1 Y1 -- X and Y found and store, then create robtarget P1:=[[X1,Y1,Z1],...
... X2 Y2 -- X and Y found and store, then create robtarget P2:=[[X2,Y2,Z1],...
... Z2 -- new Z found, then store temporarily without creation of new point until it will be found the new X and Y
... X3 Y3 -- X and Y find and store, then create robtarget P3:=[[X3,Y3,Z2],...
etc...

that's my expectation of this program I would like to create, and I want to store variables X and Y in the cases which there is E and F, but not to store E and F, so they must be ignored.

Frak89
Posts: 4
Joined: 06 May 2014 07:06

Re: Variable replacement

#5 Post by Frak89 » 06 May 2014 09:35

penpen wrote:penpen

Yes, but I don't want to have "G1" and "E" or "F", just only X, Y and Z. Store the numbers following by the letter of the variable and put into the robtarget Pn:=[[X,Y,Z],...;
And I want to have

Z1 -- store Z1
X1 Y1 -- store them, create robtarget P1 and store variables into "CONST robtarget P1:=[[X1,Y1,Z1],...;"
X2 Y2 -- store them, create robtarget P2 and store variables into "CONST robtarget P2:=[[X2,Y2,Z1],...;" Z1 remains because we haven't find new value yet
...
Z2 -- store Z2
X3 Y3 -- store them, create robtarget P3 and store variables into "CONST robtarget P3:=[[X3,Y3,Z2],...;"

and so on...
Maybe I must use an IF condition in order to create a new robtarget before storing the variables.
So I am supposed to have Z stored, then find X and Y, then create the new robtarget with these 3 variables inside.

penpen
Expert
Posts: 2009
Joined: 23 Jun 2013 06:15
Location: Germany

Re: Variable replacement

#6 Post by penpen » 06 May 2014 10:23

If i have understood it right, then this should produce the "CONST lines":

Code: Select all

@echo off
cls
setlocal enableDelayedExpansion
set "nomefile=composition.gcode"
set /A "xval=yval=zval=x=0"

for /F "tokens=1*" %%a in ('findstr /LV ";" "%nomefile%" ^| findstr /L "X Y Z" ') do (
   set "set_X="
   set "set_Y="
   for %%c in (%%b) do if not "%%c" == "" (
      set "store=%%c"
      set "!store:~0,1!val=!store:~1!"
      set "set_!store:~0,1!=!store:~0,1!"
   )
   if not "" == "!set_X!!set_Y!" (
      set /A "x+=1"
rem      >> "robtarget.txt" echo CONST robtarget P!x!:=[[!xval!,!yval!,!zval!],[0.00223,-0.994008,-0.109258,-0.002251],[1,0,0,0],[9E+09,9E+09,9E+09,9E+09,9E+09,9E+09]];
      echo CONST robtarget P!x!:=[[!xval!,!yval!,!zval!],[0.00223,-0.994008,-0.109258,-0.002251],[1,0,0,0],[9E+09,9E+09,9E+09,9E+09,9E+09,9E+09]];
   )
)

endlocal
(Actual only echoing to screen for debugging.)

penpen

Edit: Removed a flaw.
Edit2: Changed to use the OP variable's name.

Aacini
Expert
Posts: 1932
Joined: 06 Dec 2011 22:15
Location: México City, México
Contact:

Re: Variable replacement

#7 Post by Aacini » 06 May 2014 10:37

There are multiple unclear points in this request. For example: What happen if X/Y appear before Z? What happen if does not appear both X and Y in a line? Anyway, this is a first version:

Code: Select all

@echo off
setlocal EnableDelayedExpansion

set vars=XYZ

set "X=" & set "Y="
set i=0
for /F "delims=" %%a in ('findstr /V ";" composition.gcode') do (
   for %%b in (%%a) do (
      set var=%%b
      for /F %%v in ("!var:~0,1!") do if "!vars:%%v=!" neq "%vars%" set "%%v=!var:~1!"
   )
   if defined X (
      set /A i+=1
      echo CONST robtarget P!i!:=[[!X!,!Y!,!Z!],.....
      set "X=" & set "Y="
   )
)


Output:

Code: Select all

CONST robtarget P1:=[[152.500,152.500,15.350],.....
CONST robtarget P2:=[[347.500,152.500,15.350],.....
CONST robtarget P3:=[[347.500,347.500,15.350],.....
CONST robtarget P4:=[[343.831,156.169,15.350],.....
CONST robtarget P5:=[[347.500,152.500,20.350],.....
CONST robtarget P6:=[[347.500,347.500,20.350],.....
CONST robtarget P7:=[[152.500,347.500,20.350],.....


Antonio

Frak89
Posts: 4
Joined: 06 May 2014 07:06

Re: Variable replacement

#8 Post by Frak89 » 07 May 2014 03:12

Aacini wrote:Antonio

penpen wrote:penpen


Thank you very much for both! :D
Now the code is fully functional as I expected..
;)

Post Reply