All code examples shown in this section illustrate the protocol for opening and closing objects that you'll need to observe whenever you work with database-resident objects. This protocol ensures that objects are physically in memory when they need to be accessed but can be paged out to disk when they're not needed. Before you can modify an object, you need to open it, as shown in the following example:
acdbOpenObject(pObject, objId, AcDb::kForWrite);
The open functions have a mode parameter that specifies whether you are opening the object for read, write, or notify. While the object is open for write, you can modify it. When you are finished, you must explicitly close the object as shown in the following example, regardless of the mode in which it was opened:
pObject->close();
The following is sample code for changing the color of an entity:
Acad::ErrorStatus changeColor(AcDbObjectId entId, Adesk::UInt16 newColor) { AcDbEntity *pEntity; acdbOpenObject(pEntity, entId, AcDb::kForWrite); pEntity->setColorIndex(newColor); pEntity->close(); return Acad::eOk; }
New instances of an object are considered to be open for write. Some functions, such as the AcDbBlockTable::getAt()function, obtain an object ID and open the object at the same time. An object can't be closed until it has been added to the database. You own the object and can freely delete it at any time before the object is added to the database.
However, once the object has been added to the database, you cannot delete it directly. You can call the AcDbObject::erase() function, which marks the object as erased. Erased objects remain in the database until the database is destroyed, but do not get saved when the drawing is saved.
Directly deleting an object that has been added to the database will cause AutoCAD to terminate.