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:
- OpenSCAD --- https://openscad.org/ --- developed from Art of Illusion, this is described as "The Programmers Solid 3D CAD Modeller" and is a de facto standard, so that many other tools extend it.
- BlockSCAD --- https://www.blockscad3d.com/ --- a Blockly implementation of OpenSCAD, BlockSCAD affords a quick, easy, and interactive way to model in 3D.
- OpenSCAD Graph Editor (OSGE) --- https://github.com/derkork/openscad-graph-editor --- a visual programming system, OSGE allows full access to all of OpenSCAD (in marked contrast to the limited number of commands and options afforded by BlockSCAD), including support for the Customizer.
- METAPOST --- https://www.tug.org/metapost.html --- a PostScript-oriented re-implementation of the venerable METAFONT https://www-cs-faculty.stanford.edu/~knuth/abcde.html#mfbk MP allows one to create 2D drawings as SVGs which may then be imported into Carbide Create to make projects.
- lualatex --- http://luatex.org/ --- a latter-day implementation of the venerable TeX typesetting system, this allows Lua-scripting, and includes an embedded METAPOST interpreter.
- RapCAD --- https://rapcad.org/ --- an alternative to OpenSCAD, RapCAD notably adds the option of writing out text files with full user control.
- gcodepreview --- https://github.com/WillAdams/gcodepreview --- a library for OpenSCAD/RapCAD which allows 3D modeling tool movement/cutting and the generation of a matching G-code file.
- GSharp --- https://github.com/NRSoft/GSharp --- a system which allows programming in G-code using loops and variables even on G-code implementations which lack such features.
- FullControl GCODE --- https://fullcontrolgcode.com/software --- this was originally an Excel spreadsheet, but it was re-implemented in Python and is available as a website: https://fullcontrol.xyz/ as well as a Python module: https://github.com/FullControlXYZ/fullcontrol
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) https://www.nist.gov/manuscript-publication-search.cfm?pub_id=823374 --- 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: https://github.com/NRSoft/GSharp 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)
(PREPOSITION FOR RAPID PLUNGE)
(Move to safe Z to avoid workholding)
(TOOL/MILL,0.03, 0.00, 10.00, 45.00)
(PREPOSITION FOR RAPID PLUNGE)
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.
For a list of the G-codes supported by Carbide Motion and Grbl see: https://docs.carbide3d.com/software-faq/list-of-supported-gcodes/ and https://my.carbide3d.com/faq/grbl-g-code-definitions/
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:
- 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.
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: