|
- AcDbObjectId CBlockUtil::CopyBlockDefFromOtherDwg(const TCHAR* fileName,
- const TCHAR* blkDefName)
- {
- AcDbObjectId blockRefId = AcDbObjectId::kNull;
- //AcDbObjectId :: kNull表示当前DWG中不存在对象(图层)
-
- //使用采用SH采用DENYNO参数打开图形(只读打开),允许其他用户读写文件
- AcDbDatabase* pSourceDwg = new AcDbDatabase(false);
- Acad::ErrorStatus es = pSourceDwg->readDwgFile(fileName,
- static采用cast<AcDbDatabase::OpenMode>(采用SH采用DENYNO));
- if (es != Acad::eOk)
- {
- delete pSourceDwg;
- pSourceDwg = NULL;
-
- acutPrintf(TEXT("\n读入dwg图形错误,图形名称:%s"), fileName);
- return blockRefId;
- }
-
- //打开外部图形数据库的块表,寻找给定名称的块表记录
- AcDbBlockTable* pBlkTable = NULL;
- es = pSourceDwg->getBlockTable(pBlkTable, AcDb::kForRead);
- //以只读模式打开数据库的块表,pBlkTable获得块表地址
- assert(es == Acad::eOk);
- bool bRet = false;
-
- if (pBlkTable->has(blkDefName)) //不存在指定的图块
- {
- AcDbObjectId destBlkDefId; //指定图块的块表记录Id
- es = pBlkTable->getAt(blkDefName, destBlkDefId);
-
- //把指定的图块输出到一个临时的图形数据库
- AcDbDatabase *pTempDb = NULL; //注意:这里千万不能new
- es = pSourceDwg->wblock(pTempDb, destBlkDefId);//创建一个新的数据库对象,pTempDb指向它
- assert(es == Acad::eOk);
-
- //把临时图形数据库作为插入块插入到当前dwg
- AcDbDatabase* pCurDb = acdbHostApplicationServices()->workingDatabase();
- es = pCurDb->insert(blockRefId, blkDefName, pTempDb);//由pDb指向的数据库中的所有
- //模型空间实体都被复制到新的块表记录中,新的块表记录名称为blkDefName,Id为blockRefId
- assert(es == Acad::eOk);
- delete pTempDb;
- pTempDb = NULL;
- }
- else
- {
- acutPrintf(TEXT("\n给定的DWG文件中,不存在指定名称的块表记录."));
- }
- pBlkTable->close();
-
- delete pSourceDwg;
- pSourceDwg = NULL;
-
- return blockRefId;
- }
复制代码 |
|