VB.NET AutoCAD® Template

by Will on February 18, 2011

For those interested in AutoCAD® development, there are a few files that need to be referenced so that you can work with AutoCAD®. So, ideally, you’ll want to set up a basic template that you can use with all the basics set up ready for you. In this tutorial we will do just that.

I’m using visual Basic 2010 Express edition, but the process should be very similar for the latest versions of Visual Basic.

Firstly, create a new project. Select Class Library as your template. This type of file will compile to a .NET dll – just what we need for developing AutoCAD® tools.

Next, go to project properties, and then click on the references tab. There are two files you’ll need to add – AcMgd.dll, and AcDbMgd.dll. You can find these in your AutoCAD® directory – for example:

C:\Program Files\Autodesk\AutoCAD® Civil 3D® 2011

You may also have opted to download the ObjectARX libraries. If so, you can use the files located in here. Using these files allows you to develop for AutoCAD® without having to install AutoCAD® first. Whichever files you reference, ensure that the Copy Local property of both references is set to False. Because the files already ship with AutoCAD®, we do not want to be copying dll files all over the place as this can cause problems.

The next step requires you to save your project first, so do that. Once saved, go back to project properties and click on the Compile tab. Click on Advance Compile Options. Set the Target Framework to the version of the .NET framework that is suitable for the versions of AutoCAD® you are developing for. I have mine set to .NET framework 3.5, as I build my applications for AutoCAD® 2010 and above. Remember that .NET frameworks should be backward compatible, so if in doubt go for earlier versions rather than later versions, unless you have a specific reason not to do so.

We’re nearly there, but there is one last thing I like to do before saving as a template. Go back to your class, and we’re going to put in some default code, with some imports, and a subroutine with an associated command name that we will use when calling the command from the AutoCAD® command line:

Imports Autodesk.AutoCAD®.DatabaseServices
Imports Autodesk.AutoCAD®.Runtime
Imports Autodesk.AutoCAD®.Geometry
Imports Autodesk.AutoCAD®.ApplicationServices
Imports Autodesk.AutoCAD®.EditorInput
Imports Autodesk.AutoCAD®.Colors

Public Class Class1

<CommandMethod("COMMAND_NAME_HERE")> _
Public Sub COMMAND_NAME_HERE()

MsgBox("Hello World!")

End Sub

End Class

And that’s it! The final step is as simple as going File>Export Template, and following the wizard!

Hope this helps,

Will

P.s, sign up below to receive updates on my blog – don’t miss out on any tips like this!

Enter Your Mail Address

Be Sociable, Share!