Using C # For secondary development of AutoCAD (3) (transferred from mingjing channel)
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 applications are also objects
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.
The add method is used to create a graphic object. For example, to create a circle, the addcircle method is used, rather than the add method is used 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 source code of the program: (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: Add code here 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 () ;}} okay. It's here today.