Showing posts with label Autolisp. Show all posts
Showing posts with label Autolisp. Show all posts

Saturday, September 18, 2010

Video Productions and old 2D lisps

Once, long ago, last century I posted a heap of supposedly useful lisp routines on an old web site (now long gone). I have gone through these and reposted them on:

http://bilrocad.com/Lisps2d.html

I have also posted some ancient 3D ones on:

http://bilrocad.com/Lisps3d.html

It struck me that some of these are hard to use(!), so I have done a few videos, creating a special page for them at:

http://bilrocad.com/Videos.html

It was a bit of a learning curve doing them - they ended up with huge file sizes, so I downloaded a free converter and condensed them a bit.

Before 2000, I was interested in Virtual Reality Modelling Language (VRML) and sort of expected that this would evolve into something useful around about now. It appears not....although maybe I have not looked hard enough. I looked at all the old sites and most of them seem to be stuck around 7 years ago. Octaga seems to be the only one still chugging.

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.