VB.NET AutoCAD® Template

William Forty
William Forty

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!


Comments

Hans Priya
2011-03-17 05:43:25

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

Will
2011-03-22 13:00:26

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

RANJAN
2011-05-08 09:33:39

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

Will
2011-05-10 06:45:47

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.

Ajit Joshi
2013-03-23 05:55:37

Can you help me to connect vb.net to autocad 2013

Pardus
2014-05-29 09:30:20

Hi I have big problems with the transition from VBA to VB.NET I want to ask. if it is possible to debug an application online when writing VB.NET like in the old built-in VBA. Without debuging Dll and running through NETLOAD thanks. Pardus. Czech. R.

Will
2014-07-21 11:48:30

It is much more difficult to debug VB.NET than with VBA because as you rightly point out, you have to use a DLL. I've actually been writing my own programming language so that you don't have to keep using NETLOAD to write your simple scripts. More on this soon.

Neelima
2014-07-24 06:16:38

Thanks Will. Its working.