Doodle3D-Slicer/src/slicerworker.js

100 lines
2.1 KiB
JavaScript
Raw Normal View History

D3D.SlicerWorker = function () {
'use strict';
2015-06-12 21:19:56 +02:00
this.worker = new Worker(window.location.origin + '/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
});
2015-06-15 10:21:05 +02:00
return this;
};
D3D.SlicerWorker.prototype.setMesh = function (mesh) {
'use strict';
2015-07-10 12:59:50 +02:00
mesh.updateMatrix();
this.setGeometry(mesh.geometry, mesh.matrix);
return this;
};
D3D.SlicerWorker.prototype.setGeometry = function (geometry, matrix) {
'use strict';
if (geometry instanceof THREE.Geometry) {
geometry = new THREE.BufferGeometry().fromGeometry(geometry);
}
else {
2015-07-10 12:59:50 +02:00
geometry = geometry.clone();
2015-05-29 10:41:44 +02:00
}
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);
}
this.worker.postMessage({
'cmd': 'SET_MESH',
'geometry': {
'attributes': geometry.attributes,
'attributesKeys': geometry.attributesKeys
},
2015-07-10 12:59:50 +02:00
'matrix': matrix.toArray()
2015-05-29 10:41:44 +02:00
}, buffers);
2015-06-15 10:21:05 +02:00
return this;
};
D3D.SlicerWorker.prototype.slice = function () {
'use strict';
this.worker.postMessage({
'cmd': 'SLICE'
});
2015-06-15 10:21:05 +02:00
return this;
};
D3D.SlicerWorker.prototype.close = function () {
'use strict';
this.worker.postMessage({
'cmd': 'CLOSE'
});
2015-06-15 10:21:05 +02:00
return this;
};