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.

232 lines
7.5 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 <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. const std::shared_ptr<SHAPE_POLY_SET>& zoneFill = aZone->GetFilledPolysList( aLayer );
  70. for( FOOTPRINT* footprint : board->Footprints() )
  71. {
  72. for( PAD* pad : footprint->Pads() )
  73. {
  74. if( m_drcEngine->IsErrorLimitExceeded( DRCE_STARVED_THERMAL ) )
  75. return;
  76. if( m_drcEngine->IsCancelled() )
  77. return;
  78. // Quick tests for "connected":
  79. //
  80. if( !pad->FlashLayer( aLayer ) )
  81. continue;
  82. if( pad->GetNetCode() != aZone->GetNetCode() || pad->GetNetCode() <= 0 )
  83. continue;
  84. EDA_RECT item_boundingbox = pad->GetBoundingBox();
  85. if( !item_boundingbox.Intersects( aZone->GetCachedBoundingBox() ) )
  86. continue;
  87. // If those passed, do a thorough test:
  88. //
  89. constraint = bds.m_DRCEngine->EvalZoneConnection( pad, aZone, aLayer );
  90. ZONE_CONNECTION conn = constraint.m_ZoneConnection;
  91. if( conn != ZONE_CONNECTION::THERMAL )
  92. continue;
  93. constraint = bds.m_DRCEngine->EvalRules( MIN_RESOLVED_SPOKES_CONSTRAINT, pad, aZone,
  94. aLayer );
  95. int minCount = constraint.m_Value.Min();
  96. if( constraint.GetSeverity() == RPT_SEVERITY_IGNORE || minCount <= 0 )
  97. continue;
  98. SHAPE_POLY_SET padPoly;
  99. pad->TransformShapeWithClearanceToPolygon( padPoly, aLayer, 0, ARC_LOW_DEF,
  100. ERROR_OUTSIDE );
  101. SHAPE_LINE_CHAIN& padOutline = padPoly.Outline( 0 );
  102. std::vector<SHAPE_LINE_CHAIN::INTERSECTION> intersections;
  103. int spokes = 0;
  104. for( int jj = 0; jj < zoneFill->OutlineCount(); ++jj )
  105. padOutline.Intersect( zoneFill->Outline( jj ), intersections, true );
  106. spokes += intersections.size() / 2;
  107. if( spokes <= 0 )
  108. continue;
  109. // Now we know we're connected, so see if there are any other manual spokes
  110. // added:
  111. //
  112. for( PCB_TRACK* track : connectivity->GetConnectedTracks( pad ) )
  113. {
  114. if( padOutline.PointInside( track->GetStart() ) )
  115. {
  116. if( aZone->GetFilledPolysList( aLayer )->Collide( track->GetEnd() ) )
  117. spokes++;
  118. }
  119. else if( padOutline.PointInside( track->GetEnd() ) )
  120. {
  121. if( aZone->GetFilledPolysList( aLayer )->Collide( track->GetStart() ) )
  122. spokes++;
  123. }
  124. }
  125. // And finally report it if there aren't enough:
  126. //
  127. if( spokes < minCount )
  128. {
  129. std::shared_ptr<DRC_ITEM> drce = DRC_ITEM::Create( DRCE_STARVED_THERMAL );
  130. wxString msg;
  131. msg.Printf( _( "(%s min spoke count %d; actual %d)" ),
  132. constraint.GetName(),
  133. minCount,
  134. spokes );
  135. drce->SetErrorMessage( drce->GetErrorText() + wxS( " " ) + msg );
  136. drce->SetItems( aZone, pad );
  137. drce->SetViolatingRule( constraint.GetParentRule() );
  138. reportViolation( drce, pad->GetPosition(), UNDEFINED_LAYER );
  139. }
  140. }
  141. }
  142. }
  143. bool DRC_TEST_PROVIDER_ZONE_CONNECTIONS::Run()
  144. {
  145. BOARD* board = m_drcEngine->GetBoard();
  146. std::shared_ptr<CONNECTIVITY_DATA> connectivity = board->GetConnectivity();
  147. DRC_CONSTRAINT constraint;
  148. if( !reportPhase( _( "Checking thermal reliefs..." ) ) )
  149. return false; // DRC cancelled
  150. std::vector< std::pair<ZONE*, PCB_LAYER_ID> > zoneLayers;
  151. for( ZONE* zone : board->m_DRCCopperZones )
  152. {
  153. for( PCB_LAYER_ID layer : zone->GetLayerSet().Seq() )
  154. zoneLayers.push_back( { zone, layer } );
  155. }
  156. thread_pool& tp = GetKiCadThreadPool();
  157. std::vector<std::future<int>> returns;
  158. returns.reserve( zoneLayers.size() );
  159. for( auto& zonelayer : zoneLayers )
  160. {
  161. returns.emplace_back( tp.submit([&]( ZONE* aZone, PCB_LAYER_ID aLayer ) -> int
  162. {
  163. if( !m_drcEngine->IsCancelled() )
  164. {
  165. testZoneLayer( aZone, aLayer );
  166. m_drcEngine->AdvanceProgress();
  167. }
  168. return 0;
  169. }, zonelayer.first, zonelayer.second ) );
  170. }
  171. for( auto& retval : returns )
  172. {
  173. std::future_status status;
  174. do
  175. {
  176. m_drcEngine->KeepRefreshing();
  177. status = retval.wait_for( std::chrono::milliseconds( 100 ) );
  178. } while( status != std::future_status::ready );
  179. }
  180. return !m_drcEngine->IsCancelled();
  181. }
  182. namespace detail
  183. {
  184. static DRC_REGISTER_TEST_PROVIDER<DRC_TEST_PROVIDER_ZONE_CONNECTIONS> dummy;
  185. }