implement constants

This commit is contained in:
casperlamboo 2016-04-23 00:24:01 +02:00
parent 9d17df0cf3
commit ebbe75ef2e
8 changed files with 142 additions and 144 deletions

2
src/constants.js Normal file
View File

@ -0,0 +1,2 @@
export const CLEAN_DELTA = 0.01;
export const PRECISION = 0.01;

View File

@ -1,5 +1,14 @@
import THREE from 'three.js'; import THREE from 'three.js';
const G_COMMAND = 'G';
const M_COMMAND = 'M';
const FAN_SPEED = 'S';
const SPEED = 'F';
const EXTRUDER = 'E';
const POSITION_X = 'X';
const POSITION_Y = 'Y';
const POSITION_Z = 'Z';
export default class { export default class {
constructor () { constructor () {
this.gcode = ''; this.gcode = '';
@ -23,15 +32,14 @@ export default class {
str = action + value; str = action + value;
first = false; first = false;
} } else if (currentValue !== value) {
else if (currentValue !== value) {
str += ` ${action}${value}`; str += ` ${action}${value}`;
this.current[action] = value; this.current[action] = value;
} }
} }
this.gcode += str + '\n'; this.gcode += `${str}\n`;
} }
setSettings (settings) { setSettings (settings) {
@ -43,13 +51,8 @@ export default class {
turnFanOn (fanSpeed) { turnFanOn (fanSpeed) {
this.isFanOn = true; this.isFanOn = true;
var gcode = { const gcode = { [M_COMMAND]: 106 }
'M': 106 if (fanSpeed !== undefined) gcode[FAN_SPEED] = fanSpeed;
}
if (fanSpeed !== undefined) {
gcode['S'] = fanSpeed;
}
this._addGCode(gcode); this._addGCode(gcode);
@ -59,24 +62,24 @@ export default class {
turnFanOff () { turnFanOff () {
this.isFanOn = false; this.isFanOn = false;
this._addGCode({ this._addGCode({ [M_COMMAND]: 107 });
'M': 107
});
return this; return this;
} }
moveTo (x, y, layer) { moveTo (x, y, layer) {
var layerHeight = this.settings.config['layerHeight']; const layerHeight = this.settings.config['layerHeight'];
var travelSpeed = this.settings.config['travelSpeed']; const travelSpeed = this.settings.config['travelSpeed'];
var z = (layer + 1) * layerHeight; const z = (layer + 1) * layerHeight;
var speed = travelSpeed * 60; const speed = travelSpeed * 60;
this._addGCode({ this._addGCode({
'G': 0, [G_COMMAND]: 0,
'X': x.toFixed(3), 'Y': y.toFixed(3), 'Z': z.toFixed(3), [POSITION_X]: x.toFixed(3),
'F': speed.toFixed(3) [POSITION_Y]: y.toFixed(3),
[POSITION_Z]: z.toFixed(3),
[SPEED]: speed.toFixed(3)
}); });
this._nozzlePosition.set(x, y); this._nozzlePosition.set(x, y);
@ -85,29 +88,31 @@ export default class {
} }
lineTo (x, y, layer, type) { lineTo (x, y, layer, type) {
var newNozzlePosition = new THREE.Vector2(x, y); const newNozzlePosition = new THREE.Vector2(x, y);
var layerHeight = this.settings.config['layerHeight']; const layerHeight = this.settings.config['layerHeight'];
var nozzleDiameter = this.settings.config['nozzleDiameter']; const nozzleDiameter = this.settings.config['nozzleDiameter'];
var filamentThickness = this.settings.config['filamentThickness']; const filamentThickness = this.settings.config['filamentThickness'];
var travelSpeed = this.settings.config['travelSpeed']; const travelSpeed = this.settings.config['travelSpeed'];
var profile = this.settings.config[(this.bottom ? 'bottom' : type)]; const profile = this.settings.config[(this.bottom ? 'bottom' : type)];
var speed = profile['speed'] * 60; const speed = profile['speed'] * 60;
var flowRate = profile['flowRate']; const flowRate = profile['flowRate'];
var z = (layer + 1) * layerHeight; const z = (layer + 1) * layerHeight;
var lineLength = this._nozzlePosition.distanceTo(newNozzlePosition); const lineLength = this._nozzlePosition.distanceTo(newNozzlePosition);
var filamentSurfaceArea = Math.pow((filamentThickness / 2), 2) * Math.PI; const filamentSurfaceArea = Math.pow((filamentThickness / 2), 2) * Math.PI;
this.extruder += lineLength * nozzleDiameter * layerHeight / filamentSurfaceArea * flowRate; this.extruder += lineLength * nozzleDiameter * layerHeight / filamentSurfaceArea * flowRate;
this._addGCode({ this._addGCode({
'G': 1, [G_COMMAND]: 1,
'X': x.toFixed(3), 'Y': y.toFixed(3), 'Z': z.toFixed(3), [POSITION_X]: x.toFixed(3),
'F': speed.toFixed(3), [POSITION_Y]: y.toFixed(3),
'E': this.extruder.toFixed(3) [POSITION_Z]: z.toFixed(3),
[SPEED]: speed.toFixed(3),
[EXTRUDER]: this.extruder.toFixed(3)
}); });
this._nozzlePosition.copy(newNozzlePosition); this._nozzlePosition.copy(newNozzlePosition);
@ -116,20 +121,20 @@ export default class {
} }
unRetract () { unRetract () {
var retractionEnabled = this.settings.config['retractionEnabled']; const retractionEnabled = this.settings.config['retractionEnabled'];
var retractionMinDistance = this.settings.config['retractionMinDistance']; const retractionMinDistance = this.settings.config['retractionMinDistance'];
var retractionSpeed = this.settings.config['retractionSpeed']; const retractionSpeed = this.settings.config['retractionSpeed'];
if (this.isRetracted && retractionEnabled) { if (this.isRetracted && retractionEnabled) {
this.isRetracted = false; this.isRetracted = false;
var speed = retractionSpeed * 60; const speed = retractionSpeed * 60;
if (this.extruder > retractionMinDistance) { if (this.extruder > retractionMinDistance) {
this._addGCode({ this._addGCode({
'G': 0, [G_COMMAND]: 0,
'E': this.extruder.toFixed(3), [EXTRUDER]: this.extruder.toFixed(3),
'F': speed.toFixed(3) [SPEED]: speed.toFixed(3)
}); });
} }
} }
@ -138,21 +143,21 @@ export default class {
} }
retract () { retract () {
var retractionAmount = this.settings.config['retractionAmount']; const retractionAmount = this.settings.config['retractionAmount'];
var retractionEnabled = this.settings.config['retractionEnabled']; const retractionEnabled = this.settings.config['retractionEnabled'];
var retractionMinDistance = this.settings.config['retractionMinDistance']; const retractionMinDistance = this.settings.config['retractionMinDistance'];
var retractionSpeed = this.settings.config['retractionSpeed']; const retractionSpeed = this.settings.config['retractionSpeed'];
if (!this.isRetracted && retractionEnabled) { if (!this.isRetracted && retractionEnabled) {
this.isRetracted = true; this.isRetracted = true;
var speed = retractionSpeed * 60; const speed = retractionSpeed * 60;
if (this.extruder > retractionMinDistance && retractionEnabled) { if (this.extruder > retractionMinDistance && retractionEnabled) {
this._addGCode({ this._addGCode({
'G': 0, [G_COMMAND]: 0,
'E': (this.extruder - retractionAmount).toFixed(3), [EXTRUDER]: (this.extruder - retractionAmount).toFixed(3),
'F': speed.toFixed(3) [SPEED]: speed.toFixed(3)
}); });
} }
} }

View File

@ -1,8 +1,7 @@
import { PRECISION } from '../constants.js'
import getFillTemplate from './getFillTemplate.js'; import getFillTemplate from './getFillTemplate.js';
import Shape from 'Doodle3D/clipper-js'; import Shape from 'Doodle3D/clipper-js';
const scale = 100;
export default function generateInfills(slices, settings) { export default function generateInfills(slices, settings) {
console.log('generating infills'); console.log('generating infills');
@ -15,9 +14,9 @@ export default function generateInfills(slices, settings) {
infillOverlap infillOverlap
} = settings.config; } = settings.config;
fillGridSize *= scale; fillGridSize /= PRECISION;
nozzleDiameter *= scale; nozzleDiameter /= PRECISION;
infillOverlap *= scale; infillOverlap /= PRECISION;
const bottomSkinCount = Math.ceil(bottomThickness/layerHeight); const bottomSkinCount = Math.ceil(bottomThickness/layerHeight);
const topSkinCount = Math.ceil(topThickness/layerHeight); const topSkinCount = Math.ceil(topThickness/layerHeight);

View File

@ -1,4 +1,5 @@
const scale = 100; import { PRECISION } from '../constants.js'
const offsetOptions = { const offsetOptions = {
jointType: 'jtSquare', jointType: 'jtSquare',
endType: 'etClosedPolygon', endType: 'etClosedPolygon',
@ -7,33 +8,32 @@ const offsetOptions = {
}; };
export default function generateInnerLines(slices, settings) { export default function generateInnerLines(slices, settings) {
console.log("generating outer lines and inner lines"); 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, shellThickness } = settings.config; let { layerHeight, nozzleDiameter, shellThickness } = settings.config;
nozzleDiameter *= scale; nozzleDiameter /= PRECISION;
shellThickness *= scale; shellThickness /= PRECISION;
var nozzleRadius = nozzleDiameter / 2; const nozzleRadius = nozzleDiameter / 2;
var shells = Math.round(shellThickness / nozzleDiameter); const shells = Math.round(shellThickness / nozzleDiameter);
for (var layer = 0; layer < slices.length; layer ++) { for (let layer = 0; layer < slices.length; layer ++) {
var slice = slices[layer]; const slice = slices[layer];
for (var i = 0; i < slice.parts.length; i ++) { for (let i = 0; i < slice.parts.length; i ++) {
var part = slice.parts[i]; const part = slice.parts[i];
if (!part.shape.closed) continue; if (!part.shape.closed) continue;
// var outerLine = part.shape.clone().scaleUp(scale).offset(-nozzleRadius); const outerLine = part.shape.scaleDown(PRECISION).offset(-nozzleRadius, offsetOptions);
const outerLine = part.shape.scaleUp(scale).offset(-nozzleRadius, offsetOptions);
if (outerLine.paths.length > 0) { if (outerLine.paths.length > 0) {
part.outerLine.join(outerLine); part.outerLine.join(outerLine);
for (var shell = 1; shell < shells; shell += 1) { for (let shell = 1; shell < shells; shell += 1) {
var offset = shell * nozzleDiameter; const offset = shell * nozzleDiameter;
var innerLine = outerLine.offset(-offset, offsetOptions); const innerLine = outerLine.offset(-offset, offsetOptions);
if (innerLine.paths.length > 0) { if (innerLine.paths.length > 0) {
part.innerLines.push(innerLine); part.innerLines.push(innerLine);

View File

@ -1,7 +1,6 @@
import getFillTemplate from './getFillTemplate.js'; import getFillTemplate from './getFillTemplate.js';
import Shape from 'Doodle3D/clipper-js'; import Shape from 'Doodle3D/clipper-js';
import { PRECISION } from '../constants.js';
const scale = 100;
export default function generateSupport(slices, settings) { export default function generateSupport(slices, settings) {
console.log("generating support"); console.log("generating support");
@ -16,10 +15,10 @@ export default function generateSupport(slices, settings) {
nozzleDiameter nozzleDiameter
} = settings.config; } = settings.config;
supportGridSize *= scale; supportGridSize /= PRECISION;
supportMargin *= scale; supportMargin /= PRECISION;
plateSize *= scale; plateSize /= PRECISION;
nozzleDiameter *= scale; nozzleDiameter /= PRECISION;
var supportDistanceLayers = Math.max(Math.ceil(supportDistanceY / layerHeight), 1); var supportDistanceLayers = Math.max(Math.ceil(supportDistanceY / layerHeight), 1);
var supportAreas = new Shape([], true); var supportAreas = new Shape([], true);

View File

@ -4,43 +4,39 @@ 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"); console.log("generating slices");
var layers = []; const layers = [];
for (var layer = 1; layer < layerIntersectionIndexes.length; layer ++) { for (let layer = 1; layer < layerIntersectionIndexes.length; layer ++) {
var intersectionIndexes = layerIntersectionIndexes[layer]; const intersectionIndexes = layerIntersectionIndexes[layer];
var intersectionPoints = layerIntersectionPoints[layer]; const intersectionPoints = layerIntersectionPoints[layer];
if (intersectionIndexes.length === 0) { if (intersectionIndexes.length === 0) continue;
continue;
}
var closedShapes = []; const closedShapes = [];
var openShapes = []; const openShapes = [];
for (var i = 0; i < intersectionIndexes.length; i ++) { for (let i = 0; i < intersectionIndexes.length; i ++) {
var index = intersectionIndexes[i]; let index = intersectionIndexes[i];
if (intersectionPoints[index] === undefined) { if (intersectionPoints[index] === undefined) continue;
continue;
}
var firstPoints = [index]; const shape = [];
var isFirstPoint = true;
var closed = false;
var shape = []; const firstPoints = [index];
let isFirstPoint = true;
let closed = false;
while (index !== -1) { while (index !== -1) {
var intersection = intersectionPoints[index]; const intersection = intersectionPoints[index];
// uppercase X and Y because clipper vector // uppercase X and Y because clipper vector
shape.push(intersection); shape.push(intersection);
delete intersectionPoints[index]; delete intersectionPoints[index];
var connects = lines[index].connects; const connects = lines[index].connects;
var faceNormals = lines[index].normals; const faceNormals = lines[index].normals;
for (var j = 0; j < connects.length; j ++) { for (let i = 0; i < connects.length; i ++) {
var index = connects[j]; index = connects[i];
if (firstPoints.indexOf(index) !== -1 && shape.length > 2) { if (firstPoints.indexOf(index) !== -1 && shape.length > 2) {
closed = true; closed = true;
@ -50,10 +46,10 @@ export default function intersectionsToShapes(layerIntersectionIndexes, layerInt
// Check if index has an intersection or is already used // Check if index has an intersection or is already used
if (intersectionPoints[index] !== undefined) { if (intersectionPoints[index] !== undefined) {
var faceNormal = faceNormals[Math.floor(j / 2)]; const faceNormal = faceNormals[Math.floor(i / 2)];
var a = new THREE.Vector2(intersection.x, intersection.y); const a = new THREE.Vector2(intersection.x, intersection.y);
var b = new THREE.Vector2(intersectionPoints[index].x, intersectionPoints[index].y); const b = new THREE.Vector2(intersectionPoints[index].x, intersectionPoints[index].y);
// can't calculate normal between points if distance is smaller as 0.0001 // can't calculate normal between points if distance is smaller as 0.0001
if ((faceNormal.x === 0 && faceNormal.y === 0) || a.distanceTo(b) < 0.0001) { if ((faceNormal.x === 0 && faceNormal.y === 0) || a.distanceTo(b) < 0.0001) {
@ -63,26 +59,23 @@ export default function intersectionsToShapes(layerIntersectionIndexes, layerInt
delete intersectionPoints[index]; delete intersectionPoints[index];
connects = connects.concat(lines[index].connects); connects.push(...lines[index].connects);
faceNormals = faceNormals.concat(lines[index].normals); faceNormals.push(...lines[index].normals);
index = -1; index = -1;
} } else {
else {
// make sure the path goes the right direction // make sure the path goes the right direction
// THREE.Vector2.normal is not yet implimented // THREE.Vector2.normal is not yet implimented
// var normal = a.sub(b).normal().normalize(); // const normal = a.sub(b).normal().normalize();
var normal = a.sub(b); const normal = a.sub(b);
normal.set(-normal.y, normal.x).normalize(); normal.set(-normal.y, normal.x).normalize();
if (normal.dot(faceNormal) > 0) { if (normal.dot(faceNormal) > 0) {
break; break;
} } else {
else {
index = -1; index = -1;
} }
} }
} } else {
else {
index = -1; index = -1;
} }
} }
@ -90,25 +83,24 @@ export default function intersectionsToShapes(layerIntersectionIndexes, layerInt
} }
if (!closed) { if (!closed) {
var index = firstPoints[0]; index = firstPoints[0];
while (index !== -1) { while (index !== -1) {
if (firstPoints.indexOf(index) === -1) { if (firstPoints.indexOf(index) === -1) {
var intersection = intersectionPoints[index]; const intersection = intersectionPoints[index];
shape.unshift(intersection); shape.unshift(intersection);
delete intersectionPoints[index]; delete intersectionPoints[index];
} }
var connects = lines[index].connects; const connects = lines[index].connects;
for (var i = 0; i < connects.length; i ++) { for (let i = 0; i < connects.length; i ++) {
var index = connects[i]; index = connects[i];
if (intersectionPoints[index] !== undefined) { if (intersectionPoints[index] !== undefined) {
break; break;
} } else {
else {
index = -1; index = -1;
} }
} }
@ -117,8 +109,7 @@ export default function intersectionsToShapes(layerIntersectionIndexes, layerInt
if (closed) { if (closed) {
closedShapes.push(shape); closedShapes.push(shape);
} } else {
else {
openShapes.push(shape); openShapes.push(shape);
} }
} }

View File

@ -1,4 +1,6 @@
import THREE from 'three.js'; import THREE from 'three.js';
import { PRECISION } from '../constants.js';
const offsetOptions = { const offsetOptions = {
jointType: 'jtSquare', jointType: 'jtSquare',
endType: 'etClosedPolygon', endType: 'etClosedPolygon',
@ -9,15 +11,12 @@ const offsetOptions = {
export default function optimizePaths(slices, settings) { export default function optimizePaths(slices, settings) {
console.log("opimize paths"); console.log("opimize paths");
// need to scale up everything because of clipper rounding errors const brimOffset = settings.config["brimOffset"] / PRECISION;
var scale = 100;
var brimOffset = settings.config["brimOffset"] * scale; const start = new THREE.Vector2(0, 0);
var start = new THREE.Vector2(0, 0); for (let layer = 0; layer < slices.length; layer ++) {
const slice = slices[layer];
for (var layer = 0; layer < slices.length; layer ++) {
var slice = slices[layer];
if (layer === 0) { if (layer === 0) {
slice.brim = slice.getOutline().offset(brimOffset, offsetOptions); slice.brim = slice.getOutline().offset(brimOffset, offsetOptions);
@ -25,24 +24,24 @@ export default function optimizePaths(slices, settings) {
// start = slice.optimizePaths(start); // start = slice.optimizePaths(start);
for (var i = 0; i < slice.parts.length; i ++) { for (let i = 0; i < slice.parts.length; i ++) {
var part = slice.parts[i]; const part = slice.parts[i];
if (part.shape.closed) { if (part.shape.closed) {
part.outerLine.scaleDown(scale); part.outerLine.scaleDown(1 / PRECISION);
for (var j = 0; j < part.innerLines.length; j ++) { for (let i = 0; i < part.innerLines.length; i ++) {
var innerLine = part.innerLines[j]; const innerLine = part.innerLines[i];
innerLine.scaleDown(scale); innerLine.scaleDown(1 / PRECISION);
} }
part.fill.scaleDown(scale); part.fill.scaleDown(1 / PRECISION);
} }
} }
if (slice.support !== undefined) { if (slice.support !== undefined) {
slice.support.scaleDown(scale); slice.support.scaleDown(1 / PRECISION);
} }
if (slice.brim !== undefined) { if (slice.brim !== undefined) {
slice.brim.scaleDown(scale); slice.brim.scaleDown(1 / PRECISION);
} }
} }
} }

View File

@ -1,31 +1,34 @@
import Shape from 'Doodle3D/clipper-js'; import Shape from 'Doodle3D/clipper-js';
import Slice from '../slice.js'; import Slice from '../slice.js';
import { CLEAN_DELTA } from '../constants.js';
export default function shapesToSlices(shapes, settings) { export default function shapesToSlices(shapes, settings) {
const sliceLayers = []; const sliceLayers = [];
for (var layer = 0; layer < shapes.length; layer ++) { for (let layer = 0; layer < shapes.length; layer ++) {
var { closedShapes, openShapes } = shapes[layer]; let { closedShapes, openShapes } = shapes[layer];
closedShapes = new Shape(closedShapes, true, true) closedShapes = new Shape(closedShapes, true, true)
.clean(0.01) .clean(CLEAN_DELTA)
.fixOrientation() .fixOrientation()
.removeOverlap() .removeOverlap()
.seperateShapes(); .seperateShapes();
openShapes = new Shape(openShapes, false, true) openShapes = new Shape(openShapes, false, true)
.clean(0.01); .clean(CLEAN_DELTA);
var slice = new Slice(); const slice = new Slice();
for (var i = 0; i < closedShapes.length; i ++) { for (let i = 0; i < closedShapes.length; i ++) {
var closedShape = closedShapes[i]; const closedShape = closedShapes[i];
slice.add(closedShape); slice.add(closedShape);
// if (openShapes.path.length > 0) { // if (openShapes.path.length > 0) {
// openShapes = openShapes.difference(closedShape); // openShapes = openShapes.difference(closedShape);
// } // }
} }
if (openShapes.paths.length > 0) { if (openShapes.paths.length > 0) {
slice.add(openShapes); slice.add(openShapes);
} }