About
.NET / AutoCAD·

AutoCAD .NET Plugin Development: From Project Setup to Publishing

A comprehensive guide to developing AutoCAD plugins using the .NET API (with C#), covering everything from environment setup to app store publishing.

Developing an AutoCAD plugin using the .NET API (with C#) involves several key steps: setting up the development environment, writing the plugin code, debugging it, configuring entitlement (licensing) for distribution, and finally publishing it to the Autodesk App Store. This guide walks you through each step, providing detailed instructions and best practices for each phase.


1. Project Setup

Before you can write any code, you need to set up your development project correctly. Here’s how to create a new AutoCAD .NET plugin project in Visual Studio:

  • Install Prerequisites: Ensure you have Visual Studio (e.g. 2022 or later) and the ObjectARX SDK installed for your target AutoCAD version. The ObjectARX SDK provides the reference assemblies and tools needed to build AutoCAD plugins.
    • AutoCAD 2025 and later require .NET 8.0 as the target framework.
    • AutoCAD 2024 supports .NET 4.8.
    • AutoCAD 2020+ only supports 64-bit .NET applications (target x64).
  • Create a New Project: Open Visual Studio and create a new project. Use the AutoCAD Version C# Plug-in template. If you don’t see this template, ensure you installed the ObjectARX SDK.
  • Configure Project Settings:
    • Target Framework: Set to .NET 8.0 for AutoCAD 2025, or .NET 4.8 for older versions.
    • Platform Target: Set to x64.
    • Output Type: Class Library (.dll).
    • References: Ensure references to AutoCAD assemblies (AcMgd.dll, AcCoreMgd.dll, etc.) are set to Copy Local = False.
  • Understand the Project Structure: The template creates a class with a method marked with [CommandMethod]. This is your entry point. An AddInManifest.xml file is used by AutoCAD to load your plugin.

2. Coding the Plugin

Once your project is set up, you can write the code for your plugin.

Use AutoCAD API Namespaces

Include the necessary using directives:

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

Implement Commands

All commands must be methods marked with the [CommandMethod] attribute.

[CommandMethod("MyCommand")]
public static void MyCommand()
{
    Document doc = Application.DocumentManager.MdiActiveDocument;
    Database db = doc.Database;
    Editor ed = doc.Editor;

    using (Transaction tr = db.TransactionManager.StartTransaction())
    {
        BlockTable bt = (BlockTable)tr.GetObject(db.BlockTableId, OpenMode.ForRead);
        BlockTableRecord modelSpace = (BlockTableRecord)tr.GetObject(bt[BlockTableRecord.ModelSpace], OpenMode.ForWrite);

        // Create a new line
        Line line = new Line(new Point3d(0, 0, 0), new Point3d(10, 0, 0));
        modelSpace.AppendEntity(line);
        tr.AddNewlyCreatedDBObject(line, true);

        tr.Commit();
    }
}

Handle User Input

Use the Editor object to prompt the user for input.

PromptPointOptions ppo = new PromptPointOptions("\nSpecify point: ");
PromptPointResult ppr = ed.GetPoint(ppo);
if (ppr.Status == PromptStatus.OK)
{
    Point3d point = ppr.Value;
    ed.WriteMessage("\nPoint selected: " + point.ToString());
}

3. Debugging the Plugin

  • Launch AutoCAD from Visual Studio: Set your plugin project as the startup project. In the Debug settings, select "Start external program" and point to the acad.exe executable. Press F5 to start.
  • Set Breakpoints: Use breakpoints in Visual Studio to inspect variables as AutoCAD runs your commands.
  • Common Errors:
    • Plugin Not Loading: Check if the DLL is in a trusted path or check the SECURELOAD system variable.
    • Object Modification Errors: Ensure you have opened objects for Write mode within a transaction.

4. Entitlement Setup

Entitlement is the licensing mechanism for the Autodesk App Store.

  1. Register as a Publisher: Create an account on the Autodesk App Store.
  2. Obtain an App ID: Once your app is listed, you'll get a unique App ID.
  3. Integrate Entitlement Check:
public static async Task<bool> CheckEntitlement(string appId, string userId)
{
    string url = $"https://apps.autodesk.com/webservices/checkentitlement?userid={userId}&appid={appId}";
    using (WebClient wc = new WebClient())
    {
        string response = await wc.DownloadStringTaskAsync(url);
        dynamic json = JsonConvert.DeserializeObject(response);
        return (bool)json.IsValid;
    }
}

5. Publishing the Plugin

  • Prepare Distribution: Build the Release version of your DLL.
  • Create an App Package: Create a folder structure with your DLL and a PackageContents.xml file.
  • Submit to App Store: Upload your ZIP/Bundle to the Publisher Portal with screenshots and descriptions.

References

Leave a Comment