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.

189 lines
5.4 KiB

  1. /*
  2. * This program source code file is part of KiCad, a free EDA CAD application.
  3. *
  4. * Copyright (C) 2004-2020 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 <common.h>
  25. #include <connectivity/connectivity_data.h>
  26. #include <connectivity/connectivity_algo.h>
  27. #include <drc/drc_item.h>
  28. #include <drc/drc_rule.h>
  29. #include <drc/drc_test_provider.h>
  30. /*
  31. Connectivity test provider. Not rule-driven.
  32. Errors generated:
  33. - DRCE_DANGLING_TRACK
  34. - DRCE_DANGLING_VIA
  35. - DRCE_ZONE_HAS_EMPTY_NET
  36. */
  37. class DRC_TEST_PROVIDER_CONNECTIVITY : public DRC_TEST_PROVIDER
  38. {
  39. public:
  40. DRC_TEST_PROVIDER_CONNECTIVITY()
  41. {
  42. }
  43. virtual ~DRC_TEST_PROVIDER_CONNECTIVITY()
  44. {
  45. }
  46. virtual bool Run() override;
  47. virtual const wxString GetName() const override
  48. {
  49. return "connectivity";
  50. };
  51. virtual const wxString GetDescription() const override
  52. {
  53. return "Tests board connectivity";
  54. }
  55. virtual std::set<DRC_CONSTRAINT_TYPE_T> GetConstraintTypes() const override;
  56. int GetNumPhases() const override;
  57. };
  58. bool DRC_TEST_PROVIDER_CONNECTIVITY::Run()
  59. {
  60. if( !reportPhase( _( "Checking pad, via and zone connections..." ) ) )
  61. return false;
  62. BOARD* board = m_drcEngine->GetBoard();
  63. std::shared_ptr<CONNECTIVITY_DATA> connectivity = board->GetConnectivity();
  64. // Rebuild just in case. This really needs to be reliable.
  65. connectivity->Clear();
  66. connectivity->Build( board, m_drcEngine->GetProgressReporter() );
  67. int delta = 100; // This is the number of tests between 2 calls to the progress bar
  68. int ii = 0;
  69. int count = board->Tracks().size() + board->Zones().size();
  70. ii += count; // We gave half of this phase to CONNECTIVITY_DATA::Build()
  71. count += count;
  72. for( TRACK* track : board->Tracks() )
  73. {
  74. bool exceedT = m_drcEngine->IsErrorLimitExceeded( DRCE_DANGLING_TRACK );
  75. bool exceedV = m_drcEngine->IsErrorLimitExceeded( DRCE_DANGLING_VIA );
  76. if( exceedV && exceedT )
  77. break;
  78. else if( track->Type() == PCB_VIA_T && exceedV )
  79. continue;
  80. else if( track->Type() == PCB_TRACE_T && exceedT )
  81. continue;
  82. if( !reportProgress( ii++, count, delta ) )
  83. break;
  84. // Test for dangling items
  85. int code = track->Type() == PCB_VIA_T ? DRCE_DANGLING_VIA : DRCE_DANGLING_TRACK;
  86. wxPoint pos;
  87. if( connectivity->TestTrackEndpointDangling( track, &pos ) )
  88. {
  89. std::shared_ptr<DRC_ITEM> drcItem = DRC_ITEM::Create( code );
  90. drcItem->SetItems( track );
  91. reportViolation( drcItem, pos );
  92. }
  93. }
  94. /* test starved zones */
  95. for( ZONE* zone : board->Zones() )
  96. {
  97. if( m_drcEngine->IsErrorLimitExceeded( DRCE_ZONE_HAS_EMPTY_NET ) )
  98. break;
  99. if( !zone->IsOnCopperLayer() )
  100. continue;
  101. if( !reportProgress( ii++, count, delta ) )
  102. break;
  103. int netcode = zone->GetNetCode();
  104. // a netcode < 0 or > 0 and no pad in net is a error or strange
  105. // perhaps a "dead" net, which happens when all pads in this net were removed
  106. // Remark: a netcode < 0 should not happen (this is more a bug somewhere)
  107. int pads_in_net = ( netcode > 0 ) ? connectivity->GetPadCount( netcode ) : 1;
  108. if( ( netcode < 0 ) || pads_in_net == 0 )
  109. {
  110. std::shared_ptr<DRC_ITEM> drcItem = DRC_ITEM::Create( DRCE_ZONE_HAS_EMPTY_NET );
  111. drcItem->SetItems( zone );
  112. reportViolation( drcItem, zone->GetPosition() );
  113. }
  114. }
  115. if( !reportPhase( _( "Checking net connections..." ) ) )
  116. return false;
  117. connectivity->RecalculateRatsnest();
  118. std::vector<CN_EDGE> edges;
  119. connectivity->GetUnconnectedEdges( edges );
  120. delta = 250;
  121. ii = 0;
  122. count = edges.size();
  123. for( const CN_EDGE& edge : edges )
  124. {
  125. if( m_drcEngine->IsErrorLimitExceeded( DRCE_UNCONNECTED_ITEMS ) )
  126. break;
  127. if( !reportProgress( ii++, count, delta ) )
  128. break;
  129. std::shared_ptr<DRC_ITEM> drcItem = DRC_ITEM::Create( DRCE_UNCONNECTED_ITEMS );
  130. drcItem->SetItems( edge.GetSourceNode()->Parent(), edge.GetTargetNode()->Parent() );
  131. reportViolation( drcItem, (wxPoint) edge.GetSourceNode()->Pos() );
  132. }
  133. reportRuleStatistics();
  134. return true;
  135. }
  136. int DRC_TEST_PROVIDER_CONNECTIVITY::GetNumPhases() const
  137. {
  138. return 3;
  139. }
  140. std::set<DRC_CONSTRAINT_TYPE_T> DRC_TEST_PROVIDER_CONNECTIVITY::GetConstraintTypes() const
  141. {
  142. return {};
  143. }
  144. namespace detail
  145. {
  146. static DRC_REGISTER_TEST_PROVIDER<DRC_TEST_PROVIDER_CONNECTIVITY> dummy;
  147. }