E:!!AutoCAD Modules!!!AutoLISP06Modules 4 …thecadguys.com/Sample Content/AutoLISP/AutoLISP Sample...

44
Competency Test No. 1 The CAD Guys Ltd. Copyright © 1993 - 2013 Module 6 AutoCAD ® Self-paced eCourse AutoLISP Module 6 Competency Test No.1 Learning Outcomes When you have completed this module, you will be able to: 1 Complete a written exam and write an AutoLISP program on the principles taught in Module 1 to Module 5. Competency Tests The AutoCAD Self-paced eCourses were written with competency based modules. What that means to you as a student is that you have not completed each module until you have mastered it. To master a module, you must prove that you understand the material contained in it and have completed the lab exercises with 100% accuracy. A competency test module contains multiple choice questions and a comprehensive lab exercise to test the users mastery of the last set of modules completed. There are no answers supplied with a competency test module since it is meant to be checked by your instructor. If there are any parts of this module that you have trouble completing, go back and reread the module or modules containing the information that you are having trouble with. If necessary, redo as many workalongs and lab exercises in those modules until you fully understand the material. Students Using this eBook in a Classroom Setting Students using this eBook in a classroom setting will be given instructions on what to do after they have completed this module. Students Using this eBook by Correspondence or Distance Education Complete the following steps: Step 1 Answer the 20 multiple choice questions on pages 6-2 to 6-5. Record the answers. Step 2 Complete Lab Exercise 6-1 on page 6-6. Step 3 Login into the website www.cadinstructors.com with your name and ID number. In the website, write the online written test for Module 6 - Competency Test No. 1 . It has the same 20 questions that you answered in Step 1. Step 4 Submit the file AutoLISP Lab 06-1.lsp when you write the test. Follow the prompts on the website. Step 5 If you have any problems completing Steps 1 to 4, ask your instructor for help. Step 6 Go on to Module 7.

Transcript of E:!!AutoCAD Modules!!!AutoLISP06Modules 4 …thecadguys.com/Sample Content/AutoLISP/AutoLISP Sample...

Page 1: E:!!AutoCAD Modules!!!AutoLISP06Modules 4 …thecadguys.com/Sample Content/AutoLISP/AutoLISP Sample Module… · AutoLISP Module 6 Competency Test No.1 ... To count to 16, it uses

Competency Test No. 1 The CAD Guys Ltd. Copyright © 1993 - 2013 Module 6

AutoCAD® Self-paced eCourse

AutoLISPModule 6

Competency Test No.1

Learning Outcomes

When you have completed this module, you will be able to:

1 Complete a written exam and write an AutoLISP program on the principles taught in Module 1to Module 5.

Competency Tests

The AutoCAD Self-paced eCourses were written with competency based modules. What thatmeans to you as a student is that you have not completed each module until you have masteredit. To master a module, you must prove that you understand the material contained in it and havecompleted the lab exercises with 100% accuracy.

A competency test module contains multiple choice questions and a comprehensive lab exercise totest the users mastery of the last set of modules completed. There are no answers supplied with acompetency test module since it is meant to be checked by your instructor. If there are any parts ofthis module that you have trouble completing, go back and reread the module or modulescontaining the information that you are having trouble with. If necessary, redo as many workalongsand lab exercises in those modules until you fully understand the material.

Students Using this eBook in a Classroom Setting

Students using this eBook in a classroom setting will be given instructions on what to do after theyhave completed this module.

Students Using this eBook by Correspondence or Distance Education

Complete the following steps:

Step 1 Answer the 20 multiple choice questions on pages 6-2 to 6-5. Record the answers.

Step 2 Complete Lab Exercise 6-1 on page 6-6.

Step 3 Login into the website www.cadinstructors.com with your name and ID number. In thewebsite, write the online written test for Module 6 - Competency Test No. 1. It has the same 20questions that you answered in Step 1.

Step 4 Submit the file AutoLISP Lab 06-1.lsp when you write the test. Follow the prompts on thewebsite.

Step 5 If you have any problems completing Steps 1 to 4, ask your instructor for help.

Step 6 Go on to Module 7.

Page 2: E:!!AutoCAD Modules!!!AutoLISP06Modules 4 …thecadguys.com/Sample Content/AutoLISP/AutoLISP Sample Module… · AutoLISP Module 6 Competency Test No.1 ... To count to 16, it uses

AutoCAD Self-paced eCourse - AutoLISP - Revised 2013-04-096 - 2

Competency Test No. 1 The CAD Guys Ltd. Copyright © 1993 - 2013 Module 6

AutoLISPCompetency Test No. 1

Open Book

Multiple Choice Questions

Select the BEST answer.

1 What is the name of the AutoLISP processor to which AutoLISP sends all of the expressions tobe evaluated?

A) LispB) EvaluatorC) ParenthesesD) CalculatorE) Heap

2 In what version of AutoCAD was AutoLISP added?

A) 1.0B) 2.10C) 2.18D) 9.0E) 10.0

3 AutoLISP is an implementation of the LISP programming language. What was the nameLISP derived from?

A) List ParenthesesB) Long ProgrammingC) Listing ProcessorD) Lisp is ProgramingE) List Processing

4 What is the answer to the following mathematical formula?

((17 + ((2 X 3) + 4)) + (((5 + (3 X 4)) - 11) + 15))

A) 12B) 32C) 48D) 17E) 90

5 What is the meaning of an initial space in an AutoLISP program to the AutoLISP evaluator?

A) Nothing.B) To go to the next line.C) To ignore the line.D) To stop processing.E) That the last item is complete.

Page 3: E:!!AutoCAD Modules!!!AutoLISP06Modules 4 …thecadguys.com/Sample Content/AutoLISP/AutoLISP Sample Module… · AutoLISP Module 6 Competency Test No.1 ... To count to 16, it uses

AutoCAD Self-paced eCourse - AutoLISP - Revised 2013-04-09 6 - 3

Competency Test No. 1 The CAD Guys Ltd. Copyright © 1993 - 2013 Module 6

6 Which one of the following is the definition of a local variable?

A) A variable that cannot be changed by the user.B) A variable that is saved when the program ends.C) A variable that stays in RAM memory only while the AutoLISP program, where the

variable is assigned its value or a program called by that program, is running. D) A variable that stays in RAM memory until the expression that created it ends.E) A variable that is constant.

7 Given the following program, which one of the following items is the program's argument?

(defun dtor (angd) (* pi (/ angd 180.0)) )

A) angdB) dtorC) defunD) piE) *

8 What would the following expression return?

Command: (setq num1 (- (* (+ 4 5) 3) 6))

A) 21B) 20C) 15D) 58E) 12

9 Given the following, which one is a real number to the AutoLISP evaluator?

A) 2B) -2000C) .5D) "2.5"E) 1.1

10 How many radians would an angle of 283 degrees equal?

A) 4.83992B) 4.26172C) 4.99393D) 4.93928E) 4.92922

Page 4: E:!!AutoCAD Modules!!!AutoLISP06Modules 4 …thecadguys.com/Sample Content/AutoLISP/AutoLISP Sample Module… · AutoLISP Module 6 Competency Test No.1 ... To count to 16, it uses

AutoCAD Self-paced eCourse - AutoLISP - Revised 2013-04-096 - 4

Competency Test No. 1 The CAD Guys Ltd. Copyright © 1993 - 2013 Module 6

11 Why is it important to have a line indicating the end of the program in each AutoLISP program?

A) To identify the end of the program to the users reading or editing the program.B) To tell AutoLISP where to end the program.C) It has no useful purpose.D) It makes the program look better.E) It tells AutoLISP how long the program is before it is run.

12 What does the AutoLISP evaluator do when it finds multiple spaces in an AutoLISP program?

A) It ignores the line they are on.B) Skips to the next line of the program.C) Ignores them and treats them as an single spaceD) It stops the program.E) It skips the next two lines in the program.

13 What does a semicolon in an AutoLISP program instruct the AutoLISP evaluator to do?

A) To ignore the remainder the line.B) To start the program at the next line.C) To ignore them and treat them as spaces.D) To stop the program.E) To skip the next two lines in the program.

14 Given the following, which one is an acceptable variable name in an AutoLISP program?

A) 1 angB) ang 1C) (ang1)D) angleE) ang1

15 What would the following expression return?

Command: (setq num2 (* pi (+ 5 3.3)))

A) 12.3435B) 14.9273C) 26.0752D) 34.7273E) nil

