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. Auto ISP 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). 53F Miller 04-03-06 ‘Slopet2 lisp routine allows two picks and returns the rise of the picks if the run = 12 slope inquiry command a -The new connand nane is $12--- (defun c:s12 (/ ptt pt2 hx hy) (setq ptt (getpoint “\nPick first point ")) (setq pt2 (getpoint “\nPick second point") (setq hx (- (car pt2) (car ptt))) (setq hy (- (cadr pt2) (cadr ptt))) (textscr) (princ *\n \n Rise is *) (prince (* 12.0 (/ hy hx))) (prine * on 12") (prince) ) send defun c:st2 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 Y ordinate, 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 AutoL ISP. AutoL ISP 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 Autol.|SP 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, ‘Cilgs\ Customization? JC\Sylabus\SylabusAuloCADCustom dec 06/08/08 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).1 AutoCAD would return (respond with) 21 Other operators include subtraction, multiplication and di (operand nut num2) Typing the following would perform the task after the comment character: in. The format is: Command: (- 23 7)1 ¢numl = num2 16 Command: (* 46 3). gnuml x num2 138 2 Command: (/ 128 4)4 -RiRS 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 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: (/ 187.32 (+ 12.3 9) (> Typing the missing last parentheses will finish the expression (> )4 738592 Too many parentheses gets a sensible response: Command: (/ 157.32 (+ 12.3 9)))u } error: extra right paren on input If you leave out a quotation mark, the error will be either: ‘Cilgs\CustomzationP JCASylabus\SylabusAuloCADCustom coc 05/0808 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, LY, that uses the LENGTHEN command and DYnamic option. Conmand: (defun ¢:L¥() (command "LENGTHEN* "DYnamic")).! After loading this expression, typing LY. 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¥() (command "LENGTHEN" "DYnamic") ) was written in Notepad and stored in a file named MyStuff.isp (the .Isp is required). If MyStuff.1sp was in the AutoCAD support path, then typing the following including the parenthesis at the Command prompt would load it Command: (Load“WyStuff”) J (CAigs\CustomizationP Jc\Sylabus\SylabusAutoCADCustom doe 05/0806 Page 102 ETD2332 AutoCAD Customization Syllabus If MyStuff.Isp 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 180° unless the vertex option is used (try it!) ar Ds Suppimentary gle 143° Largest Angle fasor—re __fazae possi fr AROCAD, [Complementary angi oor a7* fsa Yoot shawn: [Over 180° Angie [ra0° + 37° Jat7* [Impossible for AutoCAD An angle and its supplement angle make 180°. An angle and its compliment angle make 90°. 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. ‘Ciigs\ Customization? JCiSylabus\SytabusAUteCADCUstom doc 05/08/06 Page 103 ETD2332 AutoCAD Customization Syllabus With the APPLOAD command, browse to T:\miller\ETD2332\LISP, find ANG.|sp and [Load}it. Mose/Untend ave eee ald The new command that the 3 ANG.Isp routine makes is Leck: [ES OS Toerviva alals| named ZZ. Type ZZ./ at the Command: prompt, pick four RolatoToxt. sp points, and observe the results. Flenene: faiSTen lene ype: [AutoCAD Aope Canclap abr dbers6c"L=] Loaded Appatens | Histo ft F pattoHcn, ie [Pk al i: seadnni — C\Doraners eiSetegrFitieA, | — tee | sca Q005L.. C\Progam Fle aust vw | Stata Sole) ‘send2do.. CPs uD ‘Actin After picking the four points, ZZ will “flip” the screen to the AutoCAD text screen and show the five results. eis oe a Seer ri ‘The units of the angles in the output is ae controlled by the angle settings of the : UNITS command. Change the UNITS orrit command angle settings and retry ZZ. ase Poets Pres eeeCM Tact Offset one of the lines and use the four points on the two parallel lines, (CAlgs\CustomizationPJC\sylabus\SylabusAutoCADCustom doc 05/08/08 Page 104

You might also like