Doodle3D-Slicer/src/Slicer.js

60 lines
1.4 KiB
JavaScript
Raw Normal View History

import * as THREE from 'three.js';
2016-08-19 14:46:02 +02:00
import slice from './sliceActions/slice.js';
2016-10-13 14:22:04 +02:00
import SlicerWorker from './slicerWorker.js!worker';
2015-07-26 15:32:10 +02:00
export default class {
2017-05-13 14:48:48 +02:00
setMesh(mesh) {
mesh.updateMatrix();
this.setGeometry(mesh.geometry, mesh.matrix);
return this;
}
setGeometry(geometry, matrix) {
if (geometry.type === 'BufferGeometry') {
geometry = new THREE.Geometry().fromBufferGeometry(geometry);
} else if (geometry.type === 'Geometry') {
geometry = geometry.clone();
} else {
throw new Error('Geometry is not an instance of BufferGeometry or Geometry');
}
if (matrix) {
geometry.applyMatrix(matrix);
}
this.geometry = geometry;
return this;
}
sliceSync(settings) {
return slice(this.geometry, settings);
}
slice(settings) {
const slicerWorker = new SlicerWorker();
const geometry = this.geometry.toJSON();
const { config } = settings;
return new Promise((resolve, reject) => {
slicerWorker.onerror = reject;
slicerWorker.addEventListener('message', (event) => {
const { message, data } = event.data;
switch (message) {
case 'SLICE': {
slicerWorker.terminate();
resolve(data.gcode);
break;
}
}
});
slicerWorker.postMessage({
message: 'SLICE',
data: { geometry, config }
});
});
}
2015-07-26 15:32:10 +02:00
}