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

  1. This is Lesson 3 of a series of AutoLISP training exercises given
  2. on the CompuServe ADESK Forum by the Autodesk, Inc. Training
  3. Department.
  4.  
  5. DATA TYPES
  6.  
  7. There are two kinds of objects in AutoLISP.  Atoms are simple
  8. objects; lists are complex objects bounded by parentheses.  Among
  9. simple objects, there are many sub-categories known as data types.
  10.  
  11. A clear understanding of data types in AutoLISP is important.
  12. Along with their parent classes of objects, atoms and lists,
  13. they form the essential building blocks for all AutoLISP
  14. programs.
  15.  
  16. Data types generally describe the nature of the data that the
  17. atom is to hold, and the amount of space (or memory) AutoLISP
  18. must allocate to create the atom and store its value.  This
  19. lesson will concentrate on the former topic; memory management
  20. will be discussed in a future lesson(s).
  21.  
  22. This is the list of data types supported by Release 10.  All
  23. except one are sub-categories of atoms; the exception is the data
  24. type "list".  Lists themselves qualify as a distinct data type.
  25.  
  26.     * list
  27.     * symbols
  28.     * strings
  29.     * real numbers
  30.     * integers
  31.     * file descriptors
  32.     * AutoCAD entity "names"
  33.     * AutoCAD selection-sets
  34.     * subrs (built-in functions)
  35.  
  36. DETERMINING DATA TYPES
  37.  
  38. The AutoLISP function (type) may be used to determine the data
  39. type of any object.  (type) takes one required argument, and
  40. returns a symbol that indicates the data type of its argument.
  41.  
  42. For example, this form (a "form" being any list that can be
  43. evaluated) determines the data type of the integer 1.
  44.  
  45.     Command: (type 1)
  46.     INT
  47.  
  48. EXAMPLES OF DATA TYPES
  49.  
  50. What follows is example AutoLISP code that binds variable X to a
  51. variety of objects with different data types and determines the
  52. data type of its binding with the (type) function.  You may want
  53. to type in the expressions at the command prompt in AutoCAD.
  54.  
  55. In cases where a hexadecimal number appears as a part of the data
  56. type, no effort has been made to accurately represent the number,
  57. as it varies from time to time.  Instead, the number is
  58. represented in the examples simply as "xxxx".
  59.  
  60. LIST
  61.  
  62.     Command: (setq x (quote (1 2 3)))
  63.     (1 2 3)
  64.  
  65.     Command: !x
  66.     (1 2 3)
  67.  
  68.     Command: (type x)
  69.     LIST
  70.  
  71. SYMBOLS
  72.  
  73.     Command: (setq x 'A)
  74.     A
  75.  
  76.     Command: !x
  77.     A
  78.  
  79.     Command: (type a)
  80.     SYM
  81.  
  82. STRINGS
  83.  
  84. Text strings can be of any length, up to the maximum amount of
  85. memory that can be allocated to them.  This value varies
  86. depending on the amount of AutoLISP code and data currently in
  87. use, and the type of AutoLISP in use (Regular or Extended).
  88.  
  89. String constants are limited to a maximum length of 100 characters.
  90.  
  91.     Command: (setq x "Text string")
  92.     "Text string"
  93.  
  94.     Command: !x
  95.     "Text string"
  96.  
  97.     Command: (type x)
  98.     STR
  99.  
  100. REAL NUMBERS
  101.  
  102. AutoLISP represents real numbers as double precision floating
  103. point numbers, accurate to at least 14 significant digits of
  104. precision.  There is no data type to represent single precision
  105. floating point numbers in AutoLISP; all reals are double
  106. precision, and are represented in AutoLISP in the same manner
  107. that they are represented in AutoCAD.
  108.  
  109. DISPLAY ACCURACY AND INTERNAL ACCURACY
  110.  
  111. The display accuracy of real numbers in AutoLISP is only that;
  112. the display accuracy.  The internal representation is always
  113. accurate to at least 14 digits of precision.  If it is necessary
  114. to display or to print the value of a real number to some
  115. arbitrary precision beyond AutoLISP's default display accuracy,
  116. the real number is typically converted to a string with the
  117. AutoLISP function (rtos) and displayed or printed at the desired
  118. display accuracy.  (rtos) will be discussed in a future lesson on
  119. data type conversion.
  120.  
  121.     Command: (setq x 4.5)
  122.     4.5
  123.  
  124.     Command: !x
  125.     4.5
  126.  
  127.     Command: (type x)
  128.     REAL
  129.  
  130. INTEGERS
  131.  
  132. In DOS AutoCAD, integers are 16-bit signed numbers between -32768
  133. and +32767.  In all other versions of AutoCAD, they are 32-bit
  134. signed numbers between -2,147,483,648 and +2,147,483,647.
  135. Integers transferred between AutoLISP and AutoCAD are restricted
  136. to 16-bit values.  For compatibility reasons across versions of
  137. AutoCAD, it is generally best to use 16-bit values only in
  138. AutoLISP programs.
  139.  
  140.     Command: (setq x 1)
  141.     1
  142.  
  143.     Command: !x
  144.     1
  145.  
  146.     Command: (type x)
  147.     INT
  148.  
  149. FILE DESCRIPTORS
  150.  
  151. A file descriptor is an internal pointer to a file that has been
  152. opened for a read/write/append operation.  Functions desiring to
  153. act on an open file must use its file descriptor to access the
  154. file; therefore, when a file is opened, a variable must be bound
  155. to the file descriptor or the file cannot be accessed again.
  156.  
  157. In this example, the (open) function is use to open a file named
  158. "file.ext" in the default directory and disk drive for a write
  159. operation.  The (open) function returns the file descriptor for
  160. "file.ext".
  161.  
  162.     Command: (setq x (open "file.ext" "w"))
  163.     <File: xxxx>
  164.  
  165.     Command: !x
  166.     <File: xxxx>
  167.  
  168.     Command: (type x)
  169.     FILE
  170.  
  171.     Command: (setq x (close x))
  172.     nil
  173.  
  174. AUTOCAD ENTITY "NAMES"
  175.  
  176. An entity name is a pointer to an entity's record in the current
  177. drawing editor session.  Entity names change from session to
  178. session, and the same name is valid only in the editing session
  179. where it was retrieved (but it will be valid for that entire
  180. session).
  181.  
  182. There is no way to store an entity name within an AutoCAD drawing
  183. or an external file for subsequent use in a different drawing
  184. editor session; this facility has been deliberately disallowed.
  185.  
  186. Entity names allow AutoLISP programs to act on unique, individual
  187. entities.  Methods to accomplish this will be explored in future
  188. lessons; a brief example is illustrated here.
  189.  
  190. The (entlast) function used in this example returns the entity
  191. name of the last entity added to the drawing.
  192.  
  193.     Command: LINE
  194.     From point: 1,1
  195.     To point: 5,5
  196.     To point: return
  197.  
  198.     Command: (setq x (entlast))
  199.     <Entity name: xxxx>
  200.     
  201.     Command: !x
  202.     <Entity name: xxxx>
  203.  
  204.     Command: (type x)
  205.     ENAME
  206.  
  207.     Command: ERASE
  208.     Select objects: !x
  209.     Select objects: return
  210.  
  211. AUTOCAD SELECTION-SETS
  212.  
  213. A selection-set is a collection of entity names, stored in a
  214. temporary file.  No more than six selection-sets may be opened by
  215. AutoLISP at one time.  To close an unneeded selection-set
  216. variable, bind the variable to nil.
  217.  
  218. The (ssget) function used in this example prompts the user to
  219. create a selection-set and returns the selection-set as the value
  220. of the expression.
  221.  
  222.     Command: LINE
  223.     From point: 1,1
  224.     To point: 5,5
  225.     To point: 10,1
  226.     To point: return
  227.  
  228.     Command: (setq x (ssget))
  229.     Select objects: pick first line
  230.     Select objects: pick second line
  231.     Select objects: return
  232.     <Selection set: xxxx>
  233.  
  234.     Command: !x
  235.     <Selection set: xxxx>
  236.  
  237.     Command: (type x)
  238.     PICKSET
  239.  
  240.     Command: ERASE
  241.     Select objects: !x
  242.     Select objects: return
  243.  
  244. SUBRS
  245.  
  246. A subr is a built-in AutoLISP function.  See the Table of
  247. Contents for Chapters 4 and 5 of the AutoLISP Programmer's
  248. Reference for a list of most of the subrs in AutoLISP.
  249.  
  250.     Command: !setq
  251.     <Subr: xxxx>
  252.  
  253.     Command: (type setq)
  254.     SUBR
  255.  
  256. QUESTIONS
  257.  
  258. 1.  What is the internal accuracy of real numbers in AutoLISP?
  259.  
  260. 2.  Does AutoLISP have a data type for a single precision
  261. floating point number?
  262.  
  263. 3.  Why should a program use only 16-bit integers rather than
  264. 32-bit integers in non-DOS versions of AutoCAD?
  265.  
  266. 4.  What happens when a file is opened but its file descriptor
  267. isn't assigned to a variable?
  268.  
  269. 5.  How are selection-sets stored in AutoLISP?
  270.  
  271. 6.  How many selection-sets can be opened at one time?
  272.  
  273. 7.  What is an entity name?
  274.  
  275. 8.  How can the same entity name be stored and used in different
  276. drawing editor sessions?
  277.  
  278. 9.  What are the maximum lengths for a string constant and a
  279. string variable?
  280.  
  281. 10. What function can be used to determine the data type of any
  282. object or variable?
  283.  
  284. Next week: AutoLISP and the AutoCAD Calculator
  285.  
  286. Answers to Lesson 2 Exercises:
  287.  
  288. 1.  legal
  289. 2.  legal
  290. 3.  face value
  291. 4.  legal
  292. 5.  legal
  293. 6.  face value
  294.  
  295. 1.  (+ 3 10 5)
  296. 2.  (* 20 15)
  297. 3.  (- 16 10)
  298. 4.  (/ 15 3)
  299. 5.  (+ 5 (* 10 2))
  300. 6.  (* (+ 5 10) 2)
  301.  
  302. 1.  (setq x 1)
  303.     !x
  304.  
  305. 2.  (setq x 4.5)
  306.     !x
  307.  
  308. 3.  (setq x "text")
  309.     !x
  310.  
  311. 4.  (setq x (quote (1.0 2.0 3.0)))
  312.     !x
  313.  
  314.