如何使用ACAD 2010连接C#

时间:2012-07-09 14:33:28

标签: c# .net autocad

使用vs2008 C#,需要帮助连接autocad 2010,我按照以下步骤进行连接,但它给了我一个错误。

我必须添加一个引用,所以我转到引用>>添加引用>> [COM TAB]>> Autocad 2010类型库>> [确定]

我使用两个库:

using Autodesk.AutoCAD.Interop;
using Autodesk.AutoCAD.Interop.Common;

然后我使用以下代码连接AutoCAD 2010:

namespace Sample_CSharp_Acad_connect
{


 class Program
    {
        private static IAcadApplication oAcadApp = null;
        private static string sAcadID = "AutoCAD.Application.18";

        static void Main()
        {
            try  //get a running AutoCAD instance if avaialbale
            {
                oAcadApp = (IAcadApplication)System.Runtime.InteropServices.Marshal.GetActiveObject(sAcadID);
            }
            catch(Exception) //none found so start a new instance
            {
                System.Type AcadProg = System.Type.GetTypeFromProgID(sAcadID);
                oAcadApp = (IAcadApplication)System.Activator.CreateInstance(AcadProg);
            }
            if (oAcadApp != null)
            {
                oAcadApp.Visible = true; //could leave this false to hide Acad from the user
                //do whatever with Acad
                //oAcadApp.Quit();
            }
        }
    }

错误消息:获取组件CLSID的对象类COM时出错 enter image description here

2 个答案:

答案 0 :(得分:1)

COM?不要这样做。

AutoCAD有一个.NET API。有关Autodesk的资源,请参阅http://usa.autodesk.com/adsk/servlet/index?id=1911627&siteID=123112。另请参阅维基这里获取更多信息和链接嘉豪:https://stackoverflow.com/tags/autocad/info

答案 1 :(得分:0)

如何运行此代码?它是exe文件吗?据我所知,使用外部程序控制AutoCAD并不容易。通常存在可防止此类操作的起诉问题。

使用DLL访问AutocAD功能可能更容易。在这种情况下,可以直接访问AutoCAD的对象模型:

Dim theApp as Autodesk.AutoCAD.Interop.AcadApplications = Autodesk.AutoCAD.Interop.AcadApplication()
Debug.Print(theApp.Caption)

SDK包含大量可以加载到AutoCAD中的DLL示例。

并且:如果没有必要,请考虑使用.NET!