View Categories

How to get AutoCAD COM Interop API working

6 min read

Motivation #

Those who are coming from AutoCAD/Civil 3D .Net programming background might not realize that in addition to .Net API, AutoCAD/Civil 3D also provides COM object API. Both covers incomplete, not-always-overlapping sets of the vast AutoCAD/Civil 3D API ecosystem.

Getting started with .Net API programming is easy; tutorial abounds. But what is less well known is how to get COM API working and going. The guide here aims to fill this void.

This post is written, mainly based on the excellent discussions I had with a few Civil 3D veterans, such as norman.yuan and Jeff_M. I feel that there are a lot of insightful discussion points that should be presented and crystalized in a systematic manner for Google to discover, and for others to learn from in the future. This is the main motivation.

And of course, it’s important to preserve the knowledge, in case Autodesk decides to randomly delete old posts again. This is the secondary motivation.

Prerequisites #

  • Civil 3D 2025
  • Visual Studio 2022

Starting Point #

So, let’s start with the official guide and tutorial. At a first glance, it comes with explanation and code samples, so great! We can just learn from there, right?

Well, no. Not directly. It’s written in VB.Net, but that’s a minor quibble which I won’t want to go into.

A bigger problem is that it’s outdated and leaves a lot of things unexplained. You certainly can’t get the code snippet to work. It just won’t run in your Visual Studio. So let’s try to fill in the gap here.

At the end of this blog post, you shall be able to get working code running in your Visual Studio. Trust me.

Setting up the Project References #

To run COM Interop related project, you might have add reference to the COM Interop DLLs first. Fortunately this is not something that we need to do. AutoCAD comes with enough wrapper DLLs that you can just Add as Reference like a normal .NET DLL.

For the purpose of this exercise, you will have to add the following dlls to the References:

  • Autodesk.AECC.Interop.Land.dll
  • Autodesk.AutoCAD.Interop.Common.dll
  • Autodesk.AEC.Interop.Base.dll
  • Autodesk.AEC.Interop.UIBase.dll
  • Autodesk.AECC.Interop.Land.dll
  • Autodesk.AECC.Interop.UiLand.dll
  • Autodesk.AECC.Interop.Survey.dll

This is how it looks like

Civil 3D COM Interop DLL Assembly references

To summarize, the Autodesk.AEC.Interop.*.dll is the main assembly where all the logic lies, you can’t remove them at all. But Autodesk.AECC.Interop.*.dll ( notice the extra C) is the wrapper DLLs on top of them, that will give you intellisense and early binding to ease your development work. They can be optional; see more below.

If you are also doing COM Interop development for Gravity Pipes, then you need to add additional two dlls, Autodesk.AECC.Interop.Pipe.dll and Autodesk.AECC.Interop.UiPipe.dll

Those assemblies can be found at your AutoCAD program folder or subfolders ( specifically, the ACA and the C3D subfolder).

There are two ways you can call the COM Interop AutoCAD/Civil 3D dlls: the early binding access way, or the late binding access way.

Early Binding Access #

Early binding means that the compiler resolves method calls and property access at compile time. The immediately benefit is that you get the intellisense – you can know what methods, properties and fields a class has, even as you are typing out the code, no need to wait until the program crashes at runtime only to find out that you mistype a method name. This is a huge productivity boost, who wouldn’t want to detect and fix their errors early? And even if you mistype a method’s name, your program simply just won’t compile until you fix the error.

The only downside is that you need to reference additional interop assemblies. In our case there, it’s the dlls that have the name like Autodesk.AECC.Interop.*.dll. So here’s the full code

   using Autodesk.AECC.Interop.Land;
   using Autodesk.AECC.Interop.UiLand;
   using Autodesk.AutoCAD.Interop.Common;
   using Autodesk.AutoCAD.Interop;
   using Autodesk.AutoCAD.Runtime;

   [CommandMethod(nameof(EarlyBindingCOMObject))]
   public void EarlyBindingCOMObject()
   {
       var aeccApp = new AeccApplication();
       
          
       aeccApp.Init((AcadApplication)Autodesk.AutoCAD.ApplicationServices.Application.AcadApplication);
       AcadDocument activeDocument = aeccApp.ActiveDocument;
       var doc= (AeccDocument)activeDocument;
       var count= doc.GeneralCurveLabelStyles.Count;
       var db = (AeccDatabase)activeDocument.Database;
       AcadGroups groups = db.Groups;
       var groupPlotStyles = db.GroupPlotStyles;

   }

You can copy and paste the above code to VS 2022 and verify that it works. And use a debugger to step through the code and examine the fields and variables.

Take note that the casting to AeccDocument and AeccDatabase are absolutely essential, or else you won’t get the full intellisense. And you should ignore in this case whenever compiler is complaining about Suspicious Casting when you convert the AcadDocument to AeccDocument. Usually the compiler is right when it complains about this issue, but in this COM interop case, the complain is misplaced. I can’t figure out why.

Late Binding Access #

Contrary to Early binding, late binding doesn’t require you to reference to assemblies like Autodesk.AECC.Interop.*.dll. But the price you pay is that you don’t get the intellisense, which means you simply can’t figure out what are the properties, methods or fields a class has in your VS IDE during typing time.

But still there are a few days that you can figure it out. One way is via Object Browser, as shown below:

Object Browser

Another, more sure fire way is to discover the properties and all via runtime, by just stepping into the code, and observe the dynamic view at the Watch window to retrieve all the properties, as shown below:

Debugger Watch Window

You can then use the dynamic keyword to bypass the compile type checking and to just type out whatever methods/properties you want without compiler complaining or stopping you from running the program.

Here’s the full code snippet that was constructed by watching the outputs in the debugger watch window:

using Autodesk.AutoCAD.Interop.Common;
using Autodesk.AutoCAD.Interop;
using Autodesk.AutoCAD.Runtime;

[CommandMethod(nameof(LateBindingCOMObject))] 
public void LateBindingCOMObject()
{
        var oAcadApp = (AcadApplication)Autodesk.AutoCAD.ApplicationServices.Application.AcadApplication;
        var sCivilAppName = "AeccXUiLand.AeccApplication.13.7";

        dynamic oCivilApp = oAcadApp.GetInterfaceObject(sCivilAppName);
        dynamic activeDocument = oCivilApp.ActiveDocument;
        dynamic generalCurve = activeDocument.GeneralCurveLabelStyles;
        int count = generalCurve.Count;
        AcadGroups groups = activeDocument.Database.Groups;
        dynamic groupPlotStyles = activeDocument.Database.GroupPlotStyles;

}

Notice that there is no need for the Autodesk.AECC.Interop.*.dll reference? Because it’s late binding.

But how do I know which AeccXUiLand.AeccApplication.13.x? #

The answer: you can go to C:\Program Files\Common Files\Autodesk Shared folder, and look for folder Civil Engineering 13X where X is meant for “13.X”.

Powered by BetterDocs

× Help