Friday, December 12, 2008

Autolisp

This will be a short series of posts, concentrating on Autolisp routines that I have written.



I will attempt to provide routines that are heavily commented, so you can understand how they work.



My first point is that you do not have to be a genius to write lisp routines (?!) and that you can get quite a lot of satisfaction out of just a few lines of code.



It has been usable with every version of Autocad that I can remember, and one of it's chief advantages is that most routines will run on any version. Unlike visual basic for applications (VBA).



The implementation is easy and elegant: you get Notepad (have a look under Accessories on your Windows PC) up and running and start typing. To use, you can go "appload" in Autocad, but I prefer the lazy two screen approach: just drag and drop the file into autocad to load it.



Just remeber that file extension has to be lsp, so for instance it might be called cor.lsp.



This is a hangover from the MSDOS days and you may need to go to Windows Explorer and turn on the option that says "Hide file extensions for known file types" (Under Tools-Folder Options-View) . This is important because Notepad will save it's files with the extension .txt.



You will need to change that when saving, to .lsp.



Because blogspot don't allow file downloads, all routines will be shown in the blog post: you just cut and paste them into notepad.



One last "need to know": You need to create yet another file, always called acad.lsp.

This is a special file (again, just an easy text file) , that Autocad knows about and will check for.

If it finds it, it will load the routines listed in it so you have them all available at the command line when you need them.



Here is an example that you could cut and paste into Notepad, and, if you save it into

c:\program files\autocad\support directory (yours may vary slightly from this), it will make available the commands COR and IL. I use abbreviations to cut down the typing- COR stands for COPY AND ROTATE, and IL stands for ISOLATE LAYER. These are the two simple routines I am going to start you off on.



;--loaded functions

(defun C:cor () (load "c:/bilro/bilro1/cor")(c:cor))

(defun C:il () (load "c:/bilro/bilro2/il")(c:il))



What all the above stands for is that we are defining a function (hence defun) that can be used on the command line (hence the c:) and that part of this functions job is to load a lisp routine and make it available . It is a bit cryptic, but I find if you just follow this format, it always works.



Of course, all this loading is not much point if the routines cor and il don't exist at the path I have shown ie c:/bilro/bilro1/cor. Your path can be different-it does not matter where you keep them.



Setq - what does it mean? It just means "assign this to the said variable"



So, here is the code for COR.lsp:



;written by Bill LeCouteur-this line is just a comment -ignored by Autocad because of ";"

;this routine copies & rotates-this line is just a comment -ignored by Autocad because of ";"

(defun c:cor ()

(prompt "\nSelect the items to be copied and rotated: ")

(setq ss1 (ssget)) ;gets a selection set and sets it to a variable called ss1

(setq pt1 (getpoint "\nEnter rotation point for the picked items: ")) ;gets a user defined point

(command "copy" ss1 "" "0,0" "0,0") ; issues a standard autocad command-copy

(command "rotate" ss1 "" (list (car pt1) (cadr pt1)) pause); same again -pause is for user input

(command "redraw"); same again-standard command

(princ) ;exits cleanly

);this closes the parentheses at the start.



Here is the code for IL.lsp



;Program written by Bill Le Couteur

;Auckland NZ;

Rev 0 date 16.6.95;

This program isolates a layer by picking it

(defun c:IL()

(setq edata(entget(car(entsel)))) ;gets the user to select an item and sets the variable edata to it

(setq thelayer(cdr(assoc 8 edata))) ;finds out the layer it's from by looking at edata

(command "-layer" "s" thelayer "");sets the current layer to that layer

(command "-layer" "f" "*" "");freezes every other layer

(princ);exits cleanly

);end of defun



Whew! If you have got this far, you may have such enthusiasm that you might have actually attempted to do the above, so you may need help, so I have turned the comments on this blog to anyone can comment, and I will attempt to check this regularly and help if needed.

8 comments:

Unknown said...

Yes, I totally agree. AutoLISP rules. I like that AutoLISP has no type casting on variables. You can do some real crazy sh#$ with that! Its great as long as you don't muck it up with Visual LISP. Then it can't be run on anything but AutoCAD, and I can't afford $4000.00. I use progeCAD IntelliCAD ($375 USD) and have run into times when I couldn't run customers routines because of the short sighted, proprietary thinking of the folks in Sausalito. That, and their custom objects that render most programs showing proxies out of the DWG files, and customers can just about say goodbye to interoperablity. Luckily, progeCAD IntelliCAD can read and render objects in ADT, MDT, and Civil 3D DWG files.

William Le Couteur said...

Well,hmm...I have never heard of progeCAD. I have tried another one of the autocad clones, and just about burst into tears using it!
Which has put me off clones. Maybe one bad clone should not?

Anonymous said...

Well, there was a time in the past where the IntelliCAD product was not lets say, up to snuff. But there was some big changes in the organization around 2005 and they got their stuff together. progeCAD IntelliCAD is now a reliable low cost replacement for AutoCAD and AutoCAD LT. The ITC (IntelliCAD Technology Consortium) is bringing out a totally rewritten version first to second quarter next year including a clone of ARX. Should prove interesting! Try out the 30 day trial download and I bet its not as bad as you think, and at 1/10 the price you dont have to sell your car to get it!

