Forum Discussion

soubhagya's avatar
soubhagya
Copper Contributor
Jan 22, 2025

Connection between .NET and AUTOCAD ELECTRICAL

I want to connect .NET with Autocad Electrical . i am using ,

AutoCAD Electrical 2025.0.2 , Product Version : 22.0.81.0 , Built on: V.154.0.0 AutoCAD 2025.1.1

Visual Studio Community 2022 - 17.12.3

my system is x64 bit

 

I am creating a console application in .NET 8.0 Runtime with C# language , Added dll accoremgd , acdbmgd, acmgd, Autodesk.AutoCAD.Interop . Also in .NET in Solution Explorer , in project references , i have made "Copy Local" property as False.

 

using System;
using Autodesk.AutoCAD.Interop;
using Autodesk.AutoCAD.Runtime;
using System.Runtime.InteropServices;
using Autodesk.AutoCAD.ApplicationServices;

namespace AutoCADElecDemo
{
    class Program
    {
        static void Main(string[] args)
        {
            AcadApplication acadApp = null;
            const string progId = "AutoCAD.Application.25"; // Adjust for your AutoCAD version

            try
            {
                // Get a running instance of AutoCAD
                acadApp = GetActiveAutoCAD(progId);
            }
            catch (System.Exception ex)
            {
                Console.WriteLine($"Error initializing AutoCAD: {ex.Message}");
                return;
            }

            try
            {
                // Ensure AutoCAD is visible
                acadApp.Visible = true;
                Console.WriteLine("AutoCAD is now running.");

                // Register for the BeginQuit event
                Application.BeginQuit += OnBeginQuit;

                // Keep the application running to monitor AutoCAD's state
                Console.WriteLine("Press Enter to exit...");
                Console.ReadLine();
            }
            catch (System.Exception ex)
            {
                Console.WriteLine($"An error occurred: {ex.Message}");
            }
            finally
            {
                // Unregister the event handler before exiting
                Application.BeginQuit -= OnBeginQuit;
            }
        }

        static AcadApplication GetActiveAutoCAD(string progId)
        {
            try
            {
                var comObject = Marshal.GetActiveObject(progId);
                Console.WriteLine($"Connected to AutoCAD: {progId}");
                return (AcadApplication)comObject;
            }
            catch (System.Exception ex)
            {
                Console.WriteLine($"Error accessing AutoCAD: {ex.Message}");
                throw;
            }
        }

        static void OnBeginQuit(object sender, EventArgs e)
        {
            Console.WriteLine("AutoCAD is being closed.");
        }
    }
}


For above code, my application comes in break mode "Your app has entered a break state, but no code is currently executing that is supported by the selected debug engine (e.g. only native runtime code is executing)."

and i am getting below error : 
System.IO.FileNotFoundException: 'Could not load file or assembly 'accoremgd, Version=25.0.0.0, Culture=neutral, PublicKeyToken=null' or one of its dependencies. The system cannot find the file specified.'

dll path is also correct.

i also tried same code with .NET Framework 4.7.2 and 4.8 targeting pack , but getting same error.

How to load accoremgd.dll properly so that this application can use autocad accoremgd functions properly.

2 Replies

  • nekopoiapk's avatar
    nekopoiapk
    Copper Contributor

    C# Tutorial, we will learn about https://w3href.com/c-sharp-tutorial C# and also learn Windows Form Basic & Console Application using C# (Sharp) like data types, and OOP concepts.

  • krueg's avatar
    krueg
    Copper Contributor

    You can only use those dll files in a dll you create and NETLOAD into AutoCAD

Resources