VB Net Starting Guide

  • Upload
    msagent

  • View
    253

  • Download
    1

Embed Size (px)

Citation preview

  • 8/6/2019 VB Net Starting Guide

    1/13

    Using Visua l Basic Exp ress to c rea te an 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 VB .NET app lic a tion for AutoCAD using the Visua l Basic Express develop mentenvironment. The d oc ument foc uses on set ting up a nd using the Visua l Basic 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 Basic 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 MyVBAc adApp .

    4) A new Projec t is now c rea ted and the Visua l Stud io Express window should looksom ething like this:

  • 8/6/2019 VB Net Starting Guide

    2/13

  • 8/6/2019 VB Net Starting Guide

    3/13

    Converting y our Projec t into an AutoCAD VB .NET Ap plic ation:

    1) In your Visua l Basic 2008 Express w indo w, you should b e ab 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) Go to the Solution Explorer ta b on the right side of the Visua l Basic 2008 Expresswindow selec t the Show All Files button a s 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/6/2019 VB Net Starting Guide

    4/13

    5) Clic k on the Browse tab and Naviga te to the loca tion 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 , we

    navigated 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/6/2019 VB Net Starting Guide

    5/13

    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:

    Imports Autodesk.AutoCAD.ApplicationServices

    Imports Autodesk.AutoCAD.DatabaseServices

    Imports Autodesk.AutoCAD.EditorInput

    Imports Autodesk.AutoCAD.Runtime

    You c an a dd these nam espa c es at the to p o f the Class1.vb source file a s

    show n below .

  • 8/6/2019 VB Net Starting Guide

    6/13

    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 esp 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 'MyVBCommand'

    _

    PublicSub Cmd1() ' This method can have any name

    ' Type your code here

    Dim ed As Editor = Application.DocumentManager.MdiActiveDocument.Editor

    ed.WriteMessage("My First command implemented in VB .NET Express Edition")

    EndSub

    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 e

    wo uld look like:

  • 8/6/2019 VB Net Starting Guide

    7/13

    Now we have a functiona l AutoCAD VB .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:

  • 8/6/2019 VB Net Starting Guide

    8/13

    The last ste p is to Build the ap plic a tion. Use the BuildBuild me nu item to b uild your ap plic a tion. In the c ase o f the sam ple we used , the menu item

    was BuildBuild MyVBAcadApp.

    You no w ha ve a functiona l AutoCAD VB .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 F4 key.

    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/6/2019 VB Net Starting Guide

    9/13

    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 Basic 2008 Express Ed ition doe s no t p rov ide any m ethod 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

    Basic 2008 Express Ed ition environme nt. so w e have to p lay a sma ll tric k here:

    1) Save the project and close Visual Basic 2008 Express Edition2) 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\ Projec ts\ MyVBAca dAp p\ MyVBAca dAp p.

    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

    MyVBAc adAp p .vbp roj. You w ill not ice tha t it is an XML file. Und er 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 exec utab le on your ma c hine. This sec tion in my MyVBAc ad App.vb proj

    file looks like this a fte r I add my entries:

    Program

    C:\ Program Files\ AutoCAD 2010\ ac ad .exe

    true

    full

    true

    truebin\ Debug\

    MyVBAc adAp p .xml

    42016,41999,42017,42018,42019,42032,42036,42020,42021,42022

  • 8/6/2019 VB Net Starting Guide

    10/13

    Here is a snapshot of m y VBPROJ file op ened in notepa d :

    4) Save the VBPROJ file and op en the p rojec t a ga in in Visua l Basic 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 app lic a tion suc c essfully from the Visua l Basic 2008 Express Ed ition

    debugging environment.

    Loa ding our VB .NET Application into Auto CAD

    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/6/2019 VB Net Starting Guide

    11/13

    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\ MyVBAca dAp p\ MyVBAca 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 MyVBComm and or any other c omma nd nam e that youdefined in your .NET app lica tion. Here is the o utp ut of my com mand :

    Set a b reak point in the c ode

  • 8/6/2019 VB Net Starting Guide

    12/13

    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):

    3) Run the c omm and (in our c ase MyVBCom ma nd ) on the AutoCAD co mm andline a nd you w ill see the b rea k po int hit:

  • 8/6/2019 VB Net Starting Guide

    13/13

    You c an now use the deb ugger keys like F8 or Shift+F8 etc to traverse through

    the cod e

    Congratlations, you ha ve suc cessfully used Visua l Basic 2008 Express 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 d isc 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

    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://usa.autodesk.com/adsk/servlet/index?id=1911627&siteID=123112http://through-the-interface.typepad.com/http://www.autodesk.com/joinadnhttp://www.autodesk.com/joinadnhttp://through-the-interface.typepad.com/http://usa.autodesk.com/adsk/servlet/index?id=1911627&siteID=123112