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.
Disclaimer
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 VB.net 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 VB.net:-

ONE
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 VB.net 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 VB.net technology then you should start by using the VB.net 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.

TWO
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 VB.net (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 VB.net 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. 
Autocad_Programming_Interface
Language
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. VB.net 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 VB.net 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 http://www.microsoft.com/express/Downloads/#2008-Visual-Basic
2. Install a Autocad VB.net 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 http://www.civildev.com/downloads.php.

There a number of basic Autocad templates available on the internet Autodesk have one in a wizard form downloadable from here
http://through-the-interface.typepad.com/through_the_interface/2009/06/a-new-project-wizard-for-autocad-net-development.html
and Jerry Winters has another here that goes with his book here http://www.vbcad.com/vbcaddownloads.htm
I have not had much luck using the Autodesk template so I use Jerry’s. It would make the transition from VBA to VB.net 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.
2011

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

2008
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 VB.net.
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.

References

AUTOCAD HELP FILES
Online help for Basic Autocad
http://docs.autodesk.com/ACD/2011/ENU/filesMDG/WS1a9193826455f5ff2566ffd511ff6f8c7ca-4875.htm
Shipped help files with Civil3d

FORUMS

http://forums.autodesk.com/t5/AutoCAD-Civil-3D-Customization/Dummy-s-guide-to-Visual-Basic-Express-2010-and-Civil-3D/m-p/2696037
Theswamp
http://www.theswamp.org/index.php?board=10.0

VIDEOS
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.
AU
There have been a couple of good AU papers over the years that summarise getting started in VB.net. 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. 

http://au.autodesk.com/?nd=class&session_id=2857
Strahimir Antoljak wrote this paper for AU 2008 and I found it particularly helpful in explaining the whole VBA verse VB.net 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.
http://au.autodesk.com/?nd=class&session_id=4993
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 VB.net and but does not cover the Civil3d basics of the 2008 paper that you should read first.

Autodesk Developer Centre
http://usa.autodesk.com/adsk/servlet/index?siteID=123112&id=1911627
WEBSITES
http://www.civil3d.com/
Joshua Modglin formally of Engineering Efficiency now http://www.inmotioncon.com/index.shtml wrote a number of posts on getting started in VB programming with Civil3d 2010 here
http://www.civil3d.com/2009/04/working-with-civil3d-2010-managed-net-api-101-1/
http://www.civil3d.com/2009/04/working-with-civil3d-2010-managed-net-api-101-2/
http://www.civil3d.com/2009/04/working-with-civil3d-2010-managed-net-api-101-3/
http://www.civil3d.com/2009/04/working-with-civil3d-2010-managed-net-api-101-4/
http://www.civil3d.com/2009/05/working-with-civil3d-2010-managed-net-api-101-5/
http://www.vbcad.com/
Jerry Winters Website, Book and any of his AU presentation are good value.
http://www.quuxsoft.com/
http://www.civil3dtools.com/

General
http://visualbasic.about.com/od/learnvbnet/a/LVBE_L1.htm

BLOGS
http://blog.civil3dreminders.com/
Chris’s blog has given great practical examples over the years on how to do thing in VB with Civil3d
http://through-the-interface.typepad.com/
Kean’s blog also gives good examples of now to do various things with Autocad mostly written in C# however
 
BOOKS
This book gives the basic introduction to vb
http://books.google.co.nz/books?id=1mzTLQAACAAJ&dq=visual+basic+in+easy+steps&cd=2
This book gives great tips on using Microsoft Visual Studio
http://www.amazon.com/Microsoft%C2%AE-Visual-Studio%C2%AE-Tips-PRO-Developer/dp/0735626405/
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
http://apress.com/book/view/1590595793 
This book gives a good overview of doing the general basics in VB.net and the programming environment.
http://www.amazon.com/Complete-Idiots-Guide-Visual-Basic/dp/0028642317
Builds on the last book in a bit more depth.
http://books.google.co.nz/books?id=xZBQAAAAMAAJ&q=beginning+programming+for+dummies&dq=beginning+programming+for+dummies
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 VB.net

2 comments:

  1. This link is down:
    http://www.civildev.com/downloads.php

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

    ReplyDelete

Related Posts Plugin for WordPress, Blogger...