Hi There,
Several different people suggested that I write up how I use Perl in generating input for CamBam and my CNC machine, so I thought I would have a go at it.
Over the years I have written many programs to generate patterns of various sorts, in the first place to control a graph plotter; later to do computer graphics and now to output on the CNC router. They all basically come down to algorithms that generate a bunch of lines, followed by a conversion of the lines to the requisite commands to control some output device. It is this second part that might be useful to someone here, although I will also say something below about generating Bezier curves and Voronoi polyhedra.
Lines can be of three types: simple point-to-point connections, described as (x1, y1, x2, y); open polylines, described as (x1, y1, x2, y2, x3, y3, ... xn, yn) and closed polylines, where the last point is the same as the first one (at least to some tolerance). An example might be:
10, 160, 10, 100
10, 130, 50, 130
50, 160, 50, 100
110, 160, 70, 160, 70, 100, 110, 100
70, 130, 100, 130
130, 160, 130, 100, 170, 100
190, 160, 190, 100, 230, 100
260, 160, 250, 150, 250, 110, 260, 100, 280, 100, 290, 110, 290, 150, 280, 160, 260, 160
10, 70, 10, 10, 30, 40, 50, 10, 50, 70
80, 70, 70, 60, 70, 20, 80, 10, 100, 10, 110, 20, 110, 60, 100, 70, 80, 70
130, 10, 130, 70, 160, 70, 170, 60, 170, 50, 160, 40, 130, 40
150, 40, 170, 10
190, 70, 190, 10, 230, 10
250, 70, 250, 10, 280, 10, 290, 20, 290, 60, 280, 70, 250, 70
which consists of a dozen or so lines spelling out "Hello World".
Assume that a program has produced this as output. I have three approaches to getting this converted to GCode.
1) Convert the lines into a '.cb' file directly, which can then be used by CamBam for further manipulation and GCode generation.
2) Convert the lines into an '.stl' file, which allows for 3D profiling of the surface. This can then be imported into CamBam and then carved as a surface.
3) Generate GCode directly. This is good for simply engraving the lines at a single depth using a V-Cutter.
A fourth thing I do is to convert the lines to a 'png' file, which is handy during debugging to see what the line-drawing algorithm has generated. CamBam cannot import these. It can import '.gif's, but this is not a great approach to getting lines into CamBam.
A different approach altogether is to use a mathematical function that calculates a 'z' coordinate given 'x' and 'y' and to apply this across a grid of coordinates to generate an '.stl' file directly.
I will spread this writeup across several messages, so that I can upload files with each. The Perl code has been given a '.txt' extension to get it through the forum's file filter. You will need to remove these. I am assuming a UNIX system here. I know nothing about Windows! Presumably Perl on Windows makes provision for reading from STDIN and writing to STDOUT and for executing from a command line. I work in mm, but inches should be fine. You might need to adjust the precision of some print statements.