You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

286 lines
8.4 KiB

  1. /**
  2. * @file zones_test_and_combine_areas.cpp
  3. * @brief Functions to test, merge and cut polygons used as copper areas outlines
  4. * some pieces of code come from FreePCB.
  5. */
  6. /*
  7. * This program source code file is part of KiCad, a free EDA CAD application.
  8. *
  9. * Copyright (C) 2012 Jean-Pierre Charras, jean-pierre.charras@ujf-grenoble.fr
  10. * Copyright (C) 1992-2012 KiCad Developers, see AUTHORS.txt for contributors.
  11. *
  12. * Some code comes from FreePCB.
  13. *
  14. * This program is free software; you can redistribute it and/or
  15. * modify it under the terms of the GNU General Public License
  16. * as published by the Free Software Foundation; either version 2
  17. * of the License, or (at your option) any later version.
  18. *
  19. * This program is distributed in the hope that it will be useful,
  20. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  21. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  22. * GNU General Public License for more details.
  23. *
  24. * You should have received a copy of the GNU General Public License
  25. * along with this program; if not, you may find one here:
  26. * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
  27. * or you may search the http://www.gnu.org website for the version 2 license,
  28. * or you may write to the Free Software Foundation, Inc.,
  29. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
  30. */
  31. #include <fctsys.h>
  32. #include <common.h>
  33. #include <confirm.h>
  34. #include <undo_redo_container.h>
  35. #include <class_board.h>
  36. #include <class_zone.h>
  37. #include <class_marker_pcb.h>
  38. #include <pcbnew.h>
  39. #include <drc.h>
  40. #include <math_for_graphics.h>
  41. #define STRAIGHT 0 // To be remove after math_for_graphics code cleanup
  42. bool BOARD::OnAreaPolygonModified( PICKED_ITEMS_LIST* aModifiedZonesList,
  43. ZONE_CONTAINER* modified_area )
  44. {
  45. // clip polygon against itself
  46. bool modified = NormalizeAreaPolygon( aModifiedZonesList, modified_area );
  47. // now see if we need to clip against other areas
  48. /*
  49. LAYER_NUM layer = modified_area->GetLayer();
  50. */
  51. bool bCheckAllAreas = TestAreaIntersections( modified_area );
  52. if( bCheckAllAreas )
  53. {
  54. modified = true;
  55. CombineAllAreasInNet( aModifiedZonesList, modified_area->GetNetCode(), true );
  56. }
  57. /*
  58. FIXME : do we really need this?
  59. if( !IsCopperLayer( layer ) ) // Refill non copper zones on this layer
  60. {
  61. for( unsigned ia = 0; ia < m_ZoneDescriptorList.size(); ia++ )
  62. if( m_ZoneDescriptorList[ia]->GetLayer() == layer )
  63. m_ZoneDescriptorList[ia]->BuildFilledSolidAreasPolygons( this );
  64. }
  65. */
  66. // Test for bad areas: all zones must have more than 2 corners:
  67. // Note: should not happen, but just in case.
  68. for( unsigned ii = 0; ii < m_ZoneDescriptorList.size(); )
  69. {
  70. ZONE_CONTAINER* zone = m_ZoneDescriptorList[ii];
  71. if( zone->GetNumCorners() >= 3 )
  72. ii++;
  73. else // Remove zone because it is incorrect:
  74. RemoveArea( aModifiedZonesList, zone );
  75. }
  76. return modified;
  77. }
  78. bool BOARD::CombineAllAreasInNet( PICKED_ITEMS_LIST* aDeletedList, int aNetCode,
  79. bool aUseLocalFlags )
  80. {
  81. if( m_ZoneDescriptorList.size() <= 1 )
  82. return false;
  83. bool modified = false;
  84. // Loop through all combinations
  85. for( unsigned ia1 = 0; ia1 < m_ZoneDescriptorList.size() - 1; ia1++ )
  86. {
  87. ZONE_CONTAINER* curr_area = m_ZoneDescriptorList[ia1];
  88. if( curr_area->GetNetCode() != aNetCode )
  89. continue;
  90. // legal polygon
  91. BOX2I b1 = curr_area->Outline()->BBox();
  92. bool mod_ia1 = false;
  93. for( unsigned ia2 = m_ZoneDescriptorList.size() - 1; ia2 > ia1; ia2-- )
  94. {
  95. ZONE_CONTAINER* area2 = m_ZoneDescriptorList[ia2];
  96. if( area2->GetNetCode() != aNetCode )
  97. continue;
  98. if( curr_area->GetPriority() != area2->GetPriority() )
  99. continue;
  100. if( curr_area->GetIsKeepout() != area2->GetIsKeepout() )
  101. continue;
  102. if( curr_area->GetLayer() != area2->GetLayer() )
  103. continue;
  104. BOX2I b2 = area2->Outline()->BBox();
  105. if( b1.Intersects( b2 ) )
  106. {
  107. // check area2 against curr_area
  108. if( curr_area->GetLocalFlags() || area2->GetLocalFlags()
  109. || aUseLocalFlags == false )
  110. {
  111. bool ret = TestAreaIntersection( curr_area, area2 );
  112. if( ret )
  113. ret = CombineAreas( aDeletedList, curr_area, area2 );
  114. if( ret )
  115. {
  116. mod_ia1 = true;
  117. modified = true;
  118. }
  119. }
  120. }
  121. }
  122. if( mod_ia1 )
  123. ia1--; // if modified, we need to check it again
  124. }
  125. return modified;
  126. }
  127. bool BOARD::TestAreaIntersections( ZONE_CONTAINER* area_to_test )
  128. {
  129. for( unsigned ia2 = 0; ia2 < m_ZoneDescriptorList.size(); ia2++ )
  130. {
  131. ZONE_CONTAINER* area2 = m_ZoneDescriptorList[ia2];
  132. if( area_to_test->GetNetCode() != area2->GetNetCode() )
  133. continue;
  134. if( area_to_test == area2 )
  135. continue;
  136. // see if areas are on same layer
  137. if( area_to_test->GetLayer() != area2->GetLayer() )
  138. continue;
  139. // test for different priorities
  140. if( area_to_test->GetPriority() != area2->GetPriority() )
  141. continue;
  142. // test for different types
  143. if( area_to_test->GetIsKeepout() != area2->GetIsKeepout() )
  144. continue;
  145. if( TestAreaIntersection( area_to_test, area2 ) )
  146. return true;
  147. }
  148. return false;
  149. }
  150. bool BOARD::TestAreaIntersection( ZONE_CONTAINER* area_ref, ZONE_CONTAINER* area_to_test )
  151. {
  152. // see if areas are on same layer
  153. if( area_ref->GetLayer() != area_to_test->GetLayer() )
  154. return false;
  155. SHAPE_POLY_SET* poly1 = area_ref->Outline();
  156. SHAPE_POLY_SET* poly2 = area_to_test->Outline();
  157. // test bounding rects
  158. BOX2I b1 = poly1->BBox();
  159. BOX2I b2 = poly2->BBox();
  160. if( ! b1.Intersects( b2 ) )
  161. return false;
  162. // Now test for intersecting segments
  163. for( auto segIterator1 = poly1->IterateSegmentsWithHoles(); segIterator1; segIterator1++ )
  164. {
  165. // Build segment
  166. SEG firstSegment = *segIterator1;
  167. for( auto segIterator2 = poly2->IterateSegmentsWithHoles(); segIterator2; segIterator2++ )
  168. {
  169. // Build second segment
  170. SEG secondSegment = *segIterator2;
  171. // Check whether the two segments built collide
  172. if( firstSegment.Collide( secondSegment, 0 ) )
  173. return true;
  174. }
  175. }
  176. // If a contour is inside another contour, no segments intersects, but the zones
  177. // can be combined if a corner is inside an outline (only one corner is enough)
  178. for( auto iter = poly2->IterateWithHoles(); iter; iter++ )
  179. {
  180. if( poly1->Contains( *iter ) )
  181. return true;
  182. }
  183. for( auto iter = poly1->IterateWithHoles(); iter; iter++ )
  184. {
  185. if( poly2->Contains( *iter ) )
  186. return true;
  187. }
  188. return false;
  189. }
  190. bool BOARD::CombineAreas( PICKED_ITEMS_LIST* aDeletedList, ZONE_CONTAINER* area_ref,
  191. ZONE_CONTAINER* area_to_combine )
  192. {
  193. if( area_ref == area_to_combine )
  194. {
  195. wxASSERT( 0 );
  196. return false;
  197. }
  198. SHAPE_POLY_SET mergedOutlines = *area_ref->Outline();
  199. SHAPE_POLY_SET areaToMergePoly = *area_to_combine->Outline();
  200. mergedOutlines.BooleanAdd( areaToMergePoly, SHAPE_POLY_SET::PM_FAST );
  201. mergedOutlines.Simplify( SHAPE_POLY_SET::PM_FAST );
  202. // We should have one polygon with hole
  203. // We can have 2 polygons with hole, if the 2 initial polygons have only one common corner
  204. // and therefore cannot be merged (they are dectected as intersecting)
  205. // but we should never have more than 2 polys
  206. if( mergedOutlines.OutlineCount() > 2 )
  207. {
  208. wxLogMessage(wxT("BOARD::CombineAreas error: more than 2 polys after merging") );
  209. return false;
  210. }
  211. if( mergedOutlines.OutlineCount() > 1 )
  212. return false;
  213. // Update the area with the new merged outline
  214. delete area_ref->Outline();
  215. area_ref->SetOutline( new SHAPE_POLY_SET( mergedOutlines ) );
  216. RemoveArea( aDeletedList, area_to_combine );
  217. area_ref->SetLocalFlags( 1 );
  218. area_ref->Hatch();
  219. return true;
  220. }