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 The 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
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
91 auto calcEffort =
92 []( BOARD_ITEM* item ) -> size_t
93 {
94 switch( item->Type() )
95 {
96 case PCB_VIA_T:
97 return 1;
98
99 case PCB_PAD_T:
100 {
101 PAD* pad = static_cast<PAD*>( item );
102
103 if( !pad->HasHole() || pad->GetAttribute() != PAD_ATTRIB::PTH )
104 return 0;
105
106 size_t effort = 0;
107
108 pad->Padstack().ForEachUniqueLayer(
109 [&pad, &effort]( PCB_LAYER_ID aLayer )
110 {
111 if( pad->GetOffset( aLayer ) == VECTOR2I( 0, 0 ) )
112 {
113 switch( pad->GetShape( aLayer ) )
114 {
115 case PAD_SHAPE::CHAMFERED_RECT:
116 if( pad->GetChamferRectRatio( aLayer ) > 0.30 )
117 break;
118
120
121 case PAD_SHAPE::CIRCLE:
122 case PAD_SHAPE::OVAL:
123 case PAD_SHAPE::RECTANGLE:
124 case PAD_SHAPE::ROUNDRECT:
125 effort += 1;
126 break;
127
128 default:
129 break;
130 }
131 }
132
133 effort += 5;
134 } );
135
136 return effort;
137 }
138
139 default:
140 return 0;
141 }
142 };
143
144 auto checkPadAnnularWidth =
145 []( PAD* pad, PCB_LAYER_ID aLayer, DRC_CONSTRAINT& constraint,
146 const std::vector<const PAD*>& sameNumPads,
147 int* aMinAnnularWidth, int* aMaxAnnularWidth )
148 {
149 int annularWidth = 0;
150 bool handled = false;
151
152 if( pad->GetOffset( aLayer ) == VECTOR2I( 0, 0 ) )
153 {
154 VECTOR2I padSize = pad->GetSize( aLayer );
155
156 switch( pad->GetShape( aLayer ) )
157 {
158 case PAD_SHAPE::CIRCLE:
159 annularWidth = ( padSize.x - pad->GetDrillSizeX() ) / 2;
160
161 // If there are more pads with the same number then we'll still need to
162 // run the more generalised checks below.
163 handled = sameNumPads.empty();
164
165 break;
166
167 case PAD_SHAPE::CHAMFERED_RECT:
168 if( pad->GetChamferRectRatio( aLayer ) > 0.30 )
169 break;
170
172
173 case PAD_SHAPE::OVAL:
174 case PAD_SHAPE::RECTANGLE:
175 case PAD_SHAPE::ROUNDRECT:
176 annularWidth = std::min( padSize.x - pad->GetDrillSizeX(),
177 padSize.y - pad->GetDrillSizeY() ) / 2;
178
179 // If there are more pads with the same number then we'll still need to
180 // run the more generalised checks below.
181 handled = sameNumPads.empty();
182
183 break;
184
185 default:
186 break;
187 }
188 }
189
190 if( !handled )
191 {
192 // Slow (but general purpose) method.
193 int maxError = pad->GetBoard()->GetDesignSettings().m_MaxError;
194 SEG::ecoord dist_sq;
195 SHAPE_POLY_SET padOutline;
196 std::shared_ptr<SHAPE_SEGMENT> slot = pad->GetEffectiveHoleShape();
197
198 pad->TransformShapeToPolygon( padOutline, aLayer, 0, maxError, ERROR_INSIDE );
199
200 if( sameNumPads.empty() )
201 {
202 if( !padOutline.Collide( pad->GetPosition() ) )
203 {
204 // Hole outside pad
205 annularWidth = 0;
206 }
207 else
208 {
209 // Disable is-inside test in SquaredDistance
210 padOutline.Outline( 0 ).SetClosed( false );
211
212 dist_sq = padOutline.SquaredDistanceToSeg( slot->GetSeg() );
213 annularWidth = sqrt( dist_sq ) - slot->GetWidth() / 2;
214 }
215 }
216 else if( constraint.Value().HasMin()
217 && ( annularWidth < constraint.Value().Min() ) )
218 {
219 SHAPE_POLY_SET aggregatePadOutline = padOutline;
220 SHAPE_POLY_SET otherPadHoles;
221 SHAPE_POLY_SET slotPolygon;
222
223 slot->TransformToPolygon( slotPolygon, 0, ERROR_INSIDE );
224
225 for( const PAD* sameNumPad : sameNumPads )
226 {
227 // Construct the full pad with outline and hole.
228 sameNumPad->TransformShapeToPolygon( aggregatePadOutline,
230 maxError, ERROR_OUTSIDE );
231
232 sameNumPad->TransformHoleToPolygon( otherPadHoles, 0, maxError,
233 ERROR_INSIDE );
234 }
235
236 aggregatePadOutline.BooleanSubtract( otherPadHoles );
237
238 if( !aggregatePadOutline.Collide( pad->GetPosition() ) )
239 {
240 // Hole outside pad
241 annularWidth = 0;
242 }
243 else
244 {
245 // Disable is-inside test in SquaredDistance
246 for( int ii = 0; ii < aggregatePadOutline.OutlineCount(); ++ii )
247 aggregatePadOutline.Outline( ii ).SetClosed( false );
248
249 dist_sq = aggregatePadOutline.SquaredDistanceToSeg( slot->GetSeg() );
250 annularWidth = sqrt( dist_sq ) - slot->GetWidth() / 2;
251 }
252 }
253 }
254
255 *aMaxAnnularWidth = std::max( *aMaxAnnularWidth, annularWidth );
256 *aMinAnnularWidth = std::min( *aMinAnnularWidth, annularWidth );
257 };
258
259 auto checkAnnularWidth =
260 [&]( BOARD_ITEM* item ) -> bool
261 {
263 return false;
264
265 auto constraint = m_drcEngine->EvalRules( ANNULAR_WIDTH_CONSTRAINT, item, nullptr,
267
268 int minAnnularWidth = INT_MAX;
269 int maxAnnularWidth = 0;
270 int v_min = 0;
271 int v_max = 0;
272 bool fail_min = false;
273 bool fail_max = false;
274
275 switch( item->Type() )
276 {
277 case PCB_VIA_T:
278 {
279 PCB_VIA* via = static_cast<PCB_VIA*>( item );
280 int drill = via->GetDrillValue();
281
282 via->Padstack().ForEachUniqueLayer(
283 [&]( PCB_LAYER_ID aLayer )
284 {
285 int layerWidth = ( via->GetWidth( aLayer ) - drill ) / 2;
286 minAnnularWidth = std::min( minAnnularWidth, layerWidth );
287 maxAnnularWidth = std::max( maxAnnularWidth, layerWidth );
288 } );
289 break;
290 }
291
292 case PCB_PAD_T:
293 {
294 PAD* pad = static_cast<PAD*>( item );
295
296 if( !pad->HasHole() || pad->GetAttribute() != PAD_ATTRIB::PTH )
297 return true;
298
299 std::vector<const PAD*> sameNumPads;
300
301 if( const FOOTPRINT* fp = static_cast<const FOOTPRINT*>( pad->GetParent() ) )
302 sameNumPads = fp->GetPads( pad->GetNumber(), pad );
303
304 pad->Padstack().ForEachUniqueLayer(
305 [&]( PCB_LAYER_ID aLayer )
306 {
307 checkPadAnnularWidth( pad, aLayer, constraint, sameNumPads,
308 &minAnnularWidth, &maxAnnularWidth );
309 } );
310
311 break;
312 }
313
314 default:
315 return true;
316 }
317
318 if( constraint.GetSeverity() == RPT_SEVERITY_IGNORE )
319 return true;
320
321 if( constraint.Value().HasMin() )
322 {
323 v_min = constraint.Value().Min();
324 fail_min = minAnnularWidth < v_min;
325 }
326
327 if( constraint.Value().HasMax() )
328 {
329 v_max = constraint.Value().Max();
330 fail_max = maxAnnularWidth > v_max;
331 }
332
333 if( fail_min || fail_max )
334 {
335 std::shared_ptr<DRC_ITEM> drcItem = DRC_ITEM::Create( DRCE_ANNULAR_WIDTH );
336 wxString msg;
337
338 if( fail_min )
339 {
340 msg = formatMsg( _( "(%s min annular width %s; actual %s)" ),
341 constraint.GetName(),
342 v_min,
343 minAnnularWidth );
344 }
345
346 if( fail_max )
347 {
348 msg = formatMsg( _( "(%s max annular width %s; actual %s)" ),
349 constraint.GetName(),
350 v_max,
351 maxAnnularWidth );
352 }
353
354 drcItem->SetErrorMessage( drcItem->GetErrorText() + wxS( " " ) + msg );
355 drcItem->SetItems( item );
356 drcItem->SetViolatingRule( constraint.GetParentRule() );
357
358 reportViolation( drcItem, item->GetPosition(), item->GetLayer() );
359 }
360
361 return true;
362 };
363
364 BOARD* board = m_drcEngine->GetBoard();
365 size_t ii = 0;
366 size_t total = 0;
367
368 for( PCB_TRACK* item : board->Tracks() )
369 total += calcEffort( item );
370
371 for( FOOTPRINT* footprint : board->Footprints() )
372 {
373 for( PAD* pad : footprint->Pads() )
374 total += calcEffort( pad );
375 }
376
377 for( PCB_TRACK* item : board->Tracks() )
378 {
379 ii += calcEffort( item );
380
381 if( !reportProgress( ii, total, progressDelta ) )
382 return false; // DRC cancelled
383
384 if( !checkAnnularWidth( item ) )
385 break;
386 }
387
388 for( FOOTPRINT* footprint : board->Footprints() )
389 {
390 for( PAD* pad : footprint->Pads() )
391 {
392 ii += calcEffort( pad );
393
394 if( !reportProgress( ii, total, progressDelta ) )
395 return false; // DRC cancelled
396
397 if( !checkAnnularWidth( pad ) )
398 break;
399 }
400 }
401
403
404 return !m_drcEngine->IsCancelled();
405}
406
407
408namespace detail
409{
411}
@ 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:295
const FOOTPRINTS & Footprints() const
Definition: board.h:336
const TRACKS & Tracks() const
Definition: board.h:334
BOARD * GetBoard() const
Definition: drc_engine.h:96
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:690
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:395
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_CUSTOM_MARKER_HANDLER *aCustomHandler=nullptr)
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)
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
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.
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.
void BooleanSubtract(const SHAPE_POLY_SET &b)
Perform boolean polyset difference.
The common library.
@ DRCE_ANNULAR_WIDTH
Definition: drc_item.h:58
@ ANNULAR_WIDTH_CONSTRAINT
Definition: drc_rule.h:61
#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:695