home *** CD-ROM | disk | FTP | other *** search
/ Current Shareware 1994 January / SHAR194.ISO / cad_util / alsps.zip / ALSP5.DOC < prev    next >
Lisp/Scheme  |  1993-11-04  |  14KB  |  427 lines

  1. This is Lesson 5 of a series of AutoLISP training exercises given
  2. on the CompuServe ADESK Forum by the Autodesk, Inc.  Training
  3. Department.
  4.  
  5. WRITING NEW AUTOCAD COMMANDS
  6.  
  7. SUBRS ARE ATOMS
  8.  
  9. AutoLISP has two kinds of objects: atoms and lists.  Every object
  10. in AutoLISP is one or the other.  The empty list nil () is by
  11. definition both an atom and a list, and the only object in
  12. AutoLISP that can be both.
  13.  
  14. Atoms come in many different data types.  One of these data types
  15. is the SUBR, or sub-routine.  Subrs are names for AutoLISP
  16. internal functions.  All of the AutoLISP subrs are listed in
  17. chapters 4 and 5 of the AutoLISP Programmer's Reference (APR).
  18.  
  19. FUNCTIONS ARE FIRST IN FORMS
  20.  
  21. When a complex expression is constructed with the intent of being
  22. evaluated, it consists of a list with a subr (or internal
  23. function) as its first element.  Such an expression is also known
  24. as a form.
  25.  
  26. This is an example of a form.
  27.  
  28.     (setq x 10)
  29.  
  30. What is the subr in this example?  What position does it occupy
  31. in the list?
  32.  
  33. This is an example of a list that is not a form, that is, a list
  34. that must be treated only as data and cannot be evaluated.
  35.  
  36.     (1.0 2.0 3.0)
  37.  
  38. What would happen if AutoLISP attempted to evaluate the latter
  39. expression?  Why?
  40.  
  41. This leads us to the important distinction that any list that can
  42. be evaluated (any form) must have a function as its first
  43. element.
  44.  
  45. USER DEFINED FUNCTIONS
  46.  
  47. In addition to its many subrs, AutoLISP allows the creation of
  48. user-defined functions, that is, functions whose instruction sets
  49. and names have been defined by the user.
  50.  
  51. Consider the evaluation process of a typical form that uses a
  52. subr.
  53.  
  54.     (setq x 10)
  55.  
  56. AutoLISP evaluates the first element, where it expects to find a
  57. function.  Here, it finds the subr SETQ, and retrieves the
  58. binding of SETQ.  The binding of SETQ (in fact, the binding of
  59. any subr) is effectively a set of instructions on how processing
  60. should proceed.  Next, AutoLISP evaluates the arguments to SETQ
  61. in order, and finally applies the binding of SETQ (processing
  62. instructions) to the values of the arguments.
  63.  
  64. The result in this case is a value of 10 for the expression, and
  65. a side-effect of variable X being bound to 10.
  66.  
  67. SETQ is a subr, and its instructions have been pre-defined in
  68. AutoLISP; the user cannot change them.  However, it's possible to
  69. create entirely new functions that perform whatever processing
  70. instructions the user desires.
  71.  
  72. New function bindings are created through use of the subr DEFUN,
  73. which stands for DEfine FUNction.
  74.  
  75. HOW TO USE DEFUN
  76.  
  77. The syntax of DEFUN appears slightly different than that of other
  78. functions you've used in this tutorial, but it's really the same.
  79.  
  80.     (defun <sym name> <arg list / local var> <expr> ...)
  81.  
  82. DEFUN takes two required arguments, and a variable number of
  83. arguments following the first two.
  84.  
  85. ARGUMENTS TO DEFUN
  86.  
  87. The first required argument to DEFUN must be the symbol name of
  88. the new function to be defined.  Never use the name of a built-in
  89. function or symbol as <sym name>, since this will make the
  90. built-in function inaccessible.  Use a new name that you have
  91. created, preferably one that describes the actions of the
  92. function.  Using function names of six or fewer characters will
  93. save memory space (more about that in future lessons).
  94.  
  95. In this example, the first argument to DEFUN is the symbol name
  96. MYFUN, which becomes the name of the user-defined function.
  97.  
  98.     (defun MYFUN <arg list / local var> ... )
  99.  
  100. The second required argument to DEFUN must be a list of required
  101. arguments and/or local variables.  This argument will be
  102. discussed in detail later in this lesson.  For now, it is
  103. sufficient to use an empty list, that is, a list with no required
  104. arguments or local variables.
  105.  
  106. The previous example is expanded to include the list of required
  107. arguments and/or local variables.
  108.  
  109.     (defun MYFUN () <expr> ... )
  110.  
  111. Arguments following the list of required arguments are
  112. expressions to be evaluated when the user-defined function is
  113. executed.  They are the processing instructions that will be
  114. executed when the function is called.
  115.  
  116. EXAMPLE OF SIMPLE FUNCTION DEFINITION
  117.  
  118. In the next few examples, optional expressions will be added to
  119. the body of MYFUN.  Different sets of expressions will give
  120. different meaning to MYFUN.
  121.  
  122. In this example, MYFUN's instructions tell it to ask the user for
  123. two points, and to print the distance between the two points.
  124. The AutoLISP subr DISTANCE is introduced.  DISTANCE takes two
  125. points as arguments, and calculates the 3D distance between the
  126. points.
  127.  
  128.     (defun MYFUN ()
  129.       (setq pt1 (getpoint "\nFrom point: "))
  130.       (setq pt2 (getpoint pt1 "\nTo point: "))
  131.       (distance pt1 pt2)
  132.     )
  133.  
  134. Type in the example.  AutoLISP will return the name of the
  135. function that's been defined; in this case, MYFUN.  Call MYFUN as
  136. you would any subr.
  137.  
  138.     Command: (MYFUN)
  139.     From point: <pick>
  140.     To point: <pick>
  141.     <returns distance as a real number>
  142.  
  143. VALUES RETURNED BY USER DEFINED FUNCTIONS
  144.  
  145. Any function created by DEFUN will return a value when it is
  146. called, just as any subr returns a value.  The value returned by
  147. a user-defined function will be the value of the last expression
  148. in the body of expressions that comprise the processing
  149. instructions.
  150.  
  151. For example, in the previous example the value of the expression
  152. (myfun) will be the value of the expression (distance pt1 pt2).
  153.  
  154.     (myfun) <----- form calling user-defined function
  155.  
  156.     (defun MYFUN ()
  157.       (setq pt1 (getpoint "\nFrom point: "))
  158.       (setq pt2 (getpoint pt1 "\nTo point: "))
  159.       (distance pt1 pt2)     <----- value of last expression
  160.     )                               becomes value of form
  161.  
  162. SAVING FUNCTION DEFINITIONS TO DISK
  163.  
  164. Although it's possible to create user-defined functions by typing
  165. in the appropriate code at the command prompt, it's probably a
  166. better idea to use a text editor of your choice (EDLIN will
  167. always do in a pinch) and add this text to a new file with an
  168. extension of .LSP; you choose the name.  Then, load the file into
  169. the drawing editor with the AutoLISP LOAD function.
  170.  
  171. The LOAD function takes a single string argument, which should be
  172. the name of the .LSP file to load.
  173.  
  174.     (load "<filename>")
  175.  
  176. Remember that the AutoCAD Load command is quite a different thing
  177. from the AutoLISP LOAD function.
  178.  
  179. For example, if you add the code from the previous example to a
  180. file named MYFUN.LSP then you can load the file into the drawing
  181. editor as follows.
  182.  
  183. Use a text editor to create a text file named MYFUN.LSP and
  184. include the code that creates this user-defined function.  Then
  185. load the file in to the AutoCAD drawing editor.
  186.  
  187.     Command: (load "MYFUN")
  188.     MYFUN
  189.  
  190. AutoLISP will search the current directory, along with the other
  191. directories in the AutoCAD Library Path, for the file MYFUN.LSP.
  192. If it finds the file, it will load the code into the editor.
  193.  
  194. If AutoLISP generates an error message when loading the file, you
  195. should edit the file and fix the problem.
  196.  
  197. For a description of the directories in the AutoCAD Library Path,
  198. see section 4.45, "(findfile <filename>)", of the APR.
  199.  
  200. CALLING AUTOCAD COMMANDS FROM AUTOLISP
  201.  
  202. A very popular application for user-defined functions is to
  203. create abbreviated names for AutoCAD commands, and to combine two
  204. or more commands (or a command and its options) into a single
  205. command.
  206.  
  207. The COMMAND function calls AutoCAD commands directly from
  208. AutoLISP.  COMMAND takes a string argument that must be the name
  209. of an AutoCAD command, and any optional arguments that should be
  210. passed to the command prompts.
  211.  
  212. For example, this form calls the Line command and draws a line
  213. from 1,1 to 5,5.  The form returns nil, because the COMMAND
  214. function always returns nil.  Generally, the side-effect of a
  215. COMMAND form is much more interesting than its value.
  216.  
  217.     Command: (command "LINE" "1,1" "5,5" "")
  218.     nil
  219.  
  220. The null string argument at the end of the argument list is
  221. treated as a carriage return by the Line command (or by any
  222. command).
  223.  
  224. In addition to string arguments for options to commands, COMMAND
  225. will accept reals or integers in the appropriate places, along
  226. with lists of two or three reals as points.  COMMAND also accepts
  227. variables with appropriate values.
  228.  
  229. This is another way to draw a line from 1,1 to 5,5.
  230.  
  231.     (command "LINE" (quote (1.0 1.0)) (quote (5.0 5.0)) "")
  232.  
  233. In this example, three forms are used to draw a circle with a
  234. center point chosen by the user and a radius of 1.5 units.  The
  235. first two forms are used to gather and store data, and the last
  236. to call the Circle command and use the previously-stored
  237. variables as arguments to the COMMAND function.
  238.  
  239.     Command: (setq pt1 (getpoint "\nCenter: "))
  240.     Center: <pick>
  241.  
  242.     Command: (setq rad (getdist pt1 "\nRadius: "))
  243.     Radius: <pick>
  244.  
  245.     Command: (command "CIRCLE" pt1 rad)
  246.     nil
  247.  
  248. DEFINING NEW COMMANDS
  249.  
  250. A simple and useful function might be one that combines the Zoom
  251. command with its Window option.
  252.  
  253. In this example, the special AutoLISP symbol pause is used as an
  254. argument to the COMMAND function.  pause halts the evaluation of
  255. the expression and allows the user to respond to the current
  256. command prompt.
  257.  
  258. Type this code into a new text file named ZW.LSP, then load the
  259. file into the drawing editor.
  260.  
  261.     (defun ZW ()
  262.       (command "ZOOM" "Window" pause pause)
  263.     )
  264.  
  265.     Command: (load "ZW")
  266.     ZW
  267.  
  268.     Command: (zw)
  269.     First corner: <pick>
  270.     Other corner: <pick>
  271.     nil
  272.  
  273. The usefulness of this function is somewhat diminished by the
  274. requirement to call it as a form, that is, to enclose the
  275. function name with parentheses and call it as an AutoLISP
  276. function.  This requirement means extra typing for the end user,
  277. and the need to remember which commands are enclosed by
  278. parentheses and which aren't.
  279.  
  280. AutoLISP allows the created of user-defined functions that can be
  281. called from the command prompt without passing them as forms,
  282. that is, without enclosing the function name in parentheses.
  283. Preface the name of the function with C:, as illustrated below.
  284.  
  285. Add this code to a new text file named ZW.LSP.  Load the file.
  286.  
  287.     (defun C:ZW ()
  288.       (command "ZOOM" "Window" pause pause)
  289.     )
  290.  
  291.     Command: (load "zw")
  292.     C:ZW
  293.  
  294.     Command: ZW
  295.  
  296.     or
  297.  
  298.     Command: (C:ZW)
  299.  
  300. This is an example of a new AutoCAD command named CD.  CD draws
  301. circles by center point and diameter.
  302.  
  303. Add this code to a new text file named CD.LSP.  Load the file.
  304.  
  305.     (defun C:CD ()
  306.       (command "CIRCLE" pause "Diameter" pause)
  307.     )
  308.  
  309.     Command: (load "cd")
  310.     C:CD
  311.  
  312.     Command: CD
  313.  
  314.     or
  315.  
  316.     Command: (C:CD)
  317.  
  318. SUPPRESSING THE VALUE RETURNED BY A USER-DEFINED COMMAND
  319.  
  320. The value of the last expression in the body of expressions
  321. following the list of required arguments and local variables in a
  322. function definition is always returned as the value of the
  323. function.  In many cases, it's nice to stop AutoLISP from
  324. printing this value on the display after the function has been
  325. executed.
  326.  
  327. The AutoLISP subr PRIN1 will print a null string to the display
  328. if PRIN1 is the last expression in a function definition.
  329.  
  330. For example, this modified version of the command CD does not
  331. print "nil" at the command prompt after it has executed.
  332.  
  333.     (defun C:CD ()
  334.       (command "CIRCLE" pause "Diameter" pause)
  335.       (prin1)
  336.     )
  337.  
  338. REQUIRED ARGUMENTS TO A FUNCTION
  339.  
  340. Some functions take no arguments; some require one or more, or a
  341. variable number of arguments.  The SETQ function, for example,
  342. requires at least two arguments.
  343.  
  344. Arguments may be required during a function call if they have
  345. been included in the function definition.  The values of the
  346. arguments will be mapped into the expressions following the list
  347. of required arguments wherever variables of the same names as
  348. those defined in the list of required arguments have been used.
  349.  
  350. Consider this function definition, which requires no arguments.
  351. It prompts the user for a real number, treats the number as a
  352. value in decimal degrees, and returns the value of the number
  353. converted into radians.
  354.  
  355.     (defun dtr ()
  356.       (setq degree (getreal "\nDegrees: "))
  357.       (/ (* degree pi) 180.0)
  358.     )
  359.  
  360. This function must be called with only one object in the form;
  361. the function itself.
  362.  
  363.     Command: (dtr)
  364.     Degrees: 180
  365.     3.14159
  366.  
  367. An equivalent expression may be created which requires the value
  368. in degrees as an argument in the form that contains the function.
  369.  
  370.     (defun dtr (degree)
  371.       (/ (* degree pi) 180.0)
  372.     )
  373.  
  374. In this case, the function must be called with an argument.
  375.  
  376.     Command: (dtr 180)
  377.     3.14159
  378.  
  379. The value of the argument 180 in the previous form becomes the
  380. value of the required argument DEGREE and AutoLISP subsequently
  381. uses the value 180 anywhere in the expressions following the list
  382. of required arguments where the variable DEGREE has been used.
  383.  
  384. Next week: Writing New AutoCAD Commands, Part Two
  385.  
  386. Questions:
  387.  
  388. 1.  What are subrs?
  389.  
  390. 2.  What is a form?
  391.  
  392. 3.  What is the difference between a form and a list (or complex
  393. expression) that must be treated strictly as data?
  394.  
  395. 4.  What kind of atom must always be the first element in any
  396. form?
  397.  
  398. 5.  What subr allows the user to create his own functions?
  399.  
  400. 6.  What are the two required arguments to the function in
  401. Question 5?
  402.  
  403. 7.  What are the optional arguments to the function in Question
  404. 5?
  405.  
  406. 8.  What value is returned by a user-defined function?
  407.  
  408. 9.  How can you use an external file that contains AutoLISP code
  409. in the drawing editor?
  410.  
  411. 10.  What special symbol allows the user to respond to prompts
  412. generated by AutoCAD commands called by the AutoLISP COMMAND
  413. function?
  414.  
  415. 11.  How can you eliminate the need for a user to call a function
  416. by enclosing it in parentheses?
  417.  
  418. 12.  What function will print a null string if included as the
  419. last expression in a function definition?
  420.  
  421. 13.  How can a user define a function so that it requires an
  422. argument?
  423.  
  424. 14.  Where are the values of required arguments used within the
  425. expressions that comprise the processing instructions of a
  426. user-defined function?
  427.