新しいアプリケーションの開発では、インプロセスまたはアウトオブプロセスでアプリケーションを実行できます。AutoCAD .NET API はインプロセスのみで実行するように設計されており、インプロセスまたはアウトオブプロセスで使用できる ActiveX オートメーション ライブラリとは異なります。
AutoCAD を駆動するスタンドアロン アプリケーションを作成する必要がある場合は、CreateObject と GetObject メソッドを使用するアプリケーションを作成し、AutoCAD アプリケーションの新しいインスタンスを作成したり、現在実行中のインスタンスのいずれか 1 つを返すことをお勧めします。AcadApplication への参照が戻ったら、AcadApplication の ActiveDocument プロパティのメンバーである SendCommand メソッドを使用して、インプロセス .NET アプリケーションを AutoCAD にロードできます。
.NET アプリケーションをインプロセスで実行する代わりに、アプリケーションで COM 相互運用機能を使用できます。
Imports System
Imports System.Runtime.InteropServices
Imports Autodesk.AutoCAD.Interop
Imports Autodesk.AutoCAD.Runtime
Imports Autodesk.AutoCAD.ApplicationServices
<CommandMethod("ConnectToAcad")> _
Public Sub ConnectToAcad()
Dim acAppComObj As AcadApplication
Dim strProgId As String = "AutoCAD.Application.24.3"
On Error Resume Next
'' Get a running instance of AutoCAD
acAppComObj = GetObject(, strProgId)
'' An error occurs if no instance is running
If Err.Number > 0 Then
Err.Clear()
'' Create a new instance of AutoCAD
acAppComObj = CreateObject(strProgId)
'' Check to see if an instance of AutoCAD was created
If Err.Number > 0 Then
Err.Clear()
'' If an instance of AutoCAD is not created then message and exit
MsgBox("Instance of 'AutoCAD.Application' could not be created.")
Exit Sub
End If
End If
'' Display the application and return the name and version
acAppComObj.Visible = True
MsgBox("Now running " & acAppComObj.Name & " version " & acAppComObj.Version)
'' Get the active document
Dim acDocComObj As AcadDocument
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 " & Chr(34) & "NETLOAD" & Chr(34) & " " & _
Chr(34) & "c:/myapps/mycommands.dll" & Chr(34) & ") ")
acDocComObj.SendCommand("MyCommand ")
End Sub
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.24.3";
// 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 ");
}
Sub ConnectToAcad() Dim acadApp As AcadApplication On Error Resume Next Set acadApp = GetObject(, "AutoCAD.Application.24.3") If Err Then Err.Clear Set acadApp = CreateObject("AutoCAD.Application.24.3") If Err Then MsgBox Err.Description Exit Sub End If End If acadApp.Visible = True MsgBox "Now running " + acadApp.Name + _ " version " + acadApp.Version Dim acadDoc as AcadDocument Set acadDoc = acadApp.ActiveDocument '' Optionally, load your assembly and start your command or if your assembly '' is demandloaded, simply start the command of your in-process assembly. acadDoc.SendCommand("(command " & Chr(34) & "NETLOAD" & Chr(34) & " " & _ Chr(34) & "c:/myapps/mycommands.dll" & Chr(34) & ") ") acadDoc.SendCommand("MyCommand ") End Sub