You are on page 1of 6

ETD2332 AutoCAD Customization Syllabus

AutoLISP

AutoLiSP is an embedded programming language in AutoCAD. It is a subset of the LISP programming language. AutoLiSP code uses nested parentheses to control the sequence of program execution, thus the program is one long parenthetical expression (a line that begins with a semicolon; is a comment).

;;F Miller 04-03-06 ;;Slope12 lisp routine

;;allows two picks and returns the rise of the picks if the run = 12 ;;slope inquiry command

, ,

;;----------The new command name is 512-------(defun e:s12 (I pt1 pt2 hx hy)

(setq pt1 (getpoint "\nPick first point "» (setq pt2 (getpoint "\nPiek second point"» (setq hx (- (car pt2) (car pt1»)

(setq hy (- (cadr pt2) (eadr pt1») (textscr)

(prine "\n \n Rise is ") (prine (* 12.0 (I hy hx») (prine" on 12")

(prine)

) ;end defun e:s12

The indentions are just to make the code more readable. Sometimes a single space is required but after that the additional spacing is ignored.

LISP is an acronym for LiSt Processing. The unique feature of this programming language is that a single variable can represent a list of information. A 3D coordinate has an X ordinate, a Yordinate, and a Z ordinate. Together they are a list. Each element of the list is called an atom. Some will argue that LISP is an acronym for Lost In Stupid Parentheses.

The intent of this exercise is to familiarize the student with AutoLiSP so that AutoLiSP routines can be copied from magazines, web sites, and tip books, stored, and implemented. A small amount of program logic is presented but it is NOT the intention of this exercise to teach programming logic or the peculiarities of AutoLiSP.

AutoLiSP routines can be assigned to toolbar buttons, palette tools, and pull (pop) down menus. AutoLiSP and DIESEL can add information to the AutoCAD Status line, which is the subject of a later exercise. AutoLiSP routines can be made to run when AutoCAD is first started. Many commands are really AutoLiSP routines that are evoked in the same manner as true AutoCAD commands. There are other, more robust programming languages that AutoCAD uses like Visual Basic and C++. The entire AutoCAD renderer is actually an ARX application that is external to the main body of the AutoCAD program.

C:\lgs\CuslomizalionPJC\Syllabus\SyllabusAutoCADCustom.doc 05/08/06

Page 100

ETD2332 AutoCAD Customization Syllabus

Command Line AutoLiSP

AutoLiSP expressions can be typed at the Command: prompt. For example, if the following was typed at the Command: Prompt

Command:(+ 4 17)~

AutoCAD would return (respond with) 21

Other operators include subtraction, multiplication and division. The format is: (operand num1 num2)

Typing the following would perform the task after the comment character:

Command:(- 23 7)~ 16

Command:(* 46 3)~ 138

;numl - num2

;num1 x num2

Command:(/ 128 4)~ 32

All the numbers in the previous example were integers (whole numbers). Notice what happens to fractional answers when integers are used

Command:(/ 128 3)~

42

numl

But, if real numbers are used, the answer is correct. Command:(/ 128.0 3.0)~

42.6667

Nesting can add more functionality to this process. Command:(/ 157.32 (+ 12.3 9»~

7.38592

Notice what happens when the last (stupid) parentheses is omitted. Command:(/ 157.32 (+ 12.3 9)~

( >

Typing the missing last parentheses will finish the expression (_> )~

7.38592

Too many parentheses gets a sensible response:

Command:{/ 157.32 (+ 12.3 9»)~

; error: extra right paren on input

If you leave out a quotation mark, the error will be either:

C:\lgs\CuslomizationPJC\Syllabus\SyllabusAuloCADCuslom.doc 05/08/06

Page 101

ETD2332 AutoCAD Customization Syllabus

error: malformed list

; error: malformed string on input

AutoLiSP At Command Value Prompts

Whenever AutoCAD is asking for a value, you can respond with an AutoLiSP expression. For example, if you need a circle with a radius that is one third of 17.32, then when AutoCAD prompts for the circle radius, type (/17.32 3.0).J and the value of the expression will be returned to the radius prompt.

New Keyboard Shortcuts

More elaborate AutoLiSP expressions can be typed at the Command: prompt. For example, the following would define a new keyboard command, L Y, that uses the LENGTHEN command and DYnamic option.

Command: (defun c:LY()(command "LENGTHEN" "DYnamic"».J

After loading this expression, typing L Y.J at the Command: prompt would execute the new command. Try this on a line in a drawing.

The new command stays in memory until AutoCAD is stopped.

A macro could do the same thing but it must be assigned to a command and then a: button, pull-down menu, or palette.

Remember that aliases in the acad.pgp file cannot specify options. The previous short expression would better serve you if it was typed into Notepad or the like, loaded, and then executed.

AutoLiSP Files

Several AutoLiSP expressions can be written in a file and then loaded. Suppose the previous AutoLiSP expression:

(defun c: L Y ()

(command "LENGTHEN" "DYnamic")

)

was written in Notepad and stored in a file named MyStuff.lsp (the .Isp is required). If MyStuff.lsp was in the AutoCAD support path, then typing the following including the parenthesis at the Command prompt would load it.

Command: (load"MyStuff").J

C :\lgs\C ustom izalionP JC\Syllab us\SyliabusAutoCADCustom .doc 05/08/06

Page 102

ETD2332 AutoCAD Customization Syllabus

If MyStuff.lsp was not in the support path, it could be loaded at the command line but the goofy punctuation required for the drive letter and paths is not worth it. Instead, use the APPLOAD command.

Angle Inquiry Command

To know the angle between two lines requires that you use the dimangular command and either interrupt the command, or undo or erase the angular dimension. The dimangular command will not work in some conditions: for example between the edges of solids or regions or other complex objects.

The angle between 2 lines can be measured six different ways with two ways giving the same result. AutoCAD will not dimension any angle over 1800 unless the vertex option is used (try it!)

Smallest Angle 37° 370
Supplimenlary Angle 180°·3r 1430
S'T·I'~·>'. Anu > !dur;!::':I\:,l ,-,'" J!<
~ ... i' ~~, ~;:;:;.;;';~ ); ,)!
p(j;):;ii!;y~r:iJ' i ruck:' ,(!L;. />3:F;) 'iE:;,'" ~ 31c' 1.'];'"
Largest Angle 360" - 37" 323" Impossible for AutoCAD
Complementary angle 90° - 37" 53° not shown
Over 1800 Angle 1800 + 3r 217" Impossible for AutoCAD An angle and its supplement angle make 1800• An angle and its compliment angle make 900•

An angle inquiry command must overcome all these obstacles. The simplest way is for the user to identify four points: the first two points will establish one line and the last two points will establish a second line. The command would then return the four possible angles between the two lines - it is up to the user to decide which angle to consider. The routine also returns a fifth angle which is just the compliment of the smallest angle.

The angle inquiry command will not actually draw any lines. The four points can be object snapped and can be points on any type entity.

Start a drawing and draw two non-parallel lines.

C:\lgS\CustomizationPJC\Syliabus\SyllabusAutoCADCustom.doc 05/08/06

Page 103

ETD2332 AutoCAD Customization Syllabus

With the APPLOAD command, browse to T:\miller\ETD2332\LlSP, find ANG.lsp and ILoadl it.

The new command that the ANG.lsp routine makes is named ZZ. Type ZZ.J at the Command: prompt, pick four points, and observe the results ..

After picking the four points, ZZ will "flip" the screen to the AutoCAD text screen and show the five results.

The units of the angles in the output is controlled by the angle settings of the UNITS command. Change the UNITS command angle settings and retry ZZ.

Offset one of the lines and use the four points on the two parallel lines.

C :\lgs\CustomizationP JC\Syllabus\SyJlabusAuloCADCustom .doc 05/08/06

Page 104

ETD2332 AutoCAD Customization Syllabus

Automatic Loading of LISP Routines

It would be a nuisance to have to APPLOAD ANG.lsp every time AutoCAD is started. If it was renamed to ACADDOC.lsp and in your support path (S:\student\CUSTOM) it will load automatically every time a drawing is opened. Additional routines can be "patched" into the ACADDOC.lsp file so they too will be loaded automatically. There are other ways to get AutoLiSP routines to automatically load, but this is the easiest.

Copy ANG.lsp to your S:\student\CUSTOM folder. Rename ANG.lsp to ACADDOC.lsp. Restart AutoCAD. After making your profile current, open a new or existing drawing, and see if the ZZ command still works. If it does, use the CUI command to create a new command where the macro is simply ZZ. Assign this new command to a button and

draw an .image for the button (the suggested image was modified fromw .. ' i age used

for the dimanqular command).,<·.

Assignment: AutoLiSP Part 1

Cut and paste the AutoLiSP file named Slope12.lsp into your ACADDOC.lsp file in your support path. Determine the name of the command created by Slope12.lsp and use that command name as the macro for a new command and assign that to a button complete with image.

Demonstrate to the instructor that after restarting AutoCAD, both your four-point angle and slope inquiry buttons work.

Part 2

Go to a web site, download a AutoLiSP file, and APPLOAD or drag and drop it into your drawing. Demonstrate whatever the new command is supposed to do.

Suggested sites for AutoLiSP routines.

http://management.cadalyst.com/cadman/article/articleList.jsp?categoryld=673 http://www.ramlug.org/index.php?option=com docman&Itemid=68 http://www.tenlinks.com/cad/users/autocad/shareware/autolisp.htm http://www.tenlinks.com/cad/users/autocad/shareware/autolisp.htm http://www.manusoft.com/software/freebies/lndex.stm http://www.wport.com/-nemil

http://www.dotsoft.com/freestuff.htm http://www.cadresource.com/library/lisp.html http://www.caddepot.com/cgi-bin/cfiles/cfiles.cgi?0.40.0,0,1 http://www.cadmonkey.comllisp.htm

A file of these URL 14S are located at T:\miller\ETD2332\LlSP\URLs.txt

14 Uniform Resource Locator - the official name for web site addresses

C:\lgS\CustomizationPJC\Syliabus\SyllabusAuloCADCustom.doc 05/08/06

Page 105

You might also like