Advertisement
bueddl

objectarx join polylines

Jun 19th, 2016
180
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.92 KB | None | 0 0
  1.  
  2.     static Adesk::Boolean joinEntities(AcDbPolyline &joined, const AcDbPolyline &primary, const AcDbPolyline &secondary)
  3.     {
  4.         // Find out whether primary and/or secondary are
  5.         // intersecting each other. This may also hold
  6.         // true even if one is contained into the other.
  7.         // Assume false and attempt to prove true
  8.         Adesk::Boolean intersecting = false;
  9.         for (Adesk::uint i = 0; i < primary.numVerts(); ++i)
  10.         {
  11.             AcGeLineSeg2d pSegment;
  12.             primary.getLineSegAt(i, pSegment);
  13.  
  14.             for (Adesk::uint j = 0; j < secondary.numVerts(); ++j)
  15.             {
  16.                 AcGeLineSeg2d sSegment;
  17.                 secondary.getLineSegAt(j, sSegment);
  18.  
  19.                 AcGePoint2d segSegIntersect;
  20.                 // AcGeLineSeg2d::intersectWith also reports when both
  21.                 // lines are on top of each other.
  22.                 if (pSegment.intersectWith(sSegment, segSegIntersect))
  23.                 {
  24.                     intersecting = true;
  25.                     break;
  26.                 }
  27.             }
  28.  
  29.             if (intersecting)
  30.                 break;
  31.         }
  32.        
  33.         // Whether one polyline is contained in one other
  34.         Adesk::Boolean pContained, sContained;
  35.  
  36.         // Assume true and attempt to prove it false
  37.         pContained = true;
  38.         // Test if one polyline contains the other
  39.         // As we are running on the primary, keep an
  40.         // eye of a point that is not contained within
  41.         // the secondary (in which case we anyway would
  42.         // break) to start with later on.
  43.         Adesk::uint pStartIndex;
  44.         for (Adesk::uint i = 0; i < primary.numVerts(); ++i)
  45.         {
  46.             AcGePoint2d pPoint;
  47.             primary.getPointAt(i, pPoint);
  48.  
  49.             // App::contains also returns true if the given
  50.             // point is on a segment. This is ok for us.
  51.             if (!contains(secondary, pPoint))
  52.             {
  53.                 pContained = false;
  54.                 pStartIndex = i;
  55.                 break;
  56.             }
  57.         }
  58.            
  59.         // Assume true and attempt to prove it false
  60.         sContained = true;
  61.         // Test if one polyline contains the other
  62.         for (Adesk::uint j = 0; j < secondary.numVerts(); ++j)
  63.         {
  64.             AcGePoint2d sPoint;
  65.             secondary.getPointAt(j, sPoint);
  66.  
  67.             // App::contains also returns true if the given
  68.             // point is on a segment. This is ok for us.
  69.             if (!contains(primary, sPoint))
  70.             {
  71.                 sContained = false;
  72.                 break;
  73.             }
  74.         }
  75.  
  76.         if (!(intersecting || sContained || pContained))
  77.             return false;
  78.        
  79.         // Contained within each other
  80.         if (sContained || pContained)
  81.         {
  82.             acutPrintf(L"Not yet implemented: poly are contained within each other.\n");
  83.             return false;
  84.         }
  85.  
  86.         // If they are intersecting but not contained within
  87.         // each other, then we can pick any point of the
  88.         // primary that is not inside the secondary to start
  89.         // with.
  90.         AcGePoint2d pStartPoint, pos;
  91.         primary.getPointAt(pStartIndex, pStartPoint);
  92.        
  93.         // i is the primary polyline vertex index
  94.         // j is the secondary polyline vertex index
  95.         // k is the joined polyline vertex index
  96.         Adesk::uint i, j, k;
  97.         for (pos = pStartPoint, k = 0, i = pStartIndex; ;)
  98.         {
  99.             AcGePoint2d intersection;
  100.            
  101.             // Follow the primary poly as long as we are
  102.             // not intersecting
  103.             Adesk::Boolean intersects = false;
  104.             do {
  105.                 // Seek next primary segment
  106.                 AcGeLineSeg2d pSeg;
  107.                 primary.getLineSegAt(i++, pSeg);
  108.                 // Check collision
  109.                 for (j = 0; j < secondary.numVerts(); ++j)
  110.                 {
  111.                     AcGeLineSeg2d sSeg;
  112.                     secondary.getLineSegAt(j, sSeg);
  113.                     if (pSeg.intersectWith(sSeg, intersection))
  114.                     {
  115.                         intersects = true;
  116.                         break;
  117.                     }
  118.                 }
  119.                
  120.                 // Not intersecting for segment pSeg, so add it
  121.                 joined.addVertexAt(k++, pSeg.endPoint());
  122.             } while (!intersects);
  123.  
  124.             // Intersected secondary poly, add intersection
  125.             // and go on with following the secondary poly.
  126.             joined.addVertexAt(k++, intersection);
  127.  
  128.             // We are intersecting with segment j of secondary poly.
  129.             // Find out in which direction we have to go.
  130.             AcGeLineSeg2d sSeg;
  131.             secondary.getLineSegAt(j, sSeg);
  132.             // If the end point of the intersecting segment is
  133.             // inside the primary poly, then we have to go into the
  134.             // direction of the start point (reverse), otherwise
  135.             // forward.
  136.             // FIXME: this is not always correct
  137.             Adesk::Boolean reverse = false;
  138.             if (contains(primary, sSeg.endPoint()))
  139.                 reverse = true;
  140.            
  141.             do {
  142.                 // Check collision
  143.                 for (j = 0; j < secondary.numVerts(); ++j)
  144.                 {
  145.                     AcGeLineSeg2d pSeg;
  146.                     primary.getLineSegAt(i, pSeg);
  147.                     if (sSeg.intersectWith(pSeg, intersection))
  148.                     {
  149.                         intersects = true;
  150.                         break;
  151.                     }
  152.                 }
  153.                
  154.                 // Not intersecting for segment pSeg, so add it
  155.                 if (reverse)
  156.                     joined.addVertexAt(k++, sSeg.startPoint());
  157.                 else
  158.                     joined.addVertexAt(k++, sSeg.endPoint());
  159.                
  160.                 // Seek next secondary segment
  161.                 // TODO: handle wrap around
  162.                 if (reverse)
  163.                     secondary.getLineSegAt(j--, sSeg);
  164.                 else
  165.                     secondary.getLineSegAt(j++, sSeg);
  166.             } while (!intersects);
  167.  
  168.             // Intersected primary poly, add intersection
  169.             joined.addVertexAt(k++, intersection);
  170.  
  171.             // We have to end where we started
  172.             if (pos == pStartPoint)
  173.                 break;
  174.         }
  175.  
  176.         return true;
  177.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement