利用C#進行AutoCAD的二次開發(二)

利用C#進行AutoCAD的二次開發(二)<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

C#才鳥

       大家好,今天我繼續給各位介紹利用C#進行AutoCAD的二次開發。在這一講中,主要介紹上一講例子中存在的問題。

         在上一次的例子中我是通過引用AutoCAD 2004 Type Library來進行C#AutoCAD之間的通信,但這種方法存在兩個致命的缺點。第一個缺點是每次調試程序的時候C#都要重新啓動AutoCAD,如果調試的次數非常多(比如跟蹤錯誤然後調試),那麼編程的效率就很低,因爲啓動一次CAD還是需要較長的時間。相對於第一個缺點,第二個缺點則更要命。由於.NET本身的問題,Interop.AutoCAD.dll文件(就是通過它才實現了C#AutoCAD之間的通信)存在着一些bug,因此雖然有時你的代碼是完全正確的,但C#編譯器還是拋出莫名其妙的錯誤。那不是完蛋了嗎?我曾經有一階段就因爲這兩個要命的東東差一點放棄了C#而想改學ObjectArx了,呵呵,不過還是運氣好,我偶爾一次在網上看了一篇外國人寫的文章,他專門介紹了這兩個問題的解決辦法。下面就來解決這兩個問題。

         首先來看第二個難題,按以下步驟來進行:

1.        隨便用Visual Studio .NET建立一個C#應用程序,然後按照上一篇文章中的設置加入AutoCAD 2004 Type Library,然後不加入任何代碼,編譯你的程序。

2.        Visual Studio .NET命令行工具下用ildasm.exe(這個工具可以在Visual Studio .NET安裝光盤中找到)把Interop.AutoCAD.dll文件(這個文件在步驟1中生成的項目的Bin/Release文件夾中)編譯成中間語言Interop. AutoCAD.il。注意:在步驟1中建立的項目的編譯設置爲Release模式。

    ildasm.exe /source Interop.AutoCAD.dll /output=Interop. AutoCAD.il

    又要注意了:把ildasm.exeInterop.AutoCAD.dll放在同一目錄下。

3.在記事本中打開Interop. AutoCAD.il文件,然後查找結尾是“SinkHelper”而開頭爲 ".class private auto ansi sealed _DAcad“的語句,把語句中的private 改爲public,然後保存Interop. AutoCAD.il文件。

4.使用ilasm.exeInterop. AutoCAD.il文件編譯爲Interop.AutoCAD.dll文件,同樣是在Visual Studio .NET命令行工具下進行。

    ilasm.exe  /resource=Interop.AutoCAD.res /dll Interop.AutoCAD.il /output=Interop. AutoCAD.dll

 Interop.AutoCAD.res文件是在步驟1中生成的。

5.顯然你不願意每次編寫應用程序時都通過上一篇文章中介紹的方法來加入Interop. AutoCAD.dll,那太麻煩了。你可以用下面的方法來讓程序自動加入該文件:找到C:/Program Files/Microsoft.NET/ Primary Interop Assemblies 文件夾,然後把上面生成的

Interop.AutoCAD.dll文件拷貝進去。     

       好了,第二個問題解決了,接下來看第一個。

       VBA中,編程者可以使用GetObject函數來獲得當前活動的AutoCAD對象,但在C#中卻沒有,爲了這個函數我幾乎把MSDN給翻遍了,然後去各種C#論壇問各位高手,結果都沒得到解決,呵呵,可能國內使用C#的人比較少吧。還是在老外的論壇上看到了一篇就是講這個問題的文章才把這個難題給解決了。使用下面的語句就可以獲得當前活動的AutoCAD對象了:

   (AcadApplication)Marshal.GetActiveObject("AutoCAD.Application.16")

     (對於CAD2000CAD2002,則把16改爲15)

當然以上語句必須在AutoCAD打開的情況下才能使用,否則會發生錯誤,對於AutoCAD沒打開的情況,可以使用上一篇文章的方法來處理。完整的連接AutoCADC#的源程序如下所示:

using System;

using AutoCAD;

using System.Runtime.InteropServices;

namespace AcadExample

{

   public class AutoCADConnector : IDisposable

   {

      private AcadApplication _application;

      private bool _initialized;

      private bool _disposed;

      public AutoCADConnector()

      {

         try

         {

            // Upon creation, attempt to retrieve running instance

            _application = (AcadApplication)Marshal.GetActiveObject("AutoCAD.Application.16");

         }

         catch

         {

            try

            {

               // Create an instance and set flag to indicate this

               _application =  new AcadApplicationClass();

               _initialized = true;

            }

            catch

            {

               throw;

            }

         }

      }

      // If the user doesn't call Dispose, the

      // garbage collector will upon destruction

      ~AutoCADConnector()

      {

         Dispose(false);

      }

 

      public AcadApplication Application

      {

         get

         {

            // Return our internal instance of AutoCAD

            return _application;

         }

      }

             

      // This is the user-callable version of Dispose.

      // It calls our internal version and removes the

      // object from the garbage collector's queue.

      public void Dispose()

      {

         Dispose(true);

         GC.SuppressFinalize(this);

      }

 

      // This version of Dispose gets called by our

      // destructor.

      protected virtual void Dispose(bool disposing)

      {

         // If we created our AutoCAD instance, call its

         // Quit method to avoid leaking memory.

         if(!this._disposed && _initialized)

            _application.Quit();

            

         _disposed = true;        

      }

   }

}

利用Visual Studio.net 把上面的程序編譯成一個類庫,你就可以在以後的程序中使用它了,下面的這個例子說明了它的用法。(首先把AcadExample類庫包含在項目中)

using System;

using AcadExample;

using AutoCAD;

namespace ConsoleApplication6

{

   class Class1

   {

      [STAThread]

      static void <?xml:namespace prefix = st1 ns = "urn:schemas-microsoft-com:office:smarttags" />Main(string[] args)

      {

         using (AutoCADConnector connector = new AutoCADConnector())

         {

            Console.WriteLine(connector.Application.ActiveDocument.Name);

         }

         Console.ReadLine();

      }

   }

}

這個例子是在C#窗口中顯示AutoCAD中當前文檔的標題。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章