12
December 2-5, 2003 MGM Grand Hotel Las Vegas Visual Studio.NET for AutoCAD Programmers Speaker Name: Andrew G. Roe, P.E. Class Code: CP32-3 Class Description: In this class, we'll introduce the Visual Studio .NET environment, including VB .NET, C# .NET, and C++ .NET, and discuss how AutoCAD® programmers can tap into the multi-language platform. Using .NET, developers have a new alternative to automate AutoCAD and more readily exchange code, develop Internet- based applications, and build more sophisticated programs. Class Outline: Introduction to VS.NET Overview of the .NET Environment Building a .NET Application Converting an Existing VB Application to .NET Other Possibilities Getting More Information About the Speaker: Andrew G. Roe is a registered civil engineer and president of AGR Associates, Inc., a technology consulting firm based in Minneapolis, MN. He has over 18 years of experience in engineering, computer applications, and technical writing, and is the author of Using Visual Basic with AutoCAD, published by Autodesk Press. He has developed and documented custom applications for numerous clients, developed training material for Microsoft and Autodesk products, and written regularly for Engineering News-Record and CADENCE magazines. [email protected]

Visual Studio.NET for AutoCAD Programmers - AUGI · Visual Studio.NET for AutoCAD Programmers ... developed training material for Microsoft ... To demonstrate how to use .NET with

  • Upload
    lynhi

  • View
    228

  • Download
    7

Embed Size (px)

Citation preview

December 2-5, 2003 ◊ MGM Grand Hotel Las Vegas

Visual Studio.NET for AutoCAD Programmers

Speaker Name: Andrew G. Roe, P.E.

Class Code: CP32-3

Class Description: In this class, we'll introduce the Visual Studio .NET environment, including VB .NET, C# .NET, and C++ .NET, and discuss how AutoCAD® programmers can tap into the multi-language platform. Using .NET, developers have a new alternative to automate AutoCAD and more readily exchange code, develop Internet-based applications, and build more sophisticated programs.

Class Outline:

• Introduction to VS.NET

• Overview of the .NET Environment

• Building a .NET Application

• Converting an Existing VB Application to .NET

• Other Possibilities

• Getting More Information

About the Speaker: Andrew G. Roe is a registered civil engineer and president of AGR Associates, Inc., a technology consulting firm based in Minneapolis, MN. He has over 18 years of experience in engineering, computer applications, and technical writing, and is the author of Using Visual Basic with AutoCAD, published by Autodesk Press. He has developed and documented custom applications for numerous clients, developed training material for Microsoft and Autodesk products, and written regularly for Engineering News-Record and CADENCE magazines.

[email protected]

Visual Studio .NET for AutoCAD Programmers

2

Introduction to VS .NET What Is VS.NET?

Visual Studio .NET is an integrated set of development tools for building custom applications using Visual Basic .NET, Visual C# .NET, Visual C++ .NET, and Visual J# .NET. The same integrated development environment (IDE) is used for all programming languages, which allows developers to share components and build multi-language solutions. .NET is built around a common language specification (CLS), which provides the same services to individual languages.

Why Use It?

VS .NET offers several advantages over previous programming environments:

• State-of-the-Art Programming Tools

• Multi-Language Solutions

• Internet Applications

• Mobile Applications

• The Wave of the Future

Why Use It With AutoCAD? • Drive AutoCAD with Stand-alone Applications (COM Interface)

• Develop ObjectARX Applications (Visual Studio 2002 with AutoCAD 2004)

• Take Advantage of Advanced Programming Tools

• Use Components Developed in Multi-Language Environments

• Prepare for the Future …

Hurdles • Learning Curve

• Cost

Language Summary Chart Below is a brief summary of the various languages included in VS .NET.

Language VB .NET C# .NET C++ .NET J# .NET Predecessor Visual Basic -- C++ J++ Year Introduced 1990 2001 1983 1996 Key Characteristic Rapid Application

Development (RAD) New Hybrid of

C/C++ High-End Apps Web Apps

Visual Studio .NET for AutoCAD Programmers

3

Overview of the .NET Environment The IDE

VS .NET provides a single IDE for all programming languages. It is similar to familiar IDEs such as that of Visual Basic 6.0, with a few differences.

Some highlights:

Form Designer – Allows you to add controls to a form, arrange them, and write code for their events; much improved over VB 6, allowing you to easily resize, anchor, and dock forms; click tab above to switch to Code window.

