Using C # For secondary development of AutoCAD (1)

Source: Internet
Author: User

As we all know, the main tools used for secondary development of AutoCAD are ObjectARX, VBA, and vlisp. But their advantages and disadvantages are obvious: ObjectARX has powerful functions and high programming efficiency, but its disadvantage is that programmers must master VC ++, which is very difficult to learn; although VBA and vlisp are simple and easy to useProgramIt seems powerless. Is there a language that can combine their advantages to avoid their shortcomings? The answer is yes, that is, the new 21st century launched by Microsoft.Programming LanguageC #. For more information about C #, seeArticle.
C # communicates with AutoCAD through the bridge of AutoCAD ActiveX. AutoCAD ActiveX allows you to operate AutoCAD programmatically from inside or outside AutoCAD. It does this by displaying the AutoCAD Object to the "external world. Once these objects are displayed, they can be accessed by many different programming languages and environments. For information about AutoCAD ActiveX, refer to the help provided by AutoCAD.
Well, let's use a specific example to illustrate how to use C # For secondary development of AutoCAD. Before introducing the example, let's talk about the related Configuration:
(1) Visual Studio. NET (both 2003 and 2002 are supported. I use 2002)

(2) AutoCAD2000 or above (I use 2004)
This example is very simple. It is to start AutoCAD and draw a straight line through the form created by C. The procedure is as follows:
(1) create a C # windows application through Visual Studio. NET.
(2) In Solution Explorer, right-click the reference tab and select Add reference from the pop-up menu ", in the "add reference" dialog box, select the "AutoCAD 2004 Type Library" item in the drop-down list box under the "com" tab (Note: different versions of CAD have different numbers ), click the "select" button on the right and then click the "OK" button below.
(3) add two text boxes and a button to the C # form to enter the coordinates of the starting and ending points of the line and draw a line in CAD. The following describes Code .
(A) Add using AutoCAD at the beginning of the program; // import the AutoCAD reference space.
(B) Add private acadapplication A to the variable declaration part of the form; // declare the AutoCAD Object
(C) Add a = new acadapplicationclass () in the form constructor; // create an AutoCAD Object
A. Visible = true; // make AutoCAD visible
(D) Add the following to the message processing function of the button:
Double [] startpoint = new double [3]; // declares the starting coordinate of a straight line.
Double [] endpoint = new double [3]; // declares the coordinate of the end point of a line.
String [] STR = textbox1.text. Split (','); // retrieves the value of the text box input from the coordinate of the starting line. The input mode of the text box is "x, y, z"
For (INT I = 0; I <3; I ++)
Startpoint [I] = convert. todouble (STR [I]); // convert the STR array to the Double Type
STR = textbox2.text. Split (','); // retrieves the value of the input text box from the end coordinate of the line.
For (INT I = 0; I <3; I ++)
Endpoint [I] = convert. todouble (STR [I]);
A. activedocument. modelspace. addline (startpoint, endpoint); // draw a straight line in AutoCAD
A. application. Update (); // update display
Well, it's easy. You can compile it. I will introduce the usage of some of the above statements in detail in the next lecture.

Related Article

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.