Tuesday, August 18, 2015

Find all AcDbLine Intersections


This question came from Sandhya who asked how to find all line intersections into a drawing.
In this case, we will consider only intersections between AcDbLine entities.

First we need to prepare our CMap structure to be able to handle AcGePoint3d as the map key. The idea is to group all Lines passing through each intersection point.

CMap does not support AcGePoint3d because it does not know how to Hash it and also how to compare it as a key. To allow that we will need to define both HasKey and CompareElements templates as follows:

// 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));

Next, we will collect the AcDbLine entities in ModelSpace:

// 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;

AcDbEntity *pEnt = NULL;
es = pIter->getEntity(pEnt, AcDb::kForRead);
if (es == Acad::eOk)
if (pEnt->isKindOf(AcDbLine::desc()))


delete pIter;
pIter = NULL;

if (arrLines.length() == 0)
acutPrintf(_T("There are no lines in Model Space!\n"));
acutPrintf(_T("We've found %d lines in Model Space!\nChecking intersection with tolerance %f...\n"), 
arrLines.length(), _dTol);

Ok, with the lines collected we will then build our CMap with the information we need:

// Process lines in pairs
CMap<AcGePoint3d,AcGePoint3d,AcDbObjectIdArray,AcDbObjectIdArray&> mapLines;

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)

if (arrExist.contains(pLineB->objectId()) == false)
// New point...
AcDbObjectIdArray arrNewEnts;


To demonstrate the use of this information, we then use our CMap data to create AcDbPoint entities on ModeSpace and also print a small report at the command prompt:

// 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;

CString sEnts;
for (int e=0; e<arrEnts.length(); e++)
ACHAR pBuff[255] = _T("");
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);

This is the sample DWG (remember to adjust the PTYPE):

And this is the prompt result:

We've found 8 lines in Model Space!
Checking intersection with tolerance 0.000100...
Point (49.4194, 7.7097, 0.0000) - Entities [1E0,1E1]
Point (43.8908, 18.4889, 0.0000) - Entities [1DF,1E0]
Point (37.2051, 11.5104, 0.0000) - Entities [1DF,1E1]
Point (32.4059, 13.0038, 0.0000) - Entities [1DE,1E1]
Point (33.7651, 7.9199, 0.0000) - Entities [1DE,1DF]
Point (30.4900, 20.1703, 0.0000) - Entities [1DD,1DE]
Point (20.6648, 16.6573, 0.0000) - Entities [1E1,1E2]
Point (22.1562, 13.7469, 0.0000) - Entities [1DD,1E2]
Point (24.4172, 15.4897, 0.0000) - Entities [1DD,1E1]
Point (17.0892, 9.8415, 0.0000) - Entities [1DC,1DD]
Point (16.3380, 17.5581, 0.0000) - Entities [1DB,1DC]

Full source code can be downloaded from here:


Best regards!

Saturday, June 27, 2015

AU2015 Classes

My Autodesk University 2015 classes were accepted.

SD9661: AutoCAD, JavaScript, and the Cloud

SD9660: Creating AutoCAD Cross-Platform Plug-ins

SD9662: Creating Professional Plug-ins for Autodesk Exchange Store

GEN9663: From AutoCAD I/O to View & Data

Hope to meet you there!

Thursday, December 25, 2014


Here are my AU 2014 class material posted to Autodesk University website:

SD4865 : AutoCAD, JavaScript, and the Cloud
SD4861 : Creating AutoCAD Cross-Platform Plug-ins
SD4871 : Creating Professional Plug-ins for Autodesk Exchange Store

If you prefer to download the files directly, here is the Google Drive folder URL:


Hope you enjoy!
Best regards.