Properties window – displays design-time properties and events of selected objects; click buttons above to sort and categorize.

Solution Explorer – Similar to the Project Explorer in VB; displays projects and their files; click buttons above to display code and forms.

Toolbox – displays controls and other items available for .NET projects. The tabs and items available from the Toolbox change, depending upon the designer or editor currently in use.

Visual Studio .NET for AutoCAD Programmers

4

Fundamentals Still Applicable While .NET includes numerous new concepts and tools, fundamentals from previous programming environments still apply. Here are some of the key terms of interest to AutoCAD programmers.

Objects – Generically Speaking In programming-speak, objects are building blocks for applications. Objects can be graphical items, such as forms, buttons, and text boxes, as well as code and data. In short, an object is something that acts as a unit.

Objects – AutoCAD Style In AutoCAD, a wide variety of application-specific objects are exposed, where they can be accessed programmatically. This includes tangible drawing objects, such as lines, arcs, and text, as well as other elements such as layers, groups, and viewports. These objects are all part of the AutoCAD Object Model. Discipline-specific products, such as Architectural Desktop, Land Desktop, and Mechanical Desktop, expose additional objects unique to each particular discipline.

Properties and Methods Properties are attributes or characteristics of objects. Methods are functions that perform actions on an object. In a grammatical analogy, objects are nouns, properties are adjectives, and methods are verbs.

Classes Classes are templates for creating objects. An object is a usable instance of a class. Each instance is a unique copy of its class.

Events Events are actions recognized by an object. A mouse click is an event, as is the pressing of a key at the keyboard. AutoCAD has unique events, such as the modification of a drawing object and the saving of a drawing.

Variables and Expressions Variables hold or store data in a specific memory location. Variables can represent numbers (Integer, Double, etc.), text (String), objects (Object, AcadLine, AcadText, etc.), dates (Date), and various other data types.

Program Control and Logic While .NET offers some new twists in program control and logic, some of the familiar approaches still apply. For example, in VB.NET you still use the familiar If…Then and Select Case structures to perform decision branching.

Differences Between VB.NET and VB 6 (VB.NET is not VB 7!) • Common Language Runtime (CLR)— Source code is converted into Microsoft intermediate

language (MSIL), a set of instructions that can be converted to native code. Some rework of the VB language was required to conform to CLS.

• Data Types—Several new types; no more Variants; multiple variables declared on one line.

• No Set Keyword—Instead of Set MyObject = …, it’s just MyObject = …

• Inheritance—Allows you to define classes that serve as the basis for new classes.

• Exception Handling—Replaces error handling; In VB. NET, Try…Catch… replaces On Error….

• Garbage Collection—Automatically releases unused objects, optimizing memory management.

• Menu Editing—Menus can be designed visually, much easier than in VB 6.

• Forms and Controls—New IDE allows docking/anchoring of controls, unique controls, etc.

• Command Line Compiler—Allows testing of code fragments outside the IDE.

• Others…

Visual Studio .NET for AutoCAD Programmers

5

Building a .NET Application A VB.NET Example To demonstrate how to use .NET with AutoCAD, the following example shows how to build a VB.NET application that starts AutoCAD, draws an AutoCAD entity, extracts some information about AutoCAD entities, and quits AutoCAD.

1. Start VS.NET to display the Start Page.

2. On the Start Page, click New Project to display the New Project dialog box.

3. In the New Project dialog box, select a project type of Visual Basic Projects, use the Windows Application template, specify a project name and location, and click OK.

4. In the Solution Explorer, right-click References and select Add Reference.

Visual Studio .NET for AutoCAD Programmers

6

5. In the Add Reference dialog box, select the COM tab, then select AutoCAD 2004 type library. Click Select and OK to close the dialog box.

6. In the Code window, declare variables for the AutoCAD application object and a Boolean variable to track whether AutoCAD is running, as shown in the following code.

Public Class Form1 Inherits System.Windows.Forms.Form Dim objAcad As AutoCAD.AcadApplication Dim blnStarted As Boolean

7. In the Form Designer, insert four buttons named cmdConnect, cmdDisconnect, cmdDrawLine, and cmdIDEntity, and arranged as shown below.

8. Double-click on the Connect to AutoCAD button and assign the code show below to it. This code checks to see if AutoCAD is running, and if not, starts it.

