Hi all C# lovers,
Recently, I did a project for Govt. PSU which involved using AutoCAD with C# and I found it very interesting. I am very eager to share it with you.
This article shows how to use AutoCAD objects in a .NET application using C#.
Introduction:
AutoCAD 2004 software, is the industry-standard design tool for 2D drafting and detailing, and for 3D work. It provides native DWG compatibility, plus the ability to personalize the software or add third-party applications for your specific design requirements. It features new and enhanced productivity tools, presentation graphics, CAD standards, and more for faster, easier data creation and sharing.
For many CAD designer, drawing the design from scratch is very tedious. Like every time they have to draw some lines, circles, donuts, text etc... for every new project. To eliminate this repetitive work AutoCAD has provided its managed dlls to access its functionality. We can use VBA, VB.NET or C# to draw AutoCAD objects and access AutoCAD functions.
As I am a C# programmer, I will show how can we write C# code to access AutoCAD and draw some objects: -
Let's Code:
First of all add AutoCAD's reference to your project (I have attached a demo project with this article), because with out this reference you can access AutoCAD's functionality.
Now we will create an instance of AutoCAD application & open a document, then we will create some layers and load some types that will be used later in the project.
using System;
using System.Collections.Generic;
using System.Text;
using System.Diagnostics;
using System.Windows.Forms;
using AutoCAD;
public static AcadApplication gbl_app;
public static AcadDocument gbl_doc;
public static AcadModelSpaceClass gbl_modSpace;
public static AcadAcCmColor gbl_color;
public static double gbl_pi = 3.14159;
public static AcadLayer TerminalsLayer; //Layer For Donuts
public static AcadLayer SwitchLayer;
public static AcadLayer TerminationPoints; //Layer Termination Points
public static void CreateAutoCADObject()
{
try
{
CloseAllInstance(); // this method is used to close any opened instance of autocad, if you dont require it then comment it
gbl_app = new AcadApplication();
gbl_doc = gbl_app.ActiveDocument;
gbl_app.Application.Visible = true;
gbl_modSpace = (AcadModelSpaceClass)gbl_doc.ModelSpace;
gbl_doc.Linetypes.Load("HIDDEN", "acad.lin");
gbl_doc.Linetypes.Load("CENTER", "acad.lin");
//Other Objects Layer
SwitchLayer = PF.gbl_doc.Layers.Add("Switch110Layer");
SwitchLayer.color = AutoCAD.AcColor.acGreen;
PF.gbl_doc.ActiveLayer = SwitchLayer;
//Layer For Donuts
TerminalsLayer = PF.gbl_doc.Layers.Add("TerminalsLayer");
TerminalsLayer.color = AutoCAD.AcColor.acRed;
//Layer Termination Points
TerminationPoints = PF.gbl_doc.Layers.Add("TerminationPoints");
TerminationPoints.color = AutoCAD.AcColor.acWhite;
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
public static void CloseAllInstance()
{
Process[] aCAD =Process.GetProcessesByName("acad");
foreach (Process aCADPro in aCAD)
{
aCADPro.CloseMainWindow();
}
}
Now we will write some methods to create AutoCAD objects:
To Draw Line:
public static void DrawLine(double StartX1,
double StartY1,
double EndX2,
double EndY2,
string LineType,
bool DrawDonutsOnLineStart,
bool DrawDonutsOnLineEnds)
{
AcadLine lineObj;
double[] startPoint = new double[3];
double[] endPoint = new double[3];
startPoint[0] = StartX1;
startPoint[1] = StartY1;
startPoint[2] = 0.0;
endPoint[0] = EndX2;
endPoint[1] = EndY2;
endPoint[2] = 0.01;
lineObj = gbl_doc.ModelSpace.AddLine(startPoint, endPoint);
if (LineType.Length > 0)
{
lineObj.Linetype = LineType; //'"HIDDEN"
lineObj.LinetypeScale = 10;
lineObj.Update();
}
if (DrawDonutsOnLineStart == true)
{
DrawDonut((AcadBlock)gbl_doc.ModelSpace, 0, 3.0, StartX1, StartY1);
}
if (DrawDonutsOnLineEnds == true)
{
DrawDonut((AcadBlock)gbl_doc.ModelSpace, 0, 3.0, EndX2, EndY2);
}
gbl_app.ZoomAll();
}
public static void DrawLine(double StartX1,
double StartY1,
double EndX2,
double EndY2)
{
DrawLine(StartX1, StartY1, EndX2, EndY2, "", false, false);
}
public static void DrawLine(double StartX1,
double StartY1,
double EndX2,
double EndY2,
string LineType)
{
DrawLine(StartX1, StartY1, EndX2, EndY2, LineType, false, false);
}
public static void DrawLine(double StartX1,
double StartY1,
double EndX2,
double EndY2,
string LineType,
bool DrawDonutsOnLineStart)
{
DrawLine(StartX1, StartY1, EndX2, EndY2, LineType, DrawDonutsOnLineStart, false);
}
To Draw Solid:
public static void DrawSolid(double StartingXPoint,
double StartingYPoint,
double Length,
double Width)
{
AcadSolid solidObj;
double[] point1 = new double[3];
double[] point2 = new double[3];
double[] point3 = new double[3];
double[] point4 = new double[3];
//Solid Starts
point1[0] = StartingXPoint;
point1[1] = StartingYPoint;
point1[2] = 0.0;
point2[0] = StartingXPoint;
point2[1] = (StartingYPoint) - Width;
point2[2] = 0.0;
point3[0] = StartingXPoint + Length;
point3[1] = StartingYPoint;
point3[2] = 0.0;
point4[0] = StartingXPoint + Length;
point4[1] = (StartingYPoint) - Width;
point4[2] = 0.0;
solidObj = gbl_doc.ModelSpace.AddSolid(point1, point2, point3, point4);
//Solid ENDS
}
To Draw Text:
public static void DrawText(double StartingXPoint,
double StartingYPoint,
string textString,
double Height,
double Rotation)
{
AcadText textObj;
double[] insertionPoint = new double[3];
insertionPoint[0] = StartingXPoint;
insertionPoint[1] = StartingYPoint;
insertionPoint[2] = 0.0;
textObj = gbl_doc.ModelSpace.AddText(textString, insertionPoint, Height);
textObj.Alignment = AcAlignment.acAlignmentLeft;
textObj.Backward = false;
textObj.Rotation = Rotation;
}
public static void DrawText(double StartingXPoint,
double StartingYPoint,
string textString)
{
DrawText(StartingXPoint, StartingYPoint, textString, 3, 0);
}
public static void AddText(double StartingXPoint,
double StartingYPoint,
string textString,
double Height)
{
DrawText(StartingXPoint, StartingYPoint, textString, Height, 0);
}
To Draw Circle:
public static void DrawCircle(double StartingXPoint,
double StartingYPoint,
double Radius)
{
AcadCircle circleObj;
double[] centerPoint = new double[3];
centerPoint[0] = StartingXPoint;
centerPoint[1] = StartingYPoint;
centerPoint[2] = 0.0;
circleObj = gbl_doc.ModelSpace.AddCircle(centerPoint, Radius);
}
To Draw Arc:
public static void DrawArc(double StartingXPoint,
double StartingYPoint,
double Radius)
{
//For Drawing Arc
AcadArc arcObj;
AcadCircle circleObj;
double[] centerPoint = new double[3];
double startAngleInDegree;
double endAngleInDegree;
double startAngleInRadian;
double endAngleInRadian;
//Draw Arc
centerPoint[0] = StartingXPoint;
centerPoint[1] = StartingYPoint;
startAngleInDegree = 175.0;
endAngleInDegree = 5.0;
startAngleInRadian = startAngleInDegree * 3.141592 / 180.0;
endAngleInRadian = endAngleInDegree * 3.141592 / 180.0;
arcObj = gbl_doc.ModelSpace.AddArc(centerPoint, Radius,
startAngleInRadian, endAngleInRadian);
}
To Draw Donuts:
public static AcadLWPolyline DrawDonut(AcadBlock space,
double inRad,
double outRad,
double cenPt1,
double cenPt2)
{
double width, radius, PI;
double[] tmp = new double[2];
double[] v = new double[4];
AcadLWPolyline pl;
double[] basePnt = new double[3];
try
{
//Switch to terminals layer
gbl_doc.ActiveLayer = TerminalsLayer;
basePnt[0] = cenPt1;
basePnt[1] = cenPt2;
basePnt[2] = 0.0;
PI = Math.Atan(1) * 4;
width = (outRad - inRad) / 2;
radius = (inRad + width) / 2;
tmp = (double[])gbl_doc.Utility.PolarPoint(basePnt, PI, radius);
v[0] = tmp[0];
v[1] = tmp[1];
tmp = (double[])gbl_doc.Utility.PolarPoint(basePnt, 0, radius);
v[2] = tmp[0];
v[3] = tmp[1];
pl = space.AddLightWeightPolyline(v);
pl.Closed = true;
pl.SetWidth(0, width, width);
pl.SetBulge(0, -1);
pl.SetWidth(1, width, width);
pl.SetBulge(1, -1);
//Switch to other layer
gbl_doc.ActiveLayer = SwitchLayer;
return pl;
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
return null;
}
}
I have provided a demo application with this article which uses these public static functions to draw a device(I will not reveal the name because of our agreement) with minimal of code.
This is my first article in this site so your valuable suggestions are welcome.
hadi karimiPosted May 24, 2024, 7:13 AM
How to filter object in AutoCAD .NET and C# ?
mickael schwedlerPosted Aug 3, 2022, 12:46 PM
Hi Congratulations for the post do you have available code cad intersections, polyline, polygons for me to study?
Rajesh BarikPosted Jun 8, 2022, 10:03 AM
Hi, Can you share anything on how to access these objects from an existing .dwg file, like their values, names, types everthing using c sharp.
mustafa alaloPosted Jun 26, 2014, 2:12 AM
The AutoCAD dll references not working why?
mustafa alaloPosted Jun 26, 2014, 2:05 AM
i'm downloding this project but the .dll not working why? pls help me how i can download the .dll references to work this project
m MogharrabiPosted Feb 17, 2014, 4:34 AM
Hi and thanks for your useful topic. But you did'nt speak about AddEllipse method and it's parameters.Please write a sample of that....Thanks in advance
AlexPosted Nov 11, 2013, 1:10 PM
Oh, my spelling...( Sorry for that.
AlexPosted Nov 11, 2013, 1:08 PM
To compile this prohect, I've added to files to my project's References: "acax18ENU.tlb" and "axdb18enu.tlb" (they ship with AutoCAD 2010 ObjectARX SDK). After that, I could use the AutoCAD namespace.
AlexPosted Nov 11, 2013, 1:05 PM
Hello.
Anil SamuelPosted Apr 9, 2013, 6:55 AM
Hello it is very informative... I am very new to C#. Actually i wanted to write one script to produce 2D stepped rectangle, which is supposed to run in Autocad. Could you please send me more onformation?..Thank you in advance Id is [email protected]
Ravi KarnatakamPosted Nov 17, 2012, 10:29 AM
Hi.. Thanx for the article. Code looks great. Could you please provide the steps, prerequisites and development environment to start coding. Please mail me.
qwertyPosted Apr 3, 2012, 7:18 AM
is der any cad application that uses sharpgl for rendering purpose?? if its even like a basic paint application, its fine.. plz help..
Sarva RatchaganPosted Feb 27, 2012, 9:01 AM
why did u hide that dll By Sarva
Sarva RatchaganPosted Feb 24, 2012, 4:21 AM
Plz hlp me how can i add Tat Dll in my downloaded data best regards Sarva Ratchagan Mail id: [email protected]
Sarva RatchaganPosted Feb 24, 2012, 4:20 AM
Plz hlp me how can i add Tat Dll in my downloaded data best regards Sarva Ratchagan Mail id: [email protected]
Sarva RatchaganPosted Feb 24, 2012, 4:13 AM
Plz hlp me how can i add Tat Dll
Deshna sPosted Apr 25, 2011, 4:43 AM
Hiii Sheel Gohe.. I need autocad dll..so please help me.Thanks alot
de menPosted Dec 13, 2010, 1:49 PM
thanks for your article, it is useful for me. i'm a beginner in using C sharp, i have just got a project about using C# for designing a interface to user can draw a line, an arch, a circle and a spline by input parameters, for example: draw a line, user have to input X value and Y value, like autocad 2d but more simple, please help me more detailed, to moved and fired by your help, thanks
nasri mouradPosted Apr 18, 2010, 10:03 AM
high i'm a beginner with C# and i want to make autocad drawing using C# where can i find free autocad libraries to add as a reference thank you
jas ddPosted Oct 26, 2009, 4:04 AM
dsfsd
paulPosted Jul 12, 2009, 10:14 PM
Thanks. very useful for me .do you have any examples with forms ?
mai huy toanPosted Jul 6, 2009, 8:03 AM
That code is not run. Have bug. Are you build this solution?
e hPosted Feb 25, 2009, 2:33 PM
You article sounds very good, but I cannot get example download to work. Probably changes from Autocad 2004 to 2009. I am uising VS2005 - it cannot find the reference 'AutoCAD' and I don't see a dll with that name. I found the one you provided in the download, but apparently it is looking for something more... Thanks very much for help!!!
felixyu yueditedPosted Oct 23, 2008, 2:55 PMEdited Oct 23, 2008, 3:02 PM
Hello, Excellant example. I don't know .Net. Actually, I'm in Java, so I like to ask if I can use JAVA to do something ? If Yes, can I have an example ? ma email: [email protected] Thanks
prabhav mishraPosted Mar 15, 2008, 5:22 AM
hello I am usingf vb.net and autocad lt I want to know thaat how can we link the excel file with autocad ,so wat changes we done,it will reflect in drawing . With Regards Prabhav Mishra
sachin katkarPosted Feb 5, 2008, 1:11 AM
Hi, I liked your program when i tried to run it gives me an error as-"The referenced component 'AutoCAD' could not be found." can you please tell me how to add this reference...?
mohamed elrefyPosted Dec 1, 2007, 10:56 AM
i am sorry to disturb you but i need your help to write asimple autocad program that can draw and save line drawings by using mouse to select points thanks
Mahmood VelayatiPosted Sep 27, 2007, 5:39 AM
Hi, I want to add a toolbar in autocad. I don't know which of this language (VisualBasic.NET or Visual C#.NET) is better for this purpose. please guide me.Furthermore I don't have any sources to learn this work. please say that where can I learn this or introduce me a -Book/web link- that I learn how write this program. Please send answer to my email.([email protected]) Thanks,
rich hPosted Mar 30, 2007, 6:46 PM
I’m trying to use c# to draw in AutoCAD. I’m so lost I can understand what you are doing I can follow the code but I don’t know how to set up me VS 2003. I have acad 2005 how I open a new project and start. Should I use the templates or windows application please help me. Thank you. could you please send your replies to [email protected]
umesh patilPosted Mar 10, 2007, 2:02 AM
hi, nice to get ur help in cad I have to use acad in c#.net but i am having problem in adding refrence to it. how to add refrence to acad application please guide. Thank you in advance.
milind khirePosted Feb 9, 2007, 2:19 AM
Hi! I tried using this code and it worked fine. Now when i tried this code with Autocad 2006 LT, the code is not working. why is it so? Thanks in advance
Valentin PerezPosted Jul 31, 2006, 5:30 AM
Hi. I'm very interested in your knowledge about autocad interporalitty with c#. I'm developing a website with this funtionallity but in vb.net, when I run the application locally it works correctly and I can draw a line in autocad, but when I publish website on server I get a error message " Object reference not set to an instance of an object". Do you know if is possible work with asp.net and autocad?. Have you got any experience about that? thanks in advance.
Gharbi MehdieditedPosted Jul 21, 2006, 7:12 AMEdited Nov 14, 2006, 1:10 PM
Hello, I Liked your program so much but When I tried to add a hatch to the AutoCad drawing I did not succeed. So please tell me how to do ?? Thanks.