In this Article...
We have started the AutoLISP tutorial by creating a very simple program: zoom to origin. That program works, but it is very limited to zoom to 0,0,0 coordinate only. We will add more functionalities so users can pick other point to zoom to. Because we will enable users to pick their own point, zoom to origin may not appropriate name anymore. So I changed the name to Zoom to Point (ZP). Remember to type ZP then [enter] to run it after this.
Visual LISP Editor
Again, you basically can use notepad, or notepad ++ to create AutoLISP program. However, Visual LISP editor can be very handy to find errors when our application become more complex. If you want to learn more about it, you can read visual lisp tutorial on AfraLISP, which is very good. I will not cover about it, at least now, and focus more on how you can write the routines.
Using Variables
In programming, we use variables to save our values. In our previous sample, we haven’t use it.
(command "_ZOOM" "_C" "0,0" "2000")
Let’s say we save the coordinate to ZPT (zoom point) and save the magnification to MRAT (magnification ratio), then our program will be like this.
(command "_ZOOM" "_C" ZPT MRAT)
Remember, the program name and user variables have to be unique and not using AutoCAD system variables. You can use any other names that can be easily recognized.
So how to tell AutoCAD the variables value? You can assign the value to variables using setq function. To set the ZPT value and MRAT value, we add these following lines before our command.
(setq ZPT '(0 0))
(setq MRAT 2000)
Now let’s put it all together in our program. Now our program become like this.
(defun c:ZP ()
(setq ZPT '(0 0))
(setq MRAT 2000)
(command "_ZOOM" "_C" ZPT MRAT)
(princ)
)
Try to type it in your visual LISP editor and try it. Load the program, and run it. Does it work?
Asking for User Input
Now we are going to ask the user to define their point. Change the 0,0 coordinate to this code:
(setq ZPT (getpoint))
Now load the application again, then try to activate it again.
It works, doesn’t it? But there is one thing left. We both know that the program want us to click a point, but there is no instruction what should we do. If we give the program to other people, they will be confused! So let’s fix the code.
(setq ZPT (getpoint "
Pick a point or type coordinate: ")
Load and try again. Nice… isn’t it?
Defining Local Variables
Let us move to AutoCAD. In AutoCAD command line, type !ZPT. The exclamation mark in front of the variable name will show you the variable value. If you haven’t run the program, it should mention nil. Or it doesn’t have a value. If you have run it before, it will show the last coordinate you picked.
It means that AutoCAD remember the value and use your computer resource. It probably doesn’t matter because we only have one AutoLISP program running and only has 2 variables. But when you already have dozens of AutoLISP program running with many variables, it may affect your machine’s performance. You may want to leave the value as global variable if you do want to use it in other program. But if you are not, it is a good idea to set it as a local variable.
Close your drawing, and create or open a new drawing. Type ZP then [enter] to test if your program hasn’t loaded yet. It should say: Unknown command “ZP”. Press F1 for help. If it’s not, close AutoCAD and open it again. Do not load your program for now.
Open Visual LISP editor. In defun line, modify it to this.
(defun c:ZP (/ ZPT MRAT)
Now load your program. After you run it, type !ZPT or !MRAT to see the variable value. Now they should say nil. Now AutoCAD doesn’t keep the variable in your computer memory after the program has finished running.
Giving Comments
Most AutoLISP program have comments. The idea is to give information when other people open the program, what it does, who create it, and you can also add some information what the codes in a line does. Comments will be ignored by AutoCAD, and will not be processed. Let us complete it so it would looks like a real program.
You can give comments to your AutoLISP program using semicolon character. I added some comments like below. Now you can see how the pretty colors can easily distinguish the codes right? See what the colors mean in this AfraLISP tutorial.
If you have a problem with the code, try to copy the completed code below, and paste in your Visual LISP editor.
;Zoom to Point
;This program will quickly zoom to a specific point
;Created by: Edwin Prakoso
;website: https://www.cad-notes.com
(defun c:ZP (/ ZPT MRAT)
(setq ZPT (getpoint "
Pick a point or type coordinate: ")) ;this will ask for user input
(setq MRAT 2000)
(command "_ZOOM" "_C" ZPT MRAT) ; this will run the zoom command
(princ)
)