mirror of
https://github.com/Doodle3D/Doodle3D-Slicer.git
synced 2024-11-29 17:04:57 +01:00
60 lines
1.4 KiB
JavaScript
60 lines
1.4 KiB
JavaScript
import * as THREE from 'three.js';
|
|
import slice from './sliceActions/slice.js';
|
|
import SlicerWorker from './slicerWorker.js!worker';
|
|
|
|
export default class {
|
|
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 }
|
|
});
|
|
});
|
|
}
|
|
}
|