G Code Cleaner

Clean up your G-Code by filtering unnecessary points.

Similar projects worth following
If you have a G Code file with 50,000 lines of code?
Then perhaps some of those lines don't actually matter?
Here is a command-line utility to clean up the file.
Added a linux Mint version to the files download area.

G Code (gCode) Filter

I wrote this code as part of a laser engraver project to speed up the Grbl controller and Grbl interpreter (to stop them burning the work).

Most of the problem was with the original CAD design - far too many points.

The gCode file has 46,000 lines of code, even with a tolerance of 0.01 mm (the resolution of my laser engraver) the output is only 2000 lines of code (see the first image).

With a tolerance of 0.1 mm (very noticeable point reduction) the output is 1100 lines of code (see the second image).


FilterGCode.exe -i -o -t 0.01


-i Input file

-o Output file

-t Tolerance

Tolerance is the maximum distance the point (that if deleted) would be from the new line:


The code if written in plain C (using CodeBlocks) but uses Daniil Guitelson's BGI library.

You will need to add "graphics.h" and "libBGI.a" to your working directory, and the following linker options:

-lBGI -lgdi32

You will also need to add the working directory to your "compiler and linker search directories".

You can always delete the graphics code if you don't want it.


Now a word of warning the code has limitations. It only passes the following codes:

G, X, Y, Z, F, S, T and M

I have only considered G0 and G1 in my code so you can try G2 or G3 but you will be disappointed. The code in not complicated so you can modify it if you like.

Regards AlanX


Updated for ezx 2D library and new sscanf() behavour.

x-csrc - 8.38 kB - 12/09/2018 at 03:55



The new 2D graphics library.

gzip - 160.44 kB - 12/09/2018 at 03:53


A Linux Mint (X11) version

- 21.87 kB - 10/22/2017 at 08:40


application/x-msdownload - 39.00 kB - 08/20/2016 at 12:46



The Windows executable

x-msdownload - 38.00 kB - 07/27/2016 at 15:23


View all 15 files

  • Update for new behavour of GCC 7.3.0

    agp.cooper12/09/2018 at 03:52 0 comments

    Update for GCC 7.3.0

    The latest gcc has changed. I have had to recompile Xbgi library (i.e. 2D graphics) and I found sscanf() works differently (i.e. 0X? is recognised as hexadecimal).

    I have opted to migrate from BGI (Windows) and Xbgi (linux), to ezxdisp.

    "ezx" is short and sweet and is available for Windows and Linux.

    The latest version is available at

    Previously I used sscanf() as follows:

          for (j=0;Line[j];j++) Line[j]=toupper(Line[j]);
          if (tmp!=NULL) {
            if (f1<0) f1=0;

    It should read any number as a double which can be converted to long later.

    Now "G0X7.122" is read as G=7, X=0.0 but "G1X7122" still works fine.

    Now I have now coded:

          for (j=0;Line[j];j++) Line[j]=toupper(Line[j]);
          if (tmp!=NULL) {
            if (d1<0) d1=0;


  • Feed Override

    agp.cooper08/20/2016 at 12:46 0 comments

    Override Feed Rates

    Added two options:

    1. -G0 FeedRate
    2. -G1 FeedRate

    These options override the file feed rates (i.e. deletes feed rates in the file and uses the option rate instead).


    GCodeFilter.exe -i -o -t 0.01 -l -g0 600 -g1 120

    This line (usually in a batch file) overrides both the G0 and the G1 feed rates. Also sets "laser" mode.


  • Laser G Code converter

    agp.cooper07/30/2016 at 04:01 0 comments

    Laser gCode

    It is a pretty simple exercise to convert milling G Code to laser G Code.

    Turn off the laser (M5) for G0 movements and turn on the laser (M3) for G1 (also G2 and G3) movements. May as well delete all the old M3, M5 and Z movements as they will be redundant.

    I will add a "-L" or -"l" to the command line options for "laser code".


View all 3 project logs

Enjoy this project?



Similar Projects

Does this project spark your interest?

Become a member to follow this project and never miss any updates