在AutoCAD 开发人员指南中的 AutoCAD .NET API 基础部分-> 进程外与进程内
当您开发一个新应用程序时,它可以在进程内运行或在进程外运行。AutoCAD .NET API 设计为仅在进程内运行,这与可在进程内或进程外使用的 ActiveX 自动化库不同。进程内应用程序设计为在与主机应用程序相同的进程空间中运行。在这种情况下,DLL 程序集被加载到作为主机应用程序的 AutoCAD 中。进程外应用程序不会在与宿主应用程序相同的空间中运行。这些应用程序通常构建为独立的可执行文件。如果您需要创建一个独立的应用程序来驱动 AutoCAD,最好创建一个使用 CreateObject 和 GetObject 方法来创建 AutoCAD 应用程序的新实例或返回当前正在运行的实例之一的应用程序。
示例代码显示了如何从独立应用程序访问正在运行的 AutoCAD 实例。基本上你可以做类似的事情(插入你的特定 progId):
[CommandMethod("ConnectToAcad")]
public static void ConnectToAcad()
{
AcadApplication acAppComObj = null;
const string strProgId = "AutoCAD.Application.18";
// Get a running instance of AutoCAD
try
{
acAppComObj = (AcadApplication)Marshal.GetActiveObject(strProgId);
}
catch // An error occurs if no instance is running
{
try
{
// Create a new instance of AutoCAD
acAppComObj = (AcadApplication)Activator.CreateInstance(Type.GetTypeFromProgID(strProgId), 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;
}
}
// Display the application and return the name and version
acAppComObj.Visible = true;
System.Windows.Forms.MessageBox.Show("Now running " + acAppComObj.Name +
" version " + acAppComObj.Version);
// Get the active document
AcadDocument acDocComObj;
acDocComObj = acAppComObj.ActiveDocument;
// Optionally, load your assembly and start your command or if your assembly
// is demandloaded, simply start the command of your in-process assembly.
acDocComObj.SendCommand("(command " + (char)34 + "NETLOAD" + (char)34 + " " +
(char)34 + "c:/myapps/mycommands.dll" + (char)34 + ") ");
acDocComObj.SendCommand("MyCommand ");
}