Program AutoCAD-Based Products Without an Object Model10 Dec, 2014 By: Andrew G. Roe
Use AutoLISP to accomplish unique tasks in Raster Design.
The code might appear cryptic to those unfamiliar with AutoLISP, so here’s a brief overview of each line. The first line uses a function called defun to define a new AutoLISP routine; in this case, one called “CropCircle.” The “c:” in front of the function name means it can be run directly from the AutoCAD Command line.
The setq function sets the value of a variable called entList. This variable will hold the entity data for a circle selected by the user. The entget function retrieves the circle entity's data, while the entsel function prompts the user to select a circle. Sandwiched in between these functions is the car function, which returns just the first element of a list.
The next line uses If…then logic to determine whether the selected entity is a circle. If the user selects a circle, the next segment of code will be executed. If not, the routine skips to the next code segment, with each segment delineated by an equal number of left and right parentheses. Comments are indicated by a semicolon (;) and are not executed.
The Command-S function executes a Raster Design command and the supplied input. In this case, the supplied input includes the center point, defined by group code 10 in the entity data, and the radius defined by group code 40. If you don’t have Raster Design, you can substitute an AutoCAD command here. More information on AutoLISP functions can be found in the online help.
Execute the Command
With the application loaded, you can test the routine created in the previous steps.
1. At the AutoCAD Command line, type “CropCircle” and press Enter.
2. Select a circle when prompted. The image is cropped to the limits of the circle. If you select an entity that is not a circle, a message will be printed at the Command line.
This example accomplishes a fairly simple task and uses the Command line to interact with the user. To make the application more user-friendly, you could also build a plug-in to build a custom interface and make the command accessible from a menu. And you can probably think of various ways to further customize this example, but it shows you how to get started with using AutoLISP with a vertical product.
In future articles, I'll continue to explore additional facets of AutoCAD programming, both with standard AutoCAD and other vertical products. If you would like to suggest a topic for this series of programming articles, feel free to send me an e-mail.