2016-10-20 27 views
0

我正在使用objectARX並嘗試創建一個新文檔。我最先做的是運行AutoCad。如何等待Acad的實例運行以創建新的文檔?

Process acadApp = new Process(); 
      acadApp.StartInfo.FileName = "C:/Program Files/Autodesk/AutoCAD 2015/acad.exe"; 
      acadApp.StartInfo.WindowStyle = ProcessWindowStyle.Maximized; 
      acadApp.Start(); 

然後問題是當我等待,直到Acad的實例準備好。由於Autocad窗口還沒有準備好,我無法通過他的名字來獲取Process進程,我無法創建AcadApplication實例。它只適用於Autocad完全加載後才能使用。

bool checkInstance = true; 
      //This piece of pure shit listen for an Acad instnce until this is opened 
      while (checkInstance) 
      { 
       try 
       { 
        var checkinstance = Marshal.GetActiveObject("AutoCAD.Application"); 
        checkInstance = false; 
       } 
       catch (Exception ex) 
       { 

       } 
      } 
      //Once the acad instance is opende The show starts 
      Thread.Sleep(12000); 
      Thread jili2 = new Thread(new ThreadStart(() => acadG.AcadGrid(Convert.ToInt32(grid.floorHeight), Convert.ToInt32(grid.floorWidth), grid.numFloors))); 
      jili2.Start(); 
      // MessageBox.Show("I don't know why it was executed"); 
     } 

線程中運行的acadGrid方法在AutoCad中創建一個新文檔,然後繪製一個網格。它有時有效,有時不起作用,甚至可以使用50%的CPU。有時我得到這個例外。 enter image description here

回答

1

Process.WaitForInputIdleApplication.GetAcadState可以幫助:

Process acadProc = new Process(); 
acadProc.StartInfo.FileName = "C:/Program Files/Autodesk/AutoCAD 2015/acad.exe"; 
acadProc.StartInfo.WindowStyle = ProcessWindowStyle.Maximized; 
acadProc.Start(); 
if (!acadProc.WaitForInputIdle(300000)) 
    throw new ApplicationException("Acad takes too much time to start."); 
AcadApplication acadApp; 
while (true) 
{ 
    try 
    { 
    acadApp = Marshal.GetActiveObject("AutoCAD.Application.20"); 
    return; 
    } 
    catch (COMException ex) 
    { 
    const uint MK_E_UNAVAILABLE = 0x800401e3; 
    if ((uint) ex.ErrorCode != MK_E_UNAVAILABLE) throw; 
    Thread.Sleep(1000); 
    } 
} 
while (true) 
{ 
    AcadState state = acadApp.GetAcadState(); 
    if (state.IsQuiescent) break; 
    Thread.Sleep(1000); 
} 
+0

謝謝您現在的作品完美,我只刪除了waintForInputIdle。 – MisaelGaray

0

我相信最好的方法是創建一個腳本(.scr)文件,您將其定義爲啓動進程的參數,而不是嘗試在運行例程之前等待AutoCAD加載。

// Build parameters. 
StringBuilder param = new StringBuilder(); 
string exportScript = @"C:\script.scr"; 
if (!string.IsNullOrWhiteSpace(exportScript)) 
{ param.AppendFormat(" /s \"{0}\"", exportScript); } 

// Create Process & set the parameters. 
Process acadProcess = new Process(); 
acadProcess.StartInfo.FileName = AcadExePath; 
acadProcess.StartInfo.Arguments = param.ToString(); 
acadProcess.Start(); 

腳本文件是一個基本的文本文件,其中列出AutoCAD命令,以及任何相關的值,如果你定義它們,並加載時運行它們。將腳本作爲參數加載到進程中將自動運行該腳本。

下面是創建腳本的簡要指南 - http://www.ellenfinkelstein.com/acadblog/tutorial-automate-tasks-with-a-script-file/

您還可以使用AutoCAD Core控制檯這一過程。這是一個包含版本2013+的AutoCAD版本,如果你想加快進程運行只在命令行 - http://through-the-interface.typepad.com/through_the_interface/2012/02/the-autocad-2013-core-console.html

相關問題