refactor gcode parser
This commit is contained in:
parent
38d4473553
commit
3aa07314c2
3 changed files with 61 additions and 62 deletions
|
|
@ -93,8 +93,10 @@
|
||||||
scope.bytes = code.length;
|
scope.bytes = code.length;
|
||||||
};
|
};
|
||||||
|
|
||||||
PRO.parseGCode = function(gcode, offset, progress, done) {
|
PRO.parseGCode = function(gcode, offset, progress, done, options) {
|
||||||
let lines = gcode
|
const opts = options || {};
|
||||||
|
const fdm = opts.fdm;
|
||||||
|
const lines = gcode
|
||||||
.toUpperCase()
|
.toUpperCase()
|
||||||
.replace("X", " X")
|
.replace("X", " X")
|
||||||
.replace("Y", " Y")
|
.replace("Y", " Y")
|
||||||
|
|
@ -104,30 +106,11 @@
|
||||||
.replace(" ", " ")
|
.replace(" ", " ")
|
||||||
.split("\n");
|
.split("\n");
|
||||||
|
|
||||||
let scope = this,
|
const scope = this,
|
||||||
output = scope.output = [],
|
|
||||||
bounds = scope.bounds = {
|
bounds = scope.bounds = {
|
||||||
max: { x:-Infinity, y:-Infinity, z:-Infinity},
|
max: { x:-Infinity, y:-Infinity, z:-Infinity},
|
||||||
min: { x:Infinity, y:Infinity, z:Infinity}
|
min: { x:Infinity, y:Infinity, z:Infinity}
|
||||||
},
|
},
|
||||||
seq = [],
|
|
||||||
abs = true,
|
|
||||||
move = false,
|
|
||||||
defh = 0,
|
|
||||||
height = 0,
|
|
||||||
factor = 1,
|
|
||||||
tool = 0,
|
|
||||||
E0G0 = false,
|
|
||||||
moving = function() {
|
|
||||||
move = true;
|
|
||||||
if (seq.length > 0) {
|
|
||||||
output.push(seq);
|
|
||||||
seq = [];
|
|
||||||
seq.height = height;
|
|
||||||
}
|
|
||||||
},
|
|
||||||
LZ = 0.0,
|
|
||||||
LMZ = 0.0,
|
|
||||||
pos = {
|
pos = {
|
||||||
X: 0.0,
|
X: 0.0,
|
||||||
Y: 0.0,
|
Y: 0.0,
|
||||||
|
|
@ -146,6 +129,53 @@
|
||||||
Z: 0
|
Z: 0
|
||||||
};
|
};
|
||||||
|
|
||||||
|
let dz = 0,
|
||||||
|
abs = true,
|
||||||
|
defh = 0,
|
||||||
|
height = 0,
|
||||||
|
factor = 1,
|
||||||
|
tool = 0,
|
||||||
|
seq = [];
|
||||||
|
|
||||||
|
const output = scope.output = [ seq ];
|
||||||
|
|
||||||
|
function G0G1(moving) {
|
||||||
|
if (pos.X) bounds.min.x = Math.min(bounds.min.x, pos.X * factor);
|
||||||
|
if (pos.X) bounds.max.x = Math.max(bounds.max.x, pos.X * factor);
|
||||||
|
if (pos.Y) bounds.min.y = Math.min(bounds.min.y, pos.Y * factor);
|
||||||
|
if (pos.Y) bounds.max.y = Math.max(bounds.max.y, pos.Y * factor);
|
||||||
|
if (pos.Z) bounds.min.z = Math.min(bounds.min.z, pos.Z * factor);
|
||||||
|
if (pos.Z) bounds.max.z = Math.max(bounds.max.z, pos.Z * factor);
|
||||||
|
|
||||||
|
const point = newPoint(
|
||||||
|
factor * pos.X + off.X + xoff.X,
|
||||||
|
factor * pos.Y + off.Y + xoff.Y,
|
||||||
|
factor * pos.Z + off.Z + xoff.Z + dz
|
||||||
|
);
|
||||||
|
|
||||||
|
// always add moves to the current sequence
|
||||||
|
if (moving) {
|
||||||
|
addOutput(seq, point, false, pos.F, tool);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (seq.Z === undefined) {
|
||||||
|
seq.Z = pos.Z;
|
||||||
|
}
|
||||||
|
|
||||||
|
// non-move in a new plane means burp out
|
||||||
|
// the old sequence and start a new one
|
||||||
|
if (seq.Z != pos.Z) {
|
||||||
|
seq = [];
|
||||||
|
seq.height = height = (defh || pos.Z - seq.Z);
|
||||||
|
if (fdm) dz = -height / 2;
|
||||||
|
output.push(seq);
|
||||||
|
}
|
||||||
|
|
||||||
|
// add point to current sequence
|
||||||
|
addOutput(seq, point, true, pos.F, tool);
|
||||||
|
}
|
||||||
|
|
||||||
lines.forEach(function(line) {
|
lines.forEach(function(line) {
|
||||||
if (line.indexOf('- LAYER ') > 0) {
|
if (line.indexOf('- LAYER ') > 0) {
|
||||||
const hd = line.replace('(','').replace(')','').split(' ');
|
const hd = line.replace('(','').replace(')','').split(' ');
|
||||||
|
|
@ -161,6 +191,7 @@
|
||||||
xoff.Y = -ext[pos].extOffsetY;
|
xoff.Y = -ext[pos].extOffsetY;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
pos.E = 0.0;
|
||||||
switch (cmd) {
|
switch (cmd) {
|
||||||
case 'G20':
|
case 'G20':
|
||||||
factor = 25.4;
|
factor = 25.4;
|
||||||
|
|
@ -177,53 +208,19 @@
|
||||||
abs = false;
|
abs = false;
|
||||||
break;
|
break;
|
||||||
case 'G0':
|
case 'G0':
|
||||||
moving();
|
G0G1(true);
|
||||||
|
break;
|
||||||
case 'G1':
|
case 'G1':
|
||||||
line.forEach(function(tok) {
|
line.forEach(function(tok) {
|
||||||
pos[tok.charAt(0)] = parseFloat(tok.substring(1));
|
pos[tok.charAt(0)] = parseFloat(tok.substring(1));
|
||||||
});
|
});
|
||||||
if (seq.Z === undefined && pos.Z) {
|
G0G1(fdm ? pos.E == 0 : false);
|
||||||
seq.Z = pos.Z;
|
|
||||||
}
|
|
||||||
if (pos.X) bounds.min.x = Math.min(bounds.min.x, pos.X * factor);
|
|
||||||
if (pos.X) bounds.max.x = Math.max(bounds.max.x, pos.X * factor);
|
|
||||||
if (pos.Y) bounds.min.y = Math.min(bounds.min.y, pos.Y * factor);
|
|
||||||
if (pos.Y) bounds.max.y = Math.max(bounds.max.y, pos.Y * factor);
|
|
||||||
if (pos.Z) bounds.min.z = Math.min(bounds.min.z, pos.Z * factor);
|
|
||||||
if (pos.Z) bounds.max.z = Math.max(bounds.max.z, pos.Z * factor);
|
|
||||||
if (pos.E) E0G0 = true;
|
|
||||||
if (E0G0 && pos.E === 0.0) {
|
|
||||||
if (LZ != pos.Z) moving();
|
|
||||||
else move = true;
|
|
||||||
if (move && pos.Z > LMZ) {
|
|
||||||
height = seq.height = defh || pos.Z - LMZ;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
addOutput(
|
|
||||||
seq,
|
|
||||||
newPoint(
|
|
||||||
factor * pos.X + off.X + xoff.X,
|
|
||||||
factor * pos.Y + off.Y + xoff.Y,
|
|
||||||
factor * pos.Z + off.Z + xoff.Z
|
|
||||||
),
|
|
||||||
!move,
|
|
||||||
pos.F,
|
|
||||||
tool
|
|
||||||
);
|
|
||||||
break;
|
break;
|
||||||
case 'M6':
|
case 'M6':
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
if (move) {
|
|
||||||
LMZ = Math.max(pos.Z, LMZ);
|
|
||||||
}
|
|
||||||
move = false;
|
|
||||||
pos.E = 0.0;
|
|
||||||
LZ = pos.Z;
|
|
||||||
});
|
});
|
||||||
|
|
||||||
moving();
|
|
||||||
|
|
||||||
scope.imported = gcode;
|
scope.imported = gcode;
|
||||||
scope.lines = lines.length;
|
scope.lines = lines.length;
|
||||||
scope.bytes = gcode.length;
|
scope.bytes = gcode.length;
|
||||||
|
|
|
||||||
|
|
@ -164,7 +164,8 @@ KIRI.worker = {
|
||||||
};
|
};
|
||||||
const print = KIRI.newPrint(settings, Object.values(cache));
|
const print = KIRI.newPrint(settings, Object.values(cache));
|
||||||
const tools = settings.device.extruders;
|
const tools = settings.device.extruders;
|
||||||
const thin = settings.controller.thinRender || settings.mode !== 'FDM';
|
const mode = settings.mode;
|
||||||
|
const thin = settings.controller.thinRender || mode !== 'FDM';
|
||||||
|
|
||||||
const parsed = print.parseGCode(code, offset, progress => {
|
const parsed = print.parseGCode(code, offset, progress => {
|
||||||
send.data({ progress: progress * 0.5 });
|
send.data({ progress: progress * 0.5 });
|
||||||
|
|
@ -173,7 +174,7 @@ KIRI.worker = {
|
||||||
send.data({ progress: 0.5 + progress * 0.5 });
|
send.data({ progress: 0.5 + progress * 0.5 });
|
||||||
}, { thin, tools });
|
}, { thin, tools });
|
||||||
send.done({parsed: KIRI.codec.encode(layers)});
|
send.done({parsed: KIRI.codec.encode(layers)});
|
||||||
});
|
}, { fdm : mode === 'FDM' });
|
||||||
},
|
},
|
||||||
|
|
||||||
clear: function(data, send) {
|
clear: function(data, send) {
|
||||||
|
|
|
||||||
|
|
@ -405,8 +405,9 @@
|
||||||
const moveColor = opts.move >= 0 ? opts.move : 0xaaaaaa;
|
const moveColor = opts.move >= 0 ? opts.move : 0xaaaaaa;
|
||||||
const printColor = opts.print >= 0 ? opts.print : 0x777700;
|
const printColor = opts.print >= 0 ? opts.print : 0x777700;
|
||||||
const layers = [];
|
const layers = [];
|
||||||
|
|
||||||
const maxspd = levels.map(level => {
|
const maxspd = levels.map(level => {
|
||||||
return level.map(o => o.emit || true ? o.speed || 0 : 0).reduce((a, v) => Math.max(a,v));
|
return level.map(o => o.speed || 0).reduce((a, v) => Math.max(a,v));
|
||||||
}).reduce((a, v) => Math.max(a, v)) + 1;
|
}).reduce((a, v) => Math.max(a, v)) + 1;
|
||||||
|
|
||||||
let lastOut = null;
|
let lastOut = null;
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue