Dynamo AutoCAD — Interop Connection with C# — Digitteck
Dynamo AutoCAD — Interop Connection with C#
dotnet·29 November 2018·3 min read

Dynamo AutoCAD — Interop Connection with C#

Retrieving live data from AutoCAD while working in Dynamo was a challenge for a long time. COM Interop is the fastest approach — it attaches to an already-running AutoCAD process through its registered COM interface, giving you access to documents, layers, model space, and entities without file exports. The limitation is that only types exposed through the COM interface can be marshalled; not every AutoCAD class is available.

What Is COM Interop

COM Interop is a .NET Framework technology that enables Component Object Model (COM) objects to interact with .NET objects. When data moves between managed and unmanaged code the CLR's interoperability marshalling service handles the type conversion automatically.

AutocadConnection Class

The connection class uses Marshal.GetActiveObject to attach to an open AutoCAD session. If no session is running it falls back to Activator.CreateInstance to launch a new one:

csharp
public class AutocadConnection
{
    private AcadApplication application;
    private AcadDocument    activeDocument;
    private AcadModelSpace  activeModelSpace;

    public AcadApplication Application  => application;
    public AcadDocument    ActiveDocument => activeDocument;
    public AcadModelSpace  ActiveModelspace => activeModelSpace;

    // Connects to the already-running AutoCAD process via COM Interop.
    // Falls back to launching a new instance if one is not running.
    public AutocadConnection(string progID = "AutoCAD.Application.20")
    {
        object acApp = null;

        try
        {
            // Get the running AutoCAD instance
            acApp = (AcadApplication)Marshal.GetActiveObject(progID);
        }
        catch
        {
            try
            {
                // Launch a new AutoCAD instance
                Type acType = Type.GetTypeFromProgID(progID);
                acApp = (AcadApplication)Activator.CreateInstance(acType, true);
            }
            catch
            {
                Console.WriteLine("Cannot create object of type \"" + progID + "\"");
            }
        }

        application      = acApp == null ? null : (AcadApplication)acApp;
        activeDocument   = acApp == null ? null : application.ActiveDocument;
        activeModelSpace = acApp == null ? null : application.ActiveDocument.ModelSpace;
    }
}

Dynamo Node

Wrap the connection class in a static Dynamo node so it can be dropped into the graph:

csharp
// Dynamo node wrappers — expose the connection to the Dynamo graph
public class Connection
{
    private Connection() { }

    [MultiReturn(new[] { "Connection" })]
    public static Dictionary<string, object> AutoCADConnection()
    {
        var connection = new AutocadConnection();
        return new Dictionary<string, object>
        {
            { "Connection", connection }
        };
    }

    [MultiReturn(new[] { "ActiveDocument" })]
    public static Dictionary<string, object> ActiveDocument(AutocadConnection connection)
    {
        return new Dictionary<string, object>
        {
            { "ActiveDocument", connection.ActiveDocument }
        };
    }
}

ProgId Reference

The progID defaults to AutoCAD 2015/2016 but can be passed explicitly for other versions:

  • "AutoCAD.Application.20" — AutoCAD 2015 / 2016
  • "AutoCAD.Application.20.1" — AutoCAD 2016
  • "AutoCAD.Application.21" — AutoCAD 2017
  • "AutoCAD.Application.22" — AutoCAD 2018

Add references to Autodesk.AutoCAD.Interop.dll and Autodesk.AutoCAD.Interop.Common.dll in your project.

Tags

.NETC#DynamoAutoCAD
digitteck

© 2026 Digitteck