Using the MATLAB .NET Interface to interact with AutoCAD

20 views (last 30 days)

Accepted Answer

MathWorks Support Team
MathWorks Support Team on 2 Mar 2023
AutoCAD supports both the COM and .NET APIs, and both can be used to interact with AutoCAD. However, COM is an old technology with many drawbacks, and MathWorks has no plans to enhance the COM Interface, so we recommend that you use the .NET Interface to access AutoCAD's libraries from MATLAB.
The following example shows how you can use .NET Interface to open a drawing file, create a polyline and a region, and then save the drawing. You can download the code for this example from this link .
1.    Locate the required AutoCAD assemblies. For example,
>> rootdir = 'C:\Program Files\Autodesk\AutoCAD 2023';
>> Interop = 'Autodesk.AutoCAD.Interop.dll';
>> InteropCommon = 'Autodesk.AutoCAD.Interop.Common.dll';
2.    Make the .NET assemblies visible to MATLAB.
>> NET.addAssembly(fullfile(rootdir,Interop));
>> NET.addAssembly(fullfile(rootdir,InteropCommon));
3.    Open a new instance of AutoCAD and make it visible.
>> app = Autodesk.AutoCAD.Interop.AcadApplicationClass;
>> app.Visible = 1;
4.    Open a drawing file. For example,
>> filename = '<full path>\test1.dwg';
>> doc = app.Documents.Open(filename);
5.    Add a polyline in the form of a rectangle and get the output of the Explode method.
>> coords=[2;0;0;4;0;0;4;4;0;0;4;0];
>> rec = doc.ModelSpace.AddPolyline(coords);
>> reckage = rec.Explode;
6.    Update and zoom in.
>> app.Update
>> app.ZoomAll
7.    Create a region composed of an arc and a line.
Create the arc.
>> arc = doc.ModelSpace.AddArc([5; 3; 0], 2, 0, 3.141592);
Create a line defined by the endpoints of the arc.
>> chord = doc.ModelSpace.AddLine(arc.EndPoint, arc.StartPoint);
To create a region, we need to pass an array containing arc and chord. Because these are different object types, we use superclasses to find a superclass of both objects.
>> superclasses(arc)
Superclasses for class Autodesk.AutoCAD.Interop.Common.AcadArcClass:
System.__ComObject
System.MarshalByRefObject
System.Object
handle
Autodesk.AutoCAD.Interop.Common.IAcadArc
Autodesk.AutoCAD.Interop.Common.IAcadEntity
Autodesk.AutoCAD.Interop.Common.IAcadObject
Autodesk.AutoCAD.Interop.Common.AcadArc
Autodesk.AutoCAD.Interop.Common.IAcadObjectEvents_Event
and
>> superclasses(chord)
Superclasses for class Autodesk.AutoCAD.Interop.Common.AcadLineClass:
System.__ComObject
System.MarshalByRefObject
System.Object
handle
Autodesk.AutoCAD.Interop.Common.IAcadLine
Autodesk.AutoCAD.Interop.Common.IAcadEntity
Autodesk.AutoCAD.Interop.Common.IAcadObject
Autodesk.AutoCAD.Interop.Common.AcadLine
Autodesk.AutoCAD.Interop.Common.IAcadObjectEvents_Event
From the above output, we see that IAcadEntity is a superclass of both arc and chord, so create a .NET array of type IAcadEntity.
>> acadEntityArray = NET.createArray('Autodesk.AutoCAD.Interop.Common.IAcadEntity',2);
Add arc and chord to the array
>> acadEntityArray(1) = arc;
>> acadEntityArray(2) = chord;
Create the region.
>> halfCircleRegion = doc.ModelSpace.AddRegion(acadEntityArray);
8. Save the drawing.
>> doc.Save

More Answers (0)

Categories

Find more on Engines & Motors in Help Center and File Exchange

Tags

No tags entered yet.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!