@nebulaglitch/gcode
Version:
A library to generate gcode output
484 lines (446 loc) • 11.8 kB
JavaScript
'use strict';
Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
const numeral = require('numeral');
function _define_property$4(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;
}
class Argument {
processValue() {
let result = this.value;
if (typeof this.value === 'number') {
if (Number.isInteger(this.value)) {
result = this.value;
} else if (isNaN(this.value)) {
result = '';
} else if (!isFinite(this.value)) {
result = '';
} else {
result = numeral(this.value).format('0.0000');
}
}
return result;
}
toString() {
const output = this.label + this.processValue();
return output;
}
constructor(label, value){
_define_property$4(this, "label", void 0);
_define_property$4(this, "value", void 0);
this.label = label;
this.value = value;
}
}
function _define_property$3(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;
}
class Command {
processArgs() {
const processedArgs = this.args.map((current)=>{
let result = current;
if (typeof current === 'number') {
if (Number.isInteger(current)) {
result = current;
} else if (isNaN(current)) {
result = '';
} else if (!isFinite(current)) {
result = '';
} else {
result = numeral(current).format('0.0000');
}
} else if (current instanceof Argument) {
result = current.toString();
}
return result;
});
return processedArgs;
}
toString() {
let output = `${this.code}${this.codeNumber}`;
if (this.args.length > 0) {
output += ' ' + this.processArgs().join(' ');
}
return output;
}
constructor(code, codeNumber = ''){
_define_property$3(this, "code", void 0);
_define_property$3(this, "codeNumber", void 0);
_define_property$3(this, "args", void 0);
this.code = code;
this.codeNumber = codeNumber;
this.args = [];
}
}
function _define_property$2(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;
}
class Comment extends Command {
toString() {
return (this.newline ? '\n' : '') + this.code + ' ' + this.args.join(' ') + ' ]';
}
constructor(content, newline = true){
super('['), _define_property$2(this, "newline", false);
this.newline = newline;
this.args.push(content.toString());
}
}
function _define_property$1(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;
}
class CmdGroup extends Array {
get name() {
return this.mName;
}
set name(name) {
this.mName = name;
}
constructor(name, ...args){
super(...args), _define_property$1(this, "mName", void 0);
this.mName = name;
this.push(new Comment(name));
}
}
class GcodePrinter {
static print(arr, sb = '') {
arr.forEach((current)=>{
if (current instanceof Command) {
sb += current.toString();
} else if (current instanceof Array) {
sb += '\n';
sb = GcodePrinter.print(current, sb);
}
sb += '\n';
});
return sb;
}
}
class FeedVelocity extends Command {
constructor(velocity = 100){
super('F', velocity);
}
}
class SpindleSpeed extends Command {
constructor(speed = 100){
super('S', speed);
}
}
class RapidMove extends Command {
constructor(x = null, y = null, z = null, w = null){
super('G', 0);
if (x !== null) {
this.args.push(new Argument('X', x));
}
if (y !== null) {
this.args.push(new Argument('Y', y));
}
if (z !== null) {
this.args.push(new Argument('Z', z));
}
if (w !== null) {
this.args.push(new Argument('W', w));
}
}
}
class FeedMove extends Command {
constructor(x = null, y = null, z = null, w = null, f = null){
super('G', 1);
if (x !== null) {
this.args.push(new Argument('X', x));
}
if (y !== null) {
this.args.push(new Argument('Y', y));
}
if (z !== null) {
this.args.push(new Argument('Z', z));
}
if (w !== null) {
this.args.push(new Argument('W', w));
}
if (f !== null) {
this.args.push(new Argument('F', f));
}
}
}
class Dwell extends Command {
constructor(time = 3){
super('G4X', time);
}
}
class ClockwiseArc extends Command {
constructor(x = null, y = null, r = null){
super('G', 2);
if (x !== null) {
this.args.push(new Argument('X', x));
}
if (y !== null) {
this.args.push(new Argument('Y', y));
}
if (r !== null) {
this.args.push(new Argument('R', r));
}
}
}
class CounterClockwiseArc extends Command {
constructor(x = null, y = null, r = null){
super('G', 3);
if (x !== null) {
this.args.push(new Argument('X', x));
}
if (y !== null) {
this.args.push(new Argument('Y', y));
}
if (r !== null) {
this.args.push(new Argument('R', r));
}
}
}
class SubprogramCall extends Command {
constructor(filename = null){
super('M', 98);
if (filename !== null) {
this.args.push(new Argument('', filename));
}
}
}
class SetLocalCoordinates extends Command {
constructor(x = null, y = null, z = null){
super('G', 92);
if (x !== null) {
this.args.push(new Argument('X', x));
}
if (y !== null) {
this.args.push(new Argument('Y', y));
}
if (z !== null) {
this.args.push(new Argument('Z', z));
}
}
}
class MacroCall extends Command {
constructor(number = 0){
super('M', number);
}
}
class GMacroCall extends Command {
constructor(number = 0){
super('G', number);
}
}
class ToolSelect extends Command {
constructor(tool = 2){
super('T', tool);
}
}
class Question extends Command {
constructor(text = null){
super('G', 5);
this.args.push(new Argument('T', 0));
if (text !== null) {
this.args.push(new Argument('m', ' "' + text + '"'));
}
}
}
class JogZ extends RapidMove {
constructor(z = null){
super(null, null, z, null);
}
}
class JogY extends RapidMove {
constructor(y = null){
super(null, y, null, null);
}
}
class JogW extends RapidMove {
constructor(w = null){
super(null, null, null, w);
}
}
class Jog2 extends RapidMove {
constructor(x = null, y = null){
super(x, y, null, null);
}
}
class Jog4 extends RapidMove {
constructor(x = null, y = null, z = null, w = null){
super(x, y, z, w);
}
}
class Move2 extends FeedMove {
constructor(x = null, y = null){
super(x, y, null, null);
}
}
class MoveZ extends FeedMove {
constructor(z = null){
super(null, null, z, null);
}
}
class MoveX extends FeedMove {
constructor(x = null){
super(x, null, null, null);
}
}
class MoveY extends FeedMove {
constructor(y = null){
super(null, y, null, null);
}
}
class MoveW extends FeedMove {
constructor(w = null){
super(null, null, null, w);
}
}
class CutGCircle extends Command {
constructor(d = null, x = null, y = null, unknown1 = null, unknown2 = null, unknown3 = null, direction = null, i = null, j = null){
super('G', direction == 1 ? 2 : 3);
if (x !== null) {
this.args.push(new Argument('X', x));
}
if (y !== null) {
this.args.push(new Argument('Y', y));
}
if (d !== null) {
this.args.push(new Argument('R', d / 2));
}
if (i !== null) {
this.args.push(new Argument('I', i));
}
if (j !== null) {
this.args.push(new Argument('J', j));
}
}
}
class AbsoluteMode extends Command {
constructor(){
super('G', 90);
}
}
class Literal extends Command {
toString() {
return this.args.join(' ');
}
constructor(content){
super('');
this.args.push(content);
}
}
function _define_property(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;
}
class Mach3Comment extends Command {
toString() {
return (this.newline ? '\n' : '') + this.code + ' ' + this.args.join(' ') + ')';
}
constructor(content, newline = true){
super('('), _define_property(this, "newline", void 0);
this.newline = newline;
this.args.push(content.toString());
}
}
class ArcMove extends Command {
constructor(clockwise = true, x = null, y = null, i = null, j = null){
super('G', clockwise ? 2 : 3);
if (x !== null) {
this.args.push(new Argument('X', x));
}
if (y !== null) {
this.args.push(new Argument('Y', y));
}
if (i !== null) {
this.args.push(new Argument('I', i));
}
if (j !== null) {
this.args.push(new Argument('J', j));
}
}
}
const name = 'Gcode', version = '0.0.1';
const myObject = {
name,
version
};
exports.AbsoluteMode = AbsoluteMode;
exports.ArcMove = ArcMove;
exports.ClockwiseArc = ClockwiseArc;
exports.CmdGroup = CmdGroup;
exports.Command = Command;
exports.Comment = Comment;
exports.CounterClockwiseArc = CounterClockwiseArc;
exports.CutGCircle = CutGCircle;
exports.Dwell = Dwell;
exports.FeedMove = FeedMove;
exports.FeedVelocity = FeedVelocity;
exports.GMacroCall = GMacroCall;
exports.GcodePrinter = GcodePrinter;
exports.Jog2 = Jog2;
exports.Jog4 = Jog4;
exports.JogW = JogW;
exports.JogY = JogY;
exports.JogZ = JogZ;
exports.Literal = Literal;
exports.Mach3Comment = Mach3Comment;
exports.MacroCall = MacroCall;
exports.Move2 = Move2;
exports.MoveW = MoveW;
exports.MoveX = MoveX;
exports.MoveY = MoveY;
exports.MoveZ = MoveZ;
exports.Question = Question;
exports.RapidMove = RapidMove;
exports.SetLocalCoordinates = SetLocalCoordinates;
exports.SpindleSpeed = SpindleSpeed;
exports.SubprogramCall = SubprogramCall;
exports.ToolSelect = ToolSelect;
exports.myObject = myObject;
exports.name = name;
//# sourceMappingURL=index.cjs.map