Anonymous said...

Bricscad is wihtout doubt the best alternative platform for applications today. They support LISP, COM/VBA, DCL, and even ObjectARX in a code compatible way. So you don't need to convert or rewrite any of your source code, you simply compile for Bricscad and/or Acad based on the same source code set. You might want to try it, their website is www.bricsys.com. You can find a whole list of already ported applications there. I believe their developer reference guide is also available on the website.

Unknown said...

Dear William 1,

You told us. You have never heard of progeCAD. You have
tried another one of the AutoCAD clones and just about
burst into tears using it!

That is why you have had enough of AutoCAD clones. You
are not using them. You did not say that. But I guess
you are working with AutoCAD.

Very good. Autodesk loves what you were saying. Are
they giving you a free version of AutoCAD now? That
would be nice.

Because when you get a free version of AutoCAD, then
you do not have to spend a lot of money for getting
AutoCAD.

No. I did not burst into tears as I was using AutoCAD.
But I realized. Using AutoCAD is a waste of a lot of
money.

And Autodesk is doing everything to have you keep on
wasting a lot of money. Every year they have a new
version and new file dimensions.

I started working with IntelliCAD. And I was saving a
lot of money. That is so good about IntelliCAD. It is
the same as AutoCAD.

Yeah. You have had enough of AutoCAD clones. I have a
suggestion for you. Read my blog. There you can find
all AutoCAD clones.

Did you know? One AutoCAD clones is free of charge. It
doesn't cost you a dime. And it works like AutoCAD. See
my blog.

OK. You made some good remarks about AutoLISP. And in
your blog you had two programs. Let's have a look at
your first program.

That program is the COR.LSP program. You wrote it. And
did you test it before publishign on your blog. I don't
think so.

Here is your program. And it doesn't work.

(defun c:cor ()
(prompt
"\nSelect the items to be copied and
rotated:
"
)
(setq ss1 (ssget))
(setq pt1 (getpoint
"\nEnter rotation point for the
picked items:
"
)
)
(command "copy" ss1 "" "0,0" "0,0")
(command "rotate" ss1
""
(list (car pt1)
(cadr pt1)
)
pause
)
(command "redraw")
(princ)
)

Jos van Doorn

Unknown said...

Dear William 2,

Why does it not work? It starts with the line with
which the COPY command is invoked. You must end the
command with a carriage return.

Another silly thing. You need a rotation point for the
ROTATE command. You had a very complicated
construction. You had:

(list (car pt1) (cadr pt1))

You could have specified the rotation point. You could
have specified PT1.

I went over your AutoLISP program. I changed it. This
is the program that I have written.

(defun c:cprot (/ p1 ss)
(setvar "cmdecho" 0)
(prompt
"\nSelect the items to be copied and
rotated:
"
)
(setq ss (ssget))
(setq p1 (getpoint
"\nEnter rotation point for the
picked items:
"
)
)
(command "copy" ss "" "0,0" "0,0" "")
(command "rotate" ss "" p1 pause)
(command "redraw")
(setvar "cmdecho" 1)
(princ)
)

(c:cprot)

This is what I have done in my program. And my program
works!

- I changed the name. The name is now CPROT. CPROT
stands for COPY and ROTATE. I find the name COR not
very clear.

- I have declared the variables of the AutoLISP
program local. The variables only have a value in
the program. That saves memory.

- At the start of the program the CMDECHO system
variable gets the value zero. I don't want command
echos.

- At the end of the program the CMDECHO system
variable gets the value one. We are back in CAD and
want command echos.

- A carriage return is added to the programming line
that invokes the COPY command. Now you don't stay in
that command.

- P1 is added to the programing line that invokes the
ROTATE command. I don't have the construction with
CAR and CADR.

- The line (C:CPROT) is added to the AutoLISP program.
Because of that line the program starts right away
as it has been loaded.

Don't you think my program is much better? I don't know
about your readers. Do they not try your programs out?

Oh. You can find my blog here:

http://www.makeautocadfast.blogspot.com

If you want, I could mention your blog in my blog.
That could be interesting if you have got an AutoLISP
program that works.

Jos van Doorn


William Le Couteur said...

Hi Jos
Thanks for your comments-most likely the process of putting the routine on the blog caused problems because of carriage returns. I know this one works, and nicely because I have used it for years now. It should be a standard autocad command.

Apologies for the late reply, I have been playing with Revit and Autocad has been neglected!
Regards
Bill Le Couteur

William Le Couteur said...

By the way, I get absolutely no free copy of Autocad from Autodesk.

I have had no freebies/gifts/flights to San Francisco from Autodesk.

I can say that I find Autocad a very good CAD program. I have started to learn Revit, which a lot of people rave about. It is nice, good and fast as well, but when I had to model a coffee table the other day, which program did I use? Autocad. Then brought it into my Revit Project easily. It is horses for courses: Autocad is a very versatile general use program and Revit is about drawing buildings.

Regards
Bill Le Couteur