Git Product home page Git Product logo

gcode-interpreter's Introduction

gcode-interpreter build status Coverage Status

NPM

Install

npm install --save gcode-interpreter

Usage

const Interpreter = require('gcode-interpreter');

const Runner = function() {
    const handlers = {
        'G0': (params) => {
            console.log('G0', params);
        },
        'G1': (params) => {
            console.log('G1', params);
        }
    };

    return new Interpreter({
        handlers: handlers,
        defaultHandler: (cmd, params) => {
        }
    });
};

const runner = new Runner()
const file = 'example.nc';
const stream = fs.createReadStream(file, { encoding: 'utf8' });
const content = fs.readFileSync(file, 'utf8');

// Load G-code from stream
runner.loadFromStream(stream, function(err, data) {
});

// loadFromFile
runner.loadFromFile(file, function(err, data) {
});

// Synchronous version of loadFromFile
runner.loadFromFileSync(file);

// loadFromString
const content = fs.readFileSync(file, 'utf8');
runner.loadFromString(content, function(err, data) {
});

// Synchronous version of loadFromString
runner.loadFromStringSync(content);

Examples

Run this example with babel-node:

import Interpreter from 'gcode-interpreter';

const GCODE = [
    'N1 G17 G20 G90 G94 G54',
    'N2 G0 Z0.25',
    'N3 X-0.5 Y0.',
    'N4 Z0.1',
    'N5 G01 Z0. F5.',
    'N6 G02 X0. Y0.5 I0.5 J0. F2.5',
    'N7 X0.5 Y0. I0. J-0.5',
    'N8 X0. Y-0.5 I-0.5 J0.',
    'N9 X-0.5 Y0. I0. J0.5',
    'N10 G01 Z0.1 F5.',
    'N11 G00 X0. Y0. Z0.25'
].join('\n');

class Toolpath {
    handlers = {
        'G0': (params) => {
            console.log('G0', params);
        },
        'G1': (params) => {
            console.log('G1', params);
        },
        'G2': (params) => {
            console.log('G2', params);
        },
        'G17': (params) => {
            console.log('G17');
        },
        'G20': (params) => {
            console.log('G20');
        },
        'G90': (params) => {
            console.log('G90');
        },
        'G94': (params) => {
            console.log('G94');
        },
        'G54': (params) => {
            console.log('G54');
        }
    };

    constructor(options) {
        options = options || {};

        return new Interpreter({
            handlers: this.handlers,
            defaultHandler: (cmd, params) => {
            }
        });
    }
}

const toolpath = new Toolpath();

toolpath
    .loadFromString(GCODE, (err, results) => {
        if (err) {
            console.error(err);
            return;
        }
    })
    .on('data', (data) => {
        // 'data' event listener
    })
    .on('end', (results) => {
        // 'end' event listener
    });

and you will see the output as below:

G17
G20
G90
G94
G54
G0 { Z: 0.25 }
G0 { X: -0.5, Y: 0 }
G0 { Z: 0.1 }
G1 { Z: 0, F: 5 }
G2 { X: 0, Y: 0.5, I: 0.5, J: 0, F: 2.5 }
G2 { X: 0.5, Y: 0, I: 0, J: -0.5 }
G2 { X: 0, Y: -0.5, I: -0.5, J: 0 }
G2 { X: -0.5, Y: 0, I: 0, J: 0.5 }
G1 { Z: 0.1, F: 5 }
G0 { X: 0, Y: 0, Z: 0.25 }

G-code Toolpath

https://github.com/cncjs/gcode-toolpath

License

MIT

gcode-interpreter's People

Contributors

cheton avatar jmsgomes avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar

gcode-interpreter's Issues

Feature Request: Default handler for when codes don't have specific handlers

As I understand it, currently only handlers specific for each command will be invoked when each G-Code line is interpreted. It would be useful if we could also provide a defaultHandler function (that accepts cmd and args as parameters) that would be called when a command does not have a specific handler.

For example,

const interpreter = new Interpreter(
  {
      handlers: { /** handlers for specific command */ }, 
      defaultHandler: (cmd, args) => {},  // Called when no handler is found for a command.
  }
);

tiny gcode visualizer lib for tiny webserver

sorry to open ticket for my question but actually I'm looking for light gcode visualizer or probably cad2gcode convertor in my webpage , but this webpage hosted on tiny ESP826612F module which is connected to Arduino GRBL gcode interpreter by serial line, so I should have great care about its weight , My plan is just connect to wifi module and open the browser and do designated CAM job.,As I'm not pro in client side script like JavaScript I was hopping could have good advise here, do you suggest to make some plugin from such library so I shouldn't have to download or call all library ?
thanks

Exception in parsing

screenshot-2022-10-15-1665856225

I'm using gcode-toolpath in sandify.org to read imported gcode.

I am testing with firefox, but I see the same error in chrome.

The code we wrote to use gcode-toolpath is here:

https://github.com/jeffeb3/sandify/blob/master/src/features/importer/GCodeImporter.js#L98

The exception is thrown in readable-stream. I am using 3.6.0 and node 16. I have tried a few different versions without a fix.

I don't think any of your gcode tools have changed. I am a C++ developer in my day job, so I am a little out of my skill set with this bug. I would love any help I can get to point me in the right direction.

If you want to reproduce the bug in sandify, you have to click the "import" button and then the bigger import button, then any gcode file (I made one from the example in gcode-toolpath). The UI just sits there like nothing happens, but you can see the exception in the console. The backtrace in the live site is useless, but in my local install, it shows the backtrace I pasted in the screenshot.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.