This example demonstrates entry and checking of names for objects contained in predefined symbol tables and dictionaries using the record–table and dictionary interfaces independent from the type of objects.
The AboutObjectKey() function requires a pointer to the object contained in a dictionary as an argument and returns the dictionary key associated with passed object as an OdString value.
This function gets a pointer to the owner of the passed object using the ownerId() and safeOpenObject() methods. The function assumes that this object is stored in the dictionary and the dictionary object is owner for it. Therefore the function converts the obtained pointer to the pointer on the dictionary object and gets the associated key using the nameAt() method passing to it the ID of the passed object using the objectId() method.
The AboutObjectKey() function has the following implementation:
OdString AboutObjectKey(OdDbObject* pObject)
{
OdString sInfo;
sInfo = ((OdDbDictionary*) pObject->ownerId().safeOpenObject().get())->nameAt(pObject->objectId());
return sInfo;
}
The AboutDbObject() function requires one argument — the OdDbObjectId instance associated with an object of the database — and returns information about the passed object as an OdString value.
This function checks whether the passed Object ID is null using the isNull() method and whether it is invalid using the isValid() method of the OdDbObjectId object. If the ID is null, the function returns the "Db:kNull" string. If the ID is invalid, the function returns the "Db:Error" string. If the passed Object ID is valid, the function gets a smart pointer to the object associated with the specified ID in read mode using the safeOpenObject() method, and the function gets the handle of the object using the handle() method. Then the function checks the class of the passed object using the isA() method which returns the pointer to the class describing instance, and the function uses the isDerivedFrom() method of the class describing instance which returns True if the object is derived from the specified class or False otherwise. The function passes the pointer to the corresponding class describing instance which the function gets using the desc() static method of the class.
If the passed object is a predefined table of records, that is, it is derived from the OdDbSymbolTable class, the function retrieves the table name from the class name using the isA() method and methods of the OdSrting class and attaches the table name to the resulting string. If the passed object is a named record of the predefined table, that is, it is derived from the OdDbSymbolTableRecord class, the function converts the pointer to the base record type, gets the record name using the getName() method of the record object, retrieves the record type from the record class name using the isA() method and methods of the OdSrting class, and attaches the record name together with the record type to the resulting string. If the passed object is an entity, that is, it is derived from the OdDbEntity class, the function retrieves the class name and attaches it to the resulting string. If the passed object is an element of a dictionary, that is, its owner object is derived from the OdDbDictionary class, the function retrieves the name of the object in the dictionary using the nameAt() method of the dictionary, retrieves the class name of the object using the isA() method, and attaches the object name together with the class name to the resulting string. If the passed object does not have an owner, that is, its Owner ID is null, or the passed object is not an element of a dictionary, that is, its owner is derived from another class, the function retrieves its class name and attaches it to the resulting string. To get the owner object, the function uses the ownerId() and safeOpenObject() methods.
The AboutDbObject() function has the following implementation:
OdString AboutDbObject(OdDbObjectId idObject)
{
OdString sAbout;
if(idObject.isNull())
sAbout = L"[Db:kNull]";
else if(!idObject.isValid())
sAbout = L"[Db:Error]";
else
{
OdDbObjectPtr pObject = idObject.safeOpenObject();
sAbout = L"[" + pObject->handle().ascii();
if(pObject->isA()->isDerivedFrom(OdDbSymbolTable::desc()))
{
sAbout += L"-" + pObject->isA()->name().mid(4, pObject->isA()->name().find(L"Table")-4) + L"'s Table]";
}
else if(pObject->isA()->isDerivedFrom(OdDbSymbolTableRecord::desc()))
{
sAbout += L"-\"" + ((OdDbSymbolTableRecord*) pObject.get())->getName() + L"\"-" +
pObject->isA()->name().mid(4, pObject->isA()->name().find(L"TableRecord")-4) + L"]";
}
else if(pObject->isA()->isDerivedFrom(OdDbEntity::desc()))
{
sAbout += L"-" + pObject->isA()->name().mid(4) + L"-Entity]";
}
else
{
OdDbObjectId idOwner = pObject->ownerId();
if(!idOwner.isNull())
{
OdDbObjectPtr pOwner = idOwner.safeOpenObject();
if(pOwner->isA()->isDerivedFrom(OdDbDictionary::desc()))
{
sAbout += L"-\"" + ((OdDbDictionary*) pOwner.get())->nameAt(idObject) + L"\"";
}
}
sAbout += L"-" + pObject->isA()->name().mid(4) + L"]";
}
}
return sAbout;
}
The DemoBase() function tests the AboutDbObject() function. The DemoBase() function requires a pointer to the database object and calls the AboutDbObject() function for the predefined table object, record object, dictionary object, element of a dictionary, database object outside of collections, and the OdDb::kNull object.
The DemoBase() function has the following implementation:
void DemoAbout(OdDbDatabase* pDb)
{
wcout << L"\nStart testing";
wcout << L"\n" << AboutDbObject(pDb->getLayerTableId());
wcout << L"\n" << AboutDbObject(pDb->getLayerZeroId());
wcout << L"\n" << AboutDbObject(pDb->getMaterialDictionaryId());
wcout << L"\n" << AboutDbObject(pDb->globalMaterialId());
OdDbColorPtr pColor = OdDbColor::createObject();
OdDbObjectId idColor = pDb->addOdDbObject(pColor);
wcout << L"\n" << AboutDbObject(idColor);
wcout << L"\n" << AboutDbObject(OdDbObjectId::kNull);
wcout << L"\nStop testing";
}
Testing gives the following results:
Start testing
[2-Layer's Table]
[10-"0"-Layer]
[3B-"ACAD_MATERIAL"-Dictionary]
[3E-"Global"-Material]
[3F-Color]
[Db:kNull]
Stop testing
The AboutDbObject() function is used in the documented examples that require display of information about a stored object.
Belonging to an Instance of a Class
Collecting Database Objects using Dictionaries
Collecting Database Objects using Predefined Tables of Named Records
Copyright © 2002 – 2022. Open Design Alliance. All rights reserved.
|