KiCad PCB EDA Suite
drc_test_provider_via_diameter.cpp
Go to the documentation of this file.
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 
24 #include <pcb_track.h>
25 #include <drc/drc_engine.h>
26 #include <drc/drc_item.h>
27 #include <drc/drc_rule.h>
28 #include <drc/drc_test_provider.h>
29 
30 /*
31  Via diameter test.
32 
33  Errors generated:
34  - DRCE_VIA_DIAMETER
35 */
36 
38 {
39 public:
41  {
42  }
43 
45  {
46  }
47 
48  virtual bool Run() override;
49 
50  virtual const wxString GetName() const override
51  {
52  return "diameter";
53  };
54 
55  virtual const wxString GetDescription() const override
56  {
57  return "Tests via diameters";
58  }
59 
60  virtual std::set<DRC_CONSTRAINT_T> GetConstraintTypes() const override;
61 
62  int GetNumPhases() const override;
63 };
64 
65 
67 {
68  const int delta = 100; // This is the number of tests between 2 calls to the progress bar
69 
71  {
72  reportAux( "Via diameter violations ignored. Tests not run." );
73  return true; // continue with other tests
74  }
75 
77  {
78  reportAux( "No via diameter constraints found. Tests not run." );
79  return true; // continue with other tests
80  }
81 
82  if( !reportPhase( _( "Checking via diameters..." ) ) )
83  return false; // DRC cancelled
84 
85  auto checkViaDiameter =
86  [&]( BOARD_ITEM* item ) -> bool
87  {
89  return false;
90 
91  PCB_VIA* via = dyn_cast<PCB_VIA*>( item );
92 
93  // fixme: move to pad stack check?
94  if( !via )
95  return true;
96 
97  // TODO: once we have padstacks this will need to run per-layer...
98  auto constraint = m_drcEngine->EvalRules( VIA_DIAMETER_CONSTRAINT, item, nullptr,
100  bool fail_min = false;
101  bool fail_max = false;
102  int constraintDiameter = 0;
103  int actual = via->GetWidth();
104 
105  if( constraint.Value().HasMin() && actual < constraint.Value().Min() )
106  {
107  fail_min = true;
108  constraintDiameter = constraint.Value().Min();
109  }
110 
111  if( constraint.Value().HasMax() && actual > constraint.Value().Max() )
112  {
113  fail_max = true;
114  constraintDiameter = constraint.Value().Max();
115  }
116 
117  if( fail_min )
118  {
119  m_msg.Printf( _( "(%s min diameter %s; actual %s)" ),
120  constraint.GetName(),
121  MessageTextFromValue( userUnits(), constraintDiameter ),
122  MessageTextFromValue( userUnits(), actual ) );
123  }
124  else if( fail_max )
125  {
126  m_msg.Printf( _( "(%s max diameter %s; actual %s)" ),
127  constraint.GetName(),
128  MessageTextFromValue( userUnits(), constraintDiameter ),
129  MessageTextFromValue( userUnits(), actual ) );
130  }
131 
132  if( fail_min || fail_max )
133  {
134  std::shared_ptr<DRC_ITEM> drcItem = DRC_ITEM::Create( DRCE_VIA_DIAMETER );
135 
136  drcItem->SetErrorMessage( drcItem->GetErrorText() + wxS( " " ) + m_msg );
137  drcItem->SetItems( item );
138  drcItem->SetViolatingRule( constraint.GetParentRule() );
139 
140  reportViolation( drcItem, via->GetPosition() );
141  }
142 
143  return true;
144  };
145 
146  int ii = 0;
147 
148  for( PCB_TRACK* item : m_drcEngine->GetBoard()->Tracks() )
149  {
150  if( !reportProgress( ii++, m_drcEngine->GetBoard()->Tracks().size(), delta ) )
151  break;
152 
153  if( !checkViaDiameter( item ) )
154  break;
155  }
156 
158 
159  return true;
160 }
161 
162 
164 {
165  return 1;
166 }
167 
168 
169 std::set<DRC_CONSTRAINT_T> DRC_TEST_PROVIDER_VIA_DIAMETER::GetConstraintTypes() const
170 {
171  return { VIA_DIAMETER_CONSTRAINT };
172 }
173 
174 
175 namespace detail
176 {
178 }
wxString MessageTextFromValue(EDA_UNITS aUnits, int aValue, bool aAddUnitLabel, EDA_DATA_TYPE aType)
Convert a value to a string using double notation.
Definition: base_units.cpp:104
static std::shared_ptr< DRC_ITEM > Create(int aErrorCode)
Constructs a DRC_ITEM for the given error code.
Definition: drc_item.cpp:266
A base class for any item which can be embedded within the BOARD container class, and therefore insta...
Definition: board_item.h:49
virtual void reportViolation(std::shared_ptr< DRC_ITEM > &item, const wxPoint &aMarkerPos)
bool IsErrorLimitExceeded(int error_code)
virtual bool reportProgress(int aCount, int aSize, int aDelta)
virtual void reportRuleStatistics()
static DRC_REGISTER_TEST_PROVIDER< DRC_TEST_PROVIDER_ANNULAR_WIDTH > dummy
bool HasRulesForConstraintType(DRC_CONSTRAINT_T constraintID)
BOARD * GetBoard() const
Definition: drc_engine.h:88
virtual bool reportPhase(const wxString &aStageName)
#define _(s)
EDA_UNITS userUnits() const
virtual std::set< DRC_CONSTRAINT_T > GetConstraintTypes() const override
DRC_CONSTRAINT EvalRules(DRC_CONSTRAINT_T aConstraintType, const BOARD_ITEM *a, const BOARD_ITEM *b, PCB_LAYER_ID aLayer, REPORTER *aReporter=nullptr)
Definition: drc_engine.cpp:760
virtual const wxString GetDescription() const override
Represent a DRC "provider" which runs some DRC functions over a BOARD and spits out #DRC_ITEMs and po...
DRC_ENGINE * m_drcEngine
constexpr int delta
virtual const wxString GetName() const override
TRACKS & Tracks()
Definition: board.h:231
virtual void reportAux(wxString fmt,...)
virtual bool Run() override
Run this provider against the given PCB with configured options (if any).