Tuesday, November 3, 2009

Writing Postscript output

To start off with this post I should mention a book I have found very useful

Learning PostScript by Doing
By Andre Heck

This book has straightforward examples that build on one another and is easy to use to come quickly up to a usable understanding of postscript coding.
The simplicity and ubiquitous-ness of postscript is its strength as far as this application is concerned. So for ease of use I have decided to use postscript as the output format. Also postscripts human-readable formatting and syntax make for simpler troubleshooting during development.

Postscript can also be read by lots of other softwares and can be easily transformed into formats (such as DXF, and STL) that machines like rapidprototypers and CNC type devices understand. (So for all you computer leaning artists out there learning to code postscript allows you to create files that can drive these machines and fabricate pieces!!)

Here is a snippet of Ruby code that I use to write out some postscript to the output.ps file.

1.upto(qt11degxycount) { |z| z = z - 1

currentlinetoobj = qt11degxy[z]

currentlinetox = currentlinetoobj[0] * xstretch

currentlinetoy = currentlinetoobj[1] * ystretch

makeline = qt1x, " ", qt1y, " lineto"

movetoposition = currentlinetox, " ", currentlinetoy, " moveto"



output.puts "newpath" #writes out a postscript newpath call

output.puts movetoposition.to_s #this writes out the moveto

output.puts "addformathalfcircleqt1" #this changes the formatting to deg 1 qt1 formatting for a half circle

output.puts "newpath" #writes out a postscript newpath call

output.puts "addformat11degline"

output.puts movetoposition.to_s #this writes out the moveto

output.puts makeline.to_s #this writes out the lineto

output.puts " "

}


Another nice thing about the structure of drawing "newpath" elements in a .ps file is their modularity and lack of nesseccary dependancy in their position in the file. In other words you can put a particular "newpath" call in the beginning of the .ps file or all the way at the end and the file will still parse and display. The only real issue with drawing element position in the .ps file is layering. every time you write to the screen it draws over anything that was drawn prior.

The previous code block itterates and produces a set of postscript modules that look like this

newpath
286 1122 moveto
addformathalfcircleqt1
newpath
143 1496 lineto
addformat11degline

Postscript allows for sets of commands to be grouped into a definition and called by the use of a variable. for instance "
addformathalfcircleqt1" is a definition for a set of code that makes a particular marker in the output file. Definitions are a great way to keep formatting under control, and to organize your code.

Here is the definition that is called by the "
addformathalfcircleqt1"

/addformathalfcircleqt1
{
6 0 180 arc
0.2 1 0.1 setrgbcolor %sets color to lime green
2 setlinewidth
stroke
}
def

No comments:

Post a Comment