In this chapter, we will use Visual Studio. NET to create a new class library project. Through this project, you can create a. Net DLL file that can be loaded by AutoCAD. This DLL file will add a new command named "helloworld" to AutoCAD. After you run this command, the "Hello World" text is displayed on the AutoCAD command line.
1)Start Visual Studio. NET and choose "File> New> project" (File> New> project ). In the Create Project dialog box, select Visual C # project as the project type, select the class library template, enter lab1 in the Project Name box, and select the project storage location. Click OK to create a project.
2)In the class1.cs file of the project, a public class "class1" has been automatically created by the system. Next, add the command to this class. To add commands, you must use AUTOCAD. Net to host the encapsulation class. These managed encapsulation classes are included in two managed modules. To add references to these two managed modules, right-click "Reference" and select "add reference ". In the Add reference dialog box that appears, select Browse ". In the select component dialog box, select the installation directory of AutoCAD 2006 (C: \ Program Files \ AutoCAD 2006 \), and find "acdbmgd. DLL "and then select and open it. Select Browse again, find acmgd. dll in the installation directory of AutoCAD 2006, and open it. After these two components are added, click "OK" in the "add reference" dialog box. As their names indicate, acdbmgd. dll contains the objectdbx hosting class, while acmgd. dll contains the AutoCAD hosting class.
3)Use the Object Browser (Visual Studio. NET's "View> other Windows> Object Browser" menu item) to browse the classes provided by the added two managed modules. Expand the "AutoCAD. Net managed wrapper" Object (displayed as acmgd In the Object Browser), and we will use the class in this object throughout the tutorial. In this chapter, we will use an instance of the "Autodesk. AutoCAD. editorinput. Editor" class to display text in the AutoCAD command line. Expand "objectdbx. net managed wrapper "Object (displayed as acdbmgd In the Object Browser ), the classes in this object will be used to access and edit entities in the AutoCAD Drawing (this part will be described in later sections ).
4)After referencing the ObjectARX. Net encapsulation class, we can import them. Import the applicationservices, editorinput, and runtime namespaces before the statements of the class1 class (at the top of the class1.cs file.
Using Autodesk. AutoCAD. applicationservices;
Using Autodesk. AutoCAD. editorinput;
Using Autodesk. AutoCAD. runtime;
5)Next, add the command to class class1. To add commands that can be called in AutoCAD, you must use the "commandmethod" attribute. This attribute is provided by the runtime namespace. Add the following attributes and functions to class1.
[Commandmethod ("helloworld")]
Public void helloworld ()
{}
6)When the "helloworld" command is run in AutoCAD, The helloworld function defined above will be called. In this function, an editor class instance is created. The editor class has methods related to accessing the AutoCAD command line. It also includes selecting objects and other important functions. The editor object of the current AutoCAD activity document can be accessed using the application class. After an editor object is created, you can use its writemessage method to display the "Hello World" text in the command line. Add the following code to the helloworld function:
Editor ED = application. documentmanager. mdiactivedocument. Editor;
Ed. writemessage ("Hello World ");
7)To debug this program in AutoCAD, you can enable Visual Studio. NET to start an AutoCAD process. In solution manager, right-click "lab1" and select "properties ". On the properties page of lab1, select "Configure Properties> debug ". In the "Start" item, select "debug mode" as "program", click the ellipsis button on the right of "Start Program", and then select acad.exe under the AutoCAD 2006installation directory. After setting, press F5 to start an AutoCAD process. In this way, your program will be compiled and AutoCAD will be automatically started, and it will be stopped when there is an error after compilation. Correct any errors you may encounter.
8)The "netload" command is used to load the hosted program. Enter netload In the AutoCAD command line. The "Select. NET component" dialog box is displayed. Select the "lab1.dll" generated above and open it.
9)Enter "hellowworld" in the command line ". If everything goes well, the command line will display the "Hello World" text. Switch to Visual Studio. NET and add a breakpoint to the Ed. writemessage ("Hello World"); statement. Run the helloworld command again in AutoCAD, and you will notice that you can track the code running. There are several items in the Debug menu of l studio. Net that can be used to track program running.
From: http://bbs.beiweihy.com.cn/thread-17826-1-1.html
If you have time, check the commandmethod attribute. You will find that it has seven different forms. In the above example, we use the simplest form, which has only one input parameter (command name ). You can use other methods to control how commands work. For example, you can determine the command group name, global and local names, and command Identifiers (how to run commands.