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()->GetEnabledLayers();
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.
310 TEST_D( ( a->GetOrientation() - a->GetParentFootprint()->GetOrientation() ).Normalize().AsDegrees(),
311 ( b->GetOrientation() - b->GetParentFootprint()->GetOrientation() ).Normalize().AsDegrees(),
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
321 {
322 diff = true;
323
324 if( aReporter )
325 aReporter->Report( wxString::Format( _( "%s rounded corners differ." ), PAD_DESC( a ) ) );
326 else
327 return true;
328 }
329
332 {
333 diff = true;
334
335 if( aReporter )
336 aReporter->Report( wxString::Format( _( "%s chamfered corners differ." ), PAD_DESC( a ) ) );
337 else
338 return true;
339 }
340
341 TEST_PT( a->GetOffset(), b->GetOffset(),
342 wxString::Format( _( "%s shape offset from hole differs." ), PAD_DESC( a ) ) );
343
344 TEST( a->GetDrillShape(), b->GetDrillShape(),
345 wxString::Format( _( "%s drill shape differs." ), PAD_DESC( a ) ) );
346 TEST( a->GetDrillSize(), b->GetDrillSize(),
347 wxString::Format( _( "%s drill size differs." ), PAD_DESC( a ) ) );
348
349 // Clearance and zone connection overrides are as likely to be set at the board level as in
350 // the library.
351 //
352 // If we ignore them and someone *does* change one of them in the library, then stale
353 // footprints won't be caught.
354 //
355 // On the other hand, if we report them then boards that override at the board level are
356 // going to be VERY noisy.
357 //
358 // So we just do it when we have a reporter.
359 if( aReporter && padHasOverrides( a, b, *aReporter ) )
360 diff = true;
361
362 bool primitivesDiffer = false;
363
364 if( a->GetPrimitives().size() != b->GetPrimitives().size() )
365 {
366 primitivesDiffer = true;
367 }
368 else
369 {
370 for( size_t ii = 0; ii < a->GetPrimitives().size(); ++ii )
371 {
372 if( primitiveNeedsUpdate( a->GetPrimitives()[ii], b->GetPrimitives()[ii] ) )
373 {
374 primitivesDiffer = true;
375 break;
376 }
377 }
378 }
379
380 if( primitivesDiffer )
381 {
382 diff = true;
383
384 if( aReporter )
385 aReporter->Report( wxString::Format( _( "%s shape primitives differ." ), PAD_DESC( a ) ) );
386 else
387 return true;
388 }
389
390 return diff;
391}
392
393
394bool shapeNeedsUpdate( const PCB_SHAPE& curr_shape, const PCB_SHAPE& ref_shape )
395{
396 // curr_shape and ref_shape are expected to be normalized, for a more reliable test.
397 REPORTER* aReporter = nullptr;
398 bool diff = false;
399
400 TEST( curr_shape.GetShape(), ref_shape.GetShape(), "" );
401
402 switch( curr_shape.GetShape() )
403 {
405 {
406 BOX2I aRect( curr_shape.GetStart(), curr_shape.GetEnd() - curr_shape.GetStart() );
407 BOX2I bRect( ref_shape.GetStart(), ref_shape.GetEnd() - ref_shape.GetStart() );
408
409 aRect.Normalize();
410 bRect.Normalize();
411
412 TEST_PT( aRect.GetOrigin(), bRect.GetOrigin(), "" );
413 TEST_PT( aRect.GetEnd(), bRect.GetEnd(), "" );
414 break;
415 }
416
417 case SHAPE_T::SEGMENT:
418 case SHAPE_T::CIRCLE:
419 TEST_PT( curr_shape.GetStart(), ref_shape.GetStart(), "" );
420 TEST_PT( curr_shape.GetEnd(), ref_shape.GetEnd(), "" );
421 break;
422
423 case SHAPE_T::ARC:
424 TEST_PT( curr_shape.GetStart(), ref_shape.GetStart(), "" );
425 TEST_PT( curr_shape.GetEnd(), ref_shape.GetEnd(), "" );
426
427 // Arc center is calculated and so may have round-off errors when parents are
428 // differentially rotated.
429 if( ( curr_shape.GetCenter() - ref_shape.GetCenter() ).EuclideanNorm() > pcbIUScale.mmToIU( 0.0005 ) )
430 return true;
431
432 break;
433
434 case SHAPE_T::BEZIER:
435 TEST_PT( curr_shape.GetStart(), ref_shape.GetStart(), "" );
436 TEST_PT( curr_shape.GetEnd(), ref_shape.GetEnd(), "" );
437 TEST_PT( curr_shape.GetBezierC1(), ref_shape.GetBezierC1(), "" );
438 TEST_PT( curr_shape.GetBezierC2(), ref_shape.GetBezierC2(), "" );
439 break;
440
441 case SHAPE_T::POLY:
442 TEST( curr_shape.GetPolyShape().TotalVertices(), ref_shape.GetPolyShape().TotalVertices(), "" );
443
444 for( int ii = 0; ii < curr_shape.GetPolyShape().TotalVertices(); ++ii )
445 TEST_PT( curr_shape.GetPolyShape().CVertex( ii ), ref_shape.GetPolyShape().CVertex( ii ), "" );
446
447 break;
448
449 default:
450 UNIMPLEMENTED_FOR( curr_shape.SHAPE_T_asString() );
451 }
452
453 if( curr_shape.IsOnCopperLayer() )
454 TEST( curr_shape.GetStroke(), ref_shape.GetStroke(), "" );
455
456 TEST( curr_shape.IsFilled(), ref_shape.IsFilled(), "" );
457
458 TEST( curr_shape.GetLayer(), ref_shape.GetLayer(), "" );
459
460 return diff;
461}
462
463
464bool zoneNeedsUpdate( const ZONE* a, const ZONE* b, REPORTER* aReporter )
465{
466 bool diff = false;
467
469 wxString::Format( _( "%s corner smoothing setting differs." ), ITEM_DESC( a ) ) );
471 wxString::Format( _( "%s corner smoothing radius differs." ), ITEM_DESC( a ) ) );
472 TEST( a->GetZoneName(), b->GetZoneName(),
473 wxString::Format( _( "%s name differs." ), ITEM_DESC( a ) ) );
475 wxString::Format( _( "%s priority differs." ), ITEM_DESC( a ) ) );
476
477 TEST( a->GetIsRuleArea(), b->GetIsRuleArea(),
478 wxString::Format( _( "%s keep-out property differs." ), ITEM_DESC( a ) ) );
480 wxString::Format( _( "%s keep out copper fill setting differs." ), ITEM_DESC( a ) ) );
482 wxString::Format( _( "%s keep out footprints setting differs." ), ITEM_DESC( a ) ) );
484 wxString::Format( _( "%s keep out pads setting differs." ), ITEM_DESC( a ) ) );
486 wxString::Format( _( "%s keep out tracks setting differs." ), ITEM_DESC( a ) ) );
488 wxString::Format( _( "%s keep out vias setting differs." ), ITEM_DESC( a ) ) );
489
490 TEST( a->GetLayerSet(), b->GetLayerSet(),
491 wxString::Format( _( "%s layers differ." ), ITEM_DESC( a ) ) );
492
494 wxString::Format( _( "%s pad connection property differs." ), ITEM_DESC( a ) ) );
496 wxString::Format( _( "%s local clearance differs." ), ITEM_DESC( a ) ) );
498 wxString::Format( _( "%s thermal relief gap differs." ), ITEM_DESC( a ) ) );
500 wxString::Format( _( "%s thermal relief spoke width differs." ), ITEM_DESC( a ) ) );
501
503 wxString::Format( _( "%s min thickness differs." ), ITEM_DESC( a ) ) );
504
506 wxString::Format( _( "%s remove islands setting differs." ), ITEM_DESC( a ) ) );
508 wxString::Format( _( "%s minimum island size setting differs." ), ITEM_DESC( a ) ) );
509
510 TEST( a->GetFillMode(), b->GetFillMode(),
511 wxString::Format( _( "%s fill type differs." ), ITEM_DESC( a ) ) );
513 wxString::Format( _( "%s hatch width differs." ), ITEM_DESC( a ) ) );
514 TEST( a->GetHatchGap(), b->GetHatchGap(),
515 wxString::Format( _( "%s hatch gap differs." ), ITEM_DESC( a ) ) );
517 wxString::Format( _( "%s hatch orientation differs." ), ITEM_DESC( a ) ) );
519 wxString::Format( _( "%s hatch smoothing level differs." ), ITEM_DESC( a ) ) );
521 wxString::Format( _( "%s hatch smoothing amount differs." ), ITEM_DESC( a ) ) );
523 wxString::Format( _( "%s minimum hatch hole setting differs." ), ITEM_DESC( a ) ) );
524
525 // This is just a display property
526 // TEST( a->GetHatchBorderAlgorithm(), b->GetHatchBorderAlgorithm() );
527
529 wxString::Format( _( "%s outline corner count differs." ), ITEM_DESC( a ) ) );
530
531 bool cornersDiffer = false;
532
533 for( int ii = 0; ii < a->Outline()->TotalVertices(); ++ii )
534 {
535 if( a->Outline()->CVertex( ii ) != b->Outline()->CVertex( ii ) )
536 {
537 diff = true;
538 cornersDiffer = true;
539 break;
540 }
541 }
542
543 if( cornersDiffer && aReporter )
544 aReporter->Report( wxString::Format( _( "%s corners differ." ), ITEM_DESC( a ) ) );
545
546 return diff;
547}
548
549
550bool FOOTPRINT::FootprintNeedsUpdate( const FOOTPRINT* aLibFP, REPORTER* aReporter )
551{
552 UNITS_PROVIDER unitsProvider( pcbIUScale, EDA_UNITS::MILLIMETRES );
553
554 wxASSERT( aLibFP );
555 bool diff = false;
556
557 // To avoid issues when comparing the footprint on board and the footprint in library
558 // use the footprint from lib flipped, rotated and at same position as this.
559 // And using the footprint from lib with same changes as this minimize the issues
560 // due to rounding and shape modifications
561
562 std::unique_ptr<FOOTPRINT> temp( static_cast<FOOTPRINT*>( aLibFP->Clone() ) );
563 temp->SetParentGroup( nullptr );
564
565 temp->SetParent( GetBoard() ); // Needed to know the copper layer count;
566
567 if( IsFlipped() != temp->IsFlipped() )
568 temp->Flip( { 0, 0 }, false );
569
570 if( GetOrientation() != temp->GetOrientation() )
571 temp->SetOrientation( GetOrientation() );
572
573 if( GetPosition() != temp->GetPosition() )
574 temp->SetPosition( GetPosition() );
575
576 for( BOARD_ITEM* item : temp->GraphicalItems() )
577 item->Normalize();
578
579 // This temporary footprint must not have a parent when it goes out of scope because it
580 // must not trigger the IncrementTimestamp call in ~FOOTPRINT.
581 temp->SetParent( nullptr );
582
583 aLibFP = temp.get();
584
585 TEST( GetLibDescription(), aLibFP->GetLibDescription(), _( "Footprint descriptions differ." ) );
586 TEST( GetKeywords(), aLibFP->GetKeywords(), _( "Footprint keywords differ." ) );
587
588#define TEST_ATTR( a, b, attr, msg ) TEST( ( a & attr ), ( b & attr ), msg )
589
591 _( "Footprint types differ." ) );
592
594 wxString::Format( _( "'%s' settings differ." ),
595 _( "Allow bridged solder mask apertures between pads" ) ) );
596
598 wxString::Format( _( "'%s' settings differ." ),
599 _( "Not in schematic" ) ) );
600
602 wxString::Format( _( "'%s' settings differ." ),
603 _( "Exclude from position files" ) ) );
604
605 // this test is skipped: EXCLUDE_FROM_BOM attribute is related to a given design,
606 // not to a lib footprint. EXCLUDE_FROM_BOM must be tested only in Schematic Parity
607 #if 0
609 wxString::Format( _( "'%s' settings differ." ),
610 _( "Exclude from bill of materials" ) ) );
611 #endif
612
614 wxString::Format( _( "'%s' settings differ." ),
615 _( "Exempt From Courtyard Requirement" ) ) );
616
617 // this test is skipped: Do No Place attribute is related to a given design,
618 // not to a lib footprint. DNP must be tested only in Schematic Parity
619 #if 0
621 wxString::Format( _( "'%s' settings differ." ),
622 _( "Do not populate" ) ) );
623 #endif
624
625
626 // Clearance and zone connection overrides are as likely to be set at the board level as in
627 // the library.
628 //
629 // If we ignore them and someone *does* change one of them in the library, then stale
630 // footprints won't be caught.
631 //
632 // On the other hand, if we report them then boards that override at the board level are
633 // going to be VERY noisy.
634 //
635 // For now we report them if there's a reporter, but we DON'T generate DRC errors on them.
636 if( aReporter )
637 {
638 if( GetLocalClearance().has_value() && GetLocalClearance() != aLibFP->GetLocalClearance() )
639 {
640 diff = true;
641 aReporter->Report( _( "Pad clearance overridden." ) );
642 }
643
644 if( GetLocalSolderMaskMargin().has_value()
646 {
647 diff = true;
648 aReporter->Report( _( "Solder mask expansion overridden." ) );
649 }
650
651
652 if( GetLocalSolderPasteMargin().has_value()
654 {
655 diff = true;
656 aReporter->Report( _( "Solder paste absolute clearance overridden." ) );
657 }
658
661 {
662 diff = true;
663 aReporter->Report( _( "\"Solder paste relative clearance overridden." ) );
664 }
665
666 if( GetLocalZoneConnection() != ZONE_CONNECTION::INHERITED
668 {
669 diff = true;
670 aReporter->Report( _( "Zone connection overridden." ) );
671 }
672 }
673
674 TEST( GetNetTiePadGroups().size(), aLibFP->GetNetTiePadGroups().size(),
675 _( "Net tie pad groups differ." ) );
676
677 for( size_t ii = 0; ii < GetNetTiePadGroups().size(); ++ii )
678 {
679 TEST( GetNetTiePadGroups()[ii], aLibFP->GetNetTiePadGroups()[ii],
680 _( "Net tie pad groups differ." ) );
681 }
682
683#define REPORT( msg ) { if( aReporter ) aReporter->Report( msg ); }
684#define CHECKPOINT { if( diff && !aReporter ) return diff; }
685
686 // Text items are really problematic. We don't want to test the reference, but after that
687 // it gets messy.
688 //
689 // What about the value? Depends on whether or not it's a singleton part.
690 //
691 // And what about other texts? They might be added only to instances on the board, or even
692 // changed for instances on the board. Or they might want to be tested for equality.
693 //
694 // Currently we punt and ignore all the text items.
695
696 // Drawings and pads are also somewhat problematic as there's no guarantee that they'll be
697 // in the same order in the two footprints. Rather than building some sophisticated hashing
698 // algorithm we use the footprint sorting functions to attempt to sort them in the same
699 // order.
700
701 // However FOOTPRINT::cmp_drawings uses PCB_SHAPE coordinates and other infos, so we have
702 // already normalized graphic items in model footprint from library, so we need to normalize
703 // graphic items in the footprint to test (*this). So normalize them using a copy of this
704 FOOTPRINT dummy( *this );
705 dummy.SetParentGroup( nullptr );
706 dummy.SetParent( nullptr );
707
708 for( BOARD_ITEM* item : dummy.GraphicalItems() )
709 item->Normalize();
710
711 std::set<BOARD_ITEM*, FOOTPRINT::cmp_drawings> aShapes;
712 std::copy_if( dummy.GraphicalItems().begin(), dummy.GraphicalItems().end(),
713 std::inserter( aShapes, aShapes.begin() ),
714 []( BOARD_ITEM* item )
715 {
716 return item->Type() == PCB_SHAPE_T;
717 } );
718
719 std::set<BOARD_ITEM*, FOOTPRINT::cmp_drawings> bShapes;
720 std::copy_if( aLibFP->GraphicalItems().begin(), aLibFP->GraphicalItems().end(),
721 std::inserter( bShapes, bShapes.begin() ),
722 []( BOARD_ITEM* item )
723 {
724 return item->Type() == PCB_SHAPE_T;
725 } );
726
727 if( aShapes.size() != bShapes.size() )
728 {
729 diff = true;
730 REPORT( _( "Graphic item count differs." ) );
731 }
732 else
733 {
734 for( auto aIt = aShapes.begin(), bIt = bShapes.begin(); aIt != aShapes.end(); aIt++, bIt++ )
735 {
736 // aShapes and bShapes are the tested footprint PCB_SHAPE and the model PCB_SHAPE.
737 // These shapes are already normalized.
738 PCB_SHAPE* curr_shape = static_cast<PCB_SHAPE*>( *aIt );
739 PCB_SHAPE* test_shape = static_cast<PCB_SHAPE*>( *bIt );
740
741 if( shapeNeedsUpdate( *curr_shape, *test_shape ) )
742 {
743 diff = true;
744 REPORT( wxString::Format( _( "%s differs." ), ITEM_DESC( *aIt ) ) );
745 }
746 }
747 }
748
750
751 std::set<PAD*, FOOTPRINT::cmp_pads> aPads( Pads().begin(), Pads().end() );
752 std::set<PAD*, FOOTPRINT::cmp_pads> bPads( aLibFP->Pads().begin(), aLibFP->Pads().end() );
753
754 if( aPads.size() != bPads.size() )
755 {
756 diff = true;
757 REPORT( _( "Pad count differs." ) );
758 }
759 else
760 {
761 for( auto aIt = aPads.begin(), bIt = bPads.begin(); aIt != aPads.end(); aIt++, bIt++ )
762 {
763 if( padNeedsUpdate( *aIt, *bIt, aReporter ) )
764 diff = true;
765 else if( aReporter && padHasOverrides( *aIt, *bIt, *aReporter ) )
766 diff = true;
767 }
768 }
769
771
772 std::set<ZONE*, FOOTPRINT::cmp_zones> aZones( Zones().begin(), Zones().end() );
773 std::set<ZONE*, FOOTPRINT::cmp_zones> bZones( aLibFP->Zones().begin(), aLibFP->Zones().end() );
774
775 if( aZones.size() != bZones.size() )
776 {
777 diff = true;
778 REPORT( _( "Rule area count differs." ) );
779 }
780 else
781 {
782 for( auto aIt = aZones.begin(), bIt = bZones.begin(); aIt != aZones.end(); aIt++, bIt++ )
783 diff |= zoneNeedsUpdate( *aIt, *bIt, aReporter );
784 }
785
786 return diff;
787}
788
789
791{
792 BOARD* board = m_drcEngine->GetBoard();
793 PROJECT* project = board->GetProject();
794
795 if( !project )
796 {
797 reportAux( _( "No project loaded, skipping library parity tests." ) );
798 return true; // Continue with other tests
799 }
800
801 if( !reportPhase( _( "Loading footprint library table..." ) ) )
802 return false; // DRC cancelled
803
804 std::map<LIB_ID, std::shared_ptr<FOOTPRINT>> libFootprintCache;
805
807 wxString msg;
808 int ii = 0;
809 const int progressDelta = 250;
810
811 if( !reportPhase( _( "Checking board footprints against library..." ) ) )
812 return false;
813
814 for( FOOTPRINT* footprint : board->Footprints() )
815 {
818 {
819 return true; // Continue with other tests
820 }
821
822 if( !reportProgress( ii++, (int) board->Footprints().size(), progressDelta ) )
823 return false; // DRC cancelled
824
825 LIB_ID fpID = footprint->GetFPID();
826 wxString libName = fpID.GetLibNickname();
827 wxString fpName = fpID.GetLibItemName();
828 const LIB_TABLE_ROW* libTableRow = nullptr;
829
830 try
831 {
832 libTableRow = libTable->FindRow( libName );
833 }
834 catch( const IO_ERROR& )
835 {
836 }
837
838 if( !libTableRow )
839 {
841 {
842 std::shared_ptr<DRC_ITEM> drcItem = DRC_ITEM::Create( DRCE_LIB_FOOTPRINT_ISSUES );
843 msg.Printf( _( "The current configuration does not include the footprint library '%s'." ),
844 libName );
845 drcItem->SetErrorMessage( msg );
846 drcItem->SetItems( footprint );
847 reportViolation( drcItem, footprint->GetCenter(), UNDEFINED_LAYER );
848 }
849
850 continue;
851 }
852 else if( !libTable->HasLibrary( libName, true ) )
853 {
855 {
856 std::shared_ptr<DRC_ITEM> drcItem = DRC_ITEM::Create( DRCE_LIB_FOOTPRINT_ISSUES );
857 msg.Printf( _( "The footprint library '%s' is not enabled in the current configuration." ),
858 libName );
859 drcItem->SetErrorMessage( msg );
860 drcItem->SetItems( footprint );
861 reportViolation( drcItem, footprint->GetCenter(), UNDEFINED_LAYER );
862 }
863
864 continue;
865 }
866
867 auto cacheIt = libFootprintCache.find( fpID );
868 std::shared_ptr<FOOTPRINT> libFootprint;
869
870 if( cacheIt != libFootprintCache.end() )
871 {
872 libFootprint = cacheIt->second;
873 }
874 else
875 {
876 try
877 {
878 libFootprint.reset( libTable->FootprintLoad( libName, fpName, true ) );
879
880 if( libFootprint )
881 libFootprintCache[ fpID ] = libFootprint;
882 }
883 catch( const IO_ERROR& )
884 {
885 }
886 }
887
888 if( !libFootprint )
889 {
891 {
892 std::shared_ptr<DRC_ITEM> drcItem = DRC_ITEM::Create( DRCE_LIB_FOOTPRINT_ISSUES );
893 msg.Printf( _( "Footprint '%s' not found in library '%s'." ),
894 fpName,
895 libName );
896 drcItem->SetErrorMessage( msg );
897 drcItem->SetItems( footprint );
898 reportViolation( drcItem, footprint->GetCenter(), UNDEFINED_LAYER );
899 }
900 }
901 else if( footprint->FootprintNeedsUpdate( libFootprint.get() ) )
902 {
904 {
905 std::shared_ptr<DRC_ITEM> drcItem = DRC_ITEM::Create( DRCE_LIB_FOOTPRINT_MISMATCH );
906 msg.Printf( _( "Footprint '%s' does not match copy in library '%s'." ),
907 fpName,
908 libName );
909 drcItem->SetErrorMessage( msg );
910 drcItem->SetItems( footprint );
911 reportViolation( drcItem, footprint->GetCenter(), UNDEFINED_LAYER );
912 }
913 }
914 }
915
916 return true;
917}
918
919
920namespace detail
921{
923}
constexpr EDA_IU_SCALE pcbIUScale
Definition: base_units.h:109
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
FOOTPRINT * GetParentFootprint() const
Definition: board_item.cpp:248
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:276
LSET GetEnabledLayers() const
A proxy function that calls the corresponding function in m_BoardSettings.
Definition: board.cpp:658
FOOTPRINTS & Footprints()
Definition: board.h:318
PROJECT * GetProject() const
Definition: board.h:457
BOX2< Vec > & Normalize()
Ensure that the height and width are positive.
Definition: box2.h:120
const Vec & GetOrigin() const
Definition: box2.h:184
const Vec GetEnd() const
Definition: box2.h:186
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:331
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)
double AsDegrees() const
Definition: eda_angle.h:155
const VECTOR2I & GetBezierC2() const
Definition: eda_shape.h:190
SHAPE_POLY_SET & GetPolyShape()
Definition: eda_shape.h:263
bool IsFilled() const
Definition: eda_shape.h:90
SHAPE_T GetShape() const
Definition: eda_shape.h:119
const VECTOR2I & GetEnd() const
Return the ending point of the graphic.
Definition: eda_shape.h:151
const VECTOR2I & GetStart() const
Return the starting point of the graphic.
Definition: eda_shape.h:126
wxString SHAPE_T_asString() const
Definition: eda_shape.cpp:87
const VECTOR2I & GetBezierC1() const
Definition: eda_shape.h:187
wxString GetLibDescription() const
Definition: footprint.h:236
ZONE_CONNECTION GetLocalZoneConnection() const
Definition: footprint.h:267
EDA_ANGLE GetOrientation() const
Definition: footprint.h:209
ZONES & Zones()
Definition: footprint.h:194
std::optional< int > GetLocalSolderPasteMargin() const
Definition: footprint.h:260
EDA_ITEM * Clone() const override
Invoke a function on all children.
Definition: footprint.cpp:1734
std::optional< int > GetLocalClearance() const
Definition: footprint.h:254
int GetAttributes() const
Definition: footprint.h:269
bool FootprintNeedsUpdate(const FOOTPRINT *aLibFP, REPORTER *aReporter=nullptr)
Return true if a board footprint differs from the library version.
bool IsFlipped() const
Definition: footprint.h:370
PADS & Pads()
Definition: footprint.h:188
const std::vector< wxString > & GetNetTiePadGroups() const
Definition: footprint.h:318
std::optional< double > GetLocalSolderPasteMarginRatio() const
Definition: footprint.h:263
std::optional< int > GetLocalSolderMaskMargin() const
Definition: footprint.h:257
wxString GetKeywords() const
Definition: footprint.h:239
VECTOR2I GetPosition() const override
Definition: footprint.h:206
DRAWINGS & GraphicalItems()
Definition: footprint.h:191
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:573
Definition: pad.h:59
PAD_PROP GetProperty() const
Definition: pad.h:377
bool GetRemoveUnconnected() const
Definition: pad.h:597
PAD_DRILL_SHAPE_T GetDrillShape() const
Definition: pad.h:356
LSET GetLayerSet() const override
Return a std::bitset of all layers on which the item physically resides.
Definition: pad.h:371
std::optional< double > GetLocalSolderPasteMarginRatio() const
Definition: pad.h:398
const VECTOR2I & GetDrillSize() const
Definition: pad.h:254
PAD_ATTRIB GetAttribute() const
Definition: pad.h:374
const wxString & GetNumber() const
Definition: pad.h:131
int GetRoundRectCornerRadius() const
Definition: pad.cpp:324
const std::vector< std::shared_ptr< PCB_SHAPE > > & GetPrimitives() const
Accessor to the basic shape list for custom-shaped pads.
Definition: pad.h:301
EDA_ANGLE GetThermalSpokeAngle() const
Definition: pad.h:529
const VECTOR2I & GetOffset() const
Definition: pad.h:261
bool GetKeepTopBottom() const
Definition: pad.h:603
CUST_PAD_SHAPE_IN_ZONE GetCustomShapeInZoneOpt() const
Definition: pad.h:208
std::optional< int > GetLocalClearance() const override
Return any local clearances set in the "classic" (ie: pre-rule) system.
Definition: pad.h:389
const VECTOR2I & GetDelta() const
Definition: pad.h:251
PAD_SHAPE GetShape() const
Definition: pad.h:190
EDA_ANGLE GetOrientation() const
Return the rotation angle of the pad.
Definition: pad.h:342
std::optional< int > GetLocalSolderPasteMargin() const
Definition: pad.h:395
std::optional< int > GetLocalSolderMaskMargin() const
Definition: pad.h:392
int GetThermalSpokeWidth() const
Definition: pad.h:519
int GetChamferPositions() const
Definition: pad.h:582
ZONE_CONNECTION GetLocalZoneConnection() const
Definition: pad.h:402
double GetRoundRectRadiusRatio() const
Definition: pad.h:563
int GetThermalGap() const
Definition: pad.h:542
const VECTOR2I & GetSize() const
Definition: pad.h:244
double GetChamferRectRatio() const
Definition: pad.h:572
int GetPadToDieLength() const
Definition: pad.h:387
VECTOR2I GetCenter() const override
This defaults to the center of the bounding box if not overridden.
Definition: pcb_shape.h:72
STROKE_PARAMS GetStroke() const override
Definition: pcb_shape.h:82
PCB_LAYER_ID GetLayer() const override
Return the primary layer this item is on.
Definition: pcb_shape.h:67
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:489
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:77
@ DRCE_LIB_FOOTPRINT_MISMATCH
Definition: drc_item.h:78
#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)
Definition: lib_symbol.cpp:245
#define ITEM_DESC(item)
Definition: lib_symbol.cpp:246
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:89