Doodle3D-Slicer/src/sliceActions/slicesToGCode.js

106 lines
3.0 KiB
JavaScript
Raw Normal View History

2017-07-19 11:02:42 +02:00
import GCode from './helpers/GCode.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-07-27 18:33:25 +02:00
travel
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-07-27 18:20:08 +02:00
const z = layer * layerHeight + 0.2;
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-07-27 18:20:08 +02:00
pathToGCode(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];
2016-04-21 22:14:22 +02:00
if (part.shape.closed) {
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;
pathToGCode(gcode, shell, false, unRetract, z, profile);
}
pathToGCode(gcode, part.outerFill, false, false, z, profiles.outerInfill);
pathToGCode(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-07-27 18:33:25 +02:00
pathToGCode(gcode, part.shape, retract, retract, z, profiles.outerShell);
}
}
2017-06-22 10:19:15 +02:00
if (typeof slice.support !== 'undefined') {
2017-07-27 18:20:08 +02:00
pathToGCode(gcode, slice.support, true, true, z, profiles.support);
}
}
return gcode.getGCode();
}
2016-05-06 19:48:41 +02:00
2017-07-27 18:33:25 +02:00
function pathToGCode(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) {
// TODO
// moveTo should impliment combing
2017-07-27 18:20:08 +02:00
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
}
}