gcode generator

draw grid on table (untested)

HTML

<div id="text"></div>

JavaScript

about = "generate gcode to control cnc machine to<br>" + 
	      "; draw 1in grid lines on table with marker<br>";


limitY = 15
limitX = 9
gcode='';

//RunGCode, true returns when movement complete.
function go(x,y){gcode+="G0 X" + x + " Y" + y+"<br>"; }
function  x(pos){gcode+="G0 X"+pos+"<br>"; }
function  y(pos){gcode+="G0 Y"+pos+"<br>"; }
function  z(pos){gcode+="G0 Z"+pos+"<br>"; }
function cmt(txt){gcode+="<br>; "+txt+"<br>";}

function drawRect(x0,y0,w,h){
	z(1);
	go(x0,y0);
	z(0);
	x(x0+w);
	y(y0+h);
	x(x0);
	y(y0);
	z(1);
}

cmt(about);
go(0,0); // home pos
z(0);    // marker down

i=0;
cmt("draw horz X lines");
while(1){ 
	x(limitX); //all way right
	i++
	if(i > limitY) break;
	y(i); //up to next line
	x(0);  //all way left
	i++;
	if(i > limitY) break;
	y(i); //up to next line
}

z(1);    //marker up..
go(0,0); //go home
z(0);    //marker down

i=0;
cmt("draw vertical Y lines");
while(1){
	y(limitY); //all way up
	i++
	if(i > limitX) break;
	x(i);   //right one
	y(0);  //all way down
	i++;
	if(i > limitX) break;
	x(i);   //right one
}

cmt("draw rectangle around entire perimiter");
drawRect(0,0,limitX,limitY);

cmt("complete goto center"); 
z(1)
go(limitX/2, limitY/2);

document.getElementById('text').innerHTML = gcode;