How to wait until an Acad's instance runs to create e new documents?

时间:2016-10-20 18:35:17

标签: c# autocad objectarx

I'm using objectARX and trying to create a new document. What i'm doing first is to run AutoCad.

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

Then the problem is when I wait until the instance of Acad is ready. I can't get The process by his name with the Process class because Autocad window is not ready yet and I can't create the AcadApplication instance. It only works when Autocad is completely loaded so I use .

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

The acadGrid Method running in the thread creates a new document in AutoCad and then draws a grid. It sometimes works and sometimes not, and when it works it uses even 50% of CPU. Sometimes i gett this exception. enter image description here

2 个答案:

答案 0 :(得分: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);
}

答案 1 :(得分: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 Console执行此过程。这是版本2013+中包含的AutoCAD版本,如果您想加快流程,可以在命令行中独占运行 - http://through-the-interface.typepad.com/through_the_interface/2012/02/the-autocad-2013-core-console.html