0

我正在使用 objectARX 并尝试创建一个新文档。我首先要做的是运行 AutoCad。

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

然后问题是当我等到 Acad 的实例准备好时。由于 Autocad 窗口尚未准备好并且我无法创建 AcadApplication 实例,因此我无法使用 Process 类以他的名字获取进程。它仅在 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。有时我得到这个例外。 在此处输入图像描述

4

2 回答 2

1

Process.WaitForInputIdle并且Application.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);
}
于 2016-10-21T07:09:15.463 回答
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

于 2016-10-20T22:01:19.877 回答