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.

236 lines
7.8 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 <atomic>
  24. #include <thread>
  25. #include <board.h>
  26. #include <board_design_settings.h>
  27. #include <connectivity/connectivity_data.h>
  28. #include <zone.h>
  29. #include <footprint.h>
  30. #include <pad.h>
  31. #include <pcb_track.h>
  32. #include <geometry/shape_line_chain.h>
  33. #include <geometry/shape_poly_set.h>
  34. #include <drc/drc_rule.h>
  35. #include <drc/drc_item.h>
  36. #include <drc/drc_test_provider.h>
  37. /*
  38. This loads some rule resolvers for the ZONE_FILLER, and checks that pad thermal relief
  39. connections have at least the required number of spokes.
  40. Errors generated:
  41. - DRCE_STARVED_THERMAL
  42. */
  43. class DRC_TEST_PROVIDER_ZONE_CONNECTIONS : public DRC_TEST_PROVIDER
  44. {
  45. public:
  46. DRC_TEST_PROVIDER_ZONE_CONNECTIONS()
  47. {
  48. }
  49. virtual ~DRC_TEST_PROVIDER_ZONE_CONNECTIONS()
  50. {
  51. }
  52. virtual bool Run() override;
  53. virtual const wxString GetName() const override
  54. {
  55. return wxT( "zone connections" );
  56. };
  57. virtual const wxString GetDescription() const override
  58. {
  59. return wxT( "Checks thermal reliefs for a sufficient number of connecting spokes" );
  60. }
  61. private:
  62. void testZoneLayer( ZONE* aZone, PCB_LAYER_ID aLayer );
  63. };
  64. void DRC_TEST_PROVIDER_ZONE_CONNECTIONS::testZoneLayer( ZONE* aZone, PCB_LAYER_ID aLayer )
  65. {
  66. BOARD* board = m_drcEngine->GetBoard();
  67. BOARD_DESIGN_SETTINGS& bds = board->GetDesignSettings();
  68. std::shared_ptr<CONNECTIVITY_DATA> connectivity = board->GetConnectivity();
  69. DRC_CONSTRAINT constraint;
  70. const std::shared_ptr<SHAPE_POLY_SET>& zoneFill = aZone->GetFilledPolysList( aLayer );
  71. for( FOOTPRINT* footprint : board->Footprints() )
  72. {
  73. for( PAD* pad : footprint->Pads() )
  74. {
  75. if( m_drcEngine->IsErrorLimitExceeded( DRCE_STARVED_THERMAL ) )
  76. return;
  77. if( m_drcEngine->IsCancelled() )
  78. return;
  79. // Quick tests for "connected":
  80. //
  81. if( !pad->FlashLayer( aLayer ) )
  82. continue;
  83. if( pad->GetNetCode() != aZone->GetNetCode() || pad->GetNetCode() <= 0 )
  84. continue;
  85. EDA_RECT item_boundingbox = pad->GetBoundingBox();
  86. if( !item_boundingbox.Intersects( aZone->GetCachedBoundingBox() ) )
  87. continue;
  88. // If those passed, do a thorough test:
  89. //
  90. constraint = bds.m_DRCEngine->EvalZoneConnection( pad, aZone, aLayer );
  91. ZONE_CONNECTION conn = constraint.m_ZoneConnection;
  92. if( conn != ZONE_CONNECTION::THERMAL )
  93. continue;
  94. constraint = bds.m_DRCEngine->EvalRules( MIN_RESOLVED_SPOKES_CONSTRAINT, pad, aZone,
  95. aLayer );
  96. int minCount = constraint.m_Value.Min();
  97. if( constraint.GetSeverity() == RPT_SEVERITY_IGNORE || minCount <= 0 )
  98. continue;
  99. SHAPE_POLY_SET padPoly;
  100. pad->TransformShapeWithClearanceToPolygon( padPoly, aLayer, 0, ARC_LOW_DEF,
  101. ERROR_OUTSIDE );
  102. SHAPE_LINE_CHAIN& padOutline = padPoly.Outline( 0 );
  103. std::vector<SHAPE_LINE_CHAIN::INTERSECTION> intersections;
  104. int spokes = 0;
  105. for( int jj = 0; jj < zoneFill->OutlineCount(); ++jj )
  106. padOutline.Intersect( zoneFill->Outline( jj ), intersections, true );
  107. spokes += intersections.size() / 2;
  108. if( spokes <= 0 )
  109. continue;
  110. // Now we know we're connected, so see if there are any other manual spokes
  111. // added:
  112. //
  113. for( PCB_TRACK* track : connectivity->GetConnectedTracks( pad ) )
  114. {
  115. if( padOutline.PointInside( track->GetStart() ) )
  116. {
  117. if( aZone->GetFilledPolysList( aLayer )->Collide( track->GetEnd() ) )
  118. spokes++;
  119. }
  120. else if( padOutline.PointInside( track->GetEnd() ) )
  121. {
  122. if( aZone->GetFilledPolysList( aLayer )->Collide( track->GetStart() ) )
  123. spokes++;
  124. }
  125. }
  126. // And finally report it if there aren't enough:
  127. //
  128. if( spokes < minCount )
  129. {
  130. std::shared_ptr<DRC_ITEM> drce = DRC_ITEM::Create( DRCE_STARVED_THERMAL );
  131. wxString msg;
  132. msg.Printf( _( "(%s min spoke count %d; actual %d)" ),
  133. constraint.GetName(),
  134. minCount,
  135. spokes );
  136. drce->SetErrorMessage( drce->GetErrorText() + wxS( " " ) + msg );
  137. drce->SetItems( aZone, pad );
  138. drce->SetViolatingRule( constraint.GetParentRule() );
  139. reportViolation( drce, pad->GetPosition(), UNDEFINED_LAYER );
  140. }
  141. }
  142. }
  143. }
  144. bool DRC_TEST_PROVIDER_ZONE_CONNECTIONS::Run()
  145. {
  146. BOARD* board = m_drcEngine->GetBoard();
  147. BOARD_DESIGN_SETTINGS& bds = board->GetDesignSettings();
  148. std::shared_ptr<CONNECTIVITY_DATA> connectivity = board->GetConnectivity();
  149. DRC_CONSTRAINT constraint;
  150. if( !reportPhase( _( "Checking thermal reliefs..." ) ) )
  151. return false; // DRC cancelled
  152. std::vector< std::pair<ZONE*, PCB_LAYER_ID> > zoneLayers;
  153. for( ZONE* zone : board->m_DRCCopperZones )
  154. {
  155. for( PCB_LAYER_ID layer : zone->GetLayerSet().Seq() )
  156. zoneLayers.push_back( { zone, layer } );
  157. }
  158. int zoneLayerCount = zoneLayers.size();
  159. std::atomic<size_t> next( 0 );
  160. std::atomic<size_t> done( 1 );
  161. std::atomic<size_t> threads_finished( 0 );
  162. size_t parallelThreadCount = std::max<size_t>( std::thread::hardware_concurrency(), 2 );
  163. for( size_t ii = 0; ii < parallelThreadCount; ++ii )
  164. {
  165. std::thread t = std::thread(
  166. [&]( )
  167. {
  168. for( int i = next.fetch_add( 1 ); i < zoneLayerCount; i = next.fetch_add( 1 ) )
  169. {
  170. testZoneLayer( zoneLayers[i].first, zoneLayers[i].second );
  171. done.fetch_add( 1 );
  172. if( m_drcEngine->IsCancelled() )
  173. break;
  174. }
  175. threads_finished.fetch_add( 1 );
  176. } );
  177. t.detach();
  178. }
  179. while( threads_finished < parallelThreadCount )
  180. {
  181. m_drcEngine->ReportProgress( (double) done / (double) zoneLayerCount );
  182. std::this_thread::sleep_for( std::chrono::milliseconds( 100 ) );
  183. }
  184. return !m_drcEngine->IsCancelled();
  185. }
  186. namespace detail
  187. {
  188. static DRC_REGISTER_TEST_PROVIDER<DRC_TEST_PROVIDER_ZONE_CONNECTIONS> dummy;
  189. }