From 8820fe7cc7a44d92b493b4c81ce9e0bb70df2463 Mon Sep 17 00:00:00 2001 From: make-ing Date: Wed, 16 Mar 2016 17:24:26 +0100 Subject: [PATCH] fixed svg and gcode y-coodination problem and changed alot in the path conversion. --- .../plugins/svgtogcode/static/js/svg2gcode.js | 119 +++++++++++------- 1 file changed, 77 insertions(+), 42 deletions(-) diff --git a/src/octoprint/plugins/svgtogcode/static/js/svg2gcode.js b/src/octoprint/plugins/svgtogcode/static/js/svg2gcode.js index ab8aa4d6..60dc9fc5 100644 --- a/src/octoprint/plugins/svgtogcode/static/js/svg2gcode.js +++ b/src/octoprint/plugins/svgtogcode/static/js/svg2gcode.js @@ -14,6 +14,9 @@ Snap.plugin(function (Snap, Element, Paper, global) { gCodeList.push("G21\n"); gCodeList.push("G1F" + laserSpeed + "\n"); + svgWidth = this.paper.attr('viewBox').width; + svgHeight = this.paper.attr('viewBox').height; + var elem = this.selectAll("path"); for (var i = 0; i < elem.length; i++) { gCodeList.push(elem[i].pathStringToGCode(laserIntensity, pierceTime)); @@ -38,68 +41,100 @@ Snap.plugin(function (Snap, Element, Paper, global) { var yStr = ""; var arr = Snap.parsePathString(this.realPath); for (var i = 0; i < arr.length; i++) { - if (arr[i][0].toUpperCase() == "M") { + if (arr[i][0] == "M") { xStr = Math.round(arr[i][1] * 100) / 100; - yStr = Math.round(arr[i][2] * 100) / 100; - if (xStr != lastXstr || yStr != lastYstr) { - gcode.push("M3S0"); + yStr = Math.round((svgHeight - arr[i][2]) * 100) / 100; + gcode.push("M3S0"); + if (xStr != lastXstr && yStr != lastYstr) { gcode.push("G0X" + xStr + "Y" + yStr); - gcode.push("M3S" + laserIntensity); - if (pierceTime != 0) { - gcode.push("G4P" + pierceTime); - } - lastXstr = arr[i][1]; - lastYstr = arr[i][2]; + lastXstr = xStr; + lastYstr = yStr; + } else if (xStr != lastXstr) { + gcode.push("G0X" + xStr); + lastXstr = xStr; + } else if (yStr != lastYstr) { + gcode.push("G0Y" + yStr); + lastYstr = yStr; } - startP = [arr[i][1], arr[i][2]]; - lastP = [arr[i][1], arr[i][2]]; - } else if (arr[i][0].toUpperCase() == "L") { + gcode.push("M3S" + laserIntensity); + if (pierceTime != 0) { + gcode.push("G4P" + pierceTime); + } + startP = [arr[i][1], (svgHeight - arr[i][2])]; + lastP = startP; + } else if (arr[i][0] == "L") { xStr = Math.round(arr[i][1] * 100) / 100; - yStr = Math.round(arr[i][2] * 100) / 100; - if (xStr != lastXstr || yStr != lastYstr) { + yStr = Math.round((svgHeight - arr[i][2]) * 100) / 100; + if (xStr != lastXstr && yStr != lastYstr) { gcode.push("G1X" + xStr + "Y" + yStr); - lastP = [arr[i][1], arr[i][2]]; - } - } else if (arr[i][0].toUpperCase() == "H") { - gcode.push("H"); - } else if (arr[i][0].toUpperCase() == "V") { - gcode.push("V"); - } else if (arr[i][0].toUpperCase() == "C") { + lastXstr = xStr; + lastYstr = yStr; + } else if (xStr != lastXstr) { + gcode.push("G1X" + xStr); + lastXstr = xStr; + } else if (yStr != lastYstr) { + gcode.push("G1Y" + yStr); + lastYstr = yStr; + } + lastP = [arr[i][1], (svgHeight - arr[i][2])]; + } else if (arr[i][0] == "H") { + xStr = Math.round(arr[i][1] * 100) / 100; + gcode.push("G1X" + xStr); + lastXstr = xStr; + lastP = [arr[i][1], lastP[1]]; + } else if (arr[i][0] == "V") { + yStr = Math.round((svgHeight - arr[i][1]) * 100) / 100; + gcode.push("G1Y" + yStr); + lastYstr = yStr; + lastP = [lastP[0], (svgHeight - arr[i][1])]; + } else if (arr[i][0] == "C") { var x0 = lastP[0]; var y0 = lastP[1]; var x1 = arr[i][1]; - var y1 = arr[i][2]; + var y1 = (svgHeight - arr[i][2]); var x2 = arr[i][3]; - var y2 = arr[i][4]; + var y2 = (svgHeight - arr[i][4]); var x3 = arr[i][5]; - var y3 = arr[i][6]; - var tmp = Snap.path.getTotalLength("M" + lastP[0] + "," + lastP[1] + arr[i][0] + arr[i][1] + "," + arr[i][2] + "," + arr[i][3] + "," + arr[i][4] + "," + arr[i][5] + "," + arr[i][6]); + var y3 = (svgHeight - arr[i][6]); + var tmp = Snap.path.getTotalLength("M" + lastP[0] + "," + lastP[1] + "C" + x1 + "," + y1 + "," + x2 + "," + y2 + "," + x3 + "," + y3); var range = Math.round(tmp) * 10; for (var t = 1; t <= range; t++) { obj = Snap.path.findDotsAtSegment(x0, y0, x1, y1, x2, y2, x3, y3, t / range) xStr = Math.round(obj.x * 100) / 100; yStr = Math.round(obj.y * 100) / 100; - if (xStr != lastXstr || yStr != lastYstr) { - gcode.push("G1X" + xStr + "Y" + yStr); - } - lastXstr = xStr; - lastYstr = yStr; - } - lastP = [arr[i][5], arr[i][6]]; - } else if (arr[i][0].toUpperCase() == "Q") { - gcode.push("NOT_IMPLEMENTED"); - } else if (arr[i][0].toUpperCase() == "A") { - gcode.push("NOT_IMPLEMENTED"); - } else if (arr[i][0].toUpperCase() == "Z") { - if (lastP[0] != startP[0] || lastP[1] != startP[1]) { - xStr = Math.round(startP[0] * 100) / 100; - yStr = Math.round(startP[1] * 100) / 100; - if (xStr != lastXstr || yStr != lastYstr) { + if (xStr != lastXstr && yStr != lastYstr) { gcode.push("G1X" + xStr + "Y" + yStr); lastXstr = xStr; lastYstr = yStr; + } else if (xStr != lastXstr) { + gcode.push("G1X" + xStr); + lastXstr = xStr; + } else if (yStr != lastYstr) { + gcode.push("G1Y" + yStr); + lastYstr = yStr; } } + lastP = [x3, y3]; + } else if (arr[i][0] == "Q") { + // TODO implement Q path element + gcode.push("NOT_IMPLEMENTED"); + } else if (arr[i][0] == "A") { + // TODO implement A path element + gcode.push("NOT_IMPLEMENTED"); + } else if (arr[i][0] == "Z") { + xStr = Math.round(startP[0] * 100) / 100; + yStr = Math.round(startP[1] * 100) / 100; + if (xStr != lastXstr && yStr != lastYstr) { + gcode.push("G1X" + xStr + "Y" + yStr); + lastXstr = xStr; + lastYstr = yStr; + } else if (xStr != lastXstr) { + gcode.push("G1X" + xStr); + lastXstr = xStr; + } else if (yStr != lastYstr) { + gcode.push("G1Y" + yStr); + lastYstr = yStr; + } } } //console.log(gcode.join("\n").length)