Doodle3D-Slicer/src/slicerworker.js

85 lines
1.9 KiB
JavaScript
Raw Normal View History

D3D.SlicerWorker = function () {
'use strict';
2015-06-12 15:58:26 +02:00
this.worker = new Worker('../../webworker/worker.js');
2015-05-29 13:51:18 +02:00
var scope = this;
this.worker.addEventListener('message', function (event) {
switch (event.data['cmd']) {
case 'PROGRESS':
2015-06-09 11:08:06 +02:00
if (scope.onprogress !== undefined) {
var progress = event.data['progress'];
2015-05-29 13:51:18 +02:00
scope.onprogress(progress);
}
2015-05-29 13:51:18 +02:00
break;
case 'GCODE':
if (scope.onfinish !== undefined) {
var reader = new FileReader();
reader.addEventListener("loadend", function() {
2015-06-11 14:34:30 +02:00
var gcode = reader.result;
scope.onfinish(gcode);
});
reader.readAsBinaryString(event.data['gcode']);
}
2015-05-29 13:51:18 +02:00
break;
}
}, false);
2015-06-12 15:58:26 +02:00
this.worker.onerror = function (error) {
console.warn("Error in webworker", error);
};
}
D3D.SlicerWorker.prototype.setSettings = function (USER_SETTINGS, PRINTER_SETTINGS) {
'use strict';
this.worker.postMessage({
'cmd': 'SET_SETTINGS',
'USER_SETTINGS': USER_SETTINGS,
'PRINTER_SETTINGS': PRINTER_SETTINGS
});
};
D3D.SlicerWorker.prototype.setMesh = function (mesh) {
'use strict';
if (mesh.geometry instanceof THREE.Geometry) {
var geometry = new THREE.BufferGeometry().fromGeometry(mesh.geometry);
}
else {
2015-05-29 10:41:44 +02:00
var geometry = mesh.geometry.clone();
}
2015-05-29 10:41:44 +02:00
var buffers = [];
for (var i = 0; i < geometry.attributesKeys.length; i ++) {
var key = geometry.attributesKeys[i];
buffers.push(geometry.attributes[key].array.buffer);
}
mesh.updateMatrix();
this.worker.postMessage({
'cmd': 'SET_MESH',
'geometry': {
'attributes': geometry.attributes,
'attributesKeys': geometry.attributesKeys
},
'matrix': mesh.matrix.toArray()
2015-05-29 10:41:44 +02:00
}, buffers);
};
D3D.SlicerWorker.prototype.slice = function () {
'use strict';
this.worker.postMessage({
'cmd': 'SLICE'
});
};
D3D.SlicerWorker.prototype.close = function () {
'use strict';
this.worker.postMessage({
'cmd': 'CLOSE'
});
};