# Command line utility to 'clean' GCode

**URL:** <https://forums.maslowcnc.com/t/command-line-utility-to-clean-gcode/13073>\
**Category:** CAD/CAM Software Tips\
**Created:** [April 27, 2020, 12:12pm UTC](https://forums.maslowcnc.com/t/command-line-utility-to-clean-gcode/13073 "2020-04-27T12:12:38Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![md8n](https://yyz1.discourse-cdn.com/flex031/user_avatar/forums.maslowcnc.com/md8n/32/28197_2.png) [@md8n](https://forums.maslowcnc.com/u/md8n)\
**Post date:** [April 27, 2020, 12:12pm UTC](https://forums.maslowcnc.com/t/command-line-utility-to-clean-gcode/13073/1 "2020-04-27T12:12:38Z")

</div>

I’ve dropped this project onto GitHub: [https://github.com/md8n/GCodeClean](https://github.com/md8n/GCodeClean)

There’s a self-contained 64bit `Release` build for Linux, Windows and OSX, you can get the relevant one from [https://github.com/md8n/GCodeClean/releases](https://github.com/md8n/GCodeClean/releases) (open up `Assets` within the latest version )

But you can also build it yourself (e.g. for other chipsets like ARM, 32bit, specific Linux flavours, etc.).  
This does require the .net Core 3.1 SDK - but that runs on anything (Linux, OSX, Windows) and hopefully I’ve included enough instructions for anyone familiar with a command prompt / shell to be able to build it.

I had a 14.7MB .nc file (output from estlcam) that I was able to ‘clean’ down to an 8.92MB file with a tolerance of \< 0.001mm on all commands.

Please note that it’s still a “work in progress” as I add support for more of gcode spec (yep there is actually a spec). But what’s there already seems to work well for many gcode files.

Two things are particularly useful for ‘explaining’ gcode:

- An optional `--annotate` flag, where it will add inline comments describing what each line does.
- A `per line` linter, where lines with multiple commands (G, M, F, S, T) are split up onto individual lines in the order that they should be processed (according to the specification).

---

<div class="post-metadata">

**Author:** ![Orob](https://yyz1.discourse-cdn.com/flex031/user_avatar/forums.maslowcnc.com/orob/32/16344_2.png) [@Orob](https://forums.maslowcnc.com/u/Orob)\
**Post date:** [April 27, 2020, 1:43pm UTC](https://forums.maslowcnc.com/t/command-line-utility-to-clean-gcode/13073/2 "2020-04-27T13:43:01Z")

</div>

Thanks for taking time to make this tool. It is nicely documented and has simple instructions. After manually installing dotnet on my dev rpi:

 ![image](https://canada1.discourse-cdn.com/flex031/uploads/maslowcnc/original/3X/d/0/d03c84db7647dea27c29cfd4512a1d74238297d5.png)

(shot taken before the install completed successfully).

Just for fun I put a file through:

 ![image](https://canada1.discourse-cdn.com/flex031/uploads/maslowcnc/original/3X/d/1/d16a8a523b3b4c762967af80bbe38cf9b67566a4.png)

I’m not sure if it is my file or something else, so I’m posting for feedback.

---

<div class="post-metadata">

**Author:** ![md8n](https://yyz1.discourse-cdn.com/flex031/user_avatar/forums.maslowcnc.com/md8n/32/28197_2.png) [@md8n](https://forums.maslowcnc.com/u/md8n)\
**Post date:** [April 27, 2020, 11:40pm UTC](https://forums.maslowcnc.com/t/command-line-utility-to-clean-gcode/13073/3 "2020-04-27T23:40:19Z")

</div>

If you’re willing to share he file. Then I’d be happy to use it for testing.  
Alternatively, if you get a text editor (such as VS Code, which also runs on anything), then you can try debugging it for yourself. I’d be happy to accept pull requests on that code.

Share via dropbox, or something similar if you prefer.

---

<div class="post-metadata">

**Author:** ![md8n](https://yyz1.discourse-cdn.com/flex031/user_avatar/forums.maslowcnc.com/md8n/32/28197_2.png) [@md8n](https://forums.maslowcnc.com/u/md8n)\
**Post date:** [April 27, 2020, 11:42pm UTC](https://forums.maslowcnc.com/t/command-line-utility-to-clean-gcode/13073/4 "2020-04-27T23:42:56Z")

</div>

And I just checked that line that it reported and it’s related to processing of comments within the gcode.

---

<div class="post-metadata">

**Author:** ![md8n](https://yyz1.discourse-cdn.com/flex031/user_avatar/forums.maslowcnc.com/md8n/32/28197_2.png) [@md8n](https://forums.maslowcnc.com/u/md8n)\
**Post date:** [April 28, 2020, 4:53am UTC](https://forums.maslowcnc.com/t/command-line-utility-to-clean-gcode/13073/5 "2020-04-28T04:53:38Z")

</div>

OK - I’ve done a little fix and pushed that up to GitHub.  
See how it works out for you now.  
The big change you should see is it clipping the number of decimal places.  
But it does manage to drop a few of the linear movements.

---

<div class="post-metadata">

**Author:** ![Orob](https://yyz1.discourse-cdn.com/flex031/user_avatar/forums.maslowcnc.com/orob/32/16344_2.png) [@Orob](https://forums.maslowcnc.com/u/Orob)\
**Post date:** [April 28, 2020, 3:54pm UTC](https://forums.maslowcnc.com/t/command-line-utility-to-clean-gcode/13073/6 "2020-04-28T15:54:19Z")

</div>

It works!

---

<div class="post-metadata">

**Author:** ![md8n](https://yyz1.discourse-cdn.com/flex031/user_avatar/forums.maslowcnc.com/md8n/32/28197_2.png) [@md8n](https://forums.maslowcnc.com/u/md8n)\
**Post date:** [April 28, 2020, 10:42pm UTC](https://forums.maslowcnc.com/t/command-line-utility-to-clean-gcode/13073/7 "2020-04-28T22:42:27Z")

</div>

Here’s hoping that the IJK changes are still within tolerance for the arcs to actually be carved.

---

<div class="post-metadata">

**Author:** ![md8n](https://yyz1.discourse-cdn.com/flex031/user_avatar/forums.maslowcnc.com/md8n/32/28197_2.png) [@md8n](https://forums.maslowcnc.com/u/md8n)\
**Post date:** [April 29, 2020, 9:00am UTC](https://forums.maslowcnc.com/t/command-line-utility-to-clean-gcode/13073/8 "2020-04-29T09:00:22Z")

</div>

Now _New!_ and _Improved!_ - still adding in new ‘cleaning’ steps. Thanks to @Orob for providing that sample .gcode file.

---

<div class="post-metadata">

**Author:** ![jnowell](https://avatars.discourse-cdn.com/v4/letter/j/8e8cbc/32.png) [@jnowell](https://forums.maslowcnc.com/u/jnowell)\
**Post date:** [May 1, 2020, 2:13pm UTC](https://forums.maslowcnc.com/t/command-line-utility-to-clean-gcode/13073/9 "2020-05-01T14:13:18Z")

</div>

I’d like to jump in on the beta testing here as well, I have a few gcode files I would share also. I create my objects in Easel and let it provide the gcode. This seems to work pretty well as far as the results go, but I see it do some really strange stuff also. The file I will share cuts exactly what I want, but in one small section it makes the same cut 6 or 8 times, and each time it lifts, moves back to the start of the line, and then plunges and cuts it again (the rest of the rectangle is cut as you would expect). I wonder if your tool will remove that bit of inefficiency? Regardless, I’m all in for trying and testing and helping where I can.

Jason

[FinalMothersDay.nc](https://forums.maslowcnc.com/uploads/short-url/3oidenu3dxOS4sqYlpKiz3RS4Es.nc) (9.5 KB)

---

<div class="post-metadata">

**Author:** ![emilecantin](https://yyz1.discourse-cdn.com/flex031/user_avatar/forums.maslowcnc.com/emilecantin/32/16834_2.png) [@emilecantin](https://forums.maslowcnc.com/u/emilecantin)\
**Post date:** [May 1, 2020, 3:16pm UTC](https://forums.maslowcnc.com/t/command-line-utility-to-clean-gcode/13073/10 "2020-05-01T15:16:04Z")

</div>

I have 2 files to share, both were generated using Fusion360.

This first one causes issues; the machine slows down to a crawls in the curved portions.  
[hook.nc](https://forums.maslowcnc.com/uploads/short-url/tUBJXH97tDJCrdxhzthJolOY90p.nc) (80.3 KB)

This second one was generated after tweaking a few tolerance values (I think "minimum cutting radius was the magic one here, but I also tweaked “tolerance” and “smoothing tolerance”). It works much better, there is no slowdown while cutting and the parts that come out are virtually identical (except for the burn marks on the first, and there’s one feature that’s a little less deep on the second one)  
[hook 2.nc](https://forums.maslowcnc.com/uploads/short-url/3ZSie8rxoibsAnfO13Dhu2vT7vs.nc) (9.4 KB)

Hope that’s useful.

---

<div class="post-metadata">

**Author:** ![jnowell](https://avatars.discourse-cdn.com/v4/letter/j/8e8cbc/32.png) [@jnowell](https://forums.maslowcnc.com/u/jnowell)\
**Post date:** [May 1, 2020, 6:58pm UTC](https://forums.maslowcnc.com/t/command-line-utility-to-clean-gcode/13073/11 "2020-05-01T18:58:31Z")

</div>

Worked like a champ! Took my 9.5K file with erroneous moves and turned it into a \<6K file with no BS moves. Nice work! Thanks! I did this on Mac OSX for testing purposes. I was posting the wrong file too, I will redo with the correct files in a sec and repost them.

Jason

---

<div class="post-metadata">

**Author:** ![jnowell](https://avatars.discourse-cdn.com/v4/letter/j/8e8cbc/32.png) [@jnowell](https://forums.maslowcnc.com/u/jnowell)\
**Post date:** [May 1, 2020, 7:29pm UTC](https://forums.maslowcnc.com/t/command-line-utility-to-clean-gcode/13073/12 "2020-05-01T19:29:37Z")

</div>

OK so maybe not so perfect. Here are the screen-shots of what Ground Control shows with the files loaded up. Something went awry. I included the original and output .nc files below

Jason

 ![Screen Shot 2020-05-01 at 3.24.47 PM](https://canada1.discourse-cdn.com/flex031/uploads/maslowcnc/original/3X/c/4/c4b657404d173c50efaa71a861bc15c5af675d11.png) ![Screen Shot 2020-05-01 at 3.25.19 PM](https://canada1.discourse-cdn.com/flex031/uploads/maslowcnc/original/3X/f/9/f9053eb8650df306677992711f2e7b5217c69d14.png)

[mothersday-gcc.nc](https://forums.maslowcnc.com/uploads/short-url/rQAXYvNZT3nOeyD7zsuCnTgwHMw.nc) (21.8 KB) [MothersDay.nc](https://forums.maslowcnc.com/uploads/short-url/6ggXVYpax22wyUAKMbFEI0323yD.nc) (193.5 KB)

---

<div class="post-metadata">

**Author:** ![md8n](https://yyz1.discourse-cdn.com/flex031/user_avatar/forums.maslowcnc.com/md8n/32/28197_2.png) [@md8n](https://forums.maslowcnc.com/u/md8n)\
**Post date:** [May 2, 2020, 5:07am UTC](https://forums.maslowcnc.com/t/command-line-utility-to-clean-gcode/13073/13 "2020-05-02T05:07:03Z")

</div>

Thanks Jason,  
Yeah this thing is at very early stages. For example, I’m just figuring out that my tokenization is wrong, and that’s the start of my ‘pipeline’. There’s a whole range of uuuuugly gcode that’s perfectly legit, but it wouldn’t be able to tokenize yet (yet).

So sample very much appreciated. I won’t be publishing any of them with the project. But if you’d like to ‘release’ some then feel free to create a branch and send a pull request.

---

<div class="post-metadata">

**Author:** ![md8n](https://yyz1.discourse-cdn.com/flex031/user_avatar/forums.maslowcnc.com/md8n/32/28197_2.png) [@md8n](https://forums.maslowcnc.com/u/md8n)\
**Post date:** [May 2, 2020, 12:24pm UTC](https://forums.maslowcnc.com/t/command-line-utility-to-clean-gcode/13073/14 "2020-05-02T12:24:58Z")

</div>

Hi Jason,

Thanks for your sample file. I’ve just finished changing the DedupLinear algorithm based on your sample file. So please `git pull` (or clone) and try again. Hopefully it works a lot better for you this time.

I also appreciate that this was my first non-metric gcode file, so it was a good test just for that reason alone.

---

<div class="post-metadata">

**Author:** ![md8n](https://yyz1.discourse-cdn.com/flex031/user_avatar/forums.maslowcnc.com/md8n/32/28197_2.png) [@md8n](https://forums.maslowcnc.com/u/md8n)\
**Post date:** [May 2, 2020, 12:32pm UTC](https://forums.maslowcnc.com/t/command-line-utility-to-clean-gcode/13073/15 "2020-05-02T12:32:00Z")

</div>

Hi Emile,

Thanks for those files. I’ve tried processing both with the latest changes I’ve made, and I can see a drop in file size for both.

If you’re comfortable with grabbing the latest version of my code from GitHub and running things for yourself then please do that. But I’ll also post your ‘cleaned’ files back into this thread if you like.

Regards, Lee

---

<div class="post-metadata">

**Author:** ![jnowell](https://avatars.discourse-cdn.com/v4/letter/j/8e8cbc/32.png) [@jnowell](https://forums.maslowcnc.com/u/jnowell)\
**Post date:** [May 3, 2020, 8:55pm UTC](https://forums.maslowcnc.com/t/command-line-utility-to-clean-gcode/13073/16 "2020-05-03T20:55:06Z")

</div>

You are welcome to use that file however you see fit, I am new to GitHub so I will have to decipher the branch and pull request stuff. 🙂 I’m an old school techie, just getting into all of this…I have to say I like it!

I’m out until Tuesday but I will grab the updated build, reprocess, and report back when I get back. On the few files I played with, it reduced the estimated cutting time when I imported the parsed g-code back into Easel. It will be interesting to see if it truly reduces the cutting time once it is dialed in. I have several upcoming projects that will requires multiple identical objects so I will run some with and some without and give a true end to end test. Can’t wait.

Thanks for your work on this @md8n

Jason

---

<div class="post-metadata">

**Author:** ![md8n](https://yyz1.discourse-cdn.com/flex031/user_avatar/forums.maslowcnc.com/md8n/32/28197_2.png) [@md8n](https://forums.maslowcnc.com/u/md8n)\
**Post date:** [May 5, 2020, 10:54am UTC](https://forums.maslowcnc.com/t/command-line-utility-to-clean-gcode/13073/17 "2020-05-05T10:54:17Z")

</div>

I’ve updated the project structure and also redone the tokenizer to use a regex instead that will handle more ‘valid’ gcode (that it didn’t like before).

I was going to use regex anyway. But it normally requires taking psychotropic substances to wrap your head around the ‘regex’ way of doing things. So … anyway, done now.

`git pull` or clone and have a play with your favorite gcode files.

---

<div class="post-metadata">

**Author:** ![madgrizzle](https://yyz1.discourse-cdn.com/flex031/user_avatar/forums.maslowcnc.com/madgrizzle/32/1372_2.png) [@madgrizzle](https://forums.maslowcnc.com/u/madgrizzle)\
**Post date:** [May 5, 2020, 11:34am UTC](https://forums.maslowcnc.com/t/command-line-utility-to-clean-gcode/13073/18 "2020-05-05T11:34:26Z")

</div>

> [@md8n](#):
>
> But it normally requires taking psychotropic substances to wrap your head around the ‘regex’ way of doing things.

So that’s what I’m missing! I still believe regex is voodoo magic.

---

<div class="post-metadata">

**Author:** ![md8n](https://yyz1.discourse-cdn.com/flex031/user_avatar/forums.maslowcnc.com/md8n/32/28197_2.png) [@md8n](https://forums.maslowcnc.com/u/md8n)\
**Post date:** [May 5, 2020, 11:48am UTC](https://forums.maslowcnc.com/t/command-line-utility-to-clean-gcode/13073/19 "2020-05-05T11:48:18Z")

</div>

There is such a thing as regex golf - where you try and get the shortest regex possible to yield the correct matches. I think it’s probably played by the same people who think that SGML was a good idea.

---

<div class="post-metadata">

**Author:** ![Orob](https://yyz1.discourse-cdn.com/flex031/user_avatar/forums.maslowcnc.com/orob/32/16344_2.png) [@Orob](https://forums.maslowcnc.com/u/Orob)\
**Post date:** [May 5, 2020, 12:04pm UTC](https://forums.maslowcnc.com/t/command-line-utility-to-clean-gcode/13073/20 "2020-05-05T12:04:48Z")

</div>

I thought I was the only one who just didn’t get it and I needed some medicinal help and some mental reprogramming: perhaps a few days in concentrated meditation and some injections.

[Next page](https://forums.maslowcnc.com/t/command-line-utility-to-clean-gcode/13073.md?page=2)
