The Coding Draftsman

Changing the way you draft through code!

Create a project using the 2015 Dot Net Wizard

  • Launch your Visual Studio application.  Unless you have changed your setting it should open up to the Start Screen.

Visual Studio Community Edition 2013 Start Page

  • On the left hand side of the start screen you'll find the "New Project.." link. The new project dialog box is displayed (see below).

Visual Studio Community Edition 2013 - New Project

Select the Autodesk heading on the under the Visual C# templates heading and select the AutoCAD 2015 CSharp plug-in.  Enter "Hello From TCD" in Name: text box.

Click the OK button. The "AutoCAD .NET Wizard Configurator" dialog box is displayed.

  • Specify the location of the AcMgd.dll file.  If you downloaded the ObjectARX SDK you can find the file there if you have not however you can use the file that was installed with your AutoCAD Application.  This is generally located "C:\Program Files\Autodesk\AutoCAD 2015". 
  • Specify the location of the AutoCAD executable location.  This is the good old acad.exe file and it is generally located "C:\Program Files\Autodesk\AutoCAD 2015".
  • The AutoCAD Database (AcDbMgd.dll) AutoCAD (AcMgd.dll) and AutoCAD Core (AcCoreMgd.dll) is checked automatically.  These files are required for any dot net application.  All the other checkboxes are used to COM development. 
  • Click the OK button and your project has been created.

We now have a project created.  So what does using the Wizard do for us?  First it adds all your reference files:

Visual Studio Community Edition 2013 - References Files Created using the Wizard.

And second it created two classes for us.  MyCommand.cs is our class where we will create our hello command.  The second class is the myPlugin.cs.  This class is used to initialize objects when AutoCAD is launched.

Here is the code created by the wizard:

We however are going to erase everything between the Opening and Closing brackets of the MyCommands Class.  So the code should now look like this.

Now we are going to add some code that will create a new command for AutoCAD and introduce you to the command line editor.

The code in line 20 Creates a command named "Hello".  Hello is text that the user will enter at the command line to launch this command.

The code in line 21 creates the code block that will execute when "Hello" is entered into the command line.  Lets take a closer look at the code public void hello().  public is the keyword that allows the AutoCAD applications to use this code block.  void is a c# keyword that means this code block will not return an object.  the hello() names the code block.  Later on we will create coding blocks that contain arguments within the parentheses.

The next line of code (line 23) I included just to introduce you to the c#.net framework class System.  If you are anything like me I got into programming through the Autodesk dot net API.  So I had to learn the basics of C# programming along with learning the AutoCAD API.  This can really become overwhelming.  So we'll be covering some of the basics as we advance through this site.

The code in line 24 uses the Editor object to write a message to the command line.  The line should read Hello "your user name" Welcome to The Coding Draftsman Website.

Now lets take a look at debugging our code.  In the Solution Explorer right click on the Properties heading and select Open from the menu.  Select the debug on the left menu.

In the Start Action section select the Start external program:  Set it to C:\Program Files\Autodesk\AutoCAD 2015\acad.exe.

In the Start Options set the working directory to C:\Program Files\Autodesk\AutoCAD 2015\UserDataCache.

Now we can run our software debug mode.  From the DEBUG menu select Start Debugging.  AutoCAD will launch.  To load the dot net application enter the command "NETLOAD" at the command line.  Browse to the debug folder where you saved your visual studio project (by default it will be saved in the C:\Users\%UserName%\Documents\Visual Studio 2013\Projects\Hello From TCD\Hello From TCD\bin\Debug\Hello From TCD.dll)

Type the command "HELLO" into the command line.  The result should be "Hello 'UserName' Welcome to The Coding Draftsman website." 


Creating a Project without the Dot Net Wizard

From the file menu select New > Project.

Visual Studio New Project Dialog Class Library

Select the Visual C# heading and select the Class Library from the templates list.

Enter the Name: "Hello From TCD No Wizard"

Click "OK" and this will create a basic Visual C# project.

Next we will add the references to the AutoCAD Dot Net API files.

Visual Studio Solution Explorer.

Visual Studio Solution Explorer.

In the solution explorer right click on the Reference heading and select Add Reference... from the menu.  The Reference Manager will dialog box will be displayed.

Visual Studio Reference Manager Dialog Box.

Click the browse button and add these files from this directory C:\Program Files\Autodesk\AutoCAD 2015

  • acmgd.dll
  • accoremgd.dll
  • acdbmgd.dll

The Reference Manager dialog should now look like this.

Visual Studio Community Edition Reference Manager

Adding COM Interactions

Now we'll add the reference files for the AutoCAD COM interactions.  Select the COM section on the left hand side of the dialog.  Then check the boxes for AutoCAD 2015 Type Library and AutoCAD/ObjectDBX Common 20.0 Type Library.  As shown below.

Visual Studio Reference Manager

Now we have a basic Visual C# project with a class named "Class1.cs".  Right click on the "Class1.cs", select rename and enter "HelloCommand.cs" as the new name.

See Namespaces & Using statements for more information about namespaces and using statements.

Add these using statements below the using System.Threading.Tasks; code.

using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.EditorInput;
using Autodesk.AutoCAD.Geometry;
using Autodesk.AutoCAD.Runtime;

Enter this code after the using statements and before the namespace Hello_From_TCD_No_Wizard code.

[assembly: CommandClass(typeof(Hello_From_TCD_No_Wizard.HelloCommand))]

Enter the following code within the public class HelloCommand brackets.

[CommandMethod("Hello")]
        public void hello()
        {
            string userName = System.Security.Principal.WindowsIdentity.GetCurrent().Name;
            Application.DocumentManager.MdiActiveDocument.Editor.WriteMessage("\nHello " + userName + "\nWelcome to The Coding Draftsman website!!!");
        }

The HellowCommand.cs should look like this.

Create a Project using the AutoCAD Nuget Package

Autodesk recently added the AutoCAD Nuget Package.  If you don't know what a Nuget package is you can read all about on their website www.nuget.org.  The short version is a Nuget Package is an installable package that adds API's to your project.

From the start page create a new class library project.

This will create a new blank project.

Visual Studio Community 2013 Solution Explorer

From the "Tools" menu select NuGet Package Manager > Manage NuGet Packages for Solution...

In the search box (in the upper right hand side of the dialog) box search for AutoCAD.

Select the AutoCAD.Net AutoCAD 2015 API.

Click on the Install button.

The Select Projects dialog box is displayed.

If you have multiple projects in the solution you can add this project to multiple projects at once.  Check the boxes for the projects to add the package to and click the OK button.

You will have to accept the Autodesk license in the following dialog box.

Click the "I Accept" button and the references files are now added to your project.

We now have the basic AutoCAD Add-on application setup for dot net.  You can now add the COM reference files if needed.

Right click on the reference heading in the solution explorer and select "Add Reference..." from the menu.

Select the COM section on the left hand side of the dialog.  Then check the boxes for AutoCAD 2015 Type Library and AutoCAD/ObjectDBX Common 20.0 Type Library.  As shown below.

Visual Studio Reference Manager.

Now the project is ready for coding!