You can then lookup the relevant AutoLisp commands for a more detailed explanation.
All AutoLisp programs must contain the suffix ". Is a pre-defined set of instructions that describes a set of actions that AutoLisp is to perform, divided into three sections:. These are like empty boxes in which to store data, to be used later.
In AutoLisp, variables may be a collection of letters or numbers as long as they begin with the letters.
An AutoLisp variable may contain more than one value in a single variable. A value can be anything, such as :. Structure your program in such a way that it is easy to understand, by yourself and everyone else.
Keep input statements together. Keep your arguments together.
Keep your commands together. Track your work with the semicolon.
How to write AutoLISP programs
When you begin a line with a semicolon, anything you write after will be ignored by AutoLisp. It is used for documentation and explanation of your program. Write notes about your program, what you are doing and what the variables are. A semicolon does not have to begin the line. Parentheses are vital to writing AutoLisp programs. All commands are surrounded by parentheses. AutoLisp uses parentheses to nest, allowing you to write a command that acts on evaluates another command.
In turn, that command can act on another. As you add parentheses, you're nesting commands become deeper and deeper.
Remember to come out of the nest with an equal number of parentheses. In AutoLisp the name of the program or function must be defined in the first statement, which is done by using the command:.
Defun is the first actual command and is followed by the name of the function or program.
Defun encloses the entire program and its closing bracket comes after the main body of the program There are different ways of starting a function for example:. Strings Strings are bits of text and can be up to a maximum length of characters eg: Point 1. Lists A list is a variable that has more than one element.
A point in your drawing is described by the value of the X co-ordinate and the value of the Y co-ordinate. In AutoLisp, that point can be described by a single variable, a list of two elements, the first being the X value and the second being the Y value eg: 7 The following are also examples of lists: 5 9 7 2 and 1.
Atoms If a variable has a single indivisible value it is an atom. For example, each element in the lists above is an atom e.
Related video shorts (0)
The type function will return the data type of a variable. For example:. Will return STR meaning "string". In fact, you can try this for yourself now. Type the code above at the AutoCAD command prompt and hit enter.
The forward slash character tells AutoLisp to interpret the following character as a special character rather than just as a letter "n".
AutoLISP Quick Start
The "n" must be always be lower case. Caution: Your prompt must always be between quotes, " prompt " otherwise you will get an error.
Tip: Just as in native AutoCAD commands, any valid option keyword can be entered simply by typing the upper case part of the keyword. So, in the example above, to answer Yes to the prompt, you need only type "y" but to answer NO, you must type "no".
Autolisp programming principles and techniques pdf merge
This is used to avoid potential ambiguities when 2 or more options begin with the same letter. The number 1 after the initget function in code example 8, above is known as an initget bit code. Initget bit codes can be used to control what type of inputs are allowed.
In this example, 1 is used to disallow a null input. This forces the user to enter one of the specified option keywords rather than just hitting carriage return. Note: setq is the primary assignment command in AutoLisp. The equals character is used, but only as a non assignment statement. It does not have the ability to make one variable equal to another as it does in some other programming languages it is used for a comparison of variables, numbers or strings.
Note: The first variable after the setq is the one that receives the value. Watch out for this because it is a potential cause of confusion. This command is used simply to print a message on the command line. This is a line feed that causes the next printed text to appear on the next line.
It generally follows the prompt command. This function prints the expression on the screen and returns the expression. It can be any expression and need not only be a string. The expression can also be written to a file and will appear in the file exactly as it would on the screen.
Is the same as prin1 except that the control characters "" are not printed. Can also be used to print a blank line by using no statement after princ. Same as prin1 except that a new line is printed before the expression and a space is printed after the expression.
This function sets an AutoCAD system variable to a given value and returns that value. The variable name must be in double quotes. AutoLisp provides a number of arithmetic functions and although the format of these functions is consistent with other AutoLisp functions function first, followed by values , it is not one we are familiar with from school.
The important thing to remember is that the order of the values is consistent with what we already know.
Examines two lines and returns the point where they intersect even if they do not physically cross one another. Note: values in expressions must be enclosed in quotes whereas variable names are not. When you used setq a getpoint you assigned the X and Y coordinate numbers to variable a. That Variable now is a list that may look like 5 If you want to look at the list in variable a , AutoLISP gives you a convenient way to do that from the command line.
The primary command for taking a list apart, car gives you the first element of a list.
Principles of Programming
If the value of variable a is the list 5 10 , then:. This is the secondary command for taking a list apart. If the value of variable a is the list 2 5 7 9 11 , then:. This always produces the second element of a list. Assuming the value of variable a is the list 5 10 , then:. This always produces the third element of a list. Assuming the value of variable a is the list 3 7 5 then:.
Changes a string of text from lower case to upper case, leaving upper case characters as they are. Returns a part of a string, from a specified position, ending either at the end or another specified position. For example, if a group of items is preceded by an apostrophe, it is treated as a list. Returns an angle between two points in radians.
To use that angle in AutoCAD you have to convert it back to decimal degrees. But a is not the correct rotation angle because it is in radians. Returns the nth element in a list, where n is the number of the element to return.
Zero is the first element. Often used with the subst function; the assoc function lets you search for a specific element, then assign that element to a variable. Lets assume variable b is the list 10 5.
You want to pull out the entire element and assign it to a variable c. This assigns the entire element containing the 40 in the list b to variable c. Now c is a list that looks like this: 40 5. Allows you to substitute one aspect for another.
Now lets substitute 20 for the 5 in the variable c. The new element will look like this: 40 h. You need to construct a new variable containing the entire list element, then use the new variable in the subst function. Constructs a new list with the new element placed at the begining.
Assume variable c contains the following list: 40 5. Also, assume variable h contains the real number Therefore, car c is the new first element, followed by the value of h.