Introduction
This article is about 2D Drawing Automation in Autocad using C#. It gives step-by-step instructions to start with Autocad automation for Beginners.
Background
Prerequisites
1. C# Programming basics
2. AutoCAD drawing basics.
Tools Required
1. Visual Studio Express
2. Autocad
What is Autocad Automation?
Autocad is a software for creating 2D and 3D drawings. Autocad Automation means that, creating drawings automatically in Autocad by using programming. We can draw very big drawings with in a seconds by programming. Autocad provides interoperability with other applications, by using Autocad object model, we can create drawings in Autocad. The above diagram shows the idea behind the Autocad Automation. A custom C# application creates drawing in the Autocad by calling the commands available in the Autocad.
In this example, We will create a simple drawing in Autocad by a C# Application by using Autocad COM API. Before proceeding further, we need to know the following things,
1. How Autocad Automation is working?
2. What is Autocad object model?
3. How to Connect our application with active instance of Autocad?
How Autocad Automation is working?
Have a look at the following diagram, which shows the actual working mechanism of Autocad Automation.
Actually what happes? Autocad orgranizes everything as object (OOP), circle, line, rectangle, square, ellipse and all are considered as objects in Autocad. Each object in Autocad has its own properties and functions. Autocad exposing these objects to the world through Autocad COM API, by using this API in our programming, we can play with the objects of Autocad. So, manipulating the Autocad objects via Autocad COM API is the basic idea behind the Autocad Automation.
Consider the example from the above diagram, The collection of Autocad objects and its organization together known as Autocad object model. This model contains the details about all Autocad object's properties and functions. These objects are available in the Autocad COM API. This object model is like a pool of Autocad objects, we can use those objects to create drawing in Autocad. The diagram shows some of the properties and functions of the Circle object present in the Autocad COM API, By calling the AddCircle method from C# application, the circle will be created in Autocad. By setting the Radius and CenterPoint properties we can manipulate the created circle.
Note: AddCircle is not a method of Autocad Circle object, it is a member of Autocad ModelSpace object. I put the AddCircle method here just for understanding.
What is Autocad object Model?
Now we know the importance of Autocad object model, Autocad object model is a collection of Autocad objects like Line, Circle, Polyline, Dimension, etc., It defines properties, methods, and events of Autocad objects. It also defines the organization and structure of Autocad objects.
The following diagram represents the hierarchy of the Autocad object model.
Ref img 1: Autodesk Object Model (ActiveX) by Autodesk Knowledge Network is licensed under (CC BY-NC-SA 3.0)
From the above diagram, we need to know the following important things,
Application (Global) - It represents Autocad Application. It has a property ActiveDocument which represents the current working document opened in Autocad.
Documents - Collection of documents opened in Autocad including ActiveDocument.
Document - Single Autocad document.
Modelspace - Workspace or Drawing Canvas in Autocad.
Look at the following diagram, it just the visualization of the above things.
From this diagram we can understand the following,
The Autocad application contains, collection of documents opened in the application, and this documents collection has the ActiveDocument (Current working document in Autocad). Each Autocad document has ModelSpace (modelling space or work area or drawing canvas). If we need to create a circle in the modelspace of the active document, we have to traverse from Autocad -> Documents -> Active Document -> Modelspace, and call the corresponding function, here function for creating a circle in modelspace is, AddCircle. Like this, we can create different shapes and drawings by calling appropriate functions.
Okay, We got an idea about the working mechanism of Autocad Automation applications. Now lets see about the connection between our application and Autocad. A Connection is required between our application and Autocad. Here the connection refers the object reference of the running Autocad application. By using this object reference we can traverse through the Autocad object tree and can do manipulations with it.
How to Connect our application with active instance of Autocad?
How we can connect our c# application with Autocad?, Before creating drawings by using c# application, we need to establish a connection between our application and active instance of Autocad. This is done by COM interoperability. We have to add COM interop assemblies of Autocad into our project as a reference before creating the connection. So where Autocad COM interop assemblies are located? It always located in the following folder of the windows os.
C:\Program Files\Common Files\Autodesk Shared - this folder contains the Autocad COM assemblies.
1. acax19enu.tlb
2. axdb19enu.tlb
These files are the COM interop assemblies of Autocad, Just add these two files into our project. Here 19 is the version number of Autocad. It may differ for different Autocad versions.
Lets take a look at the following code,
AcadApplication AcadApp = (AcadApplication)Marshal.GetActiveObject("AutoCAD.Application.19");
The above code, gets the object reference for the running instance of Autocad. From the above code,
AcadApplication
- refers the Autocad Application class - it represents the Autocad Application.
GetActiveObject
- Gets the running instance of the specified application.
Marshal
- class in System.Runtime.InteropServices namespace, it provides the functionalities related to Interoperability.
"AutoCAD.Application.19" - Application program ID for Autocad in the operating system, 19 represents the version of Autocad.
By using the above code, we will get the reference of active Autocad instance. After getting the connection, we can create the drawings by invoking the function members of ModelSpace object. Okay, now lets take a sample drawing and see the steps to create this drawing in Autocad by programming.
Sample Drawing
Consider the follwoing drawing as an example, lets see the steps to create this drawing using C#.
Sample Drawing without Dimension
Sample Drawing with Dimension
Step 1:
Create a Windows Forms (C#) project in Visual Studio. Name the project like AutoCADApp or whatever name you want.
Step 2:
Add reference to Autocad COM Dlls,
1. Autocad Type Library (acax19enu.tlb). (Here 19 is the version, it varies depending on Autocad version)
2. Autocad / ObjectDBX Common Type Library (axdb19enu.tlb). (Here 19 is the version, it varies depending on Autocad version)
Step 3:
Drag control to Main form like the below image,
And create events for buttons.
Step 4: Creating Circle
Before creating the circle, lets see about AcadCircle in the Autocad object model,
AcadCircle
It represents the circle in the autocad. It has the following properties,
- Area - Gets the Area of the circle created.
- Center Point - Gets or Sets 3 Dimensional Center Point of the circle.
- Diameter - Diamter of the circle.
- Radius - Radius of the circle.
AddCircle Function
Syntax: AddCircle ( CenterPoint, Radius )
-- CenterPoint - 3 Dimensional double array
-- Radius - double value represents the radius
By using the above details, lets write code for creating circle in click event of Creat Circle button,
Declaring global variables to hold the reference of cirle and AutoCAD to be created.
private AcadCircle Circle = default(AcadCircle);
private AcadApplication AcadApp = default(AcadApplication);
"Create Cirlce" Button Click event.
private void btnCircle_Click(object sender, EventArgs e)
{
AcadApp = (AcadApplication)Marshal.GetActiveObject("AutoCAD.Application");
double[] CenterOfCircle = new double[3];
CenterOfCircle[0] = 0;
CenterOfCircle[1] = 0;
CenterOfCircle[2] = 0;
double RadiusOfCircle = Convert.ToDouble(txtRadius.Text.Trim());
Circle = AcadApp.ActiveDocument.ModelSpace.AddCircle(CenterOfCircle, RadiusOfCircle);
}
Step 4: Adding Caption to Circle
Before creating the caption, lets know about AcadText
object.
AcadText Object
It represents string value. It has the following properties,
- Height - Gets or Sets height of the text created.
- InsertionPoint - Gets or Sets the position of AcadText in 3 Dimensional double array.
- TextString - Gets or Sets the content of the AcadText object.
AddText Function
Syntax: AddText ( TextString, InsertionPoint, Height )
-- InsertionPoint - 3 Dimensional double array representing the position of AcadText.
-- TextString - represents the content of the AcadText.
-- Height - represents height of the AcadText.
By using the above details, lets write code for adding caption in click event of Add Caption button,
"Add Caption" Button Click event.
private void btnCaption_Click(object sender, EventArgs e)
{
AcadText Caption = default(AcadText);
string TextString = "CIRCLE";
double[] InsertionPoint = new double[3];
double[] CenterOfCircle = (double[])Circle.Center;
InsertionPoint[0] = CenterOfCircle[0] - Circle.Radius / 2;
InsertionPoint[1] = CenterOfCircle[1] - (Circle.Radius + 50);
InsertionPoint[2] = 0;
double Height = 20;
Caption = AcadApp.ActiveDocument.ModelSpace.AddText(TextString, InsertionPoint, Height);
Caption.color = ACAD_COLOR.acGreen;
}
Step 5: Adding Radial Dimension
There are many method available to add dimension to the AutoCAD objects, Incase of circle, we have to add radial dimension, for that we are going to use the following function from Autocad object model,
AddDimRadial
Syntax: AddDimRadial ( Center, ChordPoint, LeaderLength )
"Add Dimension" Button Click event.
private void btnDimension_Click(object sender, EventArgs e)
{
AcadDimRadial Dimension = default(AcadDimRadial);
double[] Center = Circle.Center;
double[] ChordPoint = Circle.Center;
double LeaderLength = 150;
Dimension = AcadApp.ActiveDocument.ModelSpace.AddDimRadial(Center, ChordPoint, LeaderLength);
Dimension.TextOverride = "R" + Circle.Radius.ToString();
Dimension.TextColor = ACAD_COLOR.acCyan;
Dimension.DimensionLineColor = ACAD_COLOR.acMagenta;
Dimension.ArrowheadSize = 15;
Dimension.TextHeight = 15;
}
Conclusion
According to the Autocad object model, each and every Autocad object has its own Properties and Methods. By using these Methods, we can automate our drawing process by programming.