如何制作独立的 autocad .net 应用程序

How to make a standalone autocad .net application

我正在尝试制作一个应用程序,它的一部分需要一些 autocad 功能。所以我在互联网上尝试了任何独立应用程序,但没有任何反应,我总是 return 一个错误。 (它不是插件,只需打开预装的autocad并绘制一些东西) 我的第一次尝试:

AcadApplication gbl_app = new AcadApplication();
AcadDocument gbl_doc = gbl_app.ActiveDocument;
gbl_app.Application.Visible = true;

这是第一行出现的错误。

An unhandled exception of type 'System.Runtime.InteropServices.COMException' occurred in SBDesign.exe

Additional information: 
Retrieving the COM class factory for component with CLSID {0D327DA6-B4DF-4842-B833-2CFF84F0948F} 
failed due to the following error: 80040154 
Class not registered (Exception from HRESULT: 0x80040154 (REGDB_E_CLASSNOTREG)).

我的第二次尝试:

 AcadApplication acAppComObj = null;
            const string strProgId = "AutoCAD.Application.20";

            // Get a running instance of AutoCAD
            try
            {
                acAppComObj = (AcadApplication)Marshal.GetActiveObject(strProgId);
            }
            catch // An error occurs if no instance is running
            {
                try
                {
                    // Create a new instance of AutoCAD
                    acAppComObj = (AcadApplication)Activator.CreateInstance(Type.GetTypeFromProgID(strProgId), true);
                }
                catch(Exception er)
                {
                    // If an instance of AutoCAD is not created then message and exit
                    System.Windows.Forms.MessageBox.Show("Instance of 'AutoCAD.Application'" +
                                                         " could not be created.");

                    return;
                }
            }

            // Display the application and return the name and version
            acAppComObj.Visible = true;
            System.Windows.Forms.MessageBox.Show("Now running " + acAppComObj.Name +
                                                 " version " + acAppComObj.Version);

这是错误:

Unable to cast COM object of type 'System.__ComObject' to interface type 'Autodesk.AutoCAD.Interop.AcadApplication'. 
This operation failed because the QueryInterface call on the COM component for 
the interface with IID '{10E73D12-A037-47E5-8464-9B0716BE3990}' 
failed due to the following error: 
No such interface supported (Exception from HRESULT: 0x80004002 (E_NOINTERFACE)).

那么我该如何制作一个独立的应用程序呢?

谢谢 a.h

IID {10E73D12-A037-47E5-8464-9B0716BE3990} 是 AutoCAD 2017 AcadApplication 的 IID,似乎未正确安装。

您需要使用 ProgId:AutoCAD.Application.20 并使用 Autodesk.AutoCAD.Interop.dll 和 Autodesk.AutoCAD.Interop.Common.dll 20.0.51.0 作为参考,以使用您的 AutoCAD 2015 安装。我认为您实际上使用的是 21.0.52.0 (AutoCAD 2017)。