使用wblockCloneObjects从后台读取dwg文件复制实体到当前数据库

        AcDbDatabase *pNewDb=new AcDbDatabase(Adesk::kFalse);        
        if (pNewDb == NULL)
        {
            return;
        }
        Acad::ErrorStatus es= pNewDb->readDwgFile(_T("D:\\123.dwg"));
        if (Acad::eOk != es || NULL == pNewDb)
        {
            acutPrintf(_T("\n打开dwg文件失败,错误码:%s"),acadErrorStatusText(es));
            return;
        }
        AcDbObjectIdArray objIdArray;
        AcDbBlockTable *pBlkTbl=NULL;
        es= pNewDb->getBlockTable(pBlkTbl,AcDb::kForRead);
        if (Acad::eOk != es || NULL == pBlkTbl)
        {
            acutPrintf(_T("\n获取源数据库块表失败,错误码:%s"),acadErrorStatusText(es));
            delete pNewDb;
            pNewDb=NULL;
            return;
        }
        AcDbBlockTableRecord *pMsBlkRcd=NULL;
        es= pBlkTbl->getAt(ACDB_MODEL_SPACE,pMsBlkRcd,AcDb::kForRead);
        pBlkTbl->close();
        if (Acad::eOk != es || NULL == pMsBlkRcd)
        {
            acutPrintf(_T("\n获取源数据库模型空间块表记录失败,错误码:%s"),acadErrorStatusText(es));
            delete pNewDb;
            pNewDb=NULL;
            return;
        }
        //遍历源数据库模型空间块记录中的实体
        AcDbBlockTableRecordIterator *pIter=NULL;
        pMsBlkRcd->newIterator(pIter);
        pMsBlkRcd->close();
        AcDbObjectId objId;        
        for (pIter->start();!pIter->done();pIter->step())
        {
            pIter->getEntityId(objId);
            objIdArray.append(objId);
            //将扩展字典改为硬拥有,否则写块出去后会丢失
            AcDbEntity *pEnt=NULL;
            es = pIter->getEntity(pEnt,AcDb::kForWrite);
            if (Acad::eOk == es)
            {
                AcDbObjectId dicId= pEnt->extensionDictionary();
                if (AcDbObjectId::kNull != dicId)
                {
                    AcDbObjectPointer<AcDbDictionary> pDic(dicId,AcDb::kForWrite);
                    if(Acad::eOk == pDic.openStatus())
                    {
                        pDic->setTreatElementsAsHard(Adesk::kTrue);
                        pDic->close();
                    }
                }
                pEnt->close();
            }
        }
        delete pIter;        

        //检查objectId
        if (objIdArray.isEmpty())
        {
            acutPrintf(_T("\n克隆失败,源数据库的模型空间没有实体!"));
            delete pNewDb;
            pNewDb=NULL;
            return;
        }
        AcDbIdMapping idMap; 
        AcDbDatabase *pDb= acdbHostApplicationServices()->workingDatabase();
        //获取当前数据库的空间ID
        AcDbObjectId mSpaceId= pDb->currentSpaceId();
        //写块克隆 忽略重定义模式
        es =  pNewDb->wblockCloneObjects(objIdArray,mSpaceId,idMap,AcDb::kDrcIgnore);
        if (Acad::eOk != es)
        {
            acutPrintf(_T("\ndeepCloneObjects失败错误码:%s"),acadErrorStatusText(es));
            delete pNewDb;
            pNewDb=NULL;
            return;
        }
        //删除临时数据库
        delete pNewDb;
        pNewDb=NULL;
        
    
        
        AcDbIdPair IdPair;
        AcDbObjectIdArray arrID2; 
        //获取克隆后的和原id匹配的对象的objectid,克隆idmap是全部的id映射,包括一些附带的数据
        for (int i=0;i<objIdArray.length();i++)  
        {
            IdPair.setKey(objIdArray[i]);
            if (idMap.compute(IdPair))
            {
                arrID2.append(IdPair.value());
            }
        }
        if (!arrID2.isEmpty())
        {
            acutPrintf(_T("\n复制:%d个对象成功!"),arrID2.length());
        }
        
        //#define Randmod(x) rand()%x
        //亮显选择集
        ads_name ss,ent;
        acedSSAdd(NULL,NULL,ss);
        for (int i=0;i<arrID2.length();i++)
        {
            AcDbEntityPointer pEnt(arrID2[i],AcDb::kForWrite);
            if (Acad::eOk == pEnt.openStatus())
            {
                //pEnt->setColorIndex(Randmod(11));
                acdbGetAdsName(ent,arrID2[i]);
                acedSSAdd(ent,ss,ss);
//                 if (pEnt->isKindOf(AcDbDimension::desc()))
//                 {
//                     AcDbDimension *pDim=AcDbDimension::cast(pEnt);
//                     pDim->recomputeDimBlock();
//                 }
                pEnt->recordGraphicsModified();
                pEnt->close();
            }                    
        }
        Adesk::Int32 nLength;
        acedSSLength(ss,&nLength);        
        if (nLength>0)
        {
            acedSSSetFirst(ss,NULL);
        }
        //acedCommand(RTSTR,_T("_.select"),RTPICKS,ss,RTSTR,_T(""),RTNONE);        
        acedSSFree(ss);
        

 

posted @ 2019-04-26 22:57  edata  阅读(965)  评论(0编辑  收藏