KiCad PCB EDA Suite
Loading...
Searching...
No Matches
drc_test_provider_library_parity.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 <kiway.h>
25#include <macros.h>
27#include <fp_lib_table.h>
28#include <board.h>
29#include <pcb_shape.h>
30#include <zone.h>
31#include <footprint.h>
32#include <pad.h>
33#include <drc/drc_engine.h>
34#include <drc/drc_item.h>
36#include <project_pcb.h>
37
38/*
39 Library parity test.
40
41 Errors generated:
42 - DRCE_LIB_FOOTPRINT_ISSUES
43 - DRCE_LIB_FOOTPRINT_MISMATCH
44*/
45
47{
48public:
50 {
51 m_isRuleDriven = false;
52 }
53
55 {
56 }
57
58 virtual bool Run() override;
59
60 virtual const wxString GetName() const override
61 {
62 return wxT( "library_parity" );
63 };
64
65 virtual const wxString GetDescription() const override
66 {
67 return wxT( "Performs board footprint vs library integity checks" );
68 }
69};
70
71
72//
73// The TEST*() macros have two modes:
74// In "Report" mode (aReporter != nullptr) all properties are checked and reported on.
75// In "DRC" mode (aReporter == nulltpr) properties are only checked until a difference is found.
76//
77#define TEST( a, b, msg ) \
78 do { \
79 if( a != b ) \
80 { \
81 diff = true; \
82 \
83 if( aReporter && wxString( msg ).length() ) \
84 aReporter->Report( msg ); \
85 } \
86 \
87 if( diff && !aReporter ) \
88 return diff; \
89 } while (0)
90
91#define EPSILON 2
92#define TEST_PT( a, b, msg ) \
93 do { \
94 if( abs( a.x - b.x ) > EPSILON \
95 || abs( a.y - b.y ) > EPSILON ) \
96 { \
97 diff = true; \
98 \
99 if( aReporter && wxString( msg ).length() ) \
100 aReporter->Report( msg ); \
101 } \
102 \
103 if( diff && !aReporter ) \
104 return diff; \
105 } while (0)
106
107#define EPSILON_D 0.000002
108#define TEST_D( a, b, msg ) \
109 do { \
110 if( abs( a - b ) > EPSILON_D ) \
111 { \
112 diff = true; \
113 \
114 if( aReporter && wxString( msg ).length() ) \
115 aReporter->Report( msg ); \
116 } \
117 \
118 if( diff && !aReporter ) \
119 return diff; \
120 } while (0)
121
122#define ITEM_DESC( item ) ( item )->GetItemDescription( &g_unitsProvider, true )
123#define PAD_DESC( pad ) wxString::Format( _( "Pad %s" ), ( pad )->GetNumber() )
124
125
127
128
129bool primitiveNeedsUpdate( const std::shared_ptr<PCB_SHAPE>& a,
130 const std::shared_ptr<PCB_SHAPE>& b )
131{
132 REPORTER* aReporter = nullptr;
133 bool diff = false;
134
135 TEST( a->GetShape(), b->GetShape(), "" );
136
137 switch( a->GetShape() )
138 {
140 {
141 BOX2I aRect( a->GetStart(), a->GetEnd() - a->GetStart() );
142 BOX2I bRect( b->GetStart(), b->GetEnd() - b->GetStart() );
143
144 aRect.Normalize();
145 bRect.Normalize();
146
147 TEST_PT( aRect.GetOrigin(), bRect.GetOrigin(), "" );
148 TEST_PT( aRect.GetEnd(), bRect.GetEnd(), "" );
149 break;
150 }
151
152 case SHAPE_T::SEGMENT:
153 case SHAPE_T::CIRCLE:
154 TEST_PT( a->GetStart(), b->GetStart(), "" );
155 TEST_PT( a->GetEnd(), b->GetEnd(), "" );
156 break;
157
158 case SHAPE_T::ARC:
159 TEST_PT( a->GetStart(), b->GetStart(), "" );
160 TEST_PT( a->GetEnd(), b->GetEnd(), "" );
161
162 // Arc center is calculated and so may have round-off errors when parents are
163 // differentially rotated.
164 if( ( a->GetCenter() - b->GetCenter() ).EuclideanNorm() > pcbIUScale.mmToIU( 0.0005 ) )
165 return true;
166
167 break;
168
169 case SHAPE_T::BEZIER:
170 TEST_PT( a->GetStart(), b->GetStart(), "" );
171 TEST_PT( a->GetEnd(), b->GetEnd(), "" );
172 TEST_PT( a->GetBezierC1(), b->GetBezierC1(), "" );
173 TEST_PT( a->GetBezierC2(), b->GetBezierC2(), "" );
174 break;
175
176 case SHAPE_T::POLY:
177 TEST( a->GetPolyShape().TotalVertices(), b->GetPolyShape().TotalVertices(), "" );
178
179 for( int ii = 0; ii < a->GetPolyShape().TotalVertices(); ++ii )
180 TEST_PT( a->GetPolyShape().CVertex( ii ), b->GetPolyShape().CVertex( ii ), "" );
181
182 break;
183
184 default:
185 UNIMPLEMENTED_FOR( a->SHAPE_T_asString() );
186 }
187
188 TEST( a->GetStroke(), b->GetStroke(), "" );
189 TEST( a->IsFilled(), b->IsFilled(), "" );
190
191 return diff;
192}
193
194
195bool padHasOverrides( const PAD* a, const PAD* b, REPORTER& aReporter )
196{
197 bool diff = false;
198
199#define REPORT_MSG( s, p ) aReporter.Report( wxString::Format( s, p ) )
200
201 if( a->GetLocalClearance().has_value() && a->GetLocalClearance() != b->GetLocalClearance() )
202 {
203 diff = true;
204 REPORT_MSG( _( "%s has clearance override." ), PAD_DESC( a ) );
205 }
206
207 if( a->GetLocalSolderMaskMargin().has_value()
209 {
210 diff = true;
211 REPORT_MSG( _( "%s has solder mask expansion override." ), PAD_DESC( a ) );
212 }
213
214
215 if( a->GetLocalSolderPasteMargin().has_value()
217 {
218 diff = true;
219 REPORT_MSG( _( "%s has solder paste clearance override." ), PAD_DESC( a ) );
220 }
221
224 {
225 diff = true;
226 REPORT_MSG( _( "%s has solder paste clearance override." ), PAD_DESC( a ) );
227 }
228
231 {
232 diff = true;
233 REPORT_MSG( _( "%s has zone connection override." ), PAD_DESC( a ) );
234 }
235
236 if( a->GetLocalThermalGapOverride().has_value()
237 && a->GetThermalGap() != b->GetThermalGap() )
238 {
239 diff = true;
240 REPORT_MSG( _( "%s has thermal relief gap override." ), PAD_DESC( a ) );
241 }
242
243 if( a->GetLocalThermalSpokeWidthOverride().has_value()
245 {
246 diff = true;
247 REPORT_MSG( _( "%s has thermal relief spoke width override." ), PAD_DESC( a ) );
248 }
249
251 {
252 diff = true;
253 REPORT_MSG( _( "%s has thermal relief spoke angle override." ), PAD_DESC( a ) );
254 }
255
257 {
258 diff = true;
259 REPORT_MSG( _( "%s has zone knockout setting override." ), PAD_DESC( a ) );
260 }
261
262 return diff;
263}
264
265
266bool padNeedsUpdate( const PAD* a, const PAD* b, REPORTER* aReporter )
267{
268 bool diff = false;
269
271 wxString::Format( _( "%s pad to die length differs." ), PAD_DESC( a ) ) );
273 wxString::Format( _( "%s position differs." ), PAD_DESC( a ) ) );
274
275 TEST( a->GetNumber(), b->GetNumber(),
276 wxString::Format( _( "%s has different numbers." ), PAD_DESC( a ) ) );
277
278 // These are assigned from the schematic and not from the library
279 // TEST( a->GetPinFunction(), b->GetPinFunction() );
280 // TEST( a->GetPinType(), b->GetPinType() );
281
282 bool layerSettingsDiffer = a->GetRemoveUnconnected() != b->GetRemoveUnconnected();
283
284 // NB: KeepTopBottom is undefined if RemoveUnconnected is NOT set.
285 if( a->GetRemoveUnconnected() )
286 layerSettingsDiffer |= a->GetKeepTopBottom() != b->GetKeepTopBottom();
287
288 // Trim layersets to the current board before comparing
289 LSET enabledLayers = a->GetBoard() ? a->GetBoard()->GetEnabledLayers() : LSET::AllLayersMask();
290 LSET aLayers = a->GetLayerSet() & enabledLayers;
291 LSET bLayers = b->GetLayerSet() & enabledLayers;
292
293 if( layerSettingsDiffer || aLayers != bLayers )
294 {
295 diff = true;
296
297 if( aReporter )
298 aReporter->Report( wxString::Format( _( "%s layers differ." ), PAD_DESC( a ) ) );
299 else
300 return true;
301 }
302
303 TEST( a->GetAttribute(), b->GetAttribute(),
304 wxString::Format( _( "%s pad type differs." ), PAD_DESC( a ) ) );
305 TEST( a->GetProperty(), b->GetProperty(),
306 wxString::Format( _( "%s fabrication property differs." ), PAD_DESC( a ) ) );
307
308 // The pad orientation, for historical reasons is the pad rotation + parent rotation.
311 wxString::Format( _( "%s orientation differs." ), PAD_DESC( a ) ) );
312
313 std::vector<PCB_LAYER_ID> layers = a->Padstack().UniqueLayers();
314 const BOARD* board = a->GetBoard();
315 wxString layerName;
316
317 for( PCB_LAYER_ID layer : layers )
318 {
319 layerName = board ? board->GetLayerName( layer ) : LayerName( layer );
320
321 TEST( a->GetShape( layer ), b->GetShape( layer ),
322 wxString::Format( _( "%s pad shape type differs on layer %s." ), PAD_DESC( a ),
323 layerName ) );
324
325 TEST( a->GetSize( layer ), b->GetSize( layer ),
326 wxString::Format( _( "%s size differs on layer %s." ), PAD_DESC( a ), layerName ) );
327
328 TEST( a->GetDelta( layer ), b->GetDelta( layer ),
329 wxString::Format( _( "%s trapezoid delta differs on layer %s." ), PAD_DESC( a ),
330 layerName ) );
331
332 TEST_D( a->GetRoundRectRadiusRatio( layer ),
333 b->GetRoundRectRadiusRatio( layer ),
334 wxString::Format( _( "%s rounded corners differ on layer %s." ), PAD_DESC( a ),
335 layerName ) );
336
337 TEST_D( a->GetChamferRectRatio( layer ),
338 b->GetChamferRectRatio( layer ),
339 wxString::Format( _( "%s chamfered corner sizes differ on layer %s." ),
340 PAD_DESC( a ), layerName ) );
341
342 TEST( a->GetChamferPositions( layer ),
343 b->GetChamferPositions( layer ),
344 wxString::Format( _( "%s chamfered corners differ on layer %s." ), PAD_DESC( a ),
345 layerName ) );
346
347 TEST_PT( a->GetOffset( layer ), b->GetOffset( layer ),
348 wxString::Format( _( "%s shape offset from hole differs on layer %s." ),
349 PAD_DESC( a ), layerName ) );
350 }
351
352 TEST( a->GetDrillShape(), b->GetDrillShape(),
353 wxString::Format( _( "%s drill shape differs." ), PAD_DESC( a ) ) );
354 TEST( a->GetDrillSize(), b->GetDrillSize(),
355 wxString::Format( _( "%s drill size differs." ), PAD_DESC( a ) ) );
356
357 // Clearance and zone connection overrides are as likely to be set at the board level as in
358 // the library.
359 //
360 // If we ignore them and someone *does* change one of them in the library, then stale
361 // footprints won't be caught.
362 //
363 // On the other hand, if we report them then boards that override at the board level are
364 // going to be VERY noisy.
365 //
366 // So we just do it when we have a reporter.
367 if( aReporter && padHasOverrides( a, b, *aReporter ) )
368 diff = true;
369
370 bool primitivesDiffer = false;
371 PCB_LAYER_ID firstDifferingLayer = UNDEFINED_LAYER;
372
374 [&]( PCB_LAYER_ID aLayer )
375 {
376 if( a->GetPrimitives( aLayer ).size() !=
377 b->GetPrimitives( aLayer ).size() )
378 {
379 primitivesDiffer = true;
380 }
381 else
382 {
383 for( size_t ii = 0; ii < a->GetPrimitives( aLayer ).size(); ++ii )
384 {
385 if( primitiveNeedsUpdate( a->GetPrimitives( aLayer )[ii],
386 b->GetPrimitives( aLayer )[ii] ) )
387 {
388 primitivesDiffer = true;
389 break;
390 }
391 }
392 }
393
394 if( primitivesDiffer && firstDifferingLayer == UNDEFINED_LAYER )
395 firstDifferingLayer = aLayer;
396 } );
397
398
399 if( primitivesDiffer )
400 {
401 diff = true;
402 layerName = board ? board->GetLayerName( firstDifferingLayer )
403 : LayerName( firstDifferingLayer );
404
405 if( aReporter )
406 aReporter->Report( wxString::Format( _( "%s shape primitives differ on layer %s." ),
407 PAD_DESC( a ), layerName ) );
408 else
409 return true;
410 }
411
412 return diff;
413}
414
415
416bool shapeNeedsUpdate( const PCB_SHAPE& curr_shape, const PCB_SHAPE& ref_shape )
417{
418 // curr_shape and ref_shape are expected to be normalized, for a more reliable test.
419 REPORTER* aReporter = nullptr;
420 bool diff = false;
421
422 TEST( curr_shape.GetShape(), ref_shape.GetShape(), "" );
423
424 switch( curr_shape.GetShape() )
425 {
427 {
428 BOX2I aRect( curr_shape.GetStart(), curr_shape.GetEnd() - curr_shape.GetStart() );
429 BOX2I bRect( ref_shape.GetStart(), ref_shape.GetEnd() - ref_shape.GetStart() );
430
431 aRect.Normalize();
432 bRect.Normalize();
433
434 TEST_PT( aRect.GetOrigin(), bRect.GetOrigin(), "" );
435 TEST_PT( aRect.GetEnd(), bRect.GetEnd(), "" );
436 break;
437 }
438
439 case SHAPE_T::SEGMENT:
440 case SHAPE_T::CIRCLE:
441 TEST_PT( curr_shape.GetStart(), ref_shape.GetStart(), "" );
442 TEST_PT( curr_shape.GetEnd(), ref_shape.GetEnd(), "" );
443 break;
444
445 case SHAPE_T::ARC:
446 TEST_PT( curr_shape.GetStart(), ref_shape.GetStart(), "" );
447 TEST_PT( curr_shape.GetEnd(), ref_shape.GetEnd(), "" );
448
449 // Arc center is calculated and so may have round-off errors when parents are
450 // differentially rotated.
451 if( ( curr_shape.GetCenter() - ref_shape.GetCenter() ).EuclideanNorm() > pcbIUScale.mmToIU( 0.0005 ) )
452 return true;
453
454 break;
455
456 case SHAPE_T::BEZIER:
457 TEST_PT( curr_shape.GetStart(), ref_shape.GetStart(), "" );
458 TEST_PT( curr_shape.GetEnd(), ref_shape.GetEnd(), "" );
459 TEST_PT( curr_shape.GetBezierC1(), ref_shape.GetBezierC1(), "" );
460 TEST_PT( curr_shape.GetBezierC2(), ref_shape.GetBezierC2(), "" );
461 break;
462
463 case SHAPE_T::POLY:
464 TEST( curr_shape.GetPolyShape().TotalVertices(), ref_shape.GetPolyShape().TotalVertices(), "" );
465
466 for( int ii = 0; ii < curr_shape.GetPolyShape().TotalVertices(); ++ii )
467 TEST_PT( curr_shape.GetPolyShape().CVertex( ii ), ref_shape.GetPolyShape().CVertex( ii ), "" );
468
469 break;
470
471 default:
472 UNIMPLEMENTED_FOR( curr_shape.SHAPE_T_asString() );
473 }
474
475 if( curr_shape.IsOnCopperLayer() )
476 TEST( curr_shape.GetStroke(), ref_shape.GetStroke(), "" );
477
478 TEST( curr_shape.IsFilled(), ref_shape.IsFilled(), "" );
479
480 TEST( curr_shape.GetLayer(), ref_shape.GetLayer(), "" );
481
482 return diff;
483}
484
485
486bool zoneNeedsUpdate( const ZONE* a, const ZONE* b, REPORTER* aReporter )
487{
488 bool diff = false;
489
491 wxString::Format( _( "%s corner smoothing setting differs." ), ITEM_DESC( a ) ) );
493 wxString::Format( _( "%s corner smoothing radius differs." ), ITEM_DESC( a ) ) );
494 TEST( a->GetZoneName(), b->GetZoneName(),
495 wxString::Format( _( "%s name differs." ), ITEM_DESC( a ) ) );
497 wxString::Format( _( "%s priority differs." ), ITEM_DESC( a ) ) );
498
499 TEST( a->GetIsRuleArea(), b->GetIsRuleArea(),
500 wxString::Format( _( "%s keep-out property differs." ), ITEM_DESC( a ) ) );
502 wxString::Format( _( "%s keep out copper fill setting differs." ), ITEM_DESC( a ) ) );
504 wxString::Format( _( "%s keep out footprints setting differs." ), ITEM_DESC( a ) ) );
506 wxString::Format( _( "%s keep out pads setting differs." ), ITEM_DESC( a ) ) );
508 wxString::Format( _( "%s keep out tracks setting differs." ), ITEM_DESC( a ) ) );
510 wxString::Format( _( "%s keep out vias setting differs." ), ITEM_DESC( a ) ) );
511
512 TEST( a->GetLayerSet(), b->GetLayerSet(),
513 wxString::Format( _( "%s layers differ." ), ITEM_DESC( a ) ) );
514
516 wxString::Format( _( "%s pad connection property differs." ), ITEM_DESC( a ) ) );
518 wxString::Format( _( "%s local clearance differs." ), ITEM_DESC( a ) ) );
520 wxString::Format( _( "%s thermal relief gap differs." ), ITEM_DESC( a ) ) );
522 wxString::Format( _( "%s thermal relief spoke width differs." ), ITEM_DESC( a ) ) );
523
525 wxString::Format( _( "%s min thickness differs." ), ITEM_DESC( a ) ) );
526
528 wxString::Format( _( "%s remove islands setting differs." ), ITEM_DESC( a ) ) );
530 wxString::Format( _( "%s minimum island size setting differs." ), ITEM_DESC( a ) ) );
531
532 TEST( a->GetFillMode(), b->GetFillMode(),
533 wxString::Format( _( "%s fill type differs." ), ITEM_DESC( a ) ) );
535 wxString::Format( _( "%s hatch width differs." ), ITEM_DESC( a ) ) );
536 TEST( a->GetHatchGap(), b->GetHatchGap(),
537 wxString::Format( _( "%s hatch gap differs." ), ITEM_DESC( a ) ) );
539 wxString::Format( _( "%s hatch orientation differs." ), ITEM_DESC( a ) ) );
541 wxString::Format( _( "%s hatch smoothing level differs." ), ITEM_DESC( a ) ) );
543 wxString::Format( _( "%s hatch smoothing amount differs." ), ITEM_DESC( a ) ) );
545 wxString::Format( _( "%s minimum hatch hole setting differs." ), ITEM_DESC( a ) ) );
546
547 // This is just a display property
548 // TEST( a->GetHatchBorderAlgorithm(), b->GetHatchBorderAlgorithm() );
549
551 wxString::Format( _( "%s outline corner count differs." ), ITEM_DESC( a ) ) );
552
553 bool cornersDiffer = false;
554
555 for( int ii = 0; ii < a->Outline()->TotalVertices(); ++ii )
556 {
557 if( a->Outline()->CVertex( ii ) != b->Outline()->CVertex( ii ) )
558 {
559 diff = true;
560 cornersDiffer = true;
561 break;
562 }
563 }
564
565 if( cornersDiffer && aReporter )
566 aReporter->Report( wxString::Format( _( "%s corners differ." ), ITEM_DESC( a ) ) );
567
568 return diff;
569}
570
571
572bool FOOTPRINT::FootprintNeedsUpdate( const FOOTPRINT* aLibFP, int aCompareFlags,
573 REPORTER* aReporter )
574{
575 UNITS_PROVIDER unitsProvider( pcbIUScale, EDA_UNITS::MILLIMETRES );
576
577 wxASSERT( aLibFP );
578 bool diff = false;
579
580 // To avoid issues when comparing the footprint on board and the footprint in library
581 // use the footprint from lib flipped, rotated and at same position as this.
582 // And using the footprint from lib with same changes as this minimize the issues
583 // due to rounding and shape modifications
584
585 std::unique_ptr<FOOTPRINT> temp( static_cast<FOOTPRINT*>( aLibFP->Clone() ) );
586 temp->SetParentGroup( nullptr );
587
588 temp->SetParent( GetBoard() ); // Needed to know the copper layer count;
589
590 if( IsFlipped() != temp->IsFlipped() )
591 temp->Flip( { 0, 0 }, FLIP_DIRECTION::TOP_BOTTOM );
592
593 if( GetOrientation() != temp->GetOrientation() )
594 temp->SetOrientation( GetOrientation() );
595
596 if( GetPosition() != temp->GetPosition() )
597 temp->SetPosition( GetPosition() );
598
599 for( BOARD_ITEM* item : temp->GraphicalItems() )
600 item->NormalizeForCompare();
601
602 // This temporary footprint must not have a parent when it goes out of scope because it
603 // must not trigger the IncrementTimestamp call in ~FOOTPRINT.
604 temp->SetParent( nullptr );
605
606 aLibFP = temp.get();
607
608#define TEST_ATTR( a, b, attr, msg ) TEST( ( a & attr ), ( b & attr ), msg )
609
611 _( "Footprint types differ." ) );
612
614 wxString::Format( _( "'%s' settings differ." ),
615 _( "Allow bridged solder mask apertures between pads" ) ) );
616
618 wxString::Format( _( "'%s' settings differ." ),
619 _( "Exempt From Courtyard Requirement" ) ) );
620
621 if( !( aCompareFlags & COMPARE_FLAGS::DRC ) )
622 {
623 // These tests are skipped for DRC: they are presumed to relate to a given design.
625 wxString::Format( _( "'%s' settings differ." ),
626 _( "Not in schematic" ) ) );
627
629 wxString::Format( _( "'%s' settings differ." ),
630 _( "Exclude from position files" ) ) );
631
633 wxString::Format( _( "'%s' settings differ." ),
634 _( "Exclude from bill of materials" ) ) );
635
637 wxString::Format( _( "'%s' settings differ." ),
638 _( "Do not populate" ) ) );
639 }
640
641 // Clearance and zone connection overrides are as likely to be set at the board level as in
642 // the library.
643 //
644 // If we ignore them and someone *does* change one of them in the library, then stale
645 // footprints won't be caught.
646 //
647 // On the other hand, if we report them then boards that override at the board level are
648 // going to be VERY noisy.
649 //
650 // For now we report them if there's a reporter, but we DON'T generate DRC errors on them.
651 if( aReporter )
652 {
653 if( GetLocalClearance().has_value() && GetLocalClearance() != aLibFP->GetLocalClearance() )
654 {
655 diff = true;
656 aReporter->Report( _( "Pad clearance overridden." ) );
657 }
658
659 if( GetLocalSolderMaskMargin().has_value()
661 {
662 diff = true;
663 aReporter->Report( _( "Solder mask expansion overridden." ) );
664 }
665
666
667 if( GetLocalSolderPasteMargin().has_value()
669 {
670 diff = true;
671 aReporter->Report( _( "Solder paste absolute clearance overridden." ) );
672 }
673
676 {
677 diff = true;
678 aReporter->Report( _( "Solder paste relative clearance overridden." ) );
679 }
680
681 if( GetLocalZoneConnection() != ZONE_CONNECTION::INHERITED
683 {
684 diff = true;
685 aReporter->Report( _( "Zone connection overridden." ) );
686 }
687 }
688
689 TEST( GetNetTiePadGroups().size(), aLibFP->GetNetTiePadGroups().size(),
690 _( "Net tie pad groups differ." ) );
691
692 for( size_t ii = 0; ii < GetNetTiePadGroups().size(); ++ii )
693 {
694 TEST( GetNetTiePadGroups()[ii], aLibFP->GetNetTiePadGroups()[ii],
695 _( "Net tie pad groups differ." ) );
696 }
697
698#define REPORT( msg ) { if( aReporter ) aReporter->Report( msg ); }
699#define CHECKPOINT { if( diff && !aReporter ) return diff; }
700
701 // Text items are really problematic. We don't want to test the reference, but after that
702 // it gets messy.
703 //
704 // What about the value? Depends on whether or not it's a singleton part.
705 //
706 // And what about other texts? They might be added only to instances on the board, or even
707 // changed for instances on the board. Or they might want to be tested for equality.
708 //
709 // Currently we punt and ignore all the text items.
710
711 // Drawings and pads are also somewhat problematic as there's no guarantee that they'll be
712 // in the same order in the two footprints. Rather than building some sophisticated hashing
713 // algorithm we use the footprint sorting functions to attempt to sort them in the same
714 // order.
715
716 // However FOOTPRINT::cmp_drawings uses PCB_SHAPE coordinates and other infos, so we have
717 // already normalized graphic items in model footprint from library, so we need to normalize
718 // graphic items in the footprint to test (*this). So normalize them using a copy of this
719 FOOTPRINT dummy( *this );
720 dummy.SetParentGroup( nullptr );
721 dummy.SetParent( nullptr );
722
723 for( BOARD_ITEM* item : dummy.GraphicalItems() )
724 item->NormalizeForCompare();
725
726 std::set<BOARD_ITEM*, FOOTPRINT::cmp_drawings> aShapes;
727 std::copy_if( dummy.GraphicalItems().begin(), dummy.GraphicalItems().end(),
728 std::inserter( aShapes, aShapes.begin() ),
729 []( BOARD_ITEM* item )
730 {
731 return item->Type() == PCB_SHAPE_T;
732 } );
733
734 std::set<BOARD_ITEM*, FOOTPRINT::cmp_drawings> bShapes;
735 std::copy_if( aLibFP->GraphicalItems().begin(), aLibFP->GraphicalItems().end(),
736 std::inserter( bShapes, bShapes.begin() ),
737 []( BOARD_ITEM* item )
738 {
739 return item->Type() == PCB_SHAPE_T;
740 } );
741
742 if( aShapes.size() != bShapes.size() )
743 {
744 diff = true;
745 REPORT( _( "Graphic item count differs." ) );
746 }
747 else
748 {
749 for( auto aIt = aShapes.begin(), bIt = bShapes.begin(); aIt != aShapes.end(); aIt++, bIt++ )
750 {
751 // aShapes and bShapes are the tested footprint PCB_SHAPE and the model PCB_SHAPE.
752 // These shapes are already normalized.
753 PCB_SHAPE* curr_shape = static_cast<PCB_SHAPE*>( *aIt );
754 PCB_SHAPE* test_shape = static_cast<PCB_SHAPE*>( *bIt );
755
756 if( shapeNeedsUpdate( *curr_shape, *test_shape ) )
757 {
758 diff = true;
759 REPORT( wxString::Format( _( "%s differs." ), ITEM_DESC( *aIt ) ) );
760 }
761 }
762 }
763
765
766 std::set<PAD*, FOOTPRINT::cmp_pads> aPads( Pads().begin(), Pads().end() );
767 std::set<PAD*, FOOTPRINT::cmp_pads> bPads( aLibFP->Pads().begin(), aLibFP->Pads().end() );
768
769 if( aPads.size() != bPads.size() )
770 {
771 diff = true;
772 REPORT( _( "Pad count differs." ) );
773 }
774 else
775 {
776 for( auto aIt = aPads.begin(), bIt = bPads.begin(); aIt != aPads.end(); aIt++, bIt++ )
777 {
778 if( padNeedsUpdate( *aIt, *bIt, aReporter ) )
779 diff = true;
780 else if( aReporter && padHasOverrides( *aIt, *bIt, *aReporter ) )
781 diff = true;
782 }
783 }
784
786
787 std::set<ZONE*, FOOTPRINT::cmp_zones> aZones( Zones().begin(), Zones().end() );
788 std::set<ZONE*, FOOTPRINT::cmp_zones> bZones( aLibFP->Zones().begin(), aLibFP->Zones().end() );
789
790 if( aZones.size() != bZones.size() )
791 {
792 diff = true;
793 REPORT( _( "Rule area count differs." ) );
794 }
795 else
796 {
797 for( auto aIt = aZones.begin(), bIt = bZones.begin(); aIt != aZones.end(); aIt++, bIt++ )
798 diff |= zoneNeedsUpdate( *aIt, *bIt, aReporter );
799 }
800
801 return diff;
802}
803
804
806{
807 BOARD* board = m_drcEngine->GetBoard();
808 PROJECT* project = board->GetProject();
809
810 if( !project )
811 {
812 reportAux( _( "No project loaded, skipping library parity tests." ) );
813 return true; // Continue with other tests
814 }
815
816 if( !reportPhase( _( "Loading footprint library table..." ) ) )
817 return false; // DRC cancelled
818
819 std::map<LIB_ID, std::shared_ptr<FOOTPRINT>> libFootprintCache;
820
822 wxString msg;
823 int ii = 0;
824 const int progressDelta = 250;
825
826 if( !reportPhase( _( "Checking board footprints against library..." ) ) )
827 return false;
828
829 for( FOOTPRINT* footprint : board->Footprints() )
830 {
833 {
834 return true; // Continue with other tests
835 }
836
837 if( !reportProgress( ii++, (int) board->Footprints().size(), progressDelta ) )
838 return false; // DRC cancelled
839
840 LIB_ID fpID = footprint->GetFPID();
841 wxString libName = fpID.GetLibNickname();
842 wxString fpName = fpID.GetLibItemName();
843 const LIB_TABLE_ROW* libTableRow = nullptr;
844
845 if( libName.IsEmpty() )
846 {
847 // Not much we can do here
848 continue;
849 }
850
851 try
852 {
853 libTableRow = libTable->FindRow( libName );
854 }
855 catch( const IO_ERROR& )
856 {
857 }
858
859 if( !libTableRow )
860 {
862 {
863 std::shared_ptr<DRC_ITEM> drcItem = DRC_ITEM::Create( DRCE_LIB_FOOTPRINT_ISSUES );
864 msg.Printf( _( "The current configuration does not include the footprint library '%s'." ),
865 libName );
866 drcItem->SetErrorMessage( msg );
867 drcItem->SetItems( footprint );
868 reportViolation( drcItem, footprint->GetCenter(), UNDEFINED_LAYER );
869 }
870
871 continue;
872 }
873 else if( !libTable->HasLibrary( libName, true ) )
874 {
876 {
877 std::shared_ptr<DRC_ITEM> drcItem = DRC_ITEM::Create( DRCE_LIB_FOOTPRINT_ISSUES );
878 msg.Printf( _( "The footprint library '%s' is not enabled in the current configuration." ),
879 libName );
880 drcItem->SetErrorMessage( msg );
881 drcItem->SetItems( footprint );
882 reportViolation( drcItem, footprint->GetCenter(), UNDEFINED_LAYER );
883 }
884
885 continue;
886 }
887
888 auto cacheIt = libFootprintCache.find( fpID );
889 std::shared_ptr<FOOTPRINT> libFootprint;
890
891 if( cacheIt != libFootprintCache.end() )
892 {
893 libFootprint = cacheIt->second;
894 }
895 else
896 {
897 try
898 {
899 libFootprint.reset( libTable->FootprintLoad( libName, fpName, true ) );
900
901 if( libFootprint )
902 libFootprintCache[ fpID ] = libFootprint;
903 }
904 catch( const IO_ERROR& )
905 {
906 }
907 }
908
909 if( !libFootprint )
910 {
912 {
913 std::shared_ptr<DRC_ITEM> drcItem = DRC_ITEM::Create( DRCE_LIB_FOOTPRINT_ISSUES );
914 msg.Printf( _( "Footprint '%s' not found in library '%s'." ),
915 fpName,
916 libName );
917 drcItem->SetErrorMessage( msg );
918 drcItem->SetItems( footprint );
919 reportViolation( drcItem, footprint->GetCenter(), UNDEFINED_LAYER );
920 }
921 }
922 else if( footprint->FootprintNeedsUpdate( libFootprint.get(), BOARD_ITEM::COMPARE_FLAGS::DRC ) )
923 {
925 {
926 std::shared_ptr<DRC_ITEM> drcItem = DRC_ITEM::Create( DRCE_LIB_FOOTPRINT_MISMATCH );
927 msg.Printf( _( "Footprint '%s' does not match copy in library '%s'." ),
928 fpName,
929 libName );
930 drcItem->SetErrorMessage( msg );
931 drcItem->SetItems( footprint );
932 reportViolation( drcItem, footprint->GetCenter(), UNDEFINED_LAYER );
933 }
934 }
935 }
936
937 return true;
938}
939
940
941namespace detail
942{
944}
constexpr EDA_IU_SCALE pcbIUScale
Definition: base_units.h:108
A base class for any item which can be embedded within the BOARD container class, and therefore insta...
Definition: board_item.h:79
virtual const BOARD * GetBoard() const
Return the BOARD in which this BOARD_ITEM resides, or NULL if none.
Definition: board_item.cpp:47
VECTOR2I GetFPRelativePosition() const
Definition: board_item.cpp:327
virtual bool IsOnCopperLayer() const
Definition: board_item.h:150
Information pertinent to a Pcbnew printed circuit board.
Definition: board.h:295
LSET GetEnabledLayers() const
A proxy function that calls the corresponding function in m_BoardSettings.
Definition: board.cpp:817
const FOOTPRINTS & Footprints() const
Definition: board.h:336
const wxString GetLayerName(PCB_LAYER_ID aLayer) const
Return the name of a aLayer.
Definition: board.cpp:613
PROJECT * GetProject() const
Definition: board.h:499
constexpr const Vec GetEnd() const
Definition: box2.h:212
constexpr BOX2< Vec > & Normalize()
Ensure that the height and width are positive.
Definition: box2.h:146
constexpr const Vec & GetOrigin() const
Definition: box2.h:210
BOARD * GetBoard() const
Definition: drc_engine.h:96
bool IsErrorLimitExceeded(int error_code)
static std::shared_ptr< DRC_ITEM > Create(int aErrorCode)
Constructs a DRC_ITEM for the given error code.
Definition: drc_item.cpp:395
virtual const wxString GetDescription() const override
virtual bool Run() override
Run this provider against the given PCB with configured options (if any).
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
void reportAux(const wxString &aMsg)
virtual bool reportProgress(size_t aCount, size_t aSize, size_t aDelta=1)
EDA_ANGLE Normalize()
Definition: eda_angle.h:221
double AsDegrees() const
Definition: eda_angle.h:113
const VECTOR2I & GetBezierC2() const
Definition: eda_shape.h:213
SHAPE_POLY_SET & GetPolyShape()
Definition: eda_shape.h:291
bool IsFilled() const
Definition: eda_shape.h:98
SHAPE_T GetShape() const
Definition: eda_shape.h:132
const VECTOR2I & GetEnd() const
Return the ending point of the graphic.
Definition: eda_shape.h:174
const VECTOR2I & GetStart() const
Return the starting point of the graphic.
Definition: eda_shape.h:137
wxString SHAPE_T_asString() const
Definition: eda_shape.cpp:340
const VECTOR2I & GetBezierC1() const
Definition: eda_shape.h:210
ZONE_CONNECTION GetLocalZoneConnection() const
Definition: footprint.h:286
EDA_ANGLE GetOrientation() const
Definition: footprint.h:225
ZONES & Zones()
Definition: footprint.h:210
bool FootprintNeedsUpdate(const FOOTPRINT *aLibFP, int aCompareFlags=0, REPORTER *aReporter=nullptr)
Return true if a board footprint differs from the library version.
std::optional< int > GetLocalSolderPasteMargin() const
Definition: footprint.h:279
EDA_ITEM * Clone() const override
Invoke a function on all children.
Definition: footprint.cpp:2115
std::optional< int > GetLocalClearance() const
Definition: footprint.h:273
std::deque< PAD * > & Pads()
Definition: footprint.h:204
int GetAttributes() const
Definition: footprint.h:288
bool IsFlipped() const
Definition: footprint.h:389
const std::vector< wxString > & GetNetTiePadGroups() const
Definition: footprint.h:337
std::optional< double > GetLocalSolderPasteMarginRatio() const
Definition: footprint.h:282
std::optional< int > GetLocalSolderMaskMargin() const
Definition: footprint.h:276
VECTOR2I GetPosition() const override
Definition: footprint.h:222
DRAWINGS & GraphicalItems()
Definition: footprint.h:207
const FP_LIB_TABLE_ROW * FindRow(const wxString &aNickName, bool aCheckIfEnabled=false)
Return an FP_LIB_TABLE_ROW if aNickName is found in this table or in any chained fall back table frag...
FOOTPRINT * FootprintLoad(const wxString &aNickname, const wxString &aFootprintName, bool aKeepUUID=false)
Load a footprint having aFootprintName from the library given by aNickname.
Hold an error message and may be used when throwing exceptions containing meaningful error messages.
Definition: ki_exception.h:77
A logical library item identifier and consists of various portions much like a URI.
Definition: lib_id.h:49
const UTF8 & GetLibItemName() const
Definition: lib_id.h:102
const UTF8 & GetLibNickname() const
Return the logical library name portion of a LIB_ID.
Definition: lib_id.h:87
Hold a record identifying a library accessed by the appropriate plug in object in the LIB_TABLE.
bool HasLibrary(const wxString &aNickname, bool aCheckEnabled=false) const
Test for the existence of aNickname in the library table.
LSET is a set of PCB_LAYER_IDs.
Definition: lset.h:37
static LSET AllLayersMask()
Definition: lset.cpp:587
void ForEachUniqueLayer(const std::function< void(PCB_LAYER_ID)> &aMethod) const
Runs the given callable for each active unique copper layer in this padstack, meaning F_Cu for MODE::...
Definition: padstack.cpp:879
std::vector< PCB_LAYER_ID > UniqueLayers() const
Definition: padstack.cpp:906
Definition: pad.h:54
PAD_PROP GetProperty() const
Definition: pad.h:441
bool GetRemoveUnconnected() const
Definition: pad.h:726
LSET GetLayerSet() const override
Return a std::bitset of all layers on which the item physically resides.
Definition: pad.h:435
const std::vector< std::shared_ptr< PCB_SHAPE > > & GetPrimitives(PCB_LAYER_ID aLayer) const
Accessor to the basic shape list for custom-shaped pads.
Definition: pad.h:363
std::optional< double > GetLocalSolderPasteMarginRatio() const
Definition: pad.h:468
const VECTOR2I & GetDrillSize() const
Definition: pad.h:303
PAD_ATTRIB GetAttribute() const
Definition: pad.h:438
const wxString & GetNumber() const
Definition: pad.h:134
const VECTOR2I & GetDelta(PCB_LAYER_ID aLayer) const
Definition: pad.h:297
EDA_ANGLE GetThermalSpokeAngle() const
Definition: pad.h:617
double GetRoundRectRadiusRatio(PCB_LAYER_ID aLayer) const
Definition: pad.h:663
PAD_SHAPE GetShape(PCB_LAYER_ID aLayer) const
Definition: pad.h:193
bool GetKeepTopBottom() const
Definition: pad.h:742
std::optional< int > GetLocalClearance() const override
Return any local clearances set in the "classic" (ie: pre-rule) system.
Definition: pad.h:453
const PADSTACK & Padstack() const
Definition: pad.h:319
const VECTOR2I & GetOffset(PCB_LAYER_ID aLayer) const
Definition: pad.h:315
PADSTACK::CUSTOM_SHAPE_ZONE_MODE GetCustomShapeInZoneOpt() const
Definition: pad.h:219
PAD_DRILL_SHAPE GetDrillShape() const
Definition: pad.h:420
int GetChamferPositions(PCB_LAYER_ID aLayer) const
Definition: pad.h:703
std::optional< int > GetLocalSolderPasteMargin() const
Definition: pad.h:462
std::optional< int > GetLocalSolderMaskMargin() const
Definition: pad.h:456
EDA_ANGLE GetFPRelativeOrientation() const
Definition: pad.cpp:926
double GetChamferRectRatio(PCB_LAYER_ID aLayer) const
Definition: pad.h:686
std::optional< int > GetLocalThermalSpokeWidthOverride() const
Definition: pad.h:601
ZONE_CONNECTION GetLocalZoneConnection() const
Definition: pad.h:478
int GetThermalGap() const
Definition: pad.h:633
int GetLocalThermalGapOverride(wxString *aSource) const
Definition: pad.cpp:1263
int GetPadToDieLength() const
Definition: pad.h:451
const VECTOR2I & GetSize(PCB_LAYER_ID aLayer) const
Definition: pad.h:262
VECTOR2I GetCenter() const override
This defaults to the center of the bounding box if not overridden.
Definition: pcb_shape.h:79
STROKE_PARAMS GetStroke() const override
Definition: pcb_shape.h:89
PCB_LAYER_ID GetLayer() const override
Return the primary layer this item is on.
Definition: pcb_shape.h:69
static FP_LIB_TABLE * PcbFootprintLibs(PROJECT *aProject)
Return the table of footprint libraries without Kiway.
Definition: project_pcb.cpp:37
Container for project specific data.
Definition: project.h:64
A pure virtual class used to derive REPORTER objects from.
Definition: reporter.h:72
virtual REPORTER & Report(const wxString &aText, SEVERITY aSeverity=RPT_SEVERITY_UNDEFINED)=0
Report a string with a given severity.
int TotalVertices() const
Return total number of vertices stored in the set.
const VECTOR2I & CVertex(int aIndex, int aOutline, int aHole) const
Return the index-th vertex in a given hole outline within a given outline.
Handle a list of polygons defining a copper zone.
Definition: zone.h:73
bool GetIsRuleArea() const
Accessors to parameters used in Rule Area zones:
Definition: zone.h:724
std::optional< int > GetLocalClearance() const override
Definition: zone.cpp:717
bool GetDoNotAllowVias() const
Definition: zone.h:732
bool GetDoNotAllowPads() const
Definition: zone.h:734
bool GetDoNotAllowTracks() const
Definition: zone.h:733
ISLAND_REMOVAL_MODE GetIslandRemovalMode() const
Definition: zone.h:753
SHAPE_POLY_SET * Outline()
Definition: zone.h:340
long long int GetMinIslandArea() const
Definition: zone.h:756
const wxString & GetZoneName() const
Definition: zone.h:135
int GetMinThickness() const
Definition: zone.h:273
ZONE_CONNECTION GetPadConnection() const
Definition: zone.h:270
int GetHatchThickness() const
Definition: zone.h:288
double GetHatchHoleMinArea() const
Definition: zone.h:303
int GetThermalReliefSpokeWidth() const
Definition: zone.h:217
EDA_ANGLE GetHatchOrientation() const
Definition: zone.h:294
bool GetDoNotAllowFootprints() const
Definition: zone.h:735
ZONE_FILL_MODE GetFillMode() const
Definition: zone.h:196
virtual LSET GetLayerSet() const override
Return a std::bitset of all layers on which the item physically resides.
Definition: zone.h:133
bool GetDoNotAllowCopperPour() const
Definition: zone.h:731
int GetHatchGap() const
Definition: zone.h:291
double GetHatchSmoothingValue() const
Definition: zone.h:300
int GetHatchSmoothingLevel() const
Definition: zone.h:297
unsigned int GetCornerRadius() const
Definition: zone.h:670
int GetCornerSmoothingType() const
Definition: zone.h:666
int GetThermalReliefGap() const
Definition: zone.h:206
unsigned GetAssignedPriority() const
Definition: zone.h:123
@ DRCE_LIB_FOOTPRINT_ISSUES
Definition: drc_item.h:82
@ DRCE_LIB_FOOTPRINT_MISMATCH
Definition: drc_item.h:83
#define TEST_PT(a, b, msg)
#define PAD_DESC(pad)
UNITS_PROVIDER g_unitsProvider(pcbIUScale, EDA_UNITS::MILLIMETRES)
bool primitiveNeedsUpdate(const std::shared_ptr< PCB_SHAPE > &a, const std::shared_ptr< PCB_SHAPE > &b)
#define TEST(a, b, msg)
bool padHasOverrides(const PAD *a, const PAD *b, REPORTER &aReporter)
bool shapeNeedsUpdate(const PCB_SHAPE &curr_shape, const PCB_SHAPE &ref_shape)
bool zoneNeedsUpdate(const ZONE *a, const ZONE *b, REPORTER *aReporter)
#define TEST_ATTR(a, b, attr, msg)
#define TEST_D(a, b, msg)
bool padNeedsUpdate(const PAD *a, const PAD *b, REPORTER *aReporter)
#define CHECKPOINT
#define REPORT_MSG(s, p)
#define ITEM_DESC(item)
#define _(s)
#define TEST(a, b)
@ RECTANGLE
Use RECTANGLE instead of RECT to avoid collision in a Windows header.
@ FP_SMD
Definition: footprint.h:80
@ FP_DNP
Definition: footprint.h:87
@ FP_ALLOW_MISSING_COURTYARD
Definition: footprint.h:86
@ FP_EXCLUDE_FROM_POS_FILES
Definition: footprint.h:81
@ FP_BOARD_ONLY
Definition: footprint.h:83
@ FP_EXCLUDE_FROM_BOM
Definition: footprint.h:82
@ FP_THROUGH_HOLE
Definition: footprint.h:79
@ FP_ALLOW_SOLDERMASK_BRIDGES
Definition: footprint.h:85
wxString LayerName(int aLayer)
Returns the default display name for a given layer.
Definition: layer_id.cpp:31
PCB_LAYER_ID
A quick note on layer IDs:
Definition: layer_ids.h:60
@ UNDEFINED_LAYER
Definition: layer_ids.h:61
#define REPORT(msg)
#define ITEM_DESC(item)
This file contains miscellaneous commonly used macros and functions.
#define UNIMPLEMENTED_FOR(type)
Definition: macros.h:96
static DRC_REGISTER_TEST_PROVIDER< DRC_TEST_PROVIDER_ANNULAR_WIDTH > dummy
std::vector< FAB_LAYER_COLOR > dummy
constexpr int mmToIU(double mm) const
Definition: base_units.h:88