数据库处理
创建新数据库
1 static void lzxARX_AcDbDatabaseCreateDwg(void) 2 { 3 //创建新数据库 4 AcDbDatabase* pDb = new AcDbDatabase(); 5 6 //获得符号表 7 AcDbBlockTable* pBtbl; 8 pDb->getSymbolTable(pBtbl,AcDb::kForRead); 9 10 //获得模型空间块表 11 AcDbBlockTableRecord* pBtblRcd; 12 pBtbl->getAt(ACDB_MODEL_SPACE,pBtblRcd,AcDb::kForWrite); 13 pBtbl->close(); 14 15 //创建两个实体 16 AcDbCircle* pCir1 = new AcDbCircle(AcGePoint3d(1,1,1),AcGeVector3d(0,0,1),1.0); 17 AcDbCircle* pCir2 = new AcDbCircle(AcGePoint3d(4,4,4),AcGeVector3d(0,0,1),2.0); 18 19 //将实体添加到模型空间块表记录中 20 pBtblRcd->appendAcDbEntity(pCir1); 21 pCir1->close(); 22 pBtblRcd->appendAcDbEntity(pCir2); 23 pCir2->close(); 24 pBtblRcd->close(); 25 26 //下面我们保存这个数据库 27 pDb->saveAs(L"C:\\test.dwg"); 28 29 //删除数据库 30 delete pDb; 31 }
读取数据库
1 static void lzxARX_AcDbDatabaseReadDwg(void) 2 { 3 //在创建数据库时,将其构造函数的参数设为Adesk::kFalse 4 //这样将生成一个空数据库,这个数据只能用来读取文件 5 AcDbDatabase* pDb = new AcDbDatabase(Adesk::kFalse); 6 7 //如果用户不为AcDbDatabase::readDwgFile()函数指定的文件添加扩展名, 8 //该函数会自动添加 9 pDb->readDwgFile(L"C:\\test.dwg"); 10 11 //打开模型空间块表记录 12 AcDbBlockTable* pBlkTbl; 13 pDb->getSymbolTable(pBlkTbl,AcDb::kForRead); 14 15 AcDbBlockTableRecord* pBlkTblRcd; 16 pBlkTbl->getAt(ACDB_MODEL_SPACE,pBlkTblRcd,AcDb::kForRead); 17 pBlkTbl->close(); 18 19 //创建一个新遍历器 20 AcDbBlockTableRecordIterator* pBlkTblRcdItr; 21 pBlkTblRcd->newIterator(pBlkTblRcdItr); 22 23 24 AcDbBlockTable* bt; 25 acdbHostApplicationServices()->workingDatabase()->getSymbolTable(bt,AcDb::kForRead); 26 AcDbBlockTableRecord* btr; 27 bt->getAt(ACDB_MODEL_SPACE,btr,AcDb::kForWrite); 28 bt->close(); 29 30 AcDbEntity* pEnt; 31 32 //对块表记录中实体进行遍历 33 for (pBlkTblRcdItr->start();!pBlkTblRcdItr->done();pBlkTblRcdItr->step()) 34 { 35 //获得实体 36 pBlkTblRcdItr->getEntity(pEnt,AcDb::kForRead); 37 38 //打印实体的类名 39 acutPrintf(L"ClassName: %s\n",(pEnt->isA())->name()); 40 btr->appendAcDbEntity(pEnt); 41 pEnt->close(); 42 } 43 44 //关闭对象,删除遍历器和数据库 45 pBlkTblRcd->close(); 46 btr->close(); 47 delete pBlkTblRcdItr; 48 delete pDb; 49 }