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 (C) 2021-2024 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 1
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.000001
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 )
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->GetThermalGap() != b->GetThermalGap() )
237 {
238 diff = true;
239 REPORT_MSG( _( "%s has thermal relief gap override." ), PAD_DESC( a ) );
240 }
241
243 {
244 diff = true;
245 REPORT_MSG( _( "%s has thermal relief spoke width override." ), PAD_DESC( a ) );
246 }
247
249 {
250 diff = true;
251 REPORT_MSG( _( "%s has thermal relief spoke angle override." ), PAD_DESC( a ) );
252 }
253
255 {
256 diff = true;
257 REPORT_MSG( _( "%s has zone knockout setting override." ), PAD_DESC( a ) );
258 }
259
260 return diff;
261}
262
263
264bool padNeedsUpdate( const PAD* a, const PAD* b, REPORTER* aReporter )
265{
266 bool diff = false;
267
269 wxString::Format( _( "%s pad to die length differs." ), PAD_DESC( a ) ) );
271 wxString::Format( _( "%s position differs." ), PAD_DESC( a ) ) );
272
273 TEST( a->GetNumber(), b->GetNumber(),
274 wxString::Format( _( "%s has different numbers." ), PAD_DESC( a ) ) );
275
276 // These are assigned from the schematic and not from the library
277 // TEST( a->GetPinFunction(), b->GetPinFunction() );
278 // TEST( a->GetPinType(), b->GetPinType() );
279
280 bool layerSettingsDiffer = a->GetRemoveUnconnected() != b->GetRemoveUnconnected();
281
282 // NB: KeepTopBottom is undefined if RemoveUnconnected is NOT set.
283 if( a->GetRemoveUnconnected() )
284 layerSettingsDiffer |= a->GetKeepTopBottom() != b->GetKeepTopBottom();
285
286 // Trim layersets to the current board before comparing
287 LSET enabledLayers = a->GetBoard() ? a->GetBoard()->GetEnabledLayers() : LSET::AllLayersMask();
288 LSET aLayers = a->GetLayerSet() & enabledLayers;
289 LSET bLayers = b->GetLayerSet() & enabledLayers;
290
291 if( layerSettingsDiffer || aLayers != bLayers )
292 {
293 diff = true;
294
295 if( aReporter )
296 aReporter->Report( wxString::Format( _( "%s layers differ." ), PAD_DESC( a ) ) );
297 else
298 return true;
299 }
300
301 TEST( a->GetShape(), b->GetShape(),
302 wxString::Format( _( "%s pad shape type differs." ), PAD_DESC( a ) ) );
303
304 TEST( a->GetAttribute(), b->GetAttribute(),
305 wxString::Format( _( "%s pad type differs." ), PAD_DESC( a ) ) );
306 TEST( a->GetProperty(), b->GetProperty(),
307 wxString::Format( _( "%s fabrication property differs." ), PAD_DESC( a ) ) );
308
309 // The pad orientation, for historical reasons is the pad rotation + parent rotation.
312 wxString::Format( _( "%s orientation differs." ), PAD_DESC( a ) ) );
313
314 TEST( a->GetSize(), b->GetSize(),
315 wxString::Format( _( "%s size differs." ), PAD_DESC( a ) ) );
316 TEST( a->GetDelta(), b->GetDelta(),
317 wxString::Format( _( "%s trapezoid delta differs." ), PAD_DESC( a ) ) );
318
320 wxString::Format( _( "%s rounded corners differ." ), PAD_DESC( a ) ) );
321
323 wxString::Format( _( "%s chamfered corner sizes differ." ), PAD_DESC( a ) ) );
324
326 wxString::Format( _( "%s chamfered corners differ." ), PAD_DESC( a ) ) );
327
328 TEST_PT( a->GetOffset(), b->GetOffset(),
329 wxString::Format( _( "%s shape offset from hole differs." ), PAD_DESC( a ) ) );
330
331 TEST( a->GetDrillShape(), b->GetDrillShape(),
332 wxString::Format( _( "%s drill shape differs." ), PAD_DESC( a ) ) );
333 TEST( a->GetDrillSize(), b->GetDrillSize(),
334 wxString::Format( _( "%s drill size differs." ), PAD_DESC( a ) ) );
335
336 // Clearance and zone connection overrides are as likely to be set at the board level as in
337 // the library.
338 //
339 // If we ignore them and someone *does* change one of them in the library, then stale
340 // footprints won't be caught.
341 //
342 // On the other hand, if we report them then boards that override at the board level are
343 // going to be VERY noisy.
344 //
345 // So we just do it when we have a reporter.
346 if( aReporter && padHasOverrides( a, b, *aReporter ) )
347 diff = true;
348
349 bool primitivesDiffer = false;
350
351 if( a->GetPrimitives().size() != b->GetPrimitives().size() )
352 {
353 primitivesDiffer = true;
354 }
355 else
356 {
357 for( size_t ii = 0; ii < a->GetPrimitives().size(); ++ii )
358 {
359 if( primitiveNeedsUpdate( a->GetPrimitives()[ii], b->GetPrimitives()[ii] ) )
360 {
361 primitivesDiffer = true;
362 break;
363 }
364 }
365 }
366
367 if( primitivesDiffer )
368 {
369 diff = true;
370
371 if( aReporter )
372 aReporter->Report( wxString::Format( _( "%s shape primitives differ." ), PAD_DESC( a ) ) );
373 else
374 return true;
375 }
376
377 return diff;
378}
379
380
381bool shapeNeedsUpdate( const PCB_SHAPE& curr_shape, const PCB_SHAPE& ref_shape )
382{
383 // curr_shape and ref_shape are expected to be normalized, for a more reliable test.
384 REPORTER* aReporter = nullptr;
385 bool diff = false;
386
387 TEST( curr_shape.GetShape(), ref_shape.GetShape(), "" );
388
389 switch( curr_shape.GetShape() )
390 {
392 {
393 BOX2I aRect( curr_shape.GetStart(), curr_shape.GetEnd() - curr_shape.GetStart() );
394 BOX2I bRect( ref_shape.GetStart(), ref_shape.GetEnd() - ref_shape.GetStart() );
395
396 aRect.Normalize();
397 bRect.Normalize();
398
399 TEST_PT( aRect.GetOrigin(), bRect.GetOrigin(), "" );
400 TEST_PT( aRect.GetEnd(), bRect.GetEnd(), "" );
401 break;
402 }
403
404 case SHAPE_T::SEGMENT:
405 case SHAPE_T::CIRCLE:
406 TEST_PT( curr_shape.GetStart(), ref_shape.GetStart(), "" );
407 TEST_PT( curr_shape.GetEnd(), ref_shape.GetEnd(), "" );
408 break;
409
410 case SHAPE_T::ARC:
411 TEST_PT( curr_shape.GetStart(), ref_shape.GetStart(), "" );
412 TEST_PT( curr_shape.GetEnd(), ref_shape.GetEnd(), "" );
413
414 // Arc center is calculated and so may have round-off errors when parents are
415 // differentially rotated.
416 if( ( curr_shape.GetCenter() - ref_shape.GetCenter() ).EuclideanNorm() > pcbIUScale.mmToIU( 0.0005 ) )
417 return true;
418
419 break;
420
421 case SHAPE_T::BEZIER:
422 TEST_PT( curr_shape.GetStart(), ref_shape.GetStart(), "" );
423 TEST_PT( curr_shape.GetEnd(), ref_shape.GetEnd(), "" );
424 TEST_PT( curr_shape.GetBezierC1(), ref_shape.GetBezierC1(), "" );
425 TEST_PT( curr_shape.GetBezierC2(), ref_shape.GetBezierC2(), "" );
426 break;
427
428 case SHAPE_T::POLY:
429 TEST( curr_shape.GetPolyShape().TotalVertices(), ref_shape.GetPolyShape().TotalVertices(), "" );
430
431 for( int ii = 0; ii < curr_shape.GetPolyShape().TotalVertices(); ++ii )
432 TEST_PT( curr_shape.GetPolyShape().CVertex( ii ), ref_shape.GetPolyShape().CVertex( ii ), "" );
433
434 break;
435
436 default:
437 UNIMPLEMENTED_FOR( curr_shape.SHAPE_T_asString() );
438 }
439
440 if( curr_shape.IsOnCopperLayer() )
441 TEST( curr_shape.GetStroke(), ref_shape.GetStroke(), "" );
442
443 TEST( curr_shape.IsFilled(), ref_shape.IsFilled(), "" );
444
445 TEST( curr_shape.GetLayer(), ref_shape.GetLayer(), "" );
446
447 return diff;
448}
449
450
451bool zoneNeedsUpdate( const ZONE* a, const ZONE* b, REPORTER* aReporter )
452{
453 bool diff = false;
454
456 wxString::Format( _( "%s corner smoothing setting differs." ), ITEM_DESC( a ) ) );
458 wxString::Format( _( "%s corner smoothing radius differs." ), ITEM_DESC( a ) ) );
459 TEST( a->GetZoneName(), b->GetZoneName(),
460 wxString::Format( _( "%s name differs." ), ITEM_DESC( a ) ) );
462 wxString::Format( _( "%s priority differs." ), ITEM_DESC( a ) ) );
463
464 TEST( a->GetIsRuleArea(), b->GetIsRuleArea(),
465 wxString::Format( _( "%s keep-out property differs." ), ITEM_DESC( a ) ) );
467 wxString::Format( _( "%s keep out copper fill setting differs." ), ITEM_DESC( a ) ) );
469 wxString::Format( _( "%s keep out footprints setting differs." ), ITEM_DESC( a ) ) );
471 wxString::Format( _( "%s keep out pads setting differs." ), ITEM_DESC( a ) ) );
473 wxString::Format( _( "%s keep out tracks setting differs." ), ITEM_DESC( a ) ) );
475 wxString::Format( _( "%s keep out vias setting differs." ), ITEM_DESC( a ) ) );
476
477 TEST( a->GetLayerSet(), b->GetLayerSet(),
478 wxString::Format( _( "%s layers differ." ), ITEM_DESC( a ) ) );
479
481 wxString::Format( _( "%s pad connection property differs." ), ITEM_DESC( a ) ) );
483 wxString::Format( _( "%s local clearance differs." ), ITEM_DESC( a ) ) );
485 wxString::Format( _( "%s thermal relief gap differs." ), ITEM_DESC( a ) ) );
487 wxString::Format( _( "%s thermal relief spoke width differs." ), ITEM_DESC( a ) ) );
488
490 wxString::Format( _( "%s min thickness differs." ), ITEM_DESC( a ) ) );
491
493 wxString::Format( _( "%s remove islands setting differs." ), ITEM_DESC( a ) ) );
495 wxString::Format( _( "%s minimum island size setting differs." ), ITEM_DESC( a ) ) );
496
497 TEST( a->GetFillMode(), b->GetFillMode(),
498 wxString::Format( _( "%s fill type differs." ), ITEM_DESC( a ) ) );
500 wxString::Format( _( "%s hatch width differs." ), ITEM_DESC( a ) ) );
501 TEST( a->GetHatchGap(), b->GetHatchGap(),
502 wxString::Format( _( "%s hatch gap differs." ), ITEM_DESC( a ) ) );
504 wxString::Format( _( "%s hatch orientation differs." ), ITEM_DESC( a ) ) );
506 wxString::Format( _( "%s hatch smoothing level differs." ), ITEM_DESC( a ) ) );
508 wxString::Format( _( "%s hatch smoothing amount differs." ), ITEM_DESC( a ) ) );
510 wxString::Format( _( "%s minimum hatch hole setting differs." ), ITEM_DESC( a ) ) );
511
512 // This is just a display property
513 // TEST( a->GetHatchBorderAlgorithm(), b->GetHatchBorderAlgorithm() );
514
516 wxString::Format( _( "%s outline corner count differs." ), ITEM_DESC( a ) ) );
517
518 bool cornersDiffer = false;
519
520 for( int ii = 0; ii < a->Outline()->TotalVertices(); ++ii )
521 {
522 if( a->Outline()->CVertex( ii ) != b->Outline()->CVertex( ii ) )
523 {
524 diff = true;
525 cornersDiffer = true;
526 break;
527 }
528 }
529
530 if( cornersDiffer && aReporter )
531 aReporter->Report( wxString::Format( _( "%s corners differ." ), ITEM_DESC( a ) ) );
532
533 return diff;
534}
535
536
537bool FOOTPRINT::FootprintNeedsUpdate( const FOOTPRINT* aLibFP, int aCompareFlags,
538 REPORTER* aReporter )
539{
540 UNITS_PROVIDER unitsProvider( pcbIUScale, EDA_UNITS::MILLIMETRES );
541
542 wxASSERT( aLibFP );
543 bool diff = false;
544
545 // To avoid issues when comparing the footprint on board and the footprint in library
546 // use the footprint from lib flipped, rotated and at same position as this.
547 // And using the footprint from lib with same changes as this minimize the issues
548 // due to rounding and shape modifications
549
550 std::unique_ptr<FOOTPRINT> temp( static_cast<FOOTPRINT*>( aLibFP->Clone() ) );
551 temp->SetParentGroup( nullptr );
552
553 temp->SetParent( GetBoard() ); // Needed to know the copper layer count;
554
555 if( IsFlipped() != temp->IsFlipped() )
556 temp->Flip( { 0, 0 }, false );
557
558 if( GetOrientation() != temp->GetOrientation() )
559 temp->SetOrientation( GetOrientation() );
560
561 if( GetPosition() != temp->GetPosition() )
562 temp->SetPosition( GetPosition() );
563
564 for( BOARD_ITEM* item : temp->GraphicalItems() )
565 item->Normalize();
566
567 // This temporary footprint must not have a parent when it goes out of scope because it
568 // must not trigger the IncrementTimestamp call in ~FOOTPRINT.
569 temp->SetParent( nullptr );
570
571 aLibFP = temp.get();
572
573 TEST( GetLibDescription(), aLibFP->GetLibDescription(), _( "Footprint descriptions differ." ) );
574 TEST( GetKeywords(), aLibFP->GetKeywords(), _( "Footprint keywords differ." ) );
575
576#define TEST_ATTR( a, b, attr, msg ) TEST( ( a & attr ), ( b & attr ), msg )
577
579 _( "Footprint types differ." ) );
580
582 wxString::Format( _( "'%s' settings differ." ),
583 _( "Allow bridged solder mask apertures between pads" ) ) );
584
586 wxString::Format( _( "'%s' settings differ." ),
587 _( "Exempt From Courtyard Requirement" ) ) );
588
589 if( !( aCompareFlags & COMPARE_FLAGS::DRC ) )
590 {
591 // These tests are skipped for DRC: they are presumed to relate to a given design.
593 wxString::Format( _( "'%s' settings differ." ),
594 _( "Not in schematic" ) ) );
595
597 wxString::Format( _( "'%s' settings differ." ),
598 _( "Exclude from position files" ) ) );
599
601 wxString::Format( _( "'%s' settings differ." ),
602 _( "Exclude from bill of materials" ) ) );
603
605 wxString::Format( _( "'%s' settings differ." ),
606 _( "Do not populate" ) ) );
607 }
608
609 // Clearance and zone connection overrides are as likely to be set at the board level as in
610 // the library.
611 //
612 // If we ignore them and someone *does* change one of them in the library, then stale
613 // footprints won't be caught.
614 //
615 // On the other hand, if we report them then boards that override at the board level are
616 // going to be VERY noisy.
617 //
618 // For now we report them if there's a reporter, but we DON'T generate DRC errors on them.
619 if( aReporter )
620 {
621 if( GetLocalClearance().has_value() && GetLocalClearance() != aLibFP->GetLocalClearance() )
622 {
623 diff = true;
624 aReporter->Report( _( "Pad clearance overridden." ) );
625 }
626
627 if( GetLocalSolderMaskMargin().has_value()
629 {
630 diff = true;
631 aReporter->Report( _( "Solder mask expansion overridden." ) );
632 }
633
634
635 if( GetLocalSolderPasteMargin().has_value()
637 {
638 diff = true;
639 aReporter->Report( _( "Solder paste absolute clearance overridden." ) );
640 }
641
644 {
645 diff = true;
646 aReporter->Report( _( "\"Solder paste relative clearance overridden." ) );
647 }
648
649 if( GetLocalZoneConnection() != ZONE_CONNECTION::INHERITED
651 {
652 diff = true;
653 aReporter->Report( _( "Zone connection overridden." ) );
654 }
655 }
656
657 TEST( GetNetTiePadGroups().size(), aLibFP->GetNetTiePadGroups().size(),
658 _( "Net tie pad groups differ." ) );
659
660 for( size_t ii = 0; ii < GetNetTiePadGroups().size(); ++ii )
661 {
662 TEST( GetNetTiePadGroups()[ii], aLibFP->GetNetTiePadGroups()[ii],
663 _( "Net tie pad groups differ." ) );
664 }
665
666#define REPORT( msg ) { if( aReporter ) aReporter->Report( msg ); }
667#define CHECKPOINT { if( diff && !aReporter ) return diff; }
668
669 // Text items are really problematic. We don't want to test the reference, but after that
670 // it gets messy.
671 //
672 // What about the value? Depends on whether or not it's a singleton part.
673 //
674 // And what about other texts? They might be added only to instances on the board, or even
675 // changed for instances on the board. Or they might want to be tested for equality.
676 //
677 // Currently we punt and ignore all the text items.
678
679 // Drawings and pads are also somewhat problematic as there's no guarantee that they'll be
680 // in the same order in the two footprints. Rather than building some sophisticated hashing
681 // algorithm we use the footprint sorting functions to attempt to sort them in the same
682 // order.
683
684 // However FOOTPRINT::cmp_drawings uses PCB_SHAPE coordinates and other infos, so we have
685 // already normalized graphic items in model footprint from library, so we need to normalize
686 // graphic items in the footprint to test (*this). So normalize them using a copy of this
687 FOOTPRINT dummy( *this );
688 dummy.SetParentGroup( nullptr );
689 dummy.SetParent( nullptr );
690
691 for( BOARD_ITEM* item : dummy.GraphicalItems() )
692 item->Normalize();
693
694 std::set<BOARD_ITEM*, FOOTPRINT::cmp_drawings> aShapes;
695 std::copy_if( dummy.GraphicalItems().begin(), dummy.GraphicalItems().end(),
696 std::inserter( aShapes, aShapes.begin() ),
697 []( BOARD_ITEM* item )
698 {
699 return item->Type() == PCB_SHAPE_T;
700 } );
701
702 std::set<BOARD_ITEM*, FOOTPRINT::cmp_drawings> bShapes;
703 std::copy_if( aLibFP->GraphicalItems().begin(), aLibFP->GraphicalItems().end(),
704 std::inserter( bShapes, bShapes.begin() ),
705 []( BOARD_ITEM* item )
706 {
707 return item->Type() == PCB_SHAPE_T;
708 } );
709
710 if( aShapes.size() != bShapes.size() )
711 {
712 diff = true;
713 REPORT( _( "Graphic item count differs." ) );
714 }
715 else
716 {
717 for( auto aIt = aShapes.begin(), bIt = bShapes.begin(); aIt != aShapes.end(); aIt++, bIt++ )
718 {
719 // aShapes and bShapes are the tested footprint PCB_SHAPE and the model PCB_SHAPE.
720 // These shapes are already normalized.
721 PCB_SHAPE* curr_shape = static_cast<PCB_SHAPE*>( *aIt );
722 PCB_SHAPE* test_shape = static_cast<PCB_SHAPE*>( *bIt );
723
724 if( shapeNeedsUpdate( *curr_shape, *test_shape ) )
725 {
726 diff = true;
727 REPORT( wxString::Format( _( "%s differs." ), ITEM_DESC( *aIt ) ) );
728 }
729 }
730 }
731
733
734 std::set<PAD*, FOOTPRINT::cmp_pads> aPads( Pads().begin(), Pads().end() );
735 std::set<PAD*, FOOTPRINT::cmp_pads> bPads( aLibFP->Pads().begin(), aLibFP->Pads().end() );
736
737 if( aPads.size() != bPads.size() )
738 {
739 diff = true;
740 REPORT( _( "Pad count differs." ) );
741 }
742 else
743 {
744 for( auto aIt = aPads.begin(), bIt = bPads.begin(); aIt != aPads.end(); aIt++, bIt++ )
745 {
746 if( padNeedsUpdate( *aIt, *bIt, aReporter ) )
747 diff = true;
748 else if( aReporter && padHasOverrides( *aIt, *bIt, *aReporter ) )
749 diff = true;
750 }
751 }
752
754
755 std::set<ZONE*, FOOTPRINT::cmp_zones> aZones( Zones().begin(), Zones().end() );
756 std::set<ZONE*, FOOTPRINT::cmp_zones> bZones( aLibFP->Zones().begin(), aLibFP->Zones().end() );
757
758 if( aZones.size() != bZones.size() )
759 {
760 diff = true;
761 REPORT( _( "Rule area count differs." ) );
762 }
763 else
764 {
765 for( auto aIt = aZones.begin(), bIt = bZones.begin(); aIt != aZones.end(); aIt++, bIt++ )
766 diff |= zoneNeedsUpdate( *aIt, *bIt, aReporter );
767 }
768
769 return diff;
770}
771
772
774{
775 BOARD* board = m_drcEngine->GetBoard();
776 PROJECT* project = board->GetProject();
777
778 if( !project )
779 {
780 reportAux( _( "No project loaded, skipping library parity tests." ) );
781 return true; // Continue with other tests
782 }
783
784 if( !reportPhase( _( "Loading footprint library table..." ) ) )
785 return false; // DRC cancelled
786
787 std::map<LIB_ID, std::shared_ptr<FOOTPRINT>> libFootprintCache;
788
790 wxString msg;
791 int ii = 0;
792 const int progressDelta = 250;
793
794 if( !reportPhase( _( "Checking board footprints against library..." ) ) )
795 return false;
796
797 for( FOOTPRINT* footprint : board->Footprints() )
798 {
801 {
802 return true; // Continue with other tests
803 }
804
805 if( !reportProgress( ii++, (int) board->Footprints().size(), progressDelta ) )
806 return false; // DRC cancelled
807
808 LIB_ID fpID = footprint->GetFPID();
809 wxString libName = fpID.GetLibNickname();
810 wxString fpName = fpID.GetLibItemName();
811 const LIB_TABLE_ROW* libTableRow = nullptr;
812
813 if( libName.IsEmpty() )
814 {
815 // Not much we can do here
816 continue;
817 }
818
819 try
820 {
821 libTableRow = libTable->FindRow( libName );
822 }
823 catch( const IO_ERROR& )
824 {
825 }
826
827 if( !libTableRow )
828 {
830 {
831 std::shared_ptr<DRC_ITEM> drcItem = DRC_ITEM::Create( DRCE_LIB_FOOTPRINT_ISSUES );
832 msg.Printf( _( "The current configuration does not include the footprint library '%s'." ),
833 libName );
834 drcItem->SetErrorMessage( msg );
835 drcItem->SetItems( footprint );
836 reportViolation( drcItem, footprint->GetCenter(), UNDEFINED_LAYER );
837 }
838
839 continue;
840 }
841 else if( !libTable->HasLibrary( libName, true ) )
842 {
844 {
845 std::shared_ptr<DRC_ITEM> drcItem = DRC_ITEM::Create( DRCE_LIB_FOOTPRINT_ISSUES );
846 msg.Printf( _( "The footprint library '%s' is not enabled in the current configuration." ),
847 libName );
848 drcItem->SetErrorMessage( msg );
849 drcItem->SetItems( footprint );
850 reportViolation( drcItem, footprint->GetCenter(), UNDEFINED_LAYER );
851 }
852
853 continue;
854 }
855
856 auto cacheIt = libFootprintCache.find( fpID );
857 std::shared_ptr<FOOTPRINT> libFootprint;
858
859 if( cacheIt != libFootprintCache.end() )
860 {
861 libFootprint = cacheIt->second;
862 }
863 else
864 {
865 try
866 {
867 libFootprint.reset( libTable->FootprintLoad( libName, fpName, true ) );
868
869 if( libFootprint )
870 libFootprintCache[ fpID ] = libFootprint;
871 }
872 catch( const IO_ERROR& )
873 {
874 }
875 }
876
877 if( !libFootprint )
878 {
880 {
881 std::shared_ptr<DRC_ITEM> drcItem = DRC_ITEM::Create( DRCE_LIB_FOOTPRINT_ISSUES );
882 msg.Printf( _( "Footprint '%s' not found in library '%s'." ),
883 fpName,
884 libName );
885 drcItem->SetErrorMessage( msg );
886 drcItem->SetItems( footprint );
887 reportViolation( drcItem, footprint->GetCenter(), UNDEFINED_LAYER );
888 }
889 }
890 else if( footprint->FootprintNeedsUpdate( libFootprint.get(), BOARD_ITEM::COMPARE_FLAGS::DRC ) )
891 {
893 {
894 std::shared_ptr<DRC_ITEM> drcItem = DRC_ITEM::Create( DRCE_LIB_FOOTPRINT_MISMATCH );
895 msg.Printf( _( "Footprint '%s' does not match copy in library '%s'." ),
896 fpName,
897 libName );
898 drcItem->SetErrorMessage( msg );
899 drcItem->SetItems( footprint );
900 reportViolation( drcItem, footprint->GetCenter(), UNDEFINED_LAYER );
901 }
902 }
903 }
904
905 return true;
906}
907
908
909namespace detail
910{
912}
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:77
virtual const BOARD * GetBoard() const
Return the BOARD in which this BOARD_ITEM resides, or NULL if none.
Definition: board_item.cpp:46
VECTOR2I GetFPRelativePosition() const
Definition: board_item.cpp:262
virtual bool IsOnCopperLayer() const
Definition: board_item.h:151
Information pertinent to a Pcbnew printed circuit board.
Definition: board.h:282
LSET GetEnabledLayers() const
A proxy function that calls the corresponding function in m_BoardSettings.
Definition: board.cpp:677
const FOOTPRINTS & Footprints() const
Definition: board.h:323
PROJECT * GetProject() const
Definition: board.h:476
BOX2< Vec > & Normalize()
Ensure that the height and width are positive.
Definition: box2.h:136
const Vec & GetOrigin() const
Definition: box2.h:200
const Vec GetEnd() const
Definition: box2.h:202
BOARD * GetBoard() const
Definition: drc_engine.h:89
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:338
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_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:206
SHAPE_POLY_SET & GetPolyShape()
Definition: eda_shape.h:279
bool IsFilled() const
Definition: eda_shape.h:91
SHAPE_T GetShape() const
Definition: eda_shape.h:125
const VECTOR2I & GetEnd() const
Return the ending point of the graphic.
Definition: eda_shape.h:167
const VECTOR2I & GetStart() const
Return the starting point of the graphic.
Definition: eda_shape.h:130
wxString SHAPE_T_asString() const
Definition: eda_shape.cpp:89
const VECTOR2I & GetBezierC1() const
Definition: eda_shape.h:203
wxString GetLibDescription() const
Definition: footprint.h:244
ZONE_CONNECTION GetLocalZoneConnection() const
Definition: footprint.h:275
EDA_ANGLE GetOrientation() const
Definition: footprint.h:214
ZONES & Zones()
Definition: footprint.h:199
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:268
EDA_ITEM * Clone() const override
Invoke a function on all children.
Definition: footprint.cpp:2019
std::optional< int > GetLocalClearance() const
Definition: footprint.h:262
int GetAttributes() const
Definition: footprint.h:277
bool IsFlipped() const
Definition: footprint.h:378
PADS & Pads()
Definition: footprint.h:193
const std::vector< wxString > & GetNetTiePadGroups() const
Definition: footprint.h:326
std::optional< double > GetLocalSolderPasteMarginRatio() const
Definition: footprint.h:271
std::optional< int > GetLocalSolderMaskMargin() const
Definition: footprint.h:265
wxString GetKeywords() const
Definition: footprint.h:247
VECTOR2I GetPosition() const override
Definition: footprint.h:211
DRAWINGS & GraphicalItems()
Definition: footprint.h:196
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: layer_ids.h:576
static LSET AllLayersMask()
Definition: lset.cpp:898
Definition: pad.h:53
PAD_PROP GetProperty() const
Definition: pad.h:397
bool GetRemoveUnconnected() const
Definition: pad.h:643
LSET GetLayerSet() const override
Return a std::bitset of all layers on which the item physically resides.
Definition: pad.h:391
std::optional< double > GetLocalSolderPasteMarginRatio() const
Definition: pad.h:424
const VECTOR2I & GetDrillSize() const
Definition: pad.h:266
PAD_ATTRIB GetAttribute() const
Definition: pad.h:394
const wxString & GetNumber() const
Definition: pad.h:133
const std::vector< std::shared_ptr< PCB_SHAPE > > & GetPrimitives() const
Accessor to the basic shape list for custom-shaped pads.
Definition: pad.h:317
EDA_ANGLE GetThermalSpokeAngle() const
Definition: pad.h:567
const VECTOR2I & GetOffset() const
Definition: pad.h:273
bool GetKeepTopBottom() const
Definition: pad.h:659
std::optional< int > GetLocalClearance() const override
Return any local clearances set in the "classic" (ie: pre-rule) system.
Definition: pad.h:409
const VECTOR2I & GetDelta() const
Definition: pad.h:263
PAD_SHAPE GetShape() const
Definition: pad.h:192
PADSTACK::CUSTOM_SHAPE_ZONE_MODE GetCustomShapeInZoneOpt() const
Definition: pad.h:213
PAD_DRILL_SHAPE GetDrillShape() const
Definition: pad.h:376
std::optional< int > GetLocalSolderPasteMargin() const
Definition: pad.h:418
std::optional< int > GetLocalSolderMaskMargin() const
Definition: pad.h:412
int GetThermalSpokeWidth() const
Definition: pad.h:554
EDA_ANGLE GetFPRelativeOrientation() const
Definition: pad.cpp:816
int GetChamferPositions() const
Definition: pad.h:623
ZONE_CONNECTION GetLocalZoneConnection() const
Definition: pad.h:434
double GetRoundRectRadiusRatio() const
Definition: pad.h:604
int GetThermalGap() const
Definition: pad.h:583
const VECTOR2I & GetSize() const
Definition: pad.h:255
double GetChamferRectRatio() const
Definition: pad.h:613
int GetPadToDieLength() const
Definition: pad.h:407
VECTOR2I GetCenter() const override
This defaults to the center of the bounding box if not overridden.
Definition: pcb_shape.h:75
STROKE_PARAMS GetStroke() const override
Definition: pcb_shape.h:85
PCB_LAYER_ID GetLayer() const override
Return the primary layer this item is on.
Definition: pcb_shape.h:70
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:62
A pure virtual class used to derive REPORTER objects from.
Definition: reporter.h:71
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:72
bool GetIsRuleArea() const
Accessors to parameters used in Rule Area zones:
Definition: zone.h:710
std::optional< int > GetLocalClearance() const override
Definition: zone.cpp:490
bool GetDoNotAllowVias() const
Definition: zone.h:712
bool GetDoNotAllowPads() const
Definition: zone.h:714
bool GetDoNotAllowTracks() const
Definition: zone.h:713
ISLAND_REMOVAL_MODE GetIslandRemovalMode() const
Definition: zone.h:724
SHAPE_POLY_SET * Outline()
Definition: zone.h:336
long long int GetMinIslandArea() const
Definition: zone.h:727
const wxString & GetZoneName() const
Definition: zone.h:131
int GetMinThickness() const
Definition: zone.h:269
ZONE_CONNECTION GetPadConnection() const
Definition: zone.h:266
int GetHatchThickness() const
Definition: zone.h:284
double GetHatchHoleMinArea() const
Definition: zone.h:299
int GetThermalReliefSpokeWidth() const
Definition: zone.h:213
EDA_ANGLE GetHatchOrientation() const
Definition: zone.h:290
bool GetDoNotAllowFootprints() const
Definition: zone.h:715
ZONE_FILL_MODE GetFillMode() const
Definition: zone.h:192
virtual LSET GetLayerSet() const override
Return a std::bitset of all layers on which the item physically resides.
Definition: zone.h:129
bool GetDoNotAllowCopperPour() const
Definition: zone.h:711
int GetHatchGap() const
Definition: zone.h:287
double GetHatchSmoothingValue() const
Definition: zone.h:296
int GetHatchSmoothingLevel() const
Definition: zone.h:293
unsigned int GetCornerRadius() const
Definition: zone.h:665
int GetCornerSmoothingType() const
Definition: zone.h:661
int GetThermalReliefGap() const
Definition: zone.h:202
unsigned GetAssignedPriority() const
Definition: zone.h:119
@ DRCE_LIB_FOOTPRINT_ISSUES
Definition: drc_item.h:78
@ DRCE_LIB_FOOTPRINT_MISMATCH
Definition: drc_item.h:79
#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)
@ ARC
use RECTANGLE instead of RECT to avoid collision in a Windows header
@ FP_SMD
Definition: footprint.h:73
@ FP_DNP
Definition: footprint.h:80
@ FP_ALLOW_MISSING_COURTYARD
Definition: footprint.h:79
@ FP_EXCLUDE_FROM_POS_FILES
Definition: footprint.h:74
@ FP_BOARD_ONLY
Definition: footprint.h:76
@ FP_EXCLUDE_FROM_BOM
Definition: footprint.h:75
@ FP_THROUGH_HOLE
Definition: footprint.h:72
@ FP_ALLOW_SOLDERMASK_BRIDGES
Definition: footprint.h:78
@ 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