diff --git a/example/save.js b/example/save.js index 9524c6c..1270eea 100644 --- a/example/save.js +++ b/example/save.js @@ -1,6 +1,6 @@ import * as THREE from 'three'; import { defaultSettings, Slicer } from 'src/index.js'; -import { saveAs } from 'file-saver'; +import fileSaver from 'file-saver'; const settings = { ...defaultSettings.base, @@ -12,16 +12,15 @@ const settings = { const jsonLoader = new THREE.JSONLoader(); jsonLoader.load('models/airplane.json', async geometry => { geometry.applyMatrix(new THREE.Matrix4().makeRotationX(Math.PI / -2)); - geometry.applyMatrix(new THREE.Matrix4().setPosition(new THREE.Vector3(50, -0.1, 50))); + geometry.applyMatrix(new THREE.Matrix4().setPosition(new THREE.Vector3(50, -0.0, 50))); geometry.computeFaceNormals(); const slicer = new Slicer().setGeometry(geometry); - const gcode = await slicer.slice(settings) - .progress(({ progress: { done, total, action } }) => { - const percentage = `${(done / total * 100).toFixed()}%` - document.write(`
${action}, ${percentage}
`); - }); + const gcode = await slicer.slice(settings, ({ progress: { done, total, action } }) => { + const percentage = `${(done / total * 100).toFixed()}%` + document.write(`${action}, ${percentage}
`); + }); const file = new File([gcode], 'gcode.gcode', { type: 'text/plain' }); - saveAs(file); + fileSaver.saveAs(file); }); diff --git a/src/Slicer.js b/src/Slicer.js index c80959b..c4d9d34 100644 --- a/src/Slicer.js +++ b/src/Slicer.js @@ -26,10 +26,14 @@ export default class { return this; } sliceSync(settings, onProgress) { + if (typeof this.geometry === 'undefined') { + throw new Error('Geometry is not set, use Slicer.setGeometry or Slicer.setMesh first'); + } + return slice(this.geometry, settings, onProgress); } slice(settings, onProgress) { - if (!this.geometry) { + if (typeof this.geometry === 'undefined') { throw new Error('Geometry is not set, use Slicer.setGeometry or Slicer.setMesh first'); } @@ -48,7 +52,9 @@ export default class { break; } case 'PROGRESS': { - onProgress(data); + if (typeof onProgress !== 'undefined') { + onProgress(data); + } break; } }