KiCad PCB EDA Suite
drc_test_provider_track_width.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 <common.h>
25 #include <pcb_track.h>
26 #include <drc/drc_engine.h>
27 #include <drc/drc_item.h>
28 #include <drc/drc_rule.h>
29 #include <drc/drc_test_provider.h>
30 
31 
32 /*
33  Track width test. As the name says, checks width of the tracks (including segments and arcs)
34  Errors generated:
35  - DRCE_TRACK_WIDTH
36 */
37 
39 {
40 public:
42  {
43  }
44 
46  {
47  }
48 
49  virtual bool Run() override;
50 
51  virtual const wxString GetName() const override
52  {
53  return "width";
54  };
55 
56  virtual const wxString GetDescription() const override
57  {
58  return "Tests track widths";
59  }
60 
61  virtual std::set<DRC_CONSTRAINT_T> GetConstraintTypes() const override;
62 
63  int GetNumPhases() const override;
64 };
65 
66 
68 {
69  const int delta = 100; // This is the number of tests between 2 calls to the progress bar
70 
72  {
73  reportAux( "Track width violations ignored. Tests not run." );
74  return true; // continue with other tests
75  }
76 
78  {
79  reportAux( "No track width constraints found. Tests not run." );
80  return true; // continue with other tests
81  }
82 
83  if( !reportPhase( _( "Checking track widths..." ) ) )
84  return false; // DRC cancelled
85 
86  auto checkTrackWidth =
87  [&]( BOARD_ITEM* item ) -> bool
88  {
90  return false;
91 
92  int actual;
93  wxPoint p0;
94 
95  if( PCB_ARC* arc = dyn_cast<PCB_ARC*>( item ) )
96  {
97  actual = arc->GetWidth();
98  p0 = arc->GetStart();
99  }
100  else if( PCB_TRACK* trk = dyn_cast<PCB_TRACK*>( item ) )
101  {
102  actual = trk->GetWidth();
103  p0 = ( trk->GetStart() + trk->GetEnd() ) / 2;
104  }
105  else
106  {
107  return true;
108  }
109 
110  auto constraint = m_drcEngine->EvalRules( TRACK_WIDTH_CONSTRAINT, item, nullptr,
111  item->GetLayer() );
112  bool fail_min = false;
113  bool fail_max = false;
114  int constraintWidth;
115 
116  if( constraint.Value().HasMin() && actual < constraint.Value().Min() )
117  {
118  fail_min = true;
119  constraintWidth = constraint.Value().Min();
120  }
121 
122  if( constraint.Value().HasMax() && actual > constraint.Value().Max() )
123  {
124  fail_max = true;
125  constraintWidth = constraint.Value().Max();
126  }
127 
128  if( fail_min || fail_max )
129  {
130  std::shared_ptr<DRC_ITEM> drcItem = DRC_ITEM::Create( DRCE_TRACK_WIDTH );
131 
132  if( fail_min )
133  {
134  m_msg.Printf( _( "(%s min width %s; actual %s)" ),
135  constraint.GetName(),
136  MessageTextFromValue( userUnits(), constraintWidth ),
137  MessageTextFromValue( userUnits(), actual ) );
138  }
139  else
140  {
141  m_msg.Printf( _( "(%s max width %s; actual %s)" ),
142  constraint.GetName(),
143  MessageTextFromValue( userUnits(), constraintWidth ),
144  MessageTextFromValue( userUnits(), actual ) );
145  }
146 
147  drcItem->SetErrorMessage( drcItem->GetErrorText() + wxS( " " ) + m_msg );
148  drcItem->SetItems( item );
149  drcItem->SetViolatingRule( constraint.GetParentRule() );
150 
151  reportViolation( drcItem, p0 );
152  }
153 
154  return true;
155  };
156 
157  int ii = 0;
158 
159  for( PCB_TRACK* item : m_drcEngine->GetBoard()->Tracks() )
160  {
161  if( !reportProgress( ii++, m_drcEngine->GetBoard()->Tracks().size(), delta ) )
162  break;
163 
164  if( !checkTrackWidth( item ) )
165  break;
166  }
167 
169 
170  return true;
171 }
172 
173 
175 {
176  return 1;
177 }
178 
179 
180 std::set<DRC_CONSTRAINT_T> DRC_TEST_PROVIDER_TRACK_WIDTH::GetConstraintTypes() const
181 {
182  return { TRACK_WIDTH_CONSTRAINT };
183 }
184 
185 
186 namespace detail
187 {
189 }
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
MINOPTMAX< int > & Value()
Definition: drc_rule.h:123
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)
T Min() const
Definition: minoptmax.h:33
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 std::set< DRC_CONSTRAINT_T > GetConstraintTypes() const override
virtual bool reportPhase(const wxString &aStageName)
#define _(s)
EDA_UNITS userUnits() const
virtual const wxString GetDescription() 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
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 bool Run() override
Run this provider against the given PCB with configured options (if any).
virtual const wxString GetName() const override
TRACKS & Tracks()
Definition: board.h:231
virtual void reportAux(wxString fmt,...)