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.

290 lines
9.7 KiB

  1. /*
  2. * This program source code file is part of KiCad, a free EDA CAD application.
  3. *
  4. * Copyright (C) 2021-2022 KiCad Developers.
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU General Public License
  8. * as published by the Free Software Foundation; either version 2
  9. * of the License, or (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, you may find one here:
  18. * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
  19. * or you may search the http://www.gnu.org website for the version 2 license,
  20. * or you may write to the Free Software Foundation, Inc.,
  21. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
  22. */
  23. #include <board.h>
  24. #include <board_design_settings.h>
  25. #include <connectivity/connectivity_data.h>
  26. #include <zone.h>
  27. #include <footprint.h>
  28. #include <pad.h>
  29. #include <pcb_track.h>
  30. #include <core/thread_pool.h>
  31. #include <geometry/shape_line_chain.h>
  32. #include <geometry/shape_poly_set.h>
  33. #include <drc/drc_rule.h>
  34. #include <drc/drc_item.h>
  35. #include <drc/drc_test_provider.h>
  36. /*
  37. This loads some rule resolvers for the ZONE_FILLER, and checks that pad thermal relief
  38. connections have at least the required number of spokes.
  39. Errors generated:
  40. - DRCE_STARVED_THERMAL
  41. */
  42. class DRC_TEST_PROVIDER_ZONE_CONNECTIONS : public DRC_TEST_PROVIDER
  43. {
  44. public:
  45. DRC_TEST_PROVIDER_ZONE_CONNECTIONS()
  46. {
  47. }
  48. virtual ~DRC_TEST_PROVIDER_ZONE_CONNECTIONS()
  49. {
  50. }
  51. virtual bool Run() override;
  52. virtual const wxString GetName() const override
  53. {
  54. return wxT( "zone connections" );
  55. };
  56. virtual const wxString GetDescription() const override
  57. {
  58. return wxT( "Checks thermal reliefs for a sufficient number of connecting spokes" );
  59. }
  60. private:
  61. void testZoneLayer( ZONE* aZone, PCB_LAYER_ID aLayer );
  62. };
  63. void DRC_TEST_PROVIDER_ZONE_CONNECTIONS::testZoneLayer( ZONE* aZone, PCB_LAYER_ID aLayer )
  64. {
  65. BOARD* board = m_drcEngine->GetBoard();
  66. BOARD_DESIGN_SETTINGS& bds = board->GetDesignSettings();
  67. std::shared_ptr<CONNECTIVITY_DATA> connectivity = board->GetConnectivity();
  68. DRC_CONSTRAINT constraint;
  69. wxString msg;
  70. const std::shared_ptr<SHAPE_POLY_SET>& zoneFill = aZone->GetFilledPolysList( aLayer );
  71. ISOLATED_ISLANDS isolatedIslands;
  72. auto zoneIter = board->m_ZoneIsolatedIslandsMap.find( aZone );
  73. if( zoneIter != board->m_ZoneIsolatedIslandsMap.end() )
  74. {
  75. auto layerIter = zoneIter->second.find( aLayer );
  76. if( layerIter != zoneIter->second.end() )
  77. isolatedIslands = layerIter->second;
  78. }
  79. for( FOOTPRINT* footprint : board->Footprints() )
  80. {
  81. for( PAD* pad : footprint->Pads() )
  82. {
  83. if( m_drcEngine->IsErrorLimitExceeded( DRCE_STARVED_THERMAL ) )
  84. return;
  85. if( m_drcEngine->IsCancelled() )
  86. return;
  87. //
  88. // Quick tests for "connected":
  89. //
  90. if( pad->GetNetCode() != aZone->GetNetCode() || pad->GetNetCode() <= 0 )
  91. continue;
  92. BOX2I item_bbox = pad->GetBoundingBox();
  93. if( !item_bbox.Intersects( aZone->GetBoundingBox() ) )
  94. continue;
  95. if( !pad->FlashLayer( aLayer ) )
  96. continue;
  97. //
  98. // If those passed, do a thorough test:
  99. //
  100. constraint = bds.m_DRCEngine->EvalZoneConnection( pad, aZone, aLayer );
  101. ZONE_CONNECTION conn = constraint.m_ZoneConnection;
  102. if( conn != ZONE_CONNECTION::THERMAL )
  103. continue;
  104. constraint = bds.m_DRCEngine->EvalRules( MIN_RESOLVED_SPOKES_CONSTRAINT, pad, aZone,
  105. aLayer );
  106. int minCount = constraint.m_Value.Min();
  107. if( constraint.GetSeverity() == RPT_SEVERITY_IGNORE || minCount <= 0 )
  108. continue;
  109. constraint = bds.m_DRCEngine->EvalRules( THERMAL_RELIEF_GAP_CONSTRAINT, pad, aZone,
  110. aLayer );
  111. int mid_gap = constraint.m_Value.Min() / 2;
  112. SHAPE_POLY_SET padPoly;
  113. pad->TransformShapeToPolygon( padPoly, aLayer, mid_gap, ARC_LOW_DEF, ERROR_OUTSIDE );
  114. SHAPE_LINE_CHAIN& padOutline = padPoly.Outline( 0 );
  115. BOX2I padBBox( item_bbox );
  116. int spokes = 0;
  117. int ignoredSpokes = 0;
  118. for( int jj = 0; jj < zoneFill->OutlineCount(); ++jj )
  119. {
  120. std::vector<SHAPE_LINE_CHAIN::INTERSECTION> intersections;
  121. zoneFill->Outline( jj ).Intersect( padOutline, intersections, true, &padBBox );
  122. // If we connect to an island that only connects to a single item then we *are*
  123. // that item. Thermal spokes to this (otherwise isolated) island don't provide
  124. // electrical connectivity to anything, so we don't count them.
  125. if( alg::contains( isolatedIslands.m_SingleConnectionOutlines, jj ) )
  126. ignoredSpokes += (int) intersections.size() / 2;
  127. else
  128. spokes += (int) intersections.size() / 2;
  129. }
  130. if( spokes == 0 && ignoredSpokes == 0 ) // Not connected at all
  131. continue;
  132. if( spokes >= minCount ) // We already have enough
  133. continue;
  134. //
  135. // See if there are any other manual spokes added:
  136. //
  137. for( PCB_TRACK* track : connectivity->GetConnectedTracks( pad ) )
  138. {
  139. if( padOutline.PointInside( track->GetStart() ) )
  140. {
  141. if( aZone->GetFilledPolysList( aLayer )->Collide( track->GetEnd() ) )
  142. spokes++;
  143. }
  144. else if( padOutline.PointInside( track->GetEnd() ) )
  145. {
  146. if( aZone->GetFilledPolysList( aLayer )->Collide( track->GetStart() ) )
  147. spokes++;
  148. }
  149. }
  150. //
  151. // And finally report it if there aren't enough:
  152. //
  153. if( spokes < minCount )
  154. {
  155. std::shared_ptr<DRC_ITEM> drce = DRC_ITEM::Create( DRCE_STARVED_THERMAL );
  156. if( ignoredSpokes )
  157. {
  158. msg = wxString::Format( _( "(layer %s; %d spokes connected to isolated island)" ),
  159. board->GetLayerName( aLayer ),
  160. ignoredSpokes );
  161. }
  162. else
  163. {
  164. msg = wxString::Format( _( "(layer %s; %s min spoke count %d; actual %d)" ),
  165. board->GetLayerName( aLayer ),
  166. constraint.GetName(),
  167. minCount,
  168. spokes );
  169. }
  170. drce->SetErrorMessage( drce->GetErrorText() + wxS( " " ) + msg );
  171. drce->SetItems( aZone, pad );
  172. drce->SetViolatingRule( constraint.GetParentRule() );
  173. reportViolation( drce, pad->GetPosition(), aLayer );
  174. }
  175. }
  176. }
  177. }
  178. bool DRC_TEST_PROVIDER_ZONE_CONNECTIONS::Run()
  179. {
  180. BOARD* board = m_drcEngine->GetBoard();
  181. std::shared_ptr<CONNECTIVITY_DATA> connectivity = board->GetConnectivity();
  182. DRC_CONSTRAINT constraint;
  183. if( !reportPhase( _( "Checking thermal reliefs..." ) ) )
  184. return false; // DRC cancelled
  185. std::vector< std::pair<ZONE*, PCB_LAYER_ID> > zoneLayers;
  186. std::atomic<size_t> done( 1 );
  187. size_t total_effort = 0;
  188. for( ZONE* zone : board->m_DRCCopperZones )
  189. {
  190. if( !zone->IsTeardropArea() )
  191. {
  192. for( PCB_LAYER_ID layer : zone->GetLayerSet().Seq() )
  193. {
  194. zoneLayers.push_back( { zone, layer } );
  195. total_effort += zone->GetFilledPolysList( layer )->FullPointCount();
  196. }
  197. }
  198. }
  199. total_effort = std::max( (size_t) 1, total_effort );
  200. thread_pool& tp = GetKiCadThreadPool();
  201. std::vector<std::future<int>> returns;
  202. returns.reserve( zoneLayers.size() );
  203. for( const std::pair<ZONE*, PCB_LAYER_ID>& zonelayer : zoneLayers )
  204. {
  205. returns.emplace_back( tp.submit(
  206. [&]( ZONE* aZone, PCB_LAYER_ID aLayer ) -> int
  207. {
  208. if( !m_drcEngine->IsCancelled() )
  209. {
  210. testZoneLayer( aZone, aLayer );
  211. done.fetch_add( aZone->GetFilledPolysList( aLayer )->FullPointCount() );
  212. }
  213. return 0;
  214. },
  215. zonelayer.first, zonelayer.second ) );
  216. }
  217. for( const std::future<int>& ret : returns )
  218. {
  219. std::future_status status = ret.wait_for( std::chrono::milliseconds( 250 ) );
  220. while( status != std::future_status::ready )
  221. {
  222. m_drcEngine->ReportProgress( static_cast<double>( done ) / total_effort );
  223. status = ret.wait_for( std::chrono::milliseconds( 250 ) );
  224. }
  225. }
  226. return !m_drcEngine->IsCancelled();
  227. }
  228. namespace detail
  229. {
  230. static DRC_REGISTER_TEST_PROVIDER<DRC_TEST_PROVIDER_ZONE_CONNECTIONS> dummy;
  231. }