Files
envi-code/SourceCode/Code2026/tg_shs/shs_public/DwgCompareDbMgr.cpp
T
2026-09-28 15:28:50 +08:00

367 lines
7.5 KiB
C++

#include "stdafx.h"
#include "DwgCompareDbMgr.h"
#include <dbidmap.h>
CDwgCompareDbInfo::CDwgCompareDbInfo(void)
: m_sName(_T(""))
, m_sPath(_T(""))
, m_pDb(NULL)
, m_iEntCount(0)
{
}
CDwgCompareDbInfo::~CDwgCompareDbInfo(void)
{
if (m_pDb != NULL)
{
delete m_pDb;
m_pDb = NULL;
}
}
BOOL CDwgCompareDbInfo::LoadFile( const CString sName, const CString sPath )
{
if (m_pDb != NULL)
{
delete m_pDb;
}
m_pDb = new AcDbDatabase(false, true);
XString sFile(256), sFile1(256);
sFile1 << sPath;
if(!FindFile(sFile1, sFile))
{
return FALSE;
}
#if ADS > 20
Acad::ErrorStatus es = m_pDb->readDwgFile(sFile1, AcDbDatabase::OpenMode::kForReadAndAllShare, false);
#else
Acad::ErrorStatus es = m_pDb->readDwgFile(sFile1, _SH_DENYNO, false);
#endif
// modify by cq 2017-01-05 支持CAD2017
if (es != Acad::eOk)
{
return FALSE;
}
//设置插入基点为原点
es = m_pDb->setInsbase(AcGePoint3d::kOrigin);
//es = m_pDb->updateExt(false);
m_sPath = sPath;
m_sName = sName;
//获取所有图层名
InitLayerInfo();
return TRUE;
}
BOOL CDwgCompareDbInfo::LoadFile( const CString sName, const CString sPath, XPoly2D poly )
{
if (!LoadFile(sName, sPath))
{
return FALSE;
}
//遍历模型空间,删除不在entPoly内的实体
AcDbBlockTable * pBlockTab = NULL;
Acad::ErrorStatus es = m_pDb->getBlockTable(pBlockTab, AcDb::kForRead);
if (es==Acad::eOk && pBlockTab!=NULL)
{
AcDbBlockTableRecord * pRec = NULL;
es = pBlockTab->getAt(ACDB_MODEL_SPACE, pRec, AcDb::kForRead);
pBlockTab->close();
AcDbBlockTableRecordIterator *pIterator = NULL;
if (es==Acad::eOk && pRec!=NULL)
{
pRec->newIterator(pIterator);
pRec->close();
}
for(pIterator->start(); !pIterator->done(); pIterator->step())
{
AcDbEntity *pEnt = NULL;
es = pIterator->getEntity(pEnt, AcDb::kForWrite);
if (es!=Acad::eOk || pEnt==NULL)
{
continue;
}
AcDbExtents bExtent;
pEnt->getGeomExtents(bExtent);
XPoint ptMax = bExtent.maxPoint();
XPoint ptMin = bExtent.minPoint();
ptMax.z = 0;
ptMin.z = 0;
//只保留和poly有交集的实体
if((ptMax&poly)==PR_INSIDE || (ptMin&poly)==PR_INSIDE)
{
pEnt->close();
continue;
}
pEnt->erase();
pEnt->close();
}
delete pIterator;
}
//m_pDb->updateExt(false);
return TRUE;
}
BOOL CDwgCompareDbInfo::LoadDb( CString& csFilePath, AcDbDatabase*& pDb, const AcDbObjectIdArray& ObjIds, AcGePoint3d& ptBase )
{
if (csFilePath.IsEmpty() || pDb == NULL || ObjIds.length()==0)
{
return FALSE;
}
if (m_pDb != NULL)
{
delete m_pDb;
}
m_pDb = new AcDbDatabase(false, true);
Acad::ErrorStatus es = pDb->wblock(m_pDb, ObjIds, ptBase);
if (es != Acad::eOk)
{
CString csError;
csError.Format(_T("\n文件加载失败:%s"), csFilePath);
acutPrintf(csError);
return FALSE;
}
m_sPath = csFilePath;
m_sName = m_sPath.Right(m_sPath.GetLength() - m_sPath.ReverseFind(_T('\\')) -1);
//获取所有图层名
InitLayerInfo();
//m_pDb->updateExt(false);
return TRUE;
}
void CDwgCompareDbInfo::InitLayerInfo()
{
if (m_pDb==NULL)
{
return;
}
m_vtLayerAll.clear();
AcDbLayerTable * pLayerTab = NULL;
Acad::ErrorStatus es = m_pDb->getLayerTable(pLayerTab, AcDb::kForRead);
if (es!=Acad::eOk || pLayerTab==NULL)
{
return;
}
AcDbLayerTableIterator * pIte = NULL;
es = pLayerTab->newIterator(pIte);
pLayerTab->close();
if (es!=Acad::eOk || pIte==NULL)
{
return;
}
for (pIte->start(); !pIte->done(); pIte->step())
{
AcDbLayerTableRecord * pLayerRec = NULL;
es = pIte->getRecord(pLayerRec, AcDb::kForRead);
if (es!=Acad::eOk || pLayerRec==NULL)
{
continue;
}
TCHAR * szLayer;
pLayerRec->getName(szLayer);
m_vtLayerAll.push_back(szLayer);
delete szLayer;
pLayerRec->close();
}
delete pIte;
}
void CDwgCompareDbInfo::GetAllLayer( std::vector<CString>& vtLayer ) const
{
vtLayer = m_vtLayerAll;
}
CString CDwgCompareDbInfo::GetFileName() const
{
return m_sName;
}
CString CDwgCompareDbInfo::GetFilePath() const
{
return m_sPath;
}
AcDbDatabase * CDwgCompareDbInfo::GetDb() const
{
return m_pDb;
}
int CDwgCompareDbInfo::InitEntCount()
{
m_iEntCount = 0;
if (m_pDb == NULL)
{
return m_iEntCount;
}
//遍历模型空间,统计实体个数
AcDbBlockTable * pBlockTab = NULL;
Acad::ErrorStatus es = m_pDb->getBlockTable(pBlockTab, AcDb::kForRead);
if (es==Acad::eOk && pBlockTab!=NULL)
{
AcDbBlockTableRecord * pRec = NULL;
es = pBlockTab->getAt(ACDB_MODEL_SPACE, pRec, AcDb::kForRead);
pBlockTab->close();
AcDbBlockTableRecordIterator *pIterator = NULL;
if (es==Acad::eOk && pRec!=NULL)
{
pRec->newIterator(pIterator);
pRec->close();
}
std::map<AcDbObjectId, int> mapEntCountInBlock; //块内实体个数
for(pIterator->start(); !pIterator->done(); pIterator->step())
{
m_iEntCount++;
//块参照统计块内实体数
AcDbEntity *pEnt = NULL;
es = pIterator->getEntity(pEnt, AcDb::kForRead);
if (es!=Acad::eOk || pEnt==NULL)
{
continue;
}
if (pEnt->isKindOf(AcDbBlockReference::desc()))
{
AcDbBlockReference * pRef = NULL;
pRef = AcDbBlockReference::cast(pEnt);
if (pRef==NULL)
{
continue;
}
AcDbObjectId idBlock = pRef->blockTableRecord();
m_iEntCount += GetEntCountInBlock(idBlock, mapEntCountInBlock);
}
pEnt->close();
}
delete pIterator;
}
return m_iEntCount;
}
int CDwgCompareDbInfo::GetEntCount() const
{
return m_iEntCount;
}
int CDwgCompareDbInfo::GetEntCountInBlock( AcDbObjectId& idBlock, std::map<AcDbObjectId, int>& mapBlock )
{
int iCount = 0;
if (idBlock.isNull())
{
return 0;
}
//已经计算过的块
if (mapBlock[idBlock] > 0)
{
return mapBlock[idBlock];
}
//未计算过的,遍历
AcDbBlockTableRecord * pRec = NULL;
Acad::ErrorStatus es = acdbOpenObject(pRec, idBlock, AcDb::kForRead);
if (es!=Acad::eOk || pRec==NULL)
{
return 0;
}
AcDbBlockTableRecordIterator* pIter = NULL;
es = pRec->newIterator(pIter);
pRec->close();
if (es!=Acad::eOk || pIter==NULL)
{
return 0;
}
for (pIter->start(); !pIter->done(); pIter->step())
{
iCount++;
}
delete pIter;
mapBlock[idBlock] = iCount;
return iCount;
}
//////////////////////////////////////////////////////////////////////////
CDwgCompareDbMgr::CDwgCompareDbMgr(void)
: m_bAllLayer(TRUE)
, m_bCmpClosedLayer(TRUE)
, m_bCmpFrozenLayer(TRUE)
, m_bCmpLockedlayer(TRUE)
{
}
CDwgCompareDbMgr::~CDwgCompareDbMgr(void)
{
if (m_pDb != NULL)
{
delete m_pDb;
m_pDb = NULL;
}
}
void CDwgCompareDbMgr::SetCompareLayer( std::vector<CString>& vtLayer )
{
m_vtLayerCmp.clear();
m_vtLayerCmp = vtLayer;
}
void CDwgCompareDbMgr::Init()
{
m_sName.Empty();
m_sPath.Empty();
m_vtLayerAll.clear();
m_vtLayerCmp.clear();
if (m_pDb != NULL)
{
delete m_pDb;
m_pDb = NULL;
}
}
void CDwgCompareDbMgr::GetCmpLayer( std::vector<CString>& vtLayer ) const
{
vtLayer.clear();
//无特殊条件都比对
if (m_bCmpClosedLayer && m_bCmpFrozenLayer && m_bCmpLockedlayer)
{
vtLayer = m_vtLayerCmp;
return;
}
//有不比对的条件,vtLayer中删除相应的图层
AcDbLayerTable * pLayerTab = NULL;
Acad::ErrorStatus es = m_pDb->getLayerTable(pLayerTab, AcDb::kForRead);
if (es!=Acad::eOk || pLayerTab==NULL)
{
return;
}
for (int i=0; i<(int)m_vtLayerCmp.size(); i++)
{
AcDbLayerTableRecord * pLayerRec = NULL;
es = pLayerTab->getAt(m_vtLayerCmp[i], pLayerRec, AcDb::kForRead);
if (es!=Acad::eOk || pLayerRec==NULL)
{
continue;
}
BOOL bAppend = TRUE;
if ( (!m_bCmpClosedLayer && pLayerRec->isOff())
|| (!m_bCmpFrozenLayer && pLayerRec->isFrozen())
|| (!m_bCmpLockedlayer && pLayerRec->isLocked())
)
{
bAppend = FALSE;
}
if (bAppend)
{
vtLayer.push_back(m_vtLayerCmp[i]);
}
pLayerRec->close();
}
pLayerTab->close();
}