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!

{ 4 comments… read them below or add one }

Hans Priya March 17, 2011 at 5:43 am

How to open autocad 2011 from vb.net? Please help with the complete code

Reply

Will March 22, 2011 at 1:00 pm

Really, for developing tools etc in AutoCAD, you want to compile to a dll and use NETLOAD to make them available to AutoCAD. If you really want to open AutoCAD from an external vb.net source, you want to try and create a new instance of AutoCAD using this old VB6 technique which I believe will still work:

Set Acad = GetObject(, “AutoCAD.Application”)
If Not Acad Is Nothing Then
‘Do stuff with Acad
Else
MsgBox “Could not link to AutoCAD 2011!”, vbCritical
End If

Reply

RANJAN May 8, 2011 at 9:33 am

Hi will,
I would like to build a stand alone exe file to create steel shapes in autocad 2009 using visual basic 2005………….can u please help me out with the procedures which i’m suppose to follow with a simple example….. I would be grateful if u come up with a new thread for this………plzzzzzzzzzzzzzzzz WILL u will………hope so

Reply

Will May 10, 2011 at 6:45 am

I’ll do a post on it soon, though the same principle as using Excel applies really, as you’re linking to an external process.

Reply

Leave a Comment

{ 1 trackback }

Previous post:

Next post: