Extend the Power of DraftSight with Customization – Part 3, VBA
Welcome to Part Three of our series on Customization in DraftSight. In Part One, we introduced you to Scripts as described in the popular webinar presented by Ramon Busboom and Vince Aman. In the second installment, we explored a few of the many ways you can use LISP to create your own custom commands.
In today’s post, we’re going to take a look at how Ramon uses Visual Basic for Applications (VBA) in Excel to manipulate entities within a DraftSight drawing.
Visual Basic for Applications
After scripts and LISP, the next step for interfacing with the DraftSight application object is often Visual Basic for Applications. VBA is a Microsoft® programming interface and is included within its Office applications. As a descendant of the original BASIC language, it’s intended to be easy to use.
Unlike LISP, VBA allows the programmer to access ActiveX controls and objects through its Component Object Model (COM). Don’t worry, we’re not going to get too technical here, but just know that’s kind of programming speak for “it can do things and communicate between applications that LISP can’t.”
Here’s the cool part: you can use it to access the DraftSight application and control it in various ways from Office (or other) applications.
Ramon walks us through a couple of examples of using Excel® to manipulate DraftSight entities via VBA. The first example shows us how easy it can be to update block attributes. Using Excel’s VBA, he extracts all the block definitions in a drawing, along with individual instances – including their attributes. Now, in Excel, he easily changes all the attribute values, and pushes them back into DraftSight using the VBA code he wrote with Excel.
Next, he demonstrates how a client uses a similar method to control 2D Constraints via Excel. It’s truly fascinating to see, and well worth your time to watch. If you want to jump right to it, go to the 16:56 mark in the video.
Finally, Ramon shows us how to leverage this technology to talk back and forth between DraftSight and SOLIDWORKS. It is not at all uncommon for our users to employ both programs in their workflows. Wouldn’t it be nice to make a 2D change in DraftSight, and (almost) automatically update your 3D subassembly in SOLIDWORKS? It’s all possible with some fairly simple VBA code.
Getting Started
A few blog posts certainly don’t do this webinar justice. It really is worth your time to watch it for yourself. When you do, you’ll no doubt wonder how to get started.
First, know that there’s nothing you need to add to DraftSight. There’s no Software Development Kit (SDK) needed. Most people that learn VBA are self-taught. DraftSight ships with some sample projects for you to use. Make copies of course, but check them, make some changes, see how they work.
Another popular method is to use the Macro Recorder feature in DraftSight. It will save your creation and editing actions, and then you can view the code they create and learn from it.
We think you’ll enjoy the webinar, and hopefully you’ll get some ideas on how you too can make DraftSight even more powerful with its many customization options.