0

I am working on a project that converts an image to its respective Gcode. But when i run it for simple shapes (square, circle etc) the gcode generated fills the interior of the shapes, which is not needed. i realized the error is in one file but i am not able to figure it out. A little help would be greatfull. Thanks in advance

the link to the svg file : https://drive.google.com/open?id=1FqcJA4dSPS11ubE9OAPGgNxPogQ1mKoX

i have attached the code and the image that i am using belowenter image description here

'use strict';

const fs = require('fs');
const GCanvas = require('gcanvas');
const canvg = require('canvg');

/**
 * Generates Gcode from an svg
 * DO NOT USE THIS FUNCTION: Use the <code>gcodeGenerator</code>
 * in gcodegenerator.js.
 * This function causes a ghost thread to run asynchronously
 * @param {String} filePath The path to the svg file
 * @param {*} options options for the GCode
 * @return {Promise} A promise containing the gcode as a String
 */
const getGcode = (filePath, options) => {
  let ret = '';
  let oldLog = null;
  if (require.main != module) {
    // hijack console.log if not running as the main module
    // process the data being sent to console as a String
    oldLog = console.log;
    console.log = (msg) => {
      ret += msg + '\n';
    };
  }

  const gctx = new GCanvas();
  const svg = fs.readFileSync(filePath).toString();

  if (options.hasOwnProperty('speed')) {
    gctx.speed = options.speed;
  }

  if (options.hasOwnProperty('feed')) {
    gctx.feed = options.feed;
  }

  if (options.hasOwnProperty('depth')) {
    gctx.depth = options.depth;
  }

  if (options.hasOwnProperty('depthOfCut')) {
    gctx.depthOfCut = options.depthOfCut;
  }

  if (options.hasOwnProperty('top')) {
    gctx.top = options.top;
    if (gctx.top > 0) {
      gctx.motion.rapid({z: 0});
    } else {
      gctx.motion.retract();
    }
  } else if (options.hasOwnProperty('retract')) {
    gctx.retract = options.retract;
  }

  if (options.hasOwnProperty('above')) {
    gctx.aboveTop = options.above;
  }

  if (options.hasOwnProperty('toolDiameter')) {
    gctx.toolDiameter = options.toolDiameter;
  } else {
    throw new Error('Specifying tool diameter is necessary');
  }

  if (options.hasOwnProperty('positive') && options.positive === true) {
    gctx.fill = (windingRule, depth) => {
      gctx.save();
      gctx.strokeStyle = gctx.fillStyle;
      gctx.stroke('outer', depth);
      gctx.restore();
    };
  }

  return new Promise((resolve, reject) => {
    canvg(gctx.canvas, svg);
    ret += 'M30\n';
    if (oldLog != null) {
      console.log = oldLog;
    }
    resolve(ret);
  });
};

if (require.main == module) {
  const program = require('commander');

  program.version(require('../package.json').version)
      .usage('[options] <file ...>')
      .option('-s, --speed <number>', 'spindle speed', eval)
      .option('-f, --feed <number>', 'feed rate', eval)
      .option('-d, --depth <number>', 'z of final cut depth', eval)
      .option('-c, --depthofcut <number>', 'z offset of layered cuts', eval)
      .option('-t, --top <number>', 'z of top of work surface', eval)
      .option('-a, --above <number>', 'z of safe area above the work', eval)
      .option('-D, --tooldiameter <number>', 'diameter of tool', eval)
      .option('-r, --retract <number>', 'distance to retract for fast moves',
          eval)
      .option('-p, --positive',
          'Treat fill as positive, cutting only around the outside');

  program.parse(process.argv);

  const options = {};
  if (program.speed) options.speed = program.speed;
  if (program.feed) options.feed = program.feed;
  if (program.depth) options.depth = program.depth;
  if (program.depthofcut) options.depthOfCut = program.depthofcut;
  if (program.top) options.top = program.top;
  if (program.above) options.aboveTop = program.above;
  if (program.tooldiameter) options.toolDiameter = program.tooldiameter;
  if (program.retract) options.retract = program.retract;

  if (program.args.length === 0 && process.stdin.isTTY) {
    program.outputHelp();
    process.exit(0);
  }

  if (!process.stdin.isTTY) {
    getGcode('/dev/stdin', options);
  }

  program.args.forEach(function(file) {
    getGcode(file, options);
  });

  process.exit(0);
}

exports.getGcode = getGcode;
  • This is supposed to convert an svg file to g code? Can you provide the svg file? A png file isn't going to be useful if the code requires svg. – Ouroborus Mar 06 '19 at 07:44
  • yes, i think there is an error in this file but am not able to fix it – Tarun Karthik Mar 06 '19 at 10:57
  • Dear @TarunKarthik first of all your question is not well formatted. secondly the SVG file you have provided is not a circle but a collection of arcs forming two circles! Just open in in Inkscape. see [this image](https://i.imgur.com/11jMitm.png). This is not how you make a circle in SVG, see [this](https://www.w3schools.com/graphics/svg_circle.asp) for example. There are like a dozen of [software](https://www.reddit.com/r/CNC/comments/aizatc/free_and_open_source_camcnc_software/) out there to convert SVG to gcode. maybe start by looking into their source code first. – Foad S. Farimani Apr 08 '19 at 22:21

0 Answers0