Autolisp – Visual lisp

Lesson22d- Conditional Expressions

Sometimes you will want a program to do different things depending on different conditions. This is called branching. In order to create program branches, we need to construct “test conditions.” If the test is true, then we do one result; if false, a different result. Conditional expressions can be used with several functions to achieve branching in AutoLISP routines. 1. Any expression can be used as a conditional. In AutoLISP, any expression is true if it evaluates to a non-nil value. Any Read More...

Lesson21 – Good AutoLISP Programming Techniques

When creating AutoLISP routines, the programmer should keep in mind not only correct syntax, but also a well-structured program that is easy to comprehend, forgiving of mistakes, easy to debug, and easy to edit. Formatting You can format manually while you type. But Visual LISP includes helpful formatting tools. They automatically insert tabs and spaces where needed to give you added clarity and help you track parentheses. You can either Format edit window or Format selection. AutoLISP ignores spaces and carriage Read More...

Lesson20 – Limiting Acceptable String Input Values

In addition to the bit option that restricts input, there is a keyword option of (initget) that expands the acceptable responses to include specific keyword strings. String input can be limited to selected responses by using keywords along with the (getkword) function. Explanation: (initget) with keyword (initget [bit] [keyword]) The keyword string is a single string that contains all acceptable keywords separated by spaces. (initget 1 “Red White Blue”) By capitalizing the minimum input required for each keyword, the user only needs to Read More...

Lesson19 – Restricting Acceptable Input Values

One of the most tedious tasks for the AutoLISP programmer is error trapping, making sure the routine can handle all varied inputs from the user. By restricting the acceptable input values with the (initget) function, you can control information supplied by the user. Explanation: the (initget) function (initget [bits]) The bits is an integer that refers to the following restrictions to input: The (initget) function affects only the next (getxxx) function that is encountered. None of the initget bits affect the (getstring) Read More...

Lesson18 – Using Additional Point List Functions

In the previous module we learned two AutoLISP functions that asked for user input and returned a point list: (getpoint) and (getcorner). You can also use (list), (quote), and a new function, (polar), to return a point list. Use these point lists with an AutoCAD command to place objects or to generate other lists. Two other functions also use points to give us information: (distance) and (angle). Angles in AutoLISP are always measured in radians relative to the X-axis Read More...

Lesson17b – Creating a Point List from Symbols

