diff --git a/src/sliceActions/helpers/binary.js b/src/sliceActions/helpers/binary.js new file mode 100644 index 0000000..7e6b33a --- /dev/null +++ b/src/sliceActions/helpers/binary.js @@ -0,0 +1,15 @@ +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/slicer.js b/src/slicer.js index 79590cb..2ebba5c 100644 --- a/src/slicer.js +++ b/src/slicer.js @@ -1,6 +1,7 @@ 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) { @@ -95,6 +96,9 @@ function sliceAsync(settings, geometry, openObjectIndexes, constructLinePreview, case 'SLICE': { slicerWorker.terminate(); + const { gcode } = data; + gcode.gcode = typedArrayToString(gcode.gcode); + // if (data.gcode.linePreview) { // const geometry = new THREE.BufferGeometry(); // @@ -108,7 +112,7 @@ function sliceAsync(settings, geometry, openObjectIndexes, constructLinePreview, // data.gcode.linePreview = linePreview; // } - resolve(data.gcode); + resolve(gcode); break; } case 'PROGRESS': { diff --git a/src/slicer.worker.js b/src/slicer.worker.js index 4ebcb50..e55f9e9 100644 --- a/src/slicer.worker.js +++ b/src/slicer.worker.js @@ -1,5 +1,6 @@ import 'core-js'; // polyfills import slice from './sliceActions/slice.js'; +import { stringToTypedArray } from './sliceActions/helpers/binary.js'; const onProgress = progress => { self.postMessage({ @@ -15,8 +16,10 @@ 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) { // const position = gcode.linePreview.geometry.getAttribute('position').array; // const color = gcode.linePreview.geometry.getAttribute('color').array;