Integrating AutoCAD with Other Applications (Harry's Code Class Newsletter)22 Sep, 2008
Custom programming lets your systems share data -- and ramps up your productivity.
One of the many ways that you can leverage technology to gain productivity is to bring different technologies together, such as a database and a CAD system. In the world of computer programming, this is often called integration. You might integrate your CAD system with a spreadsheet or a design program so they flow together seamlessly, or you might integrate a database into a drawing to create reports. The possibilities are only limited by the tools at your disposal and your imagination.
I’ve been building integrated systems for a long time. It is not as easy as it often appears. There is always some subtle detail or twist that makes it a challenge. So how do you do it -- how do you get one program to talk to another program? In this edition of Harry’s Code Class, I’ll try to provide some direction.
When you set out to do custom programming to integrate two software platforms, the first order of business is to diagram how you want the data to flow. Draw a simple chart showing how one application will be used to supply specific data to another. This will clarify where the data is coming from (and going to) and will put a specific focus on what you need to accomplish. Sometimes the result will be just a transfer of some data in one direction. Other times you may need to interact between the applications sending some data one way, having it processed, then accepting new data back. The goal is to lay out the data interaction in a visual format that can be reviewed and studied.
After determining which data you want to send back and forth, the next step is a little more tricky. You will need to figure out how to send and receive information between the applications. An experienced programmer will already know most of what is needed and can apply this information when describing the data flow chart. An inexperienced programmer may need to revisit the data flow chart after learning which interface tools are available, then apply any limitations discovered.
Integrating and Interfacing
The tools for interfacing data these days are quite wonderful. The primary tool is to pass data through files. One application writes a file that can be read by the other application. Integration is often just matter of creating a program to convert one file format to another for processing.
In the past decade, object-level interfacing has become the better solution. An object-level interface is one where a part of one application is exposed for access by other applications. That means you can literally run pieces of one program from inside another program. For example, you might link an Excel spreadsheet to AutoCAD by writing a program that opens the spreadsheet, searches it, and returns data. The tools to perform these feats are actually functions inside the Excel system that you can directly invoke, once a link has been established. For more information about incorporating libraries from other applications, see (VLAX-IMPORT-TYPE-LIBRARY) in AutoCAD's Visual LISP Help system.
The steps for object-level interfacing between AutoCAD and another application are the same no matter which programming language you are using. First you must establish a connection. Your choice of connections at this stage will depend on the other application but can be boiled down to either starting the other application fresh or finding it already running and ready.
When a connection is established, your program will have access to an object that is used in turn to access the rest of the application. You can think of this object as a simple pointer to the other stuff. The goal is to get a pointer to what you need. That pointer may point to the other application, an open document inside the other application, or some other object buried deeper inside. From an AutoCAD perspective, think of a line in your drawing. That line is contained inside a block such as model or paper space, which in turn is inside a drawing document. The drawing document is referenced for the open documents collection inside the root AutoCAD application object. Sounds confusing, but it is like reading a road map once you get used to it.
Objects are like trees, with one object holding another object like branches from a trunk. The base object is called the root. Navigating an object tree starts at the root, then climbs up the object tree to the items of interest. The items in an object tree will have properties and methods. Properties can be composed of further objects, and that is how the tree is traversed.
Delving into Objects
So where do you look to learn more about the objects in an application? The first place is in the automation tool more commonly known as VBA (Visual BASIC for Applications).
Many applications utilize VBA as a programming tool. AutoCAD is one of them. If you don’t know VBA, don’t despair. VBA is pretty easy to read and understand, and the Help files found in the VBA environment (just a click away when you’re in AutoCAD) hold the information about the objects we are interested in learning about. Generally every VBA-related Help system has an object reference guide of some sort for the environment hosting it. Some are nicely drawn, as in AutoCAD’s object model diagram found in the Help section, named ActiveX and VBA Reference. Now the issue is how to apply the information in the Help files to the language tool you are using for customization.
When using a language that references objects in the same manner that VBA does, converting the Help file information into something your program can use is pretty easy. It simply requires that you understand how to access properties as well as invoke methods in the language you are using. The job becomes a bit more difficult when using a language that does not reference objects in the same manner. For example, Visual LISP does not support the dotted type of object referencing you see in VBA code. That does not mean Visual LISP cannot access objects -- it does have the ability to work with other object models. It is just different in how it looks, and that can slow you down at first.
There are two main areas of concern when switching the VBA-based Help information into Visual LISP. The first is to pay attention to where something exists on the object tree, and the second is to see exactly what kind of data it works with or requires. Because Visual LISP allows symbols to take on any value of any type at any time, it is easy to provide the wrong type of data when calling an externally defined method from your program. Solving the first issue -- getting the object tree linkage right -- is more direct. For that you use symbol references and add that as the first parameter to the function. The best way to learn about these things is to look for specific examples that are close to what you are trying to do. Cadalyst’s new CAD Tips site has numerous examples -- try a keyword search for “Excel” or “VLAX” -- as do some of the references listed below.
One word of caution when looking at all this. It can be very version specific -- that is, upgrades of other programs could create difficulties and should be done with care. If you change a supporting module such as the spreadsheet version, you may have to update initial references (to the new folders) and some of the property or object names. The best trick is to isolate all the code that references the external object into a set of small routines that handle the basic operations. Then you can update just those isolated routines to get all your other automation utilities running after an upgrade to one of the supporting modules, such as the word processor or spreadsheet tool.
Learning how to interface other applications using your knowledge of AutoCAD opens the door to other worlds. There’s nothing more satisfying than watching a complete set of sheets get finished automatically by a program you created. And that may get your imagination going for even more fantastic interfaces.
Until next time, keep on programmin’.
Check out these other cool online resources that will help you understand application integration.
- Cadalyst Discussion Forums. Hot Tip Harry’s forums are a great place to ask for expert assistance!
- AUGI (Autodesk User Group International). The AUGI site is full of resources and discussion forums to help answer your application integration questions.
- Autodesk Customization Article Library. The software developer offers a long list of articles to assist your AutoCAD customization efforts.
- Visual LISP-Excel Bible. This resource from KozMoz provides some great Visual LISP-Excel interface information.
- Linking Excel and AutoCAD with Data Links. Not a programmer? Check out this article from the CAD Geek.