wblockCloneObjects从块定义写块到外部文件

wblockCloneObjects从块定义写块到外部文件简单例子

        AcDbObjectIdArray objIds;
        ads_name ent;
        ads_point pt;
        if (RTNORM != acedEntSel(_T("\n选择一个块参照对象:"),ent,pt))
        {
            acutPrintf(_T("\n未选择有效对象!"));
            return;
        }
        AcDbObjectId objId;
        acdbGetObjectId(objId,ent);
        AcDbObjectPointer<AcDbBlockReference> pBlkRef(objId,AcDb::kForRead);
        if (Acad::eOk != pBlkRef.openStatus())
        {
            acutPrintf(_T("\n未选择块参照对象!"));
            return;
        }
        AcDbObjectId blkId=pBlkRef->blockTableRecord();
        AcDbBlockTableRecordPointer pBlkDef(blkId,AcDb::kForRead);
        if (Acad::eOk != pBlkDef.openStatus())
        {
            acutPrintf(_T("\n打开块定义对象失败!"));
            return;
        }                
        AcDbBlockTableRecordIterator *pNewItor=NULL;
        pBlkDef->newIterator(pNewItor);
        if (pNewItor==NULL)
        {
            acutPrintf(_T("\n创建块定义遍历器失败!"));
            return;
        }        
        for (pNewItor->start();!pNewItor->done();pNewItor->step())
        {
            AcDbObjectId entId;
            pNewItor->getEntityId(entId);
            objIds.append(entId);
            //将扩展字典改为硬拥有,否则写块出去后会丢失
            AcDbEntity *pEnt=NULL;
            Acad::ErrorStatus es = pNewItor->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 pNewItor;
        if (objIds.length()<1)
        {
            acutPrintf(_T("\n块定义中没有实体对象!"));
            return;
        }
        AcDbDatabase *pNewDb=new AcDbDatabase();
        if (pNewDb==NULL)
        {
            acutPrintf(_T("\n创建新数据库对象失败!"));
            return;
        }
        AcDbObjectId msId=pNewDb->currentSpaceId();
        AcDbIdMapping idMap;
        //获取对象id的原始数据库指针
        AcDbDatabase *pOriginalDatabase=objIds.at(0).originalDatabase();
        //恢复到外部参照符号状态
        pOriginalDatabase->restoreOriginalXrefSymbols();
        //写块克隆
        Acad::ErrorStatus es=pOriginalDatabase->wblockCloneObjects(objIds,msId,idMap,AcDb::kDrcIgnore);    
        //恢复外部参照符号之前的状态
        pOriginalDatabase->restoreForwardingXrefSymbols();
        if(Acad::eOk == es)
        {
            acutPrintf(_T("\n克隆成功!"));
        }
        else
        {
            acutPrintf(_T("\n克隆出错,错误码%s"),acadErrorStatusText(es));
        }        
        pNewDb->saveAs(_T("d:\\myclone.dwg"));
        delete pNewDb;
        pNewDb=NULL;

 

posted @ 2020-09-25 12:28  edata  阅读(311)  评论(1编辑  收藏  举报