Wednesday, December 24, 2008

Lisp: Everyone draws pipes don't they?


Of course they don't. I know that. Here is now made available, not exactly on a plate, a FREE, fun way to draw pipes. Not quite the end of drafting drudgery, but the start of the end!


Unfortunately, when I tried to put them on my blog all the lines ended up all over the place.

So I have made the decision to make the files available on a "request only" basis. That means you e-m-a-i-l me, asking for them, and I send them to you. (For free). I am trying to avoid the spammers here by saying think about blecouteur at xtra dot co dot nz.


In the process of testing: another snafu-the dialog box won't fit on the screen. This is because at work I have a big screen.....So I have now 2 dialog boxes to send you- one a chopped down one and one that is the full nine bananas. The small one does dairy pipes only-Grr...I will have to see about more columns...which can be done, it just makes life more complicated. It probably needs doing anyway. Try the full one called pipeset.dcl. if it does not work try the smaller one.


Every time I use this routine I am pleased at the appearance of pipes, as if by magic. There is a certain amount of ego stuff here, but I am at least honest enough to say that out there somewhere someone has done the same thing. (Probably a while ago.)

I had thought briefly of selling the combined routine, but they are very short routines, and any

capable lisp writer, if they had seen them being used, would be able to re-write them easily.


The idea is you run one routine initally to set the diameter of the pipe you want to be drawing.

The name of this routine is pipeset.lsp

Then you draw lines representing the pipe routes. Fiddle around with these to get them right.

Next, use pir.lsp to do the pipe radii. Unfortunately you have to do this elbow by elbow. One day I may make a routine to do this all in one hit.


Now comes the big moment: Set the routine pip.lsp onto that bunch of lines/arcs. That's right just select the damn lot all at once! Whoopee!....or it should be.


One of the routines uses a dialog box. This is contained in a separate file called a .dcl file.

The trick with this one is that the routine that accompanies it (pipeset.lsp), needs to know where you stuck it on your hard drive. You should be able to see the bit early on in the routine saying where the dialog file is kept.


Some people just get lazy and stick them all in the support directory, but this can be a pain if you upgrade.


Monday, December 22, 2008

Lisp, Using an incremental approach, and Variables

The Development Approach
1. Start simple.

2. Test it.

Once you have one bit working, move on to the next bit.

Don't try to write the whole program all at once, all in one hit. You will make mistakes, and
these are quite tricky to find with Lisp.

Make your first lisp routine 3 or 4 lines if you can. Leave the handrail generator for when you have 3 weeks off.

Looking for help? Try going Tools/Autolisp/Visual Lisp Editor. Once the Editor is up you can hit help. Someone at Autodesk is a smart one, because he put all the lisp commands under an aphabetical listing in the help section. Which is good if you know the name of the command.

Basics of Variables
What is a variable? Just a name, that you have made up, and you assign a value to.
This value might change over usage.

For instance, consider:
(setq the_length 22)
The minute Autocad sees this it says "Aha! that variable the_length, it has an integer value of 22!" You may not know what an integer is. It is definitely nothing complicated, all it means
is it is a whole number. So for instance 22.23 would not be an integer.
Just a trap for young players here....do not use variables like angle or length. These are already
reserved for Autocad's own use and causes trouble if you do (Yes...I am guilty!)

How about:
(setq the_layer_name "Fred")
Here, the variable is being set to a string (whats a string?-just a bunch of characters)
You could change this in the course of the program by going:
(setq the_layer_name "Sam")

Naming of Variables
I'm afraid I have been a bad boy as far as consistency is concerned here: I seem to choose
whatever takes my fancy at the time. Sometimes I like running all the letters together, for
instance thelayername. Other times I go the_layer_name or TheLayerName.
Just remember lisp does not care about upper or lower case so just choose the system that
suits you best. But remember: One day you might have to read your own program back,
and having variables like "e" or "b" or "updown" won't make a lot of sense.

Get a peer review
At one stage I had written a 3d stair generator, which I was intensely proud of.
Until I gave it to a friend, who kindly pointed out that it did not compute the stairs he
thought it should be able to. He was right. I was wrong. But I went away and corrected
it and now it goes nicely. I hope to post this over the Xmas period, along with some other
ones.

There are good books out there, I bought one (Woops in 1992!) called Maximising Autolisp
by Rusty Gesner and Joseph Smith, and I have had good use out of it.

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.