mirror of
https://github.com/Doodle3D/Doodle3D-Slicer.git
synced 2024-12-23 11:33:49 +01:00
update progress api
This commit is contained in:
parent
4a238f9089
commit
0a041ddae0
@ -16,7 +16,11 @@ jsonLoader.load('models/airplane.json', async geometry => {
|
|||||||
geometry.computeFaceNormals();
|
geometry.computeFaceNormals();
|
||||||
|
|
||||||
const slicer = new Slicer().setGeometry(geometry);
|
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' });
|
const file = new File([gcode], 'gcode.gcode', { type: 'text/plain' });
|
||||||
saveAs(file);
|
saveAs(file);
|
||||||
|
@ -151,11 +151,12 @@ SystemJS.config({
|
|||||||
"github:*/*.json"
|
"github:*/*.json"
|
||||||
],
|
],
|
||||||
map: {
|
map: {
|
||||||
|
"progress-promise": "npm:progress-promise@0.0.6",
|
||||||
"text": "github:systemjs/plugin-text@0.0.11",
|
"text": "github:systemjs/plugin-text@0.0.11",
|
||||||
"js-yaml": "npm:js-yaml@3.9.0",
|
"js-yaml": "npm:js-yaml@3.9.0",
|
||||||
"clipper-js": "github:Doodle3D/clipper-js@1.0.2",
|
"clipper-js": "github:Doodle3D/clipper-js@1.0.2",
|
||||||
"three.js": "github:mrdoob/three.js@r83",
|
"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",
|
"buffer": "github:jspm/nodelibs-buffer@0.2.0-alpha",
|
||||||
"child_process": "npm:jspm-nodelibs-child_process@0.2.0",
|
"child_process": "npm:jspm-nodelibs-child_process@0.2.0",
|
||||||
"constants": "npm:jspm-nodelibs-constants@0.2.0",
|
"constants": "npm:jspm-nodelibs-constants@0.2.0",
|
||||||
|
@ -10,6 +10,7 @@
|
|||||||
"clipper-js": "github:Doodle3D/clipper-js@1.0.2",
|
"clipper-js": "github:Doodle3D/clipper-js@1.0.2",
|
||||||
"js-yaml": "npm:js-yaml@^3.9.0",
|
"js-yaml": "npm:js-yaml@^3.9.0",
|
||||||
"json": "github:systemjs/plugin-json@^0.1.2",
|
"json": "github:systemjs/plugin-json@^0.1.2",
|
||||||
|
"progress-promise": "npm:progress-promise@^0.0.6",
|
||||||
"text": "github:systemjs/plugin-text@^0.0.11",
|
"text": "github:systemjs/plugin-text@^0.0.11",
|
||||||
"three.js": "github:mrdoob/three.js@r83",
|
"three.js": "github:mrdoob/three.js@r83",
|
||||||
"worker": "github:casperlamboo/plugin-worker@master"
|
"worker": "github:casperlamboo/plugin-worker@master"
|
||||||
@ -27,7 +28,7 @@
|
|||||||
"zlib": "npm:jspm-nodelibs-zlib@^0.2.0"
|
"zlib": "npm:jspm-nodelibs-zlib@^0.2.0"
|
||||||
},
|
},
|
||||||
"peerDependencies": {
|
"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",
|
"buffer": "github:jspm/nodelibs-buffer@^0.2.0-alpha",
|
||||||
"child_process": "npm:jspm-nodelibs-child_process@^0.2.0",
|
"child_process": "npm:jspm-nodelibs-child_process@^0.2.0",
|
||||||
"constants": "npm:jspm-nodelibs-constants@^0.2.0",
|
"constants": "npm:jspm-nodelibs-constants@^0.2.0",
|
||||||
|
@ -1,6 +1,7 @@
|
|||||||
import * as THREE from 'three.js';
|
import * as THREE from 'three.js';
|
||||||
import slice from './sliceActions/slice.js';
|
import slice from './sliceActions/slice.js';
|
||||||
import SlicerWorker from './slicerWorker.js!worker';
|
import SlicerWorker from './slicerWorker.js!worker';
|
||||||
|
import ProgressPromise from 'progress-promise';
|
||||||
|
|
||||||
export default class {
|
export default class {
|
||||||
setMesh(mesh) {
|
setMesh(mesh) {
|
||||||
@ -27,15 +28,15 @@ export default class {
|
|||||||
|
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
sliceSync(settings) {
|
sliceSync(settings, onprogress) {
|
||||||
return slice(this.geometry, settings);
|
return slice(this.geometry, settings, onprogress);
|
||||||
}
|
}
|
||||||
slice(settings) {
|
slice(settings) {
|
||||||
const slicerWorker = new SlicerWorker();
|
const slicerWorker = new SlicerWorker();
|
||||||
|
|
||||||
const geometry = this.geometry.toJSON();
|
const geometry = this.geometry.toJSON();
|
||||||
|
|
||||||
return new Promise((resolve, reject) => {
|
return new ProgressPromise((resolve, reject, progress) => {
|
||||||
slicerWorker.onerror = reject;
|
slicerWorker.onerror = reject;
|
||||||
|
|
||||||
slicerWorker.addEventListener('message', (event) => {
|
slicerWorker.addEventListener('message', (event) => {
|
||||||
@ -47,9 +48,8 @@ export default class {
|
|||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
case 'PROGRESS': {
|
case 'PROGRESS': {
|
||||||
if (this.onprogress) {
|
progress(data);
|
||||||
this.onprogress(data);
|
break;
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
@ -8,8 +8,6 @@ const offsetOptions = {
|
|||||||
};
|
};
|
||||||
|
|
||||||
export default function addBrim(slices, settings) {
|
export default function addBrim(slices, settings) {
|
||||||
console.log('add brim');
|
|
||||||
|
|
||||||
let { brim: { offset: brimOffset } } = settings;
|
let { brim: { offset: brimOffset } } = settings;
|
||||||
brimOffset /= PRECISION;
|
brimOffset /= PRECISION;
|
||||||
|
|
||||||
|
@ -1,8 +1,6 @@
|
|||||||
import * as THREE from 'three.js';
|
import * as THREE from 'three.js';
|
||||||
|
|
||||||
export default function calculateLayersIntersections(lines, settings) {
|
export default function calculateLayersIntersections(lines, settings) {
|
||||||
console.log('calculating layer intersections');
|
|
||||||
|
|
||||||
const { layerHeight, dimensions: { z: dimensionsZ } } = settings;
|
const { layerHeight, dimensions: { z: dimensionsZ } } = settings;
|
||||||
|
|
||||||
const numLayers = Math.floor(dimensionsZ / layerHeight);
|
const numLayers = Math.floor(dimensionsZ / layerHeight);
|
||||||
|
@ -15,8 +15,6 @@ function addLine(geometry, lineLookup, lines, a, b, isFlat) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
export default function createLines(geometry, settings) {
|
export default function createLines(geometry, settings) {
|
||||||
console.log('constructing unique lines from geometry');
|
|
||||||
|
|
||||||
const lines = [];
|
const lines = [];
|
||||||
const lineLookup = {};
|
const lineLookup = {};
|
||||||
|
|
||||||
|
@ -1,6 +1,4 @@
|
|||||||
export default function detectOpenClosed(lines) {
|
export default function detectOpenClosed(lines) {
|
||||||
console.log('detecting open and closed lines');
|
|
||||||
|
|
||||||
const pools = getPools(lines);
|
const pools = getPools(lines);
|
||||||
const openLines = lines.map(line => line.connects.length === 2);
|
const openLines = lines.map(line => line.connects.length === 2);
|
||||||
|
|
||||||
|
@ -3,8 +3,6 @@ import getFillTemplate from './getFillTemplate.js';
|
|||||||
import Shape from 'Doodle3D/clipper-js';
|
import Shape from 'Doodle3D/clipper-js';
|
||||||
|
|
||||||
export default function generateInfills(slices, settings) {
|
export default function generateInfills(slices, settings) {
|
||||||
console.log('generating infills');
|
|
||||||
|
|
||||||
let {
|
let {
|
||||||
layerHeight,
|
layerHeight,
|
||||||
fill: { gridSize: fillGridSize },
|
fill: { gridSize: fillGridSize },
|
||||||
|
@ -8,8 +8,6 @@ const offsetOptions = {
|
|||||||
};
|
};
|
||||||
|
|
||||||
export default function generateInnerLines(slices, settings) {
|
export default function generateInnerLines(slices, settings) {
|
||||||
console.log('generating outer lines and inner lines');
|
|
||||||
|
|
||||||
// need to scale up everything because of clipper rounding errors
|
// need to scale up everything because of clipper rounding errors
|
||||||
let { layerHeight, nozzleDiameter, shell: { thickness: shellThickness } } = settings;
|
let { layerHeight, nozzleDiameter, shell: { thickness: shellThickness } } = settings;
|
||||||
nozzleDiameter /= PRECISION;
|
nozzleDiameter /= PRECISION;
|
||||||
|
@ -3,8 +3,6 @@ import Shape from 'Doodle3D/clipper-js';
|
|||||||
import { PRECISION } from '../constants.js';
|
import { PRECISION } from '../constants.js';
|
||||||
|
|
||||||
export default function generateSupport(slices, settings) {
|
export default function generateSupport(slices, settings) {
|
||||||
console.log('generating support');
|
|
||||||
|
|
||||||
if (!settings.support.enabled) return;
|
if (!settings.support.enabled) return;
|
||||||
|
|
||||||
let {
|
let {
|
||||||
|
@ -2,8 +2,6 @@ import * as THREE from 'three.js';
|
|||||||
import Shape from 'Doodle3D/clipper-js';
|
import Shape from 'Doodle3D/clipper-js';
|
||||||
|
|
||||||
export default function intersectionsToShapes(layerIntersectionIndexes, layerIntersectionPoints, lines, settings) {
|
export default function intersectionsToShapes(layerIntersectionIndexes, layerIntersectionPoints, lines, settings) {
|
||||||
console.log('generating slices');
|
|
||||||
|
|
||||||
const layers = [];
|
const layers = [];
|
||||||
|
|
||||||
for (let layer = 1; layer < layerIntersectionIndexes.length; layer ++) {
|
for (let layer = 1; layer < layerIntersectionIndexes.length; layer ++) {
|
||||||
|
@ -2,8 +2,6 @@ import * as THREE from 'three.js';
|
|||||||
import Shape from 'Doodle3D/clipper-js';
|
import Shape from 'Doodle3D/clipper-js';
|
||||||
|
|
||||||
export default function optimizePaths(slices, settings) {
|
export default function optimizePaths(slices, settings) {
|
||||||
console.log('optimize paths');
|
|
||||||
|
|
||||||
const start = new THREE.Vector2(0, 0);
|
const start = new THREE.Vector2(0, 0);
|
||||||
|
|
||||||
for (let layer = 0; layer < slices.length; layer ++) {
|
for (let layer = 0; layer < slices.length; layer ++) {
|
||||||
|
@ -3,8 +3,6 @@ import { PRECISION } from '../constants.js';
|
|||||||
const inversePrecision = 1 / PRECISION;
|
const inversePrecision = 1 / PRECISION;
|
||||||
|
|
||||||
export default function removePrecision(slices) {
|
export default function removePrecision(slices) {
|
||||||
console.log('remove precision');
|
|
||||||
|
|
||||||
for (let layer = 0; layer < slices.length; layer ++) {
|
for (let layer = 0; layer < slices.length; layer ++) {
|
||||||
const slice = slices[layer];
|
const slice = slices[layer];
|
||||||
|
|
||||||
|
@ -13,51 +13,53 @@ import applyPrecision from './applyPrecision.js';
|
|||||||
import removePrecision from './removePrecision.js';
|
import removePrecision from './removePrecision.js';
|
||||||
|
|
||||||
export default function(geometry, settings, onProgress) {
|
export default function(geometry, settings, onProgress) {
|
||||||
const totalStages = 12;
|
const totalStages = 11;
|
||||||
let current = 0;
|
let current = -1;
|
||||||
const progressMessage = () => {
|
const updateProgress = (action) => {
|
||||||
current ++;
|
current ++;
|
||||||
// postMessage({ message: 'PROGRESS', data: { done: current, total: totalStages } });
|
if (onProgress) onProgress({ done: current, total: totalStages, action });
|
||||||
};
|
};
|
||||||
|
|
||||||
geometry.computeFaceNormals();
|
geometry.computeFaceNormals();
|
||||||
|
|
||||||
// get unique lines from geometry;
|
// get unique lines from geometry;
|
||||||
|
updateProgress('Constructing unique lines from geometry');
|
||||||
const lines = createLines(geometry, settings);
|
const lines = createLines(geometry, settings);
|
||||||
progressMessage();
|
|
||||||
|
|
||||||
|
updateProgress('Detecting open vs closed shapes');
|
||||||
const openClosed = detectOpenClosed(lines);
|
const openClosed = detectOpenClosed(lines);
|
||||||
progressMessage();
|
|
||||||
|
|
||||||
|
updateProgress('Calculating layer intersections');
|
||||||
const {
|
const {
|
||||||
layerIntersectionIndexes,
|
layerIntersectionIndexes,
|
||||||
layerIntersectionPoints
|
layerIntersectionPoints
|
||||||
} = calculateLayersIntersections(lines, settings);
|
} = calculateLayersIntersections(lines, settings);
|
||||||
progressMessage();
|
|
||||||
|
|
||||||
|
updateProgress('Constructing shapes from intersections');
|
||||||
const shapes = intersectionsToShapes(layerIntersectionIndexes, layerIntersectionPoints, lines, settings);
|
const shapes = intersectionsToShapes(layerIntersectionIndexes, layerIntersectionPoints, lines, settings);
|
||||||
progressMessage();
|
|
||||||
|
|
||||||
applyPrecision(shapes);
|
applyPrecision(shapes);
|
||||||
|
|
||||||
|
updateProgress('Constructing slices from shapes');
|
||||||
const slices = shapesToSlices(shapes, settings);
|
const slices = shapesToSlices(shapes, settings);
|
||||||
progressMessage();
|
|
||||||
|
|
||||||
|
updateProgress('Generating inner lines');
|
||||||
generateInnerLines(slices, settings);
|
generateInnerLines(slices, settings);
|
||||||
progressMessage();
|
updateProgress('Generating infills');
|
||||||
generateInfills(slices, settings);
|
generateInfills(slices, settings);
|
||||||
progressMessage();
|
updateProgress('Generating support');
|
||||||
generateSupport(slices, settings);
|
generateSupport(slices, settings);
|
||||||
progressMessage();
|
updateProgress('Adding brim');
|
||||||
addBrim(slices, settings);
|
addBrim(slices, settings);
|
||||||
progressMessage();
|
updateProgress('Optimizing paths');
|
||||||
optimizePaths(slices, settings);
|
optimizePaths(slices, settings);
|
||||||
progressMessage();
|
|
||||||
removePrecision(slices);
|
|
||||||
progressMessage();
|
|
||||||
|
|
||||||
|
removePrecision(slices);
|
||||||
|
|
||||||
|
updateProgress('Constructing gcode');
|
||||||
const gcode = slicesToGCode(slices, settings);
|
const gcode = slicesToGCode(slices, settings);
|
||||||
progressMessage();
|
|
||||||
|
updateProgress('Finished');
|
||||||
|
|
||||||
return gcode;
|
return gcode;
|
||||||
}
|
}
|
||||||
|
@ -1,8 +1,6 @@
|
|||||||
import GCode from '../GCode.js';
|
import GCode from '../GCode.js';
|
||||||
|
|
||||||
export default function slicesToGCode(slices, settings) {
|
export default function slicesToGCode(slices, settings) {
|
||||||
console.log('slices to gcode');
|
|
||||||
|
|
||||||
const gcode = new GCode(settings);
|
const gcode = new GCode(settings);
|
||||||
|
|
||||||
for (let layer = 0; layer < slices.length; layer ++) {
|
for (let layer = 0; layer < slices.length; layer ++) {
|
||||||
|
@ -3,6 +3,13 @@ import * as THREE from 'three.js';
|
|||||||
|
|
||||||
const loader = new THREE.JSONLoader();
|
const loader = new THREE.JSONLoader();
|
||||||
|
|
||||||
|
const onProgress = progress => {
|
||||||
|
self.postMessage({
|
||||||
|
message: 'PROGRESS',
|
||||||
|
data: progress
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
self.addEventListener('message', (event) => {
|
self.addEventListener('message', (event) => {
|
||||||
const { message, data } = event.data;
|
const { message, data } = event.data;
|
||||||
switch (message) {
|
switch (message) {
|
||||||
@ -11,7 +18,7 @@ self.addEventListener('message', (event) => {
|
|||||||
|
|
||||||
const { geometry } = new loader.parse(JSONGeometry.data);
|
const { geometry } = new loader.parse(JSONGeometry.data);
|
||||||
|
|
||||||
const gcode = slice(geometry, settings);
|
const gcode = slice(geometry, settings, onProgress);
|
||||||
|
|
||||||
self.postMessage({
|
self.postMessage({
|
||||||
message: 'SLICE',
|
message: 'SLICE',
|
||||||
|
Loading…
Reference in New Issue
Block a user