diff --git a/src/interface/utils.js b/src/interface/utils.js index 31fc3dc..38cea15 100644 --- a/src/interface/utils.js +++ b/src/interface/utils.js @@ -181,7 +181,13 @@ export async function slice(action, name, mesh, settings, updateProgress) { .multiply(new THREE.Matrix4().makeRotationY(-Math.PI / 2.0)) .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({ action: progress.action, percentage: (currentStep + progress.done / progress.total) / steps @@ -193,16 +199,14 @@ export async function slice(action, name, mesh, settings, updateProgress) { switch (action.target) { case 'DOWNLOAD': { - const file = new Blob([gcode], { type: 'text/plain' }); - fileSaver.saveAs(file, `${name}.gcode`); + fileSaver.saveAs(gcode, `${name}.gcode`); break; } case 'WIFI_PRINT': { if (settings.printer === 'doodle3d_printer') { const body = new FormData(); - const file = new Blob([gcode], { type: 'plain/text' }); - body.append('file', file, 'doodle.gcode'); + body.append('file', gcode, 'doodle.gcode'); // because fetch has no way of retrieving progress we fake progress let loaded = 0; @@ -236,14 +240,7 @@ export async function slice(action, name, mesh, settings, updateProgress) { body.append(key, fields[key]); } - const file = new Blob([`;${JSON.stringify({ - ...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'); + body.append('file', gcode, 'doodle.gcode'); await fetchProgress(url, { method: 'POST', body }, progress => { updateProgress({ @@ -259,14 +256,7 @@ export async function slice(action, name, mesh, settings, updateProgress) { } case 'CUSTOM_UPLOAD': { const body = new FormData(); - const file = new Blob([`;${JSON.stringify({ - ...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'); + body.append('file', gcode, 'doodle.gcode'); await fetchProgress(action.url, { method: 'POST', body }, progress => { updateProgress({ diff --git a/src/sliceActions/helpers/GCode.js b/src/sliceActions/helpers/GCode.js index 2e789d1..f41e3b5 100644 --- a/src/sliceActions/helpers/GCode.js +++ b/src/sliceActions/helpers/GCode.js @@ -11,9 +11,12 @@ export const POSITION_Y = 'Y'; export const POSITION_Z = 'Z'; export default class GCode { - constructor(layerHeight) { + constructor(settings) { 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._nozzlePosition = { x: 0, y: 0 }; this._extruder = 0.0; diff --git a/src/sliceActions/helpers/binary.js b/src/sliceActions/helpers/binary.js deleted file mode 100644 index 7e6b33a..0000000 --- a/src/sliceActions/helpers/binary.js +++ /dev/null @@ -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; -} diff --git a/src/sliceActions/slice.js b/src/sliceActions/slice.js index 611934f..c51582f 100644 --- a/src/sliceActions/slice.js +++ b/src/sliceActions/slice.js @@ -65,7 +65,8 @@ export default function(settings, geometry, openObjectIndexes, constructLinePrev if (constructLinePreview) gcode.linePreview = createGcodeGeometry(gcode.gcode); - gcode.gcode = gcodeToString(gcode.gcode); + gcode.gcode = new Blob([gcodeToString(gcode.gcode)], { type: 'text/plain' }); + return gcode; } diff --git a/src/sliceActions/slicesToGCode.js b/src/sliceActions/slicesToGCode.js index 2708f75..7a06918 100644 --- a/src/sliceActions/slicesToGCode.js +++ b/src/sliceActions/slicesToGCode.js @@ -16,7 +16,7 @@ export default function slicesToGCode(slices, settings) { combing } = settings; - const gcode = new GCode(); + const gcode = new GCode(settings); gcode.updateLayerHeight(Z_OFFSET, nozzleDiameter, filamentThickness) if (settings.startCode) gcode.addGCode(settings.startCode, settings); diff --git a/src/slicer.js b/src/slicer.js index 5c28fc8..b651aec 100644 --- a/src/slicer.js +++ b/src/slicer.js @@ -1,7 +1,6 @@ import * as THREE from 'three'; import slice from './sliceActions/slice.js'; import SlicerWorker from './slicer.worker.js'; -import { typedArrayToString } from './sliceActions/helpers/binary.js'; export function sliceMesh(settings, mesh, sync = false, constructLinePreview = false, onProgress) { if (!mesh || !mesh.isMesh) { @@ -91,7 +90,6 @@ function sliceAsync(settings, geometry, openObjectIndexes, constructLinePreview, slicerWorker.terminate(); const { gcode } = data; - gcode.gcode = typedArrayToString(gcode.gcode); if (gcode.linePreview) gcode.linePreview = constructLineGeometry(gcode.linePreview); resolve(gcode); diff --git a/src/slicer.worker.js b/src/slicer.worker.js index d1a6338..a2fdcbc 100644 --- a/src/slicer.worker.js +++ b/src/slicer.worker.js @@ -1,6 +1,5 @@ import 'core-js'; // polyfills import slice from './sliceActions/slice.js'; -import { stringToTypedArray } from './sliceActions/helpers/binary.js'; const onProgress = progress => { self.postMessage({ @@ -16,9 +15,8 @@ self.addEventListener('message', (event) => { const { settings, geometry, constructLinePreview, openObjectIndexes } = data; const gcode = slice(settings, geometry, openObjectIndexes, constructLinePreview, onProgress); - gcode.gcode = stringToTypedArray(gcode.gcode); - const buffers = [gcode.gcode.buffer]; + const buffers = []; if (gcode.linePreview) { buffers.push(gcode.linePreview.positions.buffer); buffers.push(gcode.linePreview.colors.buffer);