add option to export gcode as a zip file with discrete operations

This commit is contained in:
Stewart Allen 2022-01-24 15:22:20 -05:00
commit 5788b800a8
3 changed files with 50 additions and 18 deletions

View file

@ -22,6 +22,7 @@
time = 0,
lines = 0,
bytes = 0,
opnum = 0,
factor = 1,
output = [],
spindle = 0,
@ -70,10 +71,14 @@
time_sec: 0,
time_ms: 0,
time: 0
},
append;
}
append = function(line) {
function section(section) {
append();
online({ section });
}
function append(line) {
if (line) {
lines++;
bytes += line.length;
@ -83,7 +88,7 @@
online(output.join("\r\n"));
output = [];
}
};
}
function filterEmit(array, consts) {
if (!array) return;
@ -234,6 +239,7 @@
points++;
}
section('header');
if (!stripComments) {
append(`; Generated by Kiri:Moto ${KIRI.version}`);
append(`; ${new Date().toString()}`);
@ -291,6 +297,7 @@
if (mode !== layerout.mode) {
if (mode && !stripComments) append("; ending " + mode + " op after " + Math.round(time/60) + " seconds");
mode = layerout.mode;
if (mode) section(`op-${opnum++}-${mode}`);
if (!stripComments) append("; starting " + mode + " op");
}
if (layerout.spindle && layerout.spindle !== spindle) {
@ -312,6 +319,7 @@
if (mode && !stripComments) append("; ending " + mode + " op after " + Math.round(time/60) + " seconds");
// emit gcode post
section('footer');
filterEmit(gcodes.gcodePost, consts);
// flush buffered gcode

View file

@ -38,8 +38,17 @@ console.log/** Copyright Stewart Allen <sa@grid.space> -- All Rights Reserved */
function callExport(callback, mode, names) {
let alert = API.feature.work_alerts ? API.show.alert("Exporting") : null;
let gcode = [];
let section = [];
let sections = { };
KIRI.client.export(API.conf.get(), (line) => {
if (typeof line !== 'string') {
if (line.section) {
sections[line.section] = section = [];
}
return;
}
gcode.push(line);
section.push(line);
}, (output, error) => {
API.hide.alert(alert);
if (error) {
@ -47,7 +56,7 @@ console.log/** Copyright Stewart Allen <sa@grid.space> -- All Rights Reserved */
} else if (callback) {
callback(gcode.join('\r\n'), output);
} else {
exportGCodeDialog(gcode.join('\r\n'), output, names);
exportGCodeDialog(gcode, mode === 'CAM' ? sections : undefined, output, names);
}
});
}
@ -90,10 +99,6 @@ console.log/** Copyright Stewart Allen <sa@grid.space> -- All Rights Reserved */
driver.exportSVG(settings, data),
$('print-filename').value + ".svg"
);
// saveAs(new Blob(
// [driver.exportSVG(settings, data)],
// {type:"application/octet-stream"}),
// $('print-filename').value + ".svg");
}
function download_dxf() {
@ -101,10 +106,6 @@ console.log/** Copyright Stewart Allen <sa@grid.space> -- All Rights Reserved */
driver.exportDXF(settings, data),
$('print-filename').value + ".dxf"
);
// saveAs(new Blob(
// [driver.exportDXF(settings, data)],
// {type:"application/octet-stream"}),
// $('print-filename').value + ".dxf");
}
function download_gcode() {
@ -112,10 +113,6 @@ console.log/** Copyright Stewart Allen <sa@grid.space> -- All Rights Reserved */
driver.exportGCode(settings, data),
$('print-filename').value + ".gcode"
);
// saveAs(new Blob(
// [driver.exportGCode(settings, data)],
// {type:"application/octet-stream"}),
// $('print-filename').value + ".gcode");
}
API.ajax("/kiri/output-laser.html", function(html) {
@ -131,7 +128,7 @@ console.log/** Copyright Stewart Allen <sa@grid.space> -- All Rights Reserved */
});
}
function exportGCodeDialog(gcode, info, names) {
function exportGCodeDialog(gcode, sections, info, names) {
SDB['kiri-print-seq'] = printSeq++;
let settings = API.conf.get(),
@ -150,6 +147,9 @@ console.log/** Copyright Stewart Allen <sa@grid.space> -- All Rights Reserved */
grid_local,
grid_uuid;
// join gcode array into a string
gcode = gcode.join('\r\n');
// run gcode post processor function (when supplied and valid)
if (codeproc && self[codeproc]) {
gcode = self[codeproc](gcode);
@ -467,6 +467,29 @@ console.log/** Copyright Stewart Allen <sa@grid.space> -- All Rights Reserved */
calcWeight();
}
// in cam mode, show zip file option
let downloadZip = $('print-zip');
downloadZip.style.display = sections ? 'flex' : 'none';
downloadZip.onclick = function() {
let files = [];
for (let [ name, data ] of Object.entries(sections)) {
if (name.indexOf('op-') === 0) {
let head = sections.header || [];
let foot = sections.footer || [];
files.push({
name: `${name}.${fileext}`,
data: [ ...head, ...data, ...foot ].join('\r\n')
})
}
}
KIRI.client.zip(files, progress => {
API.show.progress(progress.percent/100, "generating zip files");
}, output => {
API.show.progress(0);
API.util.download(output, `${$('print-filename').value}.zip`);
})
};
// in palette mode, show download button
let downloadPalette = $('print-palette');
downloadPalette.style.display = info.segments ? 'flex' : 'none';