update progress api

This commit is contained in:
casperlamboo 2017-07-18 12:26:30 +02:00
parent 4a238f9089
commit 0a041ddae0
17 changed files with 42 additions and 49 deletions

View File

@ -16,7 +16,11 @@ jsonLoader.load('models/airplane.json', async geometry => {
geometry.computeFaceNormals();
const slicer = new Slicer().setGeometry(geometry);
const gcode = slicer.sliceSync(settings);
const gcode = await slicer.slice(settings)
.progress(({ done, total, action }) => {
const percentage = `${(done / total * 100).toFixed()}%`
document.write(`<p>${action}, ${percentage}</p>`);
});
const file = new File([gcode], 'gcode.gcode', { type: 'text/plain' });
saveAs(file);

View File

@ -151,11 +151,12 @@ SystemJS.config({
"github:*/*.json"
],
map: {
"progress-promise": "npm:progress-promise@0.0.6",
"text": "github:systemjs/plugin-text@0.0.11",
"js-yaml": "npm:js-yaml@3.9.0",
"clipper-js": "github:Doodle3D/clipper-js@1.0.2",
"three.js": "github:mrdoob/three.js@r83",
"assert": "npm:jspm-nodelibs-assert@0.2.0",
"assert": "github:jspm/nodelibs-assert@0.2.0-alpha",
"buffer": "github:jspm/nodelibs-buffer@0.2.0-alpha",
"child_process": "npm:jspm-nodelibs-child_process@0.2.0",
"constants": "npm:jspm-nodelibs-constants@0.2.0",

View File

@ -10,6 +10,7 @@
"clipper-js": "github:Doodle3D/clipper-js@1.0.2",
"js-yaml": "npm:js-yaml@^3.9.0",
"json": "github:systemjs/plugin-json@^0.1.2",
"progress-promise": "npm:progress-promise@^0.0.6",
"text": "github:systemjs/plugin-text@^0.0.11",
"three.js": "github:mrdoob/three.js@r83",
"worker": "github:casperlamboo/plugin-worker@master"
@ -27,7 +28,7 @@
"zlib": "npm:jspm-nodelibs-zlib@^0.2.0"
},
"peerDependencies": {
"assert": "npm:jspm-nodelibs-assert@^0.2.0",
"assert": "github:jspm/nodelibs-assert@^0.2.0-alpha",
"buffer": "github:jspm/nodelibs-buffer@^0.2.0-alpha",
"child_process": "npm:jspm-nodelibs-child_process@^0.2.0",
"constants": "npm:jspm-nodelibs-constants@^0.2.0",

View File

@ -1,6 +1,7 @@
import * as THREE from 'three.js';
import slice from './sliceActions/slice.js';
import SlicerWorker from './slicerWorker.js!worker';
import ProgressPromise from 'progress-promise';
export default class {
setMesh(mesh) {
@ -27,15 +28,15 @@ export default class {
return this;
}
sliceSync(settings) {
return slice(this.geometry, settings);
sliceSync(settings, onprogress) {
return slice(this.geometry, settings, onprogress);
}
slice(settings) {
const slicerWorker = new SlicerWorker();
const geometry = this.geometry.toJSON();
return new Promise((resolve, reject) => {
return new ProgressPromise((resolve, reject, progress) => {
slicerWorker.onerror = reject;
slicerWorker.addEventListener('message', (event) => {
@ -47,9 +48,8 @@ export default class {
break;
}
case 'PROGRESS': {
if (this.onprogress) {
this.onprogress(data);
}
progress(data);
break;
}
}
});

View File

@ -8,8 +8,6 @@ const offsetOptions = {
};
export default function addBrim(slices, settings) {
console.log('add brim');
let { brim: { offset: brimOffset } } = settings;
brimOffset /= PRECISION;

View File

@ -1,8 +1,6 @@
import * as THREE from 'three.js';
export default function calculateLayersIntersections(lines, settings) {
console.log('calculating layer intersections');
const { layerHeight, dimensions: { z: dimensionsZ } } = settings;
const numLayers = Math.floor(dimensionsZ / layerHeight);

View File

@ -15,8 +15,6 @@ function addLine(geometry, lineLookup, lines, a, b, isFlat) {
}
export default function createLines(geometry, settings) {
console.log('constructing unique lines from geometry');
const lines = [];
const lineLookup = {};

View File

@ -1,6 +1,4 @@
export default function detectOpenClosed(lines) {
console.log('detecting open and closed lines');
const pools = getPools(lines);
const openLines = lines.map(line => line.connects.length === 2);

View File

@ -3,8 +3,6 @@ import getFillTemplate from './getFillTemplate.js';
import Shape from 'Doodle3D/clipper-js';
export default function generateInfills(slices, settings) {
console.log('generating infills');
let {
layerHeight,
fill: { gridSize: fillGridSize },

View File

@ -8,8 +8,6 @@ const offsetOptions = {
};
export default function generateInnerLines(slices, settings) {
console.log('generating outer lines and inner lines');
// need to scale up everything because of clipper rounding errors
let { layerHeight, nozzleDiameter, shell: { thickness: shellThickness } } = settings;
nozzleDiameter /= PRECISION;

View File

@ -3,8 +3,6 @@ import Shape from 'Doodle3D/clipper-js';
import { PRECISION } from '../constants.js';
export default function generateSupport(slices, settings) {
console.log('generating support');
if (!settings.support.enabled) return;
let {

View File

@ -2,8 +2,6 @@ import * as THREE from 'three.js';
import Shape from 'Doodle3D/clipper-js';
export default function intersectionsToShapes(layerIntersectionIndexes, layerIntersectionPoints, lines, settings) {
console.log('generating slices');
const layers = [];
for (let layer = 1; layer < layerIntersectionIndexes.length; layer ++) {

View File

@ -2,8 +2,6 @@ import * as THREE from 'three.js';
import Shape from 'Doodle3D/clipper-js';
export default function optimizePaths(slices, settings) {
console.log('optimize paths');
const start = new THREE.Vector2(0, 0);
for (let layer = 0; layer < slices.length; layer ++) {

View File

@ -3,8 +3,6 @@ import { PRECISION } from '../constants.js';
const inversePrecision = 1 / PRECISION;
export default function removePrecision(slices) {
console.log('remove precision');
for (let layer = 0; layer < slices.length; layer ++) {
const slice = slices[layer];

View File

@ -13,51 +13,53 @@ import applyPrecision from './applyPrecision.js';
import removePrecision from './removePrecision.js';
export default function(geometry, settings, onProgress) {
const totalStages = 12;
let current = 0;
const progressMessage = () => {
const totalStages = 11;
let current = -1;
const updateProgress = (action) => {
current ++;
// postMessage({ message: 'PROGRESS', data: { done: current, total: totalStages } });
if (onProgress) onProgress({ done: current, total: totalStages, action });
};
geometry.computeFaceNormals();
// get unique lines from geometry;
updateProgress('Constructing unique lines from geometry');
const lines = createLines(geometry, settings);
progressMessage();
updateProgress('Detecting open vs closed shapes');
const openClosed = detectOpenClosed(lines);
progressMessage();
updateProgress('Calculating layer intersections');
const {
layerIntersectionIndexes,
layerIntersectionPoints
} = calculateLayersIntersections(lines, settings);
progressMessage();
updateProgress('Constructing shapes from intersections');
const shapes = intersectionsToShapes(layerIntersectionIndexes, layerIntersectionPoints, lines, settings);
progressMessage();
applyPrecision(shapes);
updateProgress('Constructing slices from shapes');
const slices = shapesToSlices(shapes, settings);
progressMessage();
updateProgress('Generating inner lines');
generateInnerLines(slices, settings);
progressMessage();
updateProgress('Generating infills');
generateInfills(slices, settings);
progressMessage();
updateProgress('Generating support');
generateSupport(slices, settings);
progressMessage();
updateProgress('Adding brim');
addBrim(slices, settings);
progressMessage();
updateProgress('Optimizing paths');
optimizePaths(slices, settings);
progressMessage();
removePrecision(slices);
progressMessage();
removePrecision(slices);
updateProgress('Constructing gcode');
const gcode = slicesToGCode(slices, settings);
progressMessage();
updateProgress('Finished');
return gcode;
}

View File

@ -1,8 +1,6 @@
import GCode from '../GCode.js';
export default function slicesToGCode(slices, settings) {
console.log('slices to gcode');
const gcode = new GCode(settings);
for (let layer = 0; layer < slices.length; layer ++) {

View File

@ -3,6 +3,13 @@ import * as THREE from 'three.js';
const loader = new THREE.JSONLoader();
const onProgress = progress => {
self.postMessage({
message: 'PROGRESS',
data: progress
});
}
self.addEventListener('message', (event) => {
const { message, data } = event.data;
switch (message) {
@ -11,7 +18,7 @@ self.addEventListener('message', (event) => {
const { geometry } = new loader.parse(JSONGeometry.data);
const gcode = slice(geometry, settings);
const gcode = slice(geometry, settings, onProgress);
self.postMessage({
message: 'SLICE',