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 int maxError = pad->GetBoard()->GetDesignSettings().m_MaxError;
182 SEG::ecoord dist_sq;
183 SHAPE_POLY_SET padOutline;
184 std::shared_ptr<SHAPE_SEGMENT> slot = pad->GetEffectiveHoleShape();
185
186 pad->TransformShapeToPolygon( padOutline, aLayer, 0, maxError, ERROR_INSIDE );
187
188 if( sameNumPads.empty() )
189 {
190 if( !padOutline.Collide( pad->GetPosition() ) )
191 {
192 // Hole outside pad
193 annularWidth = 0;
194 }
195 else
196 {
197 // Disable is-inside test in SquaredDistance
198 padOutline.Outline( 0 ).SetClosed( false );
199
200 dist_sq = padOutline.SquaredDistanceToSeg( slot->GetSeg() );
201 annularWidth = sqrt( dist_sq ) - slot->GetWidth() / 2;
202 }
203 }
204 else if( constraint.Value().HasMin()
205 && ( annularWidth < constraint.Value().Min() ) )
206 {
207 SHAPE_POLY_SET aggregatePadOutline = padOutline;
208 SHAPE_POLY_SET otherPadHoles;
209 SHAPE_POLY_SET slotPolygon;
210
211 slot->TransformToPolygon( slotPolygon, 0, ERROR_INSIDE );
212
213 for( const PAD* sameNumPad : sameNumPads )
214 {
215 // Construct the full pad with outline and hole.
216 sameNumPad->TransformShapeToPolygon( aggregatePadOutline,
218 maxError, ERROR_OUTSIDE );
219
220 sameNumPad->TransformHoleToPolygon( otherPadHoles, 0, maxError,
221 ERROR_INSIDE );
222 }
223
224 aggregatePadOutline.BooleanSubtract( otherPadHoles );
225
226 if( !aggregatePadOutline.Collide( pad->GetPosition() ) )
227 {
228 // Hole outside pad
229 annularWidth = 0;
230 }
231 else
232 {
233 // Disable is-inside test in SquaredDistance
234 for( int ii = 0; ii < aggregatePadOutline.OutlineCount(); ++ii )
235 aggregatePadOutline.Outline( ii ).SetClosed( false );
236
237 dist_sq = aggregatePadOutline.SquaredDistanceToSeg( slot->GetSeg() );
238 annularWidth = sqrt( dist_sq ) - slot->GetWidth() / 2;
239 }
240 }
241 }
242
243 *aMaxAnnularWidth = std::max( *aMaxAnnularWidth, annularWidth );
244 *aMinAnnularWidth = std::min( *aMinAnnularWidth, annularWidth );
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 switch( item->Type() )
264 {
265 case PCB_VIA_T:
266 {
267 PCB_VIA* via = static_cast<PCB_VIA*>( item );
268 int drill = via->GetDrillValue();
269
270 via->Padstack().ForEachUniqueLayer(
271 [&]( PCB_LAYER_ID aLayer )
272 {
273 int layerWidth = ( via->GetWidth( aLayer ) - drill ) / 2;
274 minAnnularWidth = std::min( minAnnularWidth, layerWidth );
275 maxAnnularWidth = std::max( maxAnnularWidth, layerWidth );
276 } );
277 break;
278 }
279
280 case PCB_PAD_T:
281 {
282 PAD* pad = static_cast<PAD*>( item );
283
284 if( !pad->HasHole() || pad->GetAttribute() != PAD_ATTRIB::PTH )
285 return true;
286
287 std::vector<const PAD*> sameNumPads;
288
289 if( const FOOTPRINT* fp = static_cast<const FOOTPRINT*>( pad->GetParent() ) )
290 sameNumPads = fp->GetPads( pad->GetNumber(), pad );
291
292 pad->Padstack().ForEachUniqueLayer(
293 [&]( PCB_LAYER_ID aLayer )
294 {
295 checkPadAnnularWidth( pad, aLayer, constraint, sameNumPads,
296 &minAnnularWidth, &maxAnnularWidth );
297 } );
298
299 break;
300 }
301
302 default:
303 return true;
304 }
305
306 if( constraint.GetSeverity() == RPT_SEVERITY_IGNORE )
307 return true;
308
309 if( constraint.Value().HasMin() )
310 {
311 v_min = constraint.Value().Min();
312 fail_min = minAnnularWidth < v_min;
313 }
314
315 if( constraint.Value().HasMax() )
316 {
317 v_max = constraint.Value().Max();
318 fail_max = maxAnnularWidth > v_max;
319 }
320
321 if( fail_min || fail_max )
322 {
323 std::shared_ptr<DRC_ITEM> drcItem = DRC_ITEM::Create( DRCE_ANNULAR_WIDTH );
324 wxString msg;
325
326 if( fail_min )
327 {
328 msg = formatMsg( _( "(%s min annular width %s; actual %s)" ),
329 constraint.GetName(),
330 v_min,
331 minAnnularWidth );
332 }
333
334 if( fail_max )
335 {
336 msg = formatMsg( _( "(%s max annular width %s; actual %s)" ),
337 constraint.GetName(),
338 v_max,
339 maxAnnularWidth );
340 }
341
342 drcItem->SetErrorMessage( drcItem->GetErrorText() + wxS( " " ) + msg );
343 drcItem->SetItems( item );
344 drcItem->SetViolatingRule( constraint.GetParentRule() );
345
346 reportViolation( drcItem, item->GetPosition(), item->GetLayer() );
347 }
348
349 return true;
350 };
351
352 BOARD* board = m_drcEngine->GetBoard();
353 size_t ii = 0;
354 size_t total = 0;
355
356 for( PCB_TRACK* item : board->Tracks() )
357 total += calcEffort( item );
358
359 for( FOOTPRINT* footprint : board->Footprints() )
360 {
361 for( PAD* pad : footprint->Pads() )
362 total += calcEffort( pad );
363 }
364
365 for( PCB_TRACK* item : board->Tracks() )
366 {
367 ii += calcEffort( item );
368
369 if( !reportProgress( ii, total, progressDelta ) )
370 return false; // DRC cancelled
371
372 if( !checkAnnularWidth( item ) )
373 break;
374 }
375
376 for( FOOTPRINT* footprint : board->Footprints() )
377 {
378 for( PAD* pad : footprint->Pads() )
379 {
380 ii += calcEffort( pad );
381
382 if( !reportProgress( ii, total, progressDelta ) )
383 return false; // DRC cancelled
384
385 if( !checkAnnularWidth( pad ) )
386 break;
387 }
388 }
389
390 return !m_drcEngine->IsCancelled();
391}
392
393
394namespace detail
395{
397}
@ 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:314
const FOOTPRINTS & Footprints() const
Definition: board.h:355
const TRACKS & Tracks() const
Definition: board.h:353
BOARD * GetBoard() const
Definition: drc_engine.h:95
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:693
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: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 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