Many times you will want to create a point list but the coordinate values are contained in AutoLISP symbols such as X, Y, and Z. What happens if we type ... Command: (setq PT4 '(X Y Z)) (X Y Z) But we didn't want an un-evaluated list (X Y Z), we wanted a list of the values of X, Y, and Z. What if we type: Command: (setq PT4 (X Y Z)) ERROR: null FUNCTION (X Y Z) (SETQ PT4 (X Y Z)) To solve our dilemma, Read More...

Lesson17A – Telling AutoLISP Not to Evaluate an Expression

Besides specifying AutoLISP functions themselves, lists will be used for two key purposes in AutoCAD: specifying coordinates in space (“point” lists) or describing a specific AutoCAD entity (“entity definition” lists). To explicitly type in a point list, a list of three real numbers, creates a problem: AutoLISP will try to evaluate the list as a function. Command: (setq PT1 (1.0 2.0 0.0)) error: bad function (1.0 2.0 0.0) (setq PT1 (1.0 2.0 0.0)) Command: When typing in an explicit point list, the (quote) function will Read More...

Lesson17C – Extracting Elements from a List

Extracting Elements from a List There are times when you will want to create a list from other lists. For example, you may want to get a point midway between two other points. To do this you would need the x coordinate of one point and the y coordinate of the other, and then do a bit of math. Two key functions, (car) and (cdr), are the essential tools in extracting elements from a list. The others, (cadr), (caddr) and (last) Read More...

Lesson16 – Utility Functions to Improve Performance

You may have noticed that our BOX1 program still doesn’t run as cleanly as most AutoCAD commands or other LISP routines that you may have previously used. After getting the core of a routine to work, you can add several simple functions to make it run more smoothly. Explanation: Utility Functions Each of these special commands returns nil except (princ). The expression (princ) displays the null character ASCII 00. Example “cmdecho” Command: (setvar “cmdecho” 1) Command: (command “line” “0,0” “1,5” “”) line From point: Read More...

Lesson15 – Automatic Loading of LISP Files

Before a function can be used, the file which contains its code must be loaded using AutoLISP's (load) function. By combining a series of similar functions into a single file with a .LSP extension, you can create a library of LISP files. For example, if all the code for the BOX1, MIDPT, and TAG functions were stored in one file called CLASS.LSP, you could load all those functions at once by typing: (load “class”) Special Files: ACAD.LSP and ACADDOC.LSP If a group Read More...

Lesson14 – Creating AutoCAD Command Functions

Not only can you create new AutoLISP functions, but in a similar manner, you can also create new AutoCAD commands! That is, the function name can be typed directly at the Command: prompt without using AutoLISP syntax. Creating a command-line function is simply a modified form of (defun). Explanation: the (defun C: ) function (defun C:name () <expression1> ... <expressionn> );end defun Notes The function name argument must have a name that starts with “C:” . The function must be defined with a nil argument Read More...

Lesson13 – Defining AutoLISP Functions

So far we have saved a series of AutoLISP expressions in a *.LSP file. Every time we load the *.LSP file, all expressions contained within it are executed. What would be more useful is to be able to create new AutoLISP functions and use them as many times as desired without loading each time. Such user-defined AutoLISP functions are created using the (defun) function. Explanation: the (defun) function (defun name ( [arguments] ) [the AutoLISP routine(s)...] );close defun The name is a Read More...

Lesson12 – Troubleshooting AutoLISP Files

Whenever you work with AutoLISP you will come across errors. Sometimes this is nothing more than a misspelling of a function or, very often, the wrong number of parentheses. Below are some common errors and some possible solutions. Explanation: the (type) function The “error: bad argument” message means that you have used the wrong data type. Quite often the arguments will be stored in a symbol so you will not be able to see what type of data it is. You Read More...

Lesson11 – Using the Visual LISP Console Window

Another way to load and test routines is to use the Visual LISP console window. This lets you run certain lines or complete programs while giving you feedback in Visual LISP if you are using debugging tools. To run selected lines of code 1. In the edit window, highlight the text (from parenthesis to parenthesis) by double-clicking in front of the opening or behind the closing parenthesis. 2. Press the Load selection button on the Tools toolbar in Visual LISP. To run the Read More...

Lesson10 – Loading an AutoLISP File

Once an AutoLISP file is created, it must be loaded in AutoCAD with the (load) function in order to be used. The (load) function (load filename) Loads the specified LISP file, where filename is the AutoLISP file name without the “.LSP” extension (“.LSP” can be included, but why type more than you have to?). The filename is a string so don’t forget to include quotes “ ” around it. Command: (load “box1”) Dialog Box Access (APPLOAD) You can also load AutoLISP files and other programs Read More...

Lesson 9 – Setting Up an AutoLISP Routine

Section 1:
Setting Up an AutoLISP Routine In this section you will learn how to: Divide a LISP routine into parts Prompt for user input Create an AutoLISP file using Visual LISP Typical Structure of a Routine AutoLISP routines can become very complicated. Like any complex problem, it helps to “divide and conquer” in order to understand the steps that will be necessary for the solution. Considering one part at a time will help you figure out what else you need in other Read More...

Lesson 8 – Storing and Setting AutoCAD’s System Variables

AutoCAD's SETVAR function, when used in conjunction with a system variable name, will display the current value and permit the user to input a new value. In AutoLISP, the user can read the value of any AutoCAD system variable using the (getvar) function, and can set any non-read-only system variable using the (setvar) function. Explanation: the (getvar) function (getvar) reads the value of any AutoCAD system variable: (getvar variable_name) The variable name is always a string. You should store the system Read More...
Show Buttons
Hide Buttons
SUBSCRIBE TO OUR NEWSLETTER

It's FREE!

Sign up and receive an email every time a new tutorial is published