Description
One of the major challenges while working in Dynamo was to retrieve data from external sources. While reading autocad files was somehow possible through external sources (sat exports, excel coordinate representation, etc) a live connection was not possible for a long time.
At that time I found 2 methods to make a connection with Autocad. The easier one, using COM Interop was pretty fast to achieve, but had it’s limitations because not all classes were exposed through the interface for marshaling.
What Is
COM Interop is a technology included in the .NET Framework Common Language Runtime (CLR) that enables Component Object Model (COM) objects to interact with .NET objects, and vice versa.
All .NET Framework applications share a set of common types that enable interoperability of objects, regardless of the programming language that is used. The parameters and return values of COM objects sometimes use data types that differ from those used in managed code. Interoperability marshaling is the process of packaging parameters and return values into equivalent data types as they move to and from COM objects.
When data needs to go from managed code to unmanaged code, or vice-versa, the CLR’s marshaling service takes over and makes sure that the data is correctly transferred.
Code
public class AutocadConnection { private AcadApplication application; private AcadDocument activeDocument; private AcadModelSpace activeModelSpace; public AcadApplication Application { get { return application; } set { } } public AcadDocument ActiveDocument { get { return activeDocument; } set { } } public AcadModelSpace ActiveModelspace { get { return activeModelSpace; } set { } } /** * Creating the live connection with Autocad */ public AutocadConnection(string progID = "AutoCAD.Application.20") { Object acApp = null; try { acApp = (AcadApplication)Marshal.GetActiveObject(progID); } catch { try { Type acType = Type.GetTypeFromProgID(progID); acApp = (AcadApplication)Activator.CreateInstance( acType, true ); } catch { Console.WriteLine( "Cannot create object of type \"" + progID + "\"" ); } } application = (acApp == null) ? null : (AcadApplication)acApp; activeDocument = (acApp == null) ? null : application.ActiveDocument; activeModelSpace = (acApp == null) ? null : application.ActiveDocument.ModelSpace; } }
Dynamo Node
public class Connection { private Connection() { } // Live Connection to Autocad> [MultiReturn(new[] { "Connection" })] public static Dictionary<string, object> AutoCADConnection() { var Connection = new AutocadConnection(); return new Dictionary<string, object> { { "Connection", Connection} }; } // Live Connection to Autocad [MultiReturn(new[] { "ActiveDocument" })] public static Dictionary<string, object> ActiveDocument(AutocadConnection Connection) { return new Dictionary<string, object> { { "ActiveDocument", Connection.ActiveDocument} }; } }
ProgId
The progId’s used in AutocadConnection depend on the version of Autocad you want to connect to:
- “AutoCAD.Application.20”– AutoCAD 2015 , AutoCAD 2016.
- “AutoCAD.Application.20.1” – AutoCAD 2016,
- “AutoCAD.Application.21” – AutoCAD 2017.
- “AutoCAD.Application.22” – AutoCAD 2018.
In your project make sure to add a reference to the following:
- Autodesk.AutoCAD.Interop.dll
- Autodesk.AutoCAD.Interop.Common.dll