Thursday, 10 February 2011

Visual Basic (VB) for Civil3d

Over the last 3 or 4 years I have replied to various queries in forums on how to get started in programming for Civil3d. This was to help people get started as it is quiet hard to find an explanation in lay terms of how you go about things. So what follows below is an overview of my amateur understanding of the VB world and links to numerous references of further reading that I have found useful on the way to writing my own bits of time saving code for Autocad and Civil3d.
I am completely self taught so my descriptions of things will not be perfect for those more advanced programmers out there. But should be sufficient for those that want to start programming in Autocad Civil3d and get them heading in the right direction.
VBA and what is the difference?
For the benefit of those that have done some VBA programming before and others that want a bit of history there are two major differences in using VBA for Autocad versus

The Integrated Development Environment (IDE) ;the place you type your code for VBA is built-into Autocad and accessed by typing VBAIDE at the commandline and for is a standalone program run outside Autocad and can either be Microsoft Visual Basic Express Edition (Free) or Microsoft Visual Studio ($300US) or any other third party IDE you like.
The VBA (IDE) is being phased out of Autocad because it is old technology no longer being supported by Microsoft. As of 2010 Autodesk started to remove the VBA (IDE) from Autocad’s base install, it can however still be downloaded and installed separately, but for how much longer this will be an option no one knows.

If you are starting out from scratch or wanting to update some old VBA code to technology then you should start by using the IDE and not the VBA one. Seeing the VBA (IDE) was provided for free in Autocad and amateur programmers like myself like that model you will need to download and use Microsoft's free express programming interface. You will find other fulltime programmers use the professional version.

The way that the two IDE’s communicate with Autocad is different yet the same in places.Effectively the VBA (IDE) uses the ActiveX. .Com managed wrapper only while the (IDE) can use both the older .COM wrapper and the newer .NET wrapper. The benefit of the later being you can bring your old VBA code forward into the new IDE and type new code pretty much the same as you did before in VBA except with a few necessary modifications for the ThisDrawing object. 
Before I get into the basic setup you’ll need I should touch on the different programming languages. I tend to use the programming language Visual Basic (VB) as I started writing programs in Basic for my Commodore 64 as a kid and VB has sort of evolved from Basic. I have programmed in various languages over the years but as VB looks more like English and I program in spits and spurts, I always gravitate back to it because its easier to understand than say C# and C++. That said, it is pretty easy to pickup different programming languages once you're started in one as they use the same control statements like IF, FOR, NEXT and LOOP etc but just use different characters to represent them. to C# Examples
One of the much touted benefits of .net programming is that compiled dll files in different languages can be referenced by each other and used in one larger software solution. I have used this a number of times when i have seen something written in C# that I have wanted to use in my project. 
If you know nothing at all about programming you are going to have to learn how to use the programming environment IDE as well as the basics of the language you have chosen. Below under references, I have listed a number of good VB books that I have borrowed from the local library or brought on VBA and that I have found useful over the years. I have listed them in the order I would read them if I was starting out from scratch.

Get started
To get started on your machine you will have to
1.Download and install Microsoft Visual Basic .net Express edition either 2008 or 2010 on your PC from
2. Install a Autocad express template that lets you use the free express program by setting Autocad to load and open the .dll file you create when debugging your programs.
This is the trick to using the free Express IDE as by default the Express IDE does not give you access through its user interface to set Autocad as the program for use during debugging. To get round this Autodesk an others have created special template files that have this setting already made for you.
To date I have not found any specific Civil3d Templates for 2011 to 2009 however Strahimir Antoljak wrote one for 2008 for his AU paper which is downloadable here

There a number of basic Autocad templates available on the internet Autodesk have one in a wizard form downloadable from here
and Jerry Winters has another here that goes with his book here
I have not had much luck using the Autodesk template so I use Jerry’s. It would make the transition from VBA to a lot easier for everyone if Autodesk provided a base Civil3d Template.
3. Setup some additional references to a couple of special Civil3d .dll files that are so called managed wrappers.
Effectively I think of these files as the gateway into Autocad and Civil3d that VB Express has to use to see what commands,methods and properties that are available inside Autocad and Civil3d to be used. There are two types of references .COM ones and .NET ones. You do not have to reference all of the reference if you do not need them. But it pays to as generally you end up needing them as alot of the time with Civil3d you have to use the older .COM wrapper to work with certain objects that are not available in the .NET wrapper yet.

See the reference below to Joshua Modglin where here explains the references required for Civil3d 2010 on in a series of posts.

Upgrading old code.
For those not new to VB any programs that you have written in the VBA (IDE) in the past can be brought forward into
Stephen Preston has a good video on the process here.  If your programs are basic and do not include userform/dialog boxes or events the process is reasonably simple if not its a bit more involved.


Online help for Basic Autocad
Shipped help files with Civil3d


This link to the page where you can download the video DevTV: Introduction to AutoCAD .NET Programming I was talking about to give you a feel of what programming involves to get you started.
There have been a couple of good AU papers over the years that summarise getting started in for Civil3d. If you are an Autocad subscription member you get the benefit of access to the current years papers and presentations. If not you get access to the pervious years papers and before. As most good papers are repeated over a number of years you will generally find earlier versions of the same paper with free access.
Strahimir Antoljak wrote this paper for AU 2008 and I found it particularly helpful in explaining the whole VBA verse IDE it’s a bit old now with regards to the reference files you have to pull into the IDE but it gives you the intro and explains the Civil3d basics pretty well.
Strahimir had the same paper listed for AU2009 but did not attend and Jerry Winters stepped up last minute and presented a version of the paper which gives plenty of tips on general programming in and but does not cover the Civil3d basics of the 2008 paper that you should read first.

Autodesk Developer Centre
Joshua Modglin formally of Engineering Efficiency now wrote a number of posts on getting started in VB programming with Civil3d 2010 here
Jerry Winters Website, Book and any of his AU presentation are good value.


Chris’s blog has given great practical examples over the years on how to do thing in VB with Civil3d
Kean’s blog also gives good examples of now to do various things with Autocad mostly written in C# however
This book gives the basic introduction to vb
This book gives great tips on using Microsoft Visual Studio
Also this book is autocad specfic althrough VBA based good but better yet all the source code in the book for doing basic things in VBA is downloadable at this webpage 
This book gives a good overview of doing the general basics in and the programming environment.
Builds on the last book in a bit more depth.
Gives a good summary of programming in general especially good is the chapter on what classes are and the intro to structuring programs
Hope this helps you on the way to getting started in programming with


  1. This link is down:

  2. Really Nice Information,Thank You Very Much For Sharing.Web Designing Company


Related Posts Plugin for WordPress, Blogger...