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, 2024 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
60static inline bool collide( const SHAPE_LINE_CHAIN& aLhs, const SHAPE_LINE_CHAIN& aRhs,
61 int aClearance, int* aDistance = nullptr, VECTOR2I* aPt1 = nullptr )
62{
63 wxCHECK( aLhs.PointCount() && aRhs.PointCount(), false );
64
65 VECTOR2I pt1;
66 bool retv = false;
67 int dist = std::numeric_limits<int>::max();
68 int tmp = dist;
69
70 SHAPE_LINE_CHAIN lhs( aLhs );
71 SHAPE_LINE_CHAIN rhs( aRhs );
72
73 lhs.SetClosed( false );
74 lhs.Append( lhs.CPoint( 0 ) );
75 rhs.SetClosed( false );
76 rhs.Append( rhs.CPoint( 0 ) );
77
78 for( int i = 0; i < rhs.SegmentCount(); i ++ )
79 {
80 if( lhs.Collide( rhs.CSegment( i ), tmp, &tmp, &pt1 ) )
81 {
82 retv = true;
83
84 if( tmp < dist )
85 dist = tmp;
86
87 if( aDistance )
88 *aDistance = dist;
89
90 if( aPt1 )
91 *aPt1 = pt1;
92 }
93 }
94
95 return retv;
96}
97
98
99static bool collide( const SHAPE_POLY_SET& aLhs, const SHAPE_LINE_CHAIN& aRhs, int aClearance,
100 int* aDistance = nullptr, VECTOR2I* aPt1 = nullptr )
101{
102 VECTOR2I pt1;
103 bool retv = false;
104 int tmp = std::numeric_limits<int>::max();
105 int dist = tmp;
106
107 for( int i = 0; i < aLhs.OutlineCount(); i++ )
108 {
109 if( collide( aLhs.Outline( i ), aRhs, aClearance, &tmp, &pt1 ) )
110 {
111 retv = true;
112
113 if( tmp < dist )
114 {
115 dist = tmp;
116
117 if( aDistance )
118 *aDistance = dist;
119
120 if( aPt1 )
121 *aPt1 = pt1;
122 }
123 }
124
125 for( int j = 0; j < aLhs.HoleCount( i ); i++ )
126 {
127 if( collide( aLhs.CHole( i, j ), aRhs, aClearance, &tmp, &pt1 ) )
128 {
129 retv = true;
130
131 if( tmp < dist )
132 {
133 dist = tmp;
134
135 if( aDistance )
136 *aDistance = dist;
137
138 if( aPt1 )
139 *aPt1 = pt1;
140 }
141 }
142 }
143 }
144
145 return retv;
146}
147
148
150{
151public:
153 {
154 }
155
157 {
158 }
159
160 virtual bool Run() override;
161
162 virtual const wxString GetName() const override
163 {
164 return wxT( "annular_width" );
165 };
166
167 virtual const wxString GetDescription() const override
168 {
169 return wxT( "Tests pad/via annular rings" );
170 }
171};
172
173
175{
177 {
178 reportAux( wxT( "Annular width violations ignored. Skipping check." ) );
179 return true; // continue with other tests
180 }
181
182 const int progressDelta = 500;
183
185 {
186 reportAux( wxT( "No annular width constraints found. Tests not run." ) );
187 return true; // continue with other tests
188 }
189
190 if( !reportPhase( _( "Checking pad & via annular rings..." ) ) )
191 return false; // DRC cancelled
192
194
195 auto calcEffort =
196 []( BOARD_ITEM* item )
197 {
198 switch( item->Type() )
199 {
200 case PCB_VIA_T:
201 return 1;
202
203 case PCB_PAD_T:
204 {
205 PAD* pad = static_cast<PAD*>( item );
206
207 if( !pad->HasHole() || pad->GetAttribute() != PAD_ATTRIB::PTH )
208 return 0;
209
210 if( pad->GetOffset() == VECTOR2I( 0, 0 ) )
211 {
212 switch( pad->GetShape() )
213 {
214 case PAD_SHAPE::CHAMFERED_RECT:
215 if( pad->GetChamferRectRatio() > 0.30 )
216 break;
217
219
220 case PAD_SHAPE::CIRCLE:
221 case PAD_SHAPE::OVAL:
222 case PAD_SHAPE::RECTANGLE:
223 case PAD_SHAPE::ROUNDRECT:
224 return 1;
225
226 default:
227 break;
228 }
229 }
230
231 return 5;
232 }
233
234 default:
235 return 0;
236 }
237 };
238
239 auto checkAnnularWidth =
240 [&]( BOARD_ITEM* item ) -> bool
241 {
243 return false;
244
245 // PADSTACKS TODO: once we have padstacks we'll need to run this per-layer....
246 auto constraint = m_drcEngine->EvalRules( ANNULAR_WIDTH_CONSTRAINT, item, nullptr,
248
249 int annularWidth = 0;
250 int v_min = 0;
251 int v_max = 0;
252 bool fail_min = false;
253 bool fail_max = false;
254
255
256 switch( item->Type() )
257 {
258 case PCB_VIA_T:
259 {
260 PCB_VIA* via = static_cast<PCB_VIA*>( item );
261 annularWidth = ( via->GetWidth() - via->GetDrillValue() ) / 2;
262 break;
263 }
264
265 case PCB_PAD_T:
266 {
267 PAD* pad = static_cast<PAD*>( item );
268 bool handled = false;
269
270 if( !pad->HasHole() || pad->GetAttribute() != PAD_ATTRIB::PTH )
271 return true;
272
273 std::vector<const PAD*> sameNumPads;
274
275 const FOOTPRINT* fp = static_cast<const FOOTPRINT*>( pad->GetParent() );
276
277 if( fp )
278 sameNumPads = fp->GetPads( pad->GetNumber(), pad );
279
280 if( pad->GetOffset() == VECTOR2I( 0, 0 ) )
281 {
282 switch( pad->GetShape() )
283 {
284 case PAD_SHAPE::CIRCLE:
285 annularWidth = ( pad->GetSizeX() - pad->GetDrillSizeX() ) / 2;
286
287 // If there are more pads with the same number. Check to see if the
288 // pad is embedded inside another pad with the same number below.
289 if( sameNumPads.empty() )
290 handled = true;
291
292 break;
293
294 case PAD_SHAPE::CHAMFERED_RECT:
295 if( pad->GetChamferRectRatio() > 0.30 )
296 break;
297
299
300 case PAD_SHAPE::OVAL:
301 case PAD_SHAPE::RECTANGLE:
302 case PAD_SHAPE::ROUNDRECT:
303 annularWidth = std::min( pad->GetSizeX() - pad->GetDrillSizeX(),
304 pad->GetSizeY() - pad->GetDrillSizeY() ) / 2;
305
306 // If there are more pads with the same number. Check to see if the
307 // pad is embedded inside another pad with the same number below.
308 if( sameNumPads.empty() )
309 handled = true;
310
311 break;
312
313 default:
314 break;
315 }
316 }
317
318 if( !handled )
319 {
320 // Slow (but general purpose) method.
321 SEG::ecoord dist_sq;
322 SHAPE_POLY_SET padOutline;
323 std::shared_ptr<SHAPE_SEGMENT> slot = pad->GetEffectiveHoleShape();
324
325 pad->TransformShapeToPolygon( padOutline, UNDEFINED_LAYER, 0, maxError,
326 ERROR_INSIDE );
327
328 if( sameNumPads.empty() )
329 {
330 if( !padOutline.Collide( pad->GetPosition() ) )
331 {
332 // Hole outside pad
333 annularWidth = 0;
334 }
335 else
336 {
337 // Disable is-inside test in SquaredDistance
338 padOutline.Outline( 0 ).SetClosed( false );
339
340 dist_sq = padOutline.SquaredDistanceToSeg( slot->GetSeg() );
341 annularWidth = sqrt( dist_sq ) - slot->GetWidth() / 2;
342 }
343 }
344 else if( constraint.Value().HasMin()
345 && ( annularWidth < constraint.Value().Min() ) )
346 {
347 SHAPE_POLY_SET otherPadOutline;
348 SHAPE_POLY_SET slotPolygon;
349
350 slot->TransformToPolygon( slotPolygon, 0, ERROR_INSIDE );
351
352 for( const PAD* sameNumPad : sameNumPads )
353 {
354 // Construct the full pad with outline and hole.
355 sameNumPad->TransformShapeToPolygon( otherPadOutline,
356 UNDEFINED_LAYER, 0, maxError,
358
359 sameNumPad->TransformHoleToPolygon( otherPadOutline, 0, maxError,
360 ERROR_INSIDE );
361
362
363 // If the pad hole under test intersects with another pad outline,
364 // the annular width calculated above is used.
365 bool intersects = false;
366
367 for( int i = 0; i < otherPadOutline.OutlineCount() && !intersects; i++ )
368 {
369 intersects |= slotPolygon.COutline( 0 ).Intersects( otherPadOutline.COutline( i ) );
370 if( intersects )
371 continue;
372
373 for( int j = 0; j < otherPadOutline.HoleCount( i ) && !intersects; j++ )
374 {
375 intersects |= slotPolygon.COutline( 0 ).Intersects( otherPadOutline.CHole( i, j ) );
376 if( intersects )
377 continue;
378 }
379 }
380
381 if( intersects )
382 continue;
383
384 // Determine the effective annular width if the pad hole under
385 // test lies withing the boundary of another pad outline.
386 int effectiveWidth = std::numeric_limits<int>::max();
387
388 if( collide( otherPadOutline, slotPolygon.Outline( 0 ),
389 effectiveWidth, &effectiveWidth ) )
390 {
391 if( effectiveWidth > annularWidth )
392 annularWidth = effectiveWidth;
393 }
394 }
395 }
396 }
397
398 break;
399 }
400
401 default:
402 return true;
403 }
404
405 if( constraint.GetSeverity() == RPT_SEVERITY_IGNORE )
406 return true;
407
408 if( constraint.Value().HasMin() )
409 {
410 v_min = constraint.Value().Min();
411 fail_min = annularWidth < v_min;
412 }
413
414 if( constraint.Value().HasMax() )
415 {
416 v_max = constraint.Value().Max();
417 fail_max = annularWidth > v_max;
418 }
419
420 if( fail_min || fail_max )
421 {
422 std::shared_ptr<DRC_ITEM> drcItem = DRC_ITEM::Create( DRCE_ANNULAR_WIDTH );
423 wxString msg;
424
425 if( fail_min )
426 {
427 msg = formatMsg( _( "(%s min annular width %s; actual %s)" ),
428 constraint.GetName(),
429 v_min,
430 annularWidth );
431 }
432
433 if( fail_max )
434 {
435 msg = formatMsg( _( "(%s max annular width %s; actual %s)" ),
436 constraint.GetName(),
437 v_max,
438 annularWidth );
439 }
440
441 drcItem->SetErrorMessage( drcItem->GetErrorText() + wxS( " " ) + msg );
442 drcItem->SetItems( item );
443 drcItem->SetViolatingRule( constraint.GetParentRule() );
444
445 reportViolation( drcItem, item->GetPosition(), item->GetLayer() );
446 }
447
448 return true;
449 };
450
451 BOARD* board = m_drcEngine->GetBoard();
452 size_t ii = 0;
453 size_t total = 0;
454
455 for( PCB_TRACK* item : board->Tracks() )
456 total += calcEffort( item );
457
458 for( FOOTPRINT* footprint : board->Footprints() )
459 {
460 for( PAD* pad : footprint->Pads() )
461 total += calcEffort( pad );
462 }
463
464 for( PCB_TRACK* item : board->Tracks() )
465 {
466 ii += calcEffort( item );
467
468 if( !reportProgress( ii, total, progressDelta ) )
469 return false; // DRC cancelled
470
471 if( !checkAnnularWidth( item ) )
472 break;
473 }
474
475 for( FOOTPRINT* footprint : board->Footprints() )
476 {
477 for( PAD* pad : footprint->Pads() )
478 {
479 ii += calcEffort( pad );
480
481 if( !reportProgress( ii, total, progressDelta ) )
482 return false; // DRC cancelled
483
484 if( !checkAnnularWidth( pad ) )
485 break;
486 }
487 }
488
490
491 return !m_drcEngine->IsCancelled();
492}
493
494
495namespace detail
496{
498}
A base class for any item which can be embedded within the BOARD container class, and therefore insta...
Definition: board_item.h:79
Information pertinent to a Pcbnew printed circuit board.
Definition: board.h:288
const FOOTPRINTS & Footprints() const
Definition: board.h:329
const TRACKS & Tracks() const
Definition: board.h:327
BOARD_DESIGN_SETTINGS & GetDesignSettings() const
Definition: board.cpp:872
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:652
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:352
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)
std::vector< const PAD * > GetPads(const wxString &aPadNumber, const PAD *aIgnore=nullptr) const
Definition: footprint.cpp:1836
Definition: pad.h:54
VECTOR2I::extended_type ecoord
Definition: seg.h:44
Represent a polyline containing arcs as well as line segments: A chain of connected line and/or arc s...
void SetClosed(bool aClosed)
Mark the line chain as closed (i.e.
bool Intersects(const SHAPE_LINE_CHAIN &aChain) const
int PointCount() const
Return the number of points (vertices) in this line chain.
virtual bool Collide(const VECTOR2I &aP, int aClearance=0, int *aActual=nullptr, VECTOR2I *aLocation=nullptr) const override
Check if point aP lies closer to us than aClearance.
void Append(int aX, int aY, bool aAllowDuplication=false)
Append a new point at the end of the line chain.
const VECTOR2I & CPoint(int aIndex) const
Return a reference to a given point in the line chain.
int SegmentCount() const
Return the number of segments in this line chain.
const SEG CSegment(int aIndex) const
Return a constant copy of the aIndex segment in the line chain.
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,...
int HoleCount(int aOutline) const
Returns the number of holes in a given outline.
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.
const SHAPE_LINE_CHAIN & CHole(int aOutline, int aHole) const
int OutlineCount() const
Return the number of outlines in the set.
void TransformToPolygon(SHAPE_POLY_SET &aBuffer, int aError, ERROR_LOC aErrorLoc) const override
Fills a SHAPE_POLY_SET with a polygon representation of this shape.
const SHAPE_LINE_CHAIN & COutline(int aIndex) const
The common library.
@ DRCE_ANNULAR_WIDTH
Definition: drc_item.h:55
@ ANNULAR_WIDTH_CONSTRAINT
Definition: drc_rule.h:58
static bool collide(const SHAPE_LINE_CHAIN &aLhs, const SHAPE_LINE_CHAIN &aRhs, int aClearance, int *aDistance=nullptr, VECTOR2I *aPt1=nullptr)
Find the nearest collision point between two shape line chains.
#define _(s)
@ ERROR_OUTSIDE
@ 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< int32_t > VECTOR2I
Definition: vector2d.h:673