Drawings SDK Developer Guide > Working with .dwg Files > Introduction > Basics of Database Operations and Database Objects > Database Operations > Example of Creating and Reading Database Operations
Example of Creating and Reading Database Operations

The following two routines illustrate database creation and read operations.

The first routine creates a new database and a circle and line which are added to model space. It uses the OdDbDatabase::writeFile() method to save the drawing.


  ////////////////////////////////////////////////////////////////////////
  // Creating Database routine                                          //
  ////////////////////////////////////////////////////////////////////////
  /**********************************************************************/
  /* Create a database                                                  */
  /**********************************************************************/
  OdDbDatabasePtr pDb = svcs.createDatabase(true);
  
  /**********************************************************************/
  /* Open the Block Table                                               */
  /**********************************************************************/
  OdDbBlockTablePtr pTable  = pDb->getBlockTableId().safeOpenObject(OdDb::kForWrite);
  OdDbObjectId blockTableID = pDb->getBlockTableId();
  
  /**********************************************************************/
  /* Open the Model Space block                                         */
  /**********************************************************************/
  OdDbBlockTableRecordPtr pModelSpace = pDb->getModelSpaceId().safeOpenObject(OdDb::kForWrite); 
  
  /*************************************************************************************/
  /* Create a Circle object, put it to model space and set the center point and radius */
  /*************************************************************************************/
  OdDbCirclePtr pCircle = OdDbCircle::createObject();
  pModelSpace->appendOdDbEntity(pCircle);
  OdGePoint3d center = OdGePoint3d(2.0,0,0);
  pCircle->setCenter(center);
  pCircle->setRadius(2.0);

  /**********************************************************************************************/
  /* Create a Line object, put it to model space and set the start and end points and the color */
  /**********************************************************************************************/
  OdDbLinePtr pLine = OdDbLine::createObject();
  OdDbObjectId objId = pModelSpace->appendOdDbEntity(pLine);
  pLine->setStartPoint(OdGePoint3d(2,0,0));
  pLine->setEndPoint(OdGePoint3d(4,2,0));
  pLine->setColorIndex(12);

  /**********************************************************************/
  /* Saving the created database as a file                              */
  /**********************************************************************/
  pDb->writeFile("MyDrawing.dwg", OdDb::kDwg, OdDb::vAC24);
 
  /**********************************************************************/
  /* Delete the Database instance                                       */
  /**********************************************************************/
  pDb.release();

The second routine reads the saved drawing and prints the class names of all entities that are contained in model space.


  ////////////////////////////////////////////////////////////////////////
  // Reading Database routine                                           //
  ////////////////////////////////////////////////////////////////////////
  /**********************************************************************/
  /* Read a database from a file                                        */
  /**********************************************************************/
  OdDbDatabasePtr pDb = svcs.readFile("MyDrawing.dwg");
  /**********************************************************************/
  /* Open the Block Table                                               */
  /**********************************************************************/
  OdDbBlockTablePtr pTable  = pDb->getBlockTableId().safeOpenObject(OdDb::kForRead);
  OdDbObjectId blockTableID = pDb->getBlockTableId();
  
  /**********************************************************************/
  /* Open the Model Space block                                         */
  /**********************************************************************/
  OdDbBlockTableRecordPtr pModelSpace = pDb->getModelSpaceId().safeOpenObject(OdDb::kForRead);

  /********************************************************************/
  /* Get a SmartPointer to a new ObjectIterator                       */
  /********************************************************************/
  OdDbObjectIteratorPtr pEntIter = pModelSpace->newIterator();
    
  /********************************************************************/
  /* Step through the BlockTableRecord                                */
  /********************************************************************/
  for (; !pEntIter->done(); pEntIter->step())
  {
    /********************************************************************/
    /* Get the classname of an Entity                                   */
    /********************************************************************/

    printf("Classname: %ls\n", (pEntIter->objectId().safeOpenObject(OdDb::kForRead))->isA()->name().c_str());
  }

  /**********************************************************************/
  /* Delete the Database instance                                       */
  /**********************************************************************/
  pDb.release();

 

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