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 // TODO(JE) padstacks
206 PAD* pad = static_cast<PAD*>( item );
207
208 if( !pad->HasHole() || pad->GetAttribute() != PAD_ATTRIB::PTH )
209 return 0;
210
211 if( pad->GetOffset( PADSTACK::ALL_LAYERS ) == VECTOR2I( 0, 0 ) )
212 {
213 switch( pad->GetShape( PADSTACK::ALL_LAYERS ) )
214 {
215 case PAD_SHAPE::CHAMFERED_RECT:
216 if( pad->GetChamferRectRatio( PADSTACK::ALL_LAYERS ) > 0.30 )
217 break;
218
220
221 case PAD_SHAPE::CIRCLE:
222 case PAD_SHAPE::OVAL:
223 case PAD_SHAPE::RECTANGLE:
224 case PAD_SHAPE::ROUNDRECT:
225 return 1;
226
227 default:
228 break;
229 }
230 }
231
232 return 5;
233 }
234
235 default:
236 return 0;
237 }
238 };
239
240 auto checkAnnularWidth =
241 [&]( BOARD_ITEM* item ) -> bool
242 {
244 return false;
245
246 // PADSTACKS TODO: once we have padstacks we'll need to run this per-layer....
247 auto constraint = m_drcEngine->EvalRules( ANNULAR_WIDTH_CONSTRAINT, item, nullptr,
249
250 int annularWidth = 0;
251 int v_min = 0;
252 int v_max = 0;
253 bool fail_min = false;
254 bool fail_max = false;
255
256
257 switch( item->Type() )
258 {
259 case PCB_VIA_T:
260 {
261 PCB_VIA* via = static_cast<PCB_VIA*>( item );
262 // TODO(JE) padstacks
263 annularWidth = ( via->GetWidth( PADSTACK::ALL_LAYERS ) - via->GetDrillValue() ) / 2;
264 break;
265 }
266
267 case PCB_PAD_T:
268 {
269 PAD* pad = static_cast<PAD*>( item );
270 bool handled = false;
271
272 if( !pad->HasHole() || pad->GetAttribute() != PAD_ATTRIB::PTH )
273 return true;
274
275 std::vector<const PAD*> sameNumPads;
276
277 const FOOTPRINT* fp = static_cast<const FOOTPRINT*>( pad->GetParent() );
278
279 if( fp )
280 sameNumPads = fp->GetPads( pad->GetNumber(), pad );
281
282 if( pad->GetOffset( PADSTACK::ALL_LAYERS ) == VECTOR2I( 0, 0 ) )
283 {
284 switch( pad->GetShape( PADSTACK::ALL_LAYERS ) )
285 {
286 case PAD_SHAPE::CIRCLE:
287 annularWidth = ( pad->GetSizeX() - pad->GetDrillSizeX() ) / 2;
288
289 // If there are more pads with the same number. Check to see if the
290 // pad is embedded inside another pad with the same number below.
291 if( sameNumPads.empty() )
292 handled = true;
293
294 break;
295
296 case PAD_SHAPE::CHAMFERED_RECT:
297 if( pad->GetChamferRectRatio( PADSTACK::ALL_LAYERS ) > 0.30 )
298 break;
299
301
302 case PAD_SHAPE::OVAL:
303 case PAD_SHAPE::RECTANGLE:
304 case PAD_SHAPE::ROUNDRECT:
305 annularWidth = std::min( pad->GetSizeX() - pad->GetDrillSizeX(),
306 pad->GetSizeY() - pad->GetDrillSizeY() ) / 2;
307
308 // If there are more pads with the same number. Check to see if the
309 // pad is embedded inside another pad with the same number below.
310 if( sameNumPads.empty() )
311 handled = true;
312
313 break;
314
315 default:
316 break;
317 }
318 }
319
320 if( !handled )
321 {
322 // Slow (but general purpose) method.
323 SEG::ecoord dist_sq;
324 SHAPE_POLY_SET padOutline;
325 std::shared_ptr<SHAPE_SEGMENT> slot = pad->GetEffectiveHoleShape();
326
327 // TODO(JE) padstacks
328 pad->TransformShapeToPolygon( padOutline, PADSTACK::ALL_LAYERS, 0, maxError,
329 ERROR_INSIDE );
330
331 if( sameNumPads.empty() )
332 {
333 if( !padOutline.Collide( pad->GetPosition() ) )
334 {
335 // Hole outside pad
336 annularWidth = 0;
337 }
338 else
339 {
340 // Disable is-inside test in SquaredDistance
341 padOutline.Outline( 0 ).SetClosed( false );
342
343 dist_sq = padOutline.SquaredDistanceToSeg( slot->GetSeg() );
344 annularWidth = sqrt( dist_sq ) - slot->GetWidth() / 2;
345 }
346 }
347 else if( constraint.Value().HasMin()
348 && ( annularWidth < constraint.Value().Min() ) )
349 {
350 SHAPE_POLY_SET otherPadOutline;
351 SHAPE_POLY_SET slotPolygon;
352
353 slot->TransformToPolygon( slotPolygon, 0, ERROR_INSIDE );
354
355 for( const PAD* sameNumPad : sameNumPads )
356 {
357 // Construct the full pad with outline and hole.
358 sameNumPad->TransformShapeToPolygon( otherPadOutline,
359 PADSTACK::ALL_LAYERS, 0, maxError,
361
362 sameNumPad->TransformHoleToPolygon( otherPadOutline, 0, maxError,
363 ERROR_INSIDE );
364
365
366 // If the pad hole under test intersects with another pad outline,
367 // the annular width calculated above is used.
368 bool intersects = false;
369
370 for( int i = 0; i < otherPadOutline.OutlineCount() && !intersects; i++ )
371 {
372 intersects |= slotPolygon.COutline( 0 ).Intersects( otherPadOutline.COutline( i ) );
373 if( intersects )
374 continue;
375
376 for( int j = 0; j < otherPadOutline.HoleCount( i ) && !intersects; j++ )
377 {
378 intersects |= slotPolygon.COutline( 0 ).Intersects( otherPadOutline.CHole( i, j ) );
379 if( intersects )
380 continue;
381 }
382 }
383
384 if( intersects )
385 continue;
386
387 // Determine the effective annular width if the pad hole under
388 // test lies withing the boundary of another pad outline.
389 int effectiveWidth = std::numeric_limits<int>::max();
390
391 if( collide( otherPadOutline, slotPolygon.Outline( 0 ),
392 effectiveWidth, &effectiveWidth ) )
393 {
394 if( effectiveWidth > annularWidth )
395 annularWidth = effectiveWidth;
396 }
397 }
398 }
399 }
400
401 break;
402 }
403
404 default:
405 return true;
406 }
407
408 if( constraint.GetSeverity() == RPT_SEVERITY_IGNORE )
409 return true;
410
411 if( constraint.Value().HasMin() )
412 {
413 v_min = constraint.Value().Min();
414 fail_min = annularWidth < v_min;
415 }
416
417 if( constraint.Value().HasMax() )
418 {
419 v_max = constraint.Value().Max();
420 fail_max = annularWidth > v_max;
421 }
422
423 if( fail_min || fail_max )
424 {
425 std::shared_ptr<DRC_ITEM> drcItem = DRC_ITEM::Create( DRCE_ANNULAR_WIDTH );
426 wxString msg;
427
428 if( fail_min )
429 {
430 msg = formatMsg( _( "(%s min annular width %s; actual %s)" ),
431 constraint.GetName(),
432 v_min,
433 annularWidth );
434 }
435
436 if( fail_max )
437 {
438 msg = formatMsg( _( "(%s max annular width %s; actual %s)" ),
439 constraint.GetName(),
440 v_max,
441 annularWidth );
442 }
443
444 drcItem->SetErrorMessage( drcItem->GetErrorText() + wxS( " " ) + msg );
445 drcItem->SetItems( item );
446 drcItem->SetViolatingRule( constraint.GetParentRule() );
447
448 reportViolation( drcItem, item->GetPosition(), item->GetLayer() );
449 }
450
451 return true;
452 };
453
454 BOARD* board = m_drcEngine->GetBoard();
455 size_t ii = 0;
456 size_t total = 0;
457
458 for( PCB_TRACK* item : board->Tracks() )
459 total += calcEffort( item );
460
461 for( FOOTPRINT* footprint : board->Footprints() )
462 {
463 for( PAD* pad : footprint->Pads() )
464 total += calcEffort( pad );
465 }
466
467 for( PCB_TRACK* item : board->Tracks() )
468 {
469 ii += calcEffort( item );
470
471 if( !reportProgress( ii, total, progressDelta ) )
472 return false; // DRC cancelled
473
474 if( !checkAnnularWidth( item ) )
475 break;
476 }
477
478 for( FOOTPRINT* footprint : board->Footprints() )
479 {
480 for( PAD* pad : footprint->Pads() )
481 {
482 ii += calcEffort( pad );
483
484 if( !reportProgress( ii, total, progressDelta ) )
485 return false; // DRC cancelled
486
487 if( !checkAnnularWidth( pad ) )
488 break;
489 }
490 }
491
493
494 return !m_drcEngine->IsCancelled();
495}
496
497
498namespace detail
499{
501}
@ 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:890
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:138
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)
@ 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