In the previous article we established a live COM interop connection with a running AutoCAD instance using the AutocadConnection class. In this part we query the open document from a console application and print information about the active session.
Querying the AutoCAD Document
With the AcadApplication instance you can traverse the active document's layer, name, model space, and layout collection. The AcadDocuments collection lets you enumerate every document currently open in the session:
csharp
class Program
{
static void Main(string[] args)
{
// AutocadConnection wraps the COM interop — see Part 1
AutocadConnection connection = new AutocadConnection();
AcadApplication acApp = connection.Application;
if (acApp != null)
{
// Active document information
Console.WriteLine(acApp.Application.ActiveDocument.ActiveLayer.Name);
Console.WriteLine(acApp.Name);
Console.WriteLine(acApp.ActiveDocument.Name);
Console.WriteLine(acApp.ActiveDocument.ModelSpace.Name);
// Enumerate all layouts in the active document
AcadLayouts layouts = acApp.ActiveDocument.Database.Layouts;
IEnumerator enumerator = layouts.GetEnumerator();
while (enumerator.MoveNext())
{
AcadLayout layout = (AcadLayout)enumerator.Current;
Console.WriteLine(quot;Layout: {layout.Name}");
}
// Enumerate all open documents in the AutoCAD session
AcadDocuments documents = connection.Application.Documents;
IEnumerator enumerator2 = documents.GetEnumerator();
while (enumerator2.MoveNext())
{
AcadDocument doc = (AcadDocument)enumerator2.Current;
Console.WriteLine(quot;Document: {doc.Name}");
}
}
}
}Result
csharp
// Sample console output with AutoCAD open and a drawing loaded:
0
AutoCAD
Drawing1.dwg
Model
Layout: Model
Layout: Layout1
Document: Drawing1.dwg