Previewing G-Code using OpenSCAD

We put the commands for cutting into a .scad file and import it as a library and we can then have a LiveCode (or other) program which writes out the two sorts of files:

We then configure OpenSCAD for an external file and open the .scad file in it:

It’s then just a matter of creating the code to create the codes:

if you want any help converting the gcode into something else let me know be happy to help

(I have a pretty complete gcode parsing code base at this point, including one that deals with G2/G3 arcs correctly)

Neat! Thanks!

I’m hoping to just code the OpenSCAD end of things so that it’s orthogonal to G-Code — that’s not quite doable, since G-Code implements a given command based on current machine state, but I will hopefully manage something reasonably close.

the gcode parsing I normally do ends up converting gcode into a series of do_something(X1,Y1,Z1,X2,Y2,Z2) which seems to match the building blocks you’ve been building.
(even G2/G3 gets made into a series of small linear segments)

Some gcode (like Carbide Create) can also tell you the size of the stock and some properties of the tool (like diameter and ball radius)

1 Like

Yeah, I’ll be writing out suitable comments and so forth, once I get that far along w/ things.

Next up is encapsulating the G-code previews so that they will cut (will have to separate out the radius cut module since it’s not in BlockSCAD), which turns out to be quite simple:

but obviously it needs a module to select the correct module and parameters based on the tool # — if OpenSCAD had a facility for processing text we could import a .csv, but in the absence of that, we brute force it.

a tool that reads gcode and outputs this, can also do the CSV thing on that level

1 Like

Agreed… something like Convert to SVG [] ?

Once I’ve got the OpenSCAD end of things finalized, I’ll put it up on Github and anyone who find it of interest/use can make use of it — I’d love to see a tool which would accept G-Code and output a .scad file for this.

I’ll be happy to contribute that part of things…

do you have a preferred language, like javascript or python?
(this sort of thing is really not where C/C++ and co shine)

Python would be awesome, but JavaScript would probably be nicer in terms of providing access to folks.

In this case, brute-forcing is a series of If… end ifs:

For 90 degree arcs we seem to need a total of 8 different modules (CW/CCW and upper left, upper right, lower right, and lower left):

They’ll then be stitched into a single module which calculates which quadrant a given arc covers — working up a more general module which determines the arc angles as well will wait until that is needed for a project.

The garc modules are done — two versions, CW and CCW, though they’re a bad hack in that I just re-used existing arc modules, so directionality isn’t correct, and only 90 degree arcs are possible — addressing these failings will be done in a future update.

BlockSCAD version is available at:

You define the stock size (this will need to go into comments in the G-Code) and only bottom left spoilboard surface is possible as an origin at this time — adding at least options to match Carbide Create’s settings will need to be a future update.

The other missing element is cove radius endmills — only have this code in OpenSCAD, so it will need to be merged in when the OpenSCAD code is placed on GitHub.

There are examples of all the possible cuts:

including some which are commented out:

Put things up on GitHub:

Next up is actually putting this to use (and then improving it and fixing any bugs)

To put it to use, rather than Livecode (which I did my initial proof of concept in), shifting over to Python — first step is working up an interface which will allow customizing numbers and setting filenames and so forth and having a couple of buttons to write out the files:

EDIT: Things look better w/ the numbers centered — will have to look into decimal aligning them.

Using pycharm as an IDE, so the file is “”: (1.7 KB)

Just the skeleton of things — actually coding things up is next.

EDIT: filename insertion now works, and writing out a sample file, and getting the “Preview in OpenSCAD” button to load and render the file, so on to actual coding — writing out lines and arcs should both be in place, so next up is working out the mechanics of keeping track of the current tool and working up stuctures for actually cutting.

This is being continued at:


and an initial proof of concept file is done: