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>
31#include <macros.h>
34
35/*
36 Via/pad annular ring width test. Checks if there's sufficient copper ring around
37 PTH/NPTH holes (vias/pads)
38 Errors generated:
39 - DRCE_ANNULAR_WIDTH
40
41 Todo:
42 - check pad holes too.
43*/
44
45
47{
48public:
50 {}
51
53
54 virtual bool Run() override;
55
56 virtual const wxString GetName() const override { return wxT( "annular_width" ); };
57};
58
59
61{
63 {
64 REPORT_AUX( wxT( "Annular width violations ignored. Skipping check." ) );
65 return true; // continue with other tests
66 }
67
68 const int progressDelta = 500;
69
71 {
72 REPORT_AUX( wxT( "No annular width constraints found. Tests not run." ) );
73 return true; // continue with other tests
74 }
75
76 if( !reportPhase( _( "Checking pad & via annular rings..." ) ) )
77 return false; // DRC cancelled
78
79 auto calcEffort =
80 []( BOARD_ITEM* item ) -> size_t
81 {
82 switch( item->Type() )
83 {
84 case PCB_VIA_T:
85 return 1;
86
87 case PCB_PAD_T:
88 {
89 PAD* pad = static_cast<PAD*>( item );
90
91 if( !pad->HasHole() || pad->GetAttribute() != PAD_ATTRIB::PTH )
92 return 0;
93
94 size_t effort = 0;
95
96 pad->Padstack().ForEachUniqueLayer(
97 [&pad, &effort]( PCB_LAYER_ID aLayer )
98 {
99 if( pad->GetOffset( aLayer ) == VECTOR2I( 0, 0 ) )
100 {
101 switch( pad->GetShape( aLayer ) )
102 {
103 case PAD_SHAPE::CHAMFERED_RECT:
104 if( pad->GetChamferRectRatio( aLayer ) > 0.30 )
105 break;
106
108
109 case PAD_SHAPE::CIRCLE:
110 case PAD_SHAPE::OVAL:
111 case PAD_SHAPE::RECTANGLE:
112 case PAD_SHAPE::ROUNDRECT:
113 effort += 1;
114 break;
115
116 default:
117 break;
118 }
119 }
120
121 effort += 5;
122 } );
123
124 return effort;
125 }
126
127 default:
128 return 0;
129 }
130 };
131
132 auto checkPadAnnularWidth =
133 []( PAD* pad, PCB_LAYER_ID aLayer, DRC_CONSTRAINT& constraint,
134 const std::vector<const PAD*>& sameNumPads,
135 int* aMinAnnularWidth, int* aMaxAnnularWidth )
136 {
137 int annularWidth = 0;
138 bool handled = false;
139
140 if( pad->GetOffset( aLayer ) == VECTOR2I( 0, 0 ) )
141 {
142 VECTOR2I padSize = pad->GetSize( aLayer );
143
144 switch( pad->GetShape( aLayer ) )
145 {
146 case PAD_SHAPE::CIRCLE:
147 annularWidth = ( padSize.x - pad->GetDrillSizeX() ) / 2;
148
149 // If there are more pads with the same number then we'll still need to
150 // run the more generalised checks below.
151 handled = sameNumPads.empty();
152
153 break;
154
155 case PAD_SHAPE::CHAMFERED_RECT:
156 if( pad->GetChamferRectRatio( aLayer ) > 0.30 )
157 break;
158
160
161 case PAD_SHAPE::OVAL:
162 case PAD_SHAPE::RECTANGLE:
163 case PAD_SHAPE::ROUNDRECT:
164 annularWidth = std::min( padSize.x - pad->GetDrillSizeX(),
165 padSize.y - pad->GetDrillSizeY() ) / 2;
166
167 // If there are more pads with the same number then we'll still need to
168 // run the more generalised checks below.
169 handled = sameNumPads.empty();
170
171 break;
172
173 default:
174 break;
175 }
176 }
177
178 if( !handled )
179 {
180 // Slow (but general purpose) method.
181 SEG::ecoord dist_sq;
182 SHAPE_POLY_SET padOutline;
183 std::shared_ptr<SHAPE_SEGMENT> slot = pad->GetEffectiveHoleShape();
184
185 pad->TransformShapeToPolygon( padOutline, aLayer, 0, pad->GetMaxError(), ERROR_INSIDE );
186
187 if( sameNumPads.empty() )
188 {
189 if( !padOutline.Collide( pad->GetPosition() ) )
190 {
191 // Hole outside pad
192 annularWidth = 0;
193 }
194 else
195 {
196 // Disable is-inside test in SquaredDistance
197 padOutline.Outline( 0 ).SetClosed( false );
198
199 dist_sq = padOutline.SquaredDistanceToSeg( slot->GetSeg() );
200 annularWidth = sqrt( dist_sq ) - slot->GetWidth() / 2;
201 }
202 }
203 else if( constraint.Value().HasMin()
204 && ( annularWidth < constraint.Value().Min() ) )
205 {
206 SHAPE_POLY_SET aggregatePadOutline = padOutline;
207 SHAPE_POLY_SET otherPadHoles;
208 SHAPE_POLY_SET slotPolygon;
209
210 slot->TransformToPolygon( slotPolygon, 0, ERROR_INSIDE );
211
212 for( const PAD* sameNumPad : sameNumPads )
213 {
214 // Construct the full pad with outline and hole.
215 sameNumPad->TransformShapeToPolygon( aggregatePadOutline, PADSTACK::ALL_LAYERS,
216 0, pad->GetMaxError(), ERROR_OUTSIDE );
217
218 sameNumPad->TransformHoleToPolygon( otherPadHoles, 0, pad->GetMaxError(),
219 ERROR_INSIDE );
220 }
221
222 aggregatePadOutline.BooleanSubtract( otherPadHoles );
223
224 if( !aggregatePadOutline.Collide( pad->GetPosition() ) )
225 {
226 // Hole outside pad
227 annularWidth = 0;
228 }
229 else
230 {
231 // Disable is-inside test in SquaredDistance
232 for( int ii = 0; ii < aggregatePadOutline.OutlineCount(); ++ii )
233 aggregatePadOutline.Outline( ii ).SetClosed( false );
234
235 dist_sq = aggregatePadOutline.SquaredDistanceToSeg( slot->GetSeg() );
236 annularWidth = sqrt( dist_sq ) - slot->GetWidth() / 2;
237 }
238 }
239 }
240
241 *aMaxAnnularWidth = std::max( *aMaxAnnularWidth, annularWidth );
242 *aMinAnnularWidth = std::min( *aMinAnnularWidth, annularWidth );
243 };
244
245 auto checkAnnularWidth =
246 [&]( BOARD_ITEM* item ) -> bool
247 {
249 return false;
250
251 auto constraint = m_drcEngine->EvalRules( ANNULAR_WIDTH_CONSTRAINT, item, nullptr,
253
254 int minAnnularWidth = INT_MAX;
255 int maxAnnularWidth = 0;
256 int v_min = 0;
257 int v_max = 0;
258 bool fail_min = false;
259 bool fail_max = false;
260
261 switch( item->Type() )
262 {
263 case PCB_VIA_T:
264 {
265 PCB_VIA* via = static_cast<PCB_VIA*>( item );
266 int drill = via->GetDrillValue();
267
268 via->Padstack().ForEachUniqueLayer(
269 [&]( PCB_LAYER_ID aLayer )
270 {
271 int layerWidth = ( via->GetWidth( aLayer ) - drill ) / 2;
272 minAnnularWidth = std::min( minAnnularWidth, layerWidth );
273 maxAnnularWidth = std::max( maxAnnularWidth, layerWidth );
274 } );
275 break;
276 }
277
278 case PCB_PAD_T:
279 {
280 PAD* pad = static_cast<PAD*>( item );
281
282 if( !pad->HasHole() || pad->GetAttribute() != PAD_ATTRIB::PTH )
283 return true;
284
285 std::vector<const PAD*> sameNumPads;
286
287 if( const FOOTPRINT* fp = static_cast<const FOOTPRINT*>( pad->GetParent() ) )
288 sameNumPads = fp->GetPads( pad->GetNumber(), pad );
289
290 pad->Padstack().ForEachUniqueLayer(
291 [&]( PCB_LAYER_ID aLayer )
292 {
293 checkPadAnnularWidth( pad, aLayer, constraint, sameNumPads,
294 &minAnnularWidth, &maxAnnularWidth );
295 } );
296
297 break;
298 }
299
300 default:
301 return true;
302 }
303
304 if( constraint.GetSeverity() == RPT_SEVERITY_IGNORE )
305 return true;
306
307 if( constraint.Value().HasMin() )
308 {
309 v_min = constraint.Value().Min();
310 fail_min = minAnnularWidth < v_min;
311 }
312
313 if( constraint.Value().HasMax() )
314 {
315 v_max = constraint.Value().Max();
316 fail_max = maxAnnularWidth > v_max;
317 }
318
319 if( fail_min || fail_max )
320 {
321 std::shared_ptr<DRC_ITEM> drcItem = DRC_ITEM::Create( DRCE_ANNULAR_WIDTH );
322 wxString msg;
323
324 if( fail_min )
325 {
326 msg = formatMsg( _( "(%s min annular width %s; actual %s)" ),
327 constraint.GetName(),
328 v_min,
329 minAnnularWidth );
330 }
331
332 if( fail_max )
333 {
334 msg = formatMsg( _( "(%s max annular width %s; actual %s)" ),
335 constraint.GetName(),
336 v_max,
337 maxAnnularWidth );
338 }
339
340 drcItem->SetErrorMessage( drcItem->GetErrorText() + wxS( " " ) + msg );
341 drcItem->SetItems( item );
342 drcItem->SetViolatingRule( constraint.GetParentRule() );
343
344 reportViolation( drcItem, item->GetPosition(), item->GetLayer() );
345 }
346
347 return true;
348 };
349
350 BOARD* board = m_drcEngine->GetBoard();
351 size_t ii = 0;
352 size_t total = 0;
353
354 for( PCB_TRACK* item : board->Tracks() )
355 total += calcEffort( item );
356
357 for( FOOTPRINT* footprint : board->Footprints() )
358 {
359 for( PAD* pad : footprint->Pads() )
360 total += calcEffort( pad );
361 }
362
363 for( PCB_TRACK* item : board->Tracks() )
364 {
365 ii += calcEffort( item );
366
367 if( !reportProgress( ii, total, progressDelta ) )
368 return false; // DRC cancelled
369
370 if( !checkAnnularWidth( item ) )
371 break;
372 }
373
374 for( FOOTPRINT* footprint : board->Footprints() )
375 {
376 for( PAD* pad : footprint->Pads() )
377 {
378 ii += calcEffort( pad );
379
380 if( !reportProgress( ii, total, progressDelta ) )
381 return false; // DRC cancelled
382
383 if( !checkAnnularWidth( pad ) )
384 break;
385 }
386 }
387
388 return !m_drcEngine->IsCancelled();
389}
390
391
392namespace detail
393{
395}
@ 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:317
const FOOTPRINTS & Footprints() const
Definition: board.h:358
const TRACKS & Tracks() const
Definition: board.h:356
BOARD * GetBoard() const
Definition: drc_engine.h:100
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:706
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:393
virtual bool Run() override
Run this provider against the given PCB with configured options (if any).
virtual ~DRC_TEST_PROVIDER_ANNULAR_WIDTH()=default
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...
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
wxString formatMsg(const wxString &aFormatString, const wxString &aSource, double aConstraint, double aActual, EDA_DATA_TYPE aDataType=EDA_DATA_TYPE::DISTANCE)
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:145
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 REPORT_AUX(s)
#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