Doodle3D-Slicer/src/slicer.js

137 lines
3.6 KiB
JavaScript
Raw Normal View History

2015-07-26 15:32:10 +02:00
import THREE from 'three.js';
2016-03-29 00:35:53 +02:00
import EventDispatcher from 'casperlamboo/EventDispatcher';
import calculateLayersIntersections from './sliceActions/calculateLayersIntersections.js';
import createLines from './sliceActions/createLines.js';
import generateInfills from './sliceActions/generateInfills.js';
import generateInnerLines from './sliceActions/generateInnerLines.js';
import generateSupport from './sliceActions/generateSupport.js';
import intersectionsToShapes from './sliceActions/intersectionsToShapes.js';
2016-05-06 19:52:31 +02:00
import addBrim from './sliceActions/addBrim.js';
import optimizePaths from './sliceActions/optimizePaths.js';
import shapesToSlices from './sliceActions/shapesToSlices.js';
import slicesToGCode from './sliceActions/slicesToGCode.js';
2015-07-26 15:32:10 +02:00
2016-03-29 00:35:53 +02:00
export default class extends EventDispatcher {
2015-07-26 15:32:10 +02:00
constructor () {
2016-03-29 00:35:53 +02:00
super();
2015-07-26 15:32:10 +02:00
this.progress = {
2016-03-29 00:27:06 +02:00
createdLines: false,
calculatedLayerIntersections: false,
sliced: false,
generatedSlices: false,
generatedInnerLines: false,
generatedInfills: false,
generatedSupport: false,
optimizedPaths: false,
2015-07-26 15:32:10 +02:00
generatedGCode: false
};
2015-06-16 01:25:06 +02:00
}
2015-06-16 07:23:31 +02:00
2015-07-26 15:32:10 +02:00
setMesh (mesh) {
mesh.updateMatrix();
2015-07-26 15:32:10 +02:00
this.setGeometry(mesh.geometry, mesh.matrix);
2015-07-26 15:32:10 +02:00
return this;
}
2015-06-16 10:28:26 +02:00
2015-07-26 15:32:10 +02:00
setGeometry (geometry, matrix) {
if (geometry.type === 'BufferGeometry') {
geometry = new THREE.Geometry().fromBufferGeometry(geometry);
}
2015-10-14 17:11:29 +02:00
else if (geometry.type.endsWith('Geometry')) {
2015-07-26 15:32:10 +02:00
geometry = geometry.clone();
}
else {
console.warn('Geometry is not an instance of BufferGeometry or Geometry');
return;
}
2015-06-16 10:28:26 +02:00
2015-07-26 15:32:10 +02:00
if (matrix instanceof THREE.Matrix4) {
geometry.applyMatrix(matrix);
}
2015-06-16 10:28:26 +02:00
2015-07-26 15:32:10 +02:00
geometry.mergeVertices();
2015-07-28 12:28:05 +02:00
geometry.computeFaceNormals();
2015-07-26 15:32:10 +02:00
this.geometry = geometry;
2015-06-16 10:28:26 +02:00
2015-07-26 15:32:10 +02:00
return this;
2015-06-16 10:28:26 +02:00
}
2015-07-26 15:32:10 +02:00
slice (settings) {
2016-04-23 09:56:36 +02:00
const supportEnabled = settings.config['supportEnabled'];
2015-06-16 10:28:26 +02:00
2015-07-29 11:18:18 +02:00
// get unique lines from geometry;
2016-04-23 09:56:36 +02:00
const lines = createLines(this.geometry, settings);
2015-07-26 15:32:10 +02:00
this.progress.createdLines = true;
this._updateProgress(settings);
2015-04-24 16:12:48 +02:00
2016-04-21 22:14:22 +02:00
const {
layerIntersectionIndexes,
layerIntersectionPoints
} = calculateLayersIntersections(lines, settings);
2015-07-29 11:18:18 +02:00
this.progress.calculatedLayerIntersections = true;
this._updateProgress(settings);
2016-04-21 22:14:22 +02:00
const shapes = intersectionsToShapes(layerIntersectionIndexes, layerIntersectionPoints, lines, settings);
2015-07-26 15:32:10 +02:00
this.progress.sliced = true;
this._updateProgress(settings);
2015-07-29 11:18:18 +02:00
2016-04-21 22:14:22 +02:00
const slices = shapesToSlices(shapes, settings);
2015-07-29 11:18:18 +02:00
this.progress.generatedSlices = true;
this._updateProgress(settings);
generateInnerLines(slices, settings);
2015-07-26 15:32:10 +02:00
this.progress.generatedInnerLines = true;
this._updateProgress(settings);
2015-07-01 14:51:41 +02:00
generateInfills(slices, settings);
2015-07-26 15:32:10 +02:00
this.progress.generatedInfills = true;
this._updateProgress(settings);
2015-07-01 14:51:41 +02:00
if (supportEnabled) {
generateSupport(slices, settings);
this.progress.generatedSupport = true;
this._updateProgress(settings);
2015-06-09 21:58:22 +02:00
}
2015-07-01 14:51:41 +02:00
2016-05-06 19:52:31 +02:00
addBrim(slices, settings);
optimizePaths(slices, settings);
2015-07-26 15:32:10 +02:00
this.progress.optimizedPaths = true;
this._updateProgress(settings);
2015-06-12 15:58:26 +02:00
var gcode = slicesToGCode(slices, settings);
this.progress.generatedGCode = true;
this._updateProgress(settings);
2015-06-11 14:34:30 +02:00
2016-04-21 22:50:02 +02:00
this.dispatchEvent({ type: 'finish', gcode });
return gcode;
2015-07-26 15:32:10 +02:00
}
2015-07-10 12:59:50 +02:00
2015-07-26 15:32:10 +02:00
_updateProgress (settings) {
2016-04-21 22:50:02 +02:00
var supportEnabled = settings.config['supportEnabled'];
2016-03-29 00:35:53 +02:00
var progress = {};
var procent = 0;
var length = 0;
for (var i in this.progress) {
2016-04-21 22:50:02 +02:00
if (!(!supportEnabled && i === 'generatedSupport')) {
2016-03-29 00:35:53 +02:00
progress[i] = this.progress[i];
if (progress[i]) {
procent += 1;
2015-07-10 12:59:50 +02:00
}
2016-03-29 00:35:53 +02:00
length += 1;
2015-07-10 12:59:50 +02:00
}
2016-03-29 00:35:53 +02:00
}
2015-07-10 12:59:50 +02:00
2016-03-29 00:35:53 +02:00
progress.procent = procent / length;
2015-07-10 12:59:50 +02:00
2016-04-21 22:50:02 +02:00
this.dispatchEvent({ type: 'progress', progress });
2015-07-10 12:59:50 +02:00
}
2015-07-26 15:32:10 +02:00
}