Doodle3D-Slicer/src/sliceActions/slicesToGCode.js

118 lines
3.5 KiB
JavaScript
Raw Normal View History

2017-07-19 11:02:42 +02:00
import GCode from './helpers/GCode.js';
2017-08-11 18:54:04 +02:00
import comb from './helpers/comb.js';
2017-08-24 10:55:36 +02:00
import { PRECISION } from '../constants.js';
2017-07-27 18:33:25 +02:00
const PROFILE_TYPES = ['support', 'innerShell', 'outerShell', 'innerInfill', 'outerInfill', 'brim'];
2017-07-27 18:20:08 +02:00
2016-04-23 09:56:15 +02:00
export default function slicesToGCode(slices, settings) {
2017-07-27 18:20:08 +02:00
const {
layerHeight,
filamentThickness,
nozzleDiameter,
travelSpeed,
retraction,
2017-08-17 16:13:21 +02:00
travel,
2017-11-16 22:39:54 +01:00
combing,
zOffset
2017-07-27 18:20:08 +02:00
} = settings;
const filamentSurfaceArea = Math.pow((filamentThickness / 2), 2) * Math.PI;
const lineSurfaceArea = nozzleDiameter * layerHeight;
const nozzleToFilamentRatio = lineSurfaceArea / filamentSurfaceArea;
const gcode = new GCode(nozzleToFilamentRatio);
const defaultProfile = {
2017-07-27 18:33:25 +02:00
travelProfile: travel,
retractionProfile: retraction
2017-07-27 18:20:08 +02:00
};
2016-04-23 09:56:15 +02:00
2017-07-27 18:33:25 +02:00
let isFirstLayer = true;
2016-04-23 09:56:15 +02:00
for (let layer = 0; layer < slices.length; layer ++) {
const slice = slices[layer];
2017-11-16 22:39:54 +01:00
const z = layer * layerHeight + zOffset;
if (layer === 1) {
gcode.turnFanOn();
2017-07-27 18:33:25 +02:00
isFirstLayer = false;
}
2017-07-27 18:20:08 +02:00
const profiles = PROFILE_TYPES.reduce((profiles, profileType) => {
profiles[profileType] = {
...defaultProfile,
2017-07-27 18:33:25 +02:00
lineProfile: isFirstLayer ? settings.firstLayer : settings[profileType]
};
2017-07-27 18:20:08 +02:00
return profiles;
}, {});
2017-06-22 10:19:15 +02:00
if (typeof slice.brim !== 'undefined') {
2017-08-11 18:54:04 +02:00
pathToGCode(null, false, gcode, slice.brim, true, true, z, profiles.brim);
}
2016-04-23 09:56:15 +02:00
for (let i = 0; i < slice.parts.length; i ++) {
const part = slice.parts[i];
if (part.closed) {
2017-09-13 13:56:19 +02:00
const outline = part.shell[0];
for (let i = 0; i < part.shell.length; i ++) {
const shell = part.shell[i];
const isOuterShell = i === 0;
const unRetract = isOuterShell;
const profile = isOuterShell ? profiles.outerShell : profiles.innerShell;
2017-08-17 16:13:21 +02:00
pathToGCode(outline, combing && true, gcode, shell, false, unRetract, z, profile);
}
2017-08-17 16:13:21 +02:00
pathToGCode(outline, combing && true, gcode, part.outerFill, false, false, z, profiles.outerInfill);
pathToGCode(outline, combing && true, gcode, part.innerFill, true, false, z, profiles.innerInfill);
2016-04-23 09:56:15 +02:00
} else {
2017-06-22 10:19:15 +02:00
const retract = !(slice.parts.length === 1 && typeof slice.support === 'undefined');
2017-08-11 18:54:04 +02:00
pathToGCode(null, false, gcode, part.shape, retract, retract, z, profiles.outerShell);
}
}
2017-06-22 10:19:15 +02:00
if (typeof slice.support !== 'undefined') {
2017-08-11 18:54:04 +02:00
pathToGCode(null, false, gcode, slice.support, true, true, z, profiles.support);
}
}
return gcode.getGCode();
}
2016-05-06 19:48:41 +02:00
2017-08-11 18:54:04 +02:00
function pathToGCode(outline, combing, gcode, shape, retract, unRetract, z, { lineProfile, travelProfile, retractionProfile }) {
2016-08-27 10:01:50 +02:00
const { closed } = shape;
const paths = shape.mapToLower();
2016-05-06 19:48:41 +02:00
2016-08-27 10:01:50 +02:00
for (let i = 0; i < paths.length; i ++) {
const line = paths[i];
const length = closed ? (line.length + 1) : line.length;
2016-05-06 19:48:41 +02:00
for (let i = 0; i < length; i ++) {
const point = line[i % line.length];
if (i === 0) {
2017-08-17 16:13:21 +02:00
if (combing) {
2017-08-24 10:55:36 +02:00
const combPath = comb(outline, gcode._nozzlePosition.divideScalar(PRECISION), point);
2017-08-11 18:54:04 +02:00
for (let i = 0; i < combPath.length; i ++) {
const combPoint = combPath[i];
gcode.moveTo(combPoint.x, combPoint.y, z, travelProfile);
}
} else {
gcode.moveTo(point.x, point.y, z, travelProfile);
}
2016-05-06 19:48:41 +02:00
if (unRetract) {
2017-07-27 18:33:25 +02:00
gcode.unRetract(retractionProfile);
2016-05-06 19:48:41 +02:00
}
} else {
2017-07-27 18:20:08 +02:00
gcode.lineTo(point.x, point.y, z, lineProfile);
2016-05-06 19:48:41 +02:00
}
}
}
if (retract) {
2017-07-27 18:33:25 +02:00
gcode.retract(retractionProfile);
2016-05-06 19:48:41 +02:00
}
}