snapmaker-luban-engine
Version:
229 lines (182 loc) • 6.55 kB
JavaScript
"use strict";
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } }
function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; }
function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
var fs = require("fs");
var childProcess = require("child_process");
var getpath = require("./getpath.js");
var SLICE_3DP = "3dp";
var SLICE_LASER = "laser";
var SLICE_CNC = "cnc";
var MODEL_SUPPORT = "model_support";
var loop = function loop() {};
var Slicer = /*#__PURE__*/function () {
function Slicer(type, inputPath, outputPath, settingsPath) {
_classCallCheck(this, Slicer);
_defineProperty(this, "sliceType", void 0);
_defineProperty(this, "inputPath", void 0);
_defineProperty(this, "outputPath", void 0);
_defineProperty(this, "settingsPath", void 0);
_defineProperty(this, "process", void 0);
_defineProperty(this, "events", {});
_defineProperty(this, "stdoutEvents", {});
_defineProperty(this, "stderrEvents", {});
_defineProperty(this, "timeout", -1);
this.type(type);
this.input(inputPath);
this.output(outputPath);
this.settings(settingsPath);
}
_createClass(Slicer, [{
key: "slice",
value: function slice() {
this.checkParams();
var slice;
if (this.sliceType === SLICE_3DP) {
slice = "slice";
} else if (this.sliceType === SLICE_LASER) {
slice = "slicelaser";
} else if (this.sliceType === SLICE_CNC) {
slice = "slicecnc";
} else if (this.sliceType === MODEL_SUPPORT) {
slice = "modelsupport";
}
var args = [slice, "-v", "-p", "-j", this.settingsPath, "-l", this.inputPath, "-o", this.outputPath];
this.process = childProcess.spawn(getpath(), args);
return this;
}
}, {
key: "type",
value: function type(_type) {
if (![SLICE_3DP, SLICE_LASER, SLICE_CNC, MODEL_SUPPORT].includes(_type)) {
throw new Error("Slice type can only be one of 3dp, laser, cnc, model_support wrong parameter: " + _type);
}
this.sliceType = _type;
return this;
}
}, {
key: "settings",
value: function settings(settingsPath) {
if (!fs.existsSync(settingsPath)) {
throw new Error("Settings path does not exist: " + settingsPath);
}
this.settingsPath = settingsPath;
return this;
}
}, {
key: "input",
value: function input(inputPath) {
if (!fs.existsSync(inputPath)) {
throw new Error("Input path does not exist: " + inputPath);
}
this.inputPath = inputPath;
return this;
}
}, {
key: "checkParams",
value: function checkParams() {
if (!this.sliceType || !this.settingsPath || !this.inputPath || !this.outputPath) {
throw new Error("Incomplete parameters.");
}
if (![SLICE_3DP, SLICE_LASER, SLICE_CNC, MODEL_SUPPORT].includes(this.sliceType)) {
throw new Error("Slice type can only be one of 3dp, laser, cnc, wrong parameter: " + this.sliceType);
}
if (!fs.existsSync(this.settingsPath)) {
throw new Error("Settings path does not exist: " + this.settingsPath);
}
if (!fs.existsSync(this.inputPath)) {
throw new Error("Input path does not exist: " + this.inputPath);
}
if (!fs.existsSync(this.outputPath)) {
throw new Error("Output path does not exist: " + this.outputPath);
}
}
}, {
key: "output",
value: function output(outputPath) {
if (!fs.existsSync(outputPath)) {
throw new Error("Output path does not exist: " + outputPath);
}
this.outputPath = outputPath;
return this;
}
}, {
key: "on",
value: function on(key, fn) {
this.events[key] = fn;
return this;
}
}, {
key: "onStderr",
value: function onStderr(key, fn) {
this.stderrEvents[key] = fn;
return this;
}
}, {
key: "onStdout",
value: function onStdout(key, fn) {
this.stdoutEvents[key] = fn;
return this;
}
}, {
key: "end",
value: function end() {
var fn = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : loop;
if (!this.process) {
this.slice();
}
for (var _i = 0, _Object$keys = Object.keys(this.events); _i < _Object$keys.length; _i++) {
var key = _Object$keys[_i];
if (typeof this.events[key] !== "function") {
continue;
}
this.process.on(key, this.events[key]);
}
for (var _i2 = 0, _Object$keys2 = Object.keys(this.stdoutEvents); _i2 < _Object$keys2.length; _i2++) {
var _key = _Object$keys2[_i2];
if (typeof this.stdoutEvents[_key] !== "function") {
continue;
}
this.process.stdout.on(_key, this.stdoutEvents[_key]);
}
for (var _i3 = 0, _Object$keys3 = Object.keys(this.stderrEvents); _i3 < _Object$keys3.length; _i3++) {
var _key2 = _Object$keys3[_i3];
if (typeof this.stderrEvents[_key2] !== "function") {
continue;
}
this.process.stderr.on(_key2, this.stderrEvents[_key2]);
}
this.process.on('close', function (code, signal) {
var codeErrorMsg = {
'1': 'Failed to load model',
'2': 'Error for Settings'
};
var msg;
if (code === null) {
code = 139;
msg = signal;
} else {
msg = codeErrorMsg[code];
}
var result = {
code: code,
msg: msg
};
if (code === 0) {
fn(null, result);
} else {
fn(result, null);
}
});
}
}]);
return Slicer;
}();
module.exports = {
Slicer: Slicer,
SLICE_3DP: SLICE_3DP,
SLICE_LASER: SLICE_LASER,
SLICE_CNC: SLICE_CNC,
MODEL_SUPPORT: MODEL_SUPPORT
};