获取 AutoCAD.Application 实例

发布于 2025-01-09 08:05:20 字数 973 浏览 5 评论 0原文

我想获得一个 AutoCAD.Application 实例。 但是,在 AutoCAD 加载屏幕期间执行 GetActiveObject() 时会发生错误。 如果我设置断点并在加载完成后调用 GetActiveObject() ,则实例会正常运行。 加载窗口结束时,.Net 中是否有事件或其他方法可以使用?

static void Main(string[] args)
{
    AcadApplication objAcad = default(AcadApplication);
    const string strProgId = "AutoCAD.Application.24.1";

    Process myProcess = new Process();
    myProcess.StartInfo.FileName = @"C:\Program Files\Autodesk\AutoCAD 2022\" + "acad.exe";
    myProcess.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
    myProcess.Start();
    
    try
    {
        objAcad = Marshal.GetActiveObject(strProgId) as AcadApplication;
        if (objAcad is null)
            throw new Exception("obj is null");
    }
    catch(Exception ex) // An error occurs if no instance is running
    {
        Type acType = Type.GetTypeFromProgID(strProgId);

        objAcad = (AcadApplication)Activator.CreateInstance(acType, true);
    }

    objAcad.Visible = true;
}

I want to get an AutoCAD.Application instance.
However, an error occurs when GetActiveObject() is executed during the AutoCAD loading screen.
If I set a breakpoint and call GetActiveObject() after loading is finished, the instance is got well.
Is there an event or other method in .Net to use when the loading window ends?

static void Main(string[] args)
{
    AcadApplication objAcad = default(AcadApplication);
    const string strProgId = "AutoCAD.Application.24.1";

    Process myProcess = new Process();
    myProcess.StartInfo.FileName = @"C:\Program Files\Autodesk\AutoCAD 2022\" + "acad.exe";
    myProcess.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
    myProcess.Start();
    
    try
    {
        objAcad = Marshal.GetActiveObject(strProgId) as AcadApplication;
        if (objAcad is null)
            throw new Exception("obj is null");
    }
    catch(Exception ex) // An error occurs if no instance is running
    {
        Type acType = Type.GetTypeFromProgID(strProgId);

        objAcad = (AcadApplication)Activator.CreateInstance(acType, true);
    }

    objAcad.Visible = true;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

椒妓 2025-01-16 08:05:20

通常这是这样完成的:

  AcadApplication acadApp = null;
  const string progId = "AutoCAD.Application.24.1";
 
  // Try to get a running instance of AutoCAD
  try
  {
      acadApp = (AcadApplication)Marshal.GetActiveObject(progId);
  }
  catch // An error occurs if no instance is running
  {
      try
      {
          // Try to create a new instance of AutoCAD
          acadApp = (AcadApplication)Activator.CreateInstance(Type.GetTypeFromProgID(progId), true);
      }
      catch
      {
          // If an instance of AutoCAD is not created then message and exit
          System.Windows.Forms.MessageBox.Show(
              "Instance of 'AutoCAD.Application' could not be created.");
 
          return;
      }
  }

Typically this is done this way:

  AcadApplication acadApp = null;
  const string progId = "AutoCAD.Application.24.1";
 
  // Try to get a running instance of AutoCAD
  try
  {
      acadApp = (AcadApplication)Marshal.GetActiveObject(progId);
  }
  catch // An error occurs if no instance is running
  {
      try
      {
          // Try to create a new instance of AutoCAD
          acadApp = (AcadApplication)Activator.CreateInstance(Type.GetTypeFromProgID(progId), true);
      }
      catch
      {
          // If an instance of AutoCAD is not created then message and exit
          System.Windows.Forms.MessageBox.Show(
              "Instance of 'AutoCAD.Application' could not be created.");
 
          return;
      }
  }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文