找回密码
 立即注册

QQ登录

只需一步,快速开始

扫一扫,访问微社区

查看: 624|回复: 0

[每日一码] 查找多个AcDbLine的所有交点

[复制链接]

已领礼包: 13个

财富等级: 恭喜发财

发表于 2016-12-24 19:14:46 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有账号?立即注册

×
本帖最后由 LoveArx 于 2016-12-24 19:29 编辑

        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:




  1. // These are template classes to allow AcGePoint3d do be used as a Key do CMap class
  2. const double _dTol = 0.0001;

  3. template<> UINT AFXAPI HashKey<AcGePoint3d> (AcGePoint3d key)
  4. {
  5.         CString sPoint;
  6.         sPoint.Format(_T("%f,%f,%f"),key.x, key.y ,key.z);

  7.         UINT iHash = (NULL == &key) ? 0 : HashKey((LPCSTR)sPoint.GetBuffer());
  8.         return iHash;
  9. }

  10. template<> BOOL AFXAPI CompareElements<AcGePoint3d, AcGePoint3d>
  11. (const AcGePoint3d* pElement1, const AcGePoint3d* pElement2)
  12. {
  13.         if ((pElement1 == NULL) || (pElement2 == NULL))
  14.                 return false;

  15.         AcGeTol gTol;
  16.         gTol.setEqualPoint(_dTol); // Point comparison tolerance
  17.         return (pElement1->isEqualTo(*pElement2,gTol));
  18. }


Next, we will collect the AcDbLine entities in ModelSpace:

  1. // Collect lines from ModelSpace
  2. Acad::ErrorStatus es;
  3. AcDbDatabase *pDb = acdbHostApplicationServices()->workingDatabase();
  4. AcDbBlockTableRecordPointer pBTR(acdbSymUtil()->blockModelSpaceId(pDb),AcDb::kForWrite);

  5. AcDbBlockTableRecordIterator *pIter = NULL;
  6. pBTR->newIterator(pIter, true);
  7. AcDbObjectIdArray arrLines;

  8. while(!pIter->done())
  9. {
  10.         AcDbEntity *pEnt = NULL;
  11.         es = pIter->getEntity(pEnt, AcDb::kForRead);
  12.         if (es == Acad::eOk)
  13.         {
  14.                 if (pEnt->isKindOf(AcDbLine::desc()))
  15.                         arrLines.append(pEnt->objectId());

  16.                 pEnt->close();
  17.         }

  18.         pIter->step(true);
  19. }
  20. delete pIter;
  21. pIter = NULL;

  22. if (arrLines.length() == 0)
  23. {
  24.         acutPrintf(_T("There are no lines in Model Space!\n"));
  25.         return;
  26. }
  27. else
  28. {
  29.         acutPrintf(_T("We've found %d lines in Model Space!\nChecking intersection with tolerance %f...\n"),
  30.                 arrLines.length(), _dTol);
  31. }


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

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

  3. acdbTransactionManager->startTransaction();
  4. for (int i=0; i<arrLines.length()-1; i++)
  5. {
  6.         AcDbLine* pLineA = NULL;
  7.         if (acdbTransactionManager->getObject((AcDbObject*&)pLineA,arrLines, AcDb::kForRead) == Acad::eOk)
  8.         {
  9.                 for (int j=i+1; j<arrLines.length(); j++)
  10.                 {
  11.                         AcDbLine* pLineB = NULL;
  12.                         if (acdbTransactionManager->getObject((AcDbObject*&)pLineB,arrLines[j], AcDb::kForRead) == Acad::eOk)
  13.                         {
  14.                                 AcGePoint3dArray arrPts;
  15.                                 if (pLineA->intersectWith(pLineB,AcDb::kOnBothOperands,arrPts) == Acad::eOk)
  16.                                 {
  17.                                         if (arrPts.length() > 0)
  18.                                         {
  19.                                                 for (int p=0; p<arrPts.length(); p++)
  20.                                                 {
  21.                                                         AcDbObjectIdArray arrExist;
  22.                                                         if (mapLines.Lookup(arrPts[p],arrExist) == TRUE)
  23.                                                         {
  24.                                                                 // Existing point...
  25.                                                                 if (arrExist.contains(pLineA->objectId()) == false)
  26.                                                                         mapLines[arrPts[p]].append(pLineA->objectId());

  27.                                                                 if (arrExist.contains(pLineB->objectId()) == false)
  28.                                                                         mapLines[arrPts[p]].append(pLineB->objectId());
  29.                                                         }
  30.                                                         else
  31.                                                         {
  32.                                                                 // New point...
  33.                                                                 AcDbObjectIdArray arrNewEnts;
  34.                                                                 arrNewEnts.append(pLineA->objectId());
  35.                                                                 arrNewEnts.append(pLineB->objectId());
  36.                                                                 mapLines.SetAt(arrPts[p],arrNewEnts);
  37.                                                         }
  38.                                                 }
  39.                                         }
  40.                                 }
  41.                         }
  42.                 }
  43.         }
  44. }

  45. acdbTransactionManager->endTransaction();


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:

  1. // Just as demonstration, walk through points and add an AcDbPoint entity to ModelSpace then print the info
  2. POSITION pos = mapLines.GetStartPosition();
  3. while (pos)
  4. {
  5.         AcGePoint3d ptKey(0,0,0);
  6.         AcDbObjectIdArray arrEnts;
  7.         mapLines.GetNextAssoc(pos,ptKey, arrEnts);

  8.         AcDbPoint* ptEnt = new AcDbPoint(ptKey);
  9.         AcDbObjectId idPointEnt;
  10.         pBTR->appendAcDbEntity(idPointEnt,ptEnt);
  11.         ptEnt->close();

  12.         CString sEnts;
  13.         for (int e=0; e<arrEnts.length(); e++)
  14.         {
  15.                 ACHAR pBuff[255] = _T("");
  16.                 arrEnts[e].handle().getIntoAsciiBuffer(pBuff);
  17.                 CString sBuff;
  18.                 sBuff.Format( (e==arrEnts.length()-1) ? _T("%s"): _T("%s,"), pBuff);
  19.                 sEnts += sBuff;
  20.         }

  21.         CString sPromptReport;
  22.         sPromptReport.Format(_T("Point (%.4f, %.4f, %.4f) - Entities [%s]\n"),ptKey.x, ptKey.y, ptKey.z, sEnts);
  23.         acutPrintf(sPromptReport);
  24. }


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


0.jpg





And this is the prompt result:


        And this is the prompt result:

Command: LINEINTS
                         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:
0.jpg
论坛插件加载方法
发帖求助前要善用【论坛搜索】功能,那里可能会有你要找的答案;
如果你在论坛求助问题,并且已经从坛友或者管理的回复中解决了问题,请把帖子标题加上【已解决】;
如何回报帮助你解决问题的坛友,一个好办法就是给对方加【D豆】,加分不会扣除自己的积分,做一个热心并受欢迎的人!
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|申请友链|Archiver|手机版|小黑屋|辽公网安备|晓东CAD家园 ( 辽ICP备15016793号 )

GMT+8, 2024-5-5 19:02 , Processed in 0.327072 second(s), 30 queries , Gzip On.

Powered by Discuz! X3.5

© 2001-2024 Discuz! Team.

快速回复 返回顶部 返回列表