Page 5: E:!!AutoCAD Modules!!!AutoLISP06Modules 4 …thecadguys.com/Sample Content/AutoLISP/AutoLISP Sample Module… · AutoLISP Module 6 Competency Test No.1 ... To count to 16, it uses

AutoCAD Self-paced eCourse - AutoLISP - Revised 2013-04-09 6 - 5

Competency Test No. 1 The CAD Guys Ltd. Copyright © 1993 - 2013 Module 6

16 Why is it important to indent lines in an AutoLISP program?

A) The program will not run without it.B) It helps the programmer read, edit, and understand the program.C) It instructs the AutoLISP interpreter to ignore the line.D) It instructs the AutoLISP evaluator to return a result on the line.E) It instructs the AutoLISP interpreter to stop processing the program.

17 What is the largest integer that can be passed to AutoCAD from AutoLISP?

A) 2,000B) 32,767C) 16D) 1024E) 256

18 What would the following expression return?

Command: (princ "\nHello my name is \"John\"")(princ)

A) Hello my name is \"John\"B) Hello my name is JohnC) Hello my name is "John"D) Hello my name is

"John"E) nil

19 What would the following expression return?

Command: (setq ans1 3 ans2 4.5 ans3 "y")

A) nilB) 3 4.5 "y"C) "y"D) nothingE) T

20 What does an expression that has been entered in upper-case characters mean toAutoLISP?

A) It's the most important function.B) To ignore the function.C) To process the function first.D) Nothing.E) It's an AutoCAD command.

Page 6: E:!!AutoCAD Modules!!!AutoLISP06Modules 4 …thecadguys.com/Sample Content/AutoLISP/AutoLISP Sample Module… · AutoLISP Module 6 Competency Test No.1 ... To count to 16, it uses

AutoCAD Self-paced eCourse - AutoLISP - Revised 2013-04-096 - 6

Competency Test No. 1 The CAD Guys Ltd. Copyright © 1993 - 2013 Module 6

Lab Exercise 6-1

Program Name Template

AutoLISP Lab 06-1.lsp AutoLISP English

Purpose: To write a program, using arguments, that calculates the number of square inchesremaining when the largest circle is placed inside a square and prints the answers. The size of thesquare is entered by the user.

Step 1 Create a program naming it AutoLISP Lab 06-1.lsp

Step 2 Write a function, named AREAREM, that runs as an AutoCAD command.

Step 3 Declare all variables as local variables.

Step 4 Prompt the user to enter the length of one side of the square. Save it as a real number.

Step 5 Calculate the area of the circle and the square. (Figure Step 5)

Step 6 Calculate the area remaining when the area of the circle is subtracted from the area ofthe square.

Step 7 The program must include the two additional functions, cofs and cofc, that runs witharguments sent to them.

Step 8 The function cofs calculates the circumference of a square.

Step 9 The function cofc calculates the circumference of a circle.

Figure Step 5

Page 7: E:!!AutoCAD Modules!!!AutoLISP06Modules 4 …thecadguys.com/Sample Content/AutoLISP/AutoLISP Sample Module… · AutoLISP Module 6 Competency Test No.1 ... To count to 16, it uses

AutoCAD Self-paced eCourse - AutoLISP - Revised 2013-04-09 6 - 7

Competency Test No. 1 The CAD Guys Ltd. Copyright © 1993 - 2013 Module 6

Step 10 Open the Text window.

Step 11 Print the answers as shown in the figure.

Step 12 Prevent the program from printing a return when it ends.

Step 13 Load and run the program in AutoCAD. (Figure Step 13)

Author's Comments: This program does not draw anything in AutoCAD. It simply asks the userto enter the length of one side of a square, calculates the necessary answers and prints them asshow in Figure Step 13.

Figure Step 13

Page 8: E:!!AutoCAD Modules!!!AutoLISP06Modules 4 …thecadguys.com/Sample Content/AutoLISP/AutoLISP Sample Module… · AutoLISP Module 6 Competency Test No.1 ... To count to 16, it uses

Looping - Part 1 The CAD Guys Ltd. Copyright © 1993 - 2013 Module 12

AutoCAD ® Self-paced eCourse

AutoLISPModule 12

Looping - Part 1

Learning Outcomes

When you have completed this module, you will be able to:

1 Describe the ASCII code and the binary, decimal, hexadecimal and octal numbering systems.2 Describe and apply the =, /=, >, <, <= and >= functions.3 Describe and apply the while and cond functions.

ASCII Code

The American Standard Code of Information Interchange (ASCII code) was design to standardizecharacters on the keyboard. See Table 12-1 on pages 12-2 to 12-9. Study it, and pay specialattention to the ASCII column.

Binary Numbers

All computers work in binary numbers. The binary number system is made up exclusively of 0'sand 1's. Each binary digit is called a bit and 8 bits make a byte. There are 256 differentcombinations of 0's and 1's in one byte. When a key is pressed on the keyboard, it sends a onebyte binary number to the CPU. Counting in binary numbers is as follows:

0000 0000 0000 0001 0000 0010 0000 0011 0000 0100 ...

Decimal Numbers

The decimal numbering system is based on multiples of 10. It is the numbering system used ineveryone's day to day life. Counting in decimal numbers is as follows:

0 1 2 3 4 5 6 7 8 9 10 11 ....

Hexadecimal Numbers

The hexadecimal numbering system is based on multiplies of 16. To count to 16, it uses theletters A to F as a part of the numbering system. Counting in hexadecimal numbers is as follows:

0 1 2 3 4 5 6 7 8 9 A B C D E F 10 11 12 13 14 15 16 17 18 19 1A 1B ...

It takes one hexadecimal number for all the combinations in 4 bits, called a nibble. It takes 2hexadecimal numbers for all the combinations in one byte or 256 different combinations.

Octal Numbers

The octal numbering system is based on multiples of 8. The number 8 is important because it is2 cubed. Counting in octal numbers is as follows:

0 1 2 3 4 5 6 7 10 11 12 13 14 15 16 17 20 21 ...

Page 9: E:!!AutoCAD Modules!!!AutoLISP06Modules 4 …thecadguys.com/Sample Content/AutoLISP/AutoLISP Sample Module… · AutoLISP Module 6 Competency Test No.1 ... To count to 16, it uses

AutoCAD Self-paced eCourse - AutoLISP - Revised 2013-04-1012 - 2

Looping - Part 1 The CAD Guys Ltd. Copyright © 1993 - 2013 Module 12

Table 12-1 ASCII Code 1 of 8

Binary Decimal Hexadecimal Octal ASCII Control Teletype

0000 0000 0 0 0 ^@ NUL null

0000 0001 1 1 1 ( ^A SOH Start of Header

0000 0010 2 2 2 ) ^B STX Start of Transmission

0000 0011 3 3 3 Ì ^C EXT End of Transmission

0000 0100 4 4 4 Ë ^D EOT End of Text

0000 0101 5 5 5 Ê ^E ENQ Enquiry

0000 0110 6 6 6 Í ^F ACK Acknowledge

0000 0111 7 7 7 ! ^G BEL bell

0000 1000 8 8 10 3 ^H BS backspace

0000 1001 9 9 11 " ^I HT horizontal tab

0000 1010 10 A 12 4 ^J LF line feed

0000 1011 11 B 13 % ^K VT vertical tab

0000 1100 12 C 14 & ^L FF form feed

0000 1101 13 D 15 * ^M CR carriage return

0000 1110 14 E 16 + ^N SO shift out

0000 1111 15 F 17 ' ^O SI shift in

0001 0000 16 10 20 < ^P DLE

0001 0001 17 11 21 = ^Q DC1 device control #1

0001 0010 18 12 22 ù ^R DC2 device control #2

0001 0011 19 13 23 . ^S DC3 device control #3

0001 0100 20 14 24 ¶ ^T DC4 device control #4

0001 0101 21 15 25 § ^U NAK negative acknowledge

0001 0110 22 16 26 ( ^V SYN

0001 0111 23 17 27 0 ^W ETB

0001 1000 24 18 30 8 ^X CAN cancel

0001 1001 25 19 31 9 ^Y EM

0001 1010 26 1A 32 6 ^Z EOF end of file

0001 1011 27 1B 33 7 ^[ ESC escape

0001 1100 28 1C 34 2 ^\ FS field separator

0001 1101 29 1D 35 : ^] GS

0001 1110 30 1 36 > ^^ RS record separator

0001 1111 31 1F 37 ? ^- US underscore

Page 10: E:!!AutoCAD Modules!!!AutoLISP06Modules 4 …thecadguys.com/Sample Content/AutoLISP/AutoLISP Sample Module… · AutoLISP Module 6 Competency Test No.1 ... To count to 16, it uses

AutoCAD Self-paced eCourse - AutoLISP - Revised 2013-04-10 12 - 3

Looping - Part 1 The CAD Guys Ltd. Copyright © 1993 - 2013 Module 12

Table 12-1 ASCII Code 2 of 8

Binary Decimal Hexadecimal Octal ASCII Notes

0010 0000 32 20 40 Space character

0010 0001 33 21 41 !

0010 0010 34 22 42 "

0010 0011 35 23 43 #

0010 0100 36 24 44 $

0010 0101 37 25 45 %

0010 0110 38 26 46 &

0010 0111 39 27 47 '

0010 1000 40 28 50 (

0010 1001 41 29 51 )

0010 1010 42 2A 52 *

0010 1011 43 2B 53 +

0010 1100 44 2C 54 ,

0010 1101 45 2D 55 -

0010 1110 46 2 56 .

0010 1111 47 2F 57 /

0011 0000 48 30 60 0

0011 0001 49 31 61 1

0011 0010 50 32 62 2

0011 0011 51 33 63 3

0011 0100 52 34 64 4

0011 0101 53 35 65 5

0011 0110 54 36 66 6

0011 0111 55 37 67 7

0011 1000 56 38 70 8

0011 1001 57 39 71 9

0011 1010 58 3A 72 :

0011 1011 59 3B 73 ;

0011 1100 60 3C 74 <

0011 1101 61 3D 75 =

0011 1110 62 3 76 >

0011 1111 63 3F 77 ?

Page 11: E:!!AutoCAD Modules!!!AutoLISP06Modules 4 …thecadguys.com/Sample Content/AutoLISP/AutoLISP Sample Module… · AutoLISP Module 6 Competency Test No.1 ... To count to 16, it uses

AutoCAD Self-paced eCourse - AutoLISP - Revised 2013-04-1012 - 4

Looping - Part 1 The CAD Guys Ltd. Copyright © 1993 - 2013 Module 12

Table 12-1 ASCII Code 3 of 8

Binary Decimal Hexadecimal Octal ASCII Notes

0100 0000 64 40 100 @

0100 0001 65 41 101 A

0100 0010 66 42 102 B

0100 0011 67 43 103 C

0100 0100 68 44 104 D

0100 0101 69 45 105 E

0100 0110 70 46 106 F

0100 0111 71 47 107 G

0100 1000 72 48 110 H

0100 1001 73 49 111 I

0100 1010 74 4A 112 J

0100 1011 75 4B 113 K

0100 1100 76 4C 114 L

0100 1101 77 4D 115 M

0100 1110 78 4 116 N

0100 1111 79 4F 117 O

0101 0000 80 50 120 P

0101 0001 81 51 121 Q

0101 0010 82 52 122 R

0101 0011 83 53 123 S

0101 0100 84 54 124 T

0101 0101 85 55 125 U

0101 0110 86 56 126 V

0101 0111 87 57 127 W

0101 1000 88 58 130 X

0101 1001 89 59 131 Y

0101 1010 90 5A 132 Z

0101 1011 91 5B 133 [

0101 1100 92 5C 134 \

0101 1101 93 5D 135 ]

0101 1110 94 5 136 ^

0101 1111 95 5F 137 _

Page 12: E:!!AutoCAD Modules!!!AutoLISP06Modules 4 …thecadguys.com/Sample Content/AutoLISP/AutoLISP Sample Module… · AutoLISP Module 6 Competency Test No.1 ... To count to 16, it uses

AutoCAD Self-paced eCourse - AutoLISP - Revised 2013-04-10 12 - 5

Looping - Part 1 The CAD Guys Ltd. Copyright © 1993 - 2013 Module 12

Table 12-1 ASCII Code 4 of 8

Binary Decimal Hexadecimal Octal ASCII Notes

0110 0000 96 60 140 `

0110 0001 97 61 141 a

0110 0010 98 62 142 b

0110 0011 99 63 143 c

0110 0100 100 64 144 d

0110 0101 101 65 145 e

0110 0110 102 66 146 f

0110 0111 103 67 147 g

0110 1000 104 68 150 h

0110 1001 105 69 151 i

0110 1010 106 6A 152 j

0110 1011 107 6B 153 k

0110 1100 108 6C 154 l

0110 1101 109 6D 155 m

0110 1110 110 6 156 n

0110 1111 111 6F 157 o

0111 0000 112 70 160 p

0111 0001 113 71 161 q

0111 0010 114 72 162 r

0111 0011 115 73 163 s

0111 0100 116 74 164 t

0111 0101 117 75 165 u

0111 0110 118 76 166 v

0111 0111 119 77 167 w

0111 1000 120 78 170 x

0111 1001 121 79 171 y

0111 1010 122 7A 172 z

0111 1011 123 7B 173

0111 1100 124 7C 174 |

0111 1101 125 7D 175

0111 1110 126 7 176 ~

0111 1111 127 7F 177 -

Page 13: E:!!AutoCAD Modules!!!AutoLISP06Modules 4 …thecadguys.com/Sample Content/AutoLISP/AutoLISP Sample Module… · AutoLISP Module 6 Competency Test No.1 ... To count to 16, it uses

AutoCAD Self-paced eCourse - AutoLISP - Revised 2013-04-1012 - 6

Looping - Part 1 The CAD Guys Ltd. Copyright © 1993 - 2013 Module 12

Table 12-1 ASCII Code 5 of 8

Binary Decimal Hexadecimal Octal ASCII Notes

1000 0000 128 80 200 Ç

1000 0001 129 81 201 ü

1000 0010 130 82 202 é

1000 0011 131 83 203 â

1000 0100 132 84 204 ä

1000 0101 133 85 205 à

1000 0110 134 86 206 å

1000 0111 135 87 207 ç

1000 1000 136 88 210 ê

1000 1001 137 89 211 ë

1000 1010 138 8A 212 è

1000 1011 139 8B 213 ï

1000 1100 140 8C 214 î

1000 1101 141 8D 215 ì

1000 1110 142 8 216 Ä

1000 1111 143 8F 217 Å

1001 0000 144 90 220 É

1001 0001 145 91 221 æ

1001 0010 146 92 222 Æ

1001 0011 147 93 223 ô

1001 0100 148 94 224 ö

1001 0101 149 95 225 ò

1001 0110 150 96 226 û

1001 0111 151 97 227 ù

1001 1000 152 98 230 ÿ

1001 1001 153 99 231 Ö

1001 1010 154 9A 232 Ü

1001 1011 155 9B 233 ¢

1001 1100 156 9C 234 £

1001 1101 157 9D 235 ¥

1001 1110 158 9 236 .

1001 1111 159 9F 237 ƒ

Page 14: E:!!AutoCAD Modules!!!AutoLISP06Modules 4 …thecadguys.com/Sample Content/AutoLISP/AutoLISP Sample Module… · AutoLISP Module 6 Competency Test No.1 ... To count to 16, it uses

AutoCAD Self-paced eCourse - AutoLISP - Revised 2013-04-10 12 - 7

Looping - Part 1 The CAD Guys Ltd. Copyright © 1993 - 2013 Module 12

Table 12-1 ASCII Code 6 of 8

Binary Decimal Hexadecimal Octal ASCII Notes

1010 0000 160 A0 240 á

1010 0001 161 A1 241 í

1010 0010 162 A2 242 ó

1010 0011 163 A3 243 ú

1010 0100 164 A4 244 ñ

1010 0101 165 A5 245 Ñ

1010 0110 166 A6 246 ª

1010 0111 167 A7 247 º

1010 1000 168 A8 250 ¿

1010 1001 169 A9 251 1

1010 1010 170 AA 252 ¬

1010 1011 171 AB 253 ½

1010 1100 172 AC 254 ¼

1010 1101 173 AD 255 ¡

1010 1110 174 AE 256 «

1010 1111 175 AF 257 »

1011 0000 176 B0 260 !

1011 0001 177 B1 261 "

1011 0010 178 B2 262 #

1011 0011 179 B3 263 *

1011 0100 180 B4 264 1

1011 0101 181 B5 265 I

1011 0110 182 B6 266 M

1011 0111 183 B7 267 D

1011 1000 184 B8 270 @

1011 1001 185 B9 271 <

1011 1010 186 BA 272 5

1011 1011 187 BB 273 7

1011 1100 188 BC 274 8

1011 1101 189 BD 275 E

1011 1110 190 BE 276 A

1011 1111 191 BF 277 ,

Page 15: E:!!AutoCAD Modules!!!AutoLISP06Modules 4 …thecadguys.com/Sample Content/AutoLISP/AutoLISP Sample Module… · AutoLISP Module 6 Competency Test No.1 ... To count to 16, it uses

AutoCAD Self-paced eCourse - AutoLISP - Revised 2013-04-1012 - 8

Looping - Part 1 The CAD Guys Ltd. Copyright © 1993 - 2013 Module 12

Table 12-1 ASCII Code 7 of 8

Binary Decimal Hexadecimal Octal ASCII Notes

1100 0000 192 C0 300 .

1100 0001 193 C1 301 2

1100 0010 194 C2 302 0

1100 0011 195 C3 303 /

1100 0100 196 C4 304 )

1100 0101 197 C5 305 3

1100 0110 198 C6 306 G

1100 0111 199 C7 307 K

1100 1000 200 C8 310 9

1100 1001 201 C9 311 6

1100 1010 202 CA 312 =

1100 1011 203 CB 313 ;

1100 1100 204 CC 314 :

1100 1101 205 CD 315 4

1100 1110 206 CE 316 >

1100 1111 207 CF 317 N

1101 0000 208 D0 320 J

1101 0001 209 D1 321 L

1101 0010 210 D2 322 H

1101 0011 211 D3 323 F

1101 0100 212 D4 324 B

1101 0101 213 D5 325 ?

1101 0110 214 D6 326 C

1101 0111 215 D7 327 O

1101 1000 216 D8 330 P

1101 1001 217 D9 331 -

1101 1010 218 DA 332 +

1101 1011 219 DB 333 $

1101 1100 220 DC 334 (

1101 1101 221 DD 335 %

1101 1110 222 DE 336 '

1101 1111 223 DF 337 &

Page 16: E:!!AutoCAD Modules!!!AutoLISP06Modules 4 …thecadguys.com/Sample Content/AutoLISP/AutoLISP Sample Module… · AutoLISP Module 6 Competency Test No.1 ... To count to 16, it uses

AutoCAD Self-paced eCourse - AutoLISP - Revised 2013-04-10 12 - 9

Looping - Part 1 The CAD Guys Ltd. Copyright © 1993 - 2013 Module 12

Table 12-1 ASCII Code 8 of 8

Binary Decimal Hexadecimal Octal ASCII Notes

1110 0000 224 E0 340 α

1110 0001 225 E1 341 ß

1110 0010 226 E2 342 Γ

1110 0011 227 E3 343 π

1110 0100 228 E4 344 Σ

1110 0101 229 E5 345 σ

1110 0110 230 E6 346 µ

1110 0111 231 E7 347 τ

1110 1000 232 E8 350 Φ

1110 1001 233 E9 351 Θ

1110 1010 234 EA 352 Ω

1110 1011 235 EB 353 δ

1110 1100 236 EC 354 4

1110 1101 237 ED 355 φ

1110 1110 238 EE 356 ε

1110 1111 239 EF 357 1

1111 0000 240 F0 360 /

1111 0001 241 F1 361 ±

1111 0010 242 F2 362 $

1111 0011 243 F3 363 #

1111 0100 244 F4 364 !

1111 0101 245 F5 365 "

1111 0110 246 F6 366 ÷

1111 0111 247 F7 367 .

1111 1000 248 F8 370 E

1111 1001 249 F9 371 @

1111 1010 250 FA 372 A

1111 1011 251 FB 373 %

1111 1100 252 FC 374 6

1111 1101 253 FD 375 ²

1111 1110 254 FE 376 #

1111 1111 255 FF 377

Page 17: E:!!AutoCAD Modules!!!AutoLISP06Modules 4 …thecadguys.com/Sample Content/AutoLISP/AutoLISP Sample Module… · AutoLISP Module 6 Competency Test No.1 ... To count to 16, it uses

AutoCAD Self-paced eCourse - AutoLISP - Revised 2013-04-1012 - 10

Looping - Part 1 The CAD Guys Ltd. Copyright © 1993 - 2013 Module 12

AutoLISP Function: =The ' = ' equals function is used to compare two or more numbers or strings testing to find if theyare numerically equal to one another.

When testing numbers, this function does not check for the same data type. It simply checks thenumerical value of the numbers. When testing strings, this function compares the numerical ASCIIcode value of the strings. For example, an upper-case A and a lowercase a are not equal.

Returns: T if the atoms are numerically equal, nil if they are not.

Format: (= atom atom ... )

Examples:

1 Command: (= 12 12.0)TCommand:

Author's Comments: The atoms 12 and 12.0 are numerically equal, even though they are notthe same data type, therefore it returns T.

2 Command: (= 55 75)nilCommand:

Author's Comments: The two integers are not equal, therefore it returns nil.

3 Command: (= 7.5 7.5 7.5)TCommand:

Author's Comments: This example show more than two numbers being entered. Since all thenumbers are equal, it returns T.

4 Command: (= "PIPE" "PIPE")TCommand:

Author's Comments: If two strings are being compared, both strings must be identical for it toreturn T.

5 Command: (= "Pipe" "PIPE")nilCommand:

Author's Comments: If the ASCII code of the two strings being compared are not numericallyequal, it returns nil.

6 Command: (setq num1 5.0 num2 5)5Command: (= num1 num2)T

Author's Comments: The variables num1 and num2 are both set to the numerical value 5.

Page 18: E:!!AutoCAD Modules!!!AutoLISP06Modules 4 …thecadguys.com/Sample Content/AutoLISP/AutoLISP Sample Module… · AutoLISP Module 6 Competency Test No.1 ... To count to 16, it uses

AutoCAD Self-paced eCourse - AutoLISP - Revised 2013-04-10 12 - 11

Looping - Part 1 The CAD Guys Ltd. Copyright © 1993 - 2013 Module 12

AutoLISP Function: /=The ' /= ' not equal function is used to compare two numbers or strings testing to find if they arenumerically not equal to one another.

When testing numbers, this function does not check for the same data type. It simply checks thenumerical value of the numbers. When testing strings, this function checks the numerical ASCIIcode value of the strings. An upper-case A and a lowercase a are not equal.

Returns: T if the atoms are numerically not equal, nil if they are.

Format: (/= atom atom )

Examples:

1 Command: (/= 12 12.0)nilCommand:

Author's Comments: The atoms 12 and 12.0 are numerically equal even though they are notthe same data type, therefore it returns nil.

2 Command: (/= 55 75)TCommand:

Author's Comments: The two integers are not equal, therefore, it returns T.

3 Command: (/= "Pipe" "PIPE")TCommand:

Author's Comments: The two strings do not have the same numerically ASCII code value,therefore, it returns T.

4 Command: (setq num1 5.0 num2 5)5Command: (/= num1 num2)nilCommand:

Author's Comments: The variables num1 and num2 are both set to the numerical value of 5,therefore, it returns nil.

When testing numbers, testing functions do not check for the same data type. They simply check the numerical value of the numbers.

When testing strings, testing functions check the numerical ASCII code value ofthe strings. An upper-case A and a lowercase a are not equal.

Page 19: E:!!AutoCAD Modules!!!AutoLISP06Modules 4 …thecadguys.com/Sample Content/AutoLISP/AutoLISP Sample Module… · AutoLISP Module 6 Competency Test No.1 ... To count to 16, it uses

AutoCAD Self-paced eCourse - AutoLISP - Revised 2013-04-1012 - 12

Looping - Part 1 The CAD Guys Ltd. Copyright © 1993 - 2013 Module 12

AutoLISP Function: >The ' > ' greater than function is used to compare two or more numbers or strings, testing to findif the atom on the left is numerically greater than the atom to its right.

If two atoms are given, this function tests to see if the first atom is greater than the second atom. If more than two atoms are given, it tests to see if each atom is greater than the atom to its right.

When testing numbers, this function does not check for the same data type. It simply checks thenumerical value of the numbers. When testing strings, this function checks the numerical ASCIIcode value of the strings. An upper-case A and a lowercase a are not equal.

Returns: T if the first atom is numerically greater than the second atom, nil if it is not. If morethan two atoms are given, it returns T if each atom is greater than the atom to its right, nil if it isnot.

Format: (> atom atom ... )

Examples:

1 Command: (> 12 12.0)nilCommand:

Author's Comments: The atoms 12 and 12.0 are numerically equal, even though they are notthe same data type. It returns nil since the number on the left is not greater than the number to itsright.

2 Command: (> 75 55)TCommand:

Author's Comments: The first integer 75 is greater than the second integer 55, therefore, itreturns T.

3 Command: (> 7.5 7.5 7.5)nilCommand:

Author's Comments: This example show more than two numbers being entered. Since all thenumbers are numerically equal, it returns nil.

4 Command: (> 2000 1000 500)TCommand:

Author's Comments: This example shows three integers being entered. It returns T since eachnumber is greater then the number to its right.

5 Command: (> "PIPE" "pipe")nilCommand:

Author's Comments: When comparing two string, the numerical ASCII code value is used.

Page 20: E:!!AutoCAD Modules!!!AutoLISP06Modules 4 …thecadguys.com/Sample Content/AutoLISP/AutoLISP Sample Module… · AutoLISP Module 6 Competency Test No.1 ... To count to 16, it uses

AutoCAD Self-paced eCourse - AutoLISP - Revised 2013-04-10 12 - 13

Looping - Part 1 The CAD Guys Ltd. Copyright © 1993 - 2013 Module 12

AutoLISP Function: <The ' < ' less than function is used to compare numbers or strings, testing to find if the atom onthe left is numerically less than the atom to its right.

If two atoms are given, this function tests to see if the first atom is less than the second atom. Ifmore than two atoms are given, it tests to see if each atom is less than the atom to its right.

When testing numbers, this function does not check for the same data type. It simply checks thenumerical value of the numbers. When testing strings, this function checks the numerical ASCIIcode value of the strings. An upper-case A and a lowercase a are not equal.

Returns: T if the first atom is numerically less than the second atom, nil if it is not. If more thantwo atoms are given, it returns T if each atom is less than the atom to its right, nil if it is not.

Format: (< atom atom ... )

Examples:

1 Command: (< 12 12.0)nilCommand:

Author's Comments: The atoms 12 and 12.0 are numerically equal, even though they are notthe same data type, therefore it returns nil.

2 Command: (< 75 55)nilCommand:

Author's Comments: The first integer is greater than the second integer, therefore, it returns nil.

3 Command: (< 3.5 3.7 7.5)TCommand:

Author's Comments: This example show more than two numbers being entered. Since all thenumbers are less than the number to its right, it returns T.

4 Command: (< 500 1000 3000 500)nilCommand:

Author's Comments: Since each number is not less then number to its right, it returns nil

5 Command: (< "PIPE" "pipe")TCommand:

Author's Comments: Since the numerical ASCII code value of the upper-case PIPE is less thanthe numerical ASCII code value for the lowercase pipe, it returns T.

Page 21: E:!!AutoCAD Modules!!!AutoLISP06Modules 4 …thecadguys.com/Sample Content/AutoLISP/AutoLISP Sample Module… · AutoLISP Module 6 Competency Test No.1 ... To count to 16, it uses

AutoCAD Self-paced eCourse - AutoLISP - Revised 2013-04-1012 - 14

Looping - Part 1 The CAD Guys Ltd. Copyright © 1993 - 2013 Module 12

AutoLISP Function: >=The ' >= ' greater than or equal to function is used to compare two or more numbers or strings,testing to find if the atom on the left is numerically greater than or equal to the atom on the right.

If two atoms are given, this function will test to see if the first atom is numerically greater than orequal to the atom to its left. If more than two atoms are given, it tests to see if each atom isgreater than or equal to the atom to its right.

When testing numbers, this function does not check for the same data type. It simply checks thenumerical value of the numbers. When testing strings, this function checks the ASCII code valueof the strings. An upper-case A and a lowercase a are not equal.

Returns: T if the first atom is numerically greater than or equal to the second atom and nil if it isnot. If more than two atoms are given, it returns T if each atom is greater than or equal to theatom to its right and nil if it is not.

Format: (>= atom atom ... )

Examples:

1 Command: (>= 12 12.0)TCommand:

Author's Comments: The atoms 12 and 12.0 are numerically equal, even though they are notthe same data type, therefore it returns T.

2 Command: (>= 75 55)TCommand:

Author's Comments: The 75 is greater than 55, therefore, it returns T.

3 Command: (>= 3.5 3.5 7.5)nilCommand:

Author's Comments: This example has more than two real numbers being entered. Since thesecond 3.5 number is not equal or greater than the 7.5 number on the left, it returns nil.

4 Command: (>= 2000 1000 3000 500)nilCommand:

Author's Comments: The expression returns nil since not all of the numbers are greater than orequal to the number to their right.

5 Command: (>= "PIPE" "pipe")TCommand:

Author's Comments: This example show two strings being compared. Since the ASCII value ofthe upper-case PIPE is less than the ASCII value for the lowercase pipe,, it returns nil.

Page 22: E:!!AutoCAD Modules!!!AutoLISP06Modules 4 …thecadguys.com/Sample Content/AutoLISP/AutoLISP Sample Module… · AutoLISP Module 6 Competency Test No.1 ... To count to 16, it uses

AutoCAD Self-paced eCourse - AutoLISP - Revised 2013-04-10 12 - 15

Looping - Part 1 The CAD Guys Ltd. Copyright © 1993 - 2013 Module 12

AutoLISP Function: <=The ' <= ' less than or equal to function is used to compare two or more numbers or strings,testing to find if the atom on the left is numerically less than or equal to the atom on the right.

If two atoms are given, this function will test to see if the first atom is numerically less than or equalto the atom to its right. If more than two atoms are given, it tests to see if each atom is less than orequal to the atom to its right.

When testing numbers, this function does not check for the same data type. It simply checks thenumerical value of the numbers. When testing strings, this function checks the numerical ASCIIcode value of the strings. An upper-case A and a lowercase a are not equal.

Returns: T if the first atom is numerically less than or equal to the second atom, nil if it is not. Ifmore than two atoms are given, it returns T if each atom is less than or equal to the atom to itsright, nil if it is not.

Format: (<= atom atom ... )

Examples:

1 Command: (<= 12 12.0)TCommand:

Author's Comments: The atoms 12 and 12.0 are numerically equal, even though they are notthe same data type, therefore it returns T.

2 Command: (<= 75 55)nilCommand:

Author's Comments: The first integer is greater than the integer on its right, therefore, it returnsnil.

3 Command: (<= 3.5 3.5 7.5)TCommand:

Author's Comments: This example show more than two numbers being entered. Since all thenumbers are less than or equal to the number to their right, it returns T.

4 Command: (<= 2000 1000 3000 500)nilCommand:

Author's Comments: With four integers entered in the expression, it returns nil since not all ofthe numbers are less than or equal to the number to their right.

5 Command: (<= "PIPE" "pipe")TCommand:

Author's Comments: Since the numerical ASCII code value of the upper-case PIPE is less thanthe numerical ASCII code value for the lowercase pipe, it returns T.

Page 23: E:!!AutoCAD Modules!!!AutoLISP06Modules 4 …thecadguys.com/Sample Content/AutoLISP/AutoLISP Sample Module… · AutoLISP Module 6 Competency Test No.1 ... To count to 16, it uses

AutoCAD Self-paced eCourse - AutoLISP - Revised 2013-04-1012 - 16

Looping - Part 1 The CAD Guys Ltd. Copyright © 1993 - 2013 Module 12

Author's Comments: All example AutoLISP programs in the AutoLISP eCourse are located inyour Examples folder. I suggest that you load and run these programs after you study the code. Ensure that you can read back each line of the code and understand what it means, in plainEnglish. After you do that, load the program and run it, if necessary, to help you understand howthe programs works.

AutoLISP Function: whileThe while as long as function is used to loop through and evaluate an expression or a series ofexpressions in a continuous loop until a specified condition is met.

Each while function must contain one test-expression, which is evaluated at the start of eachloop. If the test-expression evaluates nil, the while loop terminates. If the test-expressionevaluates to anything but nil, the while function evaluates the expression(s) it contains. It thenevaluates the test-expression again in a continuous loop. The loop is terminated when the test-expression evaluates nil.

A method for the while loop to terminate must be written into the code. If there is none, theprogram will go into a continuous loop and the user would have to stop it by pressing the Esc key.

Returns: The value of the last expression evaluated.

Format: (while (test-expression) (expression) ( ...)

)

Examples:

1

Figure Example 1A

Page 24: E:!!AutoCAD Modules!!!AutoLISP06Modules 4 …thecadguys.com/Sample Content/AutoLISP/AutoLISP Sample Module… · AutoLISP Module 6 Competency Test No.1 ... To count to 16, it uses

AutoCAD Self-paced eCourse - AutoLISP - Revised 2013-04-10 12 - 17

Looping - Part 1 The CAD Guys Ltd. Copyright © 1993 - 2013 Module 12

Author's Comments: This small program prints the numbers 7 to 16, each on a new line. Thewhile statement says " as long as " num1 is less than or equal to 16, print the number. Take notehow the num1 variable increases by one number on each loop of the function. The functionterminates when num1 equals 16.

2

Figure Example 1B

Figure Example 2A

Page 25: E:!!AutoCAD Modules!!!AutoLISP06Modules 4 …thecadguys.com/Sample Content/AutoLISP/AutoLISP Sample Module… · AutoLISP Module 6 Competency Test No.1 ... To count to 16, it uses

AutoCAD Self-paced eCourse - AutoLISP - Revised 2013-04-1012 - 18

Looping - Part 1 The CAD Guys Ltd. Copyright © 1993 - 2013 Module 12

Author's Comments: This program tests the numerically value of the strings entered by theuser. In a while loop, it will ask the user to reenter the string until the strings are identical.

Figure Example 2B

Using the While Function

Purpose: This program works similar to the MEASURE command. It insertspoints on a line that is drawn by the user. The distance between the points isspecified by the user. A point is inserted on the start coordinate of the line and

continues entering points, at the specified distance, until the distance between the last pointinserted and the endpoint of the line is less than the distance between points.

Step 1 Start a new drawing using the template AutoLISP English.

Step 2 Create a program naming it AutoLISP Work 12-1.lsp

Step 3 Name the function MEASURELINE1. Have it run like an AutoCAD command.

Step 4 Declare all variables as local variables.

Step 5 Save the current value of the system variables cmdecho, clayer, osmode andorthomode.

Step 6 Set the value of the system variables cmdecho, osmode and orthomode to 0. Set the pdmode to 34.

Step 7 Ask the user to enter the layer name and the layer color. Save both values as strings.

Step 8 Using the input from Step 7, create a new layer and set it as the current layer.

Step 9 Ask the user to enter the distance between points.

Step 10 Ask the user to specify the start and end points of the line.

Step 11 Draw a line between the start and end points specified in Step 10.

Step 12 Insert a point at the start point of the line.

Page 26: E:!!AutoCAD Modules!!!AutoLISP06Modules 4 …thecadguys.com/Sample Content/AutoLISP/AutoLISP Sample Module… · AutoLISP Module 6 Competency Test No.1 ... To count to 16, it uses

AutoCAD Self-paced eCourse - AutoLISP - Revised 2013-04-10 12 - 19

Looping - Part 1 The CAD Guys Ltd. Copyright © 1993 - 2013 Module 12

Step 13 In a while loop, insert points on the line using the distance between points. Continue toenter points until the distance left to the end point of the line is smaller than the distance betweenpoints.

Hint: Use polar functions to find the XY location of the points.

Step 14 Reset the original value of the system variables cmdecho, clayer, osmode andorthomode.

Hint: Do not reset the original pdmode system variable as this will change the point type on thedrawing.

Step 15 Load and run the program in AutoCAD. (Figure Step 15A and 15B)

Author's Comments: Do your best to complete the AutoLISP program on your own. If you getstuck and cannot complete it on your own, check my answer in page 12-28. If you complete theprogram on your own and it returns the correct results, it is correct even if it does not match myprogram exactly. There are many ways to write the same program.

Figure Step 15A

Figure Step 15B

Page 27: E:!!AutoCAD Modules!!!AutoLISP06Modules 4 …thecadguys.com/Sample Content/AutoLISP/AutoLISP Sample Module… · AutoLISP Module 6 Competency Test No.1 ... To count to 16, it uses

AutoCAD Self-paced eCourse - AutoLISP - Revised 2013-04-1012 - 20

Looping - Part 1 The CAD Guys Ltd. Copyright © 1993 - 2013 Module 12

AutoLISP Function: condThe cond if true - do this function is used to evaluate more than one test-expressions within oneexpression.

The cond function evaluates the first test-expression and if it returns a value other than nil, itevaluates the result-expressions(s) that follows it. If the first test-expression evaluates nil, it thenevaluates the next test-expression and continues to do this until it finds one test-expression thatevaluates something other than nil. It will only terminate when it finds one test-expression thatreturns a value other than nil and evaluates the expression(s) contained in that result area. Afterfinding one expression that returns a value, the cond function will terminate.

Each cond loop should include a true statement as the last test-expression in the function. See theexamples below.

Returns: The evaluation of the applicable result-expression. If the test-expression does not havea result, cond returns the value of the test-expression.

Format: (cond ((test-expression1)(result1)(...)) ((...)(...)))

Examples:

1

Figure Example 1A

Figure Example 1B

Page 28: E:!!AutoCAD Modules!!!AutoLISP06Modules 4 …thecadguys.com/Sample Content/AutoLISP/AutoLISP Sample Module… · AutoLISP Module 6 Competency Test No.1 ... To count to 16, it uses

AutoCAD Self-paced eCourse - AutoLISP - Revised 2013-04-10 12 - 21

Looping - Part 1 The CAD Guys Ltd. Copyright © 1993 - 2013 Module 12

Author's Comments: This example could replace the AutoCAD QUIT command. If the user entersa lowercase y or an upper-case Y, the drawing is quit but if any other answer is given, the AutoLISPprogram terminates and AutoCAD stays active. Notice the last function in the cond function (t nil). This statement always returns a true and allows the cond function to terminate if it cannot find a truetest-expression. You should include one true test-expression in every cond expression. It shouldalways be the last test-expression in the cond expression.

2

Author's Comments: The cond function is used to test 12 different expressions. If the userenters a number higher then 12, it prints an error message and ends. If a while loop was added tothe program, it could go into an continuous loop until the user entered a number between 1 and12.

Figure Example 2A

Figure Example 1B

Page 29: E:!!AutoCAD Modules!!!AutoLISP06Modules 4 …thecadguys.com/Sample Content/AutoLISP/AutoLISP Sample Module… · AutoLISP Module 6 Competency Test No.1 ... To count to 16, it uses

AutoCAD Self-paced eCourse - AutoLISP - Revised 2013-04-1012 - 22

Looping - Part 1 The CAD Guys Ltd. Copyright © 1993 - 2013 Module 12

Each cond loop should include a true statement as the last test-expression in thefunction.

Using the Cond Function

Purpose: This program edits the code of the AutoLISP Work 12-1.lsp programthat you wrote in the first workalong. It adds a method for the user to select thepoint style to be inserted plus counting and printing the number of points inserted.

Step 1 Open the program AutoLISP Work 12-1.lsp that you wrote in the first workalong.

Step 2 Using SAVEAS, save the program with the name AutoLISP Work 12-2.lsp.

Step 3 Rename the function MEASURELINE2.

Step 4 Using the cond function, add the necessary code that allows the user to select the pointstyle for the points that are inserted. Only allow the four point styles; 2, 34, 66 and 98.

Step 5 If the user enters a number other then 2, 34, 66 and 98, the program should print themessage "The Point Style 3 will be used" and then set the current point style to 3.

Step 6 Add the necessary code to count the number of points inserted and print how manypoints were inserted.

Step 7 Load and run the program in AutoCAD. (Figure Step 7A, 7B, 7C, and 7D)

Figure Step 7A

Figure Step 7B

Page 30: E:!!AutoCAD Modules!!!AutoLISP06Modules 4 …thecadguys.com/Sample Content/AutoLISP/AutoLISP Sample Module… · AutoLISP Module 6 Competency Test No.1 ... To count to 16, it uses

AutoCAD Self-paced eCourse - AutoLISP - Revised 2013-04-10 12 - 23

Looping - Part 1 The CAD Guys Ltd. Copyright © 1993 - 2013 Module 12

Author's Comments: Do your best to complete the AutoLISP program on your own. If you getstuck and cannot complete it on your own, check my answer in page 12-29. If you complete theprogram on your own and it returns the correct results, it is correct even if it does not match myprogram exactly. There are many ways to write the same program.

Figure Step 7C

Figure Step 7D

A method for the while loop to terminate must be written into the code. If there isnone, the program will go into a continuous loop and it would have to be stoppedby pressing the Esc key.

The Key Principles in Module 12

1 The American Standard Code of Information Interchange (ASCII code) was design tostandardize characters on the keyboard.2 Testing functions do not check for the same data type. They simply checks the numericalvalue of the numbers or the numerical value of the ASCII code if strings are tested. 3 The while function is used to loop through a expression, or a series of expressions, andevaluates those expressions in a continuous loop until a specified condition is met.4 A method for the while loop to terminate must be written into the while expression.5 The cond function is used to evaluate several test-expressions within one expression.6 The cond function should always include a true statement as the last test-expression in thefunction.

Page 31: E:!!AutoCAD Modules!!!AutoLISP06Modules 4 …thecadguys.com/Sample Content/AutoLISP/AutoLISP Sample Module… · AutoLISP Module 6 Competency Test No.1 ... To count to 16, it uses

AutoCAD Self-paced eCourse - AutoLISP - Revised 2013-04-1012 - 24

Looping - Part 1 The CAD Guys Ltd. Copyright © 1993 - 2013 Module 12

Lab Exercise 12-1

Program Name Template

AutoLISP Lab 12-1.lsp AutoLISP English

Purpose: This program will ask the user for information and draw a primary circle and spacingbetween circles. Using the spacing between circles, the program loops and draws as manycircles, inside the primary circle, as it can until the space between circles is less than the distanceremaining to the center of the circle.

Step 1 Write a program naming the function CONCIR. Have it run like an AutoCAD command.

Step 2 Declare all variables as local variables.

Step 3 Save the current value of the required system variables.

Step 4 Set the values of the required system variables.

Step 5 Ask the user to enter the layer name and the layer color. Save both values as strings.

Step 6 Using the input from Step 5, create a new layer and set it as the current layer.

Step 7 Ask the user to enter the following:A Specify the location for center of the circle:B Enter diameter of circle:C Enter spacing between circles: (Figure Step 7)

Figure Step 7

Page 32: E:!!AutoCAD Modules!!!AutoLISP06Modules 4 …thecadguys.com/Sample Content/AutoLISP/AutoLISP Sample Module… · AutoLISP Module 6 Competency Test No.1 ... To count to 16, it uses

AutoCAD Self-paced eCourse - AutoLISP - Revised 2013-04-10 12 - 25

Looping - Part 1 The CAD Guys Ltd. Copyright © 1993 - 2013 Module 12

Step 8 Draw a circle using the entered diameter at the location entered in Step 7. This is theprimary circle.

Step 9 Using a while loop, draw as many circles as possible, inside the primary circle, using thespacing between circles until the space left to the center of the circle is smaller than the spacebetween circles.

Step 10 Add the code to count the number of circles inserted and print the results.

Step 11 Reset the original system variables values as necessary.

Step 12 Load and run the program in AutoCAD. (Figure Step 12A and 12B)

Author's Comments: Do your best to complete the AutoLISP program on your own. If you getstuck and cannot complete it on your own, check my answer in Module 28. If you complete theprogram on your own and it returns the correct results, it is correct even if it does not match myprogram exactly. There are many ways to write the same program.

Figure Step 12A

Figure Step 12B

Page 33: E:!!AutoCAD Modules!!!AutoLISP06Modules 4 …thecadguys.com/Sample Content/AutoLISP/AutoLISP Sample Module… · AutoLISP Module 6 Competency Test No.1 ... To count to 16, it uses

AutoCAD Self-paced eCourse - AutoLISP - Revised 2013-04-1012 - 26

Looping - Part 1 The CAD Guys Ltd. Copyright © 1993 - 2013 Module 12

Lab Exercise 12-2

Program Name Template

AutoLISP Lab 12-2.lsp AutoLISP English

Purpose: This program asks the user to select two points, select the block, enter the distancebetween blocks and the scale of the blocks. It inserts blocks between the two points, includingthe start point. It inserts blocks until there is no space left between the last block inserted and theend point.

Step 1 Write a program naming the function IBLOCK. Have it run like an AutoCAD command.

Step 2 Declare all variables as local variables.

Step 3 Save the current value of the required system variables.

Step 4 Set the values of the required system variables.

Step 5 Ask the user to enter the layer name and the layer color. Save both values as strings.

Step 6 Using the input from Step 5, create a new layer and set it as the current layer.

Step 7 Ask the user to enter the following:A Enter distance between blocks:B Enter the scale of the block:C Specify start point:D Specify end point:

Step 8 Display the Text window and display the following menu:

Block List1. Circle - O2. Letter - X3. Centerline - CLEnter Block number 1 to 3:

Step 9 Using the cond function, insert the block selected by the user on the start point. If theuser does not enter a number from 1 to 3, print an error message and do not insert the block.

Step 10 Change to the Graphic window.

Step 11 Using the while function, insert blocks, until the distance between blocks is smaller thanthe distance remaining to the end point.

Step 12 Count the number of blocks inserted and print the number on the screen.

Step 13 Set the necessary system variables back to their original values.

Page 34: E:!!AutoCAD Modules!!!AutoLISP06Modules 4 …thecadguys.com/Sample Content/AutoLISP/AutoLISP Sample Module… · AutoLISP Module 6 Competency Test No.1 ... To count to 16, it uses

AutoCAD Self-paced eCourse - AutoLISP - Revised 2013-04-10 12 - 27

Looping - Part 1 The CAD Guys Ltd. Copyright © 1993 - 2013 Module 12

Step 14 Load and run the program in AutoCAD. (Figure Step 14A, 14B and 14C)

Author's Comments: Do your best tocomplete the AutoLISP program on your own. If you get stuck and cannot complete it onyour own, check my answer in Module 28. Ifyou complete the program on your own and itreturns the correct results, it is correct even ifit does not match my program exactly. Thereare many ways to write the same program.

Figure Step 14A

Figure Step 14B

Figure Step 14C

Page 35: E:!!AutoCAD Modules!!!AutoLISP06Modules 4 …thecadguys.com/Sample Content/AutoLISP/AutoLISP Sample Module… · AutoLISP Module 6 Competency Test No.1 ... To count to 16, it uses

AutoCAD Self-paced eCourse - AutoLISP - Revised 2013-04-1012 - 28

Looping - Part 1 The CAD Guys Ltd. Copyright © 1993 - 2013 Module 12

Answers to Workalongs

AutoLISP Work 12-1.lsp

Page 36: E:!!AutoCAD Modules!!!AutoLISP06Modules 4 …thecadguys.com/Sample Content/AutoLISP/AutoLISP Sample Module… · AutoLISP Module 6 Competency Test No.1 ... To count to 16, it uses

AutoCAD Self-paced eCourse - AutoLISP - Revised 2013-04-10 12 - 29

Looping - Part 1 The CAD Guys Ltd. Copyright © 1993 - 2013 Module 12

AutoLISP Work 12-2.lsp

Page 37: E:!!AutoCAD Modules!!!AutoLISP06Modules 4 …thecadguys.com/Sample Content/AutoLISP/AutoLISP Sample Module… · AutoLISP Module 6 Competency Test No.1 ... To count to 16, it uses

Lists - Part 2 The CAD Guys Ltd. Copyright © 1993 - 2013 Module 20

AutoCAD ® Self-paced eCourse

AutoLISPModule 20

Lists - Part 2

Learning Outcomes

When you have completed this module, you will be able to:

1 Describe and apply the quote, length, last, append and member functions.

AutoLISP Function: quoteThe quote use - do not evaluate function is used to obtain the expression unevaluated.

This function can also be written as the character ' (single quote). Quote is a very handy functionand can save the programmer many keystrokes.

Returns: The expression unevaluated.

Format: (quote expression) or 'expression

Examples:

1 Command: (quote s)SCommand:

Author's Comments: The character ' s ' is returned unevaluated. Notice how quote returns theupper-case ' S '.

2 Command: (' s)error: bad function((QUOTE S))*Cancel*Command:

Author's Comments: As you can see the ' (single quote character) cannot be used by itself atthe command prompt. It must be used inside an AutoLISP functions as shown in Example No. 3.

3 Command: (setq lst1 (quote (2.5 5.5))) (2.5 5.5)

Command: (setq lst2 '(2.5 5.5))(2.5 5.5)Command: (setq lst3 (list 2.5 5.5))(2.5 5.5)Command:

Author's Comments: Notice that the same list is returned, using either the quote function, the ' (single quote) or the list function.

Page 38: E:!!AutoCAD Modules!!!AutoLISP06Modules 4 …thecadguys.com/Sample Content/AutoLISP/AutoLISP Sample Module… · AutoLISP Module 6 Competency Test No.1 ... To count to 16, it uses

AutoCAD Self-paced eCourse - AutoLISP - Revised 2013-04-1020 - 2

Lists - Part 2 The CAD Guys Ltd. Copyright © 1993 - 2013 Module 20

AutoLISP Function: lengthThe length length of list function is used to obtain the number of elements in a list.

Returns: An integer.

Format: (length list)

Examples:

1 Command: (length (list 2 6 9 10))4Command:

Author's Comments: The length function returns the integer 4 which is the number of elementsin the list.

2 Command: (setq num3 (length (list 2.3 5.6 2.1 '(5 9 6 3))))4Command

Author's Comments: The length function counts a nested list as only one element.

AutoLISP Function: lastThe last last element in list function is used to obtain the last element in a list.

The specified list must not be nil.

Returns: An element.

Format: (last list)

Examples:

1 Command: (setq lst1 (list 2 6 9 10))

(2 6 9 10)Command: (setq e1 (last lst1))10Command:

Author's Comments: The last function returns the element 10. It is the last element in the list.

2 Command: (setq e2 ( last '("pipe" "motor" "valve")))"valve"Command:

Author's Comments: The last function returns the string "valve" since it is the last element in thelist.

3 Command: (setq num3 (last (list 2.3 5.6 2.1 '(5 9 6 3))))(5 9 6 3)Command:

Author's Comments: Since the list is nested, the last function returns the last item, a list.

Page 39: E:!!AutoCAD Modules!!!AutoLISP06Modules 4 …thecadguys.com/Sample Content/AutoLISP/AutoLISP Sample Module… · AutoLISP Module 6 Competency Test No.1 ... To count to 16, it uses

AutoCAD Self-paced eCourse - AutoLISP - Revised 2013-04-10 20 - 3

Lists - Part 2 The CAD Guys Ltd. Copyright © 1993 - 2013 Module 20

AutoLISP Function: appendThe append join lists function is used to join lists together to form one. All arguments specifiedin this function must be lists.

Returns: A list.

Format: (append list ...)

Examples:

1 Command: (setq lst1 (append '(3 6 9) '(5 2 7)))(3 6 9 5 2 7)Command:

Author's Comments: The two lists are joined together to make one.

2 Command: (setq lst2 (append (list "desk" 2 4.5) '("chair" (4.5 6.7) (2.8 5.2))))("desk" 2 4.5"chair" (4.5 6.7) (2.8 5.2))Command:

Author's Comments: Different data types can be included in the same list. Note how appendwill not affect nested lists.

AutoLISP Function: memberThe member search list function is used to search a list for an expression.

It will search the list for the expression and returns the first occurrence of the expression and theremainder of the list. If it does not find the expression, it will return nil.

Returns: A list or nil if expression is not found in the list.

Format: (member expression list)

Examples:

1 Command: (setq lst1 (member "chair" (list "desk" "table" "chair" "computer" "printer")))("chair" "computer" "printer")Command:

Author's Comments: Member returns the list starting at the string "chair".

2 Command: (setq lst2 (member 'c '(a b c )))(C)Command:

Author's Comments: Member returns a list with only one element.

3 Command: (setq num1 (car (member '6.5 '(3.2 5.2 6.5 7.8))))6.5Command:

Author's Comments: Note how one element is returned from a list. Since the member functionalways returns a list, the car function returns the first element in the list.

Page 40: E:!!AutoCAD Modules!!!AutoLISP06Modules 4 …thecadguys.com/Sample Content/AutoLISP/AutoLISP Sample Module… · AutoLISP Module 6 Competency Test No.1 ... To count to 16, it uses

AutoCAD Self-paced eCourse - AutoLISP - Revised 2013-04-1020 - 4

Lists - Part 2 The CAD Guys Ltd. Copyright © 1993 - 2013 Module 20

Using the length, append and member Functions

Purpose: This program demonstrates using the list functions length, last,member and append.

Step 1 Start a new drawing using the template AutoLISP English. Create a program and nameit AutoLISP Work 20-1.lsp

Step 2 Name the function LIST1. Have it run like an AutoCAD command.

Step 3 Declare all variables as local variables.

Step 4 Save the current value of the required system variables.

Step 5 Set the required system variables to the required values.

Step 6 Create the following lists:A (setq lst1 (list "desk" "chair" "cabinet"))B (setq lst2 (list "computer" "mouse" "keyboard"))

Step 7 Join the two lists to form one list.

Step 8 Open the Text window and print the following two statements on the screen.A The last item in the list is:B The number of items in the list is:

Step 9 In a loop, extract the item from the list and print them on the screen.

Step 10 Ask the user to enter one of the item in the list.

Step 11 Search the original list for the item entered by the user in Step 10 and return that itemand the remainder of the list, as a list. Print the new list.

Step 12 Reset the original system variable's values, as necessary.

Step 13 Load and run the program in AutoCAD. (Figure Step 13)

Figure Step 13

Page 41: E:!!AutoCAD Modules!!!AutoLISP06Modules 4 …thecadguys.com/Sample Content/AutoLISP/AutoLISP Sample Module… · AutoLISP Module 6 Competency Test No.1 ... To count to 16, it uses

AutoCAD Self-paced eCourse - AutoLISP - Revised 2013-04-10 20 - 5

Lists - Part 2 The CAD Guys Ltd. Copyright © 1993 - 2013 Module 20

Notes

The Key Principles in Module 20

1 The quote function is used to obtain the expression unevaluated.2 The length function is used to obtain the number of elements in a list.3 The last function is used to obtain the last element in a list.4 The append function is used to join lists together to form one.5 The member function is used to search a list for an expression.

Page 42: E:!!AutoCAD Modules!!!AutoLISP06Modules 4 …thecadguys.com/Sample Content/AutoLISP/AutoLISP Sample Module… · AutoLISP Module 6 Competency Test No.1 ... To count to 16, it uses

AutoCAD Self-paced eCourse - AutoLISP - Revised 2013-04-1020 - 6

Lists - Part 2 The CAD Guys Ltd. Copyright © 1993 - 2013 Module 20

Lab Exercise 20-1

Program Name Template

AutoLISP Lab 20-1.lsp AutoLISP English

Purpose: This program allows the user to draw a rectangle with a specified number of divisions.In the center of each division it inserts a number that increments by 1. It draws a verticalrectangle box if the Y axis is longer than the X axis and a horizontal rectangle of the X axis islonger than the Y axis.

Step 1 Name the function NUMBOX. Have it run like an AutoCAD command.

Step 2 Declare all variables as local variables.

Step 3 Save the current value of the required system variables.

Step 4 Set the required system variables to the required values.

Step 5 Ask the user to enter the layer name. Display the current layer name. If the userpresses Enter, use the current layer name.

Step 6 Ask the user to enter the layer color. Do not allow a null input and display the colors:"RED,R GREEN,G CYAN,C BLUE,B MAGENTA,M")

Author's Comments: If the user presses Enter to Step 5, change the color of the current layer inStep 6.

Step 7 Ask the user the following and save the coordinates. Rubberband a window and do notaccept a null input:A Specify the bottom left corner of rectangle: B Specify the top right corner of rectangle:

Step 8 Ask the user to Enter number of divisions: and save the input as an integer. Do not allowa null input, a negative input or a zero input.

Step 9 Calculate which axis is longer the X or the Y.

Step 10 Calculate the coordinates and draw the either a vertical rectangle or a horizontalrectangle including the lines for the divisions.

Step 11 Insert the numbers using the TEXT command. Set the text height to one-half the heightof the rectangle and center it in the sector.

Step 12 Reset the original system variable's values as necessary.

Page 43: E:!!AutoCAD Modules!!!AutoLISP06Modules 4 …thecadguys.com/Sample Content/AutoLISP/AutoLISP Sample Module… · AutoLISP Module 6 Competency Test No.1 ... To count to 16, it uses

AutoCAD Self-paced eCourse - AutoLISP - Revised 2013-04-10 20 - 7

Lists - Part 2 The CAD Guys Ltd. Copyright © 1993 - 2013 Module 20

Step 13 Load and run the program in AutoCAD. (Figure Step 13A, 13B, 13C and 13D)

Author's Comments: Do your best to complete the AutoLISP program on your own. If you getstuck and cannot complete it on your own, check my answer in Module 28. If you complete theprogram on your own and it returns the correct results, it is correct even if it does not match myprogram exactly. There are many ways to write the same program.

Figure Step 13A

FigureStep 13D

Figure Step 13B

Figure Step 13C

Page 44: E:!!AutoCAD Modules!!!AutoLISP06Modules 4 …thecadguys.com/Sample Content/AutoLISP/AutoLISP Sample Module… · AutoLISP Module 6 Competency Test No.1 ... To count to 16, it uses

AutoCAD Self-paced eCourse - AutoLISP - Revised 2013-04-1020 - 8

Lists - Part 2 The CAD Guys Ltd. Copyright © 1993 - 2013 Module 20

Answers to Workalongs

AutoLISP Work 20-1.lsp