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, see <https://www.gnu.org/licenses/>.
20 */
21
22#include <algorithm>
23#include <map>
24#include <numeric>
25#include <set>
26
27#include "connection_graph.h"
28#include "kiface_ids.h"
29#include <advanced_config.h>
30#include <common.h> // for ExpandEnvVarSubstitutions
31#include <erc/erc.h>
34#include <string_utils.h>
35#include <sch_pin.h>
36#include <project_sch.h>
39#include <sch_bus_entry.h>
40#include <sch_edit_frame.h>
41#include <sch_marker.h>
42#include <sch_reference_list.h>
43#include <sch_rule_area.h>
44#include <sch_sheet.h>
45#include <sch_sheet_pin.h>
46#include <sch_pin.h>
47#include <sch_textbox.h>
48#include <sch_line.h>
49#include <schematic.h>
50#include <lib_symbol.h>
51#include <sch_symbol.h>
52#include <pin_map.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#include <pgm_base.h>
62#include <trace_helpers.h>
64
65
66/* ERC tests :
67 * 1 - conflicts between connected pins ( example: 2 connected outputs )
68 * 2 - minimal connections requirements ( 1 input *must* be connected to an
69 * output, or a passive pin )
70 */
71
72/*
73 * Minimal ERC requirements:
74 * All pins *must* be connected (except ELECTRICAL_PINTYPE::PT_NC).
75 * When a pin is not connected in schematic, the user must place a "non
76 * connected" symbol to this pin.
77 * This ensures a forgotten connection will be detected.
78 */
79
80// Messages for matrix rows:
81const wxString CommentERC_H[] =
82{
83 _( "Input Pin" ),
84 _( "Output Pin" ),
85 _( "Bidirectional Pin" ),
86 _( "Tri-State Pin" ),
87 _( "Passive Pin" ),
88 _( "Free Pin" ),
89 _( "Unspecified Pin" ),
90 _( "Power Input Pin" ),
91 _( "Power Output Pin" ),
92 _( "Open Collector" ),
93 _( "Open Emitter" ),
94 _( "No Connection" )
95};
96
97// Messages for matrix columns
98const wxString CommentERC_V[] =
99{
100 _( "Input Pin" ),
101 _( "Output Pin" ),
102 _( "Bidirectional Pin" ),
103 _( "Tri-State Pin" ),
104 _( "Passive Pin" ),
105 _( "Free Pin" ),
106 _( "Unspecified Pin" ),
107 _( "Power Input Pin" ),
108 _( "Power Output Pin" ),
109 _( "Open Collector" ),
110 _( "Open Emitter" ),
111 _( "No Connection" )
112};
113
114
115// List of pin types that are considered drivers for usual input pins
116// i.e. pin type = ELECTRICAL_PINTYPE::PT_INPUT, but not PT_POWER_IN
117// that need only a PT_POWER_OUT pin type to be driven
126
127// List of pin types that are considered drivers for power pins
128// In fact only a ELECTRICAL_PINTYPE::PT_POWER_OUT pin type can drive
129// power input pins
130const std::set<ELECTRICAL_PINTYPE> DrivingPowerPinTypes =
131{
133};
134
135// List of pin types that require a driver elsewhere on the net
136const std::set<ELECTRICAL_PINTYPE> DrivenPinTypes =
137{
140};
141
142extern void CheckDuplicatePins( LIB_SYMBOL* aSymbol, std::vector<wxString>& aMessages,
143 UNITS_PROVIDER* aUnitsProvider );
144
145int ERC_TESTER::TestDuplicateSheetNames( bool aCreateMarker )
146{
147 int err_count = 0;
148
149 for( const SCH_SHEET_PATH& path : m_schematic->Hierarchy() )
150 {
151 SCH_SCREEN* screen = path.LastScreen();
152 std::vector<SCH_SHEET*> list;
153
154 for( SCH_ITEM* item : screen->Items().OfType( SCH_SHEET_T ) )
155 list.push_back( static_cast<SCH_SHEET*>( item ) );
156
157 for( size_t i = 0; i < list.size(); i++ )
158 {
159 SCH_SHEET* sheet = list[i];
160
161 for( size_t j = i + 1; j < list.size(); j++ )
162 {
163 SCH_SHEET* test_item = list[j];
164
165 // We have found a second sheet: compare names
166 // we are using case insensitive comparison to avoid mistakes between
167 // similar names like Mysheet and mysheet
168 const wxString variant = m_schematic->GetCurrentVariant();
169 const wxString name =
170 sheet->GetField( FIELD_T::SHEET_NAME )->GetShownText( &path, RESOLVED, variant, 0 );
171 const wxString other =
172 test_item->GetField( FIELD_T::SHEET_NAME )->GetShownText( &path, RESOLVED, variant, 0 );
173
174 if( name.IsSameAs( other, false ) )
175 {
176 if( aCreateMarker )
177 {
179 ercItem->SetItems( sheet, test_item );
180 ercItem->SetSheetSpecificPath( path );
181 ercItem->SetItemsSheetPaths( path, path );
182
183 SCH_MARKER* marker = new SCH_MARKER( std::move( ercItem ), sheet->GetPosition() );
184 screen->Append( marker );
185 }
186
187 err_count++;
188 }
189 }
190 }
191 }
192
193 return err_count;
194}
195
196
197int ERC_TESTER::TestPinMap( KIFACE* aCvPcb, PROJECT* aProject )
198{
199 int errors = 0;
200 const bool checkStale = m_settings.IsTestEnabled( ERCE_PIN_MAP_STALE_PIN );
201 const bool checkDuplicate = m_settings.IsTestEnabled( ERCE_PIN_MAP_DUPLICATE_PAD );
202 const bool checkBadPad = m_settings.IsTestEnabled( ERCE_PIN_MAP_BAD_PAD );
203
204 typedef void ( *PAD_NUMBERS_FN_PTR )( const wxString&, PROJECT*, std::set<wxString>& );
205
206 PAD_NUMBERS_FN_PTR padFetcher =
207 aCvPcb ? (PAD_NUMBERS_FN_PTR) aCvPcb->IfaceOrAddress( KIFACE_FOOTPRINT_PAD_NUMBERS ) : nullptr;
208
209 std::map<wxString, std::set<wxString>> padCache;
210
211 auto getPads = [&]( const wxString& aFootprintId ) -> const std::set<wxString>&
212 {
213 auto it = padCache.find( aFootprintId );
214
215 if( it != padCache.end() )
216 return it->second;
217
218 std::set<wxString>& pads = padCache[aFootprintId];
219
220 if( padFetcher && !aFootprintId.IsEmpty() )
221 padFetcher( aFootprintId, aProject, pads );
222
223 return pads;
224 };
225
226 // Pin maps and the symbol's pin numbers are library-symbol properties, so iterate unique
227 // screens (not sheet paths) to avoid double-reporting on reused hierarchical sheets.
228 for( SCH_SCREEN* screen = m_screens.GetFirst(); screen; screen = m_screens.GetNext() )
229 {
230 for( SCH_ITEM* item : screen->Items().OfType( SCH_SYMBOL_T ) )
231 {
232 SCH_SYMBOL* symbol = static_cast<SCH_SYMBOL*>( item );
233 LIB_SYMBOL* lib = symbol->GetLibSymbolRef().get();
234
235 if( !lib )
236 continue;
237
238 const PIN_MAP_SET& maps = lib->GetEffectivePinMaps();
239
240 if( maps.IsEmpty() )
241 continue;
242
243 std::set<wxString> pinNumbers;
244
245 for( const SCH_PIN* pin : lib->GetPins() )
246 pinNumbers.insert( pin->GetNumber() );
247
248 const std::vector<std::set<wxString>>& jumperGroups = lib->JumperPinGroups();
249
250 auto sharesJumperGroup = [&]( const wxString& aPinA, const wxString& aPinB )
251 {
252 for( const std::set<wxString>& group : jumperGroups )
253 {
254 if( group.count( aPinA ) && group.count( aPinB ) )
255 return true;
256 }
257
258 return false;
259 };
260
261 for( const PIN_MAP& map : maps.GetAll() )
262 {
263 if( checkStale )
264 {
265 for( const PIN_MAP_ENTRY& entry : map.GetEntries() )
266 {
267 if( pinNumbers.count( entry.m_PinNumber ) )
268 continue;
269
270 std::shared_ptr<ERC_ITEM> ercItem = ERC_ITEM::Create( ERCE_PIN_MAP_STALE_PIN );
271 ercItem->SetItems( symbol );
272 ercItem->SetErrorMessage(
273 wxString::Format( _( "Pin map '%s' references unknown symbol pin '%s'" ), map.GetName(),
274 entry.m_PinNumber ) );
275 screen->Append( new SCH_MARKER( std::move( ercItem ), symbol->GetPosition() ) );
276 errors++;
277 }
278 }
279
280 // A single pin stacked across several pads is allowed. Two pins on one pad is not.
281 if( checkDuplicate )
282 {
283 std::map<wxString, wxString> padToPin;
284
285 for( const PIN_MAP_ENTRY& entry : map.GetEntries() )
286 {
287 for( const wxString& pad : ExpandStackedPinNotation( entry.m_PadNumber ) )
288 {
289 auto it = padToPin.find( pad );
290
291 if( it == padToPin.end() )
292 {
293 padToPin[pad] = entry.m_PinNumber;
294 }
295 else if( it->second != entry.m_PinNumber
296 && !sharesJumperGroup( it->second, entry.m_PinNumber ) )
297 {
298 std::shared_ptr<ERC_ITEM> ercItem = ERC_ITEM::Create( ERCE_PIN_MAP_DUPLICATE_PAD );
299 ercItem->SetItems( symbol );
300 ercItem->SetErrorMessage(
301 wxString::Format( _( "Symbol pins '%s' and '%s' both map to pad '%s'" ),
302 it->second, entry.m_PinNumber, pad ) );
303 screen->Append( new SCH_MARKER( std::move( ercItem ), symbol->GetPosition() ) );
304 errors++;
305 }
306 }
307 }
308 }
309 }
310
311 if( checkBadPad && padFetcher )
312 {
313 for( const ASSOCIATED_FOOTPRINT& assoc : lib->GetEffectiveAssociatedFootprints() )
314 {
315 const PIN_MAP* boundMap = maps.FindByName( assoc.m_MapName );
316
317 if( !boundMap )
318 continue;
319
320 const std::set<wxString>& pads = getPads( assoc.m_FootprintLibId.GetUniStringLibId() );
321
322 if( pads.empty() )
323 continue;
324
325 for( const PIN_MAP_ENTRY& entry : boundMap->GetEntries() )
326 {
327 for( const wxString& pad : ExpandStackedPinNotation( entry.m_PadNumber ) )
328 {
329 if( pads.count( pad ) )
330 continue;
331
332 std::shared_ptr<ERC_ITEM> ercItem = ERC_ITEM::Create( ERCE_PIN_MAP_BAD_PAD );
333 ercItem->SetItems( symbol );
334 ercItem->SetErrorMessage( wxString::Format(
335 _( "Pin map '%s' references pad '%s' not present on footprint '%s'" ),
336 boundMap->GetName(), pad, assoc.m_FootprintLibId.GetUniStringLibId() ) );
337 screen->Append( new SCH_MARKER( std::move( ercItem ), symbol->GetPosition() ) );
338 errors++;
339 }
340 }
341 }
342 }
343 }
344 }
345
346 if( m_settings.IsTestEnabled( ERCE_PIN_MAP_UNMAPPED_PIN ) && padFetcher )
347 {
348 const wxString variant = m_schematic ? m_schematic->GetCurrentVariant() : wxString();
349
350 for( SCH_SHEET_PATH& sheet : m_sheetList )
351 {
352 for( SCH_ITEM* item : sheet.LastScreen()->Items().OfType( SCH_SYMBOL_T ) )
353 {
354 SCH_SYMBOL* symbol = static_cast<SCH_SYMBOL*>( item );
355 LIB_SYMBOL* lib = symbol->GetLibSymbolRef().get();
356
357 if( !lib || lib->GetEffectiveAssociatedFootprints().empty() )
358 continue;
359
360 wxString fpText = symbol->GetFootprintFieldText( &sheet, RESOLVED );
361 LIB_ID fpId;
362
363 if( fpText.IsEmpty() || fpId.Parse( fpText, true ) >= 0 )
364 continue;
365
366 const std::set<wxString>& pads = getPads( fpId.GetUniStringLibId() );
367
368 if( pads.empty() )
369 continue;
370
371 for( SCH_PIN* pin : symbol->GetPins( &sheet ) )
372 {
373 if( pin->IsDangling() )
374 continue;
375
376 SCH_PIN::PAD_RESOLUTION state = SCH_PIN::PAD_RESOLUTION::MAPPED;
377 pin->GetEffectivePadNumber( sheet, variant, fpId, &pads, &state );
378
379 if( state != SCH_PIN::PAD_RESOLUTION::UNMAPPED )
380 continue;
381
382 std::shared_ptr<ERC_ITEM> ercItem = ERC_ITEM::Create( ERCE_PIN_MAP_UNMAPPED_PIN );
383 ercItem->SetItems( pin );
384 ercItem->SetErrorMessage( wxString::Format( _( "Pin '%s' is connected but maps to no pad "
385 "on footprint '%s'" ),
386 pin->GetNumber(),
387 fpText ) );
388 sheet.LastScreen()->Append( new SCH_MARKER( std::move( ercItem ), pin->GetPosition() ) );
389 errors++;
390 }
391 }
392 }
393 }
394
395 return errors;
396}
397
398
400{
402
403 // The leading "(^|[^\\\\])" group requires the marker to start the string or follow a non-backslash,
404 // so `\${ERC_ERROR ...}` stays inert. (The group is just to make it easier for a human to parse.)
405 static wxRegEx varRefRegEx( wxT( "(^|[^\\\\])\\$\\{.*\\}.*" ) );
406
407 auto unresolved =
408 [&]( const wxString& str )
409 {
410 return varRefRegEx.Matches( ExpandEnvVarSubstitutions( str, &m_schematic->Project() ) );
411 };
412
413 auto testAssertion =
414 []( const SCH_ITEM* item, const SCH_SHEET_PATH& sheet, SCH_SCREEN* screen,
415 const wxString& text, const VECTOR2I& pos )
416 {
417 // Match anywhere in the text so users can embed ${ERC_ERROR ...}
418 // or ${ERC_WARNING ...} inside placeholder strings rather than
419 // only at the start of the field. The leading "(^|[^\\\\])"
420 // group requires the marker to start the string or follow a
421 // non-backslash, so `\${ERC_ERROR ...}` stays inert; the
422 // captured message is group 2.
423 static wxRegEx warningExpr( wxS( "(^|[^\\\\])\\$\\{ERC_WARNING\\s*([^}]*)\\}" ) );
424 static wxRegEx errorExpr( wxS( "(^|[^\\\\])\\$\\{ERC_ERROR\\s*([^}]*)\\}" ) );
425
426 auto reportEach =
427 [&]( wxRegEx& aExpr, int aErrorCode )
428 {
429 // Return true on any *match*, not only when a marker is appended,
430 // so the caller-side unresolved-variable suppression stays
431 // correct even if the limit-exceeded short-circuit lands here in
432 // the future.
433 bool found = false;
434 wxString remaining = text;
435
436 while( aExpr.Matches( remaining ) )
437 {
438 found = true;
439
440 wxString ercText = aExpr.GetMatch( remaining, 2 );
441
442 std::shared_ptr<ERC_ITEM> ercItem = ERC_ITEM::Create( aErrorCode );
443
444 if( item )
445 ercItem->SetItems( item );
446 else
447 ercText += _( " (in drawing sheet)" );
448
449 ercItem->SetSheetSpecificPath( sheet );
450 ercItem->SetErrorMessage( ercText );
451
452 SCH_MARKER* marker = new SCH_MARKER( std::move( ercItem ), pos );
453 screen->Append( marker );
454
455 size_t start = 0;
456 size_t len = 0;
457
458 if( !aExpr.GetMatch( &start, &len, 0 ) || len == 0 )
459 break;
460
461 remaining = remaining.Mid( start + len );
462 }
463
464 return found;
465 };
466
467 bool foundWarning = reportEach( warningExpr, ERCE_GENERIC_WARNING );
468 bool foundError = reportEach( errorExpr, ERCE_GENERIC_ERROR );
469
470 return foundWarning || foundError;
471 };
472
473 if( aDrawingSheet )
474 {
475 wsItems.SetPageNumber( wxS( "1" ) );
476 wsItems.SetSheetCount( 1 );
477 wsItems.SetFileName( wxS( "dummyFilename" ) );
478 wsItems.SetSheetName( wxS( "dummySheet" ) );
479 wsItems.SetSheetLayer( wxS( "dummyLayer" ) );
480 wsItems.SetProject( &m_schematic->Project() );
481 wsItems.BuildDrawItemsList( aDrawingSheet->GetPageInfo(), aDrawingSheet->GetTitleBlock() );
482 }
483
484 for( const SCH_SHEET_PATH& sheet : m_sheetList )
485 {
486 SCH_SCREEN* screen = sheet.LastScreen();
487
488 for( SCH_ITEM* item : screen->Items().OfType( SCH_LOCATE_ANY_T ) )
489 {
490 if( item->Type() == SCH_SYMBOL_T )
491 {
492 SCH_SYMBOL* symbol = static_cast<SCH_SYMBOL*>( item );
493
494 for( SCH_FIELD& field : symbol->GetFields() )
495 {
496 if( testAssertion( &field, sheet, screen, field.GetText(), field.GetPosition() ) )
497 {
498 // Don't run unresolved test
499 }
500 else if( unresolved( field.GetShownText( &sheet, FOR_ERC_DRC ) ) )
501 {
503 ercItem->SetItems( symbol );
504 ercItem->SetSheetSpecificPath( sheet );
505
506 SCH_MARKER* marker = new SCH_MARKER( std::move( ercItem ), field.GetPosition() );
507 screen->Append( marker );
508 }
509 }
510
511 if( symbol->GetLibSymbolRef() )
512 {
514 [&]( SCH_ITEM* child )
515 {
516 if( child->Type() == SCH_FIELD_T )
517 {
518 // test only SCH_SYMBOL fields, not LIB_SYMBOL fields
519 }
520 else if( child->Type() == SCH_TEXT_T )
521 {
522 SCH_TEXT* textItem = static_cast<SCH_TEXT*>( child );
523
524 if( testAssertion( symbol, sheet, screen, textItem->GetText(),
525 textItem->GetPosition() ) )
526 {
527 // Don't run unresolved test
528 }
529 else if( unresolved( textItem->GetShownText( &sheet, FOR_ERC_DRC ) ) )
530 {
531 std::shared_ptr<ERC_ITEM> ercItem = ERC_ITEM::Create( ERCE_UNRESOLVED_VARIABLE );
532 ercItem->SetItems( symbol );
533 ercItem->SetSheetSpecificPath( sheet );
534
535 BOX2I bbox = textItem->GetBoundingBox();
536 bbox = symbol->GetTransform().TransformCoordinate( bbox );
537 VECTOR2I pos = bbox.Centre() + symbol->GetPosition();
538
539 SCH_MARKER* marker = new SCH_MARKER( std::move( ercItem ), pos );
540 screen->Append( marker );
541 }
542 }
543 else if( child->Type() == SCH_TEXTBOX_T )
544 {
545 SCH_TEXTBOX* textboxItem = static_cast<SCH_TEXTBOX*>( child );
546
547 if( testAssertion( symbol, sheet, screen, textboxItem->GetText(),
548 textboxItem->GetPosition() ) )
549 {
550 // Don't run unresolved test
551 }
552 else if( unresolved( textboxItem->GetShownText( nullptr, &sheet, FOR_ERC_DRC ) ) )
553 {
554 std::shared_ptr<ERC_ITEM> ercItem = ERC_ITEM::Create( ERCE_UNRESOLVED_VARIABLE );
555 ercItem->SetItems( symbol );
556 ercItem->SetSheetSpecificPath( sheet );
557
558 BOX2I bbox = textboxItem->GetBoundingBox();
559 bbox = symbol->GetTransform().TransformCoordinate( bbox );
560 VECTOR2I pos = bbox.Centre() + symbol->GetPosition();
561
562 SCH_MARKER* marker = new SCH_MARKER( std::move( ercItem ), pos );
563 screen->Append( marker );
564 }
565 }
566 },
568 }
569 }
570 else if( SCH_LABEL_BASE* label = dynamic_cast<SCH_LABEL_BASE*>( item ) )
571 {
572 for( SCH_FIELD& field : label->GetFields() )
573 {
574 if( testAssertion( &field, sheet, screen, field.GetText(), field.GetPosition() ) )
575 {
576 // Don't run unresolved test
577 }
578 else if( unresolved( field.GetShownText( &sheet, FOR_ERC_DRC ) ) )
579 {
581 ercItem->SetItems( label );
582 ercItem->SetSheetSpecificPath( sheet );
583
584 SCH_MARKER* marker = new SCH_MARKER( std::move( ercItem ), field.GetPosition() );
585 screen->Append( marker );
586 }
587 }
588 }
589 else if( item->Type() == SCH_SHEET_T )
590 {
591 SCH_SHEET* subSheet = static_cast<SCH_SHEET*>( item );
592
593 for( SCH_FIELD& field : subSheet->GetFields() )
594 {
595 if( testAssertion( &field, sheet, screen, field.GetText(), field.GetPosition() ) )
596 {
597 // Don't run unresolved test
598 }
599 else if( unresolved( field.GetShownText( &sheet, FOR_ERC_DRC ) ) )
600 {
602 ercItem->SetItems( subSheet );
603 ercItem->SetSheetSpecificPath( sheet );
604
605 SCH_MARKER* marker = new SCH_MARKER( std::move( ercItem ), field.GetPosition() );
606 screen->Append( marker );
607 }
608 }
609
610 SCH_SHEET_PATH subSheetPath = sheet;
611 subSheetPath.push_back( subSheet );
612
613 for( SCH_SHEET_PIN* pin : subSheet->GetPins() )
614 {
615 if( varRefRegEx.Matches( pin->GetShownText( &subSheetPath, FOR_ERC_DRC ) ) )
616 {
617 std::shared_ptr<ERC_ITEM> ercItem = ERC_ITEM::Create( ERCE_UNRESOLVED_VARIABLE );
618 ercItem->SetItems( pin );
619 ercItem->SetSheetSpecificPath( sheet );
620
621 SCH_MARKER* marker = new SCH_MARKER( std::move( ercItem ), pin->GetPosition() );
622 screen->Append( marker );
623 }
624 }
625 }
626 else if( SCH_TEXT* text = dynamic_cast<SCH_TEXT*>( item ) )
627 {
628 if( testAssertion( text, sheet, screen, text->GetText(), text->GetPosition() ) )
629 {
630 // Don't run unresolved test
631 }
632 else if( varRefRegEx.Matches( text->GetShownText( &sheet, FOR_ERC_DRC ) ) )
633 {
634 std::shared_ptr<ERC_ITEM> ercItem = ERC_ITEM::Create( ERCE_UNRESOLVED_VARIABLE );
635 ercItem->SetItems( text );
636 ercItem->SetSheetSpecificPath( sheet );
637
638 SCH_MARKER* marker = new SCH_MARKER( std::move( ercItem ), text->GetPosition() );
639 screen->Append( marker );
640 }
641 }
642 else if( SCH_TEXTBOX* textBox = dynamic_cast<SCH_TEXTBOX*>( item ) )
643 {
644 if( testAssertion( textBox, sheet, screen, textBox->GetText(), textBox->GetPosition() ) )
645 {
646 // Don't run unresolved test
647 }
648 else if( varRefRegEx.Matches( textBox->GetShownText( nullptr, &sheet, FOR_ERC_DRC ) ) )
649 {
650 std::shared_ptr<ERC_ITEM> ercItem = ERC_ITEM::Create( ERCE_UNRESOLVED_VARIABLE );
651 ercItem->SetItems( textBox );
652 ercItem->SetSheetSpecificPath( sheet );
653
654 SCH_MARKER* marker = new SCH_MARKER( std::move( ercItem ), textBox->GetPosition() );
655 screen->Append( marker );
656 }
657 }
658 }
659
660 for( DS_DRAW_ITEM_BASE* item = wsItems.GetFirst(); item; item = wsItems.GetNext() )
661 {
662 if( DS_DRAW_ITEM_TEXT* text = dynamic_cast<DS_DRAW_ITEM_TEXT*>( item ) )
663 {
664 if( testAssertion( nullptr, sheet, screen, text->GetText(), text->GetPosition() ) )
665 {
666 // Don't run unresolved test
667 }
668 else if( varRefRegEx.Matches( text->GetShownText( FOR_ERC_DRC ) ) )
669 {
670 std::shared_ptr<ERC_ITEM> ercItem = ERC_ITEM::Create( ERCE_UNRESOLVED_VARIABLE );
671 ercItem->SetErrorMessage( _( "Unresolved text variable in drawing sheet" ) );
672 ercItem->SetSheetSpecificPath( sheet );
673
674 SCH_MARKER* marker = new SCH_MARKER( std::move( ercItem ), text->GetPosition() );
675 screen->Append( marker );
676 }
677 }
678 }
679 }
680}
681
682
684{
685 int errors = 0;
686
687 // No directive labels, they carry no text (the netclass lives in a field)
689
690 for( const SCH_SHEET_PATH& sheet : m_sheetList )
691 {
692 SCH_SCREEN* screen = sheet.LastScreen();
693
694 for( KICAD_T labelType : labelTypes )
695 {
696 for( SCH_ITEM* item : screen->Items().OfType( labelType ) )
697 {
698 SCH_LABEL_BASE* label = static_cast<SCH_LABEL_BASE*>( item );
699
700 wxString text = label->GetText();
701 text.Trim( false ).Trim( true );
702
703 if( text.IsEmpty() )
704 {
705 auto ercItem = ERC_ITEM::Create( ERCE_EMPTY_LABEL_NAME );
706 ercItem->SetItems( label );
707 ercItem->SetItemsSheetPaths( sheet );
708 ercItem->SetSheetSpecificPath( sheet );
709
710 SCH_MARKER* marker = new SCH_MARKER( std::move( ercItem ), label->GetPosition() );
711 screen->Append( marker );
712 errors++;
713 }
714 }
715 }
716 }
717
718 return errors;
719}
720
721
723{
724 int warnings = 0;
725
726 for( const SCH_SHEET_PATH& sheet : m_sheetList )
727 {
728 SCH_SCREEN* screen = sheet.LastScreen();
729
730 for( SCH_ITEM* item : screen->Items().OfType( SCH_SYMBOL_T ) )
731 {
732 SCH_SYMBOL* symbol = static_cast<SCH_SYMBOL*>( item );
733
734 for( SCH_FIELD& field : symbol->GetFields() )
735 {
736 wxString trimmedFieldName = field.GetName();
737 trimmedFieldName.Trim();
738 trimmedFieldName.Trim( false );
739
740 if( field.GetName() != trimmedFieldName )
741 {
743 ercItem->SetItems( symbol, &field );
744 ercItem->SetItemsSheetPaths( sheet, sheet );
745 ercItem->SetSheetSpecificPath( sheet );
746 ercItem->SetErrorMessage(
747 wxString::Format(
748 _( "Field name has leading or trailing whitespace: '%s'" ),
749 field.GetName() ) );
750
751 SCH_MARKER* marker = new SCH_MARKER( std::move( ercItem ), field.GetPosition() );
752 screen->Append( marker );
753 warnings++;
754 }
755 }
756 }
757
758 for( SCH_ITEM* item : screen->Items().OfType( SCH_SHEET_T ) )
759 {
760 SCH_SHEET* subSheet = static_cast<SCH_SHEET*>( item );
761
762 for( SCH_FIELD& field : subSheet->GetFields() )
763 {
764 wxString trimmedFieldName = field.GetName();
765 trimmedFieldName.Trim();
766 trimmedFieldName.Trim( false );
767
768 if( field.GetName() != trimmedFieldName )
769 {
771 ercItem->SetItems( subSheet, &field );
772 ercItem->SetItemsSheetPaths( sheet, sheet );
773 ercItem->SetSheetSpecificPath( sheet );
774 ercItem->SetErrorMessage(
775 wxString::Format(
776 _( "Field name has leading or trailing whitespace: '%s'" ),
777 field.GetName() ) );
778
779 SCH_MARKER* marker = new SCH_MARKER( std::move( ercItem ), field.GetPosition() );
780 screen->Append( marker );
781 warnings++;
782 }
783 }
784 }
785 }
786
787 return warnings;
788}
789
790
792{
793 int errors = 0;
794
795 for( std::pair<const wxString, SCH_REFERENCE_LIST>& symbol : m_refMap )
796 {
797 SCH_REFERENCE_LIST& refList = symbol.second;
798
799 if( refList.GetCount() == 0 )
800 {
801 wxFAIL; // it should not happen
802 continue;
803 }
804
805 // Reference footprint
806 SCH_SYMBOL* unit = nullptr;
807 wxString unitName;
808 wxString unitFP;
809
810 for( size_t ii = 0; ii < refList.GetCount(); ++ii )
811 {
812 SCH_SHEET_PATH sheetPath = refList.GetItem( ii ).GetSheetPath();
813 unitFP = refList.GetItem( ii ).GetFootprint();
814
815 if( !unitFP.IsEmpty() )
816 {
817 unit = refList.GetItem( ii ).GetSymbol();
818 unitName = unit->GetRef( &sheetPath, true );
819 break;
820 }
821 }
822
823 for( size_t ii = 0; ii < refList.GetCount(); ++ii )
824 {
825 SCH_REFERENCE& secondRef = refList.GetItem( ii );
826 SCH_SYMBOL* secondUnit = secondRef.GetSymbol();
827 wxString secondName = secondUnit->GetRef( &secondRef.GetSheetPath(), true );
828 const wxString secondFp = secondRef.GetFootprint();
829 wxString msg;
830
831 if( unit && !secondFp.IsEmpty() && unitFP != secondFp )
832 {
833 msg.Printf( _( "Different footprints assigned to %s and %s" ),
834 unitName, secondName );
835
836 std::shared_ptr<ERC_ITEM> ercItem = ERC_ITEM::Create( ERCE_DIFFERENT_UNIT_FP );
837 ercItem->SetErrorMessage( msg );
838 ercItem->SetItems( unit, secondUnit );
839
840 SCH_MARKER* marker = new SCH_MARKER( std::move( ercItem ), secondUnit->GetPosition() );
841 secondRef.GetSheetPath().LastScreen()->Append( marker );
842
843 ++errors;
844 }
845 }
846 }
847
848 return errors;
849}
850
851
853{
854 int errors = 0;
855
856 for( std::pair<const wxString, SCH_REFERENCE_LIST>& symbol : m_refMap )
857 {
858 SCH_REFERENCE_LIST& refList = symbol.second;
859
860 wxCHECK2( refList.GetCount(), continue );
861
862 // Reference unit
863 SCH_REFERENCE& base_ref = refList.GetItem( 0 );
864 SCH_SYMBOL* unit = base_ref.GetSymbol();
865 LIB_SYMBOL* libSymbol = base_ref.GetLibPart();
866
867 if( static_cast<ssize_t>( refList.GetCount() ) == libSymbol->GetUnitCount() )
868 continue;
869
870 std::set<int> lib_units;
871 std::set<int> instance_units;
872 std::set<int> missing_units;
873
874 auto report =
875 [&]( std::set<int>& aMissingUnits, const wxString& aErrorMsg, int aErrorCode )
876 {
877 wxString msg;
878 wxString missing_pin_units = wxS( "[ " );
879 int ii = 0;
880
881 for( int missing_unit : aMissingUnits )
882 {
883 if( ii++ == 3 )
884 {
885 missing_pin_units += wxS( "..." );
886 break;
887 }
888
889 missing_pin_units += libSymbol->GetUnitDisplayName( missing_unit, false ) + ", " ;
890 }
891
892 missing_pin_units.Truncate( missing_pin_units.length() - 2 );
893 missing_pin_units += wxS( " ]" );
894
895 msg.Printf( aErrorMsg, symbol.first, missing_pin_units );
896
897 std::shared_ptr<ERC_ITEM> ercItem = ERC_ITEM::Create( aErrorCode );
898 ercItem->SetErrorMessage( msg );
899 ercItem->SetItems( unit );
900 ercItem->SetSheetSpecificPath( base_ref.GetSheetPath() );
901 ercItem->SetItemsSheetPaths( base_ref.GetSheetPath() );
902
903 SCH_MARKER* marker = new SCH_MARKER( std::move( ercItem ), unit->GetPosition() );
904 base_ref.GetSheetPath().LastScreen()->Append( marker );
905
906 ++errors;
907 };
908
909 for( int ii = 1; ii <= libSymbol->GetUnitCount(); ++ii )
910 lib_units.insert( lib_units.end(), ii );
911
912 for( size_t ii = 0; ii < refList.GetCount(); ++ii )
913 instance_units.insert( instance_units.end(), refList.GetItem( ii ).GetUnit() );
914
915 std::set_difference( lib_units.begin(), lib_units.end(),
916 instance_units.begin(), instance_units.end(),
917 std::inserter( missing_units, missing_units.begin() ) );
918
919 if( !missing_units.empty() && m_settings.IsTestEnabled( ERCE_MISSING_UNIT ) )
920 {
921 report( missing_units, _( "Symbol %s has unplaced units %s" ), ERCE_MISSING_UNIT );
922 }
923
924 std::set<int> missing_power;
925 std::set<int> missing_input;
926 std::set<int> missing_bidi;
927
928 for( int missing_unit : missing_units )
929 {
930 int bodyStyle = 0;
931
932 for( size_t ii = 0; ii < refList.GetCount(); ++ii )
933 {
934 if( refList.GetItem( ii ).GetUnit() == missing_unit )
935 {
936 bodyStyle = refList.GetItem( ii ).GetSymbol()->GetBodyStyle();
937 break;
938 }
939 }
940
941 for( SCH_PIN* pin : libSymbol->GetGraphicalPins( missing_unit, bodyStyle ) )
942 {
943 switch( pin->GetType() )
944 {
946 missing_power.insert( missing_unit );
947 break;
948
950 missing_bidi.insert( missing_unit );
951 break;
952
954 missing_input.insert( missing_unit );
955 break;
956
957 default:
958 break;
959 }
960 }
961 }
962
963 if( !missing_power.empty() && m_settings.IsTestEnabled( ERCE_MISSING_POWER_INPUT_PIN ) )
964 {
965 report( missing_power, _( "Symbol %s has input power pins in units %s that are not placed" ),
967 }
968
969 if( !missing_input.empty() && m_settings.IsTestEnabled( ERCE_MISSING_INPUT_PIN ) )
970 {
971 report( missing_input, _( "Symbol %s has input pins in units %s that are not placed" ),
973 }
974
975 if( !missing_bidi.empty() && m_settings.IsTestEnabled( ERCE_MISSING_BIDI_PIN ) )
976 {
977 report( missing_bidi, _( "Symbol %s has bidirectional pins in units %s that are not placed" ),
979 }
980 }
981
982 return errors;
983}
984
985
987{
988 int err_count = 0;
989 std::shared_ptr<NET_SETTINGS>& settings = m_schematic->Project().GetProjectFile().NetSettings();
990 wxString defaultNetclass = settings->GetDefaultNetclass()->GetName();
991
992 auto logError =
993 [&]( const SCH_SHEET_PATH& sheet, SCH_ITEM* item, const wxString& netclass )
994 {
995 err_count++;
996
997 std::shared_ptr<ERC_ITEM> ercItem = ERC_ITEM::Create( ERCE_UNDEFINED_NETCLASS );
998
999 ercItem->SetItems( item );
1000 ercItem->SetErrorMessage( wxString::Format( _( "Netclass %s is not defined" ), netclass ) );
1001
1002 SCH_MARKER* marker = new SCH_MARKER( std::move( ercItem ), item->GetPosition() );
1003 sheet.LastScreen()->Append( marker );
1004 };
1005
1006 for( const SCH_SHEET_PATH& sheet : m_sheetList )
1007 {
1008 for( SCH_ITEM* item : sheet.LastScreen()->Items() )
1009 {
1010 item->RunOnChildren(
1011 [&]( SCH_ITEM* aChild )
1012 {
1013 if( aChild->Type() == SCH_FIELD_T )
1014 {
1015 SCH_FIELD* field = static_cast<SCH_FIELD*>( aChild );
1016
1017 if( field->GetUntranslatedName() == wxT( "Netclass" ) )
1018 {
1019 wxString netclass = field->GetShownText( &sheet, FOR_NETNAME,
1020 m_schematic->GetCurrentVariant() );
1021
1022 if( !netclass.empty() && !netclass.IsSameAs( defaultNetclass )
1023 && !settings->HasNetclass( netclass ) )
1024 {
1025 logError( sheet, item, netclass );
1026 }
1027 }
1028 }
1029
1030 return true;
1031 },
1033 }
1034 }
1035
1036 return err_count;
1037}
1038
1039
1041{
1042 int err_count = 0;
1043
1044 for( const SCH_SHEET_PATH& sheet : m_sheetList )
1045 {
1046 std::map<VECTOR2I, std::vector<SCH_ITEM*>> connMap;
1047
1048 for( SCH_ITEM* item : sheet.LastScreen()->Items().OfType( SCH_LABEL_T ) )
1049 {
1050 SCH_LABEL* label = static_cast<SCH_LABEL*>( item );
1051
1052 for( const VECTOR2I& pt : label->GetConnectionPoints() )
1053 connMap[pt].emplace_back( label );
1054 }
1055
1056 for( const std::pair<const VECTOR2I, std::vector<SCH_ITEM*>>& pair : connMap )
1057 {
1058 std::vector<SCH_ITEM*> lines;
1059
1060 for( SCH_ITEM* item : sheet.LastScreen()->Items().Overlapping( SCH_LINE_T, pair.first ) )
1061 {
1062 SCH_LINE* line = static_cast<SCH_LINE*>( item );
1063
1064 if( line->IsGraphicLine() )
1065 continue;
1066
1067 // If the line is connected at the endpoint, then there will be a junction
1068 if( !line->IsEndPoint( pair.first ) )
1069 lines.emplace_back( line );
1070 }
1071
1072 if( lines.size() > 1 )
1073 {
1074 err_count++;
1075 lines.resize( 3 ); // Only show the first 3 lines and if there are only two, adds a nullptr
1076
1077 std::shared_ptr<ERC_ITEM> ercItem = ERC_ITEM::Create( ERCE_LABEL_MULTIPLE_WIRES );
1078 wxString msg = wxString::Format( _( "Label connects more than one wire at %d, %d" ),
1079 pair.first.x, pair.first.y );
1080
1081 ercItem->SetItems( pair.second.front(), lines[0], lines[1], lines[2] );
1082 ercItem->SetErrorMessage( msg );
1083 ercItem->SetSheetSpecificPath( sheet );
1084
1085 SCH_MARKER* marker = new SCH_MARKER( std::move( ercItem ), pair.first );
1086 sheet.LastScreen()->Append( marker );
1087 }
1088 }
1089 }
1090
1091 return err_count;
1092}
1093
1094
1096{
1097 int err_count = 0;
1098
1099 auto pinStackAlreadyRepresented =
1100 []( SCH_PIN* pin, std::vector<SCH_ITEM*>& collection ) -> bool
1101 {
1102 for( SCH_ITEM*& item : collection )
1103 {
1104 if( item->Type() == SCH_PIN_T && item->GetParentSymbol() == pin->GetParentSymbol() )
1105 {
1106 if( pin->IsVisible() && !static_cast<SCH_PIN*>( item )->IsVisible() )
1107 item = pin;
1108
1109 return true;
1110 }
1111 }
1112
1113 return false;
1114 };
1115
1116 for( const SCH_SHEET_PATH& sheet : m_sheetList )
1117 {
1118 std::map<VECTOR2I, std::vector<SCH_ITEM*>> connMap;
1119 SCH_SCREEN* screen = sheet.LastScreen();
1120
1121 for( SCH_ITEM* item : screen->Items().OfType( SCH_SYMBOL_T ) )
1122 {
1123 SCH_SYMBOL* symbol = static_cast<SCH_SYMBOL*>( item );
1124
1125 for( SCH_PIN* pin : symbol->GetPins( &sheet ) )
1126 {
1127 std::vector<SCH_ITEM*>& entry = connMap[pin->GetPosition()];
1128
1129 // Only one pin per pin-stack.
1130 if( pinStackAlreadyRepresented( pin, entry ) )
1131 continue;
1132
1133 entry.emplace_back( pin );
1134 }
1135 }
1136
1137 for( SCH_ITEM* item : screen->Items().OfType( SCH_LINE_T ) )
1138 {
1139 SCH_LINE* line = static_cast<SCH_LINE*>( item );
1140
1141 if( line->IsGraphicLine() )
1142 continue;
1143
1144 for( const VECTOR2I& pt : line->GetConnectionPoints() )
1145 connMap[pt].emplace_back( line );
1146 }
1147
1148 for( const std::pair<const VECTOR2I, std::vector<SCH_ITEM*>>& pair : connMap )
1149 {
1150 if( pair.second.size() >= 4 )
1151 {
1152 err_count++;
1153
1154 std::shared_ptr<ERC_ITEM> ercItem = ERC_ITEM::Create( ERCE_FOUR_WAY_JUNCTION );
1155
1156 ercItem->SetItems( pair.second[0], pair.second[1], pair.second[2], pair.second[3] );
1157
1158 wxString msg = wxString::Format( _( "Four items connected at %d, %d" ),
1159 pair.first.x, pair.first.y );
1160 ercItem->SetErrorMessage( msg );
1161
1162 ercItem->SetSheetSpecificPath( sheet );
1163
1164 SCH_MARKER* marker = new SCH_MARKER( std::move( ercItem ), pair.first );
1165 sheet.LastScreen()->Append( marker );
1166 }
1167 }
1168 }
1169
1170 return err_count;
1171}
1172
1173
1175{
1176 int err_count = 0;
1177
1178 for( const SCH_SHEET_PATH& sheet : m_sheetList )
1179 {
1180 std::map<VECTOR2I, std::vector<SCH_ITEM*>> pinMap;
1181
1182 auto addOther =
1183 [&]( const VECTOR2I& pt, SCH_ITEM* aOther )
1184 {
1185 if( pinMap.count( pt ) )
1186 pinMap[pt].emplace_back( aOther );
1187 };
1188
1189 for( SCH_ITEM* item : sheet.LastScreen()->Items().OfType( SCH_SYMBOL_T ) )
1190 {
1191 SCH_SYMBOL* symbol = static_cast<SCH_SYMBOL*>( item );
1192
1193 for( SCH_PIN* pin : symbol->GetPins( &sheet ) )
1194 {
1195 if( pin->GetType() == ELECTRICAL_PINTYPE::PT_NC )
1196 pinMap[pin->GetPosition()].emplace_back( pin );
1197 }
1198 }
1199
1200 for( SCH_ITEM* item : sheet.LastScreen()->Items() )
1201 {
1202 if( item->Type() == SCH_SYMBOL_T )
1203 {
1204 SCH_SYMBOL* symbol = static_cast<SCH_SYMBOL*>( item );
1205
1206 for( SCH_PIN* pin : symbol->GetPins( &sheet ) )
1207 {
1208 if( pin->GetType() != ELECTRICAL_PINTYPE::PT_NC )
1209 addOther( pin->GetPosition(), pin );
1210 }
1211 }
1212 else if( item->IsConnectable() && item->Type() != SCH_NO_CONNECT_T )
1213 {
1214 for( const VECTOR2I& pt : item->GetConnectionPoints() )
1215 addOther( pt, item );
1216 }
1217 }
1218
1219 for( const std::pair<const VECTOR2I, std::vector<SCH_ITEM*>>& pair : pinMap )
1220 {
1221 if( pair.second.size() > 1 )
1222 {
1223 bool all_nc = true;
1224
1225 for( SCH_ITEM* item : pair.second )
1226 {
1227 if( item->Type() != SCH_PIN_T )
1228 {
1229 all_nc = false;
1230 break;
1231 }
1232
1233 SCH_PIN* pin = static_cast<SCH_PIN*>( item );
1234
1235 if( pin->GetType() != ELECTRICAL_PINTYPE::PT_NC )
1236 {
1237 all_nc = false;
1238 break;
1239 }
1240 }
1241
1242 if( all_nc )
1243 continue;
1244
1245 err_count++;
1246
1247 std::shared_ptr<ERC_ITEM> ercItem = ERC_ITEM::Create( ERCE_NOCONNECT_CONNECTED );
1248
1249 ercItem->SetItems( pair.second[0], pair.second[1],
1250 pair.second.size() > 2 ? pair.second[2] : nullptr,
1251 pair.second.size() > 3 ? pair.second[3] : nullptr );
1252 ercItem->SetErrorMessage( _( "Pin with 'no connection' type is connected" ) );
1253 ercItem->SetSheetSpecificPath( sheet );
1254
1255 SCH_MARKER* marker = new SCH_MARKER( std::move( ercItem ), pair.first );
1256 sheet.LastScreen()->Append( marker );
1257 }
1258 }
1259 }
1260
1261 return err_count;
1262}
1263
1264
1266{
1267 int errors = 0;
1268 std::set<wxString> powerDrivenNets;
1269
1270 if( !m_schematic->ConnectionGraph()->GetCommittedNetChains().empty() )
1271 {
1272 for( const auto& [key, subgraphs] : m_nets )
1273 {
1274 for( const CONNECTION_SUBGRAPH* subgraph : subgraphs )
1275 {
1276 for( SCH_ITEM* item : subgraph->GetItems() )
1277 {
1278 if( item->Type() != SCH_PIN_T )
1279 continue;
1280
1281 const SCH_PIN* pin = static_cast<SCH_PIN*>( item );
1282
1283 if( DrivingPowerPinTypes.contains( pin->GetType() ) )
1284 powerDrivenNets.insert( key.Name );
1285 }
1286 }
1287 }
1288 }
1289
1290
1291 // Map each net name to the pins (with sheet context) found on that net so we can later
1292 // perform cross-net compatibility checks for grouped net chains.
1293 std::unordered_map<wxString, std::vector<ERC_SCH_PIN_CONTEXT>> netToPins;
1294
1295 for( const std::pair<NET_NAME_CODE_CACHE_KEY, std::vector<CONNECTION_SUBGRAPH*>> net : m_nets )
1296 {
1297 using iterator_t = std::vector<ERC_SCH_PIN_CONTEXT>::iterator;
1298 std::vector<ERC_SCH_PIN_CONTEXT> pins;
1299 std::unordered_map<EDA_ITEM*, SCH_SCREEN*> pinToScreenMap;
1300 bool has_noconnect = false;
1301
1302 for( CONNECTION_SUBGRAPH* subgraph: net.second )
1303 {
1304 if( subgraph->GetNoConnect() )
1305 has_noconnect = true;
1306
1307 for( SCH_ITEM* item : subgraph->GetItems() )
1308 {
1309 if( item->Type() == SCH_PIN_T )
1310 {
1311 pins.emplace_back( static_cast<SCH_PIN*>( item ), subgraph->GetSheet() );
1312 netToPins[ net.first.Name ].emplace_back( static_cast<SCH_PIN*>( item ), subgraph->GetSheet() );
1313 pinToScreenMap[item] = subgraph->GetSheet().LastScreen();
1314 }
1315 }
1316 }
1317
1318 std::sort( pins.begin(), pins.end(),
1319 []( const ERC_SCH_PIN_CONTEXT& lhs, const ERC_SCH_PIN_CONTEXT& rhs )
1320 {
1321 int ret = StrNumCmp( lhs.Pin()->GetParentSymbol()->GetRef( &lhs.Sheet() ),
1322 rhs.Pin()->GetParentSymbol()->GetRef( &rhs.Sheet() ) );
1323
1324 if( ret == 0 )
1325 ret = StrNumCmp( lhs.Pin()->GetNumber(), rhs.Pin()->GetNumber() );
1326
1327 return ret != 0 ? ret < 0 : lhs < rhs;
1328 } );
1329
1330 ERC_SCH_PIN_CONTEXT needsDriver;
1332 bool hasDriver = false;
1333 std::vector<ERC_SCH_PIN_CONTEXT*> pinsNeedingDrivers;
1334 std::vector<ERC_SCH_PIN_CONTEXT*> nonPowerPinsNeedingDrivers;
1335 std::vector<ERC_SCH_PIN_CONTEXT*> powerInPinsNeedingDrivers;
1336
1337 // We need different drivers for power nets and normal nets.
1338 // A power net has at least one pin having the ELECTRICAL_PINTYPE::PT_POWER_IN
1339 // and power nets can be driven only by ELECTRICAL_PINTYPE::PT_POWER_OUT pins
1340 bool ispowerNet = false;
1341
1342 for( ERC_SCH_PIN_CONTEXT& refPin : pins )
1343 {
1344 if( refPin.Pin()->GetType() == ELECTRICAL_PINTYPE::PT_POWER_IN )
1345 {
1346 ispowerNet = true;
1347 break;
1348 }
1349 }
1350
1351 std::vector<std::tuple<iterator_t, iterator_t, PIN_ERROR>> pin_mismatches;
1352 std::map<iterator_t, int> pin_mismatch_counts;
1353
1354 for( auto refIt = pins.begin(); refIt != pins.end(); ++refIt )
1355 {
1356 ERC_SCH_PIN_CONTEXT& refPin = *refIt;
1357 ELECTRICAL_PINTYPE refType = refPin.Pin()->GetType();
1358
1359 if( DrivenPinTypes.contains( refType ) )
1360 {
1361 // needsDriver will be the pin shown in the error report eventually, so try to
1362 // upgrade to a "better" pin if possible: something visible and only a power symbol
1363 // if this net needs a power driver
1364 pinsNeedingDrivers.push_back( &refPin );
1365
1366 if( !refPin.Pin()->IsPower() )
1367 nonPowerPinsNeedingDrivers.push_back( &refPin );
1368
1369 if( refType == ELECTRICAL_PINTYPE::PT_POWER_IN )
1370 powerInPinsNeedingDrivers.push_back( &refPin );
1371
1372 if( !needsDriver.Pin()
1373 || ( !needsDriver.Pin()->IsVisible() && refPin.Pin()->IsVisible() )
1374 || ( ispowerNet != ( needsDriverType == ELECTRICAL_PINTYPE::PT_POWER_IN )
1375 && ispowerNet == ( refType == ELECTRICAL_PINTYPE::PT_POWER_IN ) ) )
1376 {
1377 needsDriver = refPin;
1378 needsDriverType = needsDriver.Pin()->GetType();
1379 }
1380 }
1381
1382 if( ispowerNet )
1383 hasDriver |= ( DrivingPowerPinTypes.count( refType ) != 0 );
1384 else
1385 hasDriver |= ( DrivingPinTypes.count( refType ) != 0 );
1386
1387 for( auto testIt = refIt + 1; testIt != pins.end(); ++testIt )
1388 {
1389 ERC_SCH_PIN_CONTEXT& testPin = *testIt;
1390
1391 // Multiple pins in the same symbol that share a type,
1392 // name and position are considered
1393 // "stacked" and shouldn't trigger ERC errors
1394 if( refPin.Pin()->IsStacked( testPin.Pin() ) && refPin.Sheet() == testPin.Sheet() )
1395 continue;
1396
1397 ELECTRICAL_PINTYPE testType = testPin.Pin()->GetType();
1398
1399 if( ispowerNet )
1400 hasDriver |= DrivingPowerPinTypes.contains( testType );
1401 else
1402 hasDriver |= DrivingPinTypes.contains( testType );
1403
1404 PIN_ERROR erc = m_settings.GetPinMapValue( refType, testType );
1405
1408
1409 if( erc != PIN_ERROR::OK && m_settings.IsTestEnabled( ercCode ) )
1410 {
1411 pin_mismatches.emplace_back( std::tuple<iterator_t, iterator_t, PIN_ERROR>{ refIt, testIt, erc } );
1412
1413 if( m_settings.GetERCSortingMetric() == ERC_PIN_SORTING_METRIC::SM_HEURISTICS )
1414 {
1415 pin_mismatch_counts[refIt] = m_settings.GetPinTypeWeight( ( *refIt ).Pin()->GetType() );
1416 pin_mismatch_counts[testIt] = m_settings.GetPinTypeWeight( ( *testIt ).Pin()->GetType() );
1417 }
1418 else
1419 {
1420 if( !pin_mismatch_counts.contains( testIt ) )
1421 pin_mismatch_counts.emplace( testIt, 1 );
1422 else
1423 pin_mismatch_counts[testIt]++;
1424
1425 if( !pin_mismatch_counts.contains( refIt ) )
1426 pin_mismatch_counts.emplace( refIt, 1 );
1427 else
1428 pin_mismatch_counts[refIt]++;
1429 }
1430 }
1431 }
1432 }
1433
1434 std::multimap<size_t, iterator_t, std::greater<size_t>> pins_dsc;
1435
1436 std::transform( pin_mismatch_counts.begin(), pin_mismatch_counts.end(),
1437 std::inserter( pins_dsc, pins_dsc.begin() ),
1438 []( const auto& p )
1439 {
1440 return std::pair<size_t, iterator_t>( p.second, p.first );
1441 } );
1442
1443 for( const auto& [amount, pinItBind] : pins_dsc )
1444 {
1445 auto& pinIt = pinItBind;
1446
1447 if( pin_mismatches.empty() )
1448 break;
1449
1450 SCH_PIN* pin = ( *pinIt ).Pin();
1451 VECTOR2I position = pin->GetPosition();
1452
1453 iterator_t nearest_pin = pins.end();
1454 double smallest_distance = std::numeric_limits<double>::infinity();
1455 PIN_ERROR erc;
1456
1457 std::erase_if(
1458 pin_mismatches,
1459 [&]( const auto& tuple )
1460 {
1461 iterator_t other;
1462
1463 if( pinIt == std::get<0>( tuple ) )
1464 other = std::get<1>( tuple );
1465 else if( pinIt == std::get<1>( tuple ) )
1466 other = std::get<0>( tuple );
1467 else
1468 return false;
1469
1470 if( ( *pinIt ).Sheet().Cmp( ( *other ).Sheet() ) != 0 )
1471 {
1472 if( std::isinf( smallest_distance ) )
1473 {
1474 nearest_pin = other;
1475 erc = std::get<2>( tuple );
1476 }
1477 }
1478 else
1479 {
1480 double distance = position.Distance( ( *other ).Pin()->GetPosition() );
1481
1482 if( std::isinf( smallest_distance ) || distance < smallest_distance )
1483 {
1484 smallest_distance = distance;
1485 nearest_pin = other;
1486 erc = std::get<2>( tuple );
1487 }
1488 }
1489
1490 return true;
1491 } );
1492
1493 if( nearest_pin != pins.end() )
1494 {
1495 SCH_PIN* other_pin = ( *nearest_pin ).Pin();
1496
1499 ercItem->SetItems( pin, other_pin );
1500 ercItem->SetSheetSpecificPath( ( *pinIt ).Sheet() );
1501 ercItem->SetItemsSheetPaths( ( *pinIt ).Sheet(), ( *nearest_pin ).Sheet() );
1502
1503 ercItem->SetErrorMessage( wxString::Format( _( "Pins of type %s and %s are connected" ),
1504 ElectricalPinTypeGetText( pin->GetType() ),
1505 ElectricalPinTypeGetText( other_pin->GetType() ) ) );
1506
1507 SCH_MARKER* marker = new SCH_MARKER( std::move( ercItem ), pin->GetPosition() );
1508 pinToScreenMap[pin]->Append( marker );
1509 errors++;
1510 }
1511 }
1512
1513 if( needsDriver.Pin() && !hasDriver && !has_noconnect )
1514 {
1515 int err_code = ispowerNet ? ERCE_POWERPIN_NOT_DRIVEN : ERCE_PIN_NOT_DRIVEN;
1516
1517 // NEW: For power nets, before reporting a not-driven error, look across the
1518 // net chain (multi-net chain formed via passives) to see if there is a
1519 // power driver pin on any other net in the same chain. If so, suppress the
1520 // error because the net chain as a whole is driven.
1521 bool suppressForNetChainDriver = false;
1522
1523 if( ispowerNet && m_schematic && m_schematic->ConnectionGraph() )
1524 {
1525 const wxString& thisNetName = net.first.Name;
1526 const auto& netChains = m_schematic->ConnectionGraph()->GetCommittedNetChains();
1527
1528 for( const auto& sig : netChains )
1529 {
1530 if( !sig )
1531 continue;
1532
1533 const auto& sigNets = sig->GetNets();
1534 bool containsThisNet = std::find( sigNets.begin(), sigNets.end(), thisNetName ) != sigNets.end();
1535
1536 if( !containsThisNet )
1537 continue;
1538
1539 // Look for a different net in this chain that has a power driver.
1540 for( const wxString& otherNet : sigNets )
1541 {
1542 if( otherNet == thisNetName )
1543 continue; // skip same net (we already know it lacks a driver)
1544
1545 if( powerDrivenNets.contains( otherNet ) )
1546 {
1547 suppressForNetChainDriver = true;
1548 break;
1549 }
1550 }
1551
1552 break; // examined the containing chain
1553 }
1554 }
1555
1556 if( !suppressForNetChainDriver && m_settings.IsTestEnabled( err_code ) )
1557 {
1558 std::vector<ERC_SCH_PIN_CONTEXT*> pinsToMark;
1559
1560 // The marker should land on a pin matching the error message: for an
1561 // ERCE_POWERPIN_NOT_DRIVEN error mark a PT_POWER_IN pin (which is what the
1562 // error refers to), for ERCE_PIN_NOT_DRIVEN prefer a pin that is not on a
1563 // power symbol so the marker is anchored to the consuming pin rather than
1564 // a power flag.
1565 if( m_showAllErrors )
1566 {
1567 if( ispowerNet && !powerInPinsNeedingDrivers.empty() )
1568 pinsToMark = powerInPinsNeedingDrivers;
1569 else if( !nonPowerPinsNeedingDrivers.empty() )
1570 pinsToMark = nonPowerPinsNeedingDrivers;
1571 else
1572 pinsToMark = pinsNeedingDrivers;
1573 }
1574 else
1575 {
1576 if( ispowerNet && !powerInPinsNeedingDrivers.empty() )
1577 pinsToMark.push_back( powerInPinsNeedingDrivers.front() );
1578 else
1579 pinsToMark.push_back( &needsDriver );
1580 }
1581
1582 for( ERC_SCH_PIN_CONTEXT* pinCtx : pinsToMark )
1583 {
1584 std::shared_ptr<ERC_ITEM> ercItem = ERC_ITEM::Create( err_code );
1585
1586 ercItem->SetItems( pinCtx->Pin() );
1587 ercItem->SetSheetSpecificPath( pinCtx->Sheet() );
1588 ercItem->SetItemsSheetPaths( pinCtx->Sheet() );
1589
1590 SCH_MARKER* marker = new SCH_MARKER( std::move( ercItem ), pinCtx->Pin()->GetPosition() );
1591 pinToScreenMap[pinCtx->Pin()]->Append( marker );
1592 errors++;
1593 }
1594 }
1595 }
1596 }
1597
1598 // --- Additional net-chain-level checking ---
1599 // If a pin participates in a grouped net chain (spanning multiple nets via passives), ensure
1600 // that all other pins reachable through that chain are electrically compatible even if
1601 // they reside on different nets.
1602 // We only consider pairs on DIFFERENT nets here to avoid duplicating existing net-level
1603 // mismatches already reported above.
1604 if( m_schematic && m_schematic->ConnectionGraph() )
1605 {
1606 auto& netChains = m_schematic->ConnectionGraph()->GetCommittedNetChains();
1607 wxLogTrace( traceSchNetChain, "ERC TestPinToPin: cross-chain phase start chains=%zu",
1608 netChains.size() );
1609
1610 for( const auto& sig : netChains )
1611 {
1612 if( !sig )
1613 continue;
1614
1615 const wxString chainName = sig->GetName();
1616 const auto& sigNets = sig->GetNets();
1617
1618 // Collect all pin contexts across the nets in this chain.
1619 struct CHAIN_PIN
1620 {
1621 ERC_SCH_PIN_CONTEXT context;
1622 const wxString* net;
1623 };
1624 std::vector<CHAIN_PIN> netChainPins;
1625 netChainPins.reserve( sigNets.size() * 4 );
1626
1627 for( const wxString& n : sigNets )
1628 {
1629 auto it = netToPins.find( n );
1630 if( it != netToPins.end() )
1631 {
1632 for( const ERC_SCH_PIN_CONTEXT& context : it->second )
1633 netChainPins.push_back( { context, &it->first } );
1634 }
1635 }
1636
1637 if( netChainPins.size() < 2 )
1638 continue; // nothing to compare
1639
1640 wxLogTrace( traceSchNetChain,
1641 "ERC TestPinToPin: chain '%s' nets=%zu collectedPins=%zu",
1642 TO_UTF8( chainName ), sigNets.size(), netChainPins.size() );
1643
1644 // For deterministic behavior, sort by reference/pin number similar to earlier pass.
1645 std::sort( netChainPins.begin(), netChainPins.end(),
1646 []( const CHAIN_PIN& left, const CHAIN_PIN& right )
1647 {
1648 const auto& lhs = left.context;
1649 const auto& rhs = right.context;
1650 int ret = StrNumCmp( lhs.Pin()->GetParentSymbol()->GetRef( &lhs.Sheet() ),
1651 rhs.Pin()->GetParentSymbol()->GetRef( &rhs.Sheet() ) );
1652 if( ret == 0 )
1653 ret = StrNumCmp( lhs.Pin()->GetNumber(), rhs.Pin()->GetNumber() );
1654 return ret != 0 ? ret < 0 : lhs < rhs;
1655 } );
1656
1657 for( size_t i = 0; i < netChainPins.size(); ++i )
1658 {
1659 const auto& aContext = netChainPins[i].context;
1660 SCH_PIN* aPin = aContext.Pin();
1661 ELECTRICAL_PINTYPE aType = aPin->GetType();
1662 const wxString& aNet = *netChainPins[i].net;
1663
1664 for( size_t j = i + 1; j < netChainPins.size(); ++j )
1665 {
1666 const auto& bContext = netChainPins[j].context;
1667 SCH_PIN* bPin = bContext.Pin();
1668 const wxString& bNet = *netChainPins[j].net;
1669
1670 if( aNet == bNet )
1671 continue; // already handled at net-level
1672
1673 ELECTRICAL_PINTYPE bType = bPin->GetType();
1674 PIN_ERROR erc = m_settings.GetPinMapValue( aType, bType );
1675
1678
1679 if( erc != PIN_ERROR::OK && m_settings.IsTestEnabled( ercCode ) )
1680 {
1681 std::shared_ptr<ERC_ITEM> ercItem = ERC_ITEM::Create( ercCode );
1682
1683 ercItem->SetItems( aPin, bPin );
1684 ercItem->SetSheetSpecificPath( aContext.Sheet() );
1685 ercItem->SetItemsSheetPaths( aContext.Sheet(), bContext.Sheet() );
1686 ercItem->SetErrorMessage( wxString::Format(
1687 _( "Pins of type %s and %s are connected via net chain %s" ),
1688 ElectricalPinTypeGetText( aType ),
1689 ElectricalPinTypeGetText( bType ), chainName ) );
1690
1691 SCH_MARKER* marker = new SCH_MARKER( std::move( ercItem ), aPin->GetPosition() );
1692 aContext.Sheet().LastScreen()->Append( marker );
1693 errors++;
1694 }
1695 }
1696 }
1697 }
1698 }
1699
1700 return errors;
1701}
1702
1703
1705{
1706 int errors = 0;
1707
1708 struct PIN_ON_NET
1709 {
1710 wxString m_netName;
1711 SCH_PIN* m_pin;
1712 SCH_SHEET_PATH m_sheet;
1713 };
1714
1715 std::unordered_map<wxString, std::vector<PIN_ON_NET>> pinToNetMap;
1716
1717 for( const std::pair<NET_NAME_CODE_CACHE_KEY, std::vector<CONNECTION_SUBGRAPH*>> net : m_nets )
1718 {
1719 const wxString& netName = net.first.Name;
1720
1721 for( CONNECTION_SUBGRAPH* subgraph : net.second )
1722 {
1723 for( SCH_ITEM* item : subgraph->GetItems() )
1724 {
1725 if( item->Type() == SCH_PIN_T )
1726 {
1727 SCH_PIN* pin = static_cast<SCH_PIN*>( item );
1728 const SCH_SHEET_PATH& sheet = subgraph->GetSheet();
1729
1730 if( !pin->GetParentSymbol()->IsMultiUnit() )
1731 continue;
1732
1733 wxString name = pin->GetParentSymbol()->GetRef( &sheet ) + ":"
1734 + pin->GetShownNumber();
1735
1736 pinToNetMap[name].push_back( { netName, pin, sheet } );
1737 }
1738 }
1739 }
1740 }
1741
1742 // Pick the reference net/pin deterministically, so the same conflict yields the same marker on every ERC run.
1743 std::vector<wxString> conflicted;
1744
1745 for( const auto& [name, pins] : pinToNetMap )
1746 {
1747 for( const PIN_ON_NET& candidate : pins )
1748 {
1749 if( candidate.m_netName != pins.front().m_netName )
1750 {
1751 conflicted.push_back( name );
1752 break;
1753 }
1754 }
1755 }
1756
1757 std::sort( conflicted.begin(), conflicted.end() );
1758
1759 for( const wxString& name : conflicted )
1760 {
1761 std::vector<PIN_ON_NET> pins = pinToNetMap[name];
1762
1763 std::sort( pins.begin(), pins.end(),
1764 []( const PIN_ON_NET& a, const PIN_ON_NET& b )
1765 {
1766 if( a.m_netName != b.m_netName )
1767 return a.m_netName < b.m_netName;
1768
1769 return a.m_pin->m_Uuid < b.m_pin->m_Uuid;
1770 } );
1771
1772 const PIN_ON_NET& first = pins.front();
1773
1774 for( const PIN_ON_NET& other : pins )
1775 {
1776 if( other.m_netName == first.m_netName )
1777 continue;
1778
1779 std::shared_ptr<ERC_ITEM> ercItem = ERC_ITEM::Create( ERCE_DIFFERENT_UNIT_NET );
1780
1781 ercItem->SetErrorMessage( wxString::Format( _( "Pin %s is connected to both %s and %s" ),
1782 other.m_pin->GetShownNumber(),
1783 other.m_netName,
1784 first.m_netName ) );
1785
1786 ercItem->SetItems( other.m_pin, first.m_pin );
1787 ercItem->SetSheetSpecificPath( other.m_sheet );
1788 ercItem->SetItemsSheetPaths( other.m_sheet, first.m_sheet );
1789
1790 SCH_MARKER* marker = new SCH_MARKER( std::move( ercItem ), other.m_pin->GetPosition() );
1791 other.m_sheet.LastScreen()->Append( marker );
1792 errors += 1;
1793 break;
1794 }
1795 }
1796
1797 return errors;
1798}
1799
1800
1802{
1803 int errors = 0;
1804
1805 for( const SCH_SHEET_PATH& sheet : m_sheetList )
1806 {
1807 SCH_SCREEN* screen = sheet.LastScreen();
1808
1809 for( SCH_ITEM* item : screen->Items().OfType( SCH_SYMBOL_T ) )
1810 {
1811 SCH_SYMBOL* symbol = static_cast<SCH_SYMBOL*>( item );
1812 LIB_SYMBOL* libSymbol = symbol->GetLibSymbolRef().get();
1813
1814 if( !libSymbol )
1815 continue;
1816
1817 if( libSymbol->GetDuplicatePinNumbersAreJumpers() )
1818 continue;
1819
1820 std::vector<SCH_PIN*> pins = symbol->GetPins( &sheet );
1821
1822 std::map<wxString, std::vector<std::pair<SCH_PIN*, wxString>>> pinsByNumber;
1823
1824 for( SCH_PIN* pin : pins )
1825 {
1826 SCH_CONNECTION* conn = pin->Connection( &sheet );
1827 wxString netName = conn ? conn->GetNetName() : wxString();
1828
1829 pinsByNumber[pin->GetNumber()].emplace_back( pin, netName );
1830 }
1831
1832 for( const auto& [pinNumber, pinNetPairs] : pinsByNumber )
1833 {
1834 if( pinNetPairs.size() < 2 )
1835 continue;
1836
1837 wxString firstNet = pinNetPairs[0].second;
1838 bool hasDifferentNets = false;
1839 SCH_PIN* conflictPin = nullptr;
1840
1841 for( size_t i = 1; i < pinNetPairs.size(); i++ )
1842 {
1843 if( pinNetPairs[i].second != firstNet )
1844 {
1845 hasDifferentNets = true;
1846 conflictPin = pinNetPairs[i].first;
1847 break;
1848 }
1849 }
1850
1851 if( hasDifferentNets )
1852 {
1853 std::shared_ptr<ERC_ITEM> ercItem = ERC_ITEM::Create( ERCE_DUPLICATE_PIN_ERROR );
1854 wxString msg;
1855
1856 msg.Printf( _( "Pin %s on symbol '%s' is connected to different nets: %s and %s" ),
1857 pinNumber,
1858 symbol->GetRef( &sheet ),
1859 firstNet.IsEmpty() ? _( "<no net>" ) : firstNet,
1860 pinNetPairs[1].second.IsEmpty() ? _( "<no net>" ) : pinNetPairs[1].second );
1861
1862 ercItem->SetErrorMessage( msg );
1863 ercItem->SetItems( pinNetPairs[0].first, conflictPin );
1864 ercItem->SetSheetSpecificPath( sheet );
1865 ercItem->SetItemsSheetPaths( sheet, sheet );
1866
1867 SCH_MARKER* marker = new SCH_MARKER( std::move( ercItem ),
1868 pinNetPairs[0].first->GetPosition() );
1869 screen->Append( marker );
1870 errors++;
1871 }
1872 }
1873 }
1874 }
1875
1876 return errors;
1877}
1878
1879
1881{
1882 int errors = 0;
1883
1884 auto isGround =
1885 []( const wxString& txt )
1886 {
1887 wxString upper = txt.Upper();
1888
1889 return upper.Contains( wxT( "GND" ) )
1890 || upper == wxT( "EARTH" ) || upper.StartsWith( wxT( "EARTH_" ) )
1891 || upper == wxT( "VSS" ) || upper == wxT( "VSSA" );
1892 };
1893
1894 for( const SCH_SHEET_PATH& sheet : m_sheetList )
1895 {
1896 SCH_SCREEN* screen = sheet.LastScreen();
1897
1898 for( SCH_ITEM* item : screen->Items().OfType( SCH_SYMBOL_T ) )
1899 {
1900 SCH_SYMBOL* symbol = static_cast<SCH_SYMBOL*>( item );
1901 bool hasGroundNet = false;
1902 std::vector<SCH_PIN*> mismatched;
1903
1904 for( SCH_PIN* pin : symbol->GetPins( &sheet ) )
1905 {
1906 // We are only interested in power pins
1907 if( pin->GetType() != ELECTRICAL_PINTYPE::PT_POWER_OUT
1908 && pin->GetType() != ELECTRICAL_PINTYPE::PT_POWER_IN )
1909 {
1910 continue;
1911 }
1912
1913 const SCH_CONNECTION* conn = pin->Connection( &sheet );
1914 const wxString net = conn ? conn->Name( true ) : wxString();
1915 const bool netIsGround = isGround( net );
1916
1917 if( netIsGround )
1918 hasGroundNet = true;
1919
1920 if( isGround( pin->GetShownName() ) && !netIsGround )
1921 mismatched.push_back( pin );
1922 }
1923
1924 if( hasGroundNet )
1925 {
1926 for( SCH_PIN* pin : mismatched )
1927 {
1928 std::shared_ptr<ERC_ITEM> ercItem = ERC_ITEM::Create( ERCE_GROUND_PIN_NOT_GROUND );
1929
1930 ercItem->SetErrorMessage( wxString::Format( _( "Pin %s not connected to ground net" ),
1931 pin->GetShownName() ) );
1932 ercItem->SetItems( pin );
1933 ercItem->SetSheetSpecificPath( sheet );
1934 ercItem->SetItemsSheetPaths( sheet );
1935
1936 SCH_MARKER* marker = new SCH_MARKER( std::move( ercItem ), pin->GetPosition() );
1937 screen->Append( marker );
1938 errors++;
1939 }
1940 }
1941 }
1942 }
1943
1944 return errors;
1945}
1946
1947
1949{
1950 int warnings = 0;
1951
1952 for( const SCH_SHEET_PATH& sheet : m_sheetList )
1953 {
1954 SCH_SCREEN* screen = sheet.LastScreen();
1955
1956 for( SCH_ITEM* item : screen->Items().OfType( SCH_SYMBOL_T ) )
1957 {
1958 SCH_SYMBOL* symbol = static_cast<SCH_SYMBOL*>( item );
1959
1960 for( SCH_PIN* pin : symbol->GetPins( &sheet ) )
1961 {
1962 bool valid;
1963 pin->GetStackedPinNumbers( &valid );
1964
1965 if( !valid )
1966 {
1967 std::shared_ptr<ERC_ITEM> ercItem = ERC_ITEM::Create( ERCE_STACKED_PIN_SYNTAX );
1968 ercItem->SetItems( pin );
1969 ercItem->SetSheetSpecificPath( sheet );
1970 ercItem->SetItemsSheetPaths( sheet );
1971
1972 SCH_MARKER* marker = new SCH_MARKER( std::move( ercItem ), pin->GetPosition() );
1973 screen->Append( marker );
1974 warnings++;
1975 }
1976 }
1977 }
1978 }
1979
1980 return warnings;
1981}
1982
1983
1985{
1986 int errCount = 0;
1987
1988 std::unordered_map<wxString, std::pair<SCH_ITEM*, SCH_SHEET_PATH>> globalLabels;
1989 std::unordered_map<wxString, std::pair<SCH_ITEM*, SCH_SHEET_PATH>> localLabels;
1990
1991 for( const std::pair<NET_NAME_CODE_CACHE_KEY, std::vector<CONNECTION_SUBGRAPH*>> net : m_nets )
1992 {
1993 for( CONNECTION_SUBGRAPH* subgraph : net.second )
1994 {
1995 const SCH_SHEET_PATH& sheet = subgraph->GetSheet();
1996
1997 for( SCH_ITEM* item : subgraph->GetItems() )
1998 {
1999 if( item->Type() == SCH_LABEL_T || item->Type() == SCH_GLOBAL_LABEL_T )
2000 {
2001 SCH_LABEL_BASE* label = static_cast<SCH_LABEL_BASE*>( item );
2002 wxString text = label->GetShownText( &sheet, FOR_NETNAME );
2003
2004 auto& map = item->Type() == SCH_LABEL_T ? localLabels : globalLabels;
2005 auto it = map.find( text );
2006
2007 // Use the lowest label UUID to ensure the check is deterministic.
2008 if( ( it == map.end() ) || ( label->m_Uuid < it->second.first->m_Uuid ) )
2009 map[text] = std::make_pair( label, sheet );
2010 }
2011 else if( item->Type() == SCH_PIN_T )
2012 {
2013 SCH_PIN* pin = static_cast<SCH_PIN*>( item );
2014
2015 if( !pin->IsPower() )
2016 continue;
2017
2018 SCH_SYMBOL* symbol = static_cast<SCH_SYMBOL*>( pin->GetParentSymbol() );
2019
2020 if( !symbol )
2021 continue;
2022
2023 wxString text = ( pin->IsGlobalPower() && !symbol->IsGlobalPower() )
2024 ? pin->GetShownName()
2025 : symbol->GetValue( &sheet, FOR_NETNAME );
2026
2027 auto& map = pin->IsGlobalPower() ? globalLabels : localLabels;
2028 auto it = map.find( text );
2029
2030 // Use the lowest pin UUID to ensure the check is deterministic.
2031 if( ( it == map.end() ) || ( pin->m_Uuid < it->second.first->m_Uuid ) )
2032 map[text] = std::make_pair( pin, sheet );
2033 }
2034 }
2035 }
2036 }
2037
2038 for( auto& [globalText, globalItem] : globalLabels )
2039 {
2040 for( auto& [localText, localItem] : localLabels )
2041 {
2042 if( globalText == localText )
2043 {
2044 ERCE_T errorCode = ( globalItem.first->Type() == SCH_PIN_T && localItem.first->Type() == SCH_PIN_T )
2047
2048 if( !m_settings.IsTestEnabled( errorCode ) )
2049 continue;
2050
2051 std::shared_ptr<ERC_ITEM> ercItem = ERC_ITEM::Create( errorCode );
2052 ercItem->SetItems( globalItem.first, localItem.first );
2053 ercItem->SetSheetSpecificPath( globalItem.second );
2054 ercItem->SetItemsSheetPaths( globalItem.second, localItem.second );
2055
2056 SCH_MARKER* marker = new SCH_MARKER( std::move( ercItem ), globalItem.first->GetPosition() );
2057 globalItem.second.LastScreen()->Append( marker );
2058
2059 errCount++;
2060 }
2061 }
2062 }
2063
2064 return errCount;
2065}
2066
2067
2069{
2070 int errors = 0;
2071
2072 struct SIMILAR_ENTRY
2073 {
2074 wxString m_text; // As shown before case normalisation.
2075 SCH_ITEM* m_item;
2076 SCH_SHEET_PATH m_sheet;
2077 };
2078
2079 std::unordered_map<wxString, std::vector<SIMILAR_ENTRY>> generalMap;
2080
2081 auto normalizeLabel =
2082 []( const wxString& aLabel )
2083 {
2084 wxString stripped = aLabel.Strip( wxString::leading ).Strip( wxString::trailing );
2085 wxString prefix;
2086 wxString value;
2087 wxString units;
2088
2089 SplitString( stripped, &prefix, &value, &units );
2090
2091 if( prefix == wxT( "+" ) )
2092 prefix = wxEmptyString;
2093
2095 units.Replace( wxS( "µ" ), 'u' );
2096 units.Replace( wxS( "μ" ), 'u' );
2097
2098 return prefix.Lower() + value.Lower() + units.Lower();
2099 };
2100
2101 // Collect first, report afterwards. The exclusion depends on the scan order of m_nets which is an
2102 // unordered map that can change between ERC runs and orphan saved exclusions.
2103 for( const std::pair<NET_NAME_CODE_CACHE_KEY, std::vector<CONNECTION_SUBGRAPH*>> net : m_nets )
2104 {
2105 for( CONNECTION_SUBGRAPH* subgraph : net.second )
2106 {
2107 const SCH_SHEET_PATH& sheet = subgraph->GetSheet();
2108
2109 for( SCH_ITEM* item : subgraph->GetItems() )
2110 {
2111 switch( item->Type() )
2112 {
2113 case SCH_LABEL_T:
2114 case SCH_HIER_LABEL_T:
2115 case SCH_GLOBAL_LABEL_T:
2116 {
2117 SCH_LABEL_BASE* label = static_cast<SCH_LABEL_BASE*>( item );
2118 wxString unnormalized = label->GetShownText( &sheet, FOR_NETNAME );
2119
2120 generalMap[normalizeLabel( unnormalized )].push_back( { unnormalized, label, sheet } );
2121 break;
2122 }
2123 case SCH_PIN_T:
2124 {
2125 SCH_PIN* pin = static_cast<SCH_PIN*>( item );
2126
2127 if( !pin->IsPower() )
2128 continue;
2129
2130 SCH_SYMBOL* symbol = static_cast<SCH_SYMBOL*>( pin->GetParentSymbol() );
2131 wxString unnormalized = symbol->GetValue( &sheet, FOR_NETNAME );
2132
2133 generalMap[normalizeLabel( unnormalized )].push_back( { unnormalized, pin, sheet } );
2134 break;
2135 }
2136
2137 default:
2138 break;
2139 }
2140 }
2141 }
2142 }
2143
2144 auto logError =
2145 [&]( const SIMILAR_ENTRY& aMain, const SIMILAR_ENTRY& aAux )
2146 {
2147 ERCE_T typeOfWarning;
2148
2149 if( aMain.m_item->Type() == SCH_PIN_T && aAux.m_item->Type() == SCH_PIN_T )
2150 typeOfWarning = ERCE_SIMILAR_POWER; // Two power pins
2151 else if( aMain.m_item->Type() == SCH_PIN_T || aAux.m_item->Type() == SCH_PIN_T )
2152 typeOfWarning = ERCE_SIMILAR_LABEL_AND_POWER; // A power pin and label
2153 else
2154 typeOfWarning = ERCE_SIMILAR_LABELS; // Two labels
2155
2156 std::shared_ptr<ERC_ITEM> ercItem = ERC_ITEM::Create( typeOfWarning );
2157 ercItem->SetItems( aMain.m_item, aAux.m_item );
2158 ercItem->SetSheetSpecificPath( aMain.m_sheet );
2159 ercItem->SetItemsSheetPaths( aMain.m_sheet, aAux.m_sheet );
2160
2161 SCH_MARKER* marker = new SCH_MARKER( std::move( ercItem ), aMain.m_item->GetPosition() );
2162 aMain.m_sheet.LastScreen()->Append( marker );
2163 };
2164
2165 // Stable sort so that both the pairing and each pair's primary and secondary roles are the same on
2166 // every ERC run.
2167 std::vector<wxString> keys;
2168 keys.reserve( generalMap.size() );
2169
2170 for( const auto& [key, entries] : generalMap )
2171 keys.push_back( key );
2172
2173 std::sort( keys.begin(), keys.end() );
2174
2175 for( const wxString& key : keys )
2176 {
2177 std::vector<SIMILAR_ENTRY> entries = generalMap[key];
2178
2179 std::sort( entries.begin(), entries.end(),
2180 []( const SIMILAR_ENTRY& a, const SIMILAR_ENTRY& b )
2181 {
2182 wxString pathA = a.m_sheet.Path().AsString();
2183 wxString pathB = b.m_sheet.Path().AsString();
2184
2185 if( pathA != pathB )
2186 return pathA < pathB;
2187
2188 return a.m_item->m_Uuid < b.m_item->m_Uuid;
2189 } );
2190
2191 // Report each unordered pair whose shown text differs only in case only once.
2192 for( size_t ii = 0; ii < entries.size(); ++ii )
2193 {
2194 for( size_t jj = ii + 1; jj < entries.size(); ++jj )
2195 {
2196 const SIMILAR_ENTRY& a = entries[ii];
2197 const SIMILAR_ENTRY& b = entries[jj];
2198
2199 if( a.m_text == b.m_text )
2200 continue;
2201
2202 // Similar local labels on different sheets are fine.
2203 if( a.m_item->Type() == SCH_LABEL_T
2204 && b.m_item->Type() == SCH_LABEL_T
2205 && a.m_sheet != b.m_sheet )
2206 {
2207 continue;
2208 }
2209
2210 logError( a, b );
2211 errors += 1;
2212 }
2213 }
2214 }
2215
2216 return errors;
2217}
2218
2219
2221{
2222 wxCHECK( m_schematic, 0 );
2223
2224 LIBRARY_MANAGER& manager = Pgm().GetLibraryManager();
2226 wxString msg;
2227 int err_count = 0;
2228
2229 for( SCH_SCREEN* screen = m_screens.GetFirst(); screen; screen = m_screens.GetNext() )
2230 {
2231 std::vector<SCH_MARKER*> markers;
2232
2233 for( SCH_ITEM* item : screen->Items().OfType( SCH_SYMBOL_T ) )
2234 {
2235 SCH_SYMBOL* symbol = static_cast<SCH_SYMBOL*>( item );
2236 LIB_SYMBOL* libSymbolInSchematic = symbol->GetLibSymbolRef().get();
2237
2238 if( !libSymbolInSchematic )
2239 continue;
2240
2241 wxString libName = symbol->GetLibId().GetLibNickname();
2242
2243 std::optional<const LIBRARY_TABLE_ROW*> optRow = manager.GetRow( LIBRARY_TABLE_TYPE::SYMBOL, libName );
2244
2245 if( !optRow || ( *optRow )->Disabled() )
2246 {
2247 if( m_settings.IsTestEnabled( ERCE_LIB_SYMBOL_ISSUES ) )
2248 {
2249 std::shared_ptr<ERC_ITEM> ercItem = ERC_ITEM::Create( ERCE_LIB_SYMBOL_ISSUES );
2250 ercItem->SetItems( symbol );
2251 msg.Printf( _( "The current configuration does not include the symbol library '%s'" ),
2252 UnescapeString( libName ) );
2253 ercItem->SetErrorMessage( msg );
2254
2255 markers.emplace_back( new SCH_MARKER( std::move( ercItem ), symbol->GetPosition() ) );
2256 }
2257
2258 continue;
2259 }
2260 else if( !adapter->IsLibraryLoaded( libName ) )
2261 {
2262 if( m_settings.IsTestEnabled( ERCE_LIB_SYMBOL_ISSUES ) )
2263 {
2264 std::shared_ptr<ERC_ITEM> ercItem = ERC_ITEM::Create( ERCE_LIB_SYMBOL_ISSUES );
2265 std::optional<wxString> uri = manager.GetFullURI( LIBRARY_TABLE_TYPE::SYMBOL, libName, true );
2266 wxCHECK2( uri.has_value(), uri = wxEmptyString );
2267 ercItem->SetItems( symbol );
2268 msg.Printf( _( "The symbol library '%s' was not found at '%s'" ),
2269 UnescapeString( libName ),
2270 *uri );
2271 ercItem->SetErrorMessage( msg );
2272
2273 markers.emplace_back( new SCH_MARKER( std::move( ercItem ), symbol->GetPosition() ) );
2274 }
2275
2276 continue;
2277 }
2278
2279 wxString symbolName = symbol->GetLibId().GetLibItemName();
2280 LIB_SYMBOL* libSymbol = adapter->LoadSymbol( symbol->GetLibId() );
2281
2282 if( libSymbol == nullptr )
2283 {
2284 if( m_settings.IsTestEnabled( ERCE_LIB_SYMBOL_ISSUES ) )
2285 {
2286 std::shared_ptr<ERC_ITEM> ercItem = ERC_ITEM::Create( ERCE_LIB_SYMBOL_ISSUES );
2287 ercItem->SetItems( symbol );
2288 msg.Printf( _( "Symbol '%s' not found in symbol library '%s'" ),
2289 UnescapeString( symbolName ),
2290 UnescapeString( libName ) );
2291 ercItem->SetErrorMessage( msg );
2292
2293 markers.emplace_back( new SCH_MARKER( std::move( ercItem ), symbol->GetPosition() ) );
2294 }
2295
2296 continue;
2297 }
2298
2299 std::unique_ptr<LIB_SYMBOL> flattenedSymbol = libSymbol->Flatten();
2300 int flags = m_schematic->Settings().SymbolCompareFlags();
2301
2302 if( m_settings.IsTestEnabled( ERCE_LIB_SYMBOL_MISMATCH ) )
2303 {
2304 // We have to check for duplicate pins first as they will cause Compare() to fail.
2305 // Symbols with duplicate pins are valid if those pins share the same net, so we
2306 // only skip the comparison here. The actual error checking for duplicate pins on
2307 // different nets is done in TestDuplicatePinNets().
2308 std::vector<wxString> messages;
2309
2310 if( !libSymbolInSchematic->GetDuplicatePinNumbersAreJumpers() )
2311 {
2312 UNITS_PROVIDER unitsProvider( schIUScale, EDA_UNITS::MILS );
2313 CheckDuplicatePins( libSymbolInSchematic, messages, &unitsProvider );
2314 }
2315
2316 if( messages.empty() && flattenedSymbol->Compare( *libSymbolInSchematic, flags ) != 0 )
2317 {
2318 std::shared_ptr<ERC_ITEM> ercItem = ERC_ITEM::Create( ERCE_LIB_SYMBOL_MISMATCH );
2319 ercItem->SetItems( symbol );
2320 msg.Printf( _( "Symbol '%s' doesn't match copy in library '%s'" ),
2321 UnescapeString( symbolName ),
2322 UnescapeString( libName ) );
2323 ercItem->SetErrorMessage( msg );
2324
2325 markers.emplace_back( new SCH_MARKER( std::move( ercItem ), symbol->GetPosition() ) );
2326 }
2327 }
2328 }
2329
2330 for( SCH_MARKER* marker : markers )
2331 {
2332 screen->Append( marker );
2333 err_count += 1;
2334 }
2335 }
2336
2337 return err_count;
2338}
2339
2340
2342{
2343 wxCHECK( m_schematic, 0 );
2344
2345 if( std::optional<LIBRARY_MANAGER_ADAPTER*> adapter =
2346 Pgm().GetLibraryManager().Adapter( LIBRARY_TABLE_TYPE::FOOTPRINT ) )
2347 {
2348 ( *adapter )->BlockUntilLoaded();
2349 }
2350
2351 wxString msg;
2352 int err_count = 0;
2353
2354 typedef int (*TESTER_FN_PTR)( const wxString&, PROJECT* );
2355
2356 TESTER_FN_PTR linkTester = (TESTER_FN_PTR) aCvPcb->IfaceOrAddress( KIFACE_TEST_FOOTPRINT_LINK );
2357
2358 for( SCH_SHEET_PATH& sheet : m_sheetList )
2359 {
2360 std::vector<SCH_MARKER*> markers;
2361
2362 for( SCH_ITEM* item : sheet.LastScreen()->Items().OfType( SCH_SYMBOL_T ) )
2363 {
2364 SCH_SYMBOL* symbol = static_cast<SCH_SYMBOL*>( item );
2365 wxString footprint = symbol->GetFootprintFieldText( &sheet, RESOLVED );
2366
2367 if( footprint.IsEmpty() )
2368 continue;
2369
2370 LIB_ID fpID;
2371
2372 if( fpID.Parse( footprint, true ) >= 0 )
2373 {
2374 std::shared_ptr<ERC_ITEM> ercItem = ERC_ITEM::Create( ERCE_FOOTPRINT_LINK_ISSUES );
2375 msg.Printf( _( "'%s' is not a valid footprint identifier" ), footprint );
2376 ercItem->SetErrorMessage( msg );
2377 ercItem->SetItems( symbol );
2378 markers.emplace_back( new SCH_MARKER( std::move( ercItem ), symbol->GetPosition() ) );
2379 continue;
2380 }
2381
2382 wxString libName = fpID.GetLibNickname();
2383 wxString fpName = fpID.GetLibItemName();
2384 int ret = (linkTester)( footprint, aProject );
2385
2387 {
2388 std::shared_ptr<ERC_ITEM> ercItem = ERC_ITEM::Create( ERCE_FOOTPRINT_LINK_ISSUES );
2389 msg.Printf( _( "The current configuration does not include the footprint library '%s'" ),
2390 libName );
2391 ercItem->SetErrorMessage( msg );
2392 ercItem->SetItems( symbol );
2393 markers.emplace_back( new SCH_MARKER( std::move( ercItem ), symbol->GetPosition() ) );
2394 }
2396 {
2397 std::shared_ptr<ERC_ITEM> ercItem = ERC_ITEM::Create( ERCE_FOOTPRINT_LINK_ISSUES );
2398 msg.Printf( _( "The footprint library '%s' is not enabled in the current configuration" ),
2399 libName );
2400 ercItem->SetErrorMessage( msg );
2401 ercItem->SetItems( symbol );
2402 markers.emplace_back( new SCH_MARKER( std::move( ercItem ), symbol->GetPosition() ) );
2403 }
2405 {
2406 std::shared_ptr<ERC_ITEM> ercItem = ERC_ITEM::Create( ERCE_FOOTPRINT_LINK_ISSUES );
2407 msg.Printf( _( "Footprint '%s' not found in library '%s'" ),
2408 fpName,
2409 libName );
2410 ercItem->SetErrorMessage( msg );
2411 ercItem->SetItems( symbol );
2412 markers.emplace_back( new SCH_MARKER( std::move( ercItem ), symbol->GetPosition() ) );
2413 }
2414 }
2415
2416 for( SCH_MARKER* marker : markers )
2417 {
2418 sheet.LastScreen()->Append( marker );
2419 err_count += 1;
2420 }
2421 }
2422
2423 return err_count;
2424}
2425
2426
2428{
2429 wxCHECK( m_schematic, 0 );
2430
2431 wxString msg;
2432 int err_count = 0;
2433
2434 for( SCH_SHEET_PATH& sheet : m_sheetList )
2435 {
2436 std::vector<SCH_MARKER*> markers;
2437
2438 for( SCH_ITEM* item : sheet.LastScreen()->Items().OfType( SCH_SYMBOL_T ) )
2439 {
2440 SCH_SYMBOL* sch_symbol = static_cast<SCH_SYMBOL*>( item );
2441 std::unique_ptr<LIB_SYMBOL>& lib_symbol = sch_symbol->GetLibSymbolRef();
2442
2443 if( !lib_symbol )
2444 continue;
2445
2446 wxArrayString filters = lib_symbol->GetFPFilters();
2447
2448 if( filters.empty() )
2449 continue;
2450
2451 wxString lowerId = sch_symbol->GetFootprintFieldText( &sheet, RESOLVED ).Lower();
2452 LIB_ID footprint;
2453
2454 if( footprint.Parse( lowerId ) > 0 )
2455 continue;
2456
2457 wxString lowerItemName = footprint.GetUniStringLibItemName().Lower();
2458 bool found = false;
2459
2460 for( wxString filter : filters )
2461 {
2462 filter.LowerCase();
2463
2464 // If the filter contains a ':' character, include the library name in the pattern
2465 if( filter.Contains( wxS( ":" ) ) )
2466 found |= lowerId.Matches( filter );
2467 else
2468 found |= lowerItemName.Matches( filter );
2469
2470 if( found )
2471 break;
2472 }
2473
2474 if( !found )
2475 {
2476 std::shared_ptr<ERC_ITEM> ercItem = ERC_ITEM::Create( ERCE_FOOTPRINT_FILTERS );
2477 msg.Printf( _( "Assigned footprint (%s) doesn't match footprint filters (%s)" ),
2478 footprint.GetUniStringLibItemName(),
2479 wxJoin( filters, ' ' ) );
2480 ercItem->SetErrorMessage( msg );
2481 ercItem->SetItems( sch_symbol );
2482 markers.emplace_back( new SCH_MARKER( std::move( ercItem ), sch_symbol->GetPosition() ) );
2483 }
2484 }
2485
2486 for( SCH_MARKER* marker : markers )
2487 {
2488 sheet.LastScreen()->Append( marker );
2489 err_count += 1;
2490 }
2491 }
2492
2493 return err_count;
2494}
2495
2496
2498{
2499 const int gridSize = m_schematic->Settings().m_ConnectionGridSize;
2500 int err_count = 0;
2501
2502 for( SCH_SCREEN* screen = m_screens.GetFirst(); screen; screen = m_screens.GetNext() )
2503 {
2504 std::vector<SCH_MARKER*> markers;
2505
2506 for( SCH_ITEM* item : screen->Items() )
2507 {
2508 if( item->Type() == SCH_LINE_T && item->IsConnectable() )
2509 {
2510 SCH_LINE* line = static_cast<SCH_LINE*>( item );
2511
2512 if( ( line->GetStartPoint().x % gridSize ) != 0
2513 || ( line->GetStartPoint().y % gridSize ) != 0 )
2514 {
2515 std::shared_ptr<ERC_ITEM> ercItem = ERC_ITEM::Create( ERCE_ENDPOINT_OFF_GRID );
2516 ercItem->SetItems( line );
2517
2518 markers.emplace_back( new SCH_MARKER( std::move( ercItem ), line->GetStartPoint() ) );
2519 }
2520 else if( ( line->GetEndPoint().x % gridSize ) != 0
2521 || ( line->GetEndPoint().y % gridSize ) != 0 )
2522 {
2523 std::shared_ptr<ERC_ITEM> ercItem = ERC_ITEM::Create( ERCE_ENDPOINT_OFF_GRID );
2524 ercItem->SetItems( line );
2525
2526 markers.emplace_back( new SCH_MARKER( std::move( ercItem ), line->GetEndPoint() ) );
2527 }
2528 }
2529 if( item->Type() == SCH_BUS_WIRE_ENTRY_T )
2530 {
2531 SCH_BUS_WIRE_ENTRY* entry = static_cast<SCH_BUS_WIRE_ENTRY*>( item );
2532
2533 for( const VECTOR2I& point : entry->GetConnectionPoints() )
2534 {
2535 if( ( point.x % gridSize ) != 0
2536 || ( point.y % gridSize ) != 0 )
2537 {
2538 std::shared_ptr<ERC_ITEM> ercItem = ERC_ITEM::Create( ERCE_ENDPOINT_OFF_GRID );
2539 ercItem->SetItems( entry );
2540
2541 markers.emplace_back( new SCH_MARKER( std::move( ercItem ), point ) );
2542 }
2543 }
2544 }
2545 else if( item->Type() == SCH_SYMBOL_T )
2546 {
2547 SCH_SYMBOL* symbol = static_cast<SCH_SYMBOL*>( item );
2548
2549 for( SCH_PIN* pin : symbol->GetPins( nullptr ) )
2550 {
2551 if( pin->GetType() == ELECTRICAL_PINTYPE::PT_NC )
2552 continue;
2553
2554 VECTOR2I pinPos = pin->GetPosition();
2555
2556 if( ( pinPos.x % gridSize ) != 0 || ( pinPos.y % gridSize ) != 0 )
2557 {
2558 std::shared_ptr<ERC_ITEM> ercItem = ERC_ITEM::Create( ERCE_ENDPOINT_OFF_GRID );
2559 ercItem->SetItems( pin );
2560
2561 markers.emplace_back( new SCH_MARKER( std::move( ercItem ), pinPos ) );
2562 break;
2563 }
2564 }
2565 }
2566 }
2567
2568 for( SCH_MARKER* marker : markers )
2569 {
2570 screen->Append( marker );
2571 err_count += 1;
2572 }
2573 }
2574
2575 return err_count;
2576}
2577
2578
2580{
2582 int err_count = 0;
2583 SIM_LIB_MGR libMgr( &m_schematic->Project() );
2584 wxString variant = m_schematic->GetCurrentVariant();
2585
2586 for( SCH_SHEET_PATH& sheet : m_sheetList )
2587 {
2588 if( sheet.GetExcludedFromSim( variant ) )
2589 continue;
2590
2591 std::vector<SCH_MARKER*> markers;
2592
2593 for( SCH_ITEM* item : sheet.LastScreen()->Items().OfType( SCH_SYMBOL_T ) )
2594 {
2595 SCH_SYMBOL* symbol = static_cast<SCH_SYMBOL*>( item );
2596
2597 // Power symbols and other symbols which have the reference starting with "#" are
2598 // not included in simulation
2599 if( symbol->GetRef( &sheet ).StartsWith( '#' ) || symbol->ResolveExcludedFromSim() )
2600 continue;
2601
2602 // Reset for each symbol
2603 reporter.Clear();
2604
2605 SIM_LIBRARY::MODEL model = libMgr.CreateModel( &sheet, *symbol, true, 0, variant, reporter );
2606
2607 if( reporter.HasMessage() )
2608 {
2609 wxString msg = reporter.GetMessages();
2610 std::shared_ptr<ERC_ITEM> ercItem = ERC_ITEM::Create( ERCE_SIMULATION_MODEL );
2611
2612 //Remove \n and \r at e.o.l if any:
2613 msg.Trim();
2614
2615 ercItem->SetErrorMessage( msg );
2616 ercItem->SetItems( symbol );
2617
2618 markers.emplace_back( new SCH_MARKER( std::move( ercItem ), symbol->GetPosition() ) );
2619 }
2620 }
2621
2622 for( SCH_MARKER* marker : markers )
2623 {
2624 sheet.LastScreen()->Append( marker );
2625 err_count += 1;
2626 }
2627 }
2628
2629 return err_count;
2630}
2631
2632
2634{
2635 wxCHECK( m_schematic, 0 );
2636
2638
2639 if( !adapter )
2640 return 0;
2641
2642 int err_count = 0;
2643
2644 // Flatten each alternate once per ERC run rather than once per referencing symbol.
2645 std::map<wxString, std::unique_ptr<LIB_SYMBOL>> flatAltCache;
2646
2647 for( SCH_SHEET_PATH& sheet : m_sheetList )
2648 {
2649 SCH_SCREEN* screen = sheet.LastScreen();
2650
2651 if( !screen )
2652 continue;
2653
2654 std::vector<SCH_MARKER*> markers;
2655
2656 for( SCH_ITEM* item : screen->Items().OfType( SCH_SYMBOL_T ) )
2657 {
2658 SCH_SYMBOL* symbol = static_cast<SCH_SYMBOL*>( item );
2659 SCH_SYMBOL_INSTANCE instance;
2660
2661 if( !symbol->GetInstance( instance, sheet.Path() ) )
2662 continue;
2663
2664 auto addMarker =
2665 [&]( int aErrorCode, const wxString& aMessage )
2666 {
2667 std::shared_ptr<ERC_ITEM> ercItem = ERC_ITEM::Create( aErrorCode );
2668
2669 ercItem->SetItems( symbol );
2670 ercItem->SetSheetSpecificPath( sheet );
2671 ercItem->SetErrorMessage( aMessage );
2672
2673 markers.emplace_back(
2674 new SCH_MARKER( std::move( ercItem ), symbol->GetPosition() ) );
2675 err_count++;
2676 };
2677
2678 for( const auto& [variantName, variant] : instance.m_Variants )
2679 {
2680 if( !variant.m_SymbolOverride )
2681 continue;
2682
2683 const LIB_ID& libId = *variant.m_SymbolOverride;
2684 wxString libIdStr = libId.Format();
2685
2686 // A self-referencing override is ignored by resolution, so skip it here too.
2687 if( libId == symbol->GetLibId() )
2688 continue;
2689
2690 auto cacheIt = flatAltCache.find( libIdStr );
2691
2692 if( cacheIt == flatAltCache.end() )
2693 {
2694 std::unique_ptr<LIB_SYMBOL> flatAlt;
2695
2696 try
2697 {
2698 if( LIB_SYMBOL* altSymbol = adapter->LoadSymbol( libId ) )
2699 flatAlt = altSymbol->Flatten();
2700 }
2701 catch( const IO_ERROR& )
2702 {
2703 }
2704
2705 cacheIt = flatAltCache.emplace( libIdStr, std::move( flatAlt ) ).first;
2706 }
2707
2708 const LIB_SYMBOL* flatAlt = cacheIt->second.get();
2709
2710 if( !flatAlt )
2711 {
2712 if( m_settings.IsTestEnabled( ERCE_VARIANT_SYMBOL_INVALID ) )
2713 {
2714 addMarker( ERCE_VARIANT_SYMBOL_INVALID,
2715 wxString::Format( _( "Variant '%s': symbol '%s' not found in libraries" ),
2716 variantName, libIdStr ) );
2717 }
2718
2719 continue;
2720 }
2721
2722 if( !m_settings.IsTestEnabled( ERCE_VARIANT_SYMBOL_INCOMPATIBLE ) )
2723 continue;
2724
2725 // The schematic's library symbol is stored flattened, so compare it directly.
2726 LIB_SYMBOL* baseSymbol = symbol->GetLibSymbolRef().get();
2727
2728 if( !baseSymbol )
2729 continue;
2730
2731 std::vector<VARIANT_COMPAT_RESULT> issues =
2732 ValidateVariantSymbolCompatibility( *baseSymbol, *flatAlt );
2733
2734 for( const VARIANT_COMPAT_RESULT& issue : issues )
2735 {
2737 wxString::Format( _( "Variant '%s', alternate '%s': %s" ),
2738 variantName, libIdStr, issue.detail ) );
2739 }
2740 }
2741 }
2742
2743 for( SCH_MARKER* marker : markers )
2744 screen->Append( marker );
2745 }
2746
2747 return err_count;
2748}
2749
2750
2752 KIFACE* aCvPcb, PROJECT* aProject, PROGRESS_REPORTER* aProgressReporter )
2753{
2754 m_sheetList.AnnotatePowerSymbols();
2755
2756 // Test duplicate sheet names inside a given sheet. While one can have multiple references
2757 // to the same file, each must have a unique name.
2758 if( m_settings.IsTestEnabled( ERCE_DUPLICATE_SHEET_NAME ) )
2759 {
2760 if( aProgressReporter )
2761 aProgressReporter->AdvancePhase( _( "Checking sheet names..." ) );
2762
2764 }
2765
2766 // Test pin-to-pad maps for stale pins, duplicate pad targets and bad pad references (issue #2282).
2767 if( m_settings.IsTestEnabled( ERCE_PIN_MAP_STALE_PIN ) || m_settings.IsTestEnabled( ERCE_PIN_MAP_DUPLICATE_PAD )
2768 || m_settings.IsTestEnabled( ERCE_PIN_MAP_BAD_PAD ) || m_settings.IsTestEnabled( ERCE_PIN_MAP_UNMAPPED_PIN ) )
2769 {
2770 if( aProgressReporter )
2771 aProgressReporter->AdvancePhase( _( "Checking pin maps..." ) );
2772
2773 TestPinMap( aCvPcb, aProject );
2774 }
2775
2776 // The connection graph has a whole set of ERC checks it can run
2777 if( aProgressReporter )
2778 aProgressReporter->AdvancePhase( _( "Checking conflicts..." ) );
2779
2780 // If we are using the new connectivity, make sure that we do a full-rebuild
2781 if( aEditFrame )
2782 {
2783 if( ADVANCED_CFG::GetCfg().m_IncrementalConnectivity )
2784 aEditFrame->RecalculateConnections( nullptr, GLOBAL_CLEANUP );
2785 else
2786 aEditFrame->RecalculateConnections( nullptr, NO_CLEANUP );
2787 }
2788
2789 m_schematic->ConnectionGraph()->RunERC();
2790
2791 if( aProgressReporter )
2792 aProgressReporter->AdvancePhase( _( "Checking units..." ) );
2793
2794 // Test is all units of each multiunit symbol have the same footprint assigned.
2795 if( m_settings.IsTestEnabled( ERCE_DIFFERENT_UNIT_FP ) )
2796 {
2797 if( aProgressReporter )
2798 aProgressReporter->AdvancePhase( _( "Checking footprints..." ) );
2799
2801 }
2802
2803 if( m_settings.IsTestEnabled( ERCE_MISSING_UNIT )
2804 || m_settings.IsTestEnabled( ERCE_MISSING_INPUT_PIN )
2805 || m_settings.IsTestEnabled( ERCE_MISSING_POWER_INPUT_PIN )
2806 || m_settings.IsTestEnabled( ERCE_MISSING_BIDI_PIN ) )
2807 {
2809 }
2810
2811 if( aProgressReporter )
2812 aProgressReporter->AdvancePhase( _( "Checking pins..." ) );
2813
2814 if( m_settings.IsTestEnabled( ERCE_DIFFERENT_UNIT_NET ) )
2816
2817 if( m_settings.IsTestEnabled( ERCE_DUPLICATE_PIN_ERROR ) )
2819
2820 // Test pins on each net against the pin connection table
2821 if( m_settings.IsTestEnabled( ERCE_PIN_TO_PIN_ERROR )
2822 || m_settings.IsTestEnabled( ERCE_PIN_TO_PIN_WARNING )
2823 || m_settings.IsTestEnabled( ERCE_POWERPIN_NOT_DRIVEN )
2824 || m_settings.IsTestEnabled( ERCE_PIN_NOT_DRIVEN ) )
2825 {
2826 TestPinToPin();
2827 }
2828
2829 if( m_settings.IsTestEnabled( ERCE_GROUND_PIN_NOT_GROUND ) )
2831
2832 if( m_settings.IsTestEnabled( ERCE_STACKED_PIN_SYNTAX ) )
2834
2835 // Test similar labels (i;e. labels which are identical when
2836 // using case insensitive comparisons)
2837 if( m_settings.IsTestEnabled( ERCE_SIMILAR_LABELS )
2838 || m_settings.IsTestEnabled( ERCE_SIMILAR_POWER )
2839 || m_settings.IsTestEnabled( ERCE_SIMILAR_LABEL_AND_POWER ) )
2840 {
2841 if( aProgressReporter )
2842 aProgressReporter->AdvancePhase( _( "Checking similar labels..." ) );
2843
2845 }
2846
2847 if( m_settings.IsTestEnabled( ERCE_SAME_LOCAL_GLOBAL_LABEL )
2848 || m_settings.IsTestEnabled( ERCE_SAME_LOCAL_GLOBAL_POWER ) )
2849 {
2850 if( aProgressReporter )
2851 aProgressReporter->AdvancePhase( _( "Checking local and global labels..." ) );
2852
2854 }
2855
2856 if( m_settings.IsTestEnabled( ERCE_UNRESOLVED_VARIABLE ) )
2857 {
2858 if( aProgressReporter )
2859 aProgressReporter->AdvancePhase( _( "Checking for unresolved variables..." ) );
2860
2861 TestTextVars( aDrawingSheet );
2862 }
2863
2864 if( m_settings.IsTestEnabled( ERCE_FIELD_NAME_WHITESPACE ) )
2865 {
2866 if( aProgressReporter )
2867 aProgressReporter->AdvancePhase( _( "Checking field names..." ) );
2868
2870 }
2871
2872 if( m_settings.IsTestEnabled( ERCE_EMPTY_LABEL_NAME ) )
2873 {
2874 if( aProgressReporter )
2875 aProgressReporter->AdvancePhase( _( "Checking for empty label names..." ) );
2876
2878 }
2879
2880 if( m_settings.IsTestEnabled( ERCE_SIMULATION_MODEL ) )
2881 {
2882 if( aProgressReporter )
2883 aProgressReporter->AdvancePhase( _( "Checking SPICE models..." ) );
2884
2886 }
2887
2888 if( m_settings.IsTestEnabled( ERCE_NOCONNECT_CONNECTED ) )
2889 {
2890 if( aProgressReporter )
2891 aProgressReporter->AdvancePhase( _( "Checking no connect pins for connections..." ) );
2892
2894 }
2895
2896 if( m_settings.IsTestEnabled( ERCE_LIB_SYMBOL_ISSUES )
2897 || m_settings.IsTestEnabled( ERCE_LIB_SYMBOL_MISMATCH ) )
2898 {
2899 if( aProgressReporter )
2900 aProgressReporter->AdvancePhase( _( "Checking for library symbol issues..." ) );
2901
2903 }
2904
2905 if( m_settings.IsTestEnabled( ERCE_FOOTPRINT_LINK_ISSUES ) && aCvPcb )
2906 {
2907 if( aProgressReporter )
2908 aProgressReporter->AdvancePhase( _( "Checking for footprint link issues..." ) );
2909
2910 TestFootprintLinkIssues( aCvPcb, aProject );
2911 }
2912
2913 if( m_settings.IsTestEnabled( ERCE_FOOTPRINT_FILTERS ) )
2914 {
2915 if( aProgressReporter )
2916 aProgressReporter->AdvancePhase( _( "Checking footprint assignments against footprint filters..." ) );
2917
2919 }
2920
2921 if( m_settings.IsTestEnabled( ERCE_ENDPOINT_OFF_GRID ) )
2922 {
2923 if( aProgressReporter )
2924 aProgressReporter->AdvancePhase( _( "Checking for off grid pins and wires..." ) );
2925
2927 }
2928
2929 if( m_settings.IsTestEnabled( ERCE_FOUR_WAY_JUNCTION ) )
2930 {
2931 if( aProgressReporter )
2932 aProgressReporter->AdvancePhase( _( "Checking for four way junctions..." ) );
2933
2935 }
2936
2937 if( m_settings.IsTestEnabled( ERCE_LABEL_MULTIPLE_WIRES ) )
2938 {
2939 if( aProgressReporter )
2940 aProgressReporter->AdvancePhase( _( "Checking for labels on more than one wire..." ) );
2941
2943 }
2944
2945 if( m_settings.IsTestEnabled( ERCE_UNDEFINED_NETCLASS ) )
2946 {
2947 if( aProgressReporter )
2948 aProgressReporter->AdvancePhase( _( "Checking for undefined netclasses..." ) );
2949
2951 }
2952
2953 if( m_settings.IsTestEnabled( ERCE_VARIANT_SYMBOL_INVALID )
2954 || m_settings.IsTestEnabled( ERCE_VARIANT_SYMBOL_INCOMPATIBLE ) )
2955 {
2956 if( aProgressReporter )
2957 aProgressReporter->AdvancePhase( _( "Checking variant symbols..." ) );
2958
2960 }
2961
2962 m_schematic->ResolveERCExclusionsPostUpdate();
2963}
const char * name
constexpr EDA_IU_SCALE schIUScale
Definition base_units.h:123
BOX2< VECTOR2I > BOX2I
Definition box2.h:927
static const ADVANCED_CFG & GetCfg()
Get the singleton instance's config, which is shared by all consumers.
constexpr Vec Centre() const
Definition box2.h:94
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:348
const KIID m_Uuid
Definition eda_item.h:597
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:118
EE_TYPE OfType(KICAD_T aType) const
Definition sch_rtree.h:248
static std::shared_ptr< ERC_ITEM > Create(int aErrorCode)
Constructs an ERC_ITEM for the given error code.
Definition erc_item.cpp:305
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:2220
const NET_MAP & m_nets
Definition erc.h:228
int TestStackedPinNotation()
Checks for pin numbers that resemble stacked pin notation but are invalid.
Definition erc.cpp:1948
void TestTextVars(DS_PROXY_VIEW_ITEM *aDrawingSheet)
Check for any unresolved text variable references.
Definition erc.cpp:399
int TestPinToPin()
Checks the full netlist against the pin-to-pin connectivity requirements.
Definition erc.cpp:1265
int TestSimilarLabels()
Checks for labels that differ only in capitalization.
Definition erc.cpp:2068
SCH_MULTI_UNIT_REFERENCE_MAP m_refMap
Definition erc.h:227
SCH_SCREENS m_screens
Definition erc.h:226
int TestEmptyLabelNames()
Check for labels with empty or whitespace-only names.
Definition erc.cpp:683
int TestFootprintLinkIssues(KIFACE *aCvPcb, PROJECT *aProject)
Test footprint links against the current footprint libraries.
Definition erc.cpp:2341
int TestOffGridEndpoints()
Test pins and wire ends for being off grid.
Definition erc.cpp:2497
int TestDuplicateSheetNames(bool aCreateMarker)
Inside a given sheet, one cannot have sheets with duplicate names (file names can be duplicated).
Definition erc.cpp:145
int TestSameLocalGlobalLabel()
Checks for global and local labels with the same name.
Definition erc.cpp:1984
int TestMultUnitPinConflicts()
Checks if shared pins on multi-unit symbols have been connected to different nets.
Definition erc.cpp:1704
int TestNoConnectPins()
In KiCad 5 and earlier, you could connect stuff up to pins with NC electrical type.
Definition erc.cpp:1174
int TestFootprintFilters()
Test symbols to ensure that assigned footprint passes any given footprint filters.
Definition erc.cpp:2427
int TestPinMap(KIFACE *aCvPcb, PROJECT *aProject)
Check pin-to-pad maps (issue #2282): stale pin references, duplicate pad targets, bad pads and unmapp...
Definition erc.cpp:197
int TestFieldNameWhitespace()
Check for field names with leading or trailing whitespace.
Definition erc.cpp:722
int TestDuplicatePinNets()
Checks if duplicate pin numbers within a symbol are connected to different nets.
Definition erc.cpp:1801
ERC_SETTINGS & m_settings
Definition erc.h:224
int TestFourWayJunction()
Test to see if there are potentially confusing 4-way junctions in the schematic.
Definition erc.cpp:1095
int TestMissingNetclasses()
Tests for netclasses that are referenced but not defined.
Definition erc.cpp:986
int TestSimModelIssues()
Test SPICE models for various issues.
Definition erc.cpp:2579
SCH_SHEET_LIST m_sheetList
Definition erc.h:225
bool m_showAllErrors
Definition erc.h:229
int TestGroundPins()
Checks for ground-labeled pins not on a ground net while another pin is.
Definition erc.cpp:1880
SCHEMATIC * m_schematic
Definition erc.h:223
void RunTests(DS_PROXY_VIEW_ITEM *aDrawingSheet, SCH_EDIT_FRAME *aEditFrame, KIFACE *aCvPcb, PROJECT *aProject, PROGRESS_REPORTER *aProgressReporter)
Definition erc.cpp:2751
int TestMissingUnits()
Test for uninstantiated units of multi unit symbols.
Definition erc.cpp:852
int TestLabelMultipleWires()
Test to see if there are labels that are connected to more than one wire.
Definition erc.cpp:1040
int TestVariantSymbols()
Test all variant symbol overrides for resolution and pin compatibility.
Definition erc.cpp:2633
int TestMultiunitFootprints()
Test if all units of each multiunit symbol have the same footprint assigned.
Definition erc.cpp:791
Hold an error message and may be used when throwing exceptions containing meaningful error messages.
bool IsLibraryLoaded(const wxString &aNickname)
std::optional< wxString > GetFullURI(LIBRARY_TABLE_TYPE aType, const wxString &aNickname, bool aSubstituted=false)
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)
A logical library item identifier and consists of various portions much like a URI.
Definition lib_id.h:45
int Parse(const UTF8 &aId, bool aFix=false)
Parse LIB_ID with the information from aId.
Definition lib_id.cpp:65
wxString GetUniStringLibId() const
Definition lib_id.h:144
UTF8 Format() const
Definition lib_id.cpp:132
const wxString GetUniStringLibItemName() const
Get strings for display messages in dialogs.
Definition lib_id.h:108
const UTF8 & GetLibItemName() const
Definition lib_id.h:98
const UTF8 & GetLibNickname() const
Return the logical library name portion of a LIB_ID.
Definition lib_id.h:83
Define a library symbol object.
Definition lib_symbol.h:119
std::vector< const SCH_PIN * > GetGraphicalPins(int aUnit=0, int aBodyStyle=0) const
Graphical pins: Return schematic pin objects as drawn (unexpanded), filtered by unit/body.
const std::vector< ASSOCIATED_FOOTPRINT > & GetEffectiveAssociatedFootprints() const
Definition lib_symbol.h:295
std::vector< SCH_PIN * > GetPins() const override
const PIN_MAP_SET & GetEffectivePinMaps() const
Definition lib_symbol.h:280
void RunOnChildren(const std::function< void(SCH_ITEM *)> &aFunction, RECURSE_MODE aMode) override
int GetUnitCount() const override
std::unique_ptr< LIB_SYMBOL > Flatten() const
Return a flattened symbol inheritance to the caller.
bool GetDuplicatePinNumbersAreJumpers() const
Definition lib_symbol.h:873
std::vector< std::set< wxString > > & JumperPinGroups()
Each jumper pin group is a set of pin numbers that should be treated as internally connected.
Definition lib_symbol.h:880
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:125
A symbol-owned ordered set of named pin maps.
Definition pin_map.h:124
bool IsEmpty() const
Definition pin_map.h:140
const std::vector< PIN_MAP > & GetAll() const
Definition pin_map.h:139
const PIN_MAP * FindByName(const wxString &aName) const
Definition pin_map.cpp:133
A named pin map.
Definition pin_map.h:65
const std::vector< PIN_MAP_ENTRY > & GetEntries() const
Definition pin_map.h:92
const wxString & GetName() const
Definition pin_map.h:70
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:63
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
wxString Name(bool aIgnoreSheet=false) const
Schematic editor (Eeschema) main window.
void RecalculateConnections(SCH_COMMIT *aCommit, SCH_CLEANUP_FLAGS aCleanupFlags, PROGRESS_REPORTER *aProgressReporter=nullptr, bool aCleanupDone=false)
Generate the connection data for the entire schematic hierarchy.
wxString GetUntranslatedName() const
Get the untranslated field name for storage, variable look-up, etc.
wxString GetShownText(const SCH_SHEET_PATH *aPath, RESOLUTION_CONTEXT aContext, const wxString &aVariantName=wxEmptyString, int aDepth=0) const
Base class for any item which can be embedded within the SCHEMATIC container class,...
Definition sch_item.h:165
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:311
std::vector< VECTOR2I > GetConnectionPoints() const override
Add all the connection points for this item to aPoints.
wxString GetShownText(const SCH_SHEET_PATH *aPath, RESOLUTION_CONTEXT aContext, int aDepth=0) const override
Segment description base class to describe items which have 2 end points (track, wire,...
Definition sch_line.h:39
std::vector< VECTOR2I > GetConnectionPoints() const override
Add all the connection points for this item to aPoints.
Definition sch_line.cpp:806
VECTOR2I GetEndPoint() const
Definition sch_line.h:145
VECTOR2I GetStartPoint() const
Definition sch_line.h:136
bool IsEndPoint(const VECTOR2I &aPoint) const override
Test if aPt is an end point of this schematic object.
Definition sch_line.h:88
bool IsGraphicLine() const
Return if the line is a graphic (non electrical line)
bool IsVisible() const
Definition sch_pin.cpp:489
PAD_RESOLUTION
Outcome of pin-to-pad resolution (issue #2282).
Definition sch_pin.h:173
VECTOR2I GetPosition() const override
Definition sch_pin.cpp:354
bool IsStacked(const SCH_PIN *aPin) const
Definition sch_pin.cpp:579
bool IsPower() const
Check if the pin is either a global or local power pin.
Definition sch_pin.cpp:483
ELECTRICAL_PINTYPE GetType() const
Definition sch_pin.cpp:411
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
int GetUnit() const
void Append(SCH_ITEM *aItem, bool aUpdateLibSymbol=true)
EE_RTREE & Items()
Get the full RTree, usually for iterating.
Definition sch_screen.h:118
const BOX2I GetBoundingBox() const override
Return the orthogonal bounding box of this object for display purposes.
VECTOR2I GetPosition() const override
Definition sch_shape.h:86
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:48
std::vector< SCH_FIELD > & GetFields()
Return a reference to the vector holding the sheet's fields.
Definition sch_sheet.h:91
SCH_FIELD * GetField(FIELD_T aFieldType)
Return a mandatory field in this sheet.
VECTOR2I GetPosition() const override
Definition sch_sheet.h:504
std::vector< SCH_SHEET_PIN * > & GetPins()
Definition sch_sheet.h:241
Schematic symbol object.
Definition sch_symbol.h:75
const wxString GetFootprintFieldText(const SCH_SHEET_PATH *aPath, RESOLUTION_CONTEXT aContext, const wxString &aVariantName=wxEmptyString) const
std::vector< const SCH_PIN * > GetPins(const SCH_SHEET_PATH *aSheet) const
Retrieve a list of the SCH_PINs for the given sheet path.
bool IsGlobalPower() const override
void GetFields(std::vector< SCH_FIELD * > &aVector, bool aVisibleOnly) const override
Populate a std::vector with SCH_FIELDs, sorted in ordinal order.
VECTOR2I GetPosition() const override
Definition sch_symbol.h:934
const LIB_ID & GetLibId() const override
Definition sch_symbol.h:164
bool GetInstance(SCH_SYMBOL_INSTANCE &aInstance, const KIID_PATH &aSheetPath, bool aTestFromEnd=false) const
const wxString GetValue(const SCH_SHEET_PATH *aPath, RESOLUTION_CONTEXT aContext, const wxString &aVariantName=wxEmptyString) const override
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, RESOLUTION_CONTEXT aContext, int aDepth=0) const
VECTOR2I GetPosition() const override
Definition sch_text.h:143
const BOX2I GetBoundingBox() const override
Return the orthogonal bounding box of this object for display purposes.
Definition sch_text.cpp:338
virtual wxString GetShownText(const SCH_SHEET_PATH *aPath, RESOLUTION_CONTEXT aContext, int aDepth=0) const
Definition sch_text.cpp:359
SIM_MODEL & CreateModel(SIM_MODEL::TYPE aType, std::span< const wxString > 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:243
VECTOR2I TransformCoordinate(const VECTOR2I &aPoint) const
Calculate a new coordinate according to the mirror/rotation transform.
Definition transform.cpp:40
double Distance(const VECTOR2< extended_type > &aVector) const
Compute the distance between two vectors.
Definition vector2d.h:549
A wrapper for reporting to a wxString object.
Definition reporter.h:242
const wxString ExpandEnvVarSubstitutions(const wxString &aString, const PROJECT *aProject)
Replace any environment variable & text variable references with their values.
Definition common.cpp:776
@ FOR_ERC_DRC
Definition common.h:91
@ FOR_NETNAME
Definition common.h:90
@ RESOLVED
Definition common.h:93
#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:98
const wxString CommentERC_H[]
Definition erc.cpp:81
const std::set< ELECTRICAL_PINTYPE > DrivenPinTypes
Definition erc.cpp:136
const std::set< ELECTRICAL_PINTYPE > DrivingPinTypes
Definition erc.cpp:118
const std::set< ELECTRICAL_PINTYPE > DrivingPowerPinTypes
Definition erc.cpp:130
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_VARIANT_SYMBOL_INCOMPATIBLE
Variant symbol override fails pin compatibility.
@ ERCE_STACKED_PIN_SYNTAX
Pin name resembles stacked pin notation.
@ ERCE_ENDPOINT_OFF_GRID
Pin or wire-end off grid.
@ ERCE_EMPTY_LABEL_NAME
Label has an empty or whitespace-only name.
@ 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_SAME_LOCAL_GLOBAL_POWER
Local power port and global power port have the same name.
@ ERCE_PIN_MAP_BAD_PAD
Pin map references a pad absent from the footprint.
@ ERCE_FOOTPRINT_LINK_ISSUES
The footprint link is invalid, or points to a missing (or inactive) footprint or library.
@ ERCE_PIN_MAP_DUPLICATE_PAD
Two symbol pins map to one pad (not stacked/jumper).
@ 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_FIELD_NAME_WHITESPACE
Field name has leading or trailing whitespace.
@ ERCE_DUPLICATE_SHEET_NAME
Duplicate sheet names within a given sheet.
@ ERCE_PIN_MAP_STALE_PIN
Pin map references a pin number not on the symbol.
@ 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_VARIANT_SYMBOL_INVALID
Variant symbol override LIB_ID cannot be resolved.
@ ERCE_PIN_MAP_UNMAPPED_PIN
A connected pin resolves to no footprint pad.
@ 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.
const wxChar *const traceSchNetChain
Flag to enable tracing of schematic net chain rebuild and ERC cross-chain checks.
@ KIFACE_TEST_FOOTPRINT_LINK_LIBRARY_NOT_ENABLED
Definition kiface_ids.h:56
@ KIFACE_TEST_FOOTPRINT_LINK
Definition kiface_ids.h:54
@ KIFACE_TEST_FOOTPRINT_LINK_NO_LIBRARY
Definition kiface_ids.h:55
@ KIFACE_TEST_FOOTPRINT_LINK_NO_FOOTPRINT
Definition kiface_ids.h:57
@ KIFACE_FOOTPRINT_PAD_NUMBERS
Function pointer type: void (*)( const wxString& aFootprint, PROJECT* aProject, std::set<wxString>& a...
Definition kiface_ids.h:62
PGM_BASE & Pgm()
The global program "get" accessor.
see class PGM_BASE
ELECTRICAL_PINTYPE
The symbol library pin object electrical types used in ERC tests.
Definition pin_type.h:32
@ PT_INPUT
usual pin input: must be connected
Definition pin_type.h:33
@ PT_NC
not connected (must be left open)
Definition pin_type.h:46
@ PT_OUTPUT
usual output
Definition pin_type.h:34
@ PT_TRISTATE
tri state bus pin
Definition pin_type.h:36
@ PT_BIDI
input or output (like port for a microprocessor)
Definition pin_type.h:35
@ PT_POWER_OUT
output of a regulator: intended to be connected to power input pins
Definition pin_type.h:43
@ PT_POWER_IN
power input (GND, VCC for ICs). Must be connected to a power output.
Definition pin_type.h:42
@ PT_UNSPECIFIED
unknown electrical properties: creates always a warning when connected
Definition pin_type.h:41
@ PT_PASSIVE
pin for passive symbols: must be connected, and can be connected to any pin.
Definition pin_type.h:39
wxString ElectricalPinTypeGetText(ELECTRICAL_PINTYPE)
Definition pin_type.cpp:203
static float distance(const SFVEC2UI &a, const SFVEC2UI &b)
static const std::vector< KICAD_T > labelTypes
Definition sch_field.cpp:45
@ NO_CLEANUP
Definition schematic.h:92
@ GLOBAL_CLEANUP
Definition schematic.h:94
std::vector< wxString > ExpandStackedPinNotation(const wxString &aPinName, bool *aValid)
Expand stacked pin notation like [1,2,3], [1-4], [A1-A4], or [AA1-AA3,AB4,CD12-CD14] into individual ...
wxString UnescapeString(const wxString &aSource)
int SplitString(const wxString &strToSplit, wxString *strBeginning, wxString *strDigits, wxString *strEnd)
Break a string into three parts: he alphabetic preamble, the numeric part, and any alphabetic ending.
#define TO_UTF8(wxstring)
Convert a wxString to a UTF8 encoded C string for all wxWidgets build modes.
A first-class footprint choice on a LIB_SYMBOL, tied to a named pin map.
Definition pin_map.h:159
Implement a participant in the KIWAY alchemy.
Definition kiway.h:153
virtual void * IfaceOrAddress(int aDataId)=0
Return pointer to the requested object.
One symbol-pin to footprint-pad mapping inside a PIN_MAP.
Definition pin_map.h:42
A simple container for schematic symbol instance information.
std::map< wxString, SCH_SYMBOL_VARIANT > m_Variants
A list of symbol variants.
std::string path
IbisParser parser & reporter
KIBIS_MODEL * model
KIBIS_PIN * pin
wxLogTrace helper definitions.
KICAD_T
The set of class identification values stored in EDA_ITEM::m_structType.
Definition typeinfo.h:70
@ SCH_LINE_T
Definition typeinfo.h:159
@ SCH_NO_CONNECT_T
Definition typeinfo.h:156
@ SCH_SYMBOL_T
Definition typeinfo.h:168
@ SCH_FIELD_T
Definition typeinfo.h:146
@ SCH_LABEL_T
Definition typeinfo.h:163
@ SCH_LOCATE_ANY_T
Definition typeinfo.h:195
@ SCH_SHEET_T
Definition typeinfo.h:171
@ SCH_HIER_LABEL_T
Definition typeinfo.h:165
@ SCH_TEXT_T
Definition typeinfo.h:147
@ SCH_BUS_WIRE_ENTRY_T
Definition typeinfo.h:157
@ SCH_TEXTBOX_T
Definition typeinfo.h:148
@ SCH_GLOBAL_LABEL_T
Definition typeinfo.h:164
@ SCH_PIN_T
Definition typeinfo.h:149
std::vector< VARIANT_COMPAT_RESULT > ValidateVariantSymbolCompatibility(const LIB_SYMBOL &aBase, const LIB_SYMBOL &aCandidate)
Check whether aCandidate can be used as a variant symbol override for aBase.
VECTOR2< int32_t > VECTOR2I
Definition vector2d.h:683