AutoCAD. Net: Chapter 2 Hello World

Source: Internet
Author: User

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) create a class library.

2) 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 acmgd. DLL, and then select and open. 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> Object Browser" menu item) to browse the classes provided by the added two managed modules.

4) import applicationservices, editorinput, and runtime namespaces.

Using Autodesk. AutoCAD. applicationservices;
Using Autodesk. AutoCAD. editorinput;
Using Autodesk. AutoCAD. runtime;

5) Add the helloworld command. To add commands that can be called in AutoCAD, you must use the "commandmethod" attribute. This attribute is provided by the Autodesk. AutoCAD. runtime namespace.

[Commandmethod ( " Helloworld " )]
Public   Void Helloworld ()
{
Editor ed=Application. documentmanager. mdiactivedocument. Editor;
Ed. writemessage ("Hello World");
}

6) debug in AutoCADProgramAllows Visual Studio. NET to start an AutoCAD process. In solution manager, right-click "helloworld" and select "properties ". In the helloworld properties page dialog box, 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 first, and then AutoCAD will be automatically started, and it will stop when there is an error after compilation.

7) manually load the Class Library:
Enter netload In the AutoCAD command line. The "Select. NET component" dialog box is displayed. Select the "helloworld. dll" generated above and open it.

8) automatically load the Class Library:
(1) In the Project Properties dialog box --> debug tag --> enable options --> enter the following in the command line parameters:
/Nologo/B "... \ Start. scr"
Let CAD automatically execute the start. scr file in the command execution project directory.
(2) create a text file in the root directory of the project directory named "start. scr" and enter the following text in the file:
Netload "... \ .. \ bin \ debug \ lubanren_2008.dll"
(3) Press F5.

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 traceCode. There are several items in the Debug menu of l studio. Net that can be used to track program running.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.