Using C # For secondary development of AutoCAD (3)

Source: Internet
Author: User

The main content of this lecture is to introduce the AutoCAD Object Model. If you understand the development of AutoCAD in VBA, this part of content should be super simple.
An object is the main construction block of the AutoCAD ActiveX interface. Each displayed object accurately represents an AutoCAD component. The main objects of the AutoCAD ActiveX interface are:
• Graphic objects such as straight lines, arcs, text, and labels.
• Style setting objects such as line style and annotation Style
• Layers, groups, blocks, and other organizational structure objects
• Graphical display objects such as views and views.
• Graphics and AutoCAD applicationsProgramItself is also an object
The root object of all objects is the AutoCAD application itself, which is represented by the acadapplication class. You can use the method described in the previous lecture to obtain the currently running acadapplication object. The acadapplication object consists of four sub-objects:

Acadpreferences object. You can use this object to access and SET related options in the "options" dialog box.
Acaddocuments object, which represents AutoCAD graphics
Acadmenubar object, which indicates the main menu bar of AutoCAD (note that it is not acadmenubars, because the application only has one main menu bar)
Acadmenugroups object, which represents the AutoCAD menu and toolbar
This section describes the rough composition of the AutoCAD ActiveX interface object model. The following describes the acaddocuments object, because most programming is related to it. First, we can see that it is in the form of a plural number. Therefore, it is a set of all open AutoCAD images. This object is called a collection object (Hehe, as if talking nonsense ). Collection objects have some important methods and features. The most important feature is that the Count feature is used to obtain the number of objects in the Set (starting from scratch); the item method is used to obtain any objects in the set. I will introduce their usage in the following example. In the singular form of acaddocuments, acaddocument indicates an open AutoCAD image. The acaddocument object consists of the following main objects:
Acadmodelspace set and acadpaperspace set provide access to graphic objects (straight lines, circles, etc.)
Acadlayers, acadlinetypes, and acadtextstyles provide access to non-graphic objects (layers, line types, text styles, etc.)
The acadplot object provides access to the settings in the "print" dialog box and various ways to print images for the application process.
The acadutility object provides user input and conversion functions.
Use add to create a graphic object For example, to create a circle, use the addcircle method, instead of the add method to create a graphic object.
The following is a simple example to illustrate the content described above. In this example, create a new layer in AutoCAD and draw a red circle and a green line in the layer. This is the program'sSource code :( Please first include the InterOP. AutoCAD. dll and autocadexample. DLL files generated in the previous lecture in the Project)
Using system;
Using acadexample;
USING AutoCAD;

Namespace circleline
{
///

/// Summary of class1.
///
Class class1
{
///

/// Main entry point of the application.
///
[Stathread]
Static void main (string [] ARGs)
{
//
// Todo: addCode To start the application
//
Using (autocadconnector conne= new autocadconne() // connect to AutoCAD
{
Acaddocument adocument = connector. application. activedocument;
// Obtain the Drawing Object of the current AutoCAD Activity
Double [] center = new double [3] {20, 20, 0}; // set the center
Double Radius = 20; // set the radius of the circle
Double [] startpoint = new double [3] {0, 0}; // you can specify the start point of a straight line.
Double [] endpoint = new double [3] {40, 40, 0}; // you can specify the end point of a straight line.
Acadlayer newlayer = adocument. layers. Add ("circleline ");
// Create a new layer named circleline
Adocument. activelayer = newlayer; // set the circleline layer to the current layer.
Acadcircle = adocument. modelspace. addcircle (center, radius); // Add a circle
Acadline line = adocument. modelspace. addline (startpoint, endpoint); // Add a straight line
Circle. Color = acad_color.acred; // turns the circle into red.
Line. Color = acad_color.acgreen; // green the straight line.
Connector. application. Update (); // update display
For (INT I = 0; I console. writeline ("this is the {0} object: {1}", I + 1, adocument. modelspace. item (I); // traverses the current Graph

}
Console. Readline ();
}
}
}
Now, we are here today.

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.