Content area
Accessing ActiveX objects from AutoLISP code provides one with the same object model, properties and methods that are available from the C++, Visual BASIC, and Delphi programming environments, with a syntax and programming style AutoCAD users are already familiar with. AutoCAD can be integrated with Microsoft Office applications and many other Windows programs that support ActiveX methodology.
Get the code
Download the code for this and all articles at www.cadonline.com. Look for file SEP99.EXE in the Get the Code area.
If you are familiar with AutoLISP, your natural next step to a higher level of programming is VLISP (Visual LISP). VLISP offers several advantages over AutoLISP. Its complete IDE (integrated development environment) includes a debugger, syntax checker, automatic indenting, code formatting, and compiling for code security; its interface is much like the VBA (Visual BASIC for Applications) IDE. VLISP continues the legacy of AutoLISP and assures developers that future releases of AutoCAD will offer
AutoLISP as a customization option. Accessing ActiveX objects from AutoLISP code provides you with the same object model, properties, and methods that are available from the C+ +, Visual BASIC, and Delphi programming environments, with a syntax and programming style you are already familiar with. As an added bonus, you can integrate AutoCAD with Microsoft Office applications and many other Windows programs that support ActiveX methodology.
Objects
AutoCAD objects, which you are already familiar with, include lines, arcs, circles, and text. These are the main components of the ActiveX application. In ActiveX, linetypes, dimension styles, text styles, layers, groups, blocks, viewports, model space, paper space, the drawing, and the AutoCAD application are represented as objects. Refer to the AutoCAD Visual LISP and ActiveX Help section under the ActiveX and VBA index item to see the complete object model, methods, properties, and events.
The AutoCAD Object Model follows a hierarchical structure. The AutoCAD Application object is at the top, or root. All objects within the object model are grouped into collections. For example, the Documents collection contains all AutoCAD drawings open in the current session. The active Document object is the current selected drawing. The Blocks collection contains all the blocks in the AutoCAD drawing, and the PaperSpace collection is a special Block object that holds all the entities in the active paper space layout.
Each object within the model provides access to the next level of objects and has one or more properties. A line object is described by properties such as startpoint, endpoint, linetype, and color. Properties such as radius, area, and linetype describe a circle object.
Objects also contain methods, which are the actions available for a specific object. Some methods may apply to most of the AutoCAD drawing objects-for example, the Move and Mirror method-but others apply only to specific objects. The Offset method applies only to objects such as lines, arcs, circles, and ellipses.
VLISP functions
You access ActiveX objects in VLISP through a set of functions added to the AutoLISP language. These functions use the prefix vla- (table 1).
A general set of ActiveX-related functions let you access custom ActiveX objects or objects from other applications, such as an Access database. VLISP provides the following AutoLISP functions that are prefixed with vlax- and apply to methods, objects, and properties (table 2).
Accessing objects
Making the transition from AutoLISP to VLISP is not as difficult as you might think. Once you become comfortable with the VLISP IDE and the editing and debugging tools, you'll wonder how you worked without them. You should begin editing your existing AutoLISP files in VLISP to get familiar with the IDE.
Even if you have little or no experience with AutoLISP, you can begin accessing ActiveX functions from within the VLISP IDE at the Console prompt. The Console prompt is the open window with the $ prompt at the bottom of the VLISP IDE.
Let's get started
The first step in using ActiveX functions with AutoLISP is to load the ActiveX supporting code to enable these functions. For this example, start AutoCAD 2000 and open the VLISP IDE, then type the following expression at the VLISP Console prompt:
Press <Enter> after the closing parenthesis. This function should be called once at the beginning of your application. It checks to see if ActiveX support is loaded. If not, AutoCAD loads ActiveX and other VLISP extensions to the AutoLISP language. Not calling this function causes the application to fail unless the ActiveX support is already loaded.
Next, from the VLISP Console prompt, establish a connection to the AutoCAD Application object by typing:
A pointer to the AutoCAD Application object is set to the ACAD-OBJECT variable as a unique VLISP data type called VLA-OBJECT. When you design a program, avoid repeated calls to the AutoCAD application because it degrades performance. Instead, obtain this object once and refer to the object pointer throughout the application. In a program, you'd replace the above expression with the following code:
Next in the AutoCAD object model hierarchy is the ActiveDocument property, which leads to a Document object that represents the current AutoCAD drawing. To return the ActiveDocument, use the following expression from the VLISP Console prompt:
You want to avoid repeated calls to the ActiveDocument as well. In a program, replace the above expression with the following code:
To access graphical objects in the AutoCAD drawing, you must set a pointer to either the ModelSpace or PaperSpace property:
Again, you want to avoid repeated calls to the objects, so replace the above expression with the following code:
Finally, you are ready to add objects to the AutoCAD drawing. To add a circle in model space from the VLISP Console prompt, type this line:
If you defined functions for the Application, ActiveDocument, and ModelSpace objects, use the following function call to add the circle:
Let's summarize the sequence of events in our ActiveX example:
Load the ActiveX support functions.
Set a pointer to the Application object.
Set a pointer to the current Active Document object.
Set a pointer to the ModelSpace object.
Draw a circle in model space.
You can see this was not difficult, though some familiarity with AutoLISP or VBA helps. Future columns will continue to explore the capabilities and integration with Office applications that VLISP opens up for ActiveX programming. I
Barry Bowen has written more than half a million lines of AutoLISP code. He owns Barry R. Bowen Associates in Memphis, Tennessee, and is an AutoCAD and AutoLISP instructor at the University of Memphis. You can reach him at 901.373. 6468 or barry.bowen@cadonline. com.
Copyright Advanstar Communications, Inc. Sep 1999