more gcode parsing improvements

This commit is contained in:
Stewart Allen 2020-11-09 16:11:02 -05:00
commit 38d4473553
2 changed files with 40 additions and 11 deletions

View file

@ -113,11 +113,12 @@
seq = [],
abs = true,
move = false,
defh = 0,
height = 0,
factor = 1,
tool = 0,
E0G0 = false,
G0 = function() {
moving = function() {
move = true;
if (seq.length > 0) {
output.push(seq);
@ -146,6 +147,10 @@
};
lines.forEach(function(line) {
if (line.indexOf('- LAYER ') > 0) {
const hd = line.replace('(','').replace(')','').split(' ');
defh = parseFloat(hd[4]);
}
line = line.split(";")[0].split(" ");
let cmd = line.shift();
if (cmd.charAt(0) === 'T') {
@ -172,11 +177,14 @@
abs = false;
break;
case 'G0':
G0();
moving();
case 'G1':
line.forEach(function(tok) {
pos[tok.charAt(0)] = parseFloat(tok.substring(1));
});
if (seq.Z === undefined && pos.Z) {
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);
@ -185,10 +193,10 @@
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) G0();
if (LZ != pos.Z) moving();
else move = true;
if (move && pos.Z > LMZ) {
height = seq.height = pos.Z - LMZ;
height = seq.height = defh || pos.Z - LMZ;
}
}
addOutput(
@ -206,13 +214,15 @@
case 'M6':
break;
}
if (move) {
LMZ = Math.max(pos.Z, LMZ);
}
move = false;
pos.E = 0.0;
LZ = pos.Z;
LMZ = Math.max(LZ, LMZ);
});
G0();
moving();
scope.imported = gcode;
scope.lines = lines.length;

View file

@ -381,6 +381,22 @@
return print.render;
};
class Counter {
constructor() {
this.map = {};
this.total = 0;
}
put(key) {
const map = this.map;
const kp = key || 'bad';
map[kp] = (map[kp] || 0) + 1;
this.total++;
}
get() {
return { map: this.map, total: this.total };
}
}
FDM.prepareRender = function(levels, update, options) {
const opts = options || {};
const tools = opts.tools || {};
@ -390,11 +406,13 @@
const printColor = opts.print >= 0 ? opts.print : 0x777700;
const layers = [];
const maxspd = levels.map(level => {
return level.map(o => o.speed || 0).reduce((a, v) => Math.max(a,v));
}).reduce((a, v) => Math.max(a, v));
return level.map(o => o.emit || true ? o.speed || 0 : 0).reduce((a, v) => Math.max(a,v));
}).reduce((a, v) => Math.max(a, v)) + 1;
let lastOut = null;
let current = null;
const cn_len = new Counter();
const cn_col = new Counter();
levels.forEach((level, index) => {
const prints = {};
@ -458,17 +476,18 @@
.setLayer('move', moveColor, true)
.addPolys(moves, { thin: true, z: opts.z });
Object.values(prints).forEach(array => {
array.forEach(poly => { output
array.forEach(poly => { if (poly.length > 1) output
.setLayer(opts.action || 'print', printColor)
.addPolys([ poly ], thin ? { thin, z: opts.z } : {
offset: array.width, height, z: opts.z,
color: { face: poly.color, line: poly.color }
})
})
})// & cn_len.put(poly.length) & cn_col.put(poly.color) & console.log(poly.points)
});
});
update(index / levels.length);
});
// console.log(cn_len.get(), cn_col.get());
return layers;
}