A Python interface to a large-format pen plotter
HPGL Files
HPGL is the Hewlett-Packard Graphics Language. This language defines a series of drawing commands that plotters and printers can interpret and use to create their output. HPGL files are plain text and use the following format:
- Two-character command
- Comma-separated arguments (if needed)
- Semicolon
This format is repeated until all data has been written. Table 1 shows the primary commands that Inkscape uses.
Table 1
HPGL Commands
Command | Command Name | Function | Arguments It Looks For |
---|---|---|---|
IN |
Initialize |
Tell the plotter to reset and get ready to plot |
None |
SP |
Select pen |
Get a pen from the cartridge |
Pen number to get |
PU |
Pen up |
Lift the pen and move to a set of coordinates |
X and Y coordinates, separated by commas |
PD |
Pen down |
Lower the pen and move to a set of coordinates |
X and Y coordinates, separated by commas |
Here are the first few commands of poker_red.hpgl
– the red layer of the poker poster I am plotting for this project.
IN;SP2;PU7138,12166;PD7108,12166;PD7108,12139;PD7058,12139;\ PD7058,12277;PD7023,12277;PD6797,12127;PD6797,12103;\ PD7023,12103;PD7023,12060;PD7058,12060;PD7058,12103;PD7108,12104;\ PD7108,12080;PD7138,12080;PD7138,12166;PU6880,12139
This series begins with IN
; that is, initialize the plotter. Then, SP2
says "go get pen number 2" (the red one in my current pen carriage). PU7138,12166
says move to coordinates 7138 by 12166, then PD7108,12166
draws the first line segment. This continues until the end of the file.
HPGL also provides commands for text output from strings, circles, arcs, and other basic geometric shapes. This allows a data analysis program to generate a graph by sending commands directly to the plotter. Because Inkscape already has many of these functions built in, it just dumps the entire drawing as Pen Up and Pen Down commands, rather than translating Inkscape objects to HPGL objects.
Plotting, Start to Finish
To begin the process, I first cut a sheet of paper and load the plotter. Getting the paper square in the machine takes a little bit of practice (Figure 3). If the paper is not square, it may wrinkle or tear. After the paper is loaded, the plotter rolls it back and forth a few times. Once the paper is square, I'm ready to start the software.
The plotting program has two menus: files (numbered commands) and plotter (lettered commands). I use 1) Open a file to open each HPGL file (Figure 4). They will show up in the file list on the bottom of the screen, and the most recently opened file will be shown on the right in list format. (See the "Creating HPGL Files" box for more information.)
Creating HPGL Files
The first step in plotting is to have a file to plot. I decided to help my friend Paul remember the order of poker hands by creating a handy poster in Inkscape.
Once my drawing is finished, I create a separate layer in Inkscape for each color I want to plot. Each layer will become a separate HPGL file. Red had the fewest elements, so I moved them to their own layer, leaving black elements in place.
To create the HPGL files, I hide all the layers (click on the eyeball to turn them off), then turn the layers on one at a time. For each layer I choose File | Save As and then choose HP Graphics Language as my output type. It will give me a few options, but the last two are the most important: pen number and plot invisible layers. Pen number is the color to use for this layer and corresponds to the pens in the plotter carriage. The operator must make sure the right color pen is in the right slot; the plotter doesn't know the difference. Plot invisible layers needs to be unchecked so that the other layers (colors) don't show up in this file.
You'll end up with an HPGL file for each color that you want in the final product.
Most often, multiple HPGL files will be different colors of the same drawing, but they could also be different drawing elements. For example, a graph might contain blank axes as one file and different data sets as additional files.
Once each file is loaded, the arrow keys navigate between them. The "active" file, denoted by a '>
', will receive any flip or shift commands. I can also view the file graphically. Choosing 5) Preview file will use Pygame to create a graphics window and draw the file into it (Figure 5). The graphics window appears on the Raspberry Pi's monitor, which sits behind the plotter.
The next step is to connect to the plotter. Once the connection is successful, the program will ask the plotter for the dimensions of the currently loaded paper and display that next to the plotter menu.
Pressing P starts the plotting process. As commands are sent to the plotter, the file display on the right will scroll automatically to show the progress. A bar graph at the bottom line of the screen shows the current state of the plotter's buffer. Initially, this will drop very quickly because the plotter is getting the pen (a very slow process). As plotter starts drawing lines, the bar graph will oscillate back and forth for the remainder of the plotting job (Figure 6).
Buy this article as PDF
(incl. VAT)