Skip to content

Instantly share code, notes, and snippets.

@tmpvar
Created February 10, 2012 23:26
Show Gist options
  • Save tmpvar/1793963 to your computer and use it in GitHub Desktop.
Save tmpvar/1793963 to your computer and use it in GitHub Desktop.
stupid easy line based svg to gcode converter
// The intention here is that you load up the svg in a browser and run the following
// in the console
var
s = 1,//-0.35,
cz = 144, // cut z
sz = 142, // safe z
materialWidth = 6, // in mm
passes = 3,
passWidth = materialWidth/passes,
feedRate = 800,
seekRate = 900,
lines = document.getElementsByTagName("line"),
scale=function(val, amt) {
return val * amt
},
gcode = [
'G90',
'G1 Z' + sz,
'G82',
'M4'
],
i, l,
rects = document.getElementsByTagName('rect');
for (i =0, l = lines.length; i<l; i++) {
var line = lines[i];
for (var p = passWidth; p<=materialWidth; p+=passWidth) {
gcode.push(['G1',
'X' + scale(line.getAttribute('x1'), s),
'Y' + scale(line.getAttribute('y1'), s),
'F' + seekRate
].join(' '));
gcode.push(['G1',
'Z' + (cz + p),
'F' + '200'
].join(' '));
gcode.push(['G1',
'X' + scale(line.getAttribute('x2'), s),
'Y' + scale(line.getAttribute('y2'), s),
'F' + feedRate
].join(' '));
gcode.push(['G1',
'Z' + sz,
'F' + '300'
].join(' '));
}
}
for (i =0, l = rects.length; i<l; i++) {
var
rect = rects[i],
x = parseFloat(rect.getAttribute('x')),
y = parseFloat(rect.getAttribute('y')),
width = parseFloat(rect.getAttribute('width')),
height = parseFloat(rect.getAttribute('height'));
gcode.push(['G1',
'X' + scale(x, s),
'Y' + scale(y, s),
'F' + seekRate
].join(' '));
for (var p = passWidth; p<=materialWidth; p+=passWidth) {
gcode.push(['G1',
'Z' + (cz + passWidth),
'F' + '200'
].join(' '));
// top right
gcode.push(['G1',
'X' + scale(width + x, s),
'Y' + scale(y, s),
'F' + feedRate
].join(' '));
// bottom right
gcode.push(['G1',
'X' + scale(width+x, s),
'Y' + scale(height+y, s),
'F' + feedRate
].join(' '));
// bottom left
gcode.push(['G1',
'X' + scale(x, s),
'Y' + scale(height+y, s),
'F' + feedRate
].join(' '));
// top left
gcode.push(['G1',
'X' + scale(x, s),
'Y' + scale(y, s),
'F' + feedRate
].join(' '));
}
gcode.push(['G1',
'Z' + sz,
'F' + '200'
].join(' '));
}
gcode.push('G4 P1');
gcode.push('M5');
gcode.push('G1 Z0 F300');
gcode.push('G1 X0 Y0 F800');
console.log(gcode.join('\n'))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment