|
[code]// These are template classes to allow AcGePoint3d do be used as a Key do CMap class
const double _dTol = 0.0001;
template<> UINT AFXAPI HashKey<AcGePoint3d> (AcGePoint3d key)
{
CString sPoint;
sPoint.Format(_T("%f,%f,%f"),key.x, key.y ,key.z);
UINT iHash = (NULL == &key) ? 0 : HashKey((LPCSTR)sPoint.GetBuffer());
return iHash;
}
template<> BOOL AFXAPI CompareElements<AcGePoint3d, AcGePoint3d>
(const AcGePoint3d* pElement1, const AcGePoint3d* pElement2)
{
if ((pElement1 == NULL) || (pElement2 == NULL))
return false;
AcGeTol gTol;
gTol.setEqualPoint(_dTol); // Point comparison tolerance
return (pElement1->isEqualTo(*pElement2,gTol));
}
// Collect lines from ModelSpace
Acad::ErrorStatus es;
AcDbDatabase *pDb = acdbHostApplicationServices()->workingDatabase();
AcDbBlockTableRecordPointer pBTR(acdbSymUtil()->blockModelSpaceId(pDb),AcDb::kForWrite);
AcDbBlockTableRecordIterator *pIter = NULL;
pBTR->newIterator(pIter, true);
AcDbObjectIdArray arrLines;
while(!pIter->done())
{
AcDbEntity *pEnt = NULL;
es = pIter->getEntity(pEnt, AcDb::kForRead);
if (es == Acad::eOk)
{
if (pEnt->isKindOf(AcDbLine::desc()))
arrLines.append(pEnt->objectId());
pEnt->close();
}
pIter->step(true);
}
delete pIter;
pIter = NULL;
if (arrLines.length() == 0)
{
acutPrintf(_T("There are no lines in Model Space!\n"));
return;
}
else
{
acutPrintf(_T("We've found %d lines in Model Space!\nChecking intersection with tolerance %f...\n"),
arrLines.length(), _dTol);
}
// Process lines in pairs
CMap<AcGePoint3d,AcGePoint3d,AcDbObjectIdArray,AcDbObjectIdArray&> mapLines;
acdbTransactionManager->startTransaction();
for (int i=0; i<arrLines.length()-1; i++)
{
AcDbLine* pLineA = NULL;
if (acdbTransactionManager->getObject((AcDbObject*&)pLineA,arrLines<i>, AcDb::kForRead) == Acad::eOk)
{
for (int j=i+1; j<arrLines.length(); j++)
{
AcDbLine* pLineB = NULL;
if (acdbTransactionManager->getObject((AcDbObject*&)pLineB,arrLines[j], AcDb::kForRead) == Acad::eOk)
{
AcGePoint3dArray arrPts;
if (pLineA->intersectWith(pLineB,AcDb::kOnBothOperands,arrPts) == Acad::eOk)
{
if (arrPts.length() > 0)
{
for (int p=0; p<arrPts.length(); p++)
{
AcDbObjectIdArray arrExist;
if (mapLines.Lookup(arrPts[p],arrExist) == TRUE)
{
// Existing point...
if (arrExist.contains(pLineA->objectId()) == false)
mapLines[arrPts[p]].append(pLineA->objectId());
if (arrExist.contains(pLineB->objectId()) == false)
mapLines[arrPts[p]].append(pLineB->objectId());
}
else
{
// New point...
AcDbObjectIdArray arrNewEnts;
arrNewEnts.append(pLineA->objectId());
arrNewEnts.append(pLineB->objectId());
mapLines.SetAt(arrPts[p],arrNewEnts);
}
}
}
}
}
}
}
}
acdbTransactionManager->endTransaction();
// Just as demonstration, walk through points and add an AcDbPoint entity to ModelSpace then print the info
POSITION pos = mapLines.GetStartPosition();
while (pos)
{
AcGePoint3d ptKey(0,0,0);
AcDbObjectIdArray arrEnts;
mapLines.GetNextAssoc(pos,ptKey, arrEnts);
AcDbPoint* ptEnt = new AcDbPoint(ptKey);
AcDbObjectId idPointEnt;
pBTR->appendAcDbEntity(idPointEnt,ptEnt);
ptEnt->close();
CString sEnts;
for (int e=0; e<arrEnts.length(); e++)
{
ACHAR pBuff[255] = _T("");
arrEnts[e].handle().getIntoAsciiBuffer(pBuff);
CString sBuff;
sBuff.Format( (e==arrEnts.length()-1) ? _T("%s"): _T("%s,"), pBuff);
sEnts += sBuff;
}
CString sPromptReport;
sPromptReport.Format(_T("Point (%.4f, %.4f, %.4f) - Entities [%s]\n"),ptKey.x, ptKey.y, ptKey.z, sEnts);
acutPrintf(sPromptReport);
}[/code] |
|