2014-06-25 48 views
3

我正在編寫一個程序,使用C#中的Windows窗體來模擬3D對象,然後應該從.NET框架打開AutoCAD來繪製對象,並且我試圖找出最好的方式來做到這一點。從Windows窗體應用程序中打開AutoCAD

我以前試過opening AutoCAD from .NET using COM,但我不知道是否有可能是未在no such interface supported錯誤結束的方式,爲各種途徑我已經試過(打開圖紙,使用COM打開AutoCAD的,等)已經結束,沒有結果。有什麼建議麼?

回答

5

當我在做這種類型的事情,我剛開始的AutoCAD與System.Diagnostics.Process,並確保在AutoCAD腳本文件,通過在啓動時:

System.Diagnostics.Process proc = new Process(); 
proc.StartInfo.FileName = @"C:\Program Files\AutoCAD 2010\acad.exe /b myScriptFile.scr"; 
proc.Start(); 

而不是

從.NET試圖直接操縱的AutoCAD如您發現的那樣,可能會出現問題,我只需將所有AutoCAD命令寫入腳本文件,然後將該腳本文件作爲命令行參數發送到AutoCAD,如上所示。

+0

如果在運行時不需要真正觸摸應用程序,這種方法效果很好。當他撰寫ScriptPro時,AutoDesk的Viru使用了同樣的方法。 –

+0

您可以在腳本文件中包含等待點以在腳本運行時暫停某些(有限)用戶輸入。 – Stewbob

+1

我的意思是在.NET中與實時交談和/或控制事物。大多數情況下,使用這種方法時,最好在空閒操作計時器中強制重新啓動AutoCAD,如果有事件掛起。由於OP的Interop方法不起作用,您的方法在這裏是合乎邏輯的選擇。 –

0

我使用您顯示的示例,但我嘗試首先獲取活動的Autocad。您可以將它與上一個答案中提到的使用過程結合起來,但這需要始終知道Autocad的安裝位置。

try 
{ 
    // you may choose to reverse this, by calling getActiveObject first 
    // or you can try repeating the getActiveObject after a short delay 
    var t = Type.GetTypeFromProgID(_autocadClassId, true); 
    // Create a new instance AutoCAD 
    _application = Activator.CreateInstance(t, true);  
} 
catch (COMException ex) 
{ 
    try 
    { 
     // Start works every time, but due to the delay it may appear not to 
     // check task manager when testing this 
     // add a delay here before trying to get what was started 
     Thread.Sleep(750); //for example 
     _application = Marshal.GetActiveObject(_autocadClassId); 
    } 
    catch (Exception e2x) 
    { 
     Log.Error(e2x); 
    } 
} 

我_Application對象是動態的,我有靜態方法CAD類封裝了AutoCAD的功能,添加新的功能,我有與使用AutoCAD的互操作,以獲得方法的CAD類的虛擬項目和屬性名稱和套管的權利,但我部署與動態。

相關問題