SkillAgentSearch skills...

Gcodepreview

OpenPythonSCAD library for moving a tool in lines and arcs so as to model how a part would be cut using G-code or described as a DXF.

Install / Use

/learn @WillAdams/Gcodepreview

README

gcodepreview

OpenPythonSCAD library for moving a tool in lines and arcs so as to model how a part would be cut or extruded using G-Code, so as to allow use as a compleat CAD/CAM solution for subtractive or additive 3-axis CNC (4th-axis support may come in a future version) by writing out G-code in addition to 3D modeling (in certain cases toolpaths which would not normally be feasible in typical tools), and to write out DXF files which may be imported into a traditional CAM program to create toolpaths.

OpenSCAD gcodepreview Unit Tests

Uses Python in OpenSCAD[^pythonscad]

[^pythonscad]: Previously this was http://www.guenther-sohler.net/openscad/

A BlockSCAD file for the initial version of the main modules is available at:

https://www.blockscad3d.com/community/projects/1244473

The project is discussed at:

https://willadams.gitbook.io/design-into-3d/programming

Since it is now programmed using Literate Programming (initially a .dtx, now a .tex file) there is a PDF: https://github.com/WillAdams/gcodepreview/blob/main/gcodepreview.pdf which includes all of the source code with commentary.

The files for this library are:

  • gcodepreview.py (gcpy) --- the Python class/functions and variables
  • gcodepreview.scad (gcpscad) --- OpenSCAD modules and parameters

And there several sample/template files which may be used as the starting point for a given project:

  • gcodepreviewtemplate.txt (gcptmpl) --- .txt file collecting various commands with brief comments which may be used as a quick reference or copy-pasting from
  • gcodepreviewtemplate.py (gcptmplpy) --- .py example file
  • gcodepreviewtemplate.scad (gcptmplscad) --- .scad example file
  • gcpdxf.py (gcpdxfpy) --- .py example file which only makes dxf file(s) and which will run in "normal" Python in addition to PythonSCAD
  • gcpgc.py (gcpgc) --- .py example which loads a G-code file and generates a 3D preview showing how the G-code will cut
  • gcpthreedp.py --- Template for 3D printing using Full Control G-code https://fullcontrolgcode.com/

Note that additional templates are in: https://github.com/WillAdams/gcodepreview/tree/main/templates

If using from PythonSCAD, place the files in C:\Users\~\Documents\OpenSCAD\libraries or, load them from Github using the command:

nimport("https://raw.githubusercontent.com/WillAdams/gcodepreview/refs/heads/main/gcodepreview.py")

If using gcodepreview.scad call as:

use <gcodepreview.py>
include <gcodepreview.scad>

Note that it is necessary to use the first file (this allows loading the Python commands and then include the last file (which allows using OpenSCAD variables to selectively implement the Python commands via their being wrapped in OpenSCAD modules) and define variables which match the project and then use commands such as:

opengcodefile(Gcode_filename);
opendxffile(DXF_filename);

gcp = gcodepreview("cut", true, true);

setupstock(219, 150, 8.35, "Top", "Center");

movetosafeZ();

toolchange(102, 17000);

cutline(219/2, 150/2, -8.35);

stockandtoolpaths();

closegcodefile();
closedxffile();

which makes a G-code file:

OpenSCAD template G-code file

but one which could only be sent to a machine so as to cut only the softest and most yielding of materials since it makes a single full-depth pass, and which has a matching DXF which may be imported into a CAM tool --- but which it is not directly possible to assign a toolpath in readily available CAM tools (since it varies in depth from beginning-to-end which is not included in the DXF since few tools make use of that information).

Importing this DXF and actually cutting it is discussed at:

https://forum.makerforums.info/t/rewriting-gcodepreview-with-python/88617/14

Alternately, gcodepreview.py may be placed in a Python library location and used directly from Python to generate DXFs as shown in gcpdxf.py (generating a 3D preview requires OpenPythonSCAD and generating G-code without a preview is not supported).

In the current version, tool numbers may match those of tooling sold by Carbide 3D (ob. discl., I work for them) and other vendors, or, a vendor-neutral system may be worked up and used as desired.

Comments are included in the G-code to match those expected by CutViewer, allowing a direct preview without the need to maintain a tool library (for such tooling as that program supports).

Supporting OpenSCAD usage makes possible such examples as: openscad_gcodepreview_cutjoinery.tres.scad which is made from an OpenSCAD Graph Editor file:

OpenSCAD Graph Editor Cut Joinery File

Written as a Literate Program in lualatex which is a version of Donald E. Knuth's TeX typesetting program extended by the Lua programming language using a custom package, literati.sty developed with a bit of help on tex.stackexchange.com rather than using a more typical IDE because of the need for keeping multiple files in synch and so as to have a single point of control (the .tex source file used to generate the .pdf and all other files for this project).

| Version | Notes | | ------------- | ------------- | | 0.1 | Version supports setting up stock, origin, rapid positioning, making cuts, and writing out matching G-code, and creating a DXF with polylines. | | | - separate dxf files are written out for each tool where tool is ball/square/V and small/large (10/31/23) | | | - re-writing as a Literate Program using the LaTeX package docmfp (begun 4/12/24) | | | - support for additional tooling shapes such as dovetail and keyhole tools | | 0.2 | Adds support for arcs, specialty toolpaths such as Keyhole which may be used for dovetail as well as keyhole cutters | | 0.3 | Support for curves along the 3rd dimension, roundover tooling | | 0.4 | Rewrite using literati documentclass, suppression of SVG code, dxfrectangle | | 0.5 | More shapes, consolidate rectangles, arcs, and circles in gcodepreview.scad | | 0.6 | Notes on modules, change file for setupstock | | 0.61 | Validate all code so that it runs without errors from sample (NEW: Note that this version is archived as gcodepreview-openscad_0_6.tex and the matching PDF is available as well| | 0.7 | Re-write completely in Python | | 0.8 | Re-re-write completely in Python and OpenSCAD, iteratively testing | | 0.801 | Add support for bowl bits with flat bottom | | 0.802 | Add support for tapered ball-nose and V tools with flat bottom | | 0.803 | Implement initial color support and joinery modules (dovetail and full blind box joint modules) | | 0.9 | Re-write to use Python lists for 3D shapes for toolpaths and rapids. | | 0.91 | Finish converting to native OpenPythonSCAD trigonometric functions. | | 0.92 | Remove multiple DXFs and unimplemented features, add hooks for 3D printing. | | 0.93 | Initial support for 3D printing. | | 0.931 | Update support for OpenSCAD modules. | | 0.9312 | Update DXF file-handling for Carbide Create 839. | | 0.9313 | Update Full_Blind_Finger_Joint to use layers, add LWPOLYLINE where appropriate

View on GitHub
GitHub Stars80
CategoryDevelopment
Updated22h ago
Forks6

Languages

TeX

Security Score

100/100

Audited on Mar 27, 2026

No findings