(function (global, factory) { if (typeof define === "function" && define.amd) { define(["exports", "three"], factory); } else if (typeof exports !== "undefined") { factory(exports, require("three")); } else { var mod = { exports: {} }; factory(mod.exports, global.three); global.GCodeLoader = mod.exports; } })(typeof globalThis !== "undefined" ? globalThis : typeof self !== "undefined" ? self : this, function (_exports, _three) { "use strict"; Object.defineProperty(_exports, "__esModule", { value: true }); _exports.GCodeLoader = void 0; 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 _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function"); } Object.defineProperty(subClass, "prototype", { value: Object.create(superClass && superClass.prototype, { constructor: { value: subClass, writable: true, configurable: true } }), writable: false }); if (superClass) _setPrototypeOf(subClass, superClass); } function _setPrototypeOf(o, p) { _setPrototypeOf = Object.setPrototypeOf || function _setPrototypeOf(o, p) { o.__proto__ = p; return o; }; return _setPrototypeOf(o, p); } function _createSuper(Derived) { var hasNativeReflectConstruct = _isNativeReflectConstruct(); return function _createSuperInternal() { var Super = _getPrototypeOf(Derived), result; if (hasNativeReflectConstruct) { var NewTarget = _getPrototypeOf(this).constructor; result = Reflect.construct(Super, arguments, NewTarget); } else { result = Super.apply(this, arguments); } return _possibleConstructorReturn(this, result); }; } function _possibleConstructorReturn(self, call) { if (call && (typeof call === "object" || typeof call === "function")) { return call; } else if (call !== void 0) { throw new TypeError("Derived constructors may only return object or undefined"); } return _assertThisInitialized(self); } function _assertThisInitialized(self) { if (self === void 0) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; } function _isNativeReflectConstruct() { if (typeof Reflect === "undefined" || !Reflect.construct) return false; if (Reflect.construct.sham) return false; if (typeof Proxy === "function") return true; try { Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () {})); return true; } catch (e) { return false; } } function _getPrototypeOf(o) { _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf : function _getPrototypeOf(o) { return o.__proto__ || Object.getPrototypeOf(o); }; return _getPrototypeOf(o); } /** * GCodeLoader is used to load gcode files usually used for 3D printing or CNC applications. * * Gcode files are composed by commands used by machines to create objects. * * @class GCodeLoader * @param {Manager} manager Loading manager. */ var GCodeLoader = /*#__PURE__*/function (_Loader) { _inherits(GCodeLoader, _Loader); var _super = _createSuper(GCodeLoader); function GCodeLoader(manager) { var _this; _classCallCheck(this, GCodeLoader); _this = _super.call(this, manager); _this.splitLayer = false; return _this; } _createClass(GCodeLoader, [{ key: "load", value: function load(url, onLoad, onProgress, onError) { var scope = this; var loader = new _three.FileLoader(scope.manager); loader.setPath(scope.path); loader.setRequestHeader(scope.requestHeader); loader.setWithCredentials(scope.withCredentials); loader.load(url, function (text) { try { onLoad(scope.parse(text)); } catch (e) { if (onError) { onError(e); } else { console.error(e); } scope.manager.itemError(url); } }, onProgress, onError); } }, { key: "parse", value: function parse(data) { var state = { x: 0, y: 0, z: 0, e: 0, f: 0, extruding: false, relative: false }; var layers = []; var currentLayer = undefined; var pathMaterial = new _three.LineBasicMaterial({ color: 0xFF0000 }); pathMaterial.name = 'path'; var extrudingMaterial = new _three.LineBasicMaterial({ color: 0x00FF00 }); extrudingMaterial.name = 'extruded'; function newLayer(line) { currentLayer = { vertex: [], pathVertex: [], z: line.z }; layers.push(currentLayer); } //Create lie segment between p1 and p2 function addSegment(p1, p2) { if (currentLayer === undefined) { newLayer(p1); } if (state.extruding) { currentLayer.vertex.push(p1.x, p1.y, p1.z); currentLayer.vertex.push(p2.x, p2.y, p2.z); } else { currentLayer.pathVertex.push(p1.x, p1.y, p1.z); currentLayer.pathVertex.push(p2.x, p2.y, p2.z); } } function delta(v1, v2) { return state.relative ? v2 : v2 - v1; } function absolute(v1, v2) { return state.relative ? v1 + v2 : v2; } var lines = data.replace(/;.+/g, '').split('\n'); var _loop = function _loop(i) { var tokens = lines[i].split(' '); var cmd = tokens[0].toUpperCase(); //Argumments var args = {}; tokens.splice(1).forEach(function (token) { if (token[0] !== undefined) { var key = token[0].toLowerCase(); var value = parseFloat(token.substring(1)); args[key] = value; } }); //Process commands //G0/G1 – Linear Movement if (cmd === 'G0' || cmd === 'G1') { var line = { x: args.x !== undefined ? absolute(state.x, args.x) : state.x, y: args.y !== undefined ? absolute(state.y, args.y) : state.y, z: args.z !== undefined ? absolute(state.z, args.z) : state.z, e: args.e !== undefined ? absolute(state.e, args.e) : state.e, f: args.f !== undefined ? absolute(state.f, args.f) : state.f }; //Layer change detection is or made by watching Z, it's made by watching when we extrude at a new Z position if (delta(state.e, line.e) > 0) { state.extruding = delta(state.e, line.e) > 0; if (currentLayer == undefined || line.z != currentLayer.z) { newLayer(line); } } addSegment(state, line); state = line; } else if (cmd === 'G2' || cmd === 'G3') {//G2/G3 - Arc Movement ( G2 clock wise and G3 counter clock wise ) //console.warn( 'THREE.GCodeLoader: Arc command not supported' ); } else if (cmd === 'G90') { //G90: Set to Absolute Positioning state.relative = false; } else if (cmd === 'G91') { //G91: Set to state.relative Positioning state.relative = true; } else if (cmd === 'G92') { //G92: Set Position var _line = state; _line.x = args.x !== undefined ? args.x : _line.x; _line.y = args.y !== undefined ? args.y : _line.y; _line.z = args.z !== undefined ? args.z : _line.z; _line.e = args.e !== undefined ? args.e : _line.e; state = _line; } else {//console.warn( 'THREE.GCodeLoader: Command not supported:' + cmd ); } }; for (var i = 0; i < lines.length; i++) { _loop(i); } function addObject(vertex, extruding, i) { var geometry = new _three.BufferGeometry(); geometry.setAttribute('position', new _three.Float32BufferAttribute(vertex, 3)); var segments = new _three.LineSegments(geometry, extruding ? extrudingMaterial : pathMaterial); segments.name = 'layer' + i; object.add(segments); } var object = new _three.Group(); object.name = 'gcode'; if (this.splitLayer) { for (var _i = 0; _i < layers.length; _i++) { var layer = layers[_i]; addObject(layer.vertex, true, _i); addObject(layer.pathVertex, false, _i); } } else { var vertex = [], pathVertex = []; for (var _i2 = 0; _i2 < layers.length; _i2++) { var _layer = layers[_i2]; var layerVertex = _layer.vertex; var layerPathVertex = _layer.pathVertex; for (var j = 0; j < layerVertex.length; j++) { vertex.push(layerVertex[j]); } for (var _j = 0; _j < layerPathVertex.length; _j++) { pathVertex.push(layerPathVertex[_j]); } } addObject(vertex, true, layers.length); addObject(pathVertex, false, layers.length); } object.quaternion.setFromEuler(new _three.Euler(-Math.PI / 2, 0, 0)); return object; } }]); return GCodeLoader; }(_three.Loader); _exports.GCodeLoader = GCodeLoader; });