store gcode as blob instead of string

Less prone to crashing
This commit is contained in:
Casper Lamboo 2018-04-03 11:38:40 +02:00
parent 61722a6985
commit 701d736cc0
7 changed files with 20 additions and 45 deletions

View File

@ -181,7 +181,13 @@ export async function slice(action, name, mesh, settings, updateProgress) {
.multiply(new THREE.Matrix4().makeRotationY(-Math.PI / 2.0)) .multiply(new THREE.Matrix4().makeRotationY(-Math.PI / 2.0))
.multiply(mesh.matrix); .multiply(mesh.matrix);
const { gcode } = await sliceGeometry(settings, mesh.geometry, mesh.material, matrix, false, false, ({ progress }) => { const { gcode } = await sliceGeometry({
...settings,
name: `${name}.gcode`,
printer: { type: settings.printers, title: printerSettings[settings.printer].title },
material: { type: settings.material, title: materialSettings[settings.material].title },
quality: { type: settings.quality, title: qualitySettings[settings.quality].title }
}, mesh.geometry, mesh.material, matrix, false, false, ({ progress }) => {
updateProgress({ updateProgress({
action: progress.action, action: progress.action,
percentage: (currentStep + progress.done / progress.total) / steps percentage: (currentStep + progress.done / progress.total) / steps
@ -193,16 +199,14 @@ export async function slice(action, name, mesh, settings, updateProgress) {
switch (action.target) { switch (action.target) {
case 'DOWNLOAD': { case 'DOWNLOAD': {
const file = new Blob([gcode], { type: 'text/plain' }); fileSaver.saveAs(gcode, `${name}.gcode`);
fileSaver.saveAs(file, `${name}.gcode`);
break; break;
} }
case 'WIFI_PRINT': { case 'WIFI_PRINT': {
if (settings.printer === 'doodle3d_printer') { if (settings.printer === 'doodle3d_printer') {
const body = new FormData(); const body = new FormData();
const file = new Blob([gcode], { type: 'plain/text' }); body.append('file', gcode, 'doodle.gcode');
body.append('file', file, 'doodle.gcode');
// because fetch has no way of retrieving progress we fake progress // because fetch has no way of retrieving progress we fake progress
let loaded = 0; let loaded = 0;
@ -236,14 +240,7 @@ export async function slice(action, name, mesh, settings, updateProgress) {
body.append(key, fields[key]); body.append(key, fields[key]);
} }
const file = new Blob([`;${JSON.stringify({ body.append('file', gcode, 'doodle.gcode');
...settings,
name: `${name}.gcode`,
printer: { type: settings.printers, title: printerSettings[settings.printer].title },
material: { type: settings.material, title: materialSettings[settings.material].title },
quality: { type: settings.quality, title: qualitySettings[settings.quality].title }
}).trim()}\n${gcode}`]);
body.append('file', file, 'doodle.gcode');
await fetchProgress(url, { method: 'POST', body }, progress => { await fetchProgress(url, { method: 'POST', body }, progress => {
updateProgress({ updateProgress({
@ -259,14 +256,7 @@ export async function slice(action, name, mesh, settings, updateProgress) {
} }
case 'CUSTOM_UPLOAD': { case 'CUSTOM_UPLOAD': {
const body = new FormData(); const body = new FormData();
const file = new Blob([`;${JSON.stringify({ body.append('file', gcode, 'doodle.gcode');
...settings,
name: `${name}.gcode`,
printer: { type: settings.printers, title: printerSettings[settings.printer].title },
material: { type: settings.material, title: materialSettings[settings.material].title },
quality: { type: settings.quality, title: qualitySettings[settings.quality].title }
}).trim()}\n${gcode}`]);
body.append('file', file, 'doodle.gcode');
await fetchProgress(action.url, { method: 'POST', body }, progress => { await fetchProgress(action.url, { method: 'POST', body }, progress => {
updateProgress({ updateProgress({

View File

@ -11,9 +11,12 @@ export const POSITION_Y = 'Y';
export const POSITION_Z = 'Z'; export const POSITION_Z = 'Z';
export default class GCode { export default class GCode {
constructor(layerHeight) { constructor(settings) {
this._nozzleToFilamentRatio = 1; this._nozzleToFilamentRatio = 1;
this._gcode = [`; Generated with Doodle3D Slicer V${VERSION}`]; this._gcode = [
`; ${JSON.stringify(settings).trim()}`,
`; Generated with Doodle3D Slicer V${VERSION}`
];
this._currentValues = {}; this._currentValues = {};
this._nozzlePosition = { x: 0, y: 0 }; this._nozzlePosition = { x: 0, y: 0 };
this._extruder = 0.0; this._extruder = 0.0;

View File

@ -1,15 +0,0 @@
export function stringToTypedArray(string) {
const array = new Uint8Array(string.length);
for (let i = 0; i < string.length; i ++) {
array[i] = string.charCodeAt(i);
}
return array;
}
export function typedArrayToString(array) {
let string = '';
for (let i = 0; i < array.length; i ++) {
string += String.fromCharCode(array[i]);
}
return string;
}

View File

@ -65,7 +65,8 @@ export default function(settings, geometry, openObjectIndexes, constructLinePrev
if (constructLinePreview) gcode.linePreview = createGcodeGeometry(gcode.gcode); if (constructLinePreview) gcode.linePreview = createGcodeGeometry(gcode.gcode);
gcode.gcode = gcodeToString(gcode.gcode); gcode.gcode = new Blob([gcodeToString(gcode.gcode)], { type: 'text/plain' });
return gcode; return gcode;
} }

View File

@ -16,7 +16,7 @@ export default function slicesToGCode(slices, settings) {
combing combing
} = settings; } = settings;
const gcode = new GCode(); const gcode = new GCode(settings);
gcode.updateLayerHeight(Z_OFFSET, nozzleDiameter, filamentThickness) gcode.updateLayerHeight(Z_OFFSET, nozzleDiameter, filamentThickness)
if (settings.startCode) gcode.addGCode(settings.startCode, settings); if (settings.startCode) gcode.addGCode(settings.startCode, settings);

View File

@ -1,7 +1,6 @@
import * as THREE from 'three'; import * as THREE from 'three';
import slice from './sliceActions/slice.js'; import slice from './sliceActions/slice.js';
import SlicerWorker from './slicer.worker.js'; import SlicerWorker from './slicer.worker.js';
import { typedArrayToString } from './sliceActions/helpers/binary.js';
export function sliceMesh(settings, mesh, sync = false, constructLinePreview = false, onProgress) { export function sliceMesh(settings, mesh, sync = false, constructLinePreview = false, onProgress) {
if (!mesh || !mesh.isMesh) { if (!mesh || !mesh.isMesh) {
@ -91,7 +90,6 @@ function sliceAsync(settings, geometry, openObjectIndexes, constructLinePreview,
slicerWorker.terminate(); slicerWorker.terminate();
const { gcode } = data; const { gcode } = data;
gcode.gcode = typedArrayToString(gcode.gcode);
if (gcode.linePreview) gcode.linePreview = constructLineGeometry(gcode.linePreview); if (gcode.linePreview) gcode.linePreview = constructLineGeometry(gcode.linePreview);
resolve(gcode); resolve(gcode);

View File

@ -1,6 +1,5 @@
import 'core-js'; // polyfills import 'core-js'; // polyfills
import slice from './sliceActions/slice.js'; import slice from './sliceActions/slice.js';
import { stringToTypedArray } from './sliceActions/helpers/binary.js';
const onProgress = progress => { const onProgress = progress => {
self.postMessage({ self.postMessage({
@ -16,9 +15,8 @@ self.addEventListener('message', (event) => {
const { settings, geometry, constructLinePreview, openObjectIndexes } = data; const { settings, geometry, constructLinePreview, openObjectIndexes } = data;
const gcode = slice(settings, geometry, openObjectIndexes, constructLinePreview, onProgress); const gcode = slice(settings, geometry, openObjectIndexes, constructLinePreview, onProgress);
gcode.gcode = stringToTypedArray(gcode.gcode);
const buffers = [gcode.gcode.buffer]; const buffers = [];
if (gcode.linePreview) { if (gcode.linePreview) {
buffers.push(gcode.linePreview.positions.buffer); buffers.push(gcode.linePreview.positions.buffer);
buffers.push(gcode.linePreview.colors.buffer); buffers.push(gcode.linePreview.colors.buffer);