Autolisp Basics In A Nutshell

Testline and Testbeam

Let's start up with something very simple and that will give you immediate results. Fire up AutoCad and type this at the command prompt:

(alert "Yebo Gogo")

Now press enter. This should appear on your screen :



Let's try something else. Type this at the command prompt and press enter :

(setq p1 (getpoint))

Then choose a point anywhere on the screen. A "list" of numbers, looking something like this, should appear in your command window.

(496.0 555.06 0.0)

This list, believe it or not, contains the x, y and z coordinates of the point you picked.

x = 496.04
y = 555.06
z = 0.0

Nesting
Just remember, that you must leave the nest with an equal number of parenthesis. Here's an example :

                    (dosomething (dosomethingelse (andanotherthing)))

You could also write the above statement like this to make it more readable : 
			
				(dosomething
					(dosomethingelse
						(andanotherthing
		 			         )
					  )
				  )

;This is a comment

!p1 (entered at command line will show the variable's current value)

Line [Enter] followed by !p1 [Enter] will leave you with an elastic line swinging from p1 waiting for the next point to be specified.

An input statment that includes a command line prompt:

(setq a ( getpoint "\nChoose a Point : "))

Page2of4

(defun c:testline ()  ;define the function
	(setq a (getpoint "\nEnter First Point: "))  ;get the first point
	(setq b (getpoint "\nEnter Second Point: "))  ;get the second point
	(command "Line" a b "")  ;draw the line
	(princ)  ;clean running (prevents annoying 'nil' left on command line)
 )	;end defun
(princ) ;clean loading (ditto)

Cmpare the command "Line above with

(command ".pline" a1 b1 "A" b2 "L" a2 "A" "CL")

from mslot.lsp



I'm sure I saw reference to a dot and thought it was something to do with 'rubber bands' but now I think it is to enable a command that might have been disabled.

It is a Pline straight from the 'a' arc a1 endpoint to the matching b1 arc endpoint then A(rc) peck to do the 'b' arc then L(ine) peck to draw a straight pline to the matching 'a' arc endpoint then A(rc) peck and complete the 'a' arc back to the start point with a Cl(ose).

Other:
If c: then testline [Enter]
No c: then (testline) [Enter]

Use notepad [Enter} [Enter] to bringup Notepad from command line. In the acad.pgp file, copy the line of code starting 'notepad' to a line down from it and edit 'notepad' to just be 'np'. ie

copy and edit..
  NOTEPAD, START NOTEPAD, 1,*File to edit: ,
to look like..
  np, start notepad, 1,,

Now issue REINIT->check pgp->OK (or re-start Autocad) to load the changed acad.pgp file. Notepad will now start with just 'np' and one [Enter] now.

Use VLIDE [Enter] to bring up the Visual Lisp Editor. It's not bad, cetainly better than Notepad. While you are in the acad .pgp file maybe add the line:

vl     *vlide

Note I don't capitalise in the acad.pgp file. It is not case sensitive and as all of the pgp file is in capitals I know that any lower case entries are my own modifications. This pretty well qualifies as 'Self Commenting' code.

Consider a lisp to quickly clear the screen of your various lines drawn while practing Autolisp:-

Based on Erase [Enter] All [Enter] [Enter]
we can have ...

(defun c:EA()
    (command "ERASE" "ALL" "")

Note the [Enter] is implicit in "erase" and "all" only the extra "" is necessary for the last [Enter] to concluding the selection process.

Indenting is to aid clarity in multiple line code. If it was done in one line it looks ok.

(defun c:EA()(command "ERASE" "ALL" ""))

You can add a prompt eg

(prompt "Enter HLX to start")

..this was found at the end after the program final closing bracket in Tony Hotchkiss's surface thread helix.lsp

(defun c:hlx ()
   (setting)
   (helix)
   (resetting)
   (princ)
) ;_ defun

(prompt "Enter HLX to start")

It was definitely an after thought because the was no (princ) put for it and a 'nil' showed on the command line.

Should have been..

(prompt "Enter HLX to start")
(princ)

Also notice the Program was on the bottom with three sub-routines above it.

See routine below. This is from two thirds way through ASP9.DOC in adesk_compuserve lessons. It looks like a Basic language Case statement to me. See a calculated resultant or return from a IF statement or choosen menu item number gets passed in as 'n' and then an action occurs from a larger list of options than is convenient with IF or ELSEIF type conditional statements. The pissy print of the value in English is just a stub to proove the thing. The power is it could call specific routines or programs. Looks like the Menu set up I use to have on diskettes with basic programs to be able to run from a menu everything on that particular diskette. Very cool.


    (defun say-digit (n)
       (cond (  (minsup n) (princ "\nNo negative values!"))
             (  (zerop n) (princ "zero"))
             (  (eq n 1) (princ "one"))
             (  (eq n 2) (princ "two"))
             (  (eq n 3) (princ "three"))
             (  (eq n 4) (princ "four"))
             (  (eq n 5) (princ "five"))
             (t (princ "Sorry, I can't count that high"))
         )
       (princ)
     )

Here is the sequence of expressions that are evaluated if the value supplied to SAY-DIGIT were 3:

     Expression      Result
     -----------------------
     (minsup n)      Nil
     (zerop n)       Nil
     (eq n 1)        Nil
     (eq n 2)        Nil
     (eq n 3)        T        <---- COND stops evaluating 's
     (princ "three") "three"        and starts evaluating 's
                                           
Enter SAY-DIGIT into your text editor, load it, and give it a try:

   Command: (say-digit 3)
   "three"
   Command: (say-digit 1)
   "one"
   Command: (say-digit 4)
   "four"
   Command: (say-digit 8)
   "Sorry, I can't count that high"
   Command:

This should make the basic function of COND clear. And, note how the default case is evaluated when no test is successful. COND is also a SPECIAL FORM that does CONDITIONAL EVALUATION. It will evaluate only to the first test expression that results in a non-nil value and will not evaluate any part of any proceeding clauses. We will also learn about a more effecient way of performing an operation like this using another AutoLISP function called ASSOC, in a future installment.

One very commonplace use of COND, is to branch on a series of tests that are performed on a value input by the user, where a command may give a user a choice of several options or "commands", where each of these options cause a different action to be taken by the program.

In the following example, we will write a TINY RPN calculator that will let you enter numeric values (or distances) and operators right on the command line, and have the results displayed. We'll be using some functions that you might not have used before, or may not be familiar with. Don't worry about them for now, the purpose here is simply to show the COND function in action. But you might find this TINY RPN calculator to be very useful with AutoCAD, so enter it into your text editor, and give it a try!

** I stopped copying here but it was good stuff ..**





Insertion Point
Length of Beam
Height of Beam
Flange Thickness
End Plate Thickness  
Length of Notch
Depth of Notch

ip
lb
hb
wt
ep
nl
nd