- 积分
- 316
- 明经币
- 个
- 注册时间
- 2004-10-13
- 在线时间
- 小时
- 威望
-
- 金钱
- 个
- 贡献
-
- 激情
-
|
发表于 2004-10-13 23:15:00
|
显示全部楼层
基本概念错误
想的太简单了
从一个文件中的块定义Id不能直接拿到另一个文件中作为块参照Id用的,给你一个函数参考
它将外部DWG文件中的块定义复制到当前文件中作为块定义,之后用这个块在当前文件中的块定义Id并插入
//将外部DWG中的块定义导入到当前图形中 //如果当前有同名块定义,则无论是否被参照都将被覆盖,同时参照实体更新 Acad::ErrorStatus ImportBlockDefineFromDwg(CString strSourceDwgFile,CString strBlockName,CString strNewBlockName) { AcDbDatabase* pSourceDb; Acad::ErrorStatus es;
//打开外部文件 pSourceDb=new AcDbDatabase(Adesk::kFalse); es=pSourceDb->readDwgFile(strSourceDwgFile); if (es!=Acad::eOk) { //ads_printf("Error readDwgFile %d",es); delete pSourceDb; return es; } //获取源文件中的块表 AcDbBlockTable *pBlockTable; #ifdef ACADR14 es=pSourceDb->getBlockTable(pBlockTable,AcDb::kForRead); #else es=pSourceDb->getSymbolTable(pBlockTable,AcDb::kForRead); #endif if (es!=Acad::eOk) { //ads_printf("Error getSymBolTable %d",es); delete pSourceDb; return es; } //寻找块strBlockName AcDbBlockTableRecord * pBlockTableRecord; es = pBlockTable->getAt(strBlockName,pBlockTableRecord,AcDb::kForRead); if (es!=Acad::eOk) { //源DWG中未找到块定义 delete pSourceDb; pBlockTable->close(); return es; }
//插入到目标DWG中 AcDbObjectId eId; AcGePoint3d ptBase; AcDbObjectIdArray ayBlock,aySolids; AcDbDatabase *pBlockDatabase; AcDbEntity *pEnt; ptBase.set(0,0,0);
//遍历源DWG中组成块定义的实体,并添加到实体表ayBlock中 AcDbBlockTableRecordIterator *pBlkIter; pBlockTableRecord->newIterator(pBlkIter); for (pBlkIter->start();!pBlkIter->done();pBlkIter->step()) { pBlkIter->getEntityId(eId); if (acdbOpenObject(pEnt,eId,AcDb::kForRead)==Acad::eOk) { if (pEnt->isKindOf(AcDbSolid::desc()) || pEnt->isKindOf(AcDb2dPolyline::desc()) || pEnt->isKindOf(AcDbPolyline::desc()) || pEnt->isKindOf(AcDbFace::desc()) || pEnt->isKindOf(CommonPolyline::desc()) || pEnt->isKindOf(CommonSolid::desc()) ) { aySolids.append(eId); } else { ayBlock.append(eId); } pEnt->close(); } } delete pBlkIter; //将aySolids中底实体按大至小排序,先显示大图形 int i; int iLen=aySolids.length(); if (iLen>0) { BOOL bExchange=TRUE; double dArea[2]; AcDbExtents extEnt1,extEnt2; AcGePoint3d ptExt[2]; AcDbObjectId eId1,eId2; AcDbEntity *pEnt1,*pEnt2;
while (bExchange==TRUE) { bExchange=FALSE; for (i=0;i<iLen-1;i++) { eId1=aySolids; eId2=aySolids[i+1]; if (acdbOpenObject(pEnt1,eId1,AcDb::kForRead)==Acad::eOk) { if (acdbOpenObject(pEnt2,eId2,AcDb::kForRead)==Acad::eOk) { if (pEnt1->getGeomExtents(extEnt1)==Acad::eOk) { if (pEnt2->getGeomExtents(extEnt2)==Acad::eOk) { ptExt[0]=extEnt1.minPoint(); ptExt[1]=extEnt1.maxPoint(); dArea[0]=fabs((ptExt[1][X]-ptExt[0][X])*(ptExt[1][Y]-ptExt[0][Y])); ptExt[0]=extEnt2.minPoint(); ptExt[1]=extEnt2.maxPoint(); dArea[1]=fabs((ptExt[1][X]-ptExt[0][X])*(ptExt[1][Y]-ptExt[0][Y])); if (dArea[0]<dArea[1]) { bExchange=TRUE; aySolids=eId2; aySolids[i+1]=eId1; } } } pEnt2->close(); } pEnt1->close(); } if (bExchange==TRUE) {break;} } } for (i=0;i<ayBlock.length();i++) {aySolids.append(ayBlock);} ayBlock=aySolids; } /////////////////////////////////////////////////////////////////////////////////// pBlockTableRecord->close(); pBlockTable->close();
//写新块数据到pBlockDatabase; es=pSourceDb->wblock(pBlockDatabase,ayBlock,ptBase); //ads_printf("\nwblock=%d",es);
AcDbObjectId eNewBlockId; //将数据pBlockDatabase读入到目标DWG中,新块定义eNewBlockId; es = CurrectWorkingDWG->insert(eNewBlockId, strNewBlockName, pBlockDatabase, Adesk::kTrue); //ads_printf("\nInsert=%d",es); delete pBlockDatabase; delete pSourceDb; return es; } |
|