我想要一个AutoCAD.Application实例。但是,当GetActiveObject()在AutoCAD加载屏幕上执行时会发生错误。如果我设置了一个断点,并在加载完成后调用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;
}发布于 2022-02-23 12:45:23
通常是这样做的:
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;
}
}https://stackoverflow.com/questions/71230955
复制相似问题