Skip to content

Instantly share code, notes, and snippets.

@biojazzard
Created October 31, 2012 07:52
Show Gist options
  • Select an option

  • Save biojazzard/3985703 to your computer and use it in GitHub Desktop.

Select an option

Save biojazzard/3985703 to your computer and use it in GitHub Desktop.
Librería JS para gcode
/* Librería javascript */
/* Tal y como se encuentra aqui: http://www.unfocusedbrain.com/projects/2009/js-g-code/jstogcode.html */
/* License GNU GPL V3*/
// functions
// function gcodeHeader()
// function gcodeFooter()
// function drawCircle(x,y,z,radius)
// function drawBox(x1,y1,z1,x2,y2,z2)
// function drawLine(x1,y1,z1,x2,y2,z2)
// function fd(number)
// function number_format( number, decimals, dec_point, thousands_sep )
// function drawCircleDeep(x,y,z,radius,depth,step)
// function cartesiantopolar(x,y)
// function polartocartesian(radius,degree)
// function roundNumber(num, dec)
// function drawArc(x,y,z,radius,start,stop)
// function calculateArc(x,y,z,radius,start,stop)
// function drawPoint(x,y,z);
// definitions
safeZ=0.0;
feedRate=1; // ipm
myPi=3.141592653589793238462643383279;
function gcodeHeader()
{
var currentTime=new Date();
var dateString="";
dateString+=currentTime.getHours()+":";
dateString+=currentTime.getMinutes()+" ";
dateString+=currentTime.getDay()+"-";
dateString+=currentTime.getMonth()+"-";
dateString+=currentTime.getFullYear()+" ";
temps="";
temps+="(Generated by gcodelib.js "+dateString+" unfocusedbrain.com)\n";
temps+="G90\n"; // absoulute mode
temps+="G20\n"; // inches
temps+="G61 (Exact Path Mode)\n"; // keeps it from rounding corners
temps+="F"+feedRate+"\n";
return temps;
}
function gcodeFooter()
{
temps="M2\n";
return temps;
}
function drawPoint(x,y,z)
{
temps="";
temps+="(point x="+x+" y="+y+" z="+z+")\n";
temps+="G0 X"+fd(x)+" Y"+fd(y)+" (rapid to start)\n";
temps+="G1 Z"+fd(z)+"\n";
temps+="G1 X"+fd(x)+" Y"+fd(y)+" Z"+fd(z)+"\n";
temps+="G0 Z"+fd(safeZ)+" (safe z)\n";
temps+="(end of point)\n\n";
//temps+="\n";
return temps;
}
function drawCircle(x,y,z,radius)
{
temps="";
ofx=x-radius;
//ofy=y-radius;
//temps= "G17 (xyplane)\n";
temps+="(circle x="+x+" y="+y+" z="+z+" radius="+radius+")\n";
temps+="G0 X"+fd(ofx)+" Y"+fd(y)+" (rapid to start)\n";
temps+="G1 Z"+fd(z)+"\n";
temps+="G17 G2 X"+fd(ofx)+" Y"+fd(y)+" I"+fd(radius)+" J"+fd(0.00)+" Z"+fd(z)+"\n";
temps+="G0 Z"+fd(safeZ)+" (safe z)\n";
temps+="(end of circle)\n\n";
//temps+="\n";
return temps;
}
function drawBox(x1,y1,z1,x2,y2,z2)
{
temps="";
temps+="(box x1="+x1+" y1="+y1+" z1="+z1+" x2="+x2+" y2="+y2+" z2="+z2+" )\n";
temps+="G0 X"+fd(x1)+" Y"+fd(y1)+" Z"+fd(safeZ)+"(rapid to start)\n";
temps+="G1 Z"+fd(z1)+"\n";
temps+="G1 X"+fd(x1)+" Y"+fd(y1)+" Z"+fd(z1)+"\n";
temps+="G1 Y"+fd(y2)+"\n";
temps+="G1 X"+fd(x2)+"\n";
temps+="G1 Y"+fd(y1)+"\n";
temps+="G1 X"+fd(x1)+"\n";
temps+="G0 Z"+fd(safeZ)+" (safe z)\n";
temps+="(end of line)\n\n";
//temps+="\n";
return temps;
}
function drawLine(x1,y1,z1,x2,y2,z2)
{
temps="";
temps+="(line x1="+x1+" y1="+y1+" z1="+z1+" x2="+x2+" y2="+y2+" z2="+z2+" )\n";
temps+="G0 X"+fd(x1)+" Y"+fd(y1)+" Z"+fd(safeZ)+"(rapid to start)\n";
temps+="G1 Z"+fd(z1)+"\n";
temps+="G1 X"+fd(x1)+" Y"+fd(y1)+" Z"+fd(z1)+" \n";
temps+="G1 X"+fd(x2)+" Y"+fd(y2)+" Z"+fd(z2)+" \n";
temps+="G0 Z"+fd(safeZ)+" (safe z)\n";
temps+="(end of line)\n\n";
//temps+="\n";
return temps;
}
function fd(number)
{
return number_format(number,6,".","");
}
function number_format( number, decimals, dec_point, thousands_sep )
{
// http://kevin.vanzonneveld.net
// + original by: Jonas Raoni Soares Silva (http://www.jsfromhell.com)
// + improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
// + bugfix by: Michael White (http://crestidg.com)
// + bugfix by: Benjamin Lupton
// + bugfix by: Allan Jensen (http://www.winternet.no)
// + revised by: Jonas Raoni Soares Silva (http://www.jsfromhell.com)
// * example 1: number_format(1234.5678, 2, '.', '');
// * returns 1: 1234.57
var n = number, c = isNaN(decimals = Math.abs(decimals)) ? 2 : decimals;
var d = dec_point == undefined ? "," : dec_point;
var t = thousands_sep == undefined ? "." : thousands_sep, s = n < 0 ? "-" : "";
var i = parseInt(n = Math.abs(+n || 0).toFixed(c)) + "", j = (j = i.length) > 3 ? j % 3 : 0;
return s + (j ? i.substr(0, j) + t : "") + i.substr(j).replace(/(\d{3})(?=\d)/g, "$1" + t) + (c ? d + Math.abs(n - i).toFixed(c).slice(2) : "");
}
function drawCircleDeep(x,y,z,radius,depth,step)
{
temps="";
lastpass=NaN;
for (lop=z; lop>=depth; lop=lop+step)
{
temps+=drawCircle(x,y,lop,radius);
lastpass=lop;
}
if (lop<depth) // just in case its not an even multiple
{
temps+=drawCircle(x,y,depth,radius);
}
return temps;
}
function cartesiantopolar(x,y)
{
//myPi=3.14159
//temp=Math.pow(x,2)+Math.pow(y,2);
r=Math.sqrt( (x*x)+(y*y) ); //(x**2)+(y**2) );
t=Math.atan(y/x);
deg=(180*t)/Math.PI;
obj=new Object;
obj.r=r;
obj.t=t;
obj.deg=deg;
return obj;
}
function polartocartesian(radius,degree)
{
//myPi=3.14159
//double x;
//double y;
//radius.to_f;
//radians=(myPi * degree) / 180.00;
radians=(Math.PI * degree) / 180.00;
y=radius * Math.cos( radians );
x=radius * Math.sin( radians );
x=roundNumber(x,6);
y=roundNumber(y,6);
obj=new Object;
obj.x=x;
obj.y=y;
return obj;
}
function roundNumber(num, dec)
{
var result = Math.round(num*Math.pow(10,dec))/Math.pow(10,dec);
return result;
}
function calculateArc(x,y,z,radius,start,stop)
{
p1=polartocartesian(radius,start);
p2=polartocartesian(radius,stop);
if (0)
{
temps+="point 1 x="+p1.x+" y="+p1.y+"\n";
temps+="point 2 x="+p2.x+" y="+p2.y+"\n";
}
// translate the points
obj=new Object;
obj.p1x=p1.x+x;
obj.p1y=p1.y+y;
obj.p2x=p2.x+x;
obj.p2y=p2.y+y;
return obj;
}
function drawArc(x,y,z,radius,start,stop)
{
//G0 X0.0000 Y1.0000 Z0.5000
//G0 Z-0.2500
//G2 X1.0000 Y0.0000 I0.0000 J-1.0000 (----go in an arc from X0.0, Y1.0 to X1.0 Y0.0, with the center of the arc at //X0.0,
temps="";
p1=polartocartesian(radius,start);
p2=polartocartesian(radius,stop);
if (0)
{
temps+="point 1 x="+p1.x+" y="+p1.y+"\n";
temps+="point 2 x="+p2.x+" y="+p2.y+"\n";
}
i=-(p1.x);
j=-(p1.y);
// translate the points
p1.x=p1.x+x;
p1.y=p1.y+y;
p2.x=p2.x+x;
p2.y=p2.y+y;
//i=i+x;
//j=j+x;
temps+="(arc center x="+x+" y="+y+" radius="+radius+" start="+start+" stop="+stop+")\n";
temps+="G0 X"+fd(p1.x)+" Y"+fd(p1.y)+" Z"+fd(safeZ)+" (rapid to start)\n";
temps+="G0 Z"+fd(z)+"\n";
temps+="G2 X"+fd(p2.x)+" Y"+fd(p2.y)+" Z"+z+" I"+fd(i)+" J"+fd(j)+"\n";
//temps+="XX X1.0000 Y0.0000 Z-0.25 I0.0000 J-1.0000 \n";
temps+="G0 Z"+fd(safeZ)+" (safe z)\n";
temps+="(end of arc)\n\n";
return temps;
}
/*
Copyright 2009 © James Delaney
This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
You should have received a copy of the GNU General Public License along with this program. If not, see http://www.gnu.org/licenses/.
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment