Merge branch 'develop' into babel

This commit is contained in:
peteruithoven 2017-07-20 16:24:14 +02:00
commit 541d205a2f
6 changed files with 133 additions and 78 deletions

View File

@ -1,10 +1,12 @@
# Doodle3D-Slicer
JavaScript gcode slicer, Intended to use with the Doodle3D WiFi-Box
# Usage
```javascript
import * as THREE from 'three';
import { defaultSettings, Slicer } from 'Doodle3D/Doodle3D-Slicer';
import { defaultSettings, sliceGeometry } from 'Doodle3D/Doodle3D-Slicer';
const settings = {
...defaultSettings.base,
@ -15,10 +17,46 @@ const settings = {
const geometry = new THREE.TorusGeometry(20, 10, 30, 30).clone();
const slicer = new SLICER.Slicer();
slicer.setGeometry(geometry);
const gcode = await slicer.slice(settings, ({ progress: { done, total, action } }) => {
const percentage = `${(done / total * 100).toFixed()}%`
console.log(action, percentage);
}));
const gcode = await sliceGeometry(settings, geometry);
```
# API
**Settings**
```javascript
import { defaultSettings } from 'Doodle3D/Doodle3D-Slicer';
const settings = {
...defaultSettings.base,
...defaultSettings.material.pla,
...defaultSettings.printer.ultimaker2go,
...defaultSettings.quality.high
};
```
Create settings object to be used by the slicer
**Slice Mesh**
```javascript
import { sliceMesh } from 'Doodle3D/Doodle3D-Slicer';
GCode: String = sliceMesh(settings: Object, mesh: THREE.Mesh, [sync: Boolean = false, onProgress: Func ])
```
Slice function that accepts Meshes
- Settings: settings object (see [settings](#settings))
- Mesh: THREE.Mesh instance that contains the geometry
- Sync: determines if the slicing progress will be sync (blocking) or async (non-blocking). A webworker is used to slice async
- onProgress: progress callback
**Slice Geometry**
```javascript
import { sliceGeometry } from 'Doodle3D/Doodle3D-Slicer';
GCode: String = sliceGeometry(settings: Object, geometry: THREE.Geometry | THREE.BufferGeometry, [matrix: THREE.Matrix, sync: Boolean = false, onProgress: Func ])
```
Slice function that accepts Geometry
- Settings: settings object (see [settings](#settings))
- Geometry: THREE.Geometry instance
- matrix: matrix that can control the scale, rotation and position of the model
- Sync: determines if the slicing progress will be sync (blocking) or async (non-blocking). A webworker is used to slice async
- onProgress: progress callback

View File

@ -1,6 +1,6 @@
import * as THREE from 'three';
import { defaultSettings, Slicer } from 'src/index.js';
import { saveAs } from 'file-saver';
import { defaultSettings, sliceGeometry } from 'src/index.js';
import fileSaver from 'file-saver';
const settings = {
...defaultSettings.base,
@ -12,16 +12,16 @@ const settings = {
const jsonLoader = new THREE.JSONLoader();
jsonLoader.load('models/airplane.json', async geometry => {
geometry.applyMatrix(new THREE.Matrix4().makeRotationX(Math.PI / -2));
geometry.applyMatrix(new THREE.Matrix4().setPosition(new THREE.Vector3(50, -0.1, 50)));
geometry.applyMatrix(new THREE.Matrix4().setPosition(new THREE.Vector3(50, -0.0, 50)));
geometry.computeFaceNormals();
const slicer = new Slicer().setGeometry(geometry);
const gcode = await slicer.slice(settings)
.progress(({ progress: { done, total, action } }) => {
const percentage = `${(done / total * 100).toFixed()}%`
document.write(`<p>${action}, ${percentage}</p>`);
});
const onProgress = ({ progress: { done, total, action } }) => {
const percentage = `${(done / total * 100).toFixed()}%`
document.write(`<p>${action}, ${percentage}</p>`);
};
const gcode = await sliceGeometry(settings, geometry, null, false, onProgress);
const file = new File([gcode], 'gcode.gcode', { type: 'text/plain' });
saveAs(file);
fileSaver.saveAs(file);
});

View File

@ -2,64 +2,73 @@ import * as THREE from 'three';
import slice from './sliceActions/slice.js';
import SlicerWorker from './slicer.worker.js';
export default class {
setMesh(mesh) {
mesh.updateMatrix();
return this.setGeometry(mesh.geometry, mesh.matrix);
export function sliceMesh(settings, mesh, sync = false, onProgress) {
if (typeof mesh === 'undefined' || !mesh.isMesh) {
throw new Error('Provide mesh is not intance of THREE.Mesh');
}
setGeometry(geometry, matrix) {
if (geometry.isBufferGeometry) {
geometry = new THREE.Geometry().fromBufferGeometry(geometry);
} else if (geometry.isGeometry) {
geometry = geometry.clone();
} else {
throw new Error('Geometry is not an instance of BufferGeometry or Geometry');
}
if (typeof matrix !== 'undefined') {
geometry.applyMatrix(matrix);
}
mesh.updateMatrix();
const { geometry, matrix } = mesh;
return sliceGeometry(settings, geometry, matrix, sync, onProgress);
}
this.geometry = geometry;
return this;
export function sliceGeometry(settings, geometry, matrix, sync = false, onProgress) {
if (typeof geometry === 'undefined') {
throw new Error('Missing required geometry argument');
} else if (geometry.isBufferGeometry) {
geometry = new THREE.Geometry().fromBufferGeometry(geometry);
} else if (geometry.isGeometry) {
geometry = geometry.clone();
} else {
throw new Error('Geometry is not an instance of BufferGeometry or Geometry');
}
sliceSync(settings, onProgress) {
return slice(this.geometry, settings, onProgress);
if (matrix) {
geometry.applyMatrix(matrix);
}
slice(settings, onProgress) {
if (!this.geometry) {
throw new Error('Geometry is not set, use Slicer.setGeometry or Slicer.setMesh first');
}
return new Promise((resolve, reject) => {
// create the slicer worker
const slicerWorker = new SlicerWorker();
slicerWorker.onerror = reject;
// listen to messages send from worker
slicerWorker.addEventListener('message', (event) => {
const { message, data } = event.data;
switch (message) {
case 'SLICE': {
slicerWorker.terminate();
resolve(data.gcode);
break;
}
case 'PROGRESS': {
onProgress(data);
break;
}
}
});
// send geometry and settings to worker to start the slicing progress
const geometry = this.geometry.toJSON();
slicerWorker.postMessage({
message: 'SLICE',
data: { geometry, settings }
});
});
if (sync) {
return sliceSync(settings, geometry, onProgress);
} else {
return sliceAsync(settings, geometry, onProgress);
}
}
function sliceSync(settings, geometry, onProgress) {
return slice(settings, geometry, onProgress);
}
function sliceAsync(settings, geometry, onProgress) {
return new Promise((resolve, reject) => {
// create the slicer worker
const slicerWorker = new SlicerWorker();
slicerWorker.onerror = reject;
// listen to messages send from worker
slicerWorker.addEventListener('message', (event) => {
const { message, data } = event.data;
switch (message) {
case 'SLICE': {
slicerWorker.terminate();
resolve(data.gcode);
break;
}
case 'PROGRESS': {
if (typeof onProgress !== 'undefined') {
onProgress(data);
}
break;
}
}
});
// send geometry and settings to worker to start the slicing progress
slicerWorker.postMessage({
message: 'SLICE',
data: {
settings,
geometry: geometry.toJSON()
}
});
});
}

View File

@ -1,4 +1,4 @@
import Slicer from './Slicer.js';
import { sliceGeometry, sliceMesh } from './slicer.js';
import baseSettings from './settings/default.yml';
import printerSettings from './settings/printer.yml';
import materialSettings from './settings/material.yml';
@ -12,6 +12,7 @@ const defaultSettings = {
};
export {
Slicer,
sliceGeometry,
sliceMesh,
defaultSettings
};

View File

@ -13,12 +13,20 @@ import detectOpenClosed from './detectOpenClosed.js';
import applyPrecision from './applyPrecision.js';
import removePrecision from './removePrecision.js';
export default function(geometry, settings, onProgress) {
export default function(settings, geometry, onProgress) {
const totalStages = 12;
let current = -1;
const updateProgress = (action) => {
current ++;
if (onProgress) onProgress({ done: current, total: totalStages, action });
if (typeof onProgress !== 'undefined') {
onProgress({
progress: {
done: current,
total: totalStages,
action
}
});
}
};
geometry.computeFaceNormals();

View File

@ -6,7 +6,7 @@ const loader = new THREE.JSONLoader();
const onProgress = progress => {
self.postMessage({
message: 'PROGRESS',
data: { progress }
data: progress
});
}
@ -14,11 +14,10 @@ self.addEventListener('message', (event) => {
const { message, data } = event.data;
switch (message) {
case 'SLICE': {
const { geometry: JSONGeometry, settings } = data;
const { settings, geometry: JSONGeometry } = data;
const { geometry } = loader.parse(JSONGeometry.data);
const gcode = slice(geometry, settings, onProgress);
const gcode = slice(settings, geometry, onProgress);
self.postMessage({
message: 'SLICE',