Upload
andjel-petrovic
View
218
Download
0
Embed Size (px)
Citation preview
8/3/2019 VS2008WithCad
1/14
Using Visua l C# Exp ress to c rea te a n AutoCAD .NET addin
Introduction
This doc ume nt will provide a step by step tutorial on how to c rea te, loa d a nd d eb ug a
simp le C# .NET app lica tion fo r AutoCAD using the Visua l C# Express deve lop me ntenvironm ent . The doc ume nt fo c uses on sett ing up and using the Visua l C# 2008 Express
environm ent. We d o not elab orate on the AutoCAD .NET API itself. We p rovide
refe renc es a t the end of this tutoria l to resources for lea rning the AutoCAD .NET API
itself.
Creating the p rojec t:
1) Launc h Visua l C# 2008 Express Ed ition2) Selec t the Menu item File New Projec t or typ e C trl+N. The New Projec t
d ialog w ill ap pe ar:
3) Selec t the Class Library Temp late (shown ab ove ) and ente r a suitable nam e inthe Name: text field. For Instanc e, we c hose MyCSAc adApp .
4) A new Projec t is now c rea ted and the Visua l C# Express window should looksom ething like this:
8/3/2019 VS2008WithCad
2/14
8/3/2019 VS2008WithCad
3/14
Conve rting your Projec t into a n AutoCAD C# .NET Applic ation:
1) In your Visua l C# 2008 Express w indo w, you should b e a b le to see a SolutionExplorer tab on the righ t. If you d on t see it, c lick the ViewSolution Explorermenu item o r type the Ctrl+R keys.
2) One should a lso b e a b le to see the Refe renc es folde r in the Solution explorernow . If you do nt see the References folder, go to the Solution Explorer tab on
the right side of the Visua l C# 2008 Express w indo w a nd selec t the Show All
Files butto n as show n be low
3) The above step will disp lay the References folder in the Solution Explorer asshow n b elow
4) Right-c lick on this Refe renc es folde r and selec t Add Referenc e item in theresulting c ontext me nu. An Add Reference dialog w ill be d isp layed :
8/3/2019 VS2008WithCad
4/14
5) Clic k on the Browse tab and naviga te to the loc ation of the AutoCAD spe c ificRefe renc e files.
Note: You will find AutoC AD spec ific refe renc e files in a c oup le o f loc a tions. If
you have Ob jec tARX SDK insta lled , you c an na viga te to the Inc lude folder (I
wo uld na viga te to C:\ObjectARX\ 2010\ inc- win32 for 32 bit p la tforms and
C:\ObjectARX\ 2010\ inc- x64) for the refe renc es. In the snapshot a bove , wenavigated to the inc-win32 folder.
Alternatively, you c an na viga te to the AutoCAD 2010 Install Folder (the defa ult
loca tion is C:\Program Files\ AutoCAD 2010) and find the referenc es there as
show n below :
8/3/2019 VS2008WithCad
5/14
6) Selec t the DLL files acmgd.dl l and acdbmgd.dl l from the loc a tion you b row se toand c lic k the OK button . These files c onta in all the nec essary .NET APIs to c reate
a g ene ric Auto CAD .NET app lic a tion.
Note: You c an selec t multip le files (two f iles in this c ase) b y pressing the Ctrl key
while selec ting them with the mouse.
7) Add the ne c essary Namespaces to the source files for co nvenient a c c ess to theAutoC AD .NET APIs. Here a re the most c om mon ly used AutoC AD .NET API
spe c ific nam espa c es:
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.EditorInput;
using Autodesk.AutoCAD.Runtime;
You c an a dd these na mespa c es near the top of the Class1.cs source file a s
show n below .
8/3/2019 VS2008WithCad
6/14
Note: We will not b e elab orating o n the purpo ses of the na me spac es and the AutoC AD
.NET APIs in this tutoria l. You c an find a lot m ore info rma tion o n th is in the refe renc es
p rovided at the e nd of this tutorial.
Creating a c omm and:
Add the c omm and method that implements the c omm and (s) you need. Here is an
examp le of the c omma nd m ethod implementing the c omma nd w e use:
// Define Command "MyCSCommand"
[CommandMethod("MyCSCommand")]
staticpublicvoid Cmd1() // This method can have any name
{
// Put your command code here
Editor ed = Application.DocumentManager.MdiActiveDocument.Editor;
ed.WriteMessage("\nMy First command implemented in CSharp .NET Express
Edition");
}
Note: We will not b e elab orating o n the c rea tion of c omma nds and the AutoCAD .NET
APIs in this tuto rial. You c an find a lot more informa tion on this in the referenc es
provided a t the e nd o f this tutorial. Here is a snap shot o f how the c om ma nd c od ewo uld look like:
8/3/2019 VS2008WithCad
7/14
Now we ha ve a functiona l AutoCAD C# .NET projec t!
Save the Projec t:
You c an save the projec t b y c lic king the FileSave A ll m enu item or hitting the
Ctrl+Shift+S keys. This w ill popup up the Save Projec t Dialog shown be low .
You m ay c hoose the Nam e, Loc a tion a nd Solution Name of the projec t a s you see fit.
Build the Projec t:
The last ste p is to Build the ap plic a tion. Use the BuildBuild Solution m enu item tobuild your ap plic ation.
8/3/2019 VS2008WithCad
8/14
You now ha ve a functiona l AutoCAD C# .NET Applic ation!
Debug ging the App lic ation:
You c an load your comp iled app lic at ion into AutoCAD now using the NETLOAD
c omm and , but youll have to m ake a few ad ditional chang es to your projec t to be
ab le to deb ug it when its running in AutoCAD.
Set Cop y Loc al p roperty to False:
1) Op en the Prop erties Tab b y c licking the ViewProperties Window menu itemor the Ctrl+W, P keys.
2) Selec t the acdbmgd refe renc e in the Refe renc es folde r in the SolutionExplorer tab a nd set its Copy Loc al p rop erty in the Properties tab to False as
show n below :
8/3/2019 VS2008WithCad
9/14
3) Do the a bo ve step for the acmgd reference a s we ll
Set the Debug App lica tion Path:
The Visua l C# 2008 Express Ed ition doe s not p rovide any method throug h its user
interfac e to spe c ify a de bug ap plic ation pa th to launc h AutoCAD from the Visual C#
2008 Express Ed ition environment. So we have to p lay a sma ll tric k here:
1) Save the p rojec t a nd c lose Visua l C# 2008 Express Ed ition2) Naviga te to the loca tion o f the p roject in Windows Exp lorer. My Projec t is in the
folder C:\ My Doc uments\ Visual Studio
2008\ Projects\ MyCSAca dAp p\ MyCSAc ad App .
3) Op en the Projec t file in a text editor of your c hoice . Ic hoose Notepa d as the text editor and my a pp lic ation projec t file is name d
MyCSAc adAp p .csproj. You will notice tha t it is an XML file. Under the XML Nod e
Add the follow ing tw o XML Node s:
Program
c:\ Program Files\ AutoCAD 2010\ ac ad .exe
Note: The StartProgram node stores the p ath o f the AutoCAD a pp lic ation
ac ad .exe. You w ill have to m od ify this pa th to the c orrec t p ath to the
ac ad .exe e xec utab le o n your mac hine. This sec tion in my M yCSAc ad App.c sproj
file looks like this a fte r I add my entries:
Program
c:\ Program Files\ AutoCAD 2010\ ac ad .exe
true
full
false
bin\ Debug\
DEBUG;TRAC Ep rompt
4
Here is a snapshot of m y CSPROJ file op ene d in notep ad :
8/3/2019 VS2008WithCad
10/14
4) Save the CSPROJ file a nd op en the p rojec t a ga in in Visua l C# 2008 ExpressEdition.
5) Build the p rojec t a ga in.6) Selec t the DebugStart Debugging Menu item or press F5. This will launc h the
AutoC AD a pp lic a tion suc c essfully from the Visua l C# 2008 Express Ed ition
debugging environment.
Loa ding our C# .NET Application into AutoCAD
1) Onc e Auto CAD is launc hed , run the NETLOAD c om mand . This will disp lay the Choo se .NET Assem bly d ialog (as shown be low):
8/3/2019 VS2008WithCad
11/14
2) Naviga te to the locat ion of your built ap p lic at ion. My ap plic ation lived in the C:\ My Doc uments\ Visual Stud io
2008\ Projec ts\ MyCSAca dApp \ MyCSAca dAp p\ bin\ Debug folder.
3)
Selec t the a pp lica tion DLL and c lick Open button. The a pplic at ion is nowloa de d into AutoCAD
4) Run the c omma nd MyCSComm and or any other co mma nd nam e that youdefined in your .NET app lica tion. Here is the o utp ut of my com mand :
8/3/2019 VS2008WithCad
12/14
Set a b reak point in the c ode
1) Set the c ursor at the line o f cod e you w ant to b rea k to.2) Clic k the DebugTog gle Break point me nu item o r p ress the F9 key. Here is a
snap shot o f the break po int at the po int of c od e w here w e set the Ed itor objec t
(The Red dot indica tes the b rea kpo int):
8/3/2019 VS2008WithCad
13/14
3) Run the c omm and (in our c ase MyCSCom ma nd ) on the AutoCAD co mm andline a nd you w ill see the b rea k po int hit:
You c an no w use the deb ugger keys like F10, F11 or Shift+F11 etc to traverse
through the co de
Congratulations, you ha ve suc cessfully used Visua l C# 2008 Exp ress Edition to
c reate a nd use a .NET ap plication in AutoCA D!
References:
1) Develope r Cente r for AutoC AD (www .autodesk.c om/ developautoca d ): Thisweb site p rovides a va riety of lea rning resource s suc h a s DevTV: Introduction to
AutoCAD .NET Programming w hic h provided an exce llent introd uc tion to the
Auto CAD .NET API and AutoCAD .NET Labs which provide s a c om prehensive self-
learning tuto ria ls. The Deve lop er Center lists ma ny other free develop me nt
resources, inc luding links to the Autod esk disc ussion g roups for the va rious
AutoC AD APIs.
2) Throug h the Inte rfac e Blog (http://through-the-interface.typepad.com/): Avery interesting and engag ing blog on a ll AutoCAD APIs and allied tec hnologies
http://usa.autodesk.com/adsk/servlet/index?id=1911627&siteID=123112http://through-the-interface.typepad.com/http://through-the-interface.typepad.com/http://usa.autodesk.com/adsk/servlet/index?id=1911627&siteID=1231128/3/2019 VS2008WithCad
14/14
3) Autodesk Developer Network: For professional support for your softwaredeve lop me nt w ork, co nside r joining the Auto desk Develop er Netw ork p rog ram .
Visit www.autodesk.com/joinadn for more de tails of the program .
http://www.autodesk.com/joinadnhttp://www.autodesk.com/joinadn