add preliminary svg import

This commit is contained in:
Stewart Allen 2019-09-17 08:36:20 -04:00
commit c51444d631
3 changed files with 55 additions and 10 deletions

View file

@ -57,6 +57,36 @@ var gs_kiri_print = exports;
PRO.addPrintPoints = addPrintPoints;
PRO.poly2polyDepthFirstEmit = poly2polyDepthFirstEmit;
PRO.parseSVG = function(code, offset) {
let scope = this,
output = scope.output = [],
bounds = scope.bounds = {
max: { x:-Infinity, y:-Infinity, z:-Infinity},
min: { x:Infinity, y:Infinity, z:Infinity}
};
let svg = new DOMParser().parseFromString(code, 'text/xml');
let lines = [...svg.getElementsByTagName('polyline')];
lines.forEach(line => {
let seq = [];
let points = [...line.points];
points.forEach(point => {
if (offset) {
point.x += offset.x;
point.y += offset.y;
}
if (point.x) bounds.min.x = Math.min(bounds.min.x, point.x);
if (point.x) bounds.max.x = Math.max(bounds.max.x, point.x);
if (point.y) bounds.min.y = Math.min(bounds.min.y, point.y);
if (point.y) bounds.max.y = Math.max(bounds.max.y, point.y);
if (point.z) bounds.min.z = Math.min(bounds.min.z, point.z);
if (point.z) bounds.max.z = Math.max(bounds.max.z, point.z);
addOutput(seq, point, seq.length > 0);
});
output.push(seq);
});
scope.lines = lines.length;
};
PRO.parseGCode = function(gcode, offset) {
var lines = gcode
.toUpperCase()

View file

@ -899,22 +899,36 @@ self.kiri.license = exports.LICENSE;
SPACE.update();
}
function loadGCode(gcode) {
function loadCode(code, type) {
setViewMode(VIEWS.PREVIEW);
clearPrint();
setOpacity(0);
currentPrint = kiri.newPrint(settings, []);
currentPrint.parseGCode(gcode, settings.process.outputOriginCenter ? null : {
let offset = settings.process.outputOriginCenter ? null : {
x: -settings.device.bedWidth / 2,
y: -settings.device.bedDepth / 2,
});
};
switch (type) {
case 'svg':
currentPrint.parseSVG(code, offset);
break;
default:
currentPrint.parseGCode(code, offset);
break;
}
currentPrint.render();
SPACE.platform.add(currentPrint.group);
SPACE.update();
UI.layerPrint.checked = true;
updateSliderMax(true);
showSlices();
exportGCode(gcode);
switch (type) {
case 'svg':
break;
default:
exportGCode(code);
break;
}
}
function preparePrint(callback) {
@ -1879,19 +1893,20 @@ self.kiri.license = exports.LICENSE;
function loadFiles(files) {
for (var i=0; i<files.length; i++) {
var lower = files[i].name.toLowerCase(),
reader = new FileReader(),
var reader = new FileReader(),
lower = files[i].name.toLowerCase(),
isstl = lower.indexOf(".stl") > 0,
issvg = lower.indexOf(".svg") > 0,
isgcode = lower.indexOf(".gcode") > 0 || lower.indexOf(".nc") > 0;
reader.file = files[i];
reader.onloadend = function (e) {
if (isstl)
platformAdd(
if (isstl) platformAdd(
newWidget()
.loadVertices(new MOTO.STL().parse(e.target.result))
.saveToCatalog(e.target.file.name)
);
if (isgcode) loadGCode(e.target.result);
if (isgcode) loadCode(e.target.result, 'gcode');
if (issvg) loadCode(e.target.result, 'svg');
};
reader.readAsBinaryString(reader.file);
}

View file

@ -1,7 +1,7 @@
var exports = {
COPYRIGHT:"Copyright (C) 2014-2019 Stewart Allen <sa@grid.space> - All Rights Reserved",
LICENSE:"See the license.md file included with the source distribution",
VERSION:"1.4.5"
VERSION:"1.4.6"
};
if (!module) var module = {};
module.exports = exports;