Two development of AutoCAD using C # (I.)

Source: Internet
Author: User
Tags reference split visual studio
As we all know, the main tools used in two development of AutoCAD are: Objectarx,vba,vlisp. But their advantages and disadvantages are obvious: Objectarx powerful, high programming efficiency, but its disadvantage is that programmers must master VC + +, and this language is very difficult to learn; While VBA and Vlisp are easy to start with, they seem powerless to develop large programs. Is there a language that can combine their merits and avoid their shortcomings? The answer is yes, that is Microsoft's new 21st Century programming language C #. About C # Detailed introduction, you can refer to the relevant articles.
C # is through the AutoCAD ActiveX of this bridge to and AutoCAD communication between. AutoCAD ActiveX enables the user to operate AutoCAD from within or outside of AutoCAD programmatically. It does this by displaying the AutoCAD object to the "outside world". Once these objects are displayed, many different programming languages and environments can access them. About the situation of AutoCAD ActiveX, we can refer to the help of AutoCAD.
Oh, said so much boring, or let us through a concrete example to illustrate how to use C # for AutoCAD two times development. Before introducing the example, let's talk about the configuration:
(1) Visual Studio. NET (2003 and 2002 are OK, I use 2002)

(2) AutoCAD2000 version (I use 2004)
This example is very simple, is the form created through C # to start AutoCAD and draw a straight line. The following are the specific steps of programming:
(1) Build a C # Windows application through Visual Studio. Net.
(2) In Solution Explorer, right-click the References tab, select Add Reference in the pop-up menu, and in the Add Reference dialog box, select the AutoCAD Type Library in the Drop-down list box under the COM tab Items (Note: Different versions of CAD have different numbers), click the Select button on the right, and finally click the OK button below.
(3) Add two text boxes and a button to the C # form to enter the coordinates of the starting line, the end point, and draw a line in CAD. The following is a major explanation for the added code.
(a) Add at the beginning of the program: using autocad;//import AutoCAD reference space
(b) Add in the Variable Declarations section of the form: private acadapplication a;//declare AutoCAD object
(c) by adding in the constructor section of the form: A=new acadapplicationclass ();//Create an AutoCAD object
a.visible=true;//makes AutoCAD visible
(d) By adding in the message handler function of the button:
Double[] Startpoint=new double[3]; Declare line start coordinates
Double[] Endpoint=new double[3];//declares line endpoint coordinates
String[] Str=textbox1.text.split (', ')//Take out the line start coordinate enter the value of the text box, 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 str array to double type
Str=textbox2.text.split (', '); Fetch the line's endpoint coordinates enter the value of the text box
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 try compiling it. As for the use of the above statements, I will explain them in detail in the next lecture.



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.