Getting started with AutoCAD and .NET
To get started with writing a .NET app for AutoCAD, download the ObjectARX SDK for AutoCAD 2007. Contained within the samples/dotNet folder of the SDK are a number of helpful samples showing how to use various features of the managed API to AutoCAD.
Incidentally, the project files etc. are generally saved in the version of Visual Studio that is recommended to build ObjectARX (C++) apps for that version of AutoCAD. So the projects in the ObjectARX 2006 SDK will be for Visual Studio .NET 2002, and in ObjectARX 2007 they will be for Visual Studio 2005. These specific Visual Studio versions are not strictly necessary to use the managed APIs for the respective versions of AutoCAD (that's one of the beauties of .NET, in that it helps decouple you from needing a specific compiler version), but for consistency and our own testing we maintain the parity with the version needed to build ObjectARX/C++ applications to work with AutoCAD.
The simplest sample to get started with is the classically named "Hello World" sample, which in this case is a VB.NET sample. I won't talk in depth about any of the samples at this stage; I'm going to focus more on how to use the ObjectARX Wizard to create a VB.NET application.
In the utils\ObjARXWiz folder of the ObjectARX SDK, you'll find the installer for the ObjectARX Wizards (ArxWizards.msi). I'm using the Wizard provided with the ObjectARX SDK for AutoCAD 2007.
Once installed, you can, of course, create new ObjectARX/C++ projects; we use this tool all the time in DevTech to help generate new SDK samples as well as diagnose API issues reported to us. A relatively new feature is the AppWizard for VB.NET and C#. This is visible when you ask Visual Studio 2005 to create a new project:
Once you select "OK", you will be shown a single page to configure your project settings - all very simple stuff:
Selecting "Finish" will set up the required project settings and generate the basic code needed for your application to define a single command called "Asdkcmd1".
Before we look into the code, what has the Wizard done? It has created a Class Library project, adding a couple of references to the DLLs defining the managed API to AutoCAD. If you select "Add Reference" on the created project, you can see them in the "Recent" list:
There are two AutoCAD-centric references listed here: acdbmgd.dll, which exposes the internal AcDb and supporting classes (common to both AutoCAD and RealDWG), and acmgd.dll, which exposes classes that are specific to the AutoCAD application.
So now let's look at the code. It's really very straighforward - it imports a namespace (which saves us from prefixing certain keywords such as CommandMethod with "Autodesk.AutoCAD.Runtime.") and then defines a class to represent our application module. This class (AdskClass) defines callbacks that can be declared as commands. This is enough to tell AutoCAD that the Asdkcmd1 method needs to be registered as a command and should be executed when someone types that string at the command-line.
Imports Autodesk.AutoCAD.Runtime
Public Class AdskClass
' Define command 'Asdkcmd1'
<CommandMethod("Asdkcmd1")> _
Public Sub Asdkcmd1()
' Type your code here
End Sub
End Class
And that's really all there is to it. To see it working, add a function call to the command function, such as MsgBox("Hello!"), build the app, and use AutoCAD's NETLOAD command to load the resultant DLL. When you type in ASDKCMD1 at the command line, your custom command defined by VB.NET should be called.
Time for some quick credits: a number of the DevTech team have been involved over the years in developing the ObjectARX Wizard (including the recent versions that support .NET) but the chief architect of the tool is Cyrille Fauvel, who is part of the DevTech EMEA team and is based in France.




Subscribe via RSS
... thank you for getting me started in Object ARX! I've used AutoLISP since '93, VB/VBA since 2000, and stated using .NET in 2002. I've always want to use ARX and can't wait to get going -- thanks again!
Posted by: Lanny | December 14, 2006 at 05:41 PM
My pleasure, Lanny! :-)
Kean
Posted by: Kean | December 14, 2006 at 05:43 PM
Hi Kean,
Great post, I'm new in developing programs in Visual Studio for using in Autocad and I would like to know the other way around, i.e, if it's possible to programme in .NET in the AutoCad environment...until now we had had always to use VBA or LISP, but I don't know if we can use VB.NET and use all the power of the .NET Framework in Autocad.
Best Regards
Juan Carlos
Posted by: Juan Carlos | March 21, 2007 at 11:09 PM
Hi Juan,
You can absolutely use Visual Studio - even one of the Express Editions - to write managed assemblies that load and run inside AutoCAD.
There currently isn't an Integrated Development Environment (IDE) inside AutoCAD that supports .NET development (such as VSTA - Microsoft's version of Visual Studio that can be embedded by applications), but that shouldn't stop you from writing and debugging .NET code inside AutoCAD.
Best regards,
Kean
Posted by: Kean | March 22, 2007 at 08:30 AM
Hi Kean,
Can i create dockable forms this way also?
In the past i used delphi to develop activex components and accont16.arx to run them.
Now i have to pay for accont17.arx so i'm looking for something else.
Marcel
Posted by: Marcel Prösch | May 02, 2007 at 07:52 PM
Hi Marcel,
Check out the DockingPalette sample on the ObjectARX SDK - this will show you how to create a dockable window through .NET. I believe the .NET Labs on the AutoCAD Developer Center (http://www.autodesk.com/developautocad) also covers this topic.
Regards,
Kean
Posted by: Kean | May 04, 2007 at 02:00 AM
Hi Kean,
Since from 3 years i am using Lisp, VBA. Now i would like to create AutoCAD Customize Programs through VB.Net, Is there any Complete reference for Beginners? Because i gone threw your post's its very use full for experienced .Net Programmers.
Please give me an idea ASAP.
Thanks,
Senthil Prabu B R
Banglore.
Posted by: Senthil Prabu B R | June 04, 2007 at 08:33 AM
Gr8 tut.... I did worked on this tech during 2000. Easily recollect with this stuff...
ThanX...
Sunilkumar S.
Mumbai-India
Posted by: Sunilkumar Shinde | June 26, 2007 at 12:17 PM
how can getting start autocad.....?
Posted by: aneeshkrishna | July 30, 2007 at 09:30 AM
Can you rephrase the question?
Kean
Posted by: Kean | July 30, 2007 at 09:38 AM
You've been kicked (a good thing) - Trackback from CadKicks.com
http://www.cadkicks.com/adkautocad/Getting_started_with_AutoCAD_and_NET
Posted by: CadKicks.com | November 10, 2007 at 02:42 PM
I just stated learning VB.net the past couple weeks after just learning some AutoLisp a few months ago. I liked the article and hope to read more like it to help me along the way. It was very easy to read and started with the basic which I need.
Posted by: chris | November 21, 2007 at 01:25 AM
Hi Kean!
I am working in the C#.net applications and we need to know how to import the autocad files like DWG files and also we want to load the dwg files into the .net applications..
Any ideas?
Thanks in Advance!
Posted by: parames | February 21, 2008 at 09:10 PM
There are two ways to "import" DWG files to be accessible in a .NET application: one is to load them in the editor (use the DocumentManager for that) the other is to create a Database object and use ReadDWG file to load it in. This second approach creates an "in-memory" or "side" database - the DWG is loaded and accessible but not open in the editor. With either approach you can then open the various objects in the DWG file - most commonly by getting the block table and then the model-/paper-space block table records.
I hope this helps,
Kean
Posted by: Kean | February 22, 2008 at 03:02 PM
Kean, just getting started. Installed Visual Studio 2008, downloaded ObjectARX 2008 SDK. Ran the "ArxWizards.msi" file, it said it was looking for Visual Studio 2005 only.
Can I use VS2008 for Vb.NET development?
Posted by: R.K. McSwain | October 16, 2008 at 12:28 AM
You certainly can, but the Wizard is primarily for ObjectARX development and so only installs on Visual Studio 2005. You could probably fool the installer using the technique in this post, and then just use the VB.NET project template (the current .NET portion of the Wizard is very basic - just project setup, which is already straightforward in .NET).
I probably wouldn't bother, on balance - there's not much to gain for all that effort. This post may actually be of more use to you.
Kean
Posted by: Kean | October 16, 2008 at 10:01 AM
Is there a way to dynamically create commandsmethods at runtime in VB.Net?
Posted by: Chuck Dodson | October 16, 2008 at 06:31 PM
There are two ways I know of to do this:
1. P/Invoke the C++ API.
2. Use the unsupported Autodesk.AutoCAD.Internals.Utils.Add/RemoveCommand (from acdbmgdinternal.dll).
Kean
Posted by: Kean | October 16, 2008 at 09:04 PM
Hi Kean,
Love the blogs, I have spent a lot of time reading through them lately.
This topic covers opening .net from AutoCAD and creating a new command. I want to do the opposite, open AutoCAD 2008 from a .net (3.5)/VB Express form and issue existing AutoCAD commands to create and populate a 2-3 page .dwg based on user input from the form. I am relatively new to both AutoCAD and VB, any idea where I can find information or tutorials that would help me along this path? I already have Jerry Winters book and have covered about the first 7 chapters, but it seems to also work from the AutoCAD side. Or am I missing something?
Thanks.
Posted by: Joey | November 26, 2008 at 04:16 PM
Hi Joey,
This post should be of help.
Kean
Posted by: Kean | November 26, 2008 at 04:39 PM
Examples in C#: http://devdump.wordpress.com/2009/03/04/autocad-and-net/
Posted by: Tom | March 04, 2009 at 10:29 PM
Hi Kean,
I want to use parameters for this function. How i can declare parameters in the CommandMethod instruction ?
[Autodesk.AutoCAD.Runtime.CommandMethod("SetCustom")]
public void SetCustom(string varName, string varValue)
{
Database db = Application.DocumentManager.MdiActiveDocument.Database;
DatabaseSummaryInfoBuilder infoBuilder = new DatabaseSummaryInfoBuilder(db.SummaryInfo);
infoBuilder.CustomPropertyTable.Add(varName, varValue);
db.SummaryInfo = infoBuilder.ToDatabaseSummaryInfo();
}
Thank you.
Posted by: Marcel Martial | April 27, 2009 at 10:05 PM
Hi Marcel,
This question comes up from time to time (which makes me think it deserves a dedicated post).
You have a choice: you can use the LispFunction attribute to create a LISP-callable function - which means you would need to call it using (setcustom "name" "value") - or otherwise you can use the Get* functions (as described in this comment) to let you pass the arguments using SendStringToExecute() or SendCommand().
Regards,
Kean
Posted by: Kean Walmsley | April 28, 2009 at 09:05 AM
I am starting on a project in autocad and have try VB & C++ helloworld in dotNet and can't get them to work I am using 2008 AutoCAD & DotNet. Is there anything in these version that is keeping these from working? Could you send me a completed program that works in C++.
Posted by: rodbiser | May 12, 2009 at 12:41 AM
There's nothing that should be a problem, assuming you're creating a Class Library (a .DLL for .NET and a .ARX for C++) and are loading them via NETLOAD or ARXLOAD (there are other ways, but I won't go into them here).
I suggest working through the resources provided on the AutoCAD Developer Center, especially the ObjectARX and AutoCAD .NET Labs and all the samples that ship with the ObjectARX SDK.
Kean
Posted by: Kean Walmsley | May 12, 2009 at 09:46 AM