Close

Relief for ODA Team in Ukraine

Learn more
ODA Drawings SDK
Example of Using the Record–Table and Dictionary Interfaces for Entering Names

This example demonstrates how to enter and check names of record objects contained in predefined tables and names of database objects contained in dictionaries using the record–table and dictionary interfaces, independent from the object type.

To check entered names, the example declares the enumCheckName enumerator that defines four values: kDontCheck – do not check the name after entry, kMustAbsent – the entered name must be absent from the specified dictionary or predefined table, kMustExist – the entered name must exist in the specified dictionary or predefined table, and kCanReplace – the entered name can replace an existing name when the names are the same. This enumerator has the following implementation:


enum enumCheckName { 
  kDontCheck = 0,
  kMustAbsent = 1,
  kMustExist = 2,
  kCanReplace = 3
};

The EntryRecordName() function requires two arguments — the pointer to the collection (predefined table or dictionary) object in which the name must be checked and the flag to check as a value of  the enumCheckName enumerator — and returns the name entered by the user as a non-empty OdString value or returns an empty string if the entered name is incorrect or the user cancels entry. The first argument is mandatory, the second argument is optional and equals kDontCheck by default.

For name entry, the EntryRecordName() function declares a keyword variable as an OdString value and inquires about the name as a string using the EntryTextAs() function. The record name of a dictionary or predefined table cannot contain inadmissible letters (see Naming objects). To check a name for inadmissible letters, the EntryRecordName() function passes the kCorrectName value as an argument of the EntryTextAs() function. If the entered name is incorrect or the user cancels entry, the function returns an empty string.

If the entered name is correct, the EntryRecordName() function checks whether the name has a duplicate in the collection (predefined table or dictionary) object specified by the first argument. The function checks the class of the passed object using the isA() method which returns the pointer to the class describing instance, and the method also uses the isDerivedFrom() method of the class describing instance that returns True if the object is derived from the specified class or False otherwise. The function passes the pointer to the OdDbSymbolTable class describing instance, and then passes the pointer to the OdDbDictionary class describing instance using the desc() static method of these classes. If the first argument is a predefined table object, that is, it is derived from the OdDbSymbolTable class, the function converts the passed pointer to the base table type, calls the has() method of the table object, and passes to it the entered name. If the first argument is a dictionary object, that is, it is derived from the OdDbDictionary class, the function converts the passed pointer to the base dictionary type, calls the has() method of the dictionary object, and passes to it the entered name. If the first argument is the object of another class, the function displays an error message and returns an empty string. The has() method of both classes returns True if the specified name exists in the collection (predefined table or dictionary) object or False if the name is not found. The function saves the returned value of the has() method in the bExist variable.

Then the EntryRecordName() function checks whether the entered name corresponds to the check flag specified by the second argument. If the entered name is absent from the collection and the second argument requires an existing name, the function displays an error message and returns an empty string. If the entered name exists in the collection and the second argument requires a new name, the function displays an error message and returns an empty string. If the entered name satisfies the second argument, the function converts it to an OdString value and returns it in the calling function.

The EntryRecordName() function has the following implementation:


OdString EntryRecordName(OdDbObject* pCollection, enum enumCheckName isExist = kDontCheck)
{
  bool bExist;
  OdString keyword;

  if((keyword = EntryTextAs(kCorrectName)).isEmpty()) return OdString::kEmpty;

  if(pCollection->isA()->isDerivedFrom(OdDbSymbolTable::desc()))
  {
    bExist = ((OdDbSymbolTable*) pCollection)->has(keyword);
  }
  else if(pCollection->isA()->isDerivedFrom(OdDbDictionary::desc()))
  {
    bExist = ((OdDbDictionary*) pCollection)->has(keyword);
  }
  else
  {
    wcout << L"\nError: undefined container object\n";
    return OdString::kEmpty;
  }
  if(isExist == kMustExist && !bExist)
  {
    wcout << L"\nError: the name \"" << keyword << L"\" - is not found\n";
    return OdString::kEmpty;
  }
  if(isExist == kMustAbsent && bExist)
  {
    wcout << L"\nError: the name \"" << keyword << L"\" - already exists\n";
    return OdString::kEmpty;
  }
  return keyword;
}

The EntryRecordName() function is used in the documented examples that require entry of a record name for the specified dictionary or predefined table.

See Also

Example of Entering Characters for String Objects

Working with Dictionaries of Objects

Working with Predefined Tables of Named Records

Getting and Checking Records in Tables

Getting and Checking Objects and Names in Dictionaries

Copyright © 2002 – 2022. Open Design Alliance. All rights reserved.