2017-12-18 16:37:03 +01:00
|
|
|
import 'babel-polyfill'
|
2017-12-06 11:54:09 +01:00
|
|
|
import { Color } from 'three/src/math/Color.js';
|
|
|
|
import { BufferGeometry } from 'three/src/core/BufferGeometry.js';
|
|
|
|
import { BufferAttribute } from 'three/src/core/BufferAttribute.js';
|
|
|
|
import { LineBasicMaterial } from 'three/src/materials/LineBasicMaterial.js';
|
|
|
|
import { VertexColors } from 'three/src/constants.js';
|
|
|
|
import { LineSegments } from 'three/src/objects/LineSegments.js';
|
2016-08-19 14:09:51 +02:00
|
|
|
import calculateLayersIntersections from './calculateLayersIntersections.js';
|
|
|
|
import createLines from './createLines.js';
|
|
|
|
import generateInfills from './generateInfills.js';
|
|
|
|
import generateInnerLines from './generateInnerLines.js';
|
2017-07-19 17:33:55 +02:00
|
|
|
import generateOutlines from './generateOutlines.js';
|
2016-08-19 14:09:51 +02:00
|
|
|
import generateSupport from './generateSupport.js';
|
|
|
|
import intersectionsToShapes from './intersectionsToShapes.js';
|
|
|
|
import addBrim from './addBrim.js';
|
|
|
|
import optimizePaths from './optimizePaths.js';
|
|
|
|
import shapesToSlices from './shapesToSlices.js';
|
|
|
|
import slicesToGCode from './slicesToGCode.js';
|
2017-12-18 16:37:03 +01:00
|
|
|
import generateGeometry from './generateGeometry.js';
|
2016-08-19 14:09:51 +02:00
|
|
|
import applyPrecision from './applyPrecision.js';
|
2017-12-18 16:37:03 +01:00
|
|
|
// // import removePrecision from './removePrecision.js';
|
2016-08-19 14:09:51 +02:00
|
|
|
|
2017-12-18 16:37:03 +01:00
|
|
|
export default function(settings, sketch, matrix, constructLinePreview, onProgress) {
|
2017-07-19 17:37:40 +02:00
|
|
|
const totalStages = 12;
|
2017-07-18 12:26:30 +02:00
|
|
|
let current = -1;
|
|
|
|
const updateProgress = (action) => {
|
2017-07-18 11:26:11 +02:00
|
|
|
current ++;
|
2017-07-20 10:29:33 +02:00
|
|
|
if (typeof onProgress !== 'undefined') {
|
|
|
|
onProgress({
|
|
|
|
progress: {
|
|
|
|
done: current,
|
|
|
|
total: totalStages,
|
|
|
|
action
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2017-07-04 14:19:07 +02:00
|
|
|
};
|
|
|
|
|
2017-12-18 16:37:03 +01:00
|
|
|
updateProgress('Generating geometry');
|
|
|
|
const { geometry, open } = generateGeometry(sketch, matrix);
|
2017-05-26 17:12:01 +02:00
|
|
|
|
2017-07-18 12:26:30 +02:00
|
|
|
updateProgress('Constructing unique lines from geometry');
|
2017-12-18 16:37:03 +01:00
|
|
|
const { lines, faces } = createLines(geometry, settings);
|
2016-08-19 14:09:51 +02:00
|
|
|
|
2017-07-18 12:26:30 +02:00
|
|
|
updateProgress('Calculating layer intersections');
|
2017-12-18 16:37:03 +01:00
|
|
|
const layers = calculateLayersIntersections(lines, settings);
|
2016-08-19 14:09:51 +02:00
|
|
|
|
2017-07-18 12:26:30 +02:00
|
|
|
updateProgress('Constructing shapes from intersections');
|
2017-12-18 16:37:03 +01:00
|
|
|
const shapes = intersectionsToShapes(layers, faces, open, settings);
|
2016-08-19 14:09:51 +02:00
|
|
|
|
|
|
|
applyPrecision(shapes);
|
|
|
|
|
2017-07-18 12:26:30 +02:00
|
|
|
updateProgress('Constructing slices from shapes');
|
2016-08-19 14:09:51 +02:00
|
|
|
const slices = shapesToSlices(shapes, settings);
|
|
|
|
|
2017-07-18 12:26:30 +02:00
|
|
|
updateProgress('Generating inner lines');
|
2016-08-19 14:09:51 +02:00
|
|
|
generateInnerLines(slices, settings);
|
2017-07-19 17:33:55 +02:00
|
|
|
updateProgress('Generating out lines');
|
|
|
|
generateOutlines(slices, settings);
|
2017-07-18 12:26:30 +02:00
|
|
|
updateProgress('Generating infills');
|
2016-08-19 14:09:51 +02:00
|
|
|
generateInfills(slices, settings);
|
2017-07-18 12:26:30 +02:00
|
|
|
updateProgress('Generating support');
|
2016-08-19 14:09:51 +02:00
|
|
|
generateSupport(slices, settings);
|
2017-07-18 12:26:30 +02:00
|
|
|
updateProgress('Adding brim');
|
2016-08-19 14:09:51 +02:00
|
|
|
addBrim(slices, settings);
|
2017-07-18 12:26:30 +02:00
|
|
|
updateProgress('Optimizing paths');
|
2016-08-19 14:09:51 +02:00
|
|
|
optimizePaths(slices, settings);
|
2017-07-18 12:26:30 +02:00
|
|
|
|
2017-08-24 10:55:36 +02:00
|
|
|
// removePrecision(slices);
|
2016-08-19 14:09:51 +02:00
|
|
|
|
2017-07-18 12:26:30 +02:00
|
|
|
updateProgress('Constructing gcode');
|
2016-08-19 14:09:51 +02:00
|
|
|
const gcode = slicesToGCode(slices, settings);
|
2017-07-18 12:26:30 +02:00
|
|
|
|
|
|
|
updateProgress('Finished');
|
2016-08-19 14:09:51 +02:00
|
|
|
|
2017-11-12 11:53:45 +01:00
|
|
|
if (constructLinePreview) gcode.linePreview = createGcodeGeometry(gcode.gcode);
|
|
|
|
gcode.gcode = gcodeToString(gcode.gcode);
|
2016-08-19 14:09:51 +02:00
|
|
|
return gcode;
|
|
|
|
}
|
2017-11-12 11:53:45 +01:00
|
|
|
|
|
|
|
function gcodeToString(gcode) {
|
|
|
|
const currentValues = {};
|
|
|
|
return gcode.reduce((string, command) => {
|
|
|
|
let first = true;
|
|
|
|
for (const action in command) {
|
|
|
|
const value = command[action];
|
|
|
|
const currentValue = currentValues[action];
|
|
|
|
if (first) {
|
2017-12-18 16:37:03 +01:00
|
|
|
string += `${action}${value}`;
|
2017-11-12 11:53:45 +01:00
|
|
|
first = false;
|
|
|
|
} else if (currentValue !== value) {
|
|
|
|
string += ` ${action}${value}`;
|
|
|
|
currentValues[action] = value;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
string += '\n';
|
|
|
|
return string;
|
|
|
|
}, '');
|
|
|
|
}
|
|
|
|
|
|
|
|
const MAX_SPEED = 100 * 60;
|
2017-12-06 11:54:09 +01:00
|
|
|
const COLOR = new Color();
|
2017-11-12 11:53:45 +01:00
|
|
|
function createGcodeGeometry(gcode) {
|
|
|
|
const positions = [];
|
|
|
|
const colors = [];
|
|
|
|
|
2017-12-04 15:08:29 +01:00
|
|
|
let lastPoint = [0, 0, 0];
|
2017-11-12 11:53:45 +01:00
|
|
|
for (let i = 0; i < gcode.length; i ++) {
|
|
|
|
const { G, F, X, Y, Z } = gcode[i];
|
|
|
|
|
|
|
|
if (X || Y || Z) {
|
|
|
|
if (G === 1) {
|
2017-12-04 15:08:29 +01:00
|
|
|
positions.push(lastPoint.Y, lastPoint.Z, lastPoint.X);
|
2017-11-12 11:53:45 +01:00
|
|
|
positions.push(Y, Z, X);
|
|
|
|
|
2017-12-04 15:08:29 +01:00
|
|
|
const color = (G === 0) ? COLOR.setHex(0x00ff00) : COLOR.setHSL(F / MAX_SPEED, 0.5, 0.5);
|
2017-11-12 11:53:45 +01:00
|
|
|
colors.push(color.r, color.g, color.b);
|
|
|
|
colors.push(color.r, color.g, color.b);
|
|
|
|
}
|
2017-12-04 15:08:29 +01:00
|
|
|
lastPoint = { X, Y, Z };
|
2017-11-12 11:53:45 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-12-06 11:54:09 +01:00
|
|
|
const geometry = new BufferGeometry();
|
2017-11-12 11:53:45 +01:00
|
|
|
|
2017-12-06 11:54:09 +01:00
|
|
|
geometry.addAttribute('position', new BufferAttribute(new Float32Array(positions), 3));
|
|
|
|
geometry.addAttribute('color', new BufferAttribute(new Float32Array(colors), 3));
|
2017-11-12 11:53:45 +01:00
|
|
|
|
2017-12-06 11:54:09 +01:00
|
|
|
const material = new LineBasicMaterial({ vertexColors: VertexColors });
|
|
|
|
const linePreview = new LineSegments(geometry, material);
|
2017-11-12 11:53:45 +01:00
|
|
|
|
|
|
|
return linePreview;
|
|
|
|
}
|