2015-05-27 12:25:51 +02:00
|
|
|
D3D.SlicerWorker = function () {
|
2015-06-05 09:56:58 +02:00
|
|
|
'use strict';
|
2015-05-27 12:25:51 +02:00
|
|
|
|
2015-06-12 21:19:56 +02:00
|
|
|
this.worker = new Worker(window.location.origin + '/webworker/worker.js');
|
2015-05-27 12:25:51 +02:00
|
|
|
|
2015-05-29 13:51:18 +02:00
|
|
|
var scope = this;
|
2015-05-27 12:25:51 +02:00
|
|
|
this.worker.addEventListener('message', function (event) {
|
2015-06-05 09:56:58 +02:00
|
|
|
switch (event.data['cmd']) {
|
|
|
|
case 'PROGRESS':
|
2015-06-09 11:08:06 +02:00
|
|
|
|
2015-06-05 09:56:58 +02:00
|
|
|
if (scope.onprogress !== undefined) {
|
|
|
|
var progress = event.data['progress'];
|
2015-05-29 13:51:18 +02:00
|
|
|
|
2015-06-05 09:56:58 +02:00
|
|
|
scope.onprogress(progress);
|
|
|
|
}
|
2015-05-29 13:51:18 +02:00
|
|
|
break;
|
|
|
|
|
2015-06-05 09:56:58 +02:00
|
|
|
case 'GCODE':
|
|
|
|
if (scope.onfinish !== undefined) {
|
2015-06-11 10:28:21 +02:00
|
|
|
var reader = new FileReader();
|
|
|
|
reader.addEventListener("loadend", function() {
|
2015-06-11 14:34:30 +02:00
|
|
|
var gcode = reader.result;
|
2015-06-11 10:28:21 +02:00
|
|
|
scope.onfinish(gcode);
|
|
|
|
});
|
|
|
|
reader.readAsBinaryString(event.data['gcode']);
|
2015-06-05 09:56:58 +02:00
|
|
|
}
|
2015-05-29 13:51:18 +02:00
|
|
|
break;
|
|
|
|
}
|
2015-05-27 12:25:51 +02:00
|
|
|
}, false);
|
2015-06-12 15:58:26 +02:00
|
|
|
|
|
|
|
this.worker.onerror = function (error) {
|
|
|
|
console.warn("Error in webworker", error);
|
|
|
|
};
|
2015-05-27 12:25:51 +02:00
|
|
|
}
|
|
|
|
D3D.SlicerWorker.prototype.setSettings = function (USER_SETTINGS, PRINTER_SETTINGS) {
|
2015-06-05 09:56:58 +02:00
|
|
|
'use strict';
|
2015-05-27 12:25:51 +02:00
|
|
|
|
|
|
|
this.worker.postMessage({
|
2015-06-05 09:56:58 +02:00
|
|
|
'cmd': 'SET_SETTINGS',
|
|
|
|
'USER_SETTINGS': USER_SETTINGS,
|
|
|
|
'PRINTER_SETTINGS': PRINTER_SETTINGS
|
2015-05-27 12:25:51 +02:00
|
|
|
});
|
2015-06-15 10:21:05 +02:00
|
|
|
|
|
|
|
return this;
|
2015-05-27 12:25:51 +02:00
|
|
|
};
|
|
|
|
D3D.SlicerWorker.prototype.setMesh = function (mesh) {
|
2015-06-05 09:56:58 +02:00
|
|
|
'use strict';
|
2015-05-27 12:25:51 +02:00
|
|
|
|
|
|
|
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-06-01 12:06:11 +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);
|
2015-05-27 12:25:51 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
mesh.updateMatrix();
|
|
|
|
|
|
|
|
this.worker.postMessage({
|
2015-06-05 09:56:58 +02:00
|
|
|
'cmd': 'SET_MESH',
|
|
|
|
'geometry': {
|
|
|
|
'attributes': geometry.attributes,
|
|
|
|
'attributesKeys': geometry.attributesKeys
|
2015-06-01 12:06:11 +02:00
|
|
|
},
|
2015-06-05 09:56:58 +02:00
|
|
|
'matrix': mesh.matrix.toArray()
|
2015-05-29 10:41:44 +02:00
|
|
|
}, buffers);
|
2015-06-15 10:21:05 +02:00
|
|
|
|
|
|
|
return this;
|
2015-05-27 12:25:51 +02:00
|
|
|
};
|
|
|
|
D3D.SlicerWorker.prototype.slice = function () {
|
2015-06-05 09:56:58 +02:00
|
|
|
'use strict';
|
2015-05-27 12:25:51 +02:00
|
|
|
|
|
|
|
this.worker.postMessage({
|
2015-06-05 09:56:58 +02:00
|
|
|
'cmd': 'SLICE'
|
2015-05-27 12:25:51 +02:00
|
|
|
});
|
2015-06-15 10:21:05 +02:00
|
|
|
|
|
|
|
return this;
|
2015-05-27 12:25:51 +02:00
|
|
|
};
|
|
|
|
D3D.SlicerWorker.prototype.close = function () {
|
2015-06-05 09:56:58 +02:00
|
|
|
'use strict';
|
2015-05-27 12:25:51 +02:00
|
|
|
|
|
|
|
this.worker.postMessage({
|
2015-06-05 09:56:58 +02:00
|
|
|
'cmd': 'CLOSE'
|
2015-05-27 12:25:51 +02:00
|
|
|
});
|
2015-06-15 10:21:05 +02:00
|
|
|
|
|
|
|
return this;
|
2015-05-27 12:25:51 +02:00
|
|
|
};
|