Home > CNC > Path Optimizer for pcb-gcode

Path Optimizer for pcb-gcode

The last couple of days I used the CNC machine to create a PCB for a project I am working on. As the CNC was etching away the copper, I noticed that between paths (i.e. while the spindle was not cutting) there was a lot of unnecessary travel. This happened quite a few times during my 35 minute job. Because I am using slow feedrate, about 10in/min, that unnecessary travel must have added 2-3 minutes. So if you are here looking how to optimize gcode you are at the right place :)

So to solve this, we need some optimization. What a better thing to spend your afternoon than firing up python, calling a friend and colleague over your PC, and solve this problem. So me and Mike, decided to solve this the easiest and quickest way possible, just by using brute force method to combine the different paths together. The goal is of course to minimize the unnecessary travel of the spindle.

Since this is a python script, it is multi platform. I personally tested it on my Ubuntu and Windows XP.

Some results

Let me just show you a couple of screen shots of a job, run on a simulator. This is the PCB of my last project. First let’s see the screenshot before optimizing the code

Spindle path before optimization

The white line represents the travelling between two paths. As you can see there are a lot of those white lines! :) Now let’s see the second screenshot which is the same PCB job, but this time with optimized gcode

The path after optimization

As you can see the white lines are reduced. Also notice the clock (below the STOP sign)  of the simulator. We just saved a few minutes!

The program

First of all we assume that you already installed python on your system. Download the optimizer script. There is no GUI yet (feel free to expand the code, use git) so we need to run it from command line. Just write

python cncopt.py your_file_here

The script will run and give you some stats on the performance of the optimization.

Screenshot from my terminal

As shown, in my PCB gcode, the optimized code reduced the travel time from 33 inches to 14. That is a decrease of about 60%. The optimized file will have the same filename with an additional .optimized at the end.

Please note that this was a one-afternoon project just for the fun of it. All comments and feedback are welcome. I hope you find it useful.

I would like to thank Mike as we developed this together.

You can find the project on GitHub https://github.com/demestav/PCB-GCode-Path-Optimizer


Related Posts:



Incoming search terms:

  • cncopt py (78)
  • g code optimizer (34)
  • gcode optimizer (32)
Categories: CNC Tags:
  1. Tom
    June 8th, 2012 at 20:31 | #1

    Sounds like a useful script but it wont work for me. Whenever I try and run it, I just get:

    >python cncopt.py topetch.tap

    -= pcb-gcode Optimizer version 0.9 =-
    ['G20\r\nG90\r\nM03\r\nG04 P4.000000\r\n']
    Traceback (most recent call last):
    File “cncopt.py”, line 41, in
    header = result[0][0]
    IndexError: list index out of range

    >

  2. psychoul
    June 8th, 2012 at 22:02 | #2

    hey Tom…. ah yes about that :)

    well I think I spotted the problem before and I haven’t had much time to fix and test it.
    Without being sure, I think I messed up a bit with Line breaks. You see I used only ‘\n’ which works on my system.

    Are you in Windows? if that is the case then we need to change the regex to ‘\n\r’

    Let me know.

    Thanks

  3. Don
    July 29th, 2012 at 18:41 | #3

    Hi,
    Using Win7, Python 3.2 (new to Python)
    This will be great, if I can get it working. :-)

    I am having the same problem as Tom.
    I replaced all \n with \n\r, and still get the same error.
    Am I missing something?

    TIA,
    Don

  4. psychoul
    July 30th, 2012 at 12:15 | #4

    hmm I promise I will take a look when I get the chance. :)
    In the meantime if anyone has spotted what is wrong comment below.

    Thanks

  5. Mikael
    April 27th, 2013 at 10:56 | #5

    Hi
    Running on Ubuntu linuxcnc router 10.04
    get this error.

    python cncopt.py TX4550.ngc
    File “cncopt.py”, line 4

    ^
    SyntaxError: invalid syntax

  6. psychoul
    April 27th, 2013 at 20:24 | #6

    Hi Mikael,
    Well, line 4 is empty!
    I am no really sure why you got that error. Any more feedback?

  7. Sebastian
    February 23rd, 2014 at 20:47 | #7

    For everybody having following error:
    -= pcb-gcode Optimizer version 0.9 =-
    ['G20\r\nG90\r\nM03\r\nG04 P4.000000\r\n']
    Traceback (most recent call last):
    File “cncopt.py”, line 41, in
    header = result[0][0]
    IndexError: list index out of range

    Remove all the comments in your gcode, those starting with “(“. This solves the problem. You can also extend this script to recognize them automatically.

    I know its an old article, but it helped me optimize my projects and I hope to help people finding this after me.

  1. No trackbacks yet.