. NET Development AutoCAD2006 Guide (ii)

Source: Internet
Author: User
Tags command line commit constructor documentation getcolor join mathematical functions
In the last article to you briefly. NET development AutoCAD process, which introduced me to write a simplified program development of the Zhfarx library. Here's a brief introduction to the main functions in the Zhfarx library and how to use it to write. NET programs. For a description of all the functions of the Zhfarx library, please refer to the Zhfarx Help documentation in the annex to this chapter.
Before introducing the Zhfarx library, let's first look at the basic concepts. You can look at AutoCAD as a database, and all the things in AutoCAD are in the related tables of this database. For example, if you add a line to the model space of AutoCAD using the lines command, AutoCAD creates an instance of the line class and adds it to the database's Model space block table record. In the traditional C + + writing Objectarx program, you must first open the block table of the current database (because the model space is in the Block table), then open the Block table records corresponding to the model space, add an instance of the line class to the record, and then close the block table and the Model Space block table record respectively. And in. NET program, the corresponding code is written like this:
Database db= Application.DocumentManager.MdiActiveDocument.Database;
Get the current database
Dbtransman tm=db. TransactionManager;
Obtaining transaction processing
using (Transaction Trans=tm. StartTransaction ())//Start transaction processing
{
Blocktable bt= (blocktable) TM. GetObject (db. Blocktableid,openmode.forread,false);
Open the Block table for the current database
Blocktablerecord btr= (Blocktablerecord) TM. GetObject (Bt[blocktablerecord.modelspace],openmode.forwrite,false);
Open the Model Space block table record
Btr. Appendentity (ENT)//Add entity to record
Tm. Addnewlycreateddbobject (ent,true);
Trans.commit ()//COMMIT TRANSACTION
}
As you can see from the code above,. NET replaces the open and close operations in C + + with transaction processing (you can, of course, use the corresponding open and closed functions like C + + to open and close the operation, but the Objectarx managed encapsulation class has labeled these two functions as obsolete functions. This means that users are not advised to use them).
One thing to note is that the object is not added to the AutoCAD database, you can directly manipulate it (such as changing the color, moving, set the geometry of the entity properties, etc.). But once the object is added to the AutoCAD database, you must use transaction processing to open it and then do the appropriate action. For example, you add the above line in AutoCAD, and you want to change the color of the line, you can't directly call the line color or ColorIndex attribute to set, you must first use the transaction processing GetObject () function to open the line object, You can then call the color or ColorIndex property to set the colors for the line.
The role of Zhfarx library is to simplify these operations, with it you add objects in AutoCAD when you do not have to open the AutoCAD database table, you can also directly to the objects have been added to the database to operate, It also includes these features to make it easier to create AutoCAD entity constructors, traverse database tables, add groups and expand dictionaries, and other commonly used functions.
First of all, I would like to introduce the tools in Zhfarx library. This class is the main class of the Zhfarx library, and the main features of the Zhfarx library described above are implemented in this library. The main members of this class are described below (the members of this class are static):
L Property
Includes 3: Database (get the current database), Editor (get the AutoCAD command line), Transactinmanager (get the transaction manager). With these three properties, you should not enter code as long as Application.DocumentManager.MdiActiveDocument.Database, but simply write as tools.database.
L function
The functions can be grouped into the following categories:
1. Join the Object
Includes the following:
Øpublic static ObjectId addentity (Entity ent);
This function adds an entity class object (such as a line, circle, etc.) to the AutoCAD database, and the following code adds a straight line to the database:
Line line;
.......
Tools.addentity (line);
Øpublic static ObjectId Addsymboltablerecord (Symboltablerecord str,objectid Symboltableid)
This function adds the symbol table record to the AutoCAD database, and the symbol table is a series of tables such as layer, line type and so on in AutoCAD database. The input parameter of the function is a symbol table Record object (str) to join, and the other is the object ID of the symbol table that the symbol table records to join (which can be obtained by accessing the properties of the database, such as the Layertableid property to represent the database's layer table). The following code adds a new layer to the database:
Layertablerecord ltr;
......
Tools.addsymboltablerecord (Ltr,tools.database.layertableid);

Øpublic static ObjectId Adddictionaryobject (String searchkey)
Join the Dictionary object. About the Dictionary object of the introduction, please see my later writing articles.
Øpublic static ObjectId adddictionaryobject (string searchkey,dbobject newvalue,objectid ownerid)
Add dictionary class objects, such as extended objects, groups, and so on.
2. Set or read Common properties of objects
Common properties include: color, color index (COLORINDEX), Layer (Layer), line Style (Linetype), linear scale (Linetypescale), line width (lineweight), print style name (plotstylename ) and visibility (Visible).
Functions that set common properties are preceded by a corresponding generic property name, such as a function that sets the color of the object as Putcolor. There are two forms of functions that set each common property, and the following is an example of a function that sets the color.
The first form is Putcolor (Entity ent,color Color), the first argument is an instance of the object, and the second is the value to set.
The second form is Putcolor (ObjectId id,color Color), the first parameter is the ID of the object instance, and the second parameter is the value to set.
The function that gets the common property is preceded by a get start with the corresponding generic property name, such as the function that gets the object color GetColor. There are also two forms, as an example of a function that gets the color.
The first form is GetColor (Entity ent), and the input parameter is an instance of the object.
The second form is getcolor (ObjectId ID), and the input parameter is the ID of the object instance.
3. Transform operation
It is very puzzling that in the Objectarx managed encapsulation class, entity classes (Entity) do not have functions such as rotation, translation, scaling, and you can only implement these changes through the Transformby () function of the entity class. The Transformby () function is not easy to use because it uses a matrix parameter. In the Zhfarx library, you have rewritten functions for transformation operations, which you can use to facilitate related transformation operations. There are two forms of transformation operation functions, the difference being that the first input function can be an instance of an entity object. Can also be the ID of an entity object, in the introduction below I only describe the type of input parameter as an instance of an entity object, and for another, you can simply change the corresponding input parameter to the ID of the entity object. The transformation operation functions are as follows:
Øpublic static void Move (Entity ent,point3d Frompoint,point3d topoint)
Move the entity Ent from point Frompoint to Topoint
Øpublic static void Rotate (Entity ent,point3d basepoint,double rotationangle)
Point Basepoint as the Datum point, the entity ENT rotation rotationangle angle (for radians value).
Øpublic static void Scale (Entity ent,point3d basepoint,double scalefactor)
With point Basepoint as the Datum point, the entity Ent scaled scalefactor times (>1 to enlarge, <1 to shrink).
Øpublic static ObjectId Mirror (Entity ent,point3d Mirrorpoint1,point3d mirrorpoint2,bool)
The entity ENT is mirrored by a line line made up of Point mirrorPoint1 and Point MirrorPoint2, and the parameter erasesourceobject indicates whether the source object is deleted.
4. Some other common operation functions
Øpublic static ObjectId Copy (Entity ent)
The entity ENT is replicated and an overloaded function is entered, and the input parameter is the ID of the entity.
Øpublic static void Erase (Entity ent)
Delete Entity Ent, and an overloaded function with the input parameter as the ID of the entity.
Øpublic static Entity getentity (ObjectId ID)
The object ID is used to get the entity object (and to get the object ID of the entity, you just have to access its Objectid attribute).
Øpublic static DBObject getdbobject (ObjectId ID)
The object ID is used to obtain the non entity class object.
Øpublic static dbobjectcollection getiteratorforsymboltable (ObjectId ID)
Gets the iterator (object collection) that is used to traverse the symbol table (represented by ID).
Øpublic static objectidcollection Getiteratorforsymboltableid (ObjectId ID)
Gets the iterator (object ID collection) that is used to traverse the symbol table (represented by ID).
Øpublic static Point3D getmidpoint (Point3D Pt1,point3d pt2)
Gets the midpoint of the segment represented by the two point. (Oh, originally also want to write such as intersection, judge whether parallel, vertical, etc. mathematical functions, but Objectarx managed encapsulation classes have related functions, there is no write).
Finally introduced the tools class members, and then to introduce the other classes in the Zhfarx library. Tools class outside the other classes are some of the AutoCAD entity classes such as lines, circles, such as rewriting, to facilitate. NET program writing. Until the current version of Zhfarx, the entity classes I have rewritten are: lines (line), Circle (Circle), Arc (ARC), ellipse (Ellipse), multiple segment lines (polylines, this multiple-segment line is two-dimensional, also light multiple segment lines), Single-line text (dbtext, Please note that the class in the Objectarx managed wrapper class is not text, but dbtext), multiple lines (mtext), tables (table), padding (Hatch), various annotations (including alignment callout aligneddimension, Diameter callout diametricdimension, Angle callout LineAngularDimension2, radius callout radialdimension and rotate callout rotateddimension). The overridden entity classes are all derived from these entity classes and can be used by all function and property rewrite classes of the entity class. The name of the rewrite class is the entity class after the original, plus s, such as the rewrite of the line class is lines, and rewrite the Circle class is circles. You might say, what's the use of rewriting these classes? Oh, there are two answers. One is to facilitate the creation of entities, and the other is that you can easily modify the attributes of an entity, regardless of whether it is added to the database. A function that creates an AutoCAD entity in the managed encapsulation class of Objectarx, which is a constructor, usually has only one form, such as a circle can only be created by the center of the circle, the radius, and a normal vector that is essentially not needed. And we know there are many ways to create circles in AutoCAD, such as creating a circle by three points, through the diameter of two points. The constructors for creating circles are included in the circles class of the Zhfarx library. The following example illustrates the creation of a circle by three points:
Circles Circle=new circles (PT1,PT2,PT3);
For a description of these constructors, you can refer to the Help documentation in the attachment for a detailed description.
From the foregoing, you already know that entities that are added to a database cannot be accessed directly, and these rewritten classes are available. Here's a two-paragraph code comparison to illustrate (in circles, for example), first look at the general entity classes:
Circle circle=new Circle (Center,normal,radius);
Circle. radius=1;//because the circle has not been added to the database, changing the radius of the Circle property is allowed
Tools.addentity (circle)//using a simplified function of the Zhfarx library to add a circle to the database
Circle. Radius = 2;
This sentence is wrong, because the circle has been added to the database, you must first open it, and then do the related
operation, you cannot access the circle directly like this

Here is the code that rewrites the class:
Circles Circle =new Circles (center, RADIUS);/Oh, even the constructor is simpler than the above
Circle. Radius =1;//Change the radius properties of a circle before the circle is joined to the database
Tools.addentity (circle)//using a simplified function of the Zhfarx library to add a circle to the database
Circle. The Radius =2;//Circle has been added to the database, but you can still modify its properties directly
It's also important to note that for common properties such as colors and layers, use the Get class and put class functions for the tools class (for these functions, please refer to the previous content), which is true for both generic and Overtype classes.

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.