AutoCAD.NET 开发人员手册 (1) > 简介
目录
1.4 Components of the AutoCAD .NET API 4
1.4.1 Use an AutoCAD .NET API DLL. 5
1.4.2 Location of AutoCAD .NET API DLL files. 5
1.5 Overview of Microsoft Visual Studio. 5
1.5.2 Which Edition of Microsoft Visual Studio to Use. 6
1.5.3 Use COM Interoperability with .NET. 7
1.5.7 Transition from ActiveX Automation to .NET. 9
1. 简介
本简介介绍了通过托管的 .NET应用程序接口(API)显示 AutoCAD® 对象的相关概念。AutoCAD .NET API 允许自动执行任务,例如创建或修改保存在图形文件数据库中的对象,或更改自定义文件的内容。本手册覆盖了使用 Microsoft® Visual Studio® 2008, 以及 Microsoft® Visual Basic® .NET (针对本手册的 VB.NET 部分) 和 Microsoft® Visual C#® 的 AutoCAD .NET API编程语言。
1.1 本节内容
· 本书的结构
· AutoCAD .NET API 概述
· AutoCAD .NET API 组件
· Microsoft Visual Studio 概述
· 关于更多信息
· 代码样例
· 从 ActiveX Automation 移植到 .NET
1.2 本书的结构
本手册提供了怎样通过 Microsoft Visual Studio 和 VB.NET 以及 C# 编程语言使用 AutoCAD .NET API。有关使用 Microsoft Visual Studio 开发应用程序的信息可在以下主题中找到:“Microsoft Visual Studio 入门”和“使用 Microsoft Visual Studio 开发应用程序”。
那些利用 .NET 框架的非 Microsoft Visual Studio 开发环境进行开发的编程者可跳过这两节。然而,本手册中所有代码样例均按 VB.NET 和 C# 提供。
1.3 AutoCAD .NET API 概述
AutoCAD .NET API 使用户可通过编程的方法利用程序集或库显示的 AutoCAD 和图形文件的对象来操纵它们。通过显示的对象,它们可被许多不同的编程语言和环境访问。
在 AutoCAD 中实现 .NET API 的几大优点:
· 更多的编程环境可以编程访问 AutoCAD。在 .NET API出现之前,开发人员只能局限于使用 ActiveX® 自动操作和支持COM的语言、AutoLISP®和 ObjectARX 的 C++。
· 通过使用应用程序本地化的 .NET API 或显示的 ActiveX/COM 库与其它 Windows® 应用程序(例如 Microsoft Excel 和 Word)共享数据比以前要方便得多。
· .NET 框架是为32位及64位操作系统而设计,而VBA只为32位操作系统设计。
· 相对于传统编程语言,如 C++,.NET 具有更低的学习成本来访问高级接口。
对象是 AutoCAD .NET API 的主要构造块。每一个显示的对象均精确代表一个 AutoCAD 组件,它们之间又组成了不同的程序集和命名空间。AutoCAD .NET API 有许多不同类型的对象。例如:
· 直线、圆弧、文字和标注等图形对象都是对象。
· 线型与标注样式等样式设置都是对象。
· 图层、组合和块等组织结构都是对象。
· 视图和视口等图形显示都是对象。
· 甚至图形、AutoCAD 应用程序本身也是对象。
更多有关组成 AutoCAD .NET API 的一些文件的信息,请参见 AutoCAD .NET API 组件。
1.4 Components of the AutoCAD .NET API
The AutoCAD .NET API is made up of different DLL files that contain a wide range of classes, structures, methods, and events that provide access to objects in a drawing file or the AutoCAD application. Each DLL file defines different namespaces which are used to organize the components of the libraries based on functionality.
The three main DLL files of the AutoCAD .NET API that you will frequently use are:
· AcDbMgd.dll. Use when working with objects in a drawing file.
· AcMgd.dll. Use when working with the AutoCAD application.
· AcCui.dll. Use when working with customization files.
1.4.1 Use an AutoCAD .NET API DLL
Before classes, structures, methods, and events found in one of the AutoCAD .NET API related DLLs can be used, you must reference the DLL to a project. After a DLL is referenced to a project, you can utilize the namespaces and the components in the DLL file in your project.
Once a AutoCAD .NET API DLL is referenced, you must set the Copy Local property of the referenced DLL to False. The Copy Local property determines if Microsoft Visual Studio creates a copy of the referenced DLL file and places it in the same directory as the assembly file of the project when it is built. Since the referenced files already ship with AutoCAD, creating copies of referenced DLL files can cause unexpected results when you load your assembly file in AutoCAD.
1.4.2 Location of AutoCAD .NET API DLL files
The AutoCAD .NET API DLL files can be located at <drive>:\Program Files\AutoCAD 2010 or as part of the AutoCAD 2010 ObjectARX SDK which can be downloaded from http://www.objectarx.com or the Autodesk Developer Network Web site (http://www.autodesk.com/adn).
After the ObjectARX SDK is installed, the DLL files can be found in the inc-win32 and inc-x64 folders under the main install folder.
NoteThe DLLs in the ObjectARX SDK are simplified versions of the same files that ship with AutoCAD, as they do not contain dependencies on the AutoCAD user interface. It is recommended that you download and install the ObjectARX SDK, and then reference the DLL files that come with the SDK instead of those that are found in the install directory of AutoCAD 2010.
1.4.3 Procedures
To download and install the AutoCAD 2010 ObjectARX SDK
1.Launch your default Internet browser application and browse to http://www.objectarx.com.
2.On the Web page, click License & Download.
3.Fill in the required fields and select ObjectARX for AutoCAD 2010. Click Submit.
4.On the Download page, click Download Now to use the Download Manager or click Standard Download Method to use the default download method of your Internet browser.
5.Click Save or the option used to save the file to your local drive.
6.Specify a location to download the ObjectARX SDK package file.
7.Once the package file is downloaded, browse to the location you saved it to and double-click it.
The install wizard is displayed.
8.In the ObjectARX <Release> dialog box, specify a new install location or leave the default install location. Click Install.
The install wizard closes after it is finished if no problems were encountered.
To install the ObjectARX Wizard and the Managed project templates
1.Once the ObjectARX SDK is installed, browse to its install folder which is by default c:\ObjectARX <Release>.
2.After browsing to the install folder, open the Utils folder and then the ObjARXWiz folder.
3.In the ObjARXWiz folder, double-click ArxWizards.msi.
4.In the ObjectARX Wizards for AutoCAD 2010 dialog box, click Next.
5.In the Enter Your Preferred Default RDS Symbol box, enter an abbreviation of your company's name and click Next.
The text you enter for the RDS is used as a prefix to the names of the default class or commands created with the ObjectARX Wizard and project templates.
NoteRDS stands for Registered Developer Symbol, and is used to create unqiuely named commands and classes to help avoid potential conflicts with other applications.
6.On the Member Variable Wizard page, leave Replace MS Member Variable Wizard by Autodesk Member Variable Wizard selected and click Next.
7.On the Select Installation Folder page, click Browse to specify a new installation location for the wizard or leave the default location. Click Next.
8.Click Next again to install the wizard.
9.Click Close to close the installer.
To reference an AutoCAD .NET API DLL
1. In Microsoft Visual Studio, click View menu Solution Explorer to display the Solution Explorer if it is not already displayed.
2. In the Solution Explorer, on the toolbar along the top, click Show All Files.
3. Right-click the References node and click Add Reference.
4. In the Add Reference dialog box, Browse tab, select the DLL file that contains the library you want to use and click OK.
5. In the Solution Explorer, click the plus sign to the left the References node to expand it.
6. Select the referenced library from the References node.
7. Right-click over the selected reference and click Properties.
8. In the Properties window, click the Copy Local field and select False from the drop-down list.
1.5 Overview of Microsoft Visual Studio
Microsoft Visual Studio is an object-oriented programming environment that runs independently of AutoCAD. While Microsoft Visual Studio is external to AutoCAD and other applications, it is able to interact with applications that expose either a native .NET API or ActiveX/COM library.
1.5.1 本节内容
· Which Edition of Microsoft Visual Studio to Use
· Use COM Interoperability with .NET
· 依赖性和限制
1.5.2 Which Edition of Microsoft Visual Studio to Use
Microsoft Visual Studio is available in multiple versions and editions. To use the .NET API for AutoCAD 2010, you need to use:
· Microsoft Visual Studio 2008 with Service Pack 1
· Microsoft .NET Framework 3.5 with Service Pack 1
If you are using AutoCAD 2007 through AutoCAD 2009, you should use:
· Microsoft Visual Studio 2005
· Microsoft .NET Framework 2.0 or later
Microsoft Visual Studio is offered in two editions: for free and for pay. The free edition is known as Microsoft Visual Studio 2008 Express Edition, while the for pay editions vary by name and price due to the different development tools that are incorporated into them. Microsoft Visual Studio 2008 Standard Edition is the entry level edition which provides improved debugging over Microsoft Visual Studio 2008 Express Edition along with a number of other features. The most common edition of Microsoft Visual Studio used by developers is Microsoft Visual Studio 2008 Professional Edition.
NoteWhile it is possible to use Microsoft Visual Studio Express with the AutoCAD .NET API, this guide assumes you are using one of the other versions such as Microsoft Visual Studio 2008 Standard Edition or Microsoft Visual Studio 2008 Professional Edition.
There are four main advantages to using Microsoft Visual Studio:
· Robust and accessible development environment that has a modest learning curve.
· VBA and VB.NET syntax are similar, which makes it an ideal environment for existing VBA users.
· Visually intuitive and extensive dialog box creation tools.
· Projects can be built as a standalone executable or DLL assembly which can then be loaded into AutoCAD for execution.
NoteUnlike VBA projects, .NET applications do not suffer from performance degradation when loaded and run in 64-bit AutoCAD.
关于更多信息 on the different editions of Microsoft Visual Studio, see http://www.microsoft.com/vstudio and http://www.microsoft.com/express.
1.5.3 Use COM Interoperability with .NET
Microsoft Visual Studio can utilize both native .NET and COM interfaces in the same project. By utilizing COM interop, you can migrate existing code that might have been written in Visual Basic 6 or VBA without having to completely rewrite it. To access AutoCAD automation objects from a project created in Microsoft Visual Studio, create references to the following files:
· The AutoCAD 2010 type library, acax18enu.tlb, located at <drive>:\Program Files\Common Files\Autodesk Shared.
· The AutoCAD/ObjectDBX Common 18.0 type library, axdb18enu.tlb, located at <drive>:\Program Files\Common Files\Autodesk Shared.
NoteThe previous mentioned type libraries are also available as part of the ObjectARX SDK. For information on downloading and installing the ObjectARX SDK, see Components of the AutoCAD .NET API.
These references will make the following primary interop assemblies available:
· Autodesk.AutoCAD.Interop.dll (for AutoCAD-specific types)
· Autodesk.AutoCAD.Interop.Common.dll (for types shared by ObjectDBX鈩� host applications)
The interop assemblies are located in the global assembly cache; they map automation objects to their .NET counterparts.
After you reference the type libraries, you can declare AutoCAD-based variables in Microsoft Visual Studio, as in the following examples:
Dim objAcApp As Autodesk.AutoCAD.Interop.AcadApplication
Dim objLine As Autodesk.AutoCAD.Interop.Common.AcadLine
Autodesk.AutoCAD.Interop.AcadApplication objAcApp;
Autodesk.AutoCAD.Interop.Common.AcadLine objLine;
Utilizing the interop assemblies can make the transitioning your VBA projects over to VB.NET much easier. However, in order to take full advantage of everything that .NET and the AutoCAD .NET API have to offer, you will need to rewrite your existing VBA code.
Create and Reference the AutoCAD Application
AutoCAD 2010 .NET applications can utilize the same type library (acax18enu.tlb) as AutoCAD automation projects. The type library is located in <drive>:\Program Files\Common Files\Autodesk Shared.
AutoCAD 2010 .NET applications also use the same version-dependent ProgID for the CreateObject, GetObject, and GetInterfaceObject functions. For example, CreateObject ("AutoCAD.Application.18") allows you to create an instance of AutoCAD and get an object that represents the new instance of the application.
1.5.4 依赖性和限制
Unlike ActiveX Automation, there are fewer issues with library conflicts when other applications are installed, reinstalled, or uninstalled. The reason for fewer compatibility issues is that the .NET Framework is a standardized platform. However, you can still run into dependency issues. To avoid dependency issues with the .NET Framework, be sure to use the same or an earlier version of the .NET Framework with your VB.NET or C# project that AutoCAD 2010 uses. For information on which version of the .NET Framework you should be referencing, see 使用哪个 Microsoft Visual Studio 编辑器.
1.5.5 关于更多信息
This guide assumes that you have working knowledge of either the VB.NET or C# programming languages, and does not attempt to duplicate or replace the abundance of documentation available on either of these programming languages. If you need 更多信息 on VB.NET or C#, and using Microsoft Visual Studio, see the Help system for Microsoft Visual Studio developed by Microsoft, available from the Help menu in Microsoft Visual Studio.
1.5.6 代码样例
This guide and the AutoCAD 2010 ObjectARX SDK together contain a large number of sample projects, subroutines, and functions that demonstrate the use of the classes, structures, methods, properties, and events that make up the AutoCAD .NET API.
You can also find sample projects that demonstrate some of the aspects of the AutoCAD .NET API on the Autodesk website at http://www.autodesk.com/developautocad. These sample projects show a wide range of fuctionality, from extracting AutoCAD drawing data into Microsoft Excel spreadsheets to drawing and performing stress analysis on an electrical transmission tower.
Many of these samples show how to combine various aspects of the VB.NET and C# programming languages with the power of the AutoCAD .NET API to create custom applications.
Additionally, sample code in the AutoCAD .NET Developer's Guide can be copied from the Help files, pasted directly into an open code editor window in Microsoft Visual Studio, and then built and loaded in AutoCAD. At the top of most code samples in this guide are the namespaces that are required for that particular sample. Add those that are need to top of the code window.
NoteThe 代码样例 in the Help files contains limited error handling to keep the concepts simple and easy to read. You should apply additional error handling and checking when using any 代码样例 in your project. For more information on error handling, see 处理错误.
1.5.7 Transition from ActiveX Automation to .NET
AutoCAD 2010 continues to support VBA, but the VBA components must be enabled by downloading and installing them from http://www.autodesk.com/vba-download.
While there is no established release in which VBA will no longer be supported in AutoCAD, we recommend that you begin migrating your existing VBA projects to ensure that you are ready when VBA support is dropped. We recommend that you develop any new applications with Microsoft Visual Studio and the AutoCAD .NET API, AutoLISP, or C++ and ObjectARX.
You can continue to use the AutoCAD COM Automation library in .NET to help make your transition from VBA to VB.NET easier, see 使用 COM 与 .NET 协作. If you are new to .NET, see Microsoft Visual Studio 入门 for basic information on working with Microsoft Visual Studio and the AutoCAD .NET API.