AutoLISP to Visual LISP: Design Solutions for AutoCAD
Instructor’s Guide
Chapter One
Definitions
Syntax - The way commands are arranged inside a computer program.
Source Code - Is an ASCII text file containing a formal programming language. The source code file must be compiled, interpreted or assembled before a computer can use it.
Flowchart - incorporates the use of graphic symbols to describe a sequence of operations or events. They are used to describe everything from the process involved in the treating of wastewater to the sequence of operations for a microprocessor.
Pseudo-Code - A more natural way of preparing the source code for a computer program is with pseudo-code. In this method, standard English terms are used to describe what the program is doing. The descriptions are arranged in the logical order that they appear.
GUI - A Graphical User Interface allows the user to interact with a computer by selecting icons and pictures that represent programs, commands, data files, and even hardware.
Programming Language - The actual commands used to construct a computer program.
Machine Language – A program that is coded so that a computer can directly use the instruction contain within the program without any further translation.
Hardware - is defined as the physical attributes of a computer, for example, the monitor, keyboard, central processing unit.
Software - is defined as the programs or electronic instructions that are necessary to operate a computer.
Binary - A number system that consist of two numbers one and zero. It is used extensively with computers because of its ease of implementation using digital electronics.
Answers to Review Questions
2. This process allows the programmer to gather the necessary information used to construct the application. It also allows the program to start laying out the actual program before the source code is created. Thus allowing the programmer to ensure the quality and accuracy a program.
3.
State the problemList unknown variables
List what is given
Create diagrams
List all formulas
List assumptions
Perform all necessary calculations
Check answer
4. Flowcharts use graphic symbols to represent a sequence or operation in a computer program, where as pseudo code uses standard English terms to describe what the program is doing.
Flow Chart
Psuedo-Code
Start Program
Prompt user for Information
Perform Calculations
Check Answers
Display Answers
End Program
5. AutoLISP programs can be loaded into memory using one of two methods (the AutoLISP LOAD function or the AutoCAD APPLOAD command). The AutoLISP load function is a command line function that can be entered from the AutoCAD command prompt or placed inside of an AutoLISP application, where as the APPLOAD command is dialog based program that is executed from the AutoCAD command prompt.
6. The compiler converts the source code from the language in which a program is created into a format that the computer can understand.
7. (Function1 Argument1)
8. The Rich Text Format contains special commands used to describe important formatting information (fonts and margins).
9. GUI and NonGUI. GUI systems include windows 95, 98 NT and 2000, just to name a few, where as nonGUI system include DOS, and OS400.
10. The operating system is a set of program that is designed to controls the computers components.
11. The operating system is a set of programs that manages stored information, loads and unloads programs (to and from the computer’s memory), reports the results of operations requested by the user, and manages the sequence of actions taken between the computer’s hardware and software.
12. A comment is the description placed in a program for the sole purpose of aiding the programmer in keeping track of what a program is doing.
Chapter Two
Answers to Review Questions
1. In AutoLISP an integer is any whole number that ranges from –2,147,483,648 to +2,147,483,647, where as a real number is any any number including zero that is positive or negative, rational (a number that can be expressed as an integer or a ratio of two integers, ½, 2, –5) or irrational (square root of 2).
2. The GETREAL function returns a true real number where as the GETSTRING will return the value converted to a string.
3. When an object is created in AutoCAD, it is given a handle that is used when referencing information concerning that object. The handle that is assigned to an entity remains constant throughout the drawing's life cycle. However an entity name is applicable only to a particular object in the current drawing session
4. Local Variables are only available to the function in which they are defined, where as global variable are available to the entire program.
5.
(DEFUN program (/ pt1) ;Pt1 is declared as a local ;variable.
(function argument) ;Expression.
(function argument) ;Expression.
)
6. File descriptors, just like entity names, are alphanumeric in nature and are only retained in the current drawing session or as long as the external file is open for input/output. Any time that an attempt is made to write, append, or read to a file, the file descriptor must be identified in that expression.
7. The AutoLISP OPEN function.
8.
*PRIN1 / Prints an expression to the command line or writes an expression to an open file*PRINC / Prints an expression to the command line or writes an expression to an open file
*PRINT / Prints an expression to the command line or writes an expression to an open file
*WRITE-LINE / Writes a string to the screen or to an open file
Note: All definitions starting with a * were taken from the AutoLISP Programmers Reference.
9.
Given the following equations write an AutoLISP expression for each.
X2 + 2X – 4
(defun c:X2Function ()
(setq NumberOne (getreal "\nEnter First Number : ")
)
(Princ (- (+ (expt NumberOne 2)
(* 2 NumberOne)
)
4
)
)
(princ)
)
X4 + 4X3 + 7X2 + 1
(defun c:X4Function ()
(setq NumberOne (getreal "\nEnter First Number : ")
)
(Princ (+ (expt NumberOne 4)
(expt (* 4 NumberOne) 3)
(expt (* 7 NumberOne) 2)
1
)
)
(princ)
)
R1 + R2 / (R1)(R2)
(defun c:RFunction ()
(setq R1 (getreal "\nEnter R1 : ")
R2 (getreal "\nEnter R2 : ")
)
(Princ (/ (+ R1 R2) (* R1 R2)))
(princ)
)
(2 + 5) * ( 2 * ( 4 – 5) / 6)
(* (* (/ (- 4 5)) 2) (+ 2 5))
L / Lo
(defun c:DeltaFunction()
(setq NumberOne (getreal "\nEnter First Number : ")
NumberTwo (getreal "\nEnter Second Number : ")
)
(Princ (/ (- NumberOne NumberTwo) NumberOne))
(princ)
)
Chapter Three
Definitions
Element – is an individual entity contained within a list. For example the list (Red Yellow Green) contains the elements: Red, Yellow and Green.
Truncate – To shorten a text string by removing a portion of that string. For example the text string “This is an example” is a truncated sub string of the text string “This is an example of a truncated string”.
Atom
Nested Function – A nested function is a function that is contained within another function. The nesting of functions is most often attributed with If statements and loops.
Answers to Review Questions
2. A list is capable of containing a varying amount of information. The use of list also reduces the amount of variables required in a program. They provide an efficient as well as a practical way of storing information over the traditional methods of using variables.
3. LIST (LIST 3.14 2.68 9.99 Text)
4.) Two or more list maybe combined into a single list by using either the AutoLISP function LIST or APPEND. The LIST function when supplied with multiple list as arguments, returns a single list in which its elements are the individual lists that were originally supplied to the LIST function. For example
(LIST
(3.14 5.98 9.99)
(TEST1 TEST2)
(9.11 RED MONDAY)
)
Returns
((3.14 5.98 9.99)(TEXT1 TEXT2)(9.11 RED MONDAY))
The APPEND function on the other hand merges the elements of multiple list into a single list in which its elements are the elements of the supplied lists. For example
(APPEND
(3.14 5.98 9.99)
(TEST1 TEST2)
(9.11 RED MONDAY)
)
Returns
(3.14 5.98 9.99 TEXT1 TEXT2 9.11 RED MONDAY)
- CAR – Returns the first element of a list
CDR – Returns a list starting with the second element
CADR – Returns the second element of a list
CADDR – Returns the third element of a list
(CAR (3.14 5.98 9.99 TEXT1 TEXT2 9.11 RED MONDAY)
)
Returns
3.14
(CDR (3.14 5.98 9.99 TEXT1 TEXT2 9.11 RED MONDAY)
)
Returns
(5.98 9.99 TEXT1 TEXT2 9.11 RED MONDAY)
(CADR (3.14 5.98 9.99 TEXT1 TEXT2 9.11 RED MONDAY)
)
Returns
5.98
(CADDR (3.14 5.98 9.99 TEXT1 TEXT2 9.11 RED MONDAY)
)
Returns
9.99
6. By employing the AutoLISP STRCAT function
7. False, before numeric data can be combined with a string, it must first be converted from its native data format into a string data type. For real numbers this is accomplished by using the RTOS function. For integers this is accomplished by using the ITOA function.
8.) False, in AutoLISP a string can be truncated using the SUBSTR function.
9. In AutoLISP decisions can be made by using either the IF function or the COND function. The COND function differs from the IF function in the respect that the program is allowed to use multiple test expressions, with each having its own THEN ELSE arguments.
10. The AutoLISP PROGN function allows a program to evaluate multiple expressions sequentially and return the result of the last expression evaluated.
11. (SETQ ReturnString (STRCAT “This is an example “ “of an AutoLISP “ “String”)
)
12. (SETQ ReturnString (STRCAT “The Amount of heat loss is “
(RTOS 456.87)
“ btu/’hr”
)
)
13. (SETQ ReturnString (STRCAT “The square root of “
(ITOA 2)
“ is “
(RTOS 1.4142)
)
)
14. (SETQ ReturnString (STRCAT “The program has completed ”
(ITOA 57)
“ cycles”
)
)
15. (DEFUN c:ChapterThree15Answer ()
(SETQ NumberOne (GETREAL “\nEnter NumberOne : “)
NumberTwo (GETREAL “\nEnter NumberTow : “)
)
(PRINC (STRCAT
“\nThe product of the two numbers “
(RTOS NumberOne)
“ and “
(RTOS NumberTwo)
“ is “
(RTOS (* NumberOne NumberTwo))
)
)
(PRINC)
)
Note: the underscores in the following Answers represent spaces
16. Thi
17. 57
18. of_he
19. anguage_t
20._resistor tow is 500.00 volts
21. (IF (< answer 10)
(SETQ variable 11)
)
22. (IF (AND (< answer 6)(> answer 5))
(PROGN
(SETQ VariableOne 7)
(SETQ VariableTwo 20)
)
)
23. (IF (AND (< answer 6)(> answer 2))
(PROGN
(SETQ VariableOne 15)
(SETQ VariableTwo 50)
)
(PROGN
(SETQ VariableOne 1)
(SETQ VariableTwo 2)
)
)
24. (IF (OR (< answer 6)(> answer 2))
(PROGN
(SETQ VariableOne 15)
(SETQ VariableTwo 50)
)
(PROGN
(SETQ VariableOne 1)
(SETQ VariableTwo 2)
)
)
25. ("this is an" "example list" "1" 3 4 5 T)
26. "this is an"
27. "example list"
28. ("example list" "1" 3 4 5 T)
29. "1"
30. (nth exampleList 4)
31. 4
32. ; error: bad argument type: consp nil
33. nil
34. T
35. "example list"
36. (setq two (list 'A 'B 'C 'D))
37. (append one two)
38. (list one two)
39. (append (list one two) three)
40. (append (append (list one two three) three)one)
41. (length (append (append (list one two three) three)one))
42. (CADDR (append (append (list one two three) three)one))
43. (list (append (append (list (list one two three) three (append one two) two three) three) two) three one)
Chapter Four
Definitions
Association List - is a list in which the elements that are contained within are linked by an index.
Dotted Pair – A special type of sub-list contained within an association list in which the two elements are separated by a period.
Extended Entity Data – Provides the programmer with a means of storing and managing information in an object’s association list using DXF codes. The attached information can be relevant or non-relevant to the particular entity.
Graphic Entity – is a visible object used to create a drawing (Lines, polylines, etc.).
Loop – is the continuous execution of a program or a portion of a program as long as a predefined test condition holds true.
Non-Graphic Entities is any object that is not visible to the AutoCAD user (Layer, Linetype, Dimension Styles, Text Styles, Etc.).
Answers to Review Questions
2. In an association list the elements are linked to an index, where as elements in a list are not.
3. In a dotted pair only one element is association with a unique index where as an association list can have multiple elements associated with an index.
4. When an entity is created using ENTMAKEX an owner is not assigned.
5. A loop can be created in an AutoLISP program by using either the WHILE or REPEATE functions.
6. The NENTSEL function allows a program to directly access a nested entity contained within a complex object.
7. CONS (CONS 8 “Example Layer Name”)
8. SUBST (SUBST NewListItem OldListItem EntityList)
9. ENTDEL
10. True
11. False, an AutoLISP association list can be modified by using the ENTMOD function.
12. The AutoLISP WHILE function will continue executing a sequence of functions as long as the predefined test conditions holds true, where as the REPEAT function will execute a sequence of functions a predetermined number of times.
13. False, In AutoLISP there is no limit to the number of loops that can be contained (nested) inside another or even one another.
14. False, AutoLISP allows for loops to be placed within either an IF expression or a COND expression.
15. False, The number of expressions that can be evaluated by a WHILE loop or REPEAT function is determined only by the placement of the closing parenthesis.
16. (3 . "The value is 4")
17. ; error: misplaced dot on input
18. ((4 . 0.65) (5 . 0.08))
19. ((-1 . <Entity name: 1583d58>) (0 . "LINE") (330 . <Entity name: 1583cf8>) (5 . "2B") (100 . "AcDbEntity") (67 . 0) (410 . "Model") (8 . "0") (100 . "AcDbLine") (10 8.17993 5.83581 0.0) (11 4.18605 3.58163 0.0) (210 0.0 0.0 1.0))