LinuxCNCKnowledgeBase | RecentChanges | PageIndex | Preferences | LinuxCNC.org


This is a simple command line program that takes a gcode file and replaces G1 sequences that fit an arc with G2/G3 codes.


When a CAD model is 3d printed, all the arc information is lost when the CAD model is converted to STL. The output of a slicer program will be a series of short line segments. Linuxcnc doesn't process little line segments very quickly (on my machine anyway) but it is fine with arcs. So this program attempts to restore the line segments to arcs.

The program

The program has been hacked from the file, author.py, that is in the linuxcnc source tree. The recursive subdivision smarts have been removed and now just processes the file sequentially. As such, it is now pretty slow, but it does a better job of find arcs.

The program works by taking the first two line segments and trying to fit an arc to it. If it can fit an arc, it tries to add the next point, and so on, until the next point no longer makes an arc. Those matching line segments are then removed and replaced with one arc. To ensure we really are matching an arc, the program also measures the accumulated length of the collected line segments and compares it to the length of the substitute arc (incorrectly called chord length in the code).

There are 2 tolerance tweaking parameters defined in g1tog23.py - point_tolerance and length_tolerance which are used to determine how far off the arc the next point is allowed, and how far off each line segment is allowed to be from the true length of the arc. The values are those that I use when exporting STL from Alibre Design and generating GCode from Slic3r.


This was written for Slic3r output and as such, has only been tested with Slic3r output. It only converts arcs in the XY plane. It only works in millimeters (because the tolerance factors, defined in g1tog23.py, have been tweaked for millimeters - easy to change).

The program can read and write to stdin/stdout, or it can take the name of the file to convert as a command line argument. When passed as a command line argument, a backup of the file is made first (filename.ngc.bak) Eg


g1tog23.py < mygcode.ngc > new_mygcode.ngc


g1tog23.py mygcode.ngc

In my setup, I've added the script to the Slic3r, Print Settings, Output Options, Post-processing scripts list so the script is always run when Slic3r generates output. I run slic3r under MS-Windows, so I launch g1tog23 via a batch file.

Note that the output file size can be larger, but the number of lines will never increase. This is because x/y/z/a is always output with all digits displayed even if they don't change from line to line.

At the end of the new file, a summary is added. For example,

 ; filament used = 16140.7mm (110.3cm3)
 ; GCode file processed by d:\3dprinting\g1tog23.py
 ; Input Line Count = 561428
 ; Output Line Count = 141307
 ; Line reduction = 74%





LinuxCNCKnowledgeBase | RecentChanges | PageIndex | Preferences | LinuxCNC.org
This page is read-only. Follow the BasicSteps to edit pages. | View other revisions
Last edited June 16, 2013 5:07 am by Ftkalcevic (diff)
Published under a Creative Commons License