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.

329 lines
9.9 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 layers
  137. if( area_to_test->GetLayerSet() != area2->GetLayerSet() )
  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. // Keepout area-specific tests
  146. if( area_to_test->GetIsKeepout() )
  147. {
  148. if( area_to_test->GetDoNotAllowCopperPour() != area2->GetDoNotAllowCopperPour() )
  149. continue;
  150. if( area_to_test->GetDoNotAllowTracks() != area2->GetDoNotAllowTracks() )
  151. continue;
  152. if( area_to_test->GetDoNotAllowVias() != area2->GetDoNotAllowVias() )
  153. continue;
  154. }
  155. // Filled zone specific tests
  156. else
  157. {
  158. if( area_to_test->GetClearance() != area2->GetClearance() )
  159. continue;
  160. if( area_to_test->GetThermalReliefGap() != area2->GetThermalReliefGap() )
  161. continue;
  162. if( area_to_test->GetThermalReliefCopperBridge() != area2->GetThermalReliefCopperBridge() )
  163. continue;
  164. if( area_to_test->GetArcSegmentCount() != area2->GetArcSegmentCount() )
  165. continue;
  166. if( area_to_test->GetZoneClearance() != area2->GetZoneClearance() )
  167. continue;
  168. if( area_to_test->GetPadConnection() != area2->GetPadConnection() )
  169. continue;
  170. if( area_to_test->GetMinThickness() != area2->GetMinThickness() )
  171. continue;
  172. if( area_to_test->GetCornerSmoothingType() != area2->GetCornerSmoothingType() )
  173. continue;
  174. if( area_to_test->GetCornerRadius() != area2->GetCornerRadius() )
  175. continue;
  176. }
  177. if( TestAreaIntersection( area_to_test, area2 ) )
  178. return true;
  179. }
  180. return false;
  181. }
  182. bool BOARD::TestAreaIntersection( ZONE_CONTAINER* area_ref, ZONE_CONTAINER* area_to_test )
  183. {
  184. // see if areas are on same layer
  185. if( area_ref->GetLayer() != area_to_test->GetLayer() )
  186. return false;
  187. SHAPE_POLY_SET* poly1 = area_ref->Outline();
  188. SHAPE_POLY_SET* poly2 = area_to_test->Outline();
  189. // test bounding rects
  190. BOX2I b1 = poly1->BBox();
  191. BOX2I b2 = poly2->BBox();
  192. if( ! b1.Intersects( b2 ) )
  193. return false;
  194. // Now test for intersecting segments
  195. for( auto segIterator1 = poly1->IterateSegmentsWithHoles(); segIterator1; segIterator1++ )
  196. {
  197. // Build segment
  198. SEG firstSegment = *segIterator1;
  199. for( auto segIterator2 = poly2->IterateSegmentsWithHoles(); segIterator2; segIterator2++ )
  200. {
  201. // Build second segment
  202. SEG secondSegment = *segIterator2;
  203. // Check whether the two segments built collide
  204. if( firstSegment.Collide( secondSegment, 0 ) )
  205. return true;
  206. }
  207. }
  208. // If a contour is inside another contour, no segments intersects, but the zones
  209. // can be combined if a corner is inside an outline (only one corner is enough)
  210. for( auto iter = poly2->IterateWithHoles(); iter; iter++ )
  211. {
  212. if( poly1->Contains( *iter ) )
  213. return true;
  214. }
  215. for( auto iter = poly1->IterateWithHoles(); iter; iter++ )
  216. {
  217. if( poly2->Contains( *iter ) )
  218. return true;
  219. }
  220. return false;
  221. }
  222. bool BOARD::CombineAreas( PICKED_ITEMS_LIST* aDeletedList, ZONE_CONTAINER* area_ref,
  223. ZONE_CONTAINER* area_to_combine )
  224. {
  225. if( area_ref == area_to_combine )
  226. {
  227. wxASSERT( 0 );
  228. return false;
  229. }
  230. SHAPE_POLY_SET mergedOutlines = *area_ref->Outline();
  231. SHAPE_POLY_SET areaToMergePoly = *area_to_combine->Outline();
  232. mergedOutlines.BooleanAdd( areaToMergePoly, SHAPE_POLY_SET::PM_FAST );
  233. mergedOutlines.Simplify( SHAPE_POLY_SET::PM_FAST );
  234. // We should have one polygon with hole
  235. // We can have 2 polygons with hole, if the 2 initial polygons have only one common corner
  236. // and therefore cannot be merged (they are dectected as intersecting)
  237. // but we should never have more than 2 polys
  238. if( mergedOutlines.OutlineCount() > 2 )
  239. {
  240. wxLogMessage(wxT("BOARD::CombineAreas error: more than 2 polys after merging") );
  241. return false;
  242. }
  243. if( mergedOutlines.OutlineCount() > 1 )
  244. return false;
  245. // Update the area with the new merged outline
  246. delete area_ref->Outline();
  247. area_ref->SetOutline( new SHAPE_POLY_SET( mergedOutlines ) );
  248. RemoveArea( aDeletedList, area_to_combine );
  249. area_ref->SetLocalFlags( 1 );
  250. area_ref->Hatch();
  251. return true;
  252. }