Search code examples
c#.netexcelcomautocad

Autocad .Net integration - successfully compiled DLL errors on calling a simple function from Excel


I am fighting with a project that needs an integration of Autocad in Excel. I compiled a DLL and I successfully referenced in in Excel but calling a simple function fails.                                         

There is no problem with the COM interface; the project has these ticked and I can successfully call a simple "hello world" test function from excel. I also have all the correct references in the C# project. The redundant references are needed for later work.

The function fails on this line:

var acDocMgr = Autodesk.AutoCAD.ApplicationServices.Core.Application.DocumentManager;

If fails regardless of the Autocad app being opened or not.

Please help.

using System.Collections.Generic;
using System.Runtime.InteropServices;
using System.Linq;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.Runtime;
using CadApp = Autodesk.AutoCAD.ApplicationServices.Application;
using Autodesk.AutoCAD.ApplicationServices;

[assembly: CommandClass(typeof(AutocadHandler.MyCommands))]

namespace AutocadHandler
{
    [ClassInterface(ClassInterfaceType.AutoDual)]


    public class MyCommands
    {

        public static void TestFunction()
        {                       
            string strFileName = "C:\\Users\\CORE I7\\Documents\\Drawing2XLS.dwg";
            var acDocMgr = Autodesk.AutoCAD.ApplicationServices.Core.Application.DocumentManager;
            acDocMgr.Open(strFileName, false);
            acDocMgr.MdiActiveDocument.Editor.WriteMessage("Hello Excel");
        }

    }

}

The error Excel returns is:

Run-time error '-2146233036 (80131534)': Automation error


Solution

  • Are you trying to run your code from within Excel, and trying to get Excel to open AutoCAD to manipulate the drawing? I don't think that will work. You can go the other way, open AutoCAD, load a plugin, and then feed information from AutoCAD via the API out to Excel. The AutoCAD API needs AutoCAD running (or ACCORECONSOLE, which is a commandline version of AutoCAD, but that requires some additional plumbing) to do anything with the drawing files. If its AutoCAD, not ACCORECONSOLE, your going to typically need at least one drawing open (the ..DocumentManager.MdiActiveDocument). You can then open other documents using the document manager, assuming that you have permissions to do so.

        /// <summary>
        /// Look through the Application's Document manager for a Document object with the given name.  If found return it,
        /// else open the drawing/Document and return it.
        /// </summary>
        /// <param name="name">The name to look for in the collection</param>
        /// <returns>An AutoCAD Document object.</returns>
        public static ACADApp.Document GetDocumentByName(string name)
        {
            try
            {
                foreach (ACADApp.Document doc in ACADApp.Application.DocumentManager)
                {
                    if (doc.Database.Filename.ToUpper() == name.ToUpper() || doc.Name.ToUpper() == name.ToUpper())
                    {
                        return doc;
                    }
                }
                return ACADApp.Application.DocumentManager.Open(name);
    
            }
            catch (System.Exception ex)
            {
                TBExceptionManager.HandleException(name, ex);
                return null;
            }
        }