2012-07-09 82 views
2

工作VS2008 C#,需要帮助连接到autocad 2010,我按照以下步骤连接,但它给了我一个错误。如何连接C#与ACAD 2010

我必须添加一个参考,所以我去引用>>添加参考>> [COM TAB] >> AutoCAD 2010的类型库>> [OK]

我用两个库:

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(); 
      } 
     } 
    } 

错误消息:错误地让对象类COM组件为CLSID enter image description here

+0

你得到了什么确切的错误? – 2012-07-09 14:42:41

+0

FrédéricHamidi感谢您的回复,获取组件CLSID的对象类COM出错 – shamim 2012-07-09 14:51:38

+2

看起来像AutoCAD类型库没有正确注册。你能否在'Program Files \ Common Files \ Autodesk Shared \ acax18enu.tlb'(或者你的等效本地化版本)上运行'regsrv32'? – 2012-07-09 15:11:36

回答

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!