Save time, and automate your tasks !!!
Lesson no. 3
- Until now, the examples we did directly into AutoCAD were not very practical , because the results was not saved into memory. Normaly we write a program with a text editor like "NOTEPAD". Next, we load it into memory to use it into AutoCAD. We will come back for the loading soon. For now, we will type at the comand prompt as we did before, but we will keep the result into memory.
(SETQ A (+ 2 5))
The SETQ function allow us to keep the result (7) into memory. In fact, we did reserved a space named "A" into memory in which the result is kept. The name that we give to the space can be anything but there is a few rules:
1- The name cannot begin with a number
2- Dont use an AutoCAD command or an AutoLISP function.
3- Try to have names composed of 6 caracters or less (more then 6 caracters takes 16 bits of memory instead of 8).
4- Give names that will remind you of the kind of result you wanted.Examples: Let say that you want to calculate the width, so...
(SETQ A (+ 2 5)) ® Good but "A" does not mean much
(SETQ W (+ 2 5)) ® Better, "W" for Width
(SETQ WIDTH (+ 2 5)) ® Best "WIDTH" cant be better
(SETQ THEWIDTH (+ 2 5)) ® Good but the name is too long
(SETQ 2WID (+ 2 5)) ® No "2WID" begin with a number
(SETQ LINE (+ 2 5)) ® No "LINE" is an AutoCAD command
(SETQ ABS (+ 2 5)) ® No "ABS" is an AutoLISP functionTo visualize the contents of a variable (of the reserved space), we type !width
If there is a content, it will be display, if not, "nil" will be displayCOMMAND: !width
nil
COMMAND: (setq width (+ 2 5))
7
COMMAND: !width
7We use a lot the SETQ function , because we always need to do intermediates results, and we can use the variable name to do other results.
Example 1:
(setq width (+ 2 5)) ® 7
(setq height (+ 4 4)) ® 8
(setq area (* width height)) ® 56Example 2:
(setq width "width") ® "width" (caracters strings)
(setq area (* width height)) ® Bad argument typeIn this last example, WIDTH is a variable that contains le word "width", so, the result is an error message. "Bad argument type" means that one of the two variables (or the two variables) dont contains the good type of data.