Design into 3D


The underlying mechanism behind the designs
A number of different programming tools and methodologies have been shown throughout this work. Tools which are notable and/or which are used include:
New ones are regularly developed, and future developments will be documented here as circumstances warrant.
Naturally, any programming language which is able to write out files can be used to make G-code, and many programming languages have 3D libraries which will allow modeling in 3D --- this page will focus on those tools which are specifically applicable to CNC usage.
Programming allows dividing a project into two different aspects:
  • parameters which define dimensions and features
  • algorithms which instantiate the various designs and relationships
Creating a project programmatically allows one to have multiple views of the project --- finished project, any optional states required by features such as lids, and arranging the parts so as to visualize their relationship, and so as to actually cut them out, and to move parts around so as to check fit of joinery and so forth.
Most systems for 3D modeling have one directly model the part itself, then depend on 3D CAM software to create toolpaths to cut things out. A more direct approach is to instead model toolpaths, which has the advantages of ensuring that a part can be cut, that the cutting toolpath is as efficient as it possibly can be, and eliminating the need for a separate CAM program.


The language used for toolpaths is G-code (RS-274) --- in some implementations it is a full-fledged programming language, so it is possible to use it directly to program if one has a suitable 3D previewing tool. Unfortunately, most hobby-level G-code implementations lack variables, branching, and looping, so are only suited to the G-code which is output by CAM programs.
There is a 3rd party tool which will accept G-code with such commands and instantiate them: which is incorporated into bCNC which is also on Github:
A straight-forward program which cuts an "X" in a 100mm square using two different tools from origin at the Lower-Left, and Top of the stock with a Retract Height of 5mm:
(Design File: gcode_sample_102_390.c2d)
(stockMin:0.00mm, 0.00mm, -1.00mm)
(stockMax:100.00mm, 100.00mm, 0.00mm)
(STOCK/BLOCK,100.00, 100.00, 1.00,0.00, 0.00, 1.00)
(Move to safe Z to avoid workholding)
(TOOL/MILL,3.17, 0.00, 0.00, 0.00)
(Move to safe Z to avoid workholding)
(TOOL/MILL,0.03, 0.00, 10.00, 45.00)
This previews as:
As verified by a 3rd party G-code simulator, CutViewer Mill:
The control software for many CNC machines will afford G-code entry in one or more ways. For the Carbide 3D machines which I use (and support), this is Carbide Motion, which has two options: MDI --- one can enter G-code into the MDI one line at a time, and also "Quick Actions" which allow entering small programs which can then be run at will.


OpenSCAD affords a programming environment which has variable and loops and 3D modeling, and in the RapCAD implementation is able to write out files, allowing one to export toolpaths to G-code, making it well-suited to creating a library which allows modeling in 3D as if one was cutting with a machine.
Every module must do what it does twice over, modeling in 3D in OpenSCAD, and if enabled, writing out matching G-code. Further, in some instances, it will be desirable or even necessary to directly write out G-code which has no OpenSCAD equivalent.


The first thing which must be done is to define the stock, then it is possible to model the shapes of tools in such a way that they may be hulled together along toolpaths and then subtracted from the stock. Since G-code is inherently subtractive, the stock is simply a comment which defines it. The necessary parameters are:
  • stocklength
  • stockwidth
  • stockthickness
  • zeroheight --- either Top or Bottom
  • stockorigin --- Lower-Left, Center-Left, Top-Left, or Center
The latter two match job setup options in Carbide Create and determine where the stock will be placed relative to the origin.
which generates matching G-code:


As noted above, it is necessary to be able to toggle G-code generation on/off, both for performance considerations, and because the command used for this, writeln, is not supported by OpenSCAD, but is specific to RapCAD.
Naturally, it is included as a true/false (boolean) option in the customizer:
generategcode = false;
and of course the module tests for that variable being set to true when writing out G-code is necessary.
When one is generating G-code some additional commands will be needed.


This command will output an M6 tool change command and the tool number used as an argument.


Starts the spindle at the specified RPM.


Issues a rapid movement to the specified Z-height (usually safety/retract height)


Outputs the commands to end a cut (retract to safety/retract height and M02).


Putting the commands together has several expectations and requirements. The simplest usage is one where a single cut is made and the tool is plunged at the beginning, the cut is made, and then the tool is lifted to the retract height --- more complex cuts have the same requirements, to ensure that the tool is moved so that it cuts and does not collide with the stock at a rapid rate.
Having multiple cuts presents the possibility of redundant G-code commands, but the simplistic and persistent nature of G-code, that a single command is a movement to the specified position from the current one, and that OpenSCAD requires describing both positions means that every other OpenSCAD command after the first can omit writing out the G-code, which will then omit the redundant commands and result in terse code which still describes the expected machine motion.

OpenSCAD Graph Editor

If one loads the library gcodepreview as a module into OSGE, it is pretty straight-forward to use it to create a file to cut out a design using G-code:
which when generated as OpenSCAD code previews as expected:
which with a bit of editing works as expected in RapCAD:
and generates G-code which previews as expected:
Last modified 1d ago