KiCad PCB EDA Suite
Loading...
Searching...
No Matches
drc_test_provider_annular_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-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
24#include <common.h>
25#include <pcb_track.h>
26#include <pad.h>
27#include <footprint.h>
28#include <drc/drc_engine.h>
29#include <drc/drc_item.h>
30#include <drc/drc_rule.h>
32#include <macros.h>
35
36/*
37 Via/pad annular ring width test. Checks if there's sufficient copper ring around
38 PTH/NPTH holes (vias/pads)
39 Errors generated:
40 - DRCE_ANNULAR_WIDTH
41
42 Todo:
43 - check pad holes too.
44 - pad stack support (different IAR/OAR values depending on layer)
45*/
46
48{
49public:
51 {
52 }
53
55 {
56 }
57
58 virtual bool Run() override;
59
60 virtual const wxString GetName() const override
61 {
62 return wxT( "annular_width" );
63 };
64
65 virtual const wxString GetDescription() const override
66 {
67 return wxT( "Tests pad/via annular rings" );
68 }
69};
70
71
73{
75 {
76 reportAux( wxT( "Annular width violations ignored. Skipping check." ) );
77 return true; // continue with other tests
78 }
79
80 const int progressDelta = 500;
81
83 {
84 reportAux( wxT( "No annular width constraints found. Tests not run." ) );
85 return true; // continue with other tests
86 }
87
88 if( !reportPhase( _( "Checking pad & via annular rings..." ) ) )
89 return false; // DRC cancelled
90
92
93 auto calcEffort =
94 []( BOARD_ITEM* item )
95 {
96 switch( item->Type() )
97 {
98 case PCB_VIA_T:
99 return 1;
100
101 case PCB_PAD_T:
102 {
103 PAD* pad = static_cast<PAD*>( item );
104
105 if( !pad->HasHole() || pad->GetAttribute() != PAD_ATTRIB::PTH )
106 return 0;
107
108 if( pad->GetOffset() == VECTOR2I( 0, 0 ) )
109 {
110 switch( pad->GetShape() )
111 {
112 case PAD_SHAPE::CHAMFERED_RECT:
113 if( pad->GetChamferRectRatio() > 0.30 )
114 break;
115
117
118 case PAD_SHAPE::CIRCLE:
119 case PAD_SHAPE::OVAL:
120 case PAD_SHAPE::RECTANGLE:
121 case PAD_SHAPE::ROUNDRECT:
122 return 1;
123
124 default:
125 break;
126 }
127 }
128
129 return 5;
130 }
131
132 default:
133 return 0;
134 }
135 };
136
137 auto checkAnnularWidth =
138 [&]( BOARD_ITEM* item ) -> bool
139 {
141 return false;
142
143 int annularWidth = 0;
144
145 switch( item->Type() )
146 {
147 case PCB_VIA_T:
148 {
149 PCB_VIA* via = static_cast<PCB_VIA*>( item );
150 annularWidth = ( via->GetWidth() - via->GetDrillValue() ) / 2;
151 break;
152 }
153
154 case PCB_PAD_T:
155 {
156 PAD* pad = static_cast<PAD*>( item );
157 bool handled = false;
158
159 if( !pad->HasHole() || pad->GetAttribute() != PAD_ATTRIB::PTH )
160 return true;
161
162 if( pad->GetOffset() == VECTOR2I( 0, 0 ) )
163 {
164 switch( pad->GetShape() )
165 {
166 case PAD_SHAPE::CHAMFERED_RECT:
167 if( pad->GetChamferRectRatio() > 0.30 )
168 break;
169
171
172 case PAD_SHAPE::CIRCLE:
173 case PAD_SHAPE::OVAL:
174 case PAD_SHAPE::RECTANGLE:
175 case PAD_SHAPE::ROUNDRECT:
176 annularWidth = std::min( pad->GetSizeX() - pad->GetDrillSizeX(),
177 pad->GetSizeY() - pad->GetDrillSizeY() ) / 2;
178 handled = true;
179 break;
180
181 default:
182 break;
183 }
184 }
185
186 if( !handled )
187 {
188 // Slow (but general purpose) method.
189 SHAPE_POLY_SET padOutline;
190
191 pad->TransformShapeToPolygon( padOutline, UNDEFINED_LAYER, 0, maxError,
192 ERROR_INSIDE );
193
194 if( !padOutline.Collide( pad->GetPosition() ) )
195 {
196 // Hole outside pad
197 annularWidth = 0;
198 }
199 else
200 {
201 std::shared_ptr<SHAPE_SEGMENT> slot = pad->GetEffectiveHoleShape();
202
203 // Disable is-inside test in SquaredDistance
204 padOutline.Outline( 0 ).SetClosed( false );
205
206 SEG::ecoord dist_sq = padOutline.SquaredDistanceToSeg( slot->GetSeg() );
207 annularWidth = sqrt( dist_sq ) - slot->GetWidth() / 2;
208 }
209 }
210
211 break;
212 }
213
214 default:
215 return true;
216 }
217
218 // PADSTACKS TODO: once we have padstacks we'll need to run this per-layer....
219 auto constraint = m_drcEngine->EvalRules( ANNULAR_WIDTH_CONSTRAINT, item, nullptr,
221 int v_min = 0;
222 int v_max = 0;
223 bool fail_min = false;
224 bool fail_max = false;
225
226 if( constraint.GetSeverity() == RPT_SEVERITY_IGNORE )
227 return true;
228
229 if( constraint.Value().HasMin() )
230 {
231 v_min = constraint.Value().Min();
232 fail_min = annularWidth < v_min;
233 }
234
235 if( constraint.Value().HasMax() )
236 {
237 v_max = constraint.Value().Max();
238 fail_max = annularWidth > v_max;
239 }
240
241 if( fail_min || fail_max )
242 {
243 std::shared_ptr<DRC_ITEM> drcItem = DRC_ITEM::Create( DRCE_ANNULAR_WIDTH );
244 wxString msg;
245
246 if( fail_min )
247 {
248 msg = formatMsg( _( "(%s min annular width %s; actual %s)" ),
249 constraint.GetName(),
250 v_min,
251 annularWidth );
252 }
253
254 if( fail_max )
255 {
256 msg = formatMsg( _( "(%s max annular width %s; actual %s)" ),
257 constraint.GetName(),
258 v_max,
259 annularWidth );
260 }
261
262 drcItem->SetErrorMessage( drcItem->GetErrorText() + wxS( " " ) + msg );
263 drcItem->SetItems( item );
264 drcItem->SetViolatingRule( constraint.GetParentRule() );
265
266 reportViolation( drcItem, item->GetPosition(), item->GetLayer() );
267 }
268
269 return true;
270 };
271
272 BOARD* board = m_drcEngine->GetBoard();
273 size_t ii = 0;
274 size_t total = 0;
275
276 for( PCB_TRACK* item : board->Tracks() )
277 total += calcEffort( item );
278
279 for( FOOTPRINT* footprint : board->Footprints() )
280 {
281 for( PAD* pad : footprint->Pads() )
282 total += calcEffort( pad );
283 }
284
285 for( PCB_TRACK* item : board->Tracks() )
286 {
287 ii += calcEffort( item );
288
289 if( !reportProgress( ii, total, progressDelta ) )
290 return false; // DRC cancelled
291
292 if( !checkAnnularWidth( item ) )
293 break;
294 }
295
296 for( FOOTPRINT* footprint : board->Footprints() )
297 {
298 for( PAD* pad : footprint->Pads() )
299 {
300 ii += calcEffort( pad );
301
302 if( !reportProgress( ii, total, progressDelta ) )
303 return false; // DRC cancelled
304
305 if( !checkAnnularWidth( pad ) )
306 break;
307 }
308 }
309
311
312 return !m_drcEngine->IsCancelled();
313}
314
315
316namespace detail
317{
319}
A base class for any item which can be embedded within the BOARD container class, and therefore insta...
Definition: board_item.h:77
Information pertinent to a Pcbnew printed circuit board.
Definition: board.h:281
const FOOTPRINTS & Footprints() const
Definition: board.h:322
const TRACKS & Tracks() const
Definition: board.h:320
BOARD_DESIGN_SETTINGS & GetDesignSettings() const
Definition: board.cpp:797
MINOPTMAX< int > & Value()
Definition: drc_rule.h:142
BOARD * GetBoard() const
Definition: drc_engine.h:89
bool HasRulesForConstraintType(DRC_CONSTRAINT_T constraintID)
bool IsErrorLimitExceeded(int error_code)
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:675
bool IsCancelled() const
static std::shared_ptr< DRC_ITEM > Create(int aErrorCode)
Constructs a DRC_ITEM for the given error code.
Definition: drc_item.cpp:331
virtual bool Run() override
Run this provider against the given PCB with configured options (if any).
virtual const wxString GetDescription() const override
virtual const wxString GetName() const override
Represent a DRC "provider" which runs some DRC functions over a BOARD and spits out DRC_ITEM and posi...
wxString formatMsg(const wxString &aFormatString, const wxString &aSource, double aConstraint, double aActual)
virtual bool reportPhase(const wxString &aStageName)
virtual void reportViolation(std::shared_ptr< DRC_ITEM > &item, const VECTOR2I &aMarkerPos, int aMarkerLayer)
DRC_ENGINE * m_drcEngine
void reportAux(const wxString &aMsg)
virtual void reportRuleStatistics()
virtual bool reportProgress(size_t aCount, size_t aSize, size_t aDelta=1)
T Min() const
Definition: minoptmax.h:33
Definition: pad.h:59
VECTOR2I::extended_type ecoord
Definition: seg.h:44
void SetClosed(bool aClosed)
Mark the line chain as closed (i.e.
Represent a set of closed polygons.
bool Collide(const SHAPE *aShape, int aClearance=0, int *aActual=nullptr, VECTOR2I *aLocation=nullptr) const override
Check if the boundary of shape (this) lies closer to the shape aShape than aClearance,...
SHAPE_LINE_CHAIN & Outline(int aIndex)
Return the reference to aIndex-th outline in the set.
SEG::ecoord SquaredDistanceToSeg(const SEG &aSegment, VECTOR2I *aNearest=nullptr) const
Compute the minimum distance squared between aSegment and all the polygons in the set.
The common library.
@ DRCE_ANNULAR_WIDTH
Definition: drc_item.h:55
@ ANNULAR_WIDTH_CONSTRAINT
Definition: drc_rule.h:57
#define _(s)
@ ERROR_INSIDE
@ UNDEFINED_LAYER
Definition: layer_ids.h:61
This file contains miscellaneous commonly used macros and functions.
#define KI_FALLTHROUGH
The KI_FALLTHROUGH macro is to be used when switch statement cases should purposely fallthrough from ...
Definition: macros.h:83
static DRC_REGISTER_TEST_PROVIDER< DRC_TEST_PROVIDER_ANNULAR_WIDTH > dummy
@ RPT_SEVERITY_IGNORE
@ PCB_VIA_T
class PCB_VIA, a via (like a track segment on a copper layer)
Definition: typeinfo.h:97
@ PCB_PAD_T
class PAD, a pad in a footprint
Definition: typeinfo.h:87
VECTOR2< int > VECTOR2I
Definition: vector2d.h:588