新しいアプリケーションの開発では、インプロセスまたはアウトオブプロセスでアプリケーションを実行できます。AutoCAD .NET API はインプロセスのみで実行するように設計されており、インプロセスまたはアウトオブプロセスで使用できる ActiveX オートメーション ライブラリとは異なります。
AutoCAD を駆動するスタンドアロン アプリケーションを作成する必要がある場合は、CreateObject と GetObject メソッドを使用するアプリケーションを作成し、AutoCAD アプリケーションの新しいインスタンスを作成したり、現在実行中のインスタンスのいずれか 1 つを返すことをお勧めします。AcadApplication への参照が戻ったら、AcadApplication の ActiveDocument プロパティのメンバーである SendCommand メソッドを使用して、インプロセス .NET アプリケーションを AutoCAD にロードできます。
.NET アプリケーションをインプロセスで実行する代わりに、アプリケーションで COM 相互運用機能を使用できます。
using System;
using System.Runtime.InteropServices;
using Autodesk.AutoCAD.Interop;
using Autodesk.AutoCAD.Runtime;
using Autodesk.AutoCAD.ApplicationServices;
[CommandMethod("ConnectToAcad")]
public static void ConnectToAcad()
{
AcadApplication acAppComObj = null;
const string strProgId = "AutoCAD.Application.26.0";
// 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 ");
}