KiCad PCB EDA Suite
Loading...
Searching...
No Matches
erc.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) 2015 Jean-Pierre Charras, jp.charras at wanadoo.fr
5 * Copyright (C) 2011 Wayne Stambaugh <[email protected]>
6 * Copyright The KiCad Developers, see AUTHORS.txt for contributors.
7 *
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License
10 * as published by the Free Software Foundation; either version 2
11 * of the License, or (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, you may find one here:
20 * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
21 * or you may search the http://www.gnu.org website for the version 2 license,
22 * or you may write to the Free Software Foundation, Inc.,
23 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
24 */
25
26#include <algorithm>
27#include <numeric>
28
29#include "connection_graph.h"
30#include "kiface_ids.h"
31#include <advanced_config.h>
32#include <common.h> // for ExpandEnvVarSubstitutions
33#include <erc/erc.h>
36#include <string_utils.h>
37#include <sch_pin.h>
38#include <project_sch.h>
41#include <sch_bus_entry.h>
42#include <sch_edit_frame.h>
43#include <sch_marker.h>
44#include <sch_reference_list.h>
45#include <sch_rule_area.h>
46#include <sch_sheet.h>
47#include <sch_sheet_pin.h>
48#include <sch_pin.h>
49#include <sch_textbox.h>
50#include <sch_line.h>
51#include <schematic.h>
52#include <symbol_lib_table.h>
55#include <vector>
56#include <wx/ffile.h>
57#include <sim/sim_lib_mgr.h>
58#include <progress_reporter.h>
59#include <kiway.h>
60
61
62/* ERC tests :
63 * 1 - conflicts between connected pins ( example: 2 connected outputs )
64 * 2 - minimal connections requirements ( 1 input *must* be connected to an
65 * output, or a passive pin )
66 */
67
68/*
69 * Minimal ERC requirements:
70 * All pins *must* be connected (except ELECTRICAL_PINTYPE::PT_NC).
71 * When a pin is not connected in schematic, the user must place a "non
72 * connected" symbol to this pin.
73 * This ensures a forgotten connection will be detected.
74 */
75
76// Messages for matrix rows:
77const wxString CommentERC_H[] =
78{
79 _( "Input Pin" ),
80 _( "Output Pin" ),
81 _( "Bidirectional Pin" ),
82 _( "Tri-State Pin" ),
83 _( "Passive Pin" ),
84 _( "Free Pin" ),
85 _( "Unspecified Pin" ),
86 _( "Power Input Pin" ),
87 _( "Power Output Pin" ),
88 _( "Open Collector" ),
89 _( "Open Emitter" ),
90 _( "No Connection" )
91};
92
93// Messages for matrix columns
94const wxString CommentERC_V[] =
95{
96 _( "Input Pin" ),
97 _( "Output Pin" ),
98 _( "Bidirectional Pin" ),
99 _( "Tri-State Pin" ),
100 _( "Passive Pin" ),
101 _( "Free Pin" ),
102 _( "Unspecified Pin" ),
103 _( "Power Input Pin" ),
104 _( "Power Output Pin" ),
105 _( "Open Collector" ),
106 _( "Open Emitter" ),
107 _( "No Connection" )
108};
109
110
111// List of pin types that are considered drivers for usual input pins
112// i.e. pin type = ELECTRICAL_PINTYPE::PT_INPUT, but not PT_POWER_IN
113// that need only a PT_POWER_OUT pin type to be driven
122
123// List of pin types that are considered drivers for power pins
124// In fact only a ELECTRICAL_PINTYPE::PT_POWER_OUT pin type can drive
125// power input pins
126const std::set<ELECTRICAL_PINTYPE> DrivingPowerPinTypes =
127 {
129 };
130
131// List of pin types that require a driver elsewhere on the net
132const std::set<ELECTRICAL_PINTYPE> DrivenPinTypes =
133 {
136 };
137
138extern void CheckDuplicatePins( LIB_SYMBOL* aSymbol, std::vector<wxString>& aMessages,
139 UNITS_PROVIDER* aUnitsProvider );
140
141int ERC_TESTER::TestDuplicateSheetNames( bool aCreateMarker )
142{
143 int err_count = 0;
144
145 for( SCH_SCREEN* screen = m_screens.GetFirst(); screen; screen = m_screens.GetNext() )
146 {
147 std::vector<SCH_SHEET*> list;
148
149 for( SCH_ITEM* item : screen->Items().OfType( SCH_SHEET_T ) )
150 list.push_back( static_cast<SCH_SHEET*>( item ) );
151
152 for( size_t i = 0; i < list.size(); i++ )
153 {
154 SCH_SHEET* sheet = list[i];
155
156 for( size_t j = i + 1; j < list.size(); j++ )
157 {
158 SCH_SHEET* test_item = list[j];
159
160 // We have found a second sheet: compare names
161 // we are using case insensitive comparison to avoid mistakes between
162 // similar names like Mysheet and mysheet
163 if( sheet->GetShownName( false ).IsSameAs( test_item->GetShownName( false ), false ) )
164 {
165 if( aCreateMarker )
166 {
168 ercItem->SetItems( sheet, test_item );
169
170 SCH_MARKER* marker = new SCH_MARKER( std::move( ercItem ), sheet->GetPosition() );
171 screen->Append( marker );
172 }
173
174 err_count++;
175 }
176 }
177 }
178 }
179
180 return err_count;
181}
182
183
185{
187
188 auto unresolved =
189 [this]( wxString str )
190 {
191 str = ExpandEnvVarSubstitutions( str, &m_schematic->Project() );
192 return str.Matches( wxS( "*${*}*" ) );
193 };
194
195 auto testAssertion =
196 []( const SCH_ITEM* item, const SCH_SHEET_PATH& sheet, SCH_SCREEN* screen,
197 const wxString& text, const VECTOR2I& pos )
198 {
199 static wxRegEx warningExpr( wxS( "^\\$\\{ERC_WARNING\\s*([^}]*)\\}(.*)$" ) );
200 static wxRegEx errorExpr( wxS( "^\\$\\{ERC_ERROR\\s*([^}]*)\\}(.*)$" ) );
201
202 if( warningExpr.Matches( text ) )
203 {
204 std::shared_ptr<ERC_ITEM> ercItem = ERC_ITEM::Create( ERCE_GENERIC_WARNING );
205 wxString ercText = warningExpr.GetMatch( text, 1 );
206
207 if( item )
208 ercItem->SetItems( item );
209 else
210 ercText += _( " (in drawing sheet)" );
211
212 ercItem->SetSheetSpecificPath( sheet );
213 ercItem->SetErrorMessage( ercText );
214
215 SCH_MARKER* marker = new SCH_MARKER( std::move( ercItem ), pos );
216 screen->Append( marker );
217
218 return true;
219 }
220
221 if( errorExpr.Matches( text ) )
222 {
223 std::shared_ptr<ERC_ITEM> ercItem = ERC_ITEM::Create( ERCE_GENERIC_ERROR );
224 wxString ercText = errorExpr.GetMatch( text, 1 );
225
226 if( item )
227 ercItem->SetItems( item );
228 else
229 ercText += _( " (in drawing sheet)" );
230
231 ercItem->SetSheetSpecificPath( sheet );
232 ercItem->SetErrorMessage( ercText );
233
234 SCH_MARKER* marker = new SCH_MARKER( std::move( ercItem ), pos );
235 screen->Append( marker );
236
237 return true;
238 }
239
240 return false;
241 };
242
243 if( aDrawingSheet )
244 {
245 wsItems.SetPageNumber( wxS( "1" ) );
246 wsItems.SetSheetCount( 1 );
247 wsItems.SetFileName( wxS( "dummyFilename" ) );
248 wsItems.SetSheetName( wxS( "dummySheet" ) );
249 wsItems.SetSheetLayer( wxS( "dummyLayer" ) );
250 wsItems.SetProject( &m_schematic->Project() );
251 wsItems.BuildDrawItemsList( aDrawingSheet->GetPageInfo(), aDrawingSheet->GetTitleBlock() );
252 }
253
254 for( const SCH_SHEET_PATH& sheet : m_sheetList )
255 {
256 SCH_SCREEN* screen = sheet.LastScreen();
257
258 for( SCH_ITEM* item : screen->Items().OfType( SCH_LOCATE_ANY_T ) )
259 {
260 if( item->Type() == SCH_SYMBOL_T )
261 {
262 SCH_SYMBOL* symbol = static_cast<SCH_SYMBOL*>( item );
263
264 for( SCH_FIELD& field : symbol->GetFields() )
265 {
266 if( unresolved( field.GetShownText( &sheet, true ) ) )
267 {
269 ercItem->SetItems( symbol );
270 ercItem->SetSheetSpecificPath( sheet );
271
272 SCH_MARKER* marker = new SCH_MARKER( std::move( ercItem ), field.GetPosition() );
273 screen->Append( marker );
274 }
275
276 testAssertion( &field, sheet, screen, field.GetText(), field.GetPosition() );
277 }
278
279 if( symbol->GetLibSymbolRef() )
280 {
282 [&]( SCH_ITEM* child )
283 {
284 if( child->Type() == SCH_FIELD_T )
285 {
286 // test only SCH_SYMBOL fields, not LIB_SYMBOL fields
287 }
288 else if( child->Type() == SCH_TEXT_T )
289 {
290 SCH_TEXT* textItem = static_cast<SCH_TEXT*>( child );
291
292 if( unresolved( textItem->GetShownText( &sheet, true ) ) )
293 {
295 ercItem->SetItems( symbol );
296 ercItem->SetSheetSpecificPath( sheet );
297
298 BOX2I bbox = textItem->GetBoundingBox();
299 bbox = symbol->GetTransform().TransformCoordinate( bbox );
300 VECTOR2I pos = bbox.Centre() + symbol->GetPosition();
301
302 SCH_MARKER* marker = new SCH_MARKER( std::move( ercItem ), pos );
303 screen->Append( marker );
304 }
305
306 testAssertion( symbol, sheet, screen, textItem->GetText(),
307 textItem->GetPosition() );
308 }
309 else if( child->Type() == SCH_TEXTBOX_T )
310 {
311 SCH_TEXTBOX* textboxItem = static_cast<SCH_TEXTBOX*>( child );
312
313 if( unresolved( textboxItem->GetShownText( nullptr, &sheet, true ) ) )
314 {
316 ercItem->SetItems( symbol );
317 ercItem->SetSheetSpecificPath( sheet );
318
319 BOX2I bbox = textboxItem->GetBoundingBox();
320 bbox = symbol->GetTransform().TransformCoordinate( bbox );
321 VECTOR2I pos = bbox.Centre() + symbol->GetPosition();
322
323 SCH_MARKER* marker = new SCH_MARKER( std::move( ercItem ), pos );
324 screen->Append( marker );
325 }
326
327 testAssertion( symbol, sheet, screen, textboxItem->GetText(),
328 textboxItem->GetPosition() );
329 }
330 },
332 }
333 }
334 else if( SCH_LABEL_BASE* label = dynamic_cast<SCH_LABEL_BASE*>( item ) )
335 {
336 for( SCH_FIELD& field : label->GetFields() )
337 {
338 if( unresolved( field.GetShownText( &sheet, true ) ) )
339 {
341 ercItem->SetItems( label );
342 ercItem->SetSheetSpecificPath( sheet );
343
344 SCH_MARKER* marker = new SCH_MARKER( std::move( ercItem ), field.GetPosition() );
345 screen->Append( marker );
346 }
347
348 testAssertion( &field, sheet, screen, field.GetText(), field.GetPosition() );
349 }
350 }
351 else if( item->Type() == SCH_SHEET_T )
352 {
353 SCH_SHEET* subSheet = static_cast<SCH_SHEET*>( item );
354
355 for( SCH_FIELD& field : subSheet->GetFields() )
356 {
357 if( unresolved( field.GetShownText( &sheet, true ) ) )
358 {
360 ercItem->SetItems( subSheet );
361 ercItem->SetSheetSpecificPath( sheet );
362
363 SCH_MARKER* marker = new SCH_MARKER( std::move( ercItem ), field.GetPosition() );
364 screen->Append( marker );
365 }
366
367 testAssertion( &field, sheet, screen, field.GetText(), field.GetPosition() );
368 }
369
370 SCH_SHEET_PATH subSheetPath = sheet;
371 subSheetPath.push_back( subSheet );
372
373 for( SCH_SHEET_PIN* pin : subSheet->GetPins() )
374 {
375 if( pin->GetShownText( &subSheetPath, true ).Matches( wxS( "*${*}*" ) ) )
376 {
378 ercItem->SetItems( pin );
379 ercItem->SetSheetSpecificPath( sheet );
380
381 SCH_MARKER* marker = new SCH_MARKER( std::move( ercItem ), pin->GetPosition() );
382 screen->Append( marker );
383 }
384 }
385 }
386 else if( SCH_TEXT* text = dynamic_cast<SCH_TEXT*>( item ) )
387 {
388 if( text->GetShownText( &sheet, true ).Matches( wxS( "*${*}*" ) ) )
389 {
391 ercItem->SetItems( text );
392 ercItem->SetSheetSpecificPath( sheet );
393
394 SCH_MARKER* marker = new SCH_MARKER( std::move( ercItem ), text->GetPosition() );
395 screen->Append( marker );
396 }
397
398 testAssertion( text, sheet, screen, text->GetText(), text->GetPosition() );
399 }
400 else if( SCH_TEXTBOX* textBox = dynamic_cast<SCH_TEXTBOX*>( item ) )
401 {
402 if( textBox->GetShownText( nullptr, &sheet, true ).Matches( wxS( "*${*}*" ) ) )
403 {
405 ercItem->SetItems( textBox );
406 ercItem->SetSheetSpecificPath( sheet );
407
408 SCH_MARKER* marker = new SCH_MARKER( std::move( ercItem ), textBox->GetPosition() );
409 screen->Append( marker );
410 }
411
412 testAssertion( textBox, sheet, screen, textBox->GetText(), textBox->GetPosition() );
413 }
414 }
415
416 for( DS_DRAW_ITEM_BASE* item = wsItems.GetFirst(); item; item = wsItems.GetNext() )
417 {
418 if( DS_DRAW_ITEM_TEXT* text = dynamic_cast<DS_DRAW_ITEM_TEXT*>( item ) )
419 {
420 if( testAssertion( nullptr, sheet, screen, text->GetText(), text->GetPosition() ) )
421 {
422 // Don't run unresolved test
423 }
424 else if( text->GetShownText( true ).Matches( wxS( "*${*}*" ) ) )
425 {
426 std::shared_ptr<ERC_ITEM> ercItem = ERC_ITEM::Create( ERCE_UNRESOLVED_VARIABLE );
427 ercItem->SetErrorMessage( _( "Unresolved text variable in drawing sheet" ) );
428 ercItem->SetSheetSpecificPath( sheet );
429
430 SCH_MARKER* marker = new SCH_MARKER( std::move( ercItem ), text->GetPosition() );
431 screen->Append( marker );
432 }
433 }
434 }
435 }
436}
437
438
440{
441 wxString msg;
442 int err_count = 0;
443 std::vector<std::shared_ptr<BUS_ALIAS>> aliases;
444
445 for( SCH_SCREEN* screen = m_screens.GetFirst(); screen; screen = m_screens.GetNext() )
446 {
447 const auto& screen_aliases = screen->GetBusAliases();
448
449 for( const std::shared_ptr<BUS_ALIAS>& alias : screen_aliases )
450 {
451 std::vector<wxString> aliasMembers = alias->Members();
452 std::sort( aliasMembers.begin(), aliasMembers.end() );
453
454 for( const std::shared_ptr<BUS_ALIAS>& test : aliases )
455 {
456 std::vector<wxString> testMembers = test->Members();
457 std::sort( testMembers.begin(), testMembers.end() );
458
459 if( alias->GetName() == test->GetName() && aliasMembers != testMembers )
460 {
461 msg.Printf( _( "Bus alias %s has conflicting definitions on %s and %s" ),
462 alias->GetName(),
463 alias->GetParent()->GetFileName(),
464 test->GetParent()->GetFileName() );
465
466 std::shared_ptr<ERC_ITEM> ercItem = ERC_ITEM::Create( ERCE_BUS_ALIAS_CONFLICT );
467 ercItem->SetErrorMessage( msg );
468
469 SCH_MARKER* marker = new SCH_MARKER( std::move( ercItem ), VECTOR2I() );
470 test->GetParent()->Append( marker );
471
472 ++err_count;
473 }
474 }
475 }
476
477 aliases.insert( aliases.end(), screen_aliases.begin(), screen_aliases.end() );
478 }
479
480 return err_count;
481}
482
483
485{
486 int errors = 0;
487
488 for( std::pair<const wxString, SCH_REFERENCE_LIST>& symbol : m_refMap )
489 {
490 SCH_REFERENCE_LIST& refList = symbol.second;
491
492 if( refList.GetCount() == 0 )
493 {
494 wxFAIL; // it should not happen
495 continue;
496 }
497
498 // Reference footprint
499 SCH_SYMBOL* unit = nullptr;
500 wxString unitName;
501 wxString unitFP;
502
503 for( size_t ii = 0; ii < refList.GetCount(); ++ii )
504 {
505 SCH_SHEET_PATH sheetPath = refList.GetItem( ii ).GetSheetPath();
506 unitFP = refList.GetItem( ii ).GetFootprint();
507
508 if( !unitFP.IsEmpty() )
509 {
510 unit = refList.GetItem( ii ).GetSymbol();
511 unitName = unit->GetRef( &sheetPath, true );
512 break;
513 }
514 }
515
516 for( size_t ii = 0; ii < refList.GetCount(); ++ii )
517 {
518 SCH_REFERENCE& secondRef = refList.GetItem( ii );
519 SCH_SYMBOL* secondUnit = secondRef.GetSymbol();
520 wxString secondName = secondUnit->GetRef( &secondRef.GetSheetPath(), true );
521 const wxString secondFp = secondRef.GetFootprint();
522 wxString msg;
523
524 if( unit && !secondFp.IsEmpty() && unitFP != secondFp )
525 {
526 msg.Printf( _( "Different footprints assigned to %s and %s" ),
527 unitName, secondName );
528
529 std::shared_ptr<ERC_ITEM> ercItem = ERC_ITEM::Create( ERCE_DIFFERENT_UNIT_FP );
530 ercItem->SetErrorMessage( msg );
531 ercItem->SetItems( unit, secondUnit );
532
533 SCH_MARKER* marker = new SCH_MARKER( std::move( ercItem ), secondUnit->GetPosition() );
534 secondRef.GetSheetPath().LastScreen()->Append( marker );
535
536 ++errors;
537 }
538 }
539 }
540
541 return errors;
542}
543
544
546{
547 int errors = 0;
548
549 for( std::pair<const wxString, SCH_REFERENCE_LIST>& symbol : m_refMap )
550 {
551 SCH_REFERENCE_LIST& refList = symbol.second;
552
553 wxCHECK2( refList.GetCount(), continue );
554
555 // Reference unit
556 SCH_REFERENCE& base_ref = refList.GetItem( 0 );
557 SCH_SYMBOL* unit = base_ref.GetSymbol();
558 LIB_SYMBOL* libSymbol = base_ref.GetLibPart();
559
560 if( static_cast<ssize_t>( refList.GetCount() ) == libSymbol->GetUnitCount() )
561 continue;
562
563 std::set<int> lib_units;
564 std::set<int> instance_units;
565 std::set<int> missing_units;
566
567 auto report =
568 [&]( std::set<int>& aMissingUnits, const wxString& aErrorMsg, int aErrorCode )
569 {
570 wxString msg;
571 wxString missing_pin_units = wxS( "[ " );
572 int ii = 0;
573
574 for( int missing_unit : aMissingUnits )
575 {
576 if( ii++ == 3 )
577 {
578 missing_pin_units += wxS( "..." );
579 break;
580 }
581
582 missing_pin_units += libSymbol->GetUnitDisplayName( missing_unit, false ) + ", " ;
583 }
584
585 missing_pin_units.Truncate( missing_pin_units.length() - 2 );
586 missing_pin_units += wxS( " ]" );
587
588 msg.Printf( aErrorMsg, symbol.first, missing_pin_units );
589
590 std::shared_ptr<ERC_ITEM> ercItem = ERC_ITEM::Create( aErrorCode );
591 ercItem->SetErrorMessage( msg );
592 ercItem->SetItems( unit );
593 ercItem->SetSheetSpecificPath( base_ref.GetSheetPath() );
594 ercItem->SetItemsSheetPaths( base_ref.GetSheetPath() );
595
596 SCH_MARKER* marker = new SCH_MARKER( std::move( ercItem ), unit->GetPosition() );
597 base_ref.GetSheetPath().LastScreen()->Append( marker );
598
599 ++errors;
600 };
601
602 for( int ii = 1; ii <= libSymbol->GetUnitCount(); ++ii )
603 lib_units.insert( lib_units.end(), ii );
604
605 for( size_t ii = 0; ii < refList.GetCount(); ++ii )
606 instance_units.insert( instance_units.end(), refList.GetItem( ii ).GetUnit() );
607
608 std::set_difference( lib_units.begin(), lib_units.end(),
609 instance_units.begin(), instance_units.end(),
610 std::inserter( missing_units, missing_units.begin() ) );
611
612 if( !missing_units.empty() && m_settings.IsTestEnabled( ERCE_MISSING_UNIT ) )
613 {
614 report( missing_units, _( "Symbol %s has unplaced units %s" ), ERCE_MISSING_UNIT );
615 }
616
617 std::set<int> missing_power;
618 std::set<int> missing_input;
619 std::set<int> missing_bidi;
620
621 for( int missing_unit : missing_units )
622 {
623 int bodyStyle = 0;
624
625 for( size_t ii = 0; ii < refList.GetCount(); ++ii )
626 {
627 if( refList.GetItem( ii ).GetUnit() == missing_unit )
628 {
629 bodyStyle = refList.GetItem( ii ).GetSymbol()->GetBodyStyle();
630 break;
631 }
632 }
633
634 for( SCH_PIN* pin : libSymbol->GetGraphicalPins( missing_unit, bodyStyle ) )
635 {
636 switch( pin->GetType() )
637 {
639 missing_power.insert( missing_unit );
640 break;
641
643 missing_bidi.insert( missing_unit );
644 break;
645
647 missing_input.insert( missing_unit );
648 break;
649
650 default:
651 break;
652 }
653 }
654 }
655
656 if( !missing_power.empty() && m_settings.IsTestEnabled( ERCE_MISSING_POWER_INPUT_PIN ) )
657 {
658 report( missing_power, _( "Symbol %s has input power pins in units %s that are not placed" ),
660 }
661
662 if( !missing_input.empty() && m_settings.IsTestEnabled( ERCE_MISSING_INPUT_PIN ) )
663 {
664 report( missing_input, _( "Symbol %s has input pins in units %s that are not placed" ),
666 }
667
668 if( !missing_bidi.empty() && m_settings.IsTestEnabled( ERCE_MISSING_BIDI_PIN ) )
669 {
670 report( missing_bidi, _( "Symbol %s has bidirectional pins in units %s that are not placed" ),
672 }
673 }
674
675 return errors;
676}
677
678
680{
681 int err_count = 0;
682 std::shared_ptr<NET_SETTINGS>& settings = m_schematic->Project().GetProjectFile().NetSettings();
683 wxString defaultNetclass = settings->GetDefaultNetclass()->GetName();
684
685 auto logError =
686 [&]( const SCH_SHEET_PATH& sheet, SCH_ITEM* item, const wxString& netclass )
687 {
688 err_count++;
689
690 std::shared_ptr<ERC_ITEM> ercItem = ERC_ITEM::Create( ERCE_UNDEFINED_NETCLASS );
691
692 ercItem->SetItems( item );
693 ercItem->SetErrorMessage( wxString::Format( _( "Netclass %s is not defined" ),
694 netclass ) );
695
696 SCH_MARKER* marker = new SCH_MARKER( std::move( ercItem ), item->GetPosition() );
697 sheet.LastScreen()->Append( marker );
698 };
699
700 for( const SCH_SHEET_PATH& sheet : m_sheetList )
701 {
702 for( SCH_ITEM* item : sheet.LastScreen()->Items() )
703 {
704 item->RunOnChildren(
705 [&]( SCH_ITEM* aChild )
706 {
707 if( aChild->Type() == SCH_FIELD_T )
708 {
709 SCH_FIELD* field = static_cast<SCH_FIELD*>( aChild );
710
711 if( field->GetCanonicalName() == wxT( "Netclass" ) )
712 {
713 wxString netclass = field->GetShownText( &sheet, false );
714
715 if( !netclass.empty() && !netclass.IsSameAs( defaultNetclass )
716 && !settings->HasNetclass( netclass ) )
717 {
718 logError( sheet, item, netclass );
719 }
720 }
721 }
722
723 return true;
724 },
726 }
727 }
728
729 return err_count;
730}
731
732
734{
735 int err_count = 0;
736
737 for( const SCH_SHEET_PATH& sheet : m_sheetList )
738 {
739 std::map<VECTOR2I, std::vector<SCH_ITEM*>> connMap;
740
741 for( SCH_ITEM* item : sheet.LastScreen()->Items().OfType( SCH_LABEL_T ) )
742 {
743 SCH_LABEL* label = static_cast<SCH_LABEL*>( item );
744
745 for( const VECTOR2I& pt : label->GetConnectionPoints() )
746 connMap[pt].emplace_back( label );
747 }
748
749 for( const std::pair<const VECTOR2I, std::vector<SCH_ITEM*>>& pair : connMap )
750 {
751 std::vector<SCH_ITEM*> lines;
752
753 for( SCH_ITEM* item : sheet.LastScreen()->Items().Overlapping( SCH_LINE_T, pair.first ) )
754 {
755 SCH_LINE* line = static_cast<SCH_LINE*>( item );
756
757 if( line->IsGraphicLine() )
758 continue;
759
760 // If the line is connected at the endpoint, then there will be a junction
761 if( !line->IsEndPoint( pair.first ) )
762 lines.emplace_back( line );
763 }
764
765 if( lines.size() > 1 )
766 {
767 err_count++;
768 lines.resize( 3 ); // Only show the first 3 lines and if there are only two, adds a nullptr
769
770 std::shared_ptr<ERC_ITEM> ercItem = ERC_ITEM::Create( ERCE_LABEL_MULTIPLE_WIRES );
771 wxString msg = wxString::Format( _( "Label connects more than one wire at %d, %d" ),
772 pair.first.x, pair.first.y );
773
774 ercItem->SetItems( pair.second.front(), lines[0], lines[1], lines[2] );
775 ercItem->SetErrorMessage( msg );
776 ercItem->SetSheetSpecificPath( sheet );
777
778 SCH_MARKER* marker = new SCH_MARKER( std::move( ercItem ), pair.first );
779 sheet.LastScreen()->Append( marker );
780 }
781 }
782 }
783
784 return err_count;
785}
786
787
789{
790 int err_count = 0;
791
792 auto pinStackAlreadyRepresented =
793 []( SCH_PIN* pin, std::vector<SCH_ITEM*>& collection ) -> bool
794 {
795 for( SCH_ITEM*& item : collection )
796 {
797 if( item->Type() == SCH_PIN_T && item->GetParentSymbol() == pin->GetParentSymbol() )
798 {
799 if( pin->IsVisible() && !static_cast<SCH_PIN*>( item )->IsVisible() )
800 item = pin;
801
802 return true;
803 }
804 }
805
806 return false;
807 };
808
809 for( const SCH_SHEET_PATH& sheet : m_sheetList )
810 {
811 std::map<VECTOR2I, std::vector<SCH_ITEM*>> connMap;
812 SCH_SCREEN* screen = sheet.LastScreen();
813
814 for( SCH_ITEM* item : screen->Items().OfType( SCH_SYMBOL_T ) )
815 {
816 SCH_SYMBOL* symbol = static_cast<SCH_SYMBOL*>( item );
817
818 for( SCH_PIN* pin : symbol->GetPins( &sheet ) )
819 {
820 std::vector<SCH_ITEM*>& entry = connMap[pin->GetPosition()];
821
822 // Only one pin per pin-stack.
823 if( pinStackAlreadyRepresented( pin, entry ) )
824 continue;
825
826 entry.emplace_back( pin );
827 }
828 }
829
830 for( SCH_ITEM* item : screen->Items().OfType( SCH_LINE_T ) )
831 {
832 SCH_LINE* line = static_cast<SCH_LINE*>( item );
833
834 if( line->IsGraphicLine() )
835 continue;
836
837 for( const VECTOR2I& pt : line->GetConnectionPoints() )
838 connMap[pt].emplace_back( line );
839 }
840
841 for( const std::pair<const VECTOR2I, std::vector<SCH_ITEM*>>& pair : connMap )
842 {
843 if( pair.second.size() >= 4 )
844 {
845 err_count++;
846
847 std::shared_ptr<ERC_ITEM> ercItem = ERC_ITEM::Create( ERCE_FOUR_WAY_JUNCTION );
848
849 ercItem->SetItems( pair.second[0], pair.second[1], pair.second[2], pair.second[3] );
850
851 wxString msg = wxString::Format( _( "Four items connected at %d, %d" ),
852 pair.first.x, pair.first.y );
853 ercItem->SetErrorMessage( msg );
854
855 ercItem->SetSheetSpecificPath( sheet );
856
857 SCH_MARKER* marker = new SCH_MARKER( std::move( ercItem ), pair.first );
858 sheet.LastScreen()->Append( marker );
859 }
860 }
861 }
862
863 return err_count;
864}
865
866
868{
869 int err_count = 0;
870
871 for( const SCH_SHEET_PATH& sheet : m_sheetList )
872 {
873 std::map<VECTOR2I, std::vector<SCH_ITEM*>> pinMap;
874
875 auto addOther =
876 [&]( const VECTOR2I& pt, SCH_ITEM* aOther )
877 {
878 if( pinMap.count( pt ) )
879 pinMap[pt].emplace_back( aOther );
880 };
881
882 for( SCH_ITEM* item : sheet.LastScreen()->Items().OfType( SCH_SYMBOL_T ) )
883 {
884 SCH_SYMBOL* symbol = static_cast<SCH_SYMBOL*>( item );
885
886 for( SCH_PIN* pin : symbol->GetPins( &sheet ) )
887 {
888 if( pin->GetLibPin()->GetType() == ELECTRICAL_PINTYPE::PT_NC )
889 pinMap[pin->GetPosition()].emplace_back( pin );
890 }
891 }
892
893 for( SCH_ITEM* item : sheet.LastScreen()->Items() )
894 {
895 if( item->Type() == SCH_SYMBOL_T )
896 {
897 SCH_SYMBOL* symbol = static_cast<SCH_SYMBOL*>( item );
898
899 for( SCH_PIN* pin : symbol->GetPins( &sheet ) )
900 {
901 if( pin->GetLibPin()->GetType() != ELECTRICAL_PINTYPE::PT_NC )
902 addOther( pin->GetPosition(), pin );
903 }
904 }
905 else if( item->IsConnectable() && item->Type() != SCH_NO_CONNECT_T )
906 {
907 for( const VECTOR2I& pt : item->GetConnectionPoints() )
908 addOther( pt, item );
909 }
910 }
911
912 for( const std::pair<const VECTOR2I, std::vector<SCH_ITEM*>>& pair : pinMap )
913 {
914 if( pair.second.size() > 1 )
915 {
916 err_count++;
917
918 std::shared_ptr<ERC_ITEM> ercItem = ERC_ITEM::Create( ERCE_NOCONNECT_CONNECTED );
919
920 ercItem->SetItems( pair.second[0], pair.second[1],
921 pair.second.size() > 2 ? pair.second[2] : nullptr,
922 pair.second.size() > 3 ? pair.second[3] : nullptr );
923 ercItem->SetErrorMessage( _( "Pin with 'no connection' type is connected" ) );
924 ercItem->SetSheetSpecificPath( sheet );
925
926 SCH_MARKER* marker = new SCH_MARKER( std::move( ercItem ), pair.first );
927 sheet.LastScreen()->Append( marker );
928 }
929 }
930 }
931
932 return err_count;
933}
934
935
937{
938 int errors = 0;
939
940 for( const std::pair<NET_NAME_CODE_CACHE_KEY, std::vector<CONNECTION_SUBGRAPH*>> net : m_nets )
941 {
942 using iterator_t = std::vector<ERC_SCH_PIN_CONTEXT>::iterator;
943 std::vector<ERC_SCH_PIN_CONTEXT> pins;
944 std::unordered_map<EDA_ITEM*, SCH_SCREEN*> pinToScreenMap;
945 bool has_noconnect = false;
946
947 for( CONNECTION_SUBGRAPH* subgraph: net.second )
948 {
949 if( subgraph->GetNoConnect() )
950 has_noconnect = true;
951
952 for( SCH_ITEM* item : subgraph->GetItems() )
953 {
954 if( item->Type() == SCH_PIN_T )
955 {
956 pins.emplace_back( static_cast<SCH_PIN*>( item ), subgraph->GetSheet() );
957 pinToScreenMap[item] = subgraph->GetSheet().LastScreen();
958 }
959 }
960 }
961
962 std::sort( pins.begin(), pins.end(),
963 []( const ERC_SCH_PIN_CONTEXT& lhs, const ERC_SCH_PIN_CONTEXT& rhs )
964 {
965 int ret = StrNumCmp( lhs.Pin()->GetParentSymbol()->GetRef( &lhs.Sheet() ),
966 rhs.Pin()->GetParentSymbol()->GetRef( &rhs.Sheet() ) );
967
968 if( ret == 0 )
969 ret = StrNumCmp( lhs.Pin()->GetNumber(), rhs.Pin()->GetNumber() );
970
971 if( ret == 0 )
972 ret = lhs < rhs; // Fallback to hash to guarantee deterministic sort
973
974 return ret < 0;
975 } );
976
977 ERC_SCH_PIN_CONTEXT needsDriver;
979 bool hasDriver = false;
980
981 // We need different drivers for power nets and normal nets.
982 // A power net has at least one pin having the ELECTRICAL_PINTYPE::PT_POWER_IN
983 // and power nets can be driven only by ELECTRICAL_PINTYPE::PT_POWER_OUT pins
984 bool ispowerNet = false;
985
986 for( ERC_SCH_PIN_CONTEXT& refPin : pins )
987 {
988 if( refPin.Pin()->GetType() == ELECTRICAL_PINTYPE::PT_POWER_IN )
989 {
990 ispowerNet = true;
991 break;
992 }
993 }
994
995 std::vector<std::tuple<iterator_t, iterator_t, PIN_ERROR>> pin_mismatches;
996 std::map<iterator_t, int> pin_mismatch_counts;
997
998 for( auto refIt = pins.begin(); refIt != pins.end(); ++refIt )
999 {
1000 ERC_SCH_PIN_CONTEXT& refPin = *refIt;
1001 ELECTRICAL_PINTYPE refType = refPin.Pin()->GetType();
1002
1003 if( DrivenPinTypes.contains( refType ) )
1004 {
1005 // needsDriver will be the pin shown in the error report eventually, so try to
1006 // upgrade to a "better" pin if possible: something visible and only a power symbol
1007 // if this net needs a power driver
1008 if( !needsDriver.Pin()
1009 || ( !needsDriver.Pin()->IsVisible() && refPin.Pin()->IsVisible() )
1010 || ( ispowerNet != ( needsDriverType == ELECTRICAL_PINTYPE::PT_POWER_IN )
1011 && ispowerNet == ( refType == ELECTRICAL_PINTYPE::PT_POWER_IN ) ) )
1012 {
1013 needsDriver = refPin;
1014 needsDriverType = needsDriver.Pin()->GetType();
1015 }
1016 }
1017
1018 if( ispowerNet )
1019 hasDriver |= ( DrivingPowerPinTypes.count( refType ) != 0 );
1020 else
1021 hasDriver |= ( DrivingPinTypes.count( refType ) != 0 );
1022
1023 for( auto testIt = refIt + 1; testIt != pins.end(); ++testIt )
1024 {
1025 ERC_SCH_PIN_CONTEXT& testPin = *testIt;
1026
1027 // Multiple pins in the same symbol that share a type,
1028 // name and position are considered
1029 // "stacked" and shouldn't trigger ERC errors
1030 if( refPin.Pin()->IsStacked( testPin.Pin() ) && refPin.Sheet() == testPin.Sheet() )
1031 continue;
1032
1033 ELECTRICAL_PINTYPE testType = testPin.Pin()->GetType();
1034
1035 if( ispowerNet )
1036 hasDriver |= DrivingPowerPinTypes.contains( testType );
1037 else
1038 hasDriver |= DrivingPinTypes.contains( testType );
1039
1040 PIN_ERROR erc = m_settings.GetPinMapValue( refType, testType );
1041
1042 if( erc != PIN_ERROR::OK && m_settings.IsTestEnabled( ERCE_PIN_TO_PIN_WARNING ) )
1043 {
1044 pin_mismatches.emplace_back(
1045 std::tuple<iterator_t, iterator_t, PIN_ERROR>{ refIt, testIt, erc } );
1046
1047 if( m_settings.GetERCSortingMetric() == ERC_PIN_SORTING_METRIC::SM_HEURISTICS )
1048 {
1049 pin_mismatch_counts[refIt] =
1050 m_settings.GetPinTypeWeight( ( *refIt ).Pin()->GetType() );
1051
1052 pin_mismatch_counts[testIt] =
1053 m_settings.GetPinTypeWeight( ( *testIt ).Pin()->GetType() );
1054 }
1055 else
1056 {
1057 if( !pin_mismatch_counts.contains( testIt ) )
1058 pin_mismatch_counts.emplace( testIt, 1 );
1059 else
1060 pin_mismatch_counts[testIt]++;
1061
1062 if( !pin_mismatch_counts.contains( refIt ) )
1063 pin_mismatch_counts.emplace( refIt, 1 );
1064 else
1065 pin_mismatch_counts[refIt]++;
1066 }
1067 }
1068 }
1069 }
1070
1071 std::multimap<size_t, iterator_t, std::greater<size_t>> pins_dsc;
1072
1073 std::transform( pin_mismatch_counts.begin(), pin_mismatch_counts.end(),
1074 std::inserter( pins_dsc, pins_dsc.begin() ),
1075 []( const auto& p )
1076 {
1077 return std::pair<size_t, iterator_t>( p.second, p.first );
1078 } );
1079
1080 for( const auto& [amount, pinItBind] : pins_dsc )
1081 {
1082 auto& pinIt = pinItBind;
1083
1084 if( pin_mismatches.empty() )
1085 break;
1086
1087 SCH_PIN* pin = ( *pinIt ).Pin();
1088 VECTOR2I position = pin->GetPosition();
1089
1090 iterator_t nearest_pin = pins.end();
1091 double smallest_distance = std::numeric_limits<double>::infinity();
1092 PIN_ERROR erc;
1093
1094 std::erase_if(
1095 pin_mismatches,
1096 [&]( const auto& tuple )
1097 {
1098 iterator_t other;
1099
1100 if( pinIt == std::get<0>( tuple ) )
1101 other = std::get<1>( tuple );
1102 else if( pinIt == std::get<1>( tuple ) )
1103 other = std::get<0>( tuple );
1104 else
1105 return false;
1106
1107 if( ( *pinIt ).Sheet().Cmp( ( *other ).Sheet() ) != 0 )
1108 {
1109 if( std::isinf( smallest_distance ) )
1110 {
1111 nearest_pin = other;
1112 erc = std::get<2>( tuple );
1113 }
1114 }
1115 else
1116 {
1117 double distance = position.Distance( ( *other ).Pin()->GetPosition() );
1118
1119 if( std::isinf( smallest_distance ) || distance < smallest_distance )
1120 {
1121 smallest_distance = distance;
1122 nearest_pin = other;
1123 erc = std::get<2>( tuple );
1124 }
1125 }
1126
1127 return true;
1128 } );
1129
1130 if( nearest_pin != pins.end() )
1131 {
1132 SCH_PIN* other_pin = ( *nearest_pin ).Pin();
1133
1134 std::shared_ptr<ERC_ITEM> ercItem =
1137 ercItem->SetItems( pin, other_pin );
1138 ercItem->SetSheetSpecificPath( ( *pinIt ).Sheet() );
1139 ercItem->SetItemsSheetPaths( ( *pinIt ).Sheet(), ( *nearest_pin ).Sheet() );
1140
1141 ercItem->SetErrorMessage(
1142 wxString::Format( _( "Pins of type %s and %s are connected" ),
1143 ElectricalPinTypeGetText( pin->GetType() ),
1144 ElectricalPinTypeGetText( other_pin->GetType() ) ) );
1145
1146 SCH_MARKER* marker = new SCH_MARKER( std::move( ercItem ), pin->GetPosition() );
1147 pinToScreenMap[pin]->Append( marker );
1148 errors++;
1149 }
1150 }
1151
1152 if( needsDriver.Pin() && !hasDriver && !has_noconnect )
1153 {
1154 int err_code = ispowerNet ? ERCE_POWERPIN_NOT_DRIVEN : ERCE_PIN_NOT_DRIVEN;
1155
1156 if( m_settings.IsTestEnabled( err_code ) )
1157 {
1158 std::shared_ptr<ERC_ITEM> ercItem = ERC_ITEM::Create( err_code );
1159
1160 ercItem->SetItems( needsDriver.Pin() );
1161 ercItem->SetSheetSpecificPath( needsDriver.Sheet() );
1162 ercItem->SetItemsSheetPaths( needsDriver.Sheet() );
1163
1164 SCH_MARKER* marker = new SCH_MARKER( std::move( ercItem ),
1165 needsDriver.Pin()->GetPosition() );
1166 pinToScreenMap[needsDriver.Pin()]->Append( marker );
1167 errors++;
1168 }
1169 }
1170 }
1171
1172 return errors;
1173}
1174
1175
1177{
1178 int errors = 0;
1179
1180 std::unordered_map<wxString, std::pair<wxString, SCH_PIN*>> pinToNetMap;
1181
1182 for( const std::pair<NET_NAME_CODE_CACHE_KEY, std::vector<CONNECTION_SUBGRAPH*>> net : m_nets )
1183 {
1184 const wxString& netName = net.first.Name;
1185
1186 for( CONNECTION_SUBGRAPH* subgraph : net.second )
1187 {
1188 for( SCH_ITEM* item : subgraph->GetItems() )
1189 {
1190 if( item->Type() == SCH_PIN_T )
1191 {
1192 SCH_PIN* pin = static_cast<SCH_PIN*>( item );
1193 const SCH_SHEET_PATH& sheet = subgraph->GetSheet();
1194
1195 if( !pin->GetLibPin()->GetParentSymbol()->IsMultiUnit() )
1196 continue;
1197
1198 wxString name = pin->GetParentSymbol()->GetRef( &sheet ) +
1199 + ":" + pin->GetShownNumber();
1200
1201 if( !pinToNetMap.count( name ) )
1202 {
1203 pinToNetMap[name] = std::make_pair( netName, pin );
1204 }
1205 else if( pinToNetMap[name].first != netName )
1206 {
1207 std::shared_ptr<ERC_ITEM> ercItem = ERC_ITEM::Create( ERCE_DIFFERENT_UNIT_NET );
1208
1209 ercItem->SetErrorMessage( wxString::Format( _( "Pin %s is connected to both %s and %s" ),
1210 pin->GetShownNumber(),
1211 netName,
1212 pinToNetMap[name].first ) );
1213
1214 ercItem->SetItems( pin, pinToNetMap[name].second );
1215 ercItem->SetSheetSpecificPath( sheet );
1216 ercItem->SetItemsSheetPaths( sheet, sheet );
1217
1218 SCH_MARKER* marker = new SCH_MARKER( std::move( ercItem ), pin->GetPosition() );
1219 sheet.LastScreen()->Append( marker );
1220 errors += 1;
1221 }
1222 }
1223 }
1224 }
1225 }
1226
1227 return errors;
1228}
1229
1230
1232{
1233 int errors = 0;
1234
1235 auto isGround = []( const wxString& txt )
1236 {
1237 wxString upper = txt.Upper();
1238 return upper.Contains( wxT( "GND" ) );
1239 };
1240
1241 for( const SCH_SHEET_PATH& sheet : m_sheetList )
1242 {
1243 SCH_SCREEN* screen = sheet.LastScreen();
1244
1245 for( SCH_ITEM* item : screen->Items().OfType( SCH_SYMBOL_T ) )
1246 {
1247 SCH_SYMBOL* symbol = static_cast<SCH_SYMBOL*>( item );
1248 bool hasGroundNet = false;
1249 std::vector<SCH_PIN*> mismatched;
1250
1251 for( SCH_PIN* pin : symbol->GetPins( &sheet ) )
1252 {
1253 SCH_CONNECTION* conn = pin->Connection( &sheet );
1254 wxString net = conn ? conn->GetNetName() : wxString();
1255 bool netIsGround = isGround( net );
1256
1257 // We are only interested in power pins
1258 if( pin->GetType() != ELECTRICAL_PINTYPE::PT_POWER_OUT
1259 && pin->GetType() != ELECTRICAL_PINTYPE::PT_POWER_IN )
1260 {
1261 continue;
1262 }
1263
1264 if( netIsGround )
1265 hasGroundNet = true;
1266
1267 if( isGround( pin->GetShownName() ) && !netIsGround )
1268 mismatched.push_back( pin );
1269 }
1270
1271 if( hasGroundNet )
1272 {
1273 for( SCH_PIN* pin : mismatched )
1274 {
1275 std::shared_ptr<ERC_ITEM> ercItem =
1277
1278 ercItem->SetErrorMessage(
1279 wxString::Format( _( "Pin %s not connected to ground net" ),
1280 pin->GetShownName() ) );
1281 ercItem->SetItems( pin );
1282 ercItem->SetSheetSpecificPath( sheet );
1283 ercItem->SetItemsSheetPaths( sheet );
1284
1285 SCH_MARKER* marker =
1286 new SCH_MARKER( std::move( ercItem ), pin->GetPosition() );
1287 screen->Append( marker );
1288 errors++;
1289 }
1290 }
1291 }
1292 }
1293
1294 return errors;
1295}
1296
1297
1299{
1300 int warnings = 0;
1301
1302 for( const SCH_SHEET_PATH& sheet : m_sheetList )
1303 {
1304 SCH_SCREEN* screen = sheet.LastScreen();
1305
1306 for( SCH_ITEM* item : screen->Items().OfType( SCH_SYMBOL_T ) )
1307 {
1308 SCH_SYMBOL* symbol = static_cast<SCH_SYMBOL*>( item );
1309
1310 for( SCH_PIN* pin : symbol->GetPins( &sheet ) )
1311 {
1312 bool valid;
1313 pin->GetStackedPinNumbers( &valid );
1314
1315 if( !valid )
1316 {
1317 std::shared_ptr<ERC_ITEM> ercItem =
1319 ercItem->SetItems( pin );
1320 ercItem->SetSheetSpecificPath( sheet );
1321 ercItem->SetItemsSheetPaths( sheet );
1322
1323 SCH_MARKER* marker =
1324 new SCH_MARKER( std::move( ercItem ), pin->GetPosition() );
1325 screen->Append( marker );
1326 warnings++;
1327 }
1328 }
1329 }
1330 }
1331
1332 return warnings;
1333}
1334
1335
1337{
1338 int errCount = 0;
1339
1340 std::unordered_map<wxString, std::pair<SCH_ITEM*, SCH_SHEET_PATH>> globalLabels;
1341 std::unordered_map<wxString, std::pair<SCH_ITEM*, SCH_SHEET_PATH>> localLabels;
1342
1343 for( const std::pair<NET_NAME_CODE_CACHE_KEY, std::vector<CONNECTION_SUBGRAPH*>> net : m_nets )
1344 {
1345 for( CONNECTION_SUBGRAPH* subgraph : net.second )
1346 {
1347 const SCH_SHEET_PATH& sheet = subgraph->GetSheet();
1348
1349 for( SCH_ITEM* item : subgraph->GetItems() )
1350 {
1351 if( item->Type() == SCH_LABEL_T || item->Type() == SCH_GLOBAL_LABEL_T )
1352 {
1353 SCH_LABEL_BASE* label = static_cast<SCH_LABEL_BASE*>( item );
1354 wxString text = label->GetShownText( &sheet, false );
1355
1356 auto& map = item->Type() == SCH_LABEL_T ? localLabels : globalLabels;
1357
1358 if( !map.count( text ) )
1359 {
1360 map[text] = std::make_pair( label, sheet );
1361 }
1362 }
1363 }
1364 }
1365 }
1366
1367 for( auto& [globalText, globalItem] : globalLabels )
1368 {
1369 for( auto& [localText, localItem] : localLabels )
1370 {
1371 if( globalText == localText )
1372 {
1373 std::shared_ptr<ERC_ITEM> ercItem = ERC_ITEM::Create( ERCE_SAME_LOCAL_GLOBAL_LABEL );
1374 ercItem->SetItems( globalItem.first, localItem.first );
1375 ercItem->SetSheetSpecificPath( globalItem.second );
1376 ercItem->SetItemsSheetPaths( globalItem.second, localItem.second );
1377
1378 SCH_MARKER* marker = new SCH_MARKER( std::move( ercItem ),
1379 globalItem.first->GetPosition() );
1380 globalItem.second.LastScreen()->Append( marker );
1381
1382 errCount++;
1383 }
1384 }
1385 }
1386
1387 return errCount;
1388}
1389
1390
1392{
1393 int errors = 0;
1394 std::unordered_map<wxString, std::vector<std::tuple<wxString, SCH_ITEM*, SCH_SHEET_PATH>>> generalMap;
1395
1396 auto logError =
1397 [&]( const wxString& normalized, SCH_ITEM* item, const SCH_SHEET_PATH& sheet,
1398 const std::tuple<wxString, SCH_ITEM*, SCH_SHEET_PATH>& other )
1399 {
1400 auto& [otherText, otherItem, otherSheet] = other;
1401 ERCE_T typeOfWarning = ERCE_SIMILAR_LABELS;
1402
1403 if( item->Type() == SCH_PIN_T && otherItem->Type() == SCH_PIN_T )
1404 {
1405 //Two Pins
1406 typeOfWarning = ERCE_SIMILAR_POWER;
1407 }
1408 else if( item->Type() == SCH_PIN_T || otherItem->Type() == SCH_PIN_T )
1409 {
1410 //Pin and Label
1411 typeOfWarning = ERCE_SIMILAR_LABEL_AND_POWER;
1412 }
1413 else
1414 {
1415 //Two Labels
1416 typeOfWarning = ERCE_SIMILAR_LABELS;
1417 }
1418
1419 std::shared_ptr<ERC_ITEM> ercItem = ERC_ITEM::Create( typeOfWarning );
1420 ercItem->SetItems( item, otherItem );
1421 ercItem->SetSheetSpecificPath( sheet );
1422 ercItem->SetItemsSheetPaths( sheet, otherSheet );
1423
1424 SCH_MARKER* marker = new SCH_MARKER( std::move( ercItem ), item->GetPosition() );
1425 sheet.LastScreen()->Append( marker );
1426 };
1427
1428 for( const std::pair<NET_NAME_CODE_CACHE_KEY, std::vector<CONNECTION_SUBGRAPH*>> net : m_nets )
1429 {
1430 for( CONNECTION_SUBGRAPH* subgraph : net.second )
1431 {
1432 const SCH_SHEET_PATH& sheet = subgraph->GetSheet();
1433
1434 for( SCH_ITEM* item : subgraph->GetItems() )
1435 {
1436 switch( item->Type() )
1437 {
1438 case SCH_LABEL_T:
1439 case SCH_HIER_LABEL_T:
1440 case SCH_GLOBAL_LABEL_T:
1441 {
1442 SCH_LABEL_BASE* label = static_cast<SCH_LABEL_BASE*>( item );
1443 wxString unnormalized = label->GetShownText( &sheet, false );
1444 wxString normalized = unnormalized.Lower();
1445
1446 generalMap[normalized].emplace_back( std::make_tuple( unnormalized, label, sheet ) );
1447
1448 for( const auto& otherTuple : generalMap.at( normalized ) )
1449 {
1450 const auto& [otherText, otherItem, otherSheet] = otherTuple;
1451
1452 if( unnormalized != otherText )
1453 {
1454 // Similar local labels on different sheets are fine
1455 if( item->Type() == SCH_LABEL_T && otherItem->Type() == SCH_LABEL_T
1456 && sheet != otherSheet )
1457 {
1458 continue;
1459 }
1460
1461 logError( normalized, label, sheet, otherTuple );
1462 errors += 1;
1463 }
1464 }
1465
1466 break;
1467 }
1468 case SCH_PIN_T:
1469 {
1470 SCH_PIN* pin = static_cast<SCH_PIN*>( item );
1471
1472 if( !pin->IsPower() )
1473 continue;
1474
1475 SCH_SYMBOL* symbol = static_cast<SCH_SYMBOL*>( pin->GetParentSymbol() );
1476 wxString unnormalized = symbol->GetValue( true, &sheet, false );
1477 wxString normalized = unnormalized.Lower();
1478
1479 generalMap[normalized].emplace_back( std::make_tuple( unnormalized, pin, sheet ) );
1480
1481 for( const auto& otherTuple : generalMap.at( normalized ) )
1482 {
1483 const auto& [otherText, otherItem, otherSheet] = otherTuple;
1484
1485 if( unnormalized != otherText )
1486 {
1487 logError( normalized, pin, sheet, otherTuple );
1488 errors += 1;
1489 }
1490 }
1491
1492 break;
1493 }
1494
1495 default:
1496 break;
1497 }
1498 }
1499 }
1500 }
1501
1502 return errors;
1503}
1504
1505
1507{
1508 wxCHECK( m_schematic, 0 );
1509
1511 wxString msg;
1512 int err_count = 0;
1513
1514 for( SCH_SCREEN* screen = m_screens.GetFirst(); screen; screen = m_screens.GetNext() )
1515 {
1516 std::vector<SCH_MARKER*> markers;
1517
1518 for( SCH_ITEM* item : screen->Items().OfType( SCH_SYMBOL_T ) )
1519 {
1520 SCH_SYMBOL* symbol = static_cast<SCH_SYMBOL*>( item );
1521 LIB_SYMBOL* libSymbolInSchematic = symbol->GetLibSymbolRef().get();
1522
1523 if( !libSymbolInSchematic )
1524 continue;
1525
1526 wxString libName = symbol->GetLibId().GetLibNickname();
1527 const LIB_TABLE_ROW* libTableRow = libTable->FindRow( libName, true );
1528
1529 if( !libTableRow )
1530 {
1531 if( m_settings.IsTestEnabled( ERCE_LIB_SYMBOL_ISSUES ) )
1532 {
1533 std::shared_ptr<ERC_ITEM> ercItem = ERC_ITEM::Create( ERCE_LIB_SYMBOL_ISSUES );
1534 ercItem->SetItems( symbol );
1535 msg.Printf( _( "The current configuration does not include the symbol library '%s'" ),
1536 UnescapeString( libName ) );
1537 ercItem->SetErrorMessage( msg );
1538
1539 markers.emplace_back( new SCH_MARKER( std::move( ercItem ), symbol->GetPosition() ) );
1540 }
1541
1542 continue;
1543 }
1544 else if( !libTable->HasLibrary( libName, true ) )
1545 {
1546 if( m_settings.IsTestEnabled( ERCE_LIB_SYMBOL_ISSUES ) )
1547 {
1548 std::shared_ptr<ERC_ITEM> ercItem = ERC_ITEM::Create( ERCE_LIB_SYMBOL_ISSUES );
1549 ercItem->SetItems( symbol );
1550 msg.Printf( _( "The symbol library '%s' is not enabled in the current configuration" ),
1551 UnescapeString( libName ) );
1552 ercItem->SetErrorMessage( msg );
1553
1554 markers.emplace_back( new SCH_MARKER( std::move( ercItem ), symbol->GetPosition() ) );
1555 }
1556
1557 continue;
1558 }
1559 else if( !libTableRow->LibraryExists() )
1560 {
1561 if( m_settings.IsTestEnabled( ERCE_LIB_SYMBOL_ISSUES ) )
1562 {
1563 std::shared_ptr<ERC_ITEM> ercItem = ERC_ITEM::Create( ERCE_LIB_SYMBOL_ISSUES );
1564 ercItem->SetItems( symbol );
1565 msg.Printf( _( "The symbol library '%s' was not found at '%s'" ),
1566 UnescapeString( libName ),
1567 libTableRow->GetFullURI( true ) );
1568 ercItem->SetErrorMessage( msg );
1569
1570 markers.emplace_back( new SCH_MARKER( std::move( ercItem ), symbol->GetPosition() ) );
1571 }
1572
1573 continue;
1574 }
1575
1576 wxString symbolName = symbol->GetLibId().GetLibItemName();
1577 LIB_SYMBOL* libSymbol = SchGetLibSymbol( symbol->GetLibId(), libTable );
1578
1579 if( libSymbol == nullptr )
1580 {
1581 if( m_settings.IsTestEnabled( ERCE_LIB_SYMBOL_ISSUES ) )
1582 {
1583 std::shared_ptr<ERC_ITEM> ercItem = ERC_ITEM::Create( ERCE_LIB_SYMBOL_ISSUES );
1584 ercItem->SetItems( symbol );
1585 msg.Printf( _( "Symbol '%s' not found in symbol library '%s'" ),
1586 UnescapeString( symbolName ),
1587 UnescapeString( libName ) );
1588 ercItem->SetErrorMessage( msg );
1589
1590 markers.emplace_back( new SCH_MARKER( std::move( ercItem ), symbol->GetPosition() ) );
1591 }
1592
1593 continue;
1594 }
1595
1596 std::unique_ptr<LIB_SYMBOL> flattenedSymbol = libSymbol->Flatten();
1598
1599 if( m_settings.IsTestEnabled( ERCE_LIB_SYMBOL_MISMATCH ) )
1600 {
1601 // We have to check for duplicate pins first as they will cause Compare() to fail.
1602 std::vector<wxString> messages;
1603 UNITS_PROVIDER unitsProvider( schIUScale, EDA_UNITS::MILS );
1604 CheckDuplicatePins( libSymbolInSchematic, messages, &unitsProvider );
1605
1606 if( !messages.empty() )
1607 {
1608 std::shared_ptr<ERC_ITEM> ercItem = ERC_ITEM::Create( ERCE_DUPLICATE_PIN_ERROR );
1609 ercItem->SetItems( symbol );
1610 msg.Printf( _( "Symbol '%s' has multiple pins with the same pin number" ),
1611 UnescapeString( symbolName ) );
1612 ercItem->SetErrorMessage( msg );
1613
1614 markers.emplace_back( new SCH_MARKER( std::move( ercItem ), symbol->GetPosition() ) );
1615 }
1616 else if( flattenedSymbol->Compare( *libSymbolInSchematic, flags ) != 0 )
1617 {
1618 std::shared_ptr<ERC_ITEM> ercItem = ERC_ITEM::Create( ERCE_LIB_SYMBOL_MISMATCH );
1619 ercItem->SetItems( symbol );
1620 msg.Printf( _( "Symbol '%s' doesn't match copy in library '%s'" ),
1621 UnescapeString( symbolName ),
1622 UnescapeString( libName ) );
1623 ercItem->SetErrorMessage( msg );
1624
1625 markers.emplace_back( new SCH_MARKER( std::move( ercItem ), symbol->GetPosition() ) );
1626 }
1627 }
1628 }
1629
1630 for( SCH_MARKER* marker : markers )
1631 {
1632 screen->Append( marker );
1633 err_count += 1;
1634 }
1635 }
1636
1637 return err_count;
1638}
1639
1640
1642{
1643 wxCHECK( m_schematic, 0 );
1644
1645 wxString msg;
1646 int err_count = 0;
1647
1648 typedef int (*TESTER_FN_PTR)( const wxString&, PROJECT* );
1649
1650 TESTER_FN_PTR linkTester = (TESTER_FN_PTR) aCvPcb->IfaceOrAddress( KIFACE_TEST_FOOTPRINT_LINK );
1651
1652 for( SCH_SHEET_PATH& sheet : m_sheetList )
1653 {
1654 std::vector<SCH_MARKER*> markers;
1655
1656 for( SCH_ITEM* item : sheet.LastScreen()->Items().OfType( SCH_SYMBOL_T ) )
1657 {
1658 SCH_SYMBOL* symbol = static_cast<SCH_SYMBOL*>( item );
1659 wxString footprint = symbol->GetFootprintFieldText( true, &sheet, false );
1660
1661 if( footprint.IsEmpty() )
1662 continue;
1663
1664 LIB_ID fpID;
1665
1666 if( fpID.Parse( footprint, true ) >= 0 )
1667 {
1668 std::shared_ptr<ERC_ITEM> ercItem = ERC_ITEM::Create( ERCE_FOOTPRINT_LINK_ISSUES );
1669 msg.Printf( _( "'%s' is not a valid footprint identifier" ), footprint );
1670 ercItem->SetErrorMessage( msg );
1671 ercItem->SetItems( symbol );
1672 markers.emplace_back( new SCH_MARKER( std::move( ercItem ), symbol->GetPosition() ) );
1673 continue;
1674 }
1675
1676 wxString libName = fpID.GetLibNickname();
1677 wxString fpName = fpID.GetLibItemName();
1678 int ret = (linkTester)( footprint, aProject );
1679
1681 {
1682 std::shared_ptr<ERC_ITEM> ercItem = ERC_ITEM::Create( ERCE_FOOTPRINT_LINK_ISSUES );
1683 msg.Printf( _( "The current configuration does not include the footprint library '%s'" ),
1684 libName );
1685 ercItem->SetErrorMessage( msg );
1686 ercItem->SetItems( symbol );
1687 markers.emplace_back( new SCH_MARKER( std::move( ercItem ), symbol->GetPosition() ) );
1688 }
1690 {
1691 std::shared_ptr<ERC_ITEM> ercItem = ERC_ITEM::Create( ERCE_FOOTPRINT_LINK_ISSUES );
1692 msg.Printf( _( "The footprint library '%s' is not enabled in the current configuration" ),
1693 libName );
1694 ercItem->SetErrorMessage( msg );
1695 ercItem->SetItems( symbol );
1696 markers.emplace_back( new SCH_MARKER( std::move( ercItem ), symbol->GetPosition() ) );
1697 }
1699 {
1700 std::shared_ptr<ERC_ITEM> ercItem = ERC_ITEM::Create( ERCE_FOOTPRINT_LINK_ISSUES );
1701 msg.Printf( _( "Footprint '%s' not found in library '%s'" ),
1702 fpName,
1703 libName );
1704 ercItem->SetErrorMessage( msg );
1705 ercItem->SetItems( symbol );
1706 markers.emplace_back( new SCH_MARKER( std::move( ercItem ), symbol->GetPosition() ) );
1707 }
1708 }
1709
1710 for( SCH_MARKER* marker : markers )
1711 {
1712 sheet.LastScreen()->Append( marker );
1713 err_count += 1;
1714 }
1715 }
1716
1717 return err_count;
1718}
1719
1720
1722{
1723 wxCHECK( m_schematic, 0 );
1724
1725 wxString msg;
1726 int err_count = 0;
1727
1728 for( SCH_SHEET_PATH& sheet : m_sheetList )
1729 {
1730 std::vector<SCH_MARKER*> markers;
1731
1732 for( SCH_ITEM* item : sheet.LastScreen()->Items().OfType( SCH_SYMBOL_T ) )
1733 {
1734 SCH_SYMBOL* sch_symbol = static_cast<SCH_SYMBOL*>( item );
1735 std::unique_ptr<LIB_SYMBOL>& lib_symbol = sch_symbol->GetLibSymbolRef();
1736
1737 if( !lib_symbol )
1738 continue;
1739
1740 wxArrayString filters = lib_symbol->GetFPFilters();
1741
1742 if( filters.empty() )
1743 continue;
1744
1745 wxString lowerId = sch_symbol->GetFootprintFieldText( true, &sheet, false ).Lower();
1746 LIB_ID footprint;
1747
1748 if( footprint.Parse( lowerId ) > 0 )
1749 continue;
1750
1751 wxString lowerItemName = footprint.GetUniStringLibItemName().Lower();
1752 bool found = false;
1753
1754 for( wxString filter : filters )
1755 {
1756 filter.LowerCase();
1757
1758 // If the filter contains a ':' character, include the library name in the pattern
1759 if( filter.Contains( wxS( ":" ) ) )
1760 found |= lowerId.Matches( filter );
1761 else
1762 found |= lowerItemName.Matches( filter );
1763
1764 if( found )
1765 break;
1766 }
1767
1768 if( !found )
1769 {
1770 std::shared_ptr<ERC_ITEM> ercItem = ERC_ITEM::Create( ERCE_FOOTPRINT_LINK_ISSUES );
1771 msg.Printf( _( "Assigned footprint (%s) doesn't match footprint filters (%s)" ),
1772 footprint.GetUniStringLibItemName(),
1773 wxJoin( filters, ' ' ) );
1774 ercItem->SetErrorMessage( msg );
1775 ercItem->SetItems( sch_symbol );
1776 markers.emplace_back( new SCH_MARKER( std::move( ercItem ),
1777 sch_symbol->GetPosition() ) );
1778 }
1779 }
1780
1781 for( SCH_MARKER* marker : markers )
1782 {
1783 sheet.LastScreen()->Append( marker );
1784 err_count += 1;
1785 }
1786 }
1787
1788 return err_count;
1789}
1790
1791
1793{
1794 const int gridSize = m_schematic->Settings().m_ConnectionGridSize;
1795 int err_count = 0;
1796
1797 for( SCH_SCREEN* screen = m_screens.GetFirst(); screen; screen = m_screens.GetNext() )
1798 {
1799 std::vector<SCH_MARKER*> markers;
1800
1801 for( SCH_ITEM* item : screen->Items() )
1802 {
1803 if( item->Type() == SCH_LINE_T && item->IsConnectable() )
1804 {
1805 SCH_LINE* line = static_cast<SCH_LINE*>( item );
1806
1807 if( ( line->GetStartPoint().x % gridSize ) != 0
1808 || ( line->GetStartPoint().y % gridSize ) != 0 )
1809 {
1810 std::shared_ptr<ERC_ITEM> ercItem = ERC_ITEM::Create( ERCE_ENDPOINT_OFF_GRID );
1811 ercItem->SetItems( line );
1812
1813 markers.emplace_back( new SCH_MARKER( std::move( ercItem ), line->GetStartPoint() ) );
1814 }
1815 else if( ( line->GetEndPoint().x % gridSize ) != 0
1816 || ( line->GetEndPoint().y % gridSize ) != 0 )
1817 {
1818 std::shared_ptr<ERC_ITEM> ercItem = ERC_ITEM::Create( ERCE_ENDPOINT_OFF_GRID );
1819 ercItem->SetItems( line );
1820
1821 markers.emplace_back( new SCH_MARKER( std::move( ercItem ), line->GetEndPoint() ) );
1822 }
1823 }
1824 if( item->Type() == SCH_BUS_WIRE_ENTRY_T )
1825 {
1826 SCH_BUS_WIRE_ENTRY* entry = static_cast<SCH_BUS_WIRE_ENTRY*>( item );
1827
1828 for( const VECTOR2I& point : entry->GetConnectionPoints() )
1829 {
1830 if( ( point.x % gridSize ) != 0
1831 || ( point.y % gridSize ) != 0 )
1832 {
1833 std::shared_ptr<ERC_ITEM> ercItem = ERC_ITEM::Create( ERCE_ENDPOINT_OFF_GRID );
1834 ercItem->SetItems( entry );
1835
1836 markers.emplace_back( new SCH_MARKER( std::move( ercItem ), point ) );
1837 }
1838 }
1839 }
1840 else if( item->Type() == SCH_SYMBOL_T )
1841 {
1842 SCH_SYMBOL* symbol = static_cast<SCH_SYMBOL*>( item );
1843
1844 for( SCH_PIN* pin : symbol->GetPins( nullptr ) )
1845 {
1846 if( pin->GetType() == ELECTRICAL_PINTYPE::PT_NC )
1847 continue;
1848
1849 VECTOR2I pinPos = pin->GetPosition();
1850
1851 if( ( pinPos.x % gridSize ) != 0 || ( pinPos.y % gridSize ) != 0 )
1852 {
1853 std::shared_ptr<ERC_ITEM> ercItem = ERC_ITEM::Create( ERCE_ENDPOINT_OFF_GRID );
1854 ercItem->SetItems( pin );
1855
1856 markers.emplace_back( new SCH_MARKER( std::move( ercItem ), pinPos ) );
1857 break;
1858 }
1859 }
1860 }
1861 }
1862
1863 for( SCH_MARKER* marker : markers )
1864 {
1865 screen->Append( marker );
1866 err_count += 1;
1867 }
1868 }
1869
1870 return err_count;
1871}
1872
1873
1875{
1876 WX_STRING_REPORTER reporter;
1877 int err_count = 0;
1878 SIM_LIB_MGR libMgr( &m_schematic->Project() );
1879
1880 for( SCH_SHEET_PATH& sheet : m_sheetList )
1881 {
1882 if( sheet.GetExcludedFromSim() )
1883 continue;
1884
1885 std::vector<SCH_MARKER*> markers;
1886
1887 for( SCH_ITEM* item : sheet.LastScreen()->Items().OfType( SCH_SYMBOL_T ) )
1888 {
1889 SCH_SYMBOL* symbol = static_cast<SCH_SYMBOL*>( item );
1890
1891 // Power symbols and other symbols which have the reference starting with "#" are
1892 // not included in simulation
1893 if( symbol->GetRef( &sheet ).StartsWith( '#' ) || symbol->ResolveExcludedFromSim() )
1894 continue;
1895
1896 // Reset for each symbol
1897 reporter.Clear();
1898
1899 SIM_LIBRARY::MODEL model = libMgr.CreateModel( &sheet, *symbol, true, 0, reporter );
1900
1901 if( reporter.HasMessage() )
1902 {
1903 wxString msg = reporter.GetMessages();
1904 std::shared_ptr<ERC_ITEM> ercItem = ERC_ITEM::Create( ERCE_SIMULATION_MODEL );
1905
1906 //Remove \n and \r at e.o.l if any:
1907 msg.Trim();
1908
1909 ercItem->SetErrorMessage( msg );
1910 ercItem->SetItems( symbol );
1911
1912 markers.emplace_back( new SCH_MARKER( std::move( ercItem ), symbol->GetPosition() ) );
1913 }
1914 }
1915
1916 for( SCH_MARKER* marker : markers )
1917 {
1918 sheet.LastScreen()->Append( marker );
1919 err_count += 1;
1920 }
1921 }
1922
1923 return err_count;
1924}
1925
1926
1928 KIFACE* aCvPcb, PROJECT* aProject, PROGRESS_REPORTER* aProgressReporter )
1929{
1930 m_sheetList.AnnotatePowerSymbols();
1931
1932 // Test duplicate sheet names inside a given sheet. While one can have multiple references
1933 // to the same file, each must have a unique name.
1934 if( m_settings.IsTestEnabled( ERCE_DUPLICATE_SHEET_NAME ) )
1935 {
1936 if( aProgressReporter )
1937 aProgressReporter->AdvancePhase( _( "Checking sheet names..." ) );
1938
1940 }
1941
1942 if( m_settings.IsTestEnabled( ERCE_BUS_ALIAS_CONFLICT ) )
1943 {
1944 if( aProgressReporter )
1945 aProgressReporter->AdvancePhase( _( "Checking bus conflicts..." ) );
1946
1948 }
1949
1950 // The connection graph has a whole set of ERC checks it can run
1951 if( aProgressReporter )
1952 aProgressReporter->AdvancePhase( _( "Checking conflicts..." ) );
1953
1954 // If we are using the new connectivity, make sure that we do a full-rebuild
1955 if( aEditFrame )
1956 {
1957 if( ADVANCED_CFG::GetCfg().m_IncrementalConnectivity )
1958 aEditFrame->RecalculateConnections( nullptr, GLOBAL_CLEANUP );
1959 else
1960 aEditFrame->RecalculateConnections( nullptr, NO_CLEANUP );
1961 }
1962
1963 m_schematic->ConnectionGraph()->RunERC();
1964
1965 if( aProgressReporter )
1966 aProgressReporter->AdvancePhase( _( "Checking units..." ) );
1967
1968 // Test is all units of each multiunit symbol have the same footprint assigned.
1969 if( m_settings.IsTestEnabled( ERCE_DIFFERENT_UNIT_FP ) )
1970 {
1971 if( aProgressReporter )
1972 aProgressReporter->AdvancePhase( _( "Checking footprints..." ) );
1973
1975 }
1976
1977 if( m_settings.IsTestEnabled( ERCE_MISSING_UNIT )
1978 || m_settings.IsTestEnabled( ERCE_MISSING_INPUT_PIN )
1979 || m_settings.IsTestEnabled( ERCE_MISSING_POWER_INPUT_PIN )
1980 || m_settings.IsTestEnabled( ERCE_MISSING_BIDI_PIN ) )
1981 {
1983 }
1984
1985 if( aProgressReporter )
1986 aProgressReporter->AdvancePhase( _( "Checking pins..." ) );
1987
1988 if( m_settings.IsTestEnabled( ERCE_DIFFERENT_UNIT_NET ) )
1990
1991 // Test pins on each net against the pin connection table
1992 if( m_settings.IsTestEnabled( ERCE_PIN_TO_PIN_ERROR )
1993 || m_settings.IsTestEnabled( ERCE_POWERPIN_NOT_DRIVEN )
1994 || m_settings.IsTestEnabled( ERCE_PIN_NOT_DRIVEN ) )
1995 {
1996 TestPinToPin();
1997 }
1998
1999 if( m_settings.IsTestEnabled( ERCE_GROUND_PIN_NOT_GROUND ) )
2001
2002 if( m_settings.IsTestEnabled( ERCE_STACKED_PIN_SYNTAX ) )
2004
2005 // Test similar labels (i;e. labels which are identical when
2006 // using case insensitive comparisons)
2007 if( m_settings.IsTestEnabled( ERCE_SIMILAR_LABELS )
2008 || m_settings.IsTestEnabled( ERCE_SIMILAR_POWER )
2009 || m_settings.IsTestEnabled( ERCE_SIMILAR_LABEL_AND_POWER ) )
2010 {
2011 if( aProgressReporter )
2012 aProgressReporter->AdvancePhase( _( "Checking similar labels..." ) );
2013
2015 }
2016
2017 if( m_settings.IsTestEnabled( ERCE_SAME_LOCAL_GLOBAL_LABEL ) )
2018 {
2019 if( aProgressReporter )
2020 aProgressReporter->AdvancePhase( _( "Checking local and global labels..." ) );
2021
2023 }
2024
2025 if( m_settings.IsTestEnabled( ERCE_UNRESOLVED_VARIABLE ) )
2026 {
2027 if( aProgressReporter )
2028 aProgressReporter->AdvancePhase( _( "Checking for unresolved variables..." ) );
2029
2030 TestTextVars( aDrawingSheet );
2031 }
2032
2033 if( m_settings.IsTestEnabled( ERCE_SIMULATION_MODEL ) )
2034 {
2035 if( aProgressReporter )
2036 aProgressReporter->AdvancePhase( _( "Checking SPICE models..." ) );
2037
2039 }
2040
2041 if( m_settings.IsTestEnabled( ERCE_NOCONNECT_CONNECTED ) )
2042 {
2043 if( aProgressReporter )
2044 aProgressReporter->AdvancePhase( _( "Checking no connect pins for connections..." ) );
2045
2047 }
2048
2049 if( m_settings.IsTestEnabled( ERCE_LIB_SYMBOL_ISSUES )
2050 || m_settings.IsTestEnabled( ERCE_LIB_SYMBOL_MISMATCH ) )
2051 {
2052 if( aProgressReporter )
2053 aProgressReporter->AdvancePhase( _( "Checking for library symbol issues..." ) );
2054
2056 }
2057
2058 if( m_settings.IsTestEnabled( ERCE_FOOTPRINT_LINK_ISSUES ) && aCvPcb )
2059 {
2060 if( aProgressReporter )
2061 aProgressReporter->AdvancePhase( _( "Checking for footprint link issues..." ) );
2062
2063 TestFootprintLinkIssues( aCvPcb, aProject );
2064 }
2065
2066 if( m_settings.IsTestEnabled( ERCE_FOOTPRINT_FILTERS ) )
2067 {
2068 if( aProgressReporter )
2069 aProgressReporter->AdvancePhase( _( "Checking footprint assignments against footprint filters..." ) );
2070
2072 }
2073
2074 if( m_settings.IsTestEnabled( ERCE_ENDPOINT_OFF_GRID ) )
2075 {
2076 if( aProgressReporter )
2077 aProgressReporter->AdvancePhase( _( "Checking for off grid pins and wires..." ) );
2078
2080 }
2081
2082 if( m_settings.IsTestEnabled( ERCE_FOUR_WAY_JUNCTION ) )
2083 {
2084 if( aProgressReporter )
2085 aProgressReporter->AdvancePhase( _( "Checking for four way junctions..." ) );
2086
2088 }
2089
2090 if( m_settings.IsTestEnabled( ERCE_LABEL_MULTIPLE_WIRES ) )
2091 {
2092 if( aProgressReporter )
2093 aProgressReporter->AdvancePhase( _( "Checking for labels on more than one wire..." ) );
2094
2096 }
2097
2098 if( m_settings.IsTestEnabled( ERCE_UNDEFINED_NETCLASS ) )
2099 {
2100 if( aProgressReporter )
2101 aProgressReporter->AdvancePhase( _( "Checking for undefined netclasses..." ) );
2102
2104 }
2105
2106 m_schematic->ResolveERCExclusionsPostUpdate();
2107}
const char * name
constexpr EDA_IU_SCALE schIUScale
Definition base_units.h:114
BOX2< VECTOR2I > BOX2I
Definition box2.h:922
static const ADVANCED_CFG & GetCfg()
Get the singleton instance's config, which is shared by all consumers.
constexpr Vec Centre() const
Definition box2.h:97
A subgraph is a set of items that are electrically connected on a single sheet.
const std::set< SCH_ITEM * > & GetItems() const
Provide a read-only reference to the items in the subgraph.
const SCH_ITEM * GetNoConnect() const
const SCH_SHEET_PATH & GetSheet() const
Base class to handle basic graphic items.
Store the list of graphic items: rect, lines, polygons and texts to draw/plot the title block and fra...
DS_DRAW_ITEM_BASE * GetFirst()
void BuildDrawItemsList(const PAGE_INFO &aPageInfo, const TITLE_BLOCK &aTitleBlock)
Drawing or plot the drawing sheet.
void SetFileName(const wxString &aFileName)
Set the filename to draw/plot.
void SetSheetName(const wxString &aSheetName)
Set the sheet name to draw/plot.
void SetSheetLayer(const wxString &aSheetLayer)
Set the sheet layer to draw/plot.
void SetSheetCount(int aSheetCount)
Set the value of the count of sheets, for basic inscriptions.
void SetPageNumber(const wxString &aPageNumber)
Set the value of the sheet number.
DS_DRAW_ITEM_BASE * GetNext()
void SetProject(const PROJECT *aProject)
A graphic text.
const PAGE_INFO & GetPageInfo()
const TITLE_BLOCK & GetTitleBlock()
virtual VECTOR2I GetPosition() const
Definition eda_item.h:272
KICAD_T Type() const
Returns the type of object.
Definition eda_item.h:110
virtual const wxString & GetText() const
Return the string associated with the text object.
Definition eda_text.h:97
EE_TYPE OfType(KICAD_T aType) const
Definition sch_rtree.h:241
static std::shared_ptr< ERC_ITEM > Create(int aErrorCode)
Constructs an ERC_ITEM for the given error code.
Definition erc_item.cpp:307
A class used to associate a SCH_PIN with its owning SCH_SHEET_PATH, in order to handle ERC checks acr...
const SCH_SHEET_PATH & Sheet() const
Get the SCH_SHEET_PATH context for the paired SCH_PIN.
SCH_PIN * Pin() const
Get the SCH_PIN for this context.
int TestLibSymbolIssues()
Test symbols for changed library symbols and broken symbol library links.
Definition erc.cpp:1506
const NET_MAP & m_nets
Definition erc.h:203
int TestStackedPinNotation()
Checks for pin numbers that resemble stacked pin notation but are invalid.
Definition erc.cpp:1298
void TestTextVars(DS_PROXY_VIEW_ITEM *aDrawingSheet)
Check for any unresolved text variable references.
Definition erc.cpp:184
int TestPinToPin()
Checks the full netlist against the pin-to-pin connectivity requirements.
Definition erc.cpp:936
int TestSimilarLabels()
Checks for labels that differ only in capitalization.
Definition erc.cpp:1391
SCH_MULTI_UNIT_REFERENCE_MAP m_refMap
Definition erc.h:202
SCH_SCREENS m_screens
Definition erc.h:201
int TestFootprintLinkIssues(KIFACE *aCvPcb, PROJECT *aProject)
Test footprint links against the current footprint libraries.
Definition erc.cpp:1641
int TestOffGridEndpoints()
Test pins and wire ends for being off grid.
Definition erc.cpp:1792
int TestDuplicateSheetNames(bool aCreateMarker)
Inside a given sheet, one cannot have sheets with duplicate names (file names can be duplicated).
Definition erc.cpp:141
int TestSameLocalGlobalLabel()
Checks for global and local labels with the same name.
Definition erc.cpp:1336
int TestMultUnitPinConflicts()
Checks if shared pins on multi-unit symbols have been connected to different nets.
Definition erc.cpp:1176
int TestConflictingBusAliases()
Check that there are no conflicting bus alias definitions in the schematic.
Definition erc.cpp:439
int TestNoConnectPins()
In KiCad 5 and earlier, you could connect stuff up to pins with NC electrical type.
Definition erc.cpp:867
int TestFootprintFilters()
Test symbols to ensure that assigned footprint passes any given footprint filters.
Definition erc.cpp:1721
ERC_SETTINGS & m_settings
Definition erc.h:199
int TestFourWayJunction()
Test to see if there are potentially confusing 4-way junctions in the schematic.
Definition erc.cpp:788
int TestMissingNetclasses()
Tests for netclasses that are referenced but not defined.
Definition erc.cpp:679
int TestSimModelIssues()
Test SPICE models for various issues.
Definition erc.cpp:1874
SCH_SHEET_LIST m_sheetList
Definition erc.h:200
int TestGroundPins()
Checks for ground-labeled pins not on a ground net while another pin is.
Definition erc.cpp:1231
SCHEMATIC * m_schematic
Definition erc.h:198
void RunTests(DS_PROXY_VIEW_ITEM *aDrawingSheet, SCH_EDIT_FRAME *aEditFrame, KIFACE *aCvPcb, PROJECT *aProject, PROGRESS_REPORTER *aProgressReporter)
Definition erc.cpp:1927
int TestMissingUnits()
Test for uninstantiated units of multi unit symbols.
Definition erc.cpp:545
int TestLabelMultipleWires()
Test to see if there are labels that are connected to more than one wire.
Definition erc.cpp:733
int TestMultiunitFootprints()
Test if all units of each multiunit symbol have the same footprint assigned.
Definition erc.cpp:484
A logical library item identifier and consists of various portions much like a URI.
Definition lib_id.h:49
int Parse(const UTF8 &aId, bool aFix=false)
Parse LIB_ID with the information from aId.
Definition lib_id.cpp:52
const wxString GetUniStringLibItemName() const
Get strings for display messages in dialogs.
Definition lib_id.h:112
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
Define a library symbol object.
Definition lib_symbol.h:85
void RunOnChildren(const std::function< void(SCH_ITEM *)> &aFunction, RECURSE_MODE aMode) override
std::vector< SCH_PIN * > GetGraphicalPins(int aUnit=0, int aBodyStyle=0) const
Graphical pins: Return schematic pin objects as drawn (unexpanded), filtered by unit/body.
int GetUnitCount() const override
std::unique_ptr< LIB_SYMBOL > Flatten() const
Return a flattened symbol inheritance to the caller.
wxString GetUnitDisplayName(int aUnit, bool aLabel) const override
Return the user-defined display name for aUnit for symbols with units.
Hold a record identifying a library accessed by the appropriate plug in object in the LIB_TABLE.
virtual bool LibraryExists() const =0
const wxString GetFullURI(bool aSubstituted=false) const
Return the full location specifying URI for the LIB, either in original UI form or in environment var...
bool HasLibrary(const wxString &aNickname, bool aCheckEnabled=false) const
Test for the existence of aNickname in the library table.
A progress reporter interface for use in multi-threaded environments.
virtual void AdvancePhase()=0
Use the next available virtual zone of the dialog progress bar.
static SYMBOL_LIB_TABLE * SchSymbolLibTable(PROJECT *aProject)
Accessor for project symbol library table.
Container for project specific data.
Definition project.h:65
virtual bool HasMessage() const
Returns true if any messages were reported.
Definition reporter.h:134
std::vector< VECTOR2I > GetConnectionPoints() const override
Add all the connection points for this item to aPoints.
Class for a wire to bus entry.
Each graphical item can have a SCH_CONNECTION describing its logical connection (to a bus or net).
wxString GetNetName() const
Schematic editor (Eeschema) main window.
void RecalculateConnections(SCH_COMMIT *aCommit, SCH_CLEANUP_FLAGS aCleanupFlags, PROGRESS_REPORTER *aProgressReporter=nullptr)
Generate the connection data for the entire schematic hierarchy.
wxString GetCanonicalName() const
Get a non-language-specific name for a field which can be used for storage, variable look-up,...
wxString GetShownText(const SCH_SHEET_PATH *aPath, bool aAllowExtraText, int aDepth=0) const
Base class for any item which can be embedded within the SCHEMATIC container class,...
Definition sch_item.h:167
int GetBodyStyle() const
Definition sch_item.h:244
bool ResolveExcludedFromSim() const
Definition sch_item.cpp:247
wxString GetShownText(const SCH_SHEET_PATH *aPath, bool aAllowExtraText, int aDepth=0) const override
std::vector< VECTOR2I > GetConnectionPoints() const override
Add all the connection points for this item to aPoints.
Segment description base class to describe items which have 2 end points (track, wire,...
Definition sch_line.h:42
std::vector< VECTOR2I > GetConnectionPoints() const override
Add all the connection points for this item to aPoints.
Definition sch_line.cpp:689
VECTOR2I GetEndPoint() const
Definition sch_line.h:144
VECTOR2I GetStartPoint() const
Definition sch_line.h:139
bool IsEndPoint(const VECTOR2I &aPoint) const override
Test if aPt is an end point of this schematic object.
Definition sch_line.h:91
bool IsGraphicLine() const
Return if the line is a graphic (non electrical line)
Definition sch_line.cpp:955
bool IsVisible() const
Definition sch_pin.cpp:386
VECTOR2I GetPosition() const override
Definition sch_pin.cpp:255
bool IsStacked(const SCH_PIN *aPin) const
Definition sch_pin.cpp:475
ELECTRICAL_PINTYPE GetType() const
Definition sch_pin.cpp:312
Container to create a flattened list of symbols because in a complex hierarchy, a symbol can be used ...
SCH_REFERENCE & GetItem(size_t aIdx)
A helper to define a symbol's reference designator in a schematic.
const SCH_SHEET_PATH & GetSheetPath() const
const wxString GetFootprint() const
SCH_SYMBOL * GetSymbol() const
LIB_SYMBOL * GetLibPart() const
void Append(SCH_ITEM *aItem, bool aUpdateLibSymbol=true)
EE_RTREE & Items()
Get the full RTree, usually for iterating.
Definition sch_screen.h:117
const BOX2I GetBoundingBox() const override
Return the orthogonal bounding box of this object for display purposes.
VECTOR2I GetPosition() const override
Definition sch_shape.h:85
Handle access to a stack of flattened SCH_SHEET objects by way of a path for creating a flattened sch...
SCH_SCREEN * LastScreen()
void push_back(SCH_SHEET *aSheet)
Forwarded method from std::vector.
Define a sheet pin (label) used in sheets to create hierarchical schematics.
Sheet symbol placed in a schematic, and is the entry point for a sub schematic.
Definition sch_sheet.h:47
std::vector< SCH_FIELD > & GetFields()
Return a reference to the vector holding the sheet's fields.
Definition sch_sheet.h:87
VECTOR2I GetPosition() const override
Definition sch_sheet.h:415
std::vector< SCH_SHEET_PIN * > & GetPins()
Definition sch_sheet.h:187
wxString GetShownName(bool aAllowExtraText) const
Definition sch_sheet.h:109
Schematic symbol object.
Definition sch_symbol.h:75
const wxString GetValue(bool aResolve, const SCH_SHEET_PATH *aPath, bool aAllowExtraText) const override
void GetFields(std::vector< SCH_FIELD * > &aVector, bool aVisibleOnly) const override
Populate a std::vector with SCH_FIELDs, sorted in ordinal order.
const wxString GetFootprintFieldText(bool aResolve, const SCH_SHEET_PATH *aPath, bool aAllowExtraText) const
VECTOR2I GetPosition() const override
Definition sch_symbol.h:760
std::vector< SCH_PIN * > GetPins(const SCH_SHEET_PATH *aSheet) const
Retrieve a list of the SCH_PINs for the given sheet path.
const LIB_ID & GetLibId() const override
Definition sch_symbol.h:164
std::unique_ptr< LIB_SYMBOL > & GetLibSymbolRef()
Definition sch_symbol.h:183
const wxString GetRef(const SCH_SHEET_PATH *aSheet, bool aIncludeUnit=false) const override
virtual wxString GetShownText(const RENDER_SETTINGS *aSettings, const SCH_SHEET_PATH *aPath, bool aAllowExtraText, int aDepth=0) const
VECTOR2I GetPosition() const override
Definition sch_text.h:141
const BOX2I GetBoundingBox() const override
Return the orthogonal bounding box of this object for display purposes.
Definition sch_text.cpp:296
virtual wxString GetShownText(const SCH_SHEET_PATH *aPath, bool aAllowExtraText, int aDepth=0) const
Definition sch_text.cpp:317
SIM_MODEL & CreateModel(SIM_MODEL::TYPE aType, const std::vector< SCH_PIN * > &aPins, REPORTER &aReporter)
SYMBOL_LIB_TABLE_ROW * FindRow(const wxString &aNickName, bool aCheckIfEnabled=false)
Return an SYMBOL_LIB_TABLE_ROW if aNickName is found in this table or in any chained fallBack table f...
const TRANSFORM & GetTransform() const
Definition symbol.h:202
VECTOR2I TransformCoordinate(const VECTOR2I &aPoint) const
Calculate a new coordinate according to the mirror/rotation transform.
Definition transform.cpp:44
double Distance(const VECTOR2< extended_type > &aVector) const
Compute the distance between two vectors.
Definition vector2d.h:561
A wrapper for reporting to a wxString object.
Definition reporter.h:191
void Clear() override
Definition reporter.cpp:83
const wxString & GetMessages() const
Definition reporter.cpp:77
const wxString ExpandEnvVarSubstitutions(const wxString &aString, const PROJECT *aProject)
Replace any environment variable & text variable references with their values.
Definition common.cpp:355
The common library.
#define FOR_ERC_DRC
Expand '${var-name}' templates in text.
Definition common.h:91
#define _(s)
@ NO_RECURSE
Definition eda_item.h:52
void CheckDuplicatePins(LIB_SYMBOL *aSymbol, std::vector< wxString > &aMessages, UNITS_PROVIDER *aUnitsProvider)
const wxString CommentERC_V[]
Definition erc.cpp:94
const wxString CommentERC_H[]
Definition erc.cpp:77
const std::set< ELECTRICAL_PINTYPE > DrivenPinTypes
Definition erc.cpp:132
const std::set< ELECTRICAL_PINTYPE > DrivingPinTypes
Definition erc.cpp:114
const std::set< ELECTRICAL_PINTYPE > DrivingPowerPinTypes
Definition erc.cpp:126
ERCE_T
ERC error codes.
@ ERCE_POWERPIN_NOT_DRIVEN
Power input pin connected to some others pins but no power out pin to drive it.
@ ERCE_SIMILAR_POWER
2 power pins are equal for case insensitive comparisons.
@ ERCE_MISSING_POWER_INPUT_PIN
Symbol has power input pins that are not placed on the schematic.
@ ERCE_GROUND_PIN_NOT_GROUND
A ground-labeled pin is not on a ground net while another pin is.
@ ERCE_SIMILAR_LABELS
2 labels are equal for case insensitive comparisons.
@ ERCE_STACKED_PIN_SYNTAX
Pin name resembles stacked pin notation.
@ ERCE_ENDPOINT_OFF_GRID
Pin or wire-end off grid.
@ ERCE_SAME_LOCAL_GLOBAL_LABEL
2 labels are equal for case insensitive comparisons.
@ ERCE_SIMILAR_LABEL_AND_POWER
label and pin are equal for case insensitive comparisons.
@ ERCE_FOOTPRINT_LINK_ISSUES
The footprint link is invalid, or points to a missing (or inactive) footprint or library.
@ ERCE_DUPLICATE_PIN_ERROR
@ ERCE_DIFFERENT_UNIT_NET
Shared pin in a multi-unit symbol is connected to more than one net.
@ ERCE_FOUR_WAY_JUNCTION
A four-way junction was found.
@ ERCE_UNDEFINED_NETCLASS
A netclass was referenced but not defined.
@ ERCE_UNRESOLVED_VARIABLE
A text variable could not be resolved.
@ ERCE_SIMULATION_MODEL
An error was found in the simulation model.
@ ERCE_LIB_SYMBOL_MISMATCH
Symbol doesn't match copy in library.
@ ERCE_GENERIC_ERROR
@ ERCE_DIFFERENT_UNIT_FP
Different units of the same symbol have different footprints assigned.
@ ERCE_NOCONNECT_CONNECTED
A no connect symbol is connected to more than 1 pin.
@ ERCE_PIN_TO_PIN_WARNING
@ ERCE_PIN_NOT_DRIVEN
Pin connected to some others pins but no pin to drive it.
@ ERCE_MISSING_INPUT_PIN
Symbol has input pins that are not placed.
@ ERCE_MISSING_UNIT
Symbol has units that are not placed on the schematic.
@ ERCE_DUPLICATE_SHEET_NAME
Duplicate sheet names within a given sheet.
@ ERCE_MISSING_BIDI_PIN
Symbol has bi-directional pins that are not placed.
@ ERCE_LIB_SYMBOL_ISSUES
Symbol not found in active libraries.
@ ERCE_FOOTPRINT_FILTERS
The assigned footprint doesn't match the footprint filters.
@ ERCE_BUS_ALIAS_CONFLICT
Conflicting bus alias definitions across sheets.
@ ERCE_GENERIC_WARNING
@ ERCE_LABEL_MULTIPLE_WIRES
A label is connected to more than one wire.
@ ERCE_PIN_TO_PIN_ERROR
PIN_ERROR
The values a pin-to-pin entry in the pin matrix can take on.
@ KIFACE_TEST_FOOTPRINT_LINK_LIBRARY_NOT_ENABLED
Definition kiface_ids.h:62
@ KIFACE_TEST_FOOTPRINT_LINK
Definition kiface_ids.h:60
@ KIFACE_TEST_FOOTPRINT_LINK_NO_LIBRARY
Definition kiface_ids.h:61
@ KIFACE_TEST_FOOTPRINT_LINK_NO_FOOTPRINT
Definition kiface_ids.h:63
ELECTRICAL_PINTYPE
The symbol library pin object electrical types used in ERC tests.
Definition pin_type.h:36
@ PT_INPUT
usual pin input: must be connected
Definition pin_type.h:37
@ PT_NC
not connected (must be left open)
Definition pin_type.h:50
@ PT_OUTPUT
usual output
Definition pin_type.h:38
@ PT_TRISTATE
tri state bus pin
Definition pin_type.h:40
@ PT_BIDI
input or output (like port for a microprocessor)
Definition pin_type.h:39
@ PT_POWER_OUT
output of a regulator: intended to be connected to power input pins
Definition pin_type.h:47
@ PT_POWER_IN
power input (GND, VCC for ICs). Must be connected to a power output.
Definition pin_type.h:46
@ PT_UNSPECIFIED
unknown electrical properties: creates always a warning when connected
Definition pin_type.h:45
@ PT_PASSIVE
pin for passive symbols: must be connected, and can be connected to any pin.
Definition pin_type.h:43
wxString ElectricalPinTypeGetText(ELECTRICAL_PINTYPE)
Definition pin_type.cpp:212
static float distance(const SFVEC2UI &a, const SFVEC2UI &b)
LIB_SYMBOL * SchGetLibSymbol(const LIB_ID &aLibId, SYMBOL_LIB_TABLE *aLibTable, SYMBOL_LIB *aCacheLib, wxWindow *aParent, bool aShowErrorMsg)
Load symbol from symbol library table.
@ NO_CLEANUP
Definition schematic.h:75
@ GLOBAL_CLEANUP
Definition schematic.h:77
wxString UnescapeString(const wxString &aSource)
Implement a participant in the KIWAY alchemy.
Definition kiway.h:153
virtual void * IfaceOrAddress(int aDataId)=0
Return pointer to the requested object.
@ SCH_LINE_T
Definition typeinfo.h:165
@ SCH_NO_CONNECT_T
Definition typeinfo.h:162
@ SCH_SYMBOL_T
Definition typeinfo.h:174
@ SCH_FIELD_T
Definition typeinfo.h:152
@ SCH_LABEL_T
Definition typeinfo.h:169
@ SCH_LOCATE_ANY_T
Definition typeinfo.h:201
@ SCH_SHEET_T
Definition typeinfo.h:177
@ SCH_HIER_LABEL_T
Definition typeinfo.h:171
@ SCH_TEXT_T
Definition typeinfo.h:153
@ SCH_BUS_WIRE_ENTRY_T
Definition typeinfo.h:163
@ SCH_TEXTBOX_T
Definition typeinfo.h:154
@ SCH_GLOBAL_LABEL_T
Definition typeinfo.h:170
@ SCH_PIN_T
Definition typeinfo.h:155
VECTOR2< int32_t > VECTOR2I
Definition vector2d.h:695