1 AcDbObjectId CreatBlockForAcdbobjectidarr(AcDbObjectIdArray ptarr,AcGePoint3d pt, CString sName) 2 { 3 //创建一个块参照在当前数据库中 4 AcDbBlockTable *pBlkTab=NULL; 5 acdbHostApplicationServices()->workingDatabase()->getBlockTable(pBlkTab, AcDb::kForWrite); 6 AcDbBlockTableRecord *pBlkTabRcd; 7 pBlkTabRcd = new AcDbBlockTableRecord(); 8 pBlkTabRcd->setName(sName); 9 AcDbObjectId blockId; 10 pBlkTab->add(blockId, pBlkTabRcd); 11 pBlkTabRcd->setOrigin(pt);//设置块的插入点 12 pBlkTab->close(); 13 pBlkTabRcd->close();//关闭后获得这个块的id 14 15 AcDbIdMapping adimIdMap; 16 AcApDocument* pDoc = acDocManager->curDocument(); 17 acDocManager->lockDocument(pDoc, AcAp::kWrite);//锁定当前文档 18 acdbHostApplicationServices()->workingDatabase()->deepCloneObjects(ptarr, blockId, adimIdMap);//克隆当前选中的实体到块中 19 acDocManager->unlockDocument(pDoc);//解锁文档 20 21 return blockId; 22 } 23 24 bool CreatOtherDwgBlockForAcdbobjectidarr(const TCHAR *fileName, const TCHAR *blkDefName, AcDbObjectId Tid) 25 { 26 // 使用_SH_DENYNO参数打开图形(只读打开),允许其它用户读写该文件 27 AcDbDatabase* pSourceDwg = new AcDbDatabase(false); 28 Acad::ErrorStatus es = pSourceDwg->readDwgFile(fileName, _SH_DENYNO); 29 if (es != Acad::eOk) 30 { 31 delete pSourceDwg; 32 pSourceDwg = NULL; 33 34 acutPrintf(TEXT(" 读入dwg图形错误, 图形名称: %s"), fileName); 35 return false; 36 } 37 // 打开外部图形数据库的块表,寻找给定名称的块表记录 38 AcDbBlockTable* pBlkTable = NULL; 39 es = pSourceDwg->getBlockTable(pBlkTable, AcDb::kForWrite); 40 AcDbObjectId idTemp=AcDbObjectId::kNull; 41 pBlkTable->getAt(blkDefName, idTemp); 42 if (idTemp!=AcDbObjectId::kNull) 43 { 44 acutPrintf(_T(" 外部图库文件中已有指定名称的块")); 45 pBlkTable->close(); 46 delete pSourceDwg; 47 pSourceDwg = NULL; 48 return false; 49 } 50 AcDbObjectId blockId; 51 AcDbBlockTableRecord *pTempRec = new AcDbBlockTableRecord; 52 pTempRec->setName(blkDefName); 53 pBlkTable->add(blockId, pTempRec); 54 pBlkTable->close(); 55 pTempRec->close(); 56 AcDbIdMapping adimIdMap; 57 AcApDocument* pDoc = acDocManager->curDocument(); 58 acDocManager->lockDocument(pDoc, AcAp::kWrite);//锁定当前文档 59 AcDbObjectIdArray ptarr; 60 ptarr.append(Tid); 61 acdbHostApplicationServices()->workingDatabase()->wblockCloneObjects(ptarr, blockId, adimIdMap, AcDb::kDrcReplace); 62 acDocManager->unlockDocument(pDoc);//解锁文档 63 pSourceDwg->saveAs(fileName); 64 delete pSourceDwg; 65 pSourceDwg = NULL; 66 }