Thursday, December 16, 2010

Creating basic AutoCAD LISP with Python

I recently started a new job as a design engineer in Norway and have been looking for a way to draw line and text examples that would show the various line types and colors according to our pen settings in our plot file.
So I was going to draw a line, then change the color, and write the name of the color next to it.
The problem was that there was 255 colors in AutoCAD and that would take a long time.
Because the process was repetative I decided to try to write a lisp routine that would draw an assortment of polylines and then put them on correct layers with the appropriate colors.
Now after messing around with python I created a simple script (with the help of that would generate the lisp routine.

fn = "instructions.lsp"
instructionList = ['(defun C:layermaker()',]
for i in range(1,255):
    instructionList.append('(COMMAND "-LAYER" "MAKE" "COLOR %s" "COLOR" "%s" "" "")' % (i, i))      instructionList.append('(COMMAND "PLINE" "%s,0" "%s,2000" "")' % (i*50, i*50))
    instructionList.append('(COMMAND "TEXT" "%s,2200" "30" "0" "COLOR %s")' % (i*50,i))
f = open(fn, 'w')
This python code creates a file called instructions.lsp, then makes a list called instructionList and adds the lisp function name of (defun C:layermaker()
It then iterates from 1 to 255 items and concatenates the numbers of the colors into the autoCAD commands.
Then it opens the file, writes the list to the file, adds a parenthesis and closes it.

Here's what it generates
(defun C:layermaker()
(COMMAND "-LAYER" "MAKE" "COLOR 1" "COLOR" "1" "" "")
(COMMAND "PLINE" "50,0" "50,2000" "")
(COMMAND "TEXT" "50,2200" "30" "0" "COLOR 1")
(COMMAND "-LAYER" "MAKE" "COLOR 2" "COLOR" "2" "" "")
(COMMAND "-LAYER" "MAKE" "COLOR 254" "COLOR" "254" "" "")
(COMMAND "PLINE" "12700,0" "12700,2000" "")
(COMMAND "TEXT" "12700,2200" "30" "0" "COLOR 254"))

The coding is very basic and it basically took me several hours to figure out how to do this because I'm new at python.  But I learned alot.
If you're interested in the DXF file just add a comment and I'll see what I can do to get it to you.
Basically what you'll have is a way to plot all your pen styles onto one drawing so you know what your colors and lineweights are.

Here's the code in blue and comments in red.
fn = "instructions.lsp" #Creates a file named 'instructions.lsp'

instructionList = ['(defun C:layermaker()',]
#Creates a list called instructionList and makes the first item on the list '(defun C:layermaker()' which is the name of the function.
for i in range(1,255):#Uses the range function to iterate from 1 to 255 which is the number of colors that AutoCAD has.
    instructionList.append('(COMMAND "-LAYER" "MAKE" "COLOR %s" "COLOR" "%s" "" "")' % (i, i)) #Uses the append function to add an autoCAD command that creates a new layer.  The %s is the variable and is defined at the end by i,i
    instructionList.append('(COMMAND "PLINE" "%s,0" "%s,2000" "")' % (i*50, i*50)) #Uses the append function to add an autoCAD command that creates a polyline that is 2000 units long.  It inserts the variable i multiplied by 50 to create the spacing for the line.
    instructionList.append('(COMMAND "TEXT" "%s,2200" "30" "0" "COLOR %s")' % (i*50,i)) #Uses the append function to add an autoCAD command that creates a line of text.  Notice the i*50,i.  This multiplies the variable times 50 which allows the proper spacing, then the second i refers to the name of the color.
#At this point it loops back to the first append function and iterates with the next number untill it reaches 255
f = open(fn, 'w') #this opens the file defined as fn which is defined above as instructions.lsp
f.write("\n".join(instructionList)) #this writes the list to the file
f.write(")") #this adds a parenthesis to the end of the function
f.close() #this closes the file