高级会员
注册日期: 06-11
帖子: 14579
精华: 1
现金: 224494 标准币
资产: 234494 标准币
|
【转帖】objectarx&dummies教程(四)——object lifecycle
objectarx&dummies教程(四)——object lifecycle
objectarx&dummies教程(四)——object lifecycle
class 4 - object lifecycle
now you are able to create a minimum objectarx project we will continue the course with some additional concepts.
as i have mentioned before, autocad's database is well organized to allow simple and direct manipulation of its objects. generally we have two basic types: containers and objects.
containers are special objects that provide a simple and efficient mechanism to store, edit and persist objects or collections. they are optimized to allow quick access with minimum overhead. each type of object has an appropriate container where you should always store your object. there are several containers inside autocad's database but some of the most common are: layertable, linetypetable and blocktable. each container class has standard access methods and most of them also offer a companion class to iterate through its items. every time you create an object and would like to store it inside autocad's database you need to follow its container protocol to store and persist is as well.
in other hand, objects (including entities) are the most basic types and represent each element inside autocad. they are implemented through specific classes with standard and specific methods. some of them could be derived inside your application to allow customization.
every database resident object has an exclusive identification called objectid. this identification is the "name" of each object inside database and it is used to reference, open and manipulate objects.
this could be a difficult concept for those who are fluent in standard c++ language because inside objectarx you don't delete a pointer to a database resident object. what? yes, this is a little bit strange but autocad has several reasons to do that including performance, memory management and other aspects.
so, how can i manipulate objects?
don't panic. you just need to keep in mind some basic but essential rules:
1) database resident objects should never be deleted even you have erased them!
2) if you have allocated an object but did not added it to database yet, go ahead...delete the pointer!
3) if you need to get a pointer to an object to manipulate it, acquire its objectid and use the appropriate container method to get its pointer. that's it? no. you need to close the objects just after you finish to use it. call the close() method or end its transaction to inform autocad that you are done! (don't forget this one because autocad will terminate)
most of bugs you will face at your first application will be have something to do with the above rules. trust me!
object ownership and relationship
objects can refer each other using their objectid. this can be an ownership relation or just a relationship. if you think about a layer you will understand what is involved with this concept. the layertable container owns its records which are objects (layers in this case). each layer objectid is referred inside each entity. exactly due that you can't remove a layer from a dwg file until all entities that uses this layer are erased or has its associated layer changed.
there are several examples of ownerships and relationships inside autocad. during our course you will get this concept easily when we have to manipulate basic objects.
creating a layer
on previous classes i have presented some code fragment to explain how to create simple entities. now i will present a simple code to create a layer just to allow you to feel how much protocol is involved in such operation:
acdblayertable* playertbl = null;
// get the current database
acdbdatabase* pdb = acdbhostapplicationservices()->workingdatabase();
// get the layertable for write because we will create a new entry
pdb->getsymboltable(playertbl,acdb::kforwrite);
// check if the layer is already there
if (!playertbl->has("mylayer")) {
// instantiate a new object and set its properties
acdblayertablerecord *playertblrcd = new acdblayertablerecord;
playertblrcd->setname("mylayer");
playertblrcd->setisfrozen(0); // layer set to thawed
playertblrcd->setisoff(0); // layer set to on
playertblrcd->setislocked(0); // layer un-locked
accmcolor color;
color.setcolorindex(7); // set layer color to white
playertblrcd->setcolor(color);
// now, add the new layer to its container
playertbl->add(playertblrcd);
// close the new layer (don't delete it)
playertblrcd->close();
// close the container
playertbl->close();
} else {
// if our layer is already there, just close the container and continue
playertbl->close();
acutprintf("\nmylayer already exists");
}
|