Macro Recorder.Another useful aspect to programs such as AutoLISP is their ability to perform repetitive tasks. For example, suppose you want to be able to record a series of keyboard entries as a macro. One way to do this would be to use a series of Getstring functions as in the following: (Setq str1 (getstring "\nEnter macro: ")) (Setq str2 (getstring "\nEnter macro: ")) (Setq str3 (getstring "\nEnter macro: ")) (Setq str4 (getstring "\nEnter macro: ")) Each of the str variables would then be combined to form a variable storing the keystrokes. Unfortunately, this method is inflexible. It requires that the user input a fixed number of entries, no more and no less. Also, this method would use memory storing each keyboard entry as a single variable. It would be better if you had some facility to continually read keyboard
input regardless of how many different keyboard entries are
supplied. The While function can be used to repeat a prompt and obtain
data from the user until some test condition is met.
(defun C:MACRO (/ str1 macro macname) (setq macro '(command)) ;start list with command (setq macname (getstring "\nEnter name of macro: ")) ;get name of macro (while (/= str1 "/") ;do while str1 not equal to / (setq str1 (getstring "\nEnter macro or / to exit: " )) ;get keystrokes (if (= str1 "/") (princ "\nEnd of macro ") ;if / then print message (Setq macro (append macro (list str1))) ;else append keystrokes to list ) ;end if macro list );end while (eval (list 'defun (read macname) '() macro)) ;create function (princ) );end macro (princ) Now we will use the macro program to create a keyboard macro that changes the last object drawn to layer 3. Do the following:
The Enter macro prompt appears again.
This is where the 'while' function takes action. As you enter each
response to the Enter macro prompt, 'while' tests to see if you entered a
forward slash. End of macro The input you gave to the Enter macro prompt is exactly what you would enter if you had used the change command normally. Now run your macro by entering: (chla) The line you drew should change to layer 3. When Macro starts, it first defines two variables def and macro. (setq def "defun ") (setq macro '(command)) Def is a string variable that is used later to define the macro. Macro is the beginning of a list variable which is used to store the keystrokes of the macro. The next line prompts the user to enter a name for the macro. (setq macname (getstring "\nEnter name of macro: ")) The entered name is then stored with the variable macname. (while (/= str1 "/") The 'while' expression is broken into several lines. The first line
contains the actual while function along with the test expression. (setq str1 (getstring "\nEnter macro or / to exit: " )) When the user enters a value at this prompt, it is assigned to the
variable str1. (if (= str1 "/") If the test results in T, the next line prints the string : End of macro (princ "\nEnd of macro ") This expression prints the prompt End of macro to the prompt line. (Setq macro (append macro (list str1))) The 'append' function takes one list and appends its contents to the end of another list. In this case, whatever is entered at the Enter macro prompt is appended to the variable macro. Each time a new value is entered at the Enter macro prompt, it is appended to the variable macro creating a list of keyboard entries to be saved. The next two lines close the if and while expressions. );end if );end while The last line combines all the elements of the program into an
expression that, (eval (list (read def) (read macname) '() macro)) The read function used in this expression is a special function that converts a string value into a symbol. If a string argument to read contains spaces, read will convert the first part of the string and ignore everything after the space. |