snapmaker-lunar
Version:
294 lines (236 loc) • 9.41 kB
JavaScript
"use strict";
function _createForOfIteratorHelper(o, allowArrayLike) { var it = typeof Symbol !== "undefined" && o[Symbol.iterator] || o["@@iterator"]; if (!it) { if (Array.isArray(o) || (it = _unsupportedIterableToArray(o)) || allowArrayLike && o && typeof o.length === "number") { if (it) o = it; var i = 0; var F = function F() {}; return { s: F, n: function n() { if (i >= o.length) return { done: true }; return { done: false, value: o[i++] }; }, e: function e(_e) { throw _e; }, f: F }; } throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } var normalCompletion = true, didErr = false, err; return { s: function s() { it = it.call(o); }, n: function n() { var step = it.next(); normalCompletion = step.done; return step; }, e: function e(_e2) { didErr = true; err = _e2; }, f: function f() { try { if (!normalCompletion && it["return"] != null) it["return"](); } finally { if (didErr) throw err; } } }; }
function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }
function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) { arr2[i] = arr[i]; } return arr2; }
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); Object.defineProperty(Constructor, "prototype", { writable: false }); 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 MODEL_REPAIR = "model_repair";
var MODEL_CHECK = "model_check";
var MODEL_SIMPLIFY = "model_simplify";
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);
_defineProperty(this, "result", {
code: 0,
status: 0,
msg: ""
});
this.type(type);
this.input(inputPath);
this.output(outputPath);
this.settings(settingsPath);
}
_createClass(Slicer, [{
key: "slice",
value: function slice() {
console.log('slice ()');
this.result = {
code: 0,
status: 0,
msg: ""
};
var slice;
var engine;
if (this.sliceType === SLICE_3DP) {
slice = "slice";
engine = 'Slicer';
} else if (this.sliceType === MODEL_SUPPORT) {
slice = "support";
engine = 'Slicer';
} else if (this.sliceType === SLICE_LASER) {
slice = "slicelaser";
engine = 'TPP';
} else if (this.sliceType === SLICE_CNC) {
slice = "slicecnc";
engine = 'TPP';
} else if (this.sliceType === MODEL_REPAIR) {
slice = "modelrepair";
engine = 'MP';
} else if (this.sliceType === MODEL_CHECK) {
slice = "modelcheck";
engine = 'MP';
} else if (this.sliceType === MODEL_SIMPLIFY) {
slice = "modelsimplify";
engine = 'MP';
}
var args;
if (this.settingsPath) {
args = [slice, "-v", "-p", "-j", this.settingsPath, "-l", this.inputPath, "-o", this.outputPath];
} else {
args = [slice, "-v", "-p", "-l", this.inputPath, "-o", this.outputPath];
}
this.process = childProcess.spawn(getpath(engine), args);
console.log('cmd: ' + getpath(engine) + ' ' + args.join(' '));
return this;
}
}, {
key: "type",
value: function type(_type) {
this.sliceType = _type;
return this;
}
}, {
key: "settings",
value: function settings(settingsPath) {
this.settingsPath = settingsPath;
return this;
}
}, {
key: "input",
value: function input(inputPath) {
this.inputPath = inputPath;
return this;
}
}, {
key: "output",
value: function output(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) {
var _this = this;
if (key === 'data') {
this.stderrEvents['data'] = function (data) {
var array = data.toString().split('\n');
var _iterator = _createForOfIteratorHelper(array),
_step;
try {
for (_iterator.s(); !(_step = _iterator.n()).done;) {
var str = _step.value;
_this.getResult(str);
fn(str);
}
} catch (err) {
_iterator.e(err);
} finally {
_iterator.f();
}
};
} else {
this.stderrEvents[key] = fn;
}
return this;
}
}, {
key: "onStdout",
value: function onStdout(key, fn) {
var _this2 = this;
if (key === 'data') {
this.stdoutEvents['data'] = function (data) {
var array = data.toString().split('\n');
var _iterator2 = _createForOfIteratorHelper(array),
_step2;
try {
for (_iterator2.s(); !(_step2 = _iterator2.n()).done;) {
var str = _step2.value;
_this2.getResult(str);
fn(str);
}
} catch (err) {
_iterator2.e(err);
} finally {
_iterator2.f();
}
};
} else {
this.stdoutEvents[key] = fn;
}
return this;
}
}, {
key: "getResult",
value: function getResult(str) {
if (str.indexOf('Status') !== -1) {
this.result.status = str.split(':')[1] ? parseInt(str.split(':')[1].trim()) : 0;
}
if (str.indexOf('Message') !== -1) {
this.result.msg = str.split(':')[1] ? str.split(':')[1].trim() : '';
}
}
}, {
key: "end",
value: function end() {
var _this3 = this;
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'
};
if (code === null) {
_this3.result.code = 139;
_this3.result.msg = signal;
} else {
_this3.result.msg = codeErrorMsg[code] || '';
}
if (code === 0) {
fn(null, _this3.result);
} else {
fn(_this3.result, null);
}
});
}
}]);
return Slicer;
}();
module.exports = {
Slicer: Slicer,
SLICE_3DP: SLICE_3DP,
SLICE_LASER: SLICE_LASER,
SLICE_CNC: SLICE_CNC,
MODEL_SUPPORT: MODEL_SUPPORT,
MODEL_REPAIR: MODEL_REPAIR,
MODEL_CHECK: MODEL_CHECK,
MODEL_SIMPLIFY: MODEL_SIMPLIFY
};