Close

Relief for ODA Team in Ukraine

Learn more
ODA Drawings SDK
Example of Binding to a Shape File

This example demonstrates creating a text style and associating it with a .shx file for working with shapes and text.

The BindToSHXFile() function requires three arguments — the pointer to the database object in which a new text style should be added, the .shx file name which contains the shape definitions as a non-empty OdString value, and the shape status as a Boolean value — and returns the OdDbObjectId instance associated with the text style record object which is associated with the specified .shx file or OdDb::kNull if errors occur. All arguments are required. Other examples use the BindToSHXFile() function to bind to a .shx file.

The BindToSHXFile() function divides the string specified by the second argument into the path and file name, and then, it reduces the file extension to obtain the text style name. The function uses the file name without a path (sFile variable) to create the association to the .shx file and the name without an extension (sName variable) to create a text style. If the file name or text style name is an empty string, the function displays an error message and returns kNull as a result.

If names are correct, the function gets the text style table object of the specified database using the pointer specified by the first argument and the getTextStyleTableId() method of the database. Then the function gets the smart pointer to the text style table object in write mode using safeOpenObject() and creates the iterator for traversing through the table using the newIterator() method. Next, the function traverses through the text styles using the start(), step(), done() methods of the iterator and checks whether another text style has the specified name or is already associated with the specified .shx file using the fileName() and bigFontFileName() methods. If the text style table object contains the text style record object associated with the .shx file, the function displays a message about it and returns the object ID of the found text style.

If the database does not have a text style associated with the .shx file, the function creates a new text style record object using its static pseudo-constructor createObject() method, assigns the name stored in the sName variable using the setName() method, sets the shape status to the third argument of the function (True if it is a shape file or False if it is a font file), associates with the file name stored in the sFile variable using setFileName(), and adds the new text style to the text style table of the specified database using the add() method. If creating and adding is successful, the function returns the object ID of the new text style record object.

The BindToSHXFile() function has the following implementation:


OdDbObjectId BindToSHXFile(OdDbDatabase* pDb, OdString shxFileName, bool bStatus)
{
  int n;

  if(pDb == NULL) 
  {
    wcout << L"\nError: The database is not specified";
    return (OdDbObjectId::kNull);
  }

  n = shxFileName.reverseFind(L'\\');
  OdString sFile = ((n < 1) ? shxFileName : shxFileName.mid( n+1 ));

  n = sFile.reverseFind(L'.');
  OdString sName = ((n < 1) ? sFile : sFile.left( n ));

  if(sName.isEmpty() || sFile.isEmpty()) 
  {
    wcout << L"\nError: The style name or file name is empty";
    return (OdDbObjectId::kNull);
  }

  OdDbTextStyleTablePtr pTextStyles = pDb->getTextStyleTableId().safeOpenObject(OdDb::kForWrite);
  OdDbSymbolTableIteratorPtr itTextStyle = pTextStyles->newIterator();

  OdDbTextStyleTableRecordPtr pTextStyle;

  for(itTextStyle->start() ; !itTextStyle->done() ; itTextStyle->step())
  {
    pTextStyle = itTextStyle->getRecord(OdDb::kForRead);

    if(pTextStyle->getName().compare(sName) == 0 ||
       pTextStyle->fileName().compare(sFile) == 0 || 
       pTextStyle->bigFontFileName().compare(sFile) == 0) 
    {    
      wcout << L"\nWarning: The shx-file is already associated with text style";
      return pTextStyle->objectId();
    }
  }

  pTextStyle = OdDbTextStyleTableRecord::createObject();

  try {
    pTextStyle->setName(sName);     
    pTextStyle->setIsShapeFile(bStatus);
    pTextStyle->setFileName(sFile);

    return ( pTextStyles->add(pTextStyle) );
  }
  catch(OdError& e) 
  {
    wcout << L"\nError: " << e.code() << L" - " << e.description() << L"\n";
    pTextStyle->erase();
    
    return (OdDbObjectId::kNull);
  }
}

See Also

Example of Working with the Shape Object

Example of Working with the Text Style Table Object

Example of Working with the Text Style Record Object

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