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