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:
51
53
54 virtual bool Run() override;
55
56 virtual const wxString GetName() const override { return wxT( "annular_width" ); };
57};
58
59
61{
62 if( m_drcEngine->IsErrorLimitExceeded( DRCE_ANNULAR_WIDTH ) )
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
70 if( !m_drcEngine->HasRulesForConstraintType( ANNULAR_WIDTH_CONSTRAINT ) )
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 {
104 if( pad->GetChamferRectRatio( aLayer ) > 0.30 )
105 break;
106
108
110 case PAD_SHAPE::OVAL:
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 getPadAnnulusPts =
133 []( PAD* pad, PCB_LAYER_ID aLayer, DRC_CONSTRAINT& constraint,
134 const std::vector<const PAD*>& sameNumPads, VECTOR2I* ptA, VECTOR2I* ptB )
135 {
136 bool handled = false;
137
138 if( pad->GetOffset( aLayer ) == VECTOR2I( 0, 0 ) )
139 {
140 int xDist = KiROUND( ( pad->GetSizeX() - pad->GetDrillSizeX() ) / 2.0 );
141 int yDist = KiROUND( ( pad->GetSizeY() - pad->GetDrillSizeY() ) / 2.0 );
142
143 if( yDist < xDist )
144 {
145 *ptA = pad->GetPosition() - VECTOR2I( 0, pad->GetDrillSizeY() / 2 );
146 *ptB = pad->GetPosition() - VECTOR2I( 0, pad->GetSizeY() / 2 );
147 }
148 else
149 {
150 *ptA = pad->GetPosition() - VECTOR2I( pad->GetDrillSizeX() / 2, 0 );
151 *ptB = pad->GetPosition() - VECTOR2I( pad->GetSizeX() / 2, 0 );
152 }
153
154 RotatePoint( *ptA, pad->GetPosition(), pad->GetOrientation() );
155 RotatePoint( *ptB, pad->GetPosition(), pad->GetOrientation() );
156
157 switch( pad->GetShape( aLayer ) )
158 {
160 handled = pad->GetChamferRectRatio( aLayer ) <= 0.30;
161 break;
162
164 case PAD_SHAPE::OVAL:
167 handled = true;
168
169 break;
170
171 default:
172 break;
173 }
174 }
175
176 std::vector<const PAD*> overlappingSameNumPads;
177
178 for( const PAD* p : sameNumPads )
179 {
180 if( p->IsOnLayer( aLayer )
181 && pad->GetBoundingBox().Intersects( p->GetBoundingBox() ) )
182 {
183 overlappingSameNumPads.push_back( p );
184 }
185 }
186
187 if( !handled || !overlappingSameNumPads.empty() )
188 {
189 // Slow (but general purpose) method.
190 SHAPE_POLY_SET padOutline;
191 std::shared_ptr<SHAPE_SEGMENT> slot = pad->GetEffectiveHoleShape();
192
193 pad->TransformShapeToPolygon( padOutline, aLayer, 0, pad->GetMaxError(), ERROR_INSIDE );
194
195 if( sameNumPads.empty() )
196 {
197 if( !padOutline.Collide( pad->GetPosition() ) )
198 {
199 // Hole outside pad
200 *ptA = pad->GetPosition();
201 *ptB = pad->GetPosition();
202 }
203 else
204 {
205 padOutline.NearestPoints( slot.get(), *ptA, *ptB );
206 }
207 }
208 else if( constraint.Value().HasMin() )
209 {
210 SHAPE_POLY_SET aggregatePadOutline = padOutline;
211 SHAPE_POLY_SET otherPadHoles;
212 SHAPE_POLY_SET slotPolygon;
213
214 slot->TransformToPolygon( slotPolygon, 0, ERROR_INSIDE );
215
216 for( const PAD* sameNumPad : sameNumPads )
217 {
218 // Construct the full pad with outline and hole.
219 sameNumPad->TransformShapeToPolygon( aggregatePadOutline, aLayer, 0, pad->GetMaxError(),
221
222 sameNumPad->TransformHoleToPolygon( otherPadHoles, 0, pad->GetMaxError(), ERROR_INSIDE );
223 }
224
225 aggregatePadOutline.BooleanSubtract( otherPadHoles );
226
227 if( !aggregatePadOutline.Collide( pad->GetPosition() ) )
228 {
229 // Hole outside pad
230 *ptA = pad->GetPosition();
231 *ptB = pad->GetPosition();
232 }
233 else
234 {
235 aggregatePadOutline.NearestPoints( slot.get(), *ptA, *ptB );
236 }
237 }
238 }
239 };
240
241 auto checkConstraint =
242 [&]( DRC_CONSTRAINT& constraint, BOARD_ITEM* item, const VECTOR2I& ptA, const VECTOR2I& ptB,
243 PCB_LAYER_ID aLayer )
244 {
245 if( constraint.GetSeverity() == RPT_SEVERITY_IGNORE )
246 return;
247
248 int v_min = 0;
249 int v_max = 0;
250 bool fail_min = false;
251 bool fail_max = false;
252 int width = ( ptA - ptB ).EuclideanNorm();
253
254 if( constraint.Value().HasMin() )
255 {
256 v_min = constraint.Value().Min();
257 fail_min = width < v_min;
258 }
259
260 if( constraint.Value().HasMax() )
261 {
262 v_max = constraint.Value().Max();
263 fail_max = width > v_max;
264 }
265
266 if( fail_min || fail_max )
267 {
268 std::shared_ptr<DRC_ITEM> drcItem = DRC_ITEM::Create( DRCE_ANNULAR_WIDTH );
269
270 if( fail_min )
271 {
272 drcItem->SetErrorDetail( formatMsg( _( "(%s min annular width %s; actual %s)" ),
273 constraint.GetName(),
274 v_min,
275 width ) );
276 }
277
278 if( fail_max )
279 {
280 drcItem->SetErrorDetail( formatMsg( _( "(%s max annular width %s; actual %s)" ),
281 constraint.GetName(),
282 v_max,
283 width ) );
284 }
285
286 drcItem->SetItems( item );
287 drcItem->SetViolatingRule( constraint.GetParentRule() );
288 reportTwoPointGeometry( drcItem, item->GetPosition(), ptA, ptB, aLayer );
289 }
290 };
291
292 auto checkAnnularWidth =
293 [&]( BOARD_ITEM* item ) -> bool
294 {
295 if( m_drcEngine->IsErrorLimitExceeded( DRCE_ANNULAR_WIDTH ) )
296 return false;
297
298 if( item->Type() == PCB_VIA_T )
299 {
300 PCB_VIA* via = static_cast<PCB_VIA*>( item );
301
302 via->Padstack().ForEachUniqueLayer(
303 [&]( PCB_LAYER_ID aLayer )
304 {
305 auto constraint = m_drcEngine->EvalRules( ANNULAR_WIDTH_CONSTRAINT, item,
306 nullptr, aLayer );
307
308 VECTOR2I ptA = via->GetPosition() - VECTOR2I( via->GetDrillValue() / 2, 0 );
309 VECTOR2I ptB = via->GetPosition() - VECTOR2I( via->GetWidth( aLayer ) / 2, 0 );
310 checkConstraint( constraint, via, ptA, ptB, aLayer );
311 } );
312 }
313 else if( item->Type() == PCB_PAD_T )
314 {
315 PAD* pad = static_cast<PAD*>( item );
316
317 if( !pad->HasHole() || pad->GetAttribute() != PAD_ATTRIB::PTH )
318 return true;
319
320 std::vector<const PAD*> sameNumPads;
321
322 if( const FOOTPRINT* fp = static_cast<const FOOTPRINT*>( pad->GetParent() ) )
323 sameNumPads = fp->GetPads( pad->GetNumber(), pad );
324
325 pad->Padstack().ForEachUniqueLayer(
326 [&]( PCB_LAYER_ID aLayer )
327 {
328 auto constraint = m_drcEngine->EvalRules( ANNULAR_WIDTH_CONSTRAINT, item,
329 nullptr, aLayer );
330
331 VECTOR2I ptA;
332 VECTOR2I ptB;
333 getPadAnnulusPts( pad, aLayer, constraint, sameNumPads, &ptA, &ptB );
334 checkConstraint( constraint, pad, ptA, ptB, aLayer );
335 } );
336 }
337
338 return true;
339 };
340
341 BOARD* board = m_drcEngine->GetBoard();
342 size_t ii = 0;
343 size_t total = 0;
344
345 for( PCB_TRACK* item : board->Tracks() )
346 total += calcEffort( item );
347
348 for( FOOTPRINT* footprint : board->Footprints() )
349 {
350 for( PAD* pad : footprint->Pads() )
351 total += calcEffort( pad );
352 }
353
354 for( PCB_TRACK* item : board->Tracks() )
355 {
356 ii += calcEffort( item );
357
358 if( !reportProgress( ii, total, progressDelta ) )
359 return false; // DRC cancelled
360
361 if( !checkAnnularWidth( item ) )
362 break;
363 }
364
365 for( FOOTPRINT* footprint : board->Footprints() )
366 {
367 for( PAD* pad : footprint->Pads() )
368 {
369 ii += calcEffort( pad );
370
371 if( !reportProgress( ii, total, progressDelta ) )
372 return false; // DRC cancelled
373
374 if( !checkAnnularWidth( pad ) )
375 break;
376 }
377 }
378
379 return !m_drcEngine->IsCancelled();
380}
381
382
383namespace detail
384{
386}
@ ERROR_OUTSIDE
@ ERROR_INSIDE
constexpr BOX2I KiROUND(const BOX2D &aBoxD)
Definition box2.h:990
A base class for any item which can be embedded within the BOARD container class, and therefore insta...
Definition board_item.h:84
Information pertinent to a Pcbnew printed circuit board.
Definition board.h:323
const FOOTPRINTS & Footprints() const
Definition board.h:364
const TRACKS & Tracks() const
Definition board.h:362
static std::shared_ptr< DRC_ITEM > Create(int aErrorCode)
Constructs a DRC_ITEM for the given error code.
Definition drc_item.cpp:407
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
virtual bool reportPhase(const wxString &aStageName)
void reportTwoPointGeometry(std::shared_ptr< DRC_ITEM > &aDrcItem, const VECTOR2I &aMarkerPos, const VECTOR2I &ptA, const VECTOR2I &ptB, PCB_LAYER_ID aLayer)
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)
Definition pad.h:55
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,...
void BooleanSubtract(const SHAPE_POLY_SET &b)
Perform boolean polyset difference.
bool NearestPoints(const SHAPE *aOther, VECTOR2I &aPtThis, VECTOR2I &aPtOther) const
Return the two points that mark the closest distance between this shape and aOther.
The common library.
@ DRCE_ANNULAR_WIDTH
Definition drc_item.h:59
@ ANNULAR_WIDTH_CONSTRAINT
Definition drc_rule.h:67
#define REPORT_AUX(s)
#define _(s)
PCB_LAYER_ID
A quick note on layer IDs:
Definition layer_ids.h:60
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
@ PTH
Plated through hole pad.
Definition padstack.h:98
@ CHAMFERED_RECT
Definition padstack.h:60
@ ROUNDRECT
Definition padstack.h:57
@ RECTANGLE
Definition padstack.h:54
@ RPT_SEVERITY_IGNORE
void RotatePoint(int *pX, int *pY, const EDA_ANGLE &aAngle)
Calculate the new point of coord coord pX, pY, for a rotation center 0, 0.
Definition trigo.cpp:229
@ PCB_VIA_T
class PCB_VIA, a via (like a track segment on a copper layer)
Definition typeinfo.h:94
@ PCB_PAD_T
class PAD, a pad in a footprint
Definition typeinfo.h:84
VECTOR2< int32_t > VECTOR2I
Definition vector2d.h:687