Doodle3D-Slicer/src/sliceActions/slicesToGCode.js

108 lines
2.9 KiB
JavaScript
Raw Normal View History

2017-07-19 11:02:42 +02:00
import GCode from './helpers/GCode.js';
2017-07-27 18:20:08 +02:00
const PROFILE_TYPES = ['brim', 'outerLine', 'innerLine', 'fill', 'support'];
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,
retractionEnabled
} = 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 = {
travelProfile: {
speed: travelSpeed
},
retractProfile: {
...retraction,
enabled: retractionEnabled
}
};
2016-04-23 09:56:15 +02:00
2017-07-27 18:20:08 +02:00
let isBottom = 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:20:08 +02:00
isBottom = false;
}
2017-07-27 18:20:08 +02:00
const profiles = PROFILE_TYPES.reduce((profiles, profileType) => {
profiles[profileType] = {
...defaultProfile,
lineProfile: isBottom ? settings.bottom : settings[profileType]
}
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) {
2017-07-27 18:20:08 +02:00
pathToGCode(gcode, part.outerLine, false, true, profiles.outerLine);
2016-04-23 09:56:15 +02:00
for (let i = 0; i < part.innerLines.length; i ++) {
const innerLine = part.innerLines[i];
2017-07-27 18:20:08 +02:00
pathToGCode(gcode, innerLine, false, false, z, profiles.innerLine);
}
2017-07-27 18:20:08 +02:00
pathToGCode(gcode, part.fill, true, false, z, profiles.fill);
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:20:08 +02:00
pathToGCode(gcode, part.shape, retract, retract, z, profiles.outerLine);
}
}
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:20:08 +02:00
function pathToGCode(gcode, shape, retract, unRetract, z, { lineProfile, travelProfile, retractProfile }) {
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:20:08 +02:00
gcode.unRetract(retractProfile);
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:20:08 +02:00
gcode.retract(retractProfile);
2016-05-06 19:48:41 +02:00
}
}