Program AutoCAD-Based Products Without an Object Model11 Dec, 2014 By: Andrew G. Roe
Use AutoLISP to accomplish unique tasks in Raster Design.
In a previous article, we examined how to customize AutoCAD Civil 3D using a .NET approach. While .NET can be useful for automating tasks in AutoCAD and certain Autodesk vertical products such as Civil 3D and MEP, not all Autodesk software can be customized with .NET. Some titles, such as AutoCAD Electrical and Raster Design, do not include a .NET application programming interface (API), so another approach is needed. In these cases, AutoLISP provides a handy alternative.
This example will explain how to use AutoLISP to automate a task in Raster Design, an AutoCAD add-on for editing and managing raster images in an AutoCAD environment. For those experienced with AutoLISP, this may be familiar territory, but for VBA and .NET programmers, it may present some new concepts. Even if you don’t use Raster Design, most of the concepts still apply in customizing AutoCAD-based products.
Raster Design offers a variety of tools for modifying raster images, including several commands for cropping images. For example, the Crop Circular Region command (ICROPCIRC) crops an image to a region defined by a center point and radius. As an alternative, perhaps you’d like to simply click on a circle and have the image cropped to that region, without specifying the center point and radius. This is a fairly simple task to automate with AutoLISP.
You can write an AutoLISP routine in a text editor, but the Visual LISP environment included with AutoCAD offers a more efficient way to develop and test programs. To run this example, you’ll need to insert a raster image in an AutoCAD drawing and draw a circle somewhere within the limits of the image, as shown in the figure below. Once the image has been inserted, you can build and test your routine.
Build an AutoLISP Routine
2. Click File > New File to open a code window.
3. Type (or copy and paste) the following code into the Visual LISP code window:
(defun c:CropCircle () (setq entList (entget (car (entsel "Please select a circle boundary: ")))) (if (= (cdr (assoc 0 entList)) "CIRCLE") ; Execute a Raster Design command. For AutoCAD, insert an AutoCAD command. (command-s "._ICROPCIRC" (cdr (assoc 10 entList)) (cdr (assoc 40 entList))) ; Else do the following. (princ "Entity is not a circle.") ) )
4. In the Visual LISP IDE, click File > Save and save the file in a location of your choice. The file is saved with a .LSP extension, but is a simple text file editable by any word processing software.
5. From the Manage tab of the AutoCAD ribbon, click Load Application.
6. Navigate to the previously saved LSP file and click Load. Click Close to close the Load/Unload Applications dialog box.
About the Author: Andrew G. Roe
In her easy-to-follow, friendly style, long-time Cadalyst contributing editor and Autodesk Technical Evangelist Lynn Allen guides you through a new feature or time-saving trick in every episode of her popular AutoCAD video tips. Subscribe to the free Cadalyst Video Picks newsletter and we'll notify you every time a new video tip is published. All exclusively from Cadalyst!