Private Sub cmdConnect_Click(ByVal sender As System.Object, ByVal e As _ System.EventArgs) Handles cmdConnect.Click If blnStarted <> True Then Try objAcad = GetObject(, "AutoCAD.Application.16") Catch exc As Exception objAcad = CreateObject("AutoCAD.Application.16") End Try cmdDisconnect.Enabled = True cmdDrawLine.Enabled = True cmdIDEntity.Enabled = True blnStarted = True blnStarted = True MsgBox(objAcad.Name + vbCrLf + "Version " + objAcad.Version) objAcad.Visible = True End If End Sub

9. In a similar manner, assign the code shown below to the Quit AutoCAD button. This code executes the Quit method to close AutoCAD.

Private Sub cmdDisconnect_Click(ByVal sender As System.Object, ByVal e As _ System.EventArgs) Handles cmdDisconnect.Click If blnStarted = True Then objAcad.Quit() objAcad = Nothing Me.Close() End If End Sub

10. Assign the following code to the Draw Line button and allow the user to draw a line.

Visual Studio .NET for AutoCAD Programmers

7

Private Sub cmdDrawLine_Click(ByVal sender As System.Object, ByVal e As _ System.EventArgs) Handles cmdDrawLine.Click Dim dblStartPoint(2) As Double Dim dblEndPoint(2) As Double Dim objMyLine As AutoCAD.AcadLine dblStartPoint(0) = 0 dblStartPoint(1) = 0 dblStartPoint(2) = 0 dblEndPoint(0) = 5 dblEndPoint(1) = 5 dblEndPoint(2) = 0 objMyLine = objAcad.ActiveDocument.ModelSpace.AddLine(dblStartPoint,_ dblEndPoint) objAcad.ZoomExtents() End Sub

11. Assign the following code to the ID Entity button and allow the user to determine an entity type.

Private Sub cmdIDEntity_Click(ByVal sender As System.Object, ByVal e As _ System.EventArgs) Handles cmdIDEntity.Click Dim objEnt As AutoCAD.AcadEntity Dim objPoint As Object objAcad.ActiveDocument.Utility.GetEntity(objEnt, objPoint, "Select _ an object.") MsgBox("The object name is: " & objEnt.ObjectName) End Sub

12. Add the following code to the Form Load event.

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As _ System.EventArgs) Handles MyBase.Load blnStarted = False cmdDisconnect.Enabled = False cmdDrawLine.Enabled = False cmdIDEntity.Enabled = False End Sub

13. Run the program and click the Start AutoCAD button to activate AutoCAD.

14. Click the Draw Line button to create a line, followed by the ID Entity button to obtain information about the entity.

15.Click the Quit AutoCAD button to close AutoCAD.

The Same Example in C# To build the same example in C#, the procedure is similar. When selecting a project type, select Visual C# Projects. The equivalent code segments in C# are as follows.

public class Form1 : System.Windows.Forms.Form { private AcadApplication objAcad; private AcadDocument objAcadDoc; bool started;

Visual Studio .NET for AutoCAD Programmers

8

private void cmdConnect_Click(object sender, System.EventArgs e) { if (started) return; try { objAcad=(AcadApplication)Marshal.GetActiveObject("AutoCAD.Application.16"); started = true; } catch { try { objAcad = new AutoCAD.AcadApplicationClass(); objAcad.Visible = true; started = true; } catch { throw; } } cmdDisconnect.Enabled = true; cmdDrawLine.Enabled = true; cmdIDEntity.Enabled = true; } private void cmdDisconnect_Click(object sender, System.EventArgs e) { if(started) { objAcad.Quit(); started = false; } cmdDisconnect.Enabled = false; cmdDrawLine.Enabled = false; cmdIDEntity.Enabled = false; } private void cmdDrawLine_Click(object sender, System.EventArgs e) { if (!started) return; double[] StartPoint = new double[3]; double[] EndPoint = new double[3]; StartPoint[0] = 0; StartPoint[1] = 0; StartPoint[2] = 0; EndPoint[0] = 5; EndPoint[1] = 5; EndPoint[2] = 0; objAcad.ActiveDocument.ModelSpace.AddLine(StartPoint, EndPoint); objAcad.ZoomExtents(); }

Visual Studio .NET for AutoCAD Programmers

9

private void cmdIDEntity_Click(object sender, System.EventArgs e) { Object objEnt = new Object(); Object objPoint = new Object(); objAcad.ActiveDocument.Utility.GetEntity(out objEnt, out objPoint, "Select an object"); AcadEntity ent; ent = (AcadEntity)objEnt; MessageBox.Show("The object name is: " + ent.ObjectName); } private void Form1_Load(object sender, System.EventArgs e) { cmdDisconnect.Enabled = false; cmdDrawLine.Enabled = false; cmdIDEntity.Enabled = false; } } }

Visual Studio .NET for AutoCAD Programmers

10

Converting a VB Application to .NET VB .NET includes an Upgrade Wizard to convert programs developed in VB 6.0. The steps for using the Upgrade Wizard are as follows.

1. Start VS.NET. From the File menu, select Open>Project.

2. In the Open Project dialog box, select a Visual Basic 6.0 project (.vbp) file. This launches the Visual Basic Upgrade Wizard.

3. Click Next to display the second page of the wizard, and choose from the available upgrade options.

4. On the third page of the wizard, enter the location where the new project will be created.

5. On the fourth page of the wizard, click Next to begin the upgrade process.

6. In Solution Explorer, double-click the _UpgradeReport.htm node to open the upgrade report.

7. Review the upgrade report and fix any errors. Examine warnings to ensure they do not affect application behavior.

8. On the Debug menu, choose Start. Run the application and make sure the behavior is the same as it was in Visual Basic 6.0.

Visual Studio .NET for AutoCAD Programmers

11

Other Possibilities ObjectARX ObjectARXTM (AutoCAD Runtime Extension) is a compiled-language programming environment for developing AutoCAD applications. Using ObjectARX, you can create applications that run in the same address space as AutoCAD and operate directly with core AutoCAD data structures and code. This allows you to create custom AutoCAD objects and new commands that operate exactly the same way as native AutoCAD commands.

You can use VS .NET to develop ObjectARX applications for AutoCAD 2004. VS .NET 2002 is the only version currently supported. VS.NET 2003 is not supported due to compatibility problems. You must use Visual C++ 6.0 when building ObjectARX applications for AutoCAD 2000, 2000i or 2002.

Developing Multi-Language Applications Because the VS .NET framework provides a common application programming interface (API) for all of its programming languages, developers can choose which language to use. A VS .NET solution can contain components from multiple languages, meaning you could have a component developed in VB .NET, along with another component developed in C# .NET, and another developed in C++ .NET—all part of the same solution. On large projects with multiple developers skilled in different languages, this opens up new doors for collaboration.

Internet Applications VS .NET greatly simplifies development of distributed Internet applications. It allows you to create Web Forms, which are used to create programmable Web pages. Using Web Forms, you can create Web pages by dragging and dropping controls onto the designer and then adding code, similar to the way that you create Visual Basic forms.

VS .NET also allows you to create XML Web services—applications that can receive requests and data using Extensible Markup Language (XML) over the Internet. XML provides a method for describing structured data and exchanging data across disparate applications. VS .NET includes an XML Designer to help edit XML and create XML schemas.

Future Possibilities Autodesk is actively pursuing the development of a native .NET interface for a future release of AutoCAD. This holds the promise of delivering the power of ObjectARX through the more user-friendly VB .NET.

Visual Studio .NET for AutoCAD Programmers

12

Getting More Information Online Help The VS .NET online help system includes a vast amount of information about using the product, including sample code, links to Web sites, and more. Compared with earlier online help systems, it can be a bit intimidating initially due to the volume of information, but use it frequently to get comfortable with it. From the Help menu, select one of the following:

• Dynamic Help—to get help on the current operation

• Contents—a list of topics arranged hierarchically

• Index—a list of topics arranged alphabetically

• Search—to search for a specific topic or question

Microsoft Web Sites Information can be found in several locations. A good starting place is the Microsoft Knowledge Base.

http://support.microsoft.com/

Another is a special site set up for .NET developers.

http://www.gotdotnet.com/

Online videos are also available.

http://msdn.microsoft.com/vbasic/using/migrating/default.aspx

Autodesk Web Sites Autodesk-specific sites about .NET are still in development, but some information is available on discussion groups and the Autodesk Support page. Autodesk Developer Network (ADN) members have access to additional information via the Knowledge Base for ADN members.

http://discussion.autodesk.com/ (Discussion Groups)

http://usa.autodesk.com/ (Support)

Independent Web Sites A variety are available, and the landscape changes often. Use an Internet search engine to take a peek.

Periodicals Electronic and print magazines are available. Here are a couple of popular ones.

www.visualstudiomagazine.com (Visual Studio Magazine)

www.devx.com/vsm/ (a clearinghouse of developer information)

Books Many VS .NET books are available. Check your local bookstore or online source.