Tcl as a g-code preprocessor

Tcl makes a nice g-code preprocessor. I've proposed this twice before, but everyone seems to think I'm joking---I'm actually a bit serious. Because Tcl is an existing language, it has set rules for parsing, execution, variable scoping, and all those other hard things about computer language design. By finding a way to let simple g-code snippets pass through unaltered, but also escape to the full Tcl language, you get power and familiarity with very little effort.

Here's an example, which will mill a set of slots in an efficient manner (first from left to right, then from right to left):

G20; G0 X0 Y0 Z.1; F8

for {set i 0} {$i < 10} {incr i} {
    G0 X$i; G1 Z-.1
    if {$i % 2 == 0} Y10 else Y0 
    G0 Z.1
A ".gcl" file (pending a better choice of name---I'm told this extension is already used for GNU common lisp files) is processed by "gcode.tcl" to create a .ngc file output. Almost any g-code program which does not use square brackets or variables will be unchanged when processed, but Tcl variables and control structures like the for-loop shown here, may also be used. Specifically, the .gcl file is processed just like a "source"'d .tcl file, but when an unknown Tcl command is encountered (one not built-in or defined by a 'proc'), and it starts with one of the characters In the set "GMXYZABC(", it is copied to the output.

Tcl has not only integer and floating-point numbers, but also strings, lists, and arrays. Tcl extensions can be written in C, C++, and other languages, so an interface to a library like CGAL could be implemented, easing the work of writing code to mill irregular pockets. And finally, the Tk graphical toolkit is accessible by simply putting 'package require Tk' in your .gcl file, allowing the program to include its own user-prompting facility.

The downsides to any "preprocess to linear g-code" method is that infinite programs cannot be written (though in fact, you can write O-words to the output of a gcl program), and that program logic cannot depend on the result of a probe operation (though again you can still "escape" to O-words). These may be required features for a few people, but probably not for most.

Files currently attached to this page:

example.gcl132 bytes
gcode.tcl269 bytes

(originally posted on the AXIS blog)

Entry first conceived on 28 December 2006, 14:19 UTC, last modified on 15 January 2012, 3:46 UTC
Website Copyright © 2004-2024 Jeff Epler