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*/
45
59static inline bool collide( const SHAPE_LINE_CHAIN& aLhs, const SHAPE_LINE_CHAIN& aRhs,
60 int aClearance, int* aDistance = nullptr, VECTOR2I* aPt1 = nullptr )
61{
62 wxCHECK( aLhs.PointCount() && aRhs.PointCount(), false );
63
64 VECTOR2I pt1;
65 bool retv = false;
66 int dist = std::numeric_limits<int>::max();
67 int tmp = dist;
68
69 SHAPE_LINE_CHAIN lhs( aLhs );
70 SHAPE_LINE_CHAIN rhs( aRhs );
71
72 lhs.SetClosed( false );
73 lhs.Append( lhs.CPoint( 0 ) );
74 rhs.SetClosed( false );
75 rhs.Append( rhs.CPoint( 0 ) );
76
77 for( int i = 0; i < rhs.SegmentCount(); i ++ )
78 {
79 if( lhs.Collide( rhs.CSegment( i ), tmp, &tmp, &pt1 ) )
80 {
81 retv = true;
82
83 if( tmp < dist )
84 dist = tmp;
85
86 if( aDistance )
87 *aDistance = dist;
88
89 if( aPt1 )
90 *aPt1 = pt1;
91 }
92 }
93
94 return retv;
95}
96
97
98static bool collide( const SHAPE_POLY_SET& aLhs, const SHAPE_LINE_CHAIN& aRhs, int aClearance,
99 int* aDistance = nullptr, VECTOR2I* aPt1 = nullptr )
100{
101 VECTOR2I pt1;
102 bool retv = false;
103 int tmp = std::numeric_limits<int>::max();
104 int dist = tmp;
105
106 for( int i = 0; i < aLhs.OutlineCount(); i++ )
107 {
108 if( collide( aLhs.Outline( i ), aRhs, aClearance, &tmp, &pt1 ) )
109 {
110 retv = true;
111
112 if( tmp < dist )
113 {
114 dist = tmp;
115
116 if( aDistance )
117 *aDistance = dist;
118
119 if( aPt1 )
120 *aPt1 = pt1;
121 }
122 }
123
124 for( int j = 0; j < aLhs.HoleCount( i ); i++ )
125 {
126 if( collide( aLhs.CHole( i, j ), aRhs, aClearance, &tmp, &pt1 ) )
127 {
128 retv = true;
129
130 if( tmp < dist )
131 {
132 dist = tmp;
133
134 if( aDistance )
135 *aDistance = dist;
136
137 if( aPt1 )
138 *aPt1 = pt1;
139 }
140 }
141 }
142 }
143
144 return retv;
145}
146
147
149{
150public:
152 {
153 }
154
156 {
157 }
158
159 virtual bool Run() override;
160
161 virtual const wxString GetName() const override
162 {
163 return wxT( "annular_width" );
164 };
165
166 virtual const wxString GetDescription() const override
167 {
168 return wxT( "Tests pad/via annular rings" );
169 }
170};
171
172
174{
176 {
177 reportAux( wxT( "Annular width violations ignored. Skipping check." ) );
178 return true; // continue with other tests
179 }
180
181 const int progressDelta = 500;
182
184 {
185 reportAux( wxT( "No annular width constraints found. Tests not run." ) );
186 return true; // continue with other tests
187 }
188
189 if( !reportPhase( _( "Checking pad & via annular rings..." ) ) )
190 return false; // DRC cancelled
191
193
194 auto calcEffort =
195 []( BOARD_ITEM* item ) -> size_t
196 {
197 switch( item->Type() )
198 {
199 case PCB_VIA_T:
200 return 1;
201
202 case PCB_PAD_T:
203 {
204 PAD* pad = static_cast<PAD*>( item );
205
206 if( !pad->HasHole() || pad->GetAttribute() != PAD_ATTRIB::PTH )
207 return 0;
208
209 size_t effort = 0;
210
211 pad->Padstack().ForEachUniqueLayer(
212 [&pad, &effort]( PCB_LAYER_ID aLayer )
213 {
214 if( pad->GetOffset( aLayer ) == VECTOR2I( 0, 0 ) )
215 {
216 switch( pad->GetShape( aLayer ) )
217 {
218 case PAD_SHAPE::CHAMFERED_RECT:
219 if( pad->GetChamferRectRatio( aLayer ) > 0.30 )
220 break;
221
223
224 case PAD_SHAPE::CIRCLE:
225 case PAD_SHAPE::OVAL:
226 case PAD_SHAPE::RECTANGLE:
227 case PAD_SHAPE::ROUNDRECT:
228 effort += 1;
229 break;
230
231 default:
232 break;
233 }
234 }
235
236 effort += 5;
237 } );
238
239 return effort;
240 }
241
242 default:
243 return 0;
244 }
245 };
246
247 auto checkAnnularWidth =
248 [&]( BOARD_ITEM* item ) -> bool
249 {
251 return false;
252
253 auto constraint = m_drcEngine->EvalRules( ANNULAR_WIDTH_CONSTRAINT, item, nullptr,
255
256 int minAnnularWidth = INT_MAX;
257 int maxAnnularWidth = 0;
258 int v_min = 0;
259 int v_max = 0;
260 bool fail_min = false;
261 bool fail_max = false;
262
263
264 switch( item->Type() )
265 {
266 case PCB_VIA_T:
267 {
268 PCB_VIA* via = static_cast<PCB_VIA*>( item );
269 int drill = via->GetDrillValue();
270
271 via->Padstack().ForEachUniqueLayer(
272 [&]( PCB_LAYER_ID aLayer )
273 {
274 int layerWidth = ( via->GetWidth( aLayer ) - drill ) / 2;
275 minAnnularWidth = std::min( minAnnularWidth, layerWidth );
276 maxAnnularWidth = std::max( maxAnnularWidth, layerWidth );
277 } );
278 break;
279 }
280
281 case PCB_PAD_T:
282 {
283 PAD* pad = static_cast<PAD*>( item );
284 bool handled = false;
285
286 if( !pad->HasHole() || pad->GetAttribute() != PAD_ATTRIB::PTH )
287 return true;
288
289 std::vector<const PAD*> sameNumPads;
290
291 const FOOTPRINT* fp = static_cast<const FOOTPRINT*>( pad->GetParent() );
292
293 if( fp )
294 sameNumPads = fp->GetPads( pad->GetNumber(), pad );
295
296 pad->Padstack().ForEachUniqueLayer(
297 [&]( PCB_LAYER_ID aLayer )
298 {
299 int annularWidth = 0;
300
301 if( pad->GetOffset( aLayer ) == VECTOR2I( 0, 0 ) )
302 {
303 VECTOR2I padSize = pad->GetSize( aLayer );
304
305 switch( pad->GetShape( aLayer ) )
306 {
307 case PAD_SHAPE::CIRCLE:
308 annularWidth = ( padSize.x - pad->GetDrillSizeX() ) / 2;
309
310 // If there are more pads with the same number. Check to see if the
311 // pad is embedded inside another pad with the same number below.
312 if( sameNumPads.empty() )
313 handled = true;
314
315 break;
316
317 case PAD_SHAPE::CHAMFERED_RECT:
318 if( pad->GetChamferRectRatio( aLayer ) > 0.30 )
319 break;
320
322
323 case PAD_SHAPE::OVAL:
324 case PAD_SHAPE::RECTANGLE:
325 case PAD_SHAPE::ROUNDRECT:
326 annularWidth = std::min( padSize.x - pad->GetDrillSizeX(),
327 padSize.y - pad->GetDrillSizeY() ) / 2;
328
329 // If there are more pads with the same number. Check to see if the
330 // pad is embedded inside another pad with the same number below.
331 if( sameNumPads.empty() )
332 handled = true;
333
334 break;
335
336 default:
337 break;
338 }
339 }
340
341 if( !handled )
342 {
343 // Slow (but general purpose) method.
344 SEG::ecoord dist_sq;
345 SHAPE_POLY_SET padOutline;
346 std::shared_ptr<SHAPE_SEGMENT> slot = pad->GetEffectiveHoleShape();
347
348 pad->TransformShapeToPolygon( padOutline, aLayer, 0, maxError,
349 ERROR_INSIDE );
350
351 if( sameNumPads.empty() )
352 {
353 if( !padOutline.Collide( pad->GetPosition() ) )
354 {
355 // Hole outside pad
356 annularWidth = 0;
357 }
358 else
359 {
360 // Disable is-inside test in SquaredDistance
361 padOutline.Outline( 0 ).SetClosed( false );
362
363 dist_sq = padOutline.SquaredDistanceToSeg( slot->GetSeg() );
364 annularWidth = sqrt( dist_sq ) - slot->GetWidth() / 2;
365 }
366 }
367 else if( constraint.Value().HasMin()
368 && ( minAnnularWidth < constraint.Value().Min() ) )
369 {
370 SHAPE_POLY_SET otherPadOutline;
371 SHAPE_POLY_SET slotPolygon;
372
373 slot->TransformToPolygon( slotPolygon, 0, ERROR_INSIDE );
374
375 for( const PAD* sameNumPad : sameNumPads )
376 {
377 // Construct the full pad with outline and hole.
378 sameNumPad->TransformShapeToPolygon(
379 otherPadOutline, PADSTACK::ALL_LAYERS, 0, maxError,
381
382 sameNumPad->TransformHoleToPolygon(
383 otherPadOutline, 0, maxError, ERROR_INSIDE );
384
385
386 // If the pad hole under test intersects with another pad outline,
387 // the annular width calculated above is used.
388 bool intersects = false;
389
390 for( int i = 0;
391 i < otherPadOutline.OutlineCount() && !intersects;
392 i++ )
393 {
394 intersects |= slotPolygon.COutline( 0 ).Intersects(
395 otherPadOutline.COutline( i ) );
396
397 if( intersects )
398 break;
399
400 for( int j = 0;
401 j < otherPadOutline.HoleCount( i ) && !intersects;
402 j++ )
403 {
404 intersects |= slotPolygon.COutline( 0 ).Intersects(
405 otherPadOutline.CHole( i, j ) );
406
407 if( intersects )
408 break;
409 }
410 }
411
412 if( intersects )
413 continue;
414
415 // Determine the effective annular width if the pad hole under
416 // test lies withing the boundary of another pad outline.
417 int effectiveWidth = std::numeric_limits<int>::max();
418
419 if( collide( otherPadOutline, slotPolygon.Outline( 0 ),
420 effectiveWidth, &effectiveWidth ) )
421 {
422 if( effectiveWidth > annularWidth )
423 annularWidth = effectiveWidth;
424 }
425 }
426 }
427 }
428
429 maxAnnularWidth = std::max( maxAnnularWidth, annularWidth );
430 minAnnularWidth = std::min( minAnnularWidth, annularWidth );
431 } );
432
433 break;
434 }
435
436 default:
437 return true;
438 }
439
440 if( constraint.GetSeverity() == RPT_SEVERITY_IGNORE )
441 return true;
442
443 if( constraint.Value().HasMin() )
444 {
445 v_min = constraint.Value().Min();
446 fail_min = minAnnularWidth < v_min;
447 }
448
449 if( constraint.Value().HasMax() )
450 {
451 v_max = constraint.Value().Max();
452 fail_max = maxAnnularWidth > v_max;
453 }
454
455 if( fail_min || fail_max )
456 {
457 std::shared_ptr<DRC_ITEM> drcItem = DRC_ITEM::Create( DRCE_ANNULAR_WIDTH );
458 wxString msg;
459
460 if( fail_min )
461 {
462 msg = formatMsg( _( "(%s min annular width %s; actual %s)" ),
463 constraint.GetName(),
464 v_min,
465 minAnnularWidth );
466 }
467
468 if( fail_max )
469 {
470 msg = formatMsg( _( "(%s max annular width %s; actual %s)" ),
471 constraint.GetName(),
472 v_max,
473 maxAnnularWidth );
474 }
475
476 drcItem->SetErrorMessage( drcItem->GetErrorText() + wxS( " " ) + msg );
477 drcItem->SetItems( item );
478 drcItem->SetViolatingRule( constraint.GetParentRule() );
479
480 reportViolation( drcItem, item->GetPosition(), item->GetLayer() );
481 }
482
483 return true;
484 };
485
486 BOARD* board = m_drcEngine->GetBoard();
487 size_t ii = 0;
488 size_t total = 0;
489
490 for( PCB_TRACK* item : board->Tracks() )
491 total += calcEffort( item );
492
493 for( FOOTPRINT* footprint : board->Footprints() )
494 {
495 for( PAD* pad : footprint->Pads() )
496 total += calcEffort( pad );
497 }
498
499 for( PCB_TRACK* item : board->Tracks() )
500 {
501 ii += calcEffort( item );
502
503 if( !reportProgress( ii, total, progressDelta ) )
504 return false; // DRC cancelled
505
506 if( !checkAnnularWidth( item ) )
507 break;
508 }
509
510 for( FOOTPRINT* footprint : board->Footprints() )
511 {
512 for( PAD* pad : footprint->Pads() )
513 {
514 ii += calcEffort( pad );
515
516 if( !reportProgress( ii, total, progressDelta ) )
517 return false; // DRC cancelled
518
519 if( !checkAnnularWidth( pad ) )
520 break;
521 }
522 }
523
525
526 return !m_drcEngine->IsCancelled();
527}
528
529
530namespace detail
531{
533}
@ ERROR_OUTSIDE
Definition: approximation.h:33
@ ERROR_INSIDE
Definition: approximation.h:34
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:290
const FOOTPRINTS & Footprints() const
Definition: board.h:331
const TRACKS & Tracks() const
Definition: board.h:329
BOARD_DESIGN_SETTINGS & GetDesignSettings() const
Definition: board.cpp:892
BOARD * GetBoard() const
Definition: drc_engine.h:99
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:679
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:372
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:1845
static constexpr PCB_LAYER_ID ALL_LAYERS
! Temporary layer identifier to identify code that is not padstack-aware
Definition: padstack.h:144
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:58
@ ANNULAR_WIDTH_CONSTRAINT
Definition: drc_rule.h:61
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)
PCB_LAYER_ID
A quick note on layer IDs:
Definition: layer_ids.h:60
@ 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:691