获取 AutoCAD.Application 实例
我想获得一个 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
通常这是这样完成的:
Typically this is done this way: