KiCad PCB EDA Suite
Loading...
Searching...
No Matches
sch_sheet_path.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) 2017 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 <optional>
23#include <set>
24#include <tuple>
25
26#include <refdes_utils.h>
27#include <hash.h>
28#include <sch_screen.h>
29#include <sch_marker.h>
30#include <sch_label.h>
31#include <sch_shape.h>
32#include <sch_sheet_path.h>
33#include <sch_symbol.h>
34#include <sch_sheet.h>
35#include <schematic.h>
36#include <string_utils.h>
37#include <template_fieldnames.h>
38#include <trace_helpers.h>
39
40#include <wx/filename.h>
41#include <wx/log.h>
42
43
50{
51public:
53 SCH_ITEM( nullptr, NOT_USED )
54 {}
55
56 wxString GetItemDescription( UNITS_PROVIDER* aUnitsProvider, bool aFull ) const override
57 {
58 return _( "(Deleted Item)" );
59 }
60
61 wxString GetClass() const override
62 {
63 return wxT( "DELETED_SHEET_ITEM" );
64 }
65
67 {
68 static DELETED_SHEET_ITEM* item = nullptr;
69
70 if( !item )
71 item = new DELETED_SHEET_ITEM();
72
73 return item;
74 }
75
76 // pure virtuals:
77 void SetPosition( const VECTOR2I& ) override {}
78 void Move( const VECTOR2I& aMoveVector ) override {}
79 void MirrorHorizontally( int aCenter ) override {}
80 void MirrorVertically( int aCenter ) override {}
81 void Rotate( const VECTOR2I& aCenter, bool aRotateCCW ) override {}
82
83 double Similarity( const SCH_ITEM& aOther ) const override
84 {
85 return 0.0;
86 }
87
88 bool operator==( const SCH_ITEM& aOther ) const override
89 {
90 return false;
91 }
92
93#if defined(DEBUG)
94 void Show( int , std::ostream& ) const override {}
95#endif
96};
97
98
100{
101 m_DNP = aSymbol.GetDNP();
106
107 // Snapshot the base pin-map override so a variant created for an unrelated attribute does not
108 // mask the base override on read (issue #2282).
110}
111
112
114{
115 return m_DNP != aSymbol.GetDNP() || m_ExcludedFromBOM != aSymbol.GetExcludedFromBOM()
118 || !m_PinMapOverride.IsDefault() || m_SymbolOverride.has_value();
119}
120
121
123{
124 m_DNP = aSheet.GetDNP();
128 m_ExcludedFromPosFiles = false; // Sheets don't have position files exclusion
129}
130
131
133{
134 return m_DNP != aSheet.GetDNP() || m_ExcludedFromBOM != aSheet.GetExcludedFromBOM()
135 || m_ExcludedFromSim != aSheet.GetExcludedFromSim() || !m_Fields.empty();
136}
137
138
139namespace std
140{
142 {
143 return path.GetCurrentHash();
144 }
145}
146
147
148static bool stripRefSuffix( wxString& aRef, const wxString& aSuffix )
149{
150 if( aSuffix.IsEmpty() || aRef.length() <= aSuffix.length() || !aRef.EndsWith( aSuffix )
151 || aRef.EndsWith( wxS( "?" ) ) )
152 {
153 return false;
154 }
155
156 aRef.RemoveLast( aSuffix.length() );
157 return true;
158}
159
160
166
167
169{
170 initFromOther( aOther );
171}
172
173
175{
176 initFromOther( aOther );
177 return *this;
178}
179
180
181// Move assignment operator
183{
184 m_sheets = std::move( aOther.m_sheets );
185
186 m_virtualPageNumber = aOther.m_virtualPageNumber;
187 m_current_hash = aOther.m_current_hash;
188 m_cached_page_number = aOther.m_cached_page_number;
189 m_path = std::move( aOther.m_path );
190
191 m_recursion_test_cache = std::move( aOther.m_recursion_test_cache );
192
193 return *this;
194}
195
196
197void SCH_SHEET_PATH::Swap( SCH_SHEET_PATH& aOther ) noexcept
198{
199 m_sheets.swap( aOther.m_sheets );
200 std::swap( m_virtualPageNumber, aOther.m_virtualPageNumber );
201 std::swap( m_current_hash, aOther.m_current_hash );
202 m_cached_page_number.swap( aOther.m_cached_page_number );
203 m_path.swap( aOther.m_path );
204 m_recursion_test_cache.swap( aOther.m_recursion_test_cache );
205}
206
207
209{
210 SCH_SHEET_PATH retv = *this;
211
212 size_t size = aOther.size();
213
214 for( size_t i = 0; i < size; i++ )
215 retv.push_back( aOther.at( i ) );
216
217 return retv;
218}
219
220
222{
223 m_sheets = aOther.m_sheets;
227 m_path = aOther.m_path;
228
229 // Note: don't copy m_recursion_test_cache as it is slow and we want std::vector<SCH_SHEET_PATH>
230 // to be very fast to construct for use in the connectivity algorithm.
232}
233
234
236{
237 m_sheets.push_back( aSheet );
238
239 // hash_combine folds sequentially and the path only ever grows at the end, so extend both
240 // instead of walking the whole list again. Hierarchy walks push and pop constantly.
242
243 // A virtual root carries the nil UUID and does not belong in the path
244 if( m_sheets.size() > 1 || aSheet->m_Uuid != niluuid )
245 m_path.push_back( aSheet->m_Uuid );
246}
247
248
250{
251 m_current_hash = 0;
252
253 // Keep the path built here rather than lazily in Path(). Path() is called from the parallel
254 // connectivity workers on sheet paths they share, and a lazy fill races. Retains capacity, so
255 // the repeated push_back/pop_back of a hierarchy walk does not reallocate.
256 m_path.clear();
257
258 for( SCH_SHEET* sheet : m_sheets )
259 hash_combine( m_current_hash, sheet->m_Uuid.Hash() );
260
261 if( m_sheets.empty() )
262 return;
263
264 m_path.reserve( m_sheets.size() );
265
266 // A virtual root carries the nil UUID and does not belong in the path
267 if( m_sheets[0]->m_Uuid != niluuid )
268 m_path.push_back( m_sheets[0]->m_Uuid );
269
270 for( size_t i = 1; i < m_sheets.size(); i++ )
271 m_path.push_back( m_sheets[i]->m_Uuid );
272}
273
274
275int SCH_SHEET_PATH::Cmp( const SCH_SHEET_PATH& aSheetPathToTest ) const
276{
277 if( size() > aSheetPathToTest.size() )
278 return 1;
279
280 if( size() < aSheetPathToTest.size() )
281 return -1;
282
283 // otherwise, same number of sheets.
284 for( unsigned i = 0; i < size(); i++ )
285 {
286 if( at( i )->m_Uuid < aSheetPathToTest.at( i )->m_Uuid )
287 return -1;
288
289 if( at( i )->m_Uuid != aSheetPathToTest.at( i )->m_Uuid )
290 return 1;
291 }
292
293 return 0;
294}
295
296
297int SCH_SHEET_PATH::ComparePageNum( const SCH_SHEET_PATH& aSheetPathToTest ) const
298{
299 wxString pageA = this->GetPageNumber();
300 wxString pageB = aSheetPathToTest.GetPageNumber();
301
302 int pageNumComp = SCH_SHEET::ComparePageNum( pageA, pageB );
303
304 if( pageNumComp == 0 )
305 {
306 int virtualPageA = GetVirtualPageNumber();
307 int virtualPageB = aSheetPathToTest.GetVirtualPageNumber();
308
309 if( virtualPageA > virtualPageB )
310 pageNumComp = 1;
311 else if( virtualPageA < virtualPageB )
312 pageNumComp = -1;
313 }
314
315 return pageNumComp;
316}
317
318
319bool SCH_SHEET_PATH::IsContainedWithin( const SCH_SHEET_PATH& aSheetPathToTest ) const
320{
321 if( aSheetPathToTest.size() > size() )
322 return false;
323
324 for( size_t i = 0; i < aSheetPathToTest.size(); ++i )
325 {
326 if( at( i )->m_Uuid != aSheetPathToTest.at( i )->m_Uuid )
327 {
328 wxLogTrace( traceSchSheetPaths, "Sheet path '%s' is not within path '%s'.",
329 aSheetPathToTest.Path().AsString(), Path().AsString() );
330
331 return false;
332 }
333 }
334
335 wxLogTrace( traceSchSheetPaths, "Sheet path '%s' is within path '%s'.",
336 aSheetPathToTest.Path().AsString(), Path().AsString() );
337
338 return true;
339}
340
341
343{
344 if( !empty() )
345 return m_sheets.back();
346
347 return nullptr;
348}
349
350
352{
353 SCH_SHEET* lastSheet = Last();
354
355 if( lastSheet )
356 return lastSheet->GetScreen();
357
358 return nullptr;
359}
360
361
363{
364 SCH_SHEET* lastSheet = Last();
365
366 if( lastSheet )
367 return lastSheet->GetScreen();
368
369 return nullptr;
370}
371
372
374{
375 for( SCH_SHEET* sheet : m_sheets )
376 {
377 if( sheet->GetExcludedFromSim() )
378 return true;
379 }
380
381 return false;
382}
383
384
385bool SCH_SHEET_PATH::GetExcludedFromSim( const wxString& aVariantName ) const
386{
387 if( aVariantName.IsEmpty() )
388 return GetExcludedFromSim();
389
390 SCH_SHEET_PATH copy = *this;
391
392 while( !copy.empty() )
393 {
394 SCH_SHEET* sheet = copy.Last();
395 copy.pop_back();
396
397 if( sheet->GetExcludedFromSim( &copy, aVariantName ) )
398 return true;
399 }
400
401 return false;
402}
403
404
406{
407 for( SCH_SHEET* sheet : m_sheets )
408 {
409 if( sheet->GetExcludedFromBOM() )
410 return true;
411 }
412
413 return false;
414}
415
416
417bool SCH_SHEET_PATH::GetExcludedFromBOM( const wxString& aVariantName ) const
418{
419 if( aVariantName.IsEmpty() )
420 return GetExcludedFromBOM();
421
422 SCH_SHEET_PATH copy = *this;
423
424 while( !copy.empty() )
425 {
426 SCH_SHEET* sheet = copy.Last();
427 copy.pop_back();
428
429 if( sheet->GetExcludedFromBOM( &copy, aVariantName ) )
430 return true;
431 }
432
433 return false;
434}
435
436
438{
439 for( SCH_SHEET* sheet : m_sheets )
440 {
441 if( sheet->GetExcludedFromBoard() )
442 return true;
443 }
444
445 return false;
446}
447
448
449bool SCH_SHEET_PATH::GetExcludedFromBoard( const wxString& aVariantName ) const
450{
451 if( aVariantName.IsEmpty() )
452 return GetExcludedFromBoard();
453
454 SCH_SHEET_PATH copy = *this;
455
456 while( !copy.empty() )
457 {
458 SCH_SHEET* sheet = copy.Last();
459 copy.pop_back();
460
461 if( sheet->GetExcludedFromBoard( &copy, aVariantName ) )
462 return true;
463 }
464
465 return false;
466}
467
468
470{
471 for( SCH_SHEET* sheet : m_sheets )
472 {
473 if( sheet->GetDNP() )
474 return true;
475 }
476
477 return false;
478}
479
480
481bool SCH_SHEET_PATH::GetDNP( const wxString& aVariantName ) const
482{
483 if( aVariantName.IsEmpty() )
484 return GetDNP();
485
486 SCH_SHEET_PATH copy = *this;
487
488 while( !copy.empty() )
489 {
490 SCH_SHEET* sheet = copy.Last();
491 copy.pop_back();
492
493 if( sheet->GetDNP( &copy, aVariantName ) )
494 return true;
495 }
496
497 return false;
498}
499
500
502{
503 wxString s;
504
505 s = wxT( "/" ); // This is the root path
506
507 // Start at 1 to avoid the root sheet, which does not need to be added to the path.
508 // Its timestamp changes anyway.
509 for( unsigned i = 1; i < size(); i++ )
510 s += at( i )->m_Uuid.AsString() + "/";
511
512 return s;
513}
514
515
517{
518 return m_path;
519}
520
521
523{
524 wxString suffix;
525 SCH_SHEET_PATH parentPath;
526
527 for( SCH_SHEET* sheet : m_sheets )
528 {
529 if( !sheet->IsVirtualRootSheet() )
530 suffix << sheet->GetRefSuffix( parentPath );
531
532 parentPath.push_back( sheet );
533 }
534
535 return suffix;
536}
537
538
539wxString SCH_SHEET_PATH::PathHumanReadable( bool aUseShortRootName,
540 bool aStripTrailingSeparator,
541 bool aEscapeSheetNames ) const
542{
543 wxString s;
544
545 // Determine the starting index - skip virtual root if present
546 size_t startIdx = 0;
547
548 if( !empty() && at( 0 )->IsVirtualRootSheet() )
549 startIdx = 1;
550
551 if( aUseShortRootName )
552 {
553 s = wxS( "/" ); // Use only the short name in netlists
554 }
555 else
556 {
557 wxString fileName;
558
559 if( size() > startIdx && at( startIdx )->GetScreen() )
560 fileName = at( startIdx )->GetScreen()->GetFileName();
561
562 wxFileName fn = fileName;
563
564 s = fn.GetName() + wxS( "/" );
565 }
566
567 // When the schematic has multiple top-level sheets, the top-level sheet
568 // belongs in the path: otherwise sibling top-level sheets collapse to the
569 // same prefix (just "/") and local labels with identical text on different
570 // top-level sheets end up sharing a net.
571 size_t loopStart = startIdx + 1;
572
573 if( aUseShortRootName && size() > startIdx )
574 {
575 SCH_SHEET* first = at( startIdx );
576 SCHEMATIC* schem = first ? first->Schematic() : nullptr;
577
578 if( schem && schem->GetTopLevelSheets().size() > 1 && first->IsTopLevelSheet() )
579 loopStart = startIdx;
580 }
581
582 SCH_SHEET_PATH parentPath;
583
584 for( size_t i = 0; i < loopStart && i < size(); ++i )
585 parentPath.push_back( at( i ) );
586
587 for( unsigned i = loopStart; i < size(); i++ )
588 {
589 wxString sheetName = at( i )->GetField( FIELD_T::SHEET_NAME )->GetShownText( &parentPath, FOR_GUI );
590 parentPath.push_back( at( i ) );
591
592 if( aEscapeSheetNames )
593 sheetName = EscapeString( sheetName, CTX_NETNAME );
594
595 s << sheetName << wxS( "/" );
596 }
597
598 if( aStripTrailingSeparator && s.EndsWith( "/" ) )
599 s = s.Left( s.length() - 1 );
600
601 return s;
602}
603
604
606{
607 std::vector<SCH_ITEM*> items;
608
609 std::copy_if( LastScreen()->Items().begin(), LastScreen()->Items().end(),
610 std::back_inserter( items ),
611 []( SCH_ITEM* aItem )
612 {
613 return ( aItem->Type() == SCH_SYMBOL_T
614 || aItem->Type() == SCH_GLOBAL_LABEL_T
615 || aItem->Type() == SCH_SHAPE_T );
616 } );
617
618 for( SCH_ITEM* item : items )
619 {
620 if( item->Type() == SCH_SYMBOL_T )
621 {
622 SCH_SYMBOL* symbol = static_cast<SCH_SYMBOL*>( item );
623
624 // GetBaseRef() and GetUnitSelection() are O(1) via the symbol's instance path index.
625 // Bypass SCH_FIELD::SetText so a display refresh does not invalidate connectivity
626 SCH_FIELD* reference = symbol->GetField( FIELD_T::REFERENCE );
627 reference->EDA_TEXT::SetText( symbol->GetBaseRef( this ).Strip( wxString::both ) );
628 symbol->SetUnit( symbol->GetUnitSelection( this ) );
630 }
631 else if( item->Type() == SCH_GLOBAL_LABEL_T )
632 {
633 SCH_GLOBALLABEL* label = static_cast<SCH_GLOBALLABEL*>( item );
634
635 if( label->GetFields().size() > 0 ) // Possible when reading a legacy .sch schematic
636 {
637 SCH_FIELD* intersheetRefs = label->GetField( FIELD_T::INTERSHEET_REFS );
638
639 // Fixup for legacy files which didn't store a position for the intersheet refs
640 // unless they were shown.
641 if( intersheetRefs->GetPosition() == VECTOR2I() && !intersheetRefs->IsVisible() )
643
644 intersheetRefs->SetVisible( label->Schematic()->Settings().m_IntersheetRefsShow );
645 LastScreen()->UpdateDisplayBounds( intersheetRefs );
646 }
647 }
648 else if( item->Type() == SCH_SHAPE_T )
649 {
650 SCH_SHAPE* shape = static_cast<SCH_SHAPE*>( item );
651 shape->UpdateHatching();
652 }
653 }
654}
655
656
657static bool matchesSymbolFilter( SCH_SYMBOL* aSymbol, const SCH_SHEET_PATH& aSheetPath, SYMBOL_FILTER aSymbolFilter )
658{
659 if( aSymbolFilter == SYMBOL_FILTER_ALL )
660 return true;
661
662 // A # reference still marks virtual symbols that lack the power flag, as in legacy libraries
663 const wxString ref = aSymbol->GetRef( &aSheetPath );
664 const bool isPseudoSymbol = aSymbol->IsPower() || ( !ref.IsEmpty() && ref[0] == wxT( '#' ) );
665
666 return aSymbolFilter == SYMBOL_FILTER_POWER ? isPseudoSymbol : !isPseudoSymbol;
667}
668
669
671 bool aForceIncludeOrphanSymbols ) const
672{
673 for( SCH_ITEM* item : LastScreen()->Items().OfType( SCH_SYMBOL_T ) )
674 {
675 SCH_SYMBOL* symbol = static_cast<SCH_SYMBOL*>( item );
676 AppendSymbol( aReferences, symbol, aSymbolFilter, aForceIncludeOrphanSymbols );
677 }
678}
679
680
682 bool aForceIncludeOrphanSymbols ) const
683{
684 // Skip pseudo-symbols, which have a reference starting with #. This mainly
685 // affects power symbols.
686 if( matchesSymbolFilter( aSymbol, *this, aSymbolFilter ) )
687 {
688 if( aSymbol->GetLibSymbolRef() || aForceIncludeOrphanSymbols )
689 {
690 SCH_REFERENCE schReference( aSymbol, *this );
691
692 schReference.SetSheetNumber( GetPageNumberAsInt() );
693 aReferences.AddItem( schReference );
694 }
695 }
696}
697
698
700{
701 for( SCH_ITEM* item : LastScreen()->Items().OfType( SCH_SYMBOL_T ) )
702 {
703 SCH_SYMBOL* symbol = static_cast<SCH_SYMBOL*>( item );
704 AppendMultiUnitSymbol( aRefList, symbol, aSymbolFilter );
705 }
706}
707
708
710 SYMBOL_FILTER aSymbolFilter ) const
711{
712 if( !matchesSymbolFilter( aSymbol, *this, aSymbolFilter ) )
713 return;
714
715 LIB_SYMBOL* symbol = aSymbol->GetLibSymbolRef().get();
716
717 if( symbol && symbol->GetUnitCount() > 1 )
718 {
719 SCH_REFERENCE schReference = SCH_REFERENCE( aSymbol, *this );
720 schReference.SetSheetNumber( GetPageNumberAsInt() );
721 wxString reference_str = schReference.GetRef();
722
723 // Never lock unassigned references
724 if( reference_str[reference_str.Len() - 1] == '?' )
725 return;
726
727 // Each channel of a reused sheet is its own package
728 aRefList[schReference.GetComposedRef()].AddItem( schReference );
729 }
730}
731
732
734{
735 return m_current_hash == d1.GetCurrentHash();
736}
737
738
739bool SCH_SHEET_PATH::TestForRecursion( const wxString& aSrcFileName, const wxString& aDestFileName )
740{
741 auto pair = std::make_pair( aSrcFileName, aDestFileName );
742
743 if( m_recursion_test_cache.count( pair ) )
744 return m_recursion_test_cache.at( pair );
745
746 SCHEMATIC* sch = LastScreen()->Schematic();
747
748 wxCHECK_MSG( sch, false, "No SCHEMATIC found in SCH_SHEET_PATH::TestForRecursion!" );
749
750 wxFileName rootFn = sch->GetFileName();
751 wxFileName srcFn = aSrcFileName;
752 wxFileName destFn = aDestFileName;
753
754 if( srcFn.IsRelative() )
755 srcFn.MakeAbsolute( rootFn.GetPath() );
756
757 if( destFn.IsRelative() )
758 destFn.MakeAbsolute( rootFn.GetPath() );
759
760 // The source and destination sheet file names cannot be the same.
761 if( srcFn == destFn )
762 {
763 m_recursion_test_cache[pair] = true;
764 return true;
765 }
766
770 unsigned i = 0;
771
772 while( i < size() )
773 {
774 wxFileName cmpFn = at( i )->GetFileName();
775
776 if( cmpFn.IsRelative() )
777 cmpFn.MakeAbsolute( rootFn.GetPath() );
778
779 // Test if the file name of the destination sheet is in anywhere in this sheet path.
780 if( cmpFn == destFn )
781 break;
782
783 i++;
784 }
785
786 // The destination sheet file name was not found in the sheet path or the destination
787 // sheet file name is the root sheet so no recursion is possible.
788 if( i >= size() || i == 0 )
789 {
790 m_recursion_test_cache[pair] = false;
791 return false;
792 }
793
794 // Walk back up to the root sheet to see if the source file name is already a parent in
795 // the sheet path. If so, recursion will occur.
796 do
797 {
798 i -= 1;
799
800 wxFileName cmpFn = at( i )->GetFileName();
801
802 if( cmpFn.IsRelative() )
803 cmpFn.MakeAbsolute( rootFn.GetPath() );
804
805 if( cmpFn == srcFn )
806 {
807 m_recursion_test_cache[pair] = true;
808 return true;
809 }
810
811 } while( i != 0 );
812
813 // The source sheet file name is not a parent of the destination sheet file name.
814 m_recursion_test_cache[pair] = false;
815 return false;
816}
817
818
820{
821 SCH_SHEET* sheet = Last();
822
823 wxCHECK( sheet, wxEmptyString );
824
825 KIID_PATH tmpPath = Path();
826
827 if( !tmpPath.empty() )
828 tmpPath.pop_back();
829 else
830 return wxEmptyString;
831
832 return sheet->getPageNumber( tmpPath );
833}
834
836{
837 long page;
838 wxString pageStr = GetPageNumber();
839
840 if( pageStr.ToLong( &page ) )
841 return (int) page;
842
843 return GetVirtualPageNumber();
844}
845
846
847void SCH_SHEET_PATH::SetPageNumber( const wxString& aPageNumber )
848{
849 SCH_SHEET* sheet = Last();
850
851 wxCHECK( sheet, /* void */ );
852
853 KIID_PATH tmpPath = Path();
854
855 if( !tmpPath.empty() )
856 {
857 tmpPath.pop_back();
858 }
859 else
860 {
861 wxCHECK_MSG( false, /* void */, wxS( "Sheet paths must have a least one valid sheet." ) );
862 }
863
864 sheet->addInstance( tmpPath );
865 sheet->setPageNumber( tmpPath, aPageNumber );
866}
867
868
869void SCH_SHEET_PATH::AddNewSymbolInstances( const SCH_SHEET_PATH& aPrefixSheetPath, const wxString& aProjectName,
870 const SCH_SHEET_LIST* aSourceSheets )
871{
872 wxCHECK( !aProjectName.IsEmpty(), /* void */ );
873
874 SCH_SHEET_PATH newSheetPath( aPrefixSheetPath );
875 SCH_SHEET_PATH currentSheetPath( *this );
876
877 // Prefix the new hierarchical path.
878 newSheetPath = newSheetPath + currentSheetPath;
879
880 // A loaded root takes the new sheet's UUID, so only the path below it can name a source instance
881 KIID_PATH below = Path();
882
883 if( !below.empty() )
884 below.erase( below.begin() );
885
886 for( SCH_ITEM* item : LastScreen()->Items().OfType( SCH_SYMBOL_T ) )
887 {
888 SCH_SYMBOL* symbol = static_cast<SCH_SYMBOL*>( item );
889
890 wxCHECK2( symbol, continue );
891
892 SCH_SYMBOL_INSTANCE newSymbolInstance;
893
894 if( symbol->GetInstance( newSymbolInstance, Path(), true )
895 || ( !below.empty() && symbol->GetInstance( newSymbolInstance, below, true ) ) )
896 {
897 newSymbolInstance.m_ProjectName = aProjectName;
898
899 if( aSourceSheets )
900 stripRefSuffix( newSymbolInstance.m_Reference,
901 aSourceSheets->SourceRefSuffix( *symbol, newSymbolInstance.m_Path ) );
902
903 // Use an existing symbol instance for this path if it exists.
904 newSymbolInstance.m_Path = newSheetPath.Path();
905 symbol->AddHierarchicalReference( newSymbolInstance );
906 }
907 else if( !symbol->GetInstances().empty() )
908 {
909 newSymbolInstance.m_ProjectName = aProjectName;
910
911 // Use the first symbol instance if any symbol instance data exists.
912 newSymbolInstance = symbol->GetInstances()[0];
913
914 if( aSourceSheets )
915 stripRefSuffix( newSymbolInstance.m_Reference,
916 aSourceSheets->SourceRefSuffix( *symbol, newSymbolInstance.m_Path ) );
917
918 newSymbolInstance.m_Path = newSheetPath.Path();
919 symbol->AddHierarchicalReference( newSymbolInstance );
920 }
921 else
922 {
923 newSymbolInstance.m_ProjectName = aProjectName;
924
925 // Fall back to the last saved symbol field and unit settings if there is no
926 // instance data.
927 newSymbolInstance.m_Path = newSheetPath.Path();
928 newSymbolInstance.m_Reference = symbol->GetField( FIELD_T::REFERENCE )->GetText();
929 newSymbolInstance.m_Unit = symbol->GetUnit();
930 symbol->AddHierarchicalReference( newSymbolInstance );
931 }
932 }
933}
934
935
937{
938 for( SCH_ITEM* item : LastScreen()->Items().OfType( SCH_SYMBOL_T ) )
939 {
940 SCH_SYMBOL* symbol = static_cast<SCH_SYMBOL*>( item );
941
942 wxCHECK2( symbol, continue );
943
944 SCH_SHEET_PATH fullSheetPath( aPrefixSheetPath );
945 SCH_SHEET_PATH currentSheetPath( *this );
946
947 // Prefix the hierarchical path of the symbol instance to be removed.
948 fullSheetPath = fullSheetPath + currentSheetPath;
949 symbol->RemoveInstance( fullSheetPath );
950 }
951}
952
953
954void SCH_SHEET_PATH::CheckForMissingSymbolInstances( const wxString& aProjectName )
955{
956 // Skip sheet paths without screens (e.g., sheets that haven't been loaded yet or virtual root)
957 if( aProjectName.IsEmpty() || !LastScreen() )
958 return;
959
960 wxLogTrace( traceSchSheetPaths, "CheckForMissingSymbolInstances for path: %s (project: %s)",
961 PathHumanReadable( false ), aProjectName );
962 wxLogTrace( traceSchSheetPaths, " Sheet path size=%zu, Path().AsString()='%s'",
963 size(), Path().AsString() );
964
965 for( SCH_ITEM* item : LastScreen()->Items().OfType( SCH_SYMBOL_T ) )
966 {
967 SCH_SYMBOL* symbol = static_cast<SCH_SYMBOL*>( item );
968
969 wxCHECK2( symbol, continue );
970
971 SCH_SYMBOL_INSTANCE symbolInstance;
972
973 if( !symbol->GetInstance( symbolInstance, Path() ) )
974 {
975 wxLogTrace( traceSchSheetPaths, "Adding missing symbol \"%s\" instance data for "
976 "sheet path '%s'.",
977 symbol->m_Uuid.AsString(), PathHumanReadable( false ) );
978
979 // Legacy schematics that are not shared do not contain separate instance data.
980 // The symbol reference and unit are saved in the reference field and unit entries.
981 if( !IsSharedPath() && ( LastScreen()->GetFileFormatVersionAtLoad() <= 20200310 ) )
982 {
983 SCH_FIELD* refField = symbol->GetField( FIELD_T::REFERENCE );
984 symbolInstance.m_Reference = refField->GetText();
985 symbolInstance.m_Unit = symbol->GetUnit();
986
987 wxLogTrace( traceSchSheetPaths, " Legacy format: Using reference '%s' from field, unit %d",
988 symbolInstance.m_Reference, symbolInstance.m_Unit );
989 }
990 else if( !symbol->GetInstances().empty() )
991 {
992 // Prefer an instance from the current project; shared schematics may carry
993 // instance data for several projects. Copying the full instance carries
994 // variant DNP / value / field overrides across when v9-imported files have
995 // been re-rooted and their stored paths no longer match.
996 const std::vector<SCH_SYMBOL_INSTANCE>& instances = symbol->GetInstances();
997
998 auto sourceIt = std::find_if( instances.begin(), instances.end(),
999 [&aProjectName]( const SCH_SYMBOL_INSTANCE& aInstance )
1000 {
1001 return aInstance.m_ProjectName == aProjectName;
1002 } );
1003
1004 if( sourceIt == instances.end() )
1005 sourceIt = instances.begin();
1006
1007 symbolInstance = *sourceIt;
1008
1009 wxLogTrace( traceSchSheetPaths,
1010 " Using available instance (project '%s'): ref=%s, unit=%d, variants=%zu",
1011 sourceIt->m_ProjectName, symbolInstance.m_Reference,
1012 symbolInstance.m_Unit, symbolInstance.m_Variants.size() );
1013 }
1014 else
1015 {
1016 // Fall back to the symbol's reference field and unit if no instance data exists.
1017 SCH_FIELD* refField = symbol->GetField( FIELD_T::REFERENCE );
1018 symbolInstance.m_Reference = refField->GetText();
1019 symbolInstance.m_Unit = symbol->GetUnit();
1020
1021 wxLogTrace( traceSchSheetPaths,
1022 " No instance data: Using reference '%s' from field, unit %d",
1023 symbolInstance.m_Reference, symbolInstance.m_Unit );
1024 }
1025
1026 symbolInstance.m_ProjectName = aProjectName;
1027 symbolInstance.m_Path = Path();
1028 symbol->AddHierarchicalReference( symbolInstance );
1029
1030 wxLogTrace( traceSchSheetPaths,
1031 " Created instance: ref=%s, path=%s",
1032 symbolInstance.m_Reference, symbolInstance.m_Path.AsString() );
1033 }
1034 else
1035 {
1036 wxLogTrace( traceSchSheetPaths,
1037 " Symbol %s already has instance: ref=%s, path=%s",
1038 symbol->m_Uuid.AsString(),
1039 symbolInstance.m_Reference,
1040 symbolInstance.m_Path.AsString() );
1041 }
1042 }
1043}
1044
1045
1047{
1048 wxCHECK( m_sheets.size() > 1, /* void */ );
1049
1050 wxFileName sheetFileName = Last()->GetFileName();
1051
1052 // If the sheet file name is absolute, then the user requested is so don't make it relative.
1053 if( sheetFileName.IsAbsolute() )
1054 return;
1055
1056 SCH_SCREEN* screen = LastScreen();
1057 SCH_SCREEN* parentScreen = m_sheets[ m_sheets.size() - 2 ]->GetScreen();
1058
1059 wxCHECK( screen && parentScreen, /* void */ );
1060
1061 wxFileName fileName = screen->GetFileName();
1062 wxFileName parentFileName = parentScreen->GetFileName();
1063
1064 // SCH_SCREEN file names must be absolute. If they are not, someone set them incorrectly
1065 // on load or on creation.
1066 wxCHECK( fileName.IsAbsolute() && parentFileName.IsAbsolute(), /* void */ );
1067
1068 if( fileName.GetPath() == parentFileName.GetPath() )
1069 {
1070 Last()->SetFileName( fileName.GetFullName() );
1071 }
1072 else if( fileName.MakeRelativeTo( parentFileName.GetPath() ) )
1073 {
1074 Last()->SetFileName( fileName.GetFullPath() );
1075 }
1076 else
1077 {
1078 Last()->SetFileName( screen->GetFileName() );
1079 }
1080
1081 wxLogTrace( tracePathsAndFiles,
1082 wxT( "\n File name: '%s'"
1083 "\n parent file name '%s',"
1084 "\n sheet '%s' file name '%s'." ),
1085 screen->GetFileName(), parentScreen->GetFileName(), PathHumanReadable(),
1086 Last()->GetFileName() );
1087}
1088
1089
1091{
1092 SCH_SHEET_PATH tmp = *this;
1093
1094 while( !tmp.empty() )
1095 {
1096 wxCHECK2( tmp.LastScreen(), continue );
1097
1098 if( tmp.LastScreen()->GetRefCount() > 1 )
1099 return true;
1100
1101 tmp.pop_back();
1102 }
1103
1104 return false;
1105}
1106
1107
1109{
1110 if( aSheet != nullptr )
1111 BuildSheetList( aSheet, false );
1112}
1113
1114
1115void SCH_SHEET_LIST::BuildSheetList( SCH_SHEET* aSheet, bool aCheckIntegrity )
1116{
1117 if( !aSheet )
1118 return;
1119
1120 wxLogTrace( traceSchSheetPaths,
1121 "BuildSheetList called with sheet '%s' (UUID=%s, isVirtualRoot=%d)",
1122 aSheet->GetName(),
1123 aSheet->m_Uuid.AsString(),
1124 aSheet->m_Uuid == niluuid ? 1 : 0 );
1125
1126 // Special handling for virtual root: process its children without adding the root itself
1127 if( aSheet->IsVirtualRootSheet() )
1128 {
1129 wxLogTrace( traceSchSheetPaths, " Skipping virtual root, processing children only" );
1130
1131 if( aSheet->GetScreen() )
1132 {
1133 std::vector<SCH_ITEM*> childSheets;
1134 aSheet->GetScreen()->GetSheets( &childSheets );
1135
1136 for( SCH_ITEM* item : childSheets )
1137 {
1138 SCH_SHEET* sheet = static_cast<SCH_SHEET*>( item );
1139 BuildSheetList( sheet, aCheckIntegrity );
1140 }
1141 }
1142
1143 return;
1144 }
1145
1146 std::vector<SCH_SHEET*> badSheets;
1147
1148 m_currentSheetPath.push_back( aSheet );
1149 m_currentSheetPath.SetVirtualPageNumber( static_cast<int>( size() ) + 1 );
1150 push_back( m_currentSheetPath );
1151
1152 if( m_currentSheetPath.LastScreen() )
1153 {
1154 wxString parentFileName = aSheet->GetFileName();
1155 std::vector<SCH_ITEM*> childSheets;
1156 m_currentSheetPath.LastScreen()->GetSheets( &childSheets );
1157
1158 for( SCH_ITEM* item : childSheets )
1159 {
1160 SCH_SHEET* sheet = static_cast<SCH_SHEET*>( item );
1161
1162 if( aCheckIntegrity )
1163 {
1164 if( !m_currentSheetPath.TestForRecursion( sheet->GetFileName(), parentFileName ) )
1165 BuildSheetList( sheet, true );
1166 else
1167 badSheets.push_back( sheet );
1168 }
1169 else
1170 {
1171 // If we are not performing a full recursion test, at least check if we are in
1172 // a simple recursion scenario to prevent stack overflow crashes
1173 wxCHECK2_MSG( sheet->GetFileName() != aSheet->GetFileName(), continue,
1174 wxT( "Recursion prevented in SCH_SHEET_LIST::BuildSheetList" ) );
1175
1176 BuildSheetList( sheet, false );
1177 }
1178 }
1179 }
1180
1181 if( aCheckIntegrity )
1182 {
1183 for( SCH_SHEET* sheet : badSheets )
1184 {
1185 m_currentSheetPath.LastScreen()->Remove( sheet );
1186 m_currentSheetPath.LastScreen()->SetContentModified();
1187 }
1188 }
1189
1190 m_currentSheetPath.pop_back();
1191}
1192
1193
1194void SCH_SHEET_LIST::SortByHierarchicalPageNumbers( bool aUpdateVirtualPageNums )
1195{
1196 for( const SCH_SHEET_PATH& path : *this )
1197 path.CachePageNumber();
1198
1199 std::sort( begin(), end(),
1200 []( const SCH_SHEET_PATH& a, const SCH_SHEET_PATH& b ) -> bool
1201 {
1202 // Find the divergence point in the paths
1203 size_t common_len = 0;
1204 size_t min_len = std::min( a.size(), b.size() );
1205
1206 while( common_len < min_len && a.at( common_len )->m_Uuid == b.at( common_len )->m_Uuid )
1207 common_len++;
1208
1209 // If one path is a prefix of the other, the shorter one comes first
1210 // This ensures parents come before children
1211 if( common_len == a.size() )
1212 return true; // a is a prefix of b - a is the parent
1213 if( common_len == b.size() )
1214 return false; // b is a prefix of a - b is the parent
1215
1216 // Paths diverge at common_len
1217 // If they share the same parent, sort by page number
1218 // This ensures siblings are sorted by page number
1219 SCH_SHEET* sheet_a = a.at( common_len );
1220 SCH_SHEET* sheet_b = b.at( common_len );
1221
1222 // Create partial paths to get to these sheets for page number comparison
1223 KIID_PATH ancestor;
1224 for( size_t i = 0; i < common_len; i++ )
1225 ancestor.push_back( a.at( i )->m_Uuid );
1226
1227 // Compare page numbers - use the last sheet's page number
1228 wxString page_a = sheet_a->getPageNumber( ancestor );
1229 wxString page_b = sheet_b->getPageNumber( ancestor );
1230
1231 int retval = SCH_SHEET::ComparePageNum( page_a, page_b );
1232
1233 if( retval != 0 )
1234 return retval < 0;
1235
1236 // If page numbers are the same, use virtual page numbers as a tie-breaker
1238 return true;
1239 else if( a.GetVirtualPageNumber() > b.GetVirtualPageNumber() )
1240 return false;
1241
1242 // Finally, use UUIDs for stable ordering when everything else is equal
1243 return a.GetCurrentHash() < b.GetCurrentHash();
1244 } );
1245
1246 if( aUpdateVirtualPageNums )
1247 {
1248 int virtualPageNum = 1;
1249
1250 for( SCH_SHEET_PATH& sheet : *this )
1251 sheet.SetVirtualPageNumber( virtualPageNum++ );
1252 }
1253}
1254
1255
1256void SCH_SHEET_LIST::SortByPageNumbers( bool aUpdateVirtualPageNums )
1257{
1258 for( const SCH_SHEET_PATH& path : *this )
1259 path.CachePageNumber();
1260
1261 std::sort( begin(), end(),
1262 []( const SCH_SHEET_PATH& a, const SCH_SHEET_PATH& b ) -> bool
1263 {
1265 b.GetCachedPageNumber() );
1266
1267 if( retval < 0 )
1268 return true;
1269 else if( retval > 0 )
1270 return false;
1271
1273 return true;
1274 else if( a.GetVirtualPageNumber() > b.GetVirtualPageNumber() )
1275 return false;
1276
1277 // Enforce strict ordering. If the page numbers are the same, use UUIDs
1278 return a.GetCurrentHash() < b.GetCurrentHash();
1279 } );
1280
1281 if( aUpdateVirtualPageNums )
1282 {
1283 int virtualPageNum = 1;
1284
1285 for( SCH_SHEET_PATH& sheet : *this )
1286 sheet.SetVirtualPageNumber( virtualPageNum++ );
1287 }
1288}
1289
1290
1291bool SCH_SHEET_LIST::NameExists( const wxString& aSheetName ) const
1292{
1293 for( const SCH_SHEET_PATH& sheet : *this )
1294 {
1295 if( sheet.Last()->GetName() == aSheetName )
1296 return true;
1297 }
1298
1299 return false;
1300}
1301
1302
1303bool SCH_SHEET_LIST::PageNumberExists( const wxString& aPageNumber ) const
1304{
1305 for( const SCH_SHEET_PATH& sheet : *this )
1306 {
1307 if( sheet.GetPageNumber() == aPageNumber )
1308 return true;
1309 }
1310
1311 return false;
1312}
1313
1314
1315void SCH_SHEET_LIST::TrimToPageNumbers( const std::vector<wxString>& aPageInclusions )
1316{
1317 auto it = std::remove_if( begin(), end(),
1318 [&]( const SCH_SHEET_PATH& sheet )
1319 {
1320 return std::find( aPageInclusions.begin(),
1321 aPageInclusions.end(),
1322 sheet.GetPageNumber() ) == aPageInclusions.end();
1323 } );
1324
1325 erase( it, end() );
1326}
1327
1328
1330{
1331 wxString pageNumber;
1332
1333 // Find the next available page number by checking all existing page numbers
1334 std::set<int> usedPageNumbers;
1335
1336 for( const SCH_SHEET_PATH& path : *this )
1337 {
1338 wxString existingPageNum = path.GetPageNumber();
1339 long pageNum = 0;
1340
1341 if( existingPageNum.ToLong( &pageNum ) && pageNum > 0 )
1342 usedPageNumbers.insert( static_cast<int>( pageNum ) );
1343 }
1344
1345 // Find the first available number starting from 1
1346 int nextAvailable = 1;
1347
1348 while( usedPageNumbers.count( nextAvailable ) > 0 )
1349 nextAvailable++;
1350
1351 pageNumber.Printf( wxT( "%d" ), nextAvailable );
1352 return pageNumber;
1353}
1354
1355
1357{
1358 for( const SCH_SHEET_PATH& sheet : *this )
1359 {
1360 if( sheet.LastScreen() && sheet.LastScreen()->IsContentModified() )
1361 return true;
1362 }
1363
1364 return false;
1365}
1366
1367
1369{
1370 for( const SCH_SHEET_PATH& sheet : *this )
1371 {
1372 if( sheet.LastScreen() )
1373 sheet.LastScreen()->SetContentModified( false );
1374 }
1375}
1376
1377
1378SCH_ITEM* SCH_SHEET_LIST::ResolveItem( const KIID& aID, SCH_SHEET_PATH* aPathOut, bool aAllowNullptrReturn ) const
1379{
1380 for( const SCH_SHEET_PATH& sheet : *this )
1381 {
1382 SCH_ITEM* item = sheet.ResolveItem( aID );
1383
1384 if( item )
1385 {
1386 if( aPathOut )
1387 *aPathOut = sheet;
1388
1389 return item;
1390 }
1391 }
1392
1393 // Not found; weak reference has been deleted.
1394 if( aAllowNullptrReturn )
1395 return nullptr;
1396 else
1398}
1399
1400
1402{
1403 if( !LastScreen() )
1404 return nullptr;
1405
1406 for( SCH_ITEM* aItem : LastScreen()->Items() )
1407 {
1408 if( aItem->m_Uuid == aID )
1409 return aItem;
1410
1411 SCH_ITEM* childMatch = nullptr;
1412
1413 aItem->RunOnChildren(
1414 [&]( SCH_ITEM* aChild )
1415 {
1416 if( aChild->m_Uuid == aID )
1417 childMatch = aChild;
1418 },
1420
1421 if( childMatch )
1422 return childMatch;
1423 }
1424
1425 return nullptr;
1426}
1427
1428
1429void SCH_SHEET_LIST::FillItemMap( std::unordered_map<KIID, EDA_ITEM*>& aMap ) const
1430{
1431 for( const SCH_SHEET_PATH& sheet : *this )
1432 {
1433 SCH_SCREEN* screen = sheet.LastScreen();
1434
1435 for( SCH_ITEM* aItem : screen->Items() )
1436 {
1437 aMap[ aItem->m_Uuid ] = aItem;
1438
1439 aItem->RunOnChildren(
1440 [&]( SCH_ITEM* aChild )
1441 {
1442 aMap[ aChild->m_Uuid ] = aChild;
1443 },
1445 }
1446 }
1447}
1448
1449
1451 bool aForceIncludeOrphanSymbols ) const
1452{
1453 for( const SCH_SHEET_PATH& sheet : *this )
1454 sheet.GetSymbols( aReferences, aSymbolFilter, aForceIncludeOrphanSymbols );
1455}
1456
1457
1459 SYMBOL_FILTER aSymbolFilter, bool aForceIncludeOrphanSymbols ) const
1460{
1461 for( const SCH_SHEET_PATH& sheet : *this )
1462 {
1463 if( sheet.IsContainedWithin( aSheetPath ) )
1464 sheet.GetSymbols( aReferences, aSymbolFilter, aForceIncludeOrphanSymbols );
1465 }
1466}
1467
1468
1469void SCH_SHEET_LIST::GetSheetsWithinPath( std::vector<SCH_SHEET_PATH>& aSheets,
1470 const SCH_SHEET_PATH& aSheetPath ) const
1471{
1472 for( const SCH_SHEET_PATH& sheet : *this )
1473 {
1474 if( sheet.IsContainedWithin( aSheetPath ) )
1475 aSheets.push_back( sheet );
1476 }
1477}
1478
1479
1480std::optional<SCH_SHEET_PATH> SCH_SHEET_LIST::GetSheetPathByKIIDPath( const KIID_PATH& aPath,
1481 bool aIncludeLastSheet ) const
1482{
1483 for( const SCH_SHEET_PATH& sheet : *this )
1484 {
1485 KIID_PATH testPath = sheet.Path();
1486
1487 if( !aIncludeLastSheet )
1488 testPath.pop_back();
1489
1490 if( testPath == aPath )
1491 return SCH_SHEET_PATH( sheet );
1492 }
1493
1494 return std::nullopt;
1495}
1496
1497
1499{
1500 for( auto it = begin(); it != end(); ++it )
1501 {
1503 ( *it ).GetMultiUnitSymbols( tempMap, aSymbolFilter );
1504
1505 for( SCH_MULTI_UNIT_REFERENCE_MAP::value_type& pair : tempMap )
1506 {
1507 // Merge this list into the main one
1508 unsigned n_refs = pair.second.GetCount();
1509
1510 for( unsigned thisRef = 0; thisRef < n_refs; ++thisRef )
1511 aRefList[pair.first].AddItem( pair.second[thisRef] );
1512 }
1513 }
1514}
1515
1516
1517bool SCH_SHEET_LIST::TestForRecursion( const SCH_SHEET_LIST& aSrcSheetHierarchy,
1518 const wxString& aDestFileName )
1519{
1520 if( empty() )
1521 return false;
1522
1523 SCHEMATIC* sch = at( 0 ).LastScreen()->Schematic();
1524
1525 wxCHECK_MSG( sch, false, "No SCHEMATIC found in SCH_SHEET_LIST::TestForRecursion!" );
1526
1527 wxFileName rootFn = sch->GetFileName();
1528 wxFileName destFn = aDestFileName;
1529
1530 if( destFn.IsRelative() )
1531 destFn.MakeAbsolute( rootFn.GetPath() );
1532
1533 // Test each SCH_SHEET_PATH in this SCH_SHEET_LIST for potential recursion.
1534 for( unsigned i = 0; i < size(); i++ )
1535 {
1536 // Test each SCH_SHEET_PATH in the source sheet.
1537 for( unsigned j = 0; j < aSrcSheetHierarchy.size(); j++ )
1538 {
1539 const SCH_SHEET_PATH* sheetPath = &aSrcSheetHierarchy[j];
1540
1541 for( unsigned k = 0; k < sheetPath->size(); k++ )
1542 {
1543 if( at( i ).TestForRecursion( sheetPath->GetSheet( k )->GetFileName(),
1544 aDestFileName ) )
1545 {
1546 return true;
1547 }
1548 }
1549 }
1550 }
1551
1552 // The source sheet file can safely be added to the destination sheet file.
1553 return false;
1554}
1555
1556
1558{
1559 for( SCH_SHEET_PATH& path : *this )
1560 {
1561 if( path.Path() == aPath->Path() )
1562 return &path;
1563 }
1564
1565 return nullptr;
1566}
1567
1568
1570{
1571 for( SCH_SHEET_PATH& sheetpath : *this )
1572 {
1573 if( sheetpath.LastScreen() == aScreen )
1574 return sheetpath;
1575 }
1576
1577 return SCH_SHEET_PATH();
1578}
1579
1580
1582{
1583 SCH_SHEET_LIST retval;
1584
1585 for( const SCH_SHEET_PATH& sheetpath : *this )
1586 {
1587 if( sheetpath.LastScreen() == aScreen )
1588 retval.push_back( sheetpath );
1589 }
1590
1591 return retval;
1592}
1593
1594
1596 const std::vector<SCH_SYMBOL_INSTANCE>& aSymbolInstances )
1597{
1598 for( SCH_SHEET_PATH& sheetPath : *this )
1599 {
1600 for( SCH_ITEM* item : sheetPath.LastScreen()->Items().OfType( SCH_SYMBOL_T ) )
1601 {
1602 SCH_SYMBOL* symbol = static_cast<SCH_SYMBOL*>( item );
1603
1604 wxCHECK2( symbol, continue );
1605
1606 KIID_PATH sheetPathWithSymbolUuid = sheetPath.Path();
1607 sheetPathWithSymbolUuid.push_back( symbol->m_Uuid );
1608
1609 auto it = std::find_if( aSymbolInstances.begin(), aSymbolInstances.end(),
1610 [ sheetPathWithSymbolUuid ]( const SCH_SYMBOL_INSTANCE& r ) -> bool
1611 {
1612 return sheetPathWithSymbolUuid == r.m_Path;
1613 } );
1614
1615 if( it == aSymbolInstances.end() )
1616 {
1617 wxLogTrace( traceSchSheetPaths, "No symbol instance found for symbol '%s'",
1618 sheetPathWithSymbolUuid.AsString() );
1619 continue;
1620 }
1621
1622 // Symbol instance paths are stored and looked up in memory with the root path so use
1623 // the full path here.
1624 symbol->AddHierarchicalReference( sheetPath.Path(), it->m_Reference, it->m_Unit );
1625 symbol->GetField( FIELD_T::REFERENCE )->SetText( it->m_Reference );
1626
1627 if( !it->m_Value.IsEmpty() )
1628 symbol->SetValueFieldText( it->m_Value );
1629
1630 if( !it->m_Footprint.IsEmpty() )
1631 symbol->SetFootprintFieldText( it->m_Footprint );
1632
1633 symbol->UpdatePrefix();
1634 }
1635 }
1636}
1637
1638
1639void SCH_SHEET_LIST::UpdateSheetInstanceData( const std::vector<SCH_SHEET_INSTANCE>& aSheetInstances )
1640{
1641
1642 for( SCH_SHEET_PATH& path : *this )
1643 {
1644 SCH_SHEET* sheet = path.Last();
1645
1646 wxCHECK2( sheet && path.Last(), continue );
1647
1648 auto it = std::find_if( aSheetInstances.begin(), aSheetInstances.end(),
1649 [&path]( const SCH_SHEET_INSTANCE& r ) -> bool
1650 {
1651 return path.Path() == r.m_Path;
1652 } );
1653
1654 if( it == aSheetInstances.end() )
1655 {
1656 wxLogTrace( traceSchSheetPaths, "No sheet instance found for path '%s'",
1657 path.Path().AsString() );
1658 continue;
1659 }
1660
1661 wxLogTrace( traceSchSheetPaths, "Setting sheet '%s' instance '%s' page number '%s'",
1662 ( sheet->GetName().IsEmpty() ) ? wxString( wxT( "root" ) ) : sheet->GetName(),
1663 path.Path().AsString(), it->m_PageNumber );
1664 path.SetPageNumber( it->m_PageNumber );
1665 }
1666}
1667
1668
1669std::vector<KIID_PATH> SCH_SHEET_LIST::GetPaths() const
1670{
1671 std::vector<KIID_PATH> paths;
1672
1673 for( const SCH_SHEET_PATH& sheetPath : *this )
1674 paths.emplace_back( sheetPath.Path() );
1675
1676 return paths;
1677}
1678
1679
1680std::vector<SCH_SHEET_INSTANCE> SCH_SHEET_LIST::GetSheetInstances() const
1681{
1682 std::vector<SCH_SHEET_INSTANCE> retval;
1683
1684 for( const SCH_SHEET_PATH& path : *this )
1685 {
1686 const SCH_SHEET* sheet = path.Last();
1687
1688 wxCHECK2( sheet, continue );
1689
1690 SCH_SHEET_INSTANCE instance;
1691 SCH_SHEET_PATH tmpPath = path;
1692
1693 tmpPath.pop_back();
1694 instance.m_Path = tmpPath.Path();
1695 instance.m_PageNumber = path.GetPageNumber();
1696
1697 retval.push_back( std::move( instance ) );
1698 }
1699
1700 return retval;
1701}
1702
1703
1705{
1706 for( const SCH_SHEET_PATH& instance : *this )
1707 {
1708 if( !instance.GetPageNumber().IsEmpty() )
1709 return false;
1710 }
1711
1712 return true;
1713}
1714
1715
1717{
1718 // Don't accidentally renumber existing sheets.
1719 wxCHECK( AllSheetPageNumbersEmpty(), /* void */ );
1720
1721 wxString tmp;
1722 int pageNumber = 1;
1723
1724 for( SCH_SHEET_PATH& instance : *this )
1725 {
1726 if( instance.Last()->IsVirtualRootSheet() )
1727 continue;
1728
1729 tmp.Printf( "%d", pageNumber );
1730 instance.SetPageNumber( tmp );
1731 pageNumber += 1;
1732 }
1733}
1734
1735
1737{
1738 // A page number is claimed by the first sheet in the list that uses it. Any sheet with an
1739 // empty page number, or one repeating a number an earlier sheet already claimed, is reassigned
1740 // to the lowest unused positive integer. The stored string is compared as-is, so custom
1741 // schemes (e.g. "A", "1.1") are preserved when unique. Every distinct existing page number is
1742 // reserved up front so a reassignment never steals a number a later, non-conflicting sheet
1743 // already holds.
1744 std::set<wxString> reservedPageIds;
1745
1746 for( const SCH_SHEET_PATH& instance : *this )
1747 {
1748 if( instance.Last()->IsVirtualRootSheet() )
1749 continue;
1750
1751 const wxString pageNumber = instance.GetPageNumber();
1752
1753 if( !pageNumber.IsEmpty() )
1754 reservedPageIds.insert( pageNumber );
1755 }
1756
1757 std::set<wxString> assignedPageIds;
1758 bool modified = false;
1759 long nextPage = 1;
1760
1761 for( SCH_SHEET_PATH& instance : *this )
1762 {
1763 if( instance.Last()->IsVirtualRootSheet() )
1764 continue;
1765
1766 const wxString pageNumber = instance.GetPageNumber();
1767
1768 // Keep the first sheet to claim a given page number.
1769 if( !pageNumber.IsEmpty() && assignedPageIds.insert( pageNumber ).second )
1770 continue;
1771
1772 wxString pageStr = wxString::Format( wxT( "%ld" ), nextPage );
1773
1774 while( reservedPageIds.count( pageStr ) || assignedPageIds.count( pageStr ) )
1775 {
1776 nextPage++;
1777 pageStr = wxString::Format( wxT( "%ld" ), nextPage );
1778 }
1779
1780 instance.SetPageNumber( pageStr );
1781 assignedPageIds.insert( pageStr );
1782 nextPage++;
1783 modified = true;
1784 }
1785
1786 return modified;
1787}
1788
1789
1790void SCH_SHEET_LIST::AddNewSymbolInstances( const SCH_SHEET_PATH& aPrefixSheetPath, const wxString& aProjectName,
1791 bool aStripRefSuffixes )
1792{
1793 for( SCH_SHEET_PATH& sheetPath : *this )
1794 sheetPath.AddNewSymbolInstances( aPrefixSheetPath, aProjectName, aStripRefSuffixes ? this : nullptr );
1795}
1796
1797
1799{
1800 for( SCH_SHEET_PATH& sheetPath : *this )
1801 sheetPath.RemoveSymbolInstances( aPrefixSheetPath );
1802}
1803
1804
1806 int aLastVirtualPageNumber )
1807{
1808 wxString pageNumber;
1809 int lastUsedPageNumber = 1;
1810 int nextVirtualPageNumber = aLastVirtualPageNumber;
1811
1812 // Fetch the list of page numbers already in use.
1813 std::vector< wxString > usedPageNumbers;
1814
1815 if( aPrefixSheetPath.size() )
1816 {
1817 SCH_SHEET_LIST prefixHierarchy( aPrefixSheetPath.at( 0 ) );
1818
1819 for( const SCH_SHEET_PATH& path : prefixHierarchy )
1820 {
1821 pageNumber = path.GetPageNumber();
1822
1823 if( !pageNumber.IsEmpty() )
1824 usedPageNumbers.emplace_back( pageNumber );
1825 }
1826 }
1827
1828 for( SCH_SHEET_PATH& sheetPath : *this )
1829 {
1830 KIID_PATH tmp = sheetPath.Path();
1831 SCH_SHEET_PATH newSheetPath( aPrefixSheetPath );
1832
1833 // Prefix the new hierarchical path.
1834 newSheetPath = newSheetPath + sheetPath;
1835
1836 // Sheets cannot have themselves in the path.
1837 tmp.pop_back();
1838
1839 SCH_SHEET* sheet = sheetPath.Last();
1840
1841 wxCHECK2( sheet, continue );
1842
1843 nextVirtualPageNumber += 1;
1844
1845 SCH_SHEET_INSTANCE instance;
1846
1847 // Add the instance if it doesn't already exist
1848 if( !sheet->getInstance( instance, tmp, true ) )
1849 {
1850 sheet->addInstance( tmp );
1851 sheet->getInstance( instance, tmp, true );
1852 }
1853
1854 // Get a new page number if we don't have one
1855 if( instance.m_PageNumber.IsEmpty() )
1856 {
1857 // Generate the next available page number.
1858 do
1859 {
1860 pageNumber.Printf( wxT( "%d" ), lastUsedPageNumber );
1861 lastUsedPageNumber += 1;
1862 } while( std::find( usedPageNumbers.begin(), usedPageNumbers.end(), pageNumber ) !=
1863 usedPageNumbers.end() );
1864
1865 instance.m_PageNumber = pageNumber;
1866 newSheetPath.SetVirtualPageNumber( nextVirtualPageNumber );
1867 }
1868
1869 newSheetPath.SetPageNumber( instance.m_PageNumber );
1870 usedPageNumbers.push_back( instance.m_PageNumber );
1871 }
1872}
1873
1874
1875// Units of each package by base, library id and the suffix of the sheet owning it
1876using PACKAGE_UNITS = std::map<std::tuple<wxString, wxString, wxString>, std::set<int>>;
1877
1878
1879// Older versions store a package's lowest suffix on every unit, so only a lower suffix can own it
1880static std::optional<wxString> claimPackageUnit( PACKAGE_UNITS& aPackages, const std::set<wxString>& aSuffixes,
1881 const wxString& aStored, const wxString& aLibId, int aUnit,
1882 const wxString& aOwnSuffix )
1883{
1884 for( const wxString& suffix : aSuffixes )
1885 {
1886 if( suffix >= aOwnSuffix )
1887 break;
1888
1889 wxString base = aStored;
1890
1891 if( !suffix.IsEmpty() && !stripRefSuffix( base, suffix ) )
1892 continue;
1893
1894 auto it = aPackages.find( { base, aLibId, suffix } );
1895
1896 if( it != aPackages.end() && it->second.insert( aUnit ).second )
1897 return base;
1898 }
1899
1900 return std::nullopt;
1901}
1902
1903
1905{
1906 struct MULTI_UNIT
1907 {
1908 SCH_SYMBOL* symbol;
1909 const SCH_SHEET_PATH* path;
1910 wxString stored;
1911 wxString suffix;
1912 int unit;
1913 std::optional<wxString> ownBase;
1914 };
1915
1916 std::vector<MULTI_UNIT> multiUnits;
1917 PACKAGE_UNITS packages;
1918 std::set<wxString> suffixes;
1919
1920 for( SCH_SHEET_PATH& path : *this )
1921 {
1922 const wxString suffix = path.ComposeRefSuffix();
1923
1924 suffixes.insert( suffix );
1925
1926 for( SCH_ITEM* item : path.LastScreen()->Items().OfType( SCH_SYMBOL_T ) )
1927 {
1928 SCH_SYMBOL* symbol = static_cast<SCH_SYMBOL*>( item );
1929 SCH_SYMBOL_INSTANCE instance;
1930
1931 if( !symbol->GetInstance( instance, path.PathRef() ) )
1932 continue;
1933
1934 wxString ref = instance.m_Reference;
1935
1936 if( symbol->GetUnitCount() > 1 )
1937 {
1938 std::optional<wxString> ownBase;
1939
1940 if( suffix.IsEmpty() || stripRefSuffix( ref, suffix ) )
1941 {
1942 ownBase = ref;
1943 packages[{ ref, symbol->GetLibId().Format(), suffix }].insert( instance.m_Unit );
1944 }
1945
1946 if( !suffix.IsEmpty() )
1947 {
1948 multiUnits.push_back( { symbol, &path, instance.m_Reference, suffix, instance.m_Unit,
1949 ownBase } );
1950 }
1951 }
1952 else if( stripRefSuffix( ref, suffix ) )
1953 {
1954 symbol->SetRef( &path, ref );
1955 }
1956 else if( !suffix.IsEmpty() && !ref.EndsWith( wxS( "?" ) ) )
1957 {
1958 symbol->SetRefLacksSuffix( path.PathRef() );
1959 }
1960 }
1961 }
1962
1963 // A package's own strip is only a guess, since U7 on _H2_V stores U7_H2_V on a _V unit too
1964 for( const MULTI_UNIT& entry : multiUnits )
1965 {
1966 if( std::optional<wxString> base = claimPackageUnit( packages, suffixes, entry.stored,
1967 entry.symbol->GetLibId().Format(), entry.unit,
1968 entry.suffix ) )
1969 {
1970 entry.symbol->SetRef( entry.path, *base );
1971 }
1972 else if( entry.ownBase )
1973 {
1974 entry.symbol->SetRef( entry.path, *entry.ownBase );
1975 }
1976 else if( !entry.stored.EndsWith( wxS( "?" ) ) )
1977 {
1978 entry.symbol->SetRefLacksSuffix( entry.path->Path() );
1979 }
1980 }
1981}
1982
1983
1985{
1986 struct PENDING
1987 {
1988 SCH_SYMBOL* symbol;
1989 const SCH_SHEET_PATH* path;
1990 wxString stored;
1991 wxString suffix;
1992 int unit;
1993 };
1994
1995 std::vector<PENDING> pending;
1996 PACKAGE_UNITS packages;
1997 std::set<wxString> suffixes;
1998
1999 for( const SCH_SHEET_PATH& path : *this )
2000 {
2001 const wxString suffix = path.ComposeRefSuffix();
2002
2003 suffixes.insert( suffix );
2004
2005 for( SCH_ITEM* item : path.LastScreen()->Items().OfType( SCH_SYMBOL_T ) )
2006 {
2007 SCH_SYMBOL* symbol = static_cast<SCH_SYMBOL*>( item );
2008 SCH_SYMBOL_INSTANCE instance;
2009
2010 if( symbol->GetUnitCount() < 2 || !symbol->GetInstance( instance, path.PathRef() )
2011 || instance.m_Reference.EndsWith( wxS( "?" ) ) )
2012 {
2013 continue;
2014 }
2015
2016 if( !instance.m_RefLacksSuffix )
2017 packages[{ instance.m_Reference, symbol->GetLibId().Format(), suffix }].insert( instance.m_Unit );
2018
2019 // A stripped unit's own suffix may only end the suffix of a package in another top level sheet
2020 if( !suffix.IsEmpty() )
2021 {
2022 const wxString stored = instance.m_RefLacksSuffix ? instance.m_Reference
2023 : instance.m_Reference + suffix;
2024
2025 pending.push_back( { symbol, &path, stored, suffix, instance.m_Unit } );
2026 }
2027 }
2028 }
2029
2030 for( const PENDING& entry : pending )
2031 {
2032 if( std::optional<wxString> base = claimPackageUnit( packages, suffixes, entry.stored,
2033 entry.symbol->GetLibId().Format(), entry.unit,
2034 entry.suffix ) )
2035 {
2036 // A package on an unsuffixed sheet keeps the stored reference, which SetRef() sees as no change
2037 entry.symbol->SetRef( entry.path, *base );
2038 entry.symbol->SetRefLacksSuffix( entry.path->Path(), false );
2039 }
2040 }
2041}
2042
2043
2045{
2046 const wxString base = aSymbol->GetBaseRef( &aSource );
2047 const int unit = aSymbol->GetUnitSelection( &aSource );
2048 int count = 0;
2049
2050 for( const SCH_SHEET_PATH& path : *this )
2051 {
2052 if( path.LastScreen() != aSource.LastScreen() || path == aSource )
2053 continue;
2054
2055 aSymbol->SetRef( &path, base );
2056 aSymbol->SetUnitSelection( &path, unit );
2057 count++;
2058 }
2059
2060 return count;
2061}
2062
2063
2064wxString SCH_SHEET_LIST::SourceRefSuffix( const SCH_SYMBOL& aSymbol, const KIID_PATH& aInstancePath ) const
2065{
2066 // This project's references were stripped when it loaded
2067 if( !aSymbol.Schematic() || aSymbol.Schematic()->IsInstancePathInProject( aInstancePath ) )
2068 return wxEmptyString;
2069
2070 const SCH_SHEET_PATH* source = nullptr;
2071 size_t depth = 0;
2072
2073 // The loaded root takes the new sheet's UUID, so only the path below it can match the source
2074 for( const SCH_SHEET_PATH& path : *this )
2075 {
2076 KIID_PATH below = path.Path();
2077
2078 if( !below.empty() )
2079 below.erase( below.begin() );
2080
2081 if( ( !source || below.size() > depth ) && aInstancePath.EndsWith( below ) )
2082 {
2083 source = &path;
2084 depth = below.size();
2085 }
2086 }
2087
2088 return source ? source->ComposeRefSuffix() : wxString();
2089}
2090
2091
2092void SCH_SHEET_LIST::CheckForMissingSymbolInstances( const wxString& aProjectName )
2093{
2094 wxLogTrace( traceSchSheetPaths,
2095 "SCH_SHEET_LIST::CheckForMissingSymbolInstances: Processing %zu sheet paths",
2096 size() );
2097
2098 for( SCH_SHEET_PATH& sheetPath : *this )
2099 {
2100 wxLogTrace( traceSchSheetPaths,
2101 " Processing sheet path: '%s' (size=%zu, KIID_PATH='%s')",
2102 sheetPath.PathHumanReadable( false ),
2103 sheetPath.size(),
2104 sheetPath.Path().AsString() );
2105 sheetPath.CheckForMissingSymbolInstances( aProjectName );
2106 }
2107}
2108
2109
2111{
2112 int lastVirtualPageNumber = 1;
2113
2114 for( const SCH_SHEET_PATH& sheetPath : *this )
2115 {
2116 if( sheetPath.GetVirtualPageNumber() > lastVirtualPageNumber )
2117 lastVirtualPageNumber = sheetPath.GetVirtualPageNumber();
2118 }
2119
2120 return lastVirtualPageNumber;
2121}
2122
2123
2124bool SCH_SHEET_LIST::HasPath( const KIID_PATH& aPath ) const
2125{
2126 for( const SCH_SHEET_PATH& path : *this )
2127 {
2128 if( path.Path() == aPath )
2129 return true;
2130 }
2131
2132 return false;
2133}
2134
2135
2136bool SCH_SHEET_LIST::ContainsSheet( const SCH_SHEET* aSheet ) const
2137{
2138 for( const SCH_SHEET_PATH& path : *this )
2139 {
2140 for( size_t i = 0; i < path.size(); i++ )
2141 {
2142 if( path.at( i ) == aSheet )
2143 return true;
2144 }
2145 }
2146
2147 return false;
2148}
2149
2150
2151std::optional<SCH_SHEET_PATH> SCH_SHEET_LIST::GetOrdinalPath( const SCH_SCREEN* aScreen ) const
2152{
2153 // Sheet paths with sheets that do not have a screen object are not valid.
2154 if( !aScreen )
2155 return std::nullopt;
2156
2157 for( const SCH_SHEET_PATH& path: *this )
2158 {
2159 if( path.LastScreen() == aScreen )
2160 return std::optional<SCH_SHEET_PATH>( path );
2161 }
2162
2163 return std::nullopt;
2164}
bool IsContentModified() const
Definition base_screen.h:56
void SetContentModified(bool aModified=true)
Definition base_screen.h:55
wxString GetClass() const override
Return the class name.
void SetPosition(const VECTOR2I &) override
static DELETED_SHEET_ITEM * GetInstance()
double Similarity(const SCH_ITEM &aOther) const override
Return a measure of how likely the other object is to represent the same object.
void Rotate(const VECTOR2I &aCenter, bool aRotateCCW) override
Rotate the item around aCenter 90 degrees in the clockwise direction.
void Move(const VECTOR2I &aMoveVector) override
Move the item by aMoveVector to a new position.
wxString GetItemDescription(UNITS_PROVIDER *aUnitsProvider, bool aFull) const override
Return a user-visible description string of this item.
bool operator==(const SCH_ITEM &aOther) const override
void MirrorVertically(int aCenter) override
Mirror item vertically about aCenter.
void MirrorHorizontally(int aCenter) override
Mirror item horizontally about aCenter.
const KIID m_Uuid
Definition eda_item.h:599
KICAD_T Type() const
Returns the type of object.
Definition eda_item.h:110
virtual bool IsVisible() const
Definition eda_text.h:226
virtual void SetVisible(bool aVisible)
Definition eda_text.cpp:348
EE_TYPE OfType(KICAD_T aType) const
Definition sch_rtree.h:248
bool EndsWith(const KIID_PATH &aPath) const
Test if aPath from the last path towards the first path.
Definition kiid.cpp:402
wxString AsString() const
Definition kiid.cpp:423
Definition kiid.h:46
size_t Hash() const
Definition kiid.cpp:231
wxString AsString() const
Definition kiid.cpp:264
UTF8 Format() const
Definition lib_id.cpp:132
Define a library symbol object.
Definition lib_symbol.h:114
int GetUnitCount() const override
Holds all the data relating to one schematic.
Definition schematic.h:155
wxString GetFileName() const
Helper to retrieve the filename from the root sheet screen.
SCHEMATIC_SETTINGS & Settings() const
std::vector< SCH_SHEET * > GetTopLevelSheets() const
Get the list of top-level sheets.
bool IsInstancePathInProject(const KIID_PATH &aPath) const
Test whether an instance path is rooted in this schematic's top level sheets.
VECTOR2I GetPosition() const override
virtual const wxString & GetText() const override
Return the string associated with the text object.
Definition sch_field.h:139
wxString GetShownText(const SCH_SHEET_PATH *aPath, RESOLUTION_CONTEXT aContext, const wxString &aVariantName=wxEmptyString, int aDepth=0) const
void SetText(const wxString &aText) override
SCH_FIELD * GetField(FIELD_T aFieldType)
Return a mandatory field in this label.
Base class for any item which can be embedded within the SCHEMATIC container class,...
Definition sch_item.h:170
virtual void RunOnChildren(const std::function< void(SCH_ITEM *)> &aFunction, RECURSE_MODE aMode)
Definition sch_item.h:683
SCHEMATIC * Schematic() const
Search the item hierarchy to find a SCHEMATIC.
Definition sch_item.cpp:302
int GetUnit() const
Definition sch_item.h:243
virtual void SetUnit(int aUnit)
Definition sch_item.h:242
SCH_ITEM(EDA_ITEM *aParent, KICAD_T aType, int aUnit=0, int aBodyStyle=0)
Definition sch_item.cpp:65
void AutoplaceFields(SCH_SCREEN *aScreen, AUTOPLACE_ALGO aAlgo) override
std::vector< SCH_FIELD > & GetFields()
Definition sch_label.h:210
Container to create a flattened list of symbols because in a complex hierarchy, a symbol can be used ...
void AddItem(const SCH_REFERENCE &aItem)
A helper to define a symbol's reference designator in a schematic.
wxString GetComposedRef() const
Return the reference as shown on this sheet path, with its sheet suffix.
wxString GetRef() const
void SetSheetNumber(int aSheetNumber)
void UpdateDisplayBounds(SCH_ITEM *aItem)
Refresh display bounds only; the item's source and membership must be unchanged.
EE_RTREE & Items()
Get the full RTree, usually for iterating.
Definition sch_screen.h:122
const wxString & GetFileName() const
Definition sch_screen.h:157
SCHEMATIC * Schematic() const
int GetRefCount() const
Definition sch_screen.h:175
void GetSheets(std::vector< SCH_ITEM * > *aItems) const
Similar to Items().OfType( SCH_SHEET_T ), but return the sheets in a deterministic order (L-R,...
void UpdateHatching() const override
A container for handling SCH_SHEET_PATH objects in a flattened hierarchy.
std::optional< SCH_SHEET_PATH > GetSheetPathByKIIDPath(const KIID_PATH &aPath, bool aIncludeLastSheet=true) const
Finds a SCH_SHEET_PATH that matches the provided KIID_PATH.
SCH_ITEM * ResolveItem(const KIID &aID, SCH_SHEET_PATH *aPathOut=nullptr, bool aAllowNullptrReturn=false) const
Fetch a SCH_ITEM by ID.
std::optional< SCH_SHEET_PATH > GetOrdinalPath(const SCH_SCREEN *aScreen) const
Return the ordinal sheet path of aScreen.
SCH_SHEET_PATH m_currentSheetPath
void TrimToPageNumbers(const std::vector< wxString > &aPageInclusions)
Truncates the list by removing sheet's with page numbers not in the given list.
void SortByPageNumbers(bool aUpdateVirtualPageNums=true)
Sort the list of sheets by page number.
bool NameExists(const wxString &aSheetName) const
void GetSymbolsWithinPath(SCH_REFERENCE_LIST &aReferences, const SCH_SHEET_PATH &aSheetPath, SYMBOL_FILTER aSymbolFilter, bool aForceIncludeOrphanSymbols=false) const
Add a SCH_REFERENCE object to aReferences for each symbol in the list of sheets that are contained wi...
std::vector< SCH_SHEET_INSTANCE > GetSheetInstances() const
Fetch the instance information for all of the sheets in the hierarchy.
void UpdateSheetInstanceData(const std::vector< SCH_SHEET_INSTANCE > &aSheetInstances)
Update all of the sheet instance information using aSheetInstances.
void StripRefSuffixes()
Recover the base references from the composed references a file stores.
void SetInitialPageNumbers()
Set initial sheet page numbers.
void RemoveSymbolInstances(const SCH_SHEET_PATH &aPrefixSheetPath)
SCH_SHEET_LIST FindAllSheetsForScreen(const SCH_SCREEN *aScreen) const
Return a SCH_SHEET_LIST with a copy of all the SCH_SHEET_PATH using a particular screen.
wxString GetNextPageNumber() const
bool AllSheetPageNumbersEmpty() const
Check all of the sheet instance for empty page numbers.
bool IsModified() const
Check the entire hierarchy for any modifications.
SCH_SHEET_LIST(SCH_SHEET *aSheet=nullptr)
Construct a flattened list of SCH_SHEET_PATH objects from aSheet.
int GetLastVirtualPageNumber() const
void UpdateSymbolInstanceData(const std::vector< SCH_SYMBOL_INSTANCE > &aSymbolInstances)
Update all of the symbol instance information using aSymbolInstances.
void GetSheetsWithinPath(std::vector< SCH_SHEET_PATH > &aSheets, const SCH_SHEET_PATH &aSheetPath) const
Add a SCH_SHEET_PATH object to aSheets for each sheet in the list that are contained within aSheetPat...
void FillItemMap(std::unordered_map< KIID, EDA_ITEM * > &aMap) const
Fill an item cache for temporary use when many items need to be fetched.
bool PageNumberExists(const wxString &aPageNumber) const
void SortByHierarchicalPageNumbers(bool aUpdateVirtualPageNums=true)
This works like SortByPageNumbers, but it sorts the sheets first by their hierarchical depth and then...
void AddNewSheetInstances(const SCH_SHEET_PATH &aPrefixSheetPath, int aLastVirtualPageNumber)
void GetMultiUnitSymbols(SCH_MULTI_UNIT_REFERENCE_MAP &aRefList, SYMBOL_FILTER aSymbolFilter) const
Add a SCH_REFERENCE_LIST object to aRefList for each same-reference set of multi-unit parts in the li...
void RecoverPackageRefs()
Give a unit loaded by StripRefSuffixes() the base of a package in another top level sheet.
bool ContainsSheet(const SCH_SHEET *aSheet) const
void AddNewSymbolInstances(const SCH_SHEET_PATH &aPrefixSheetPath, const wxString &aProjectName, bool aStripRefSuffixes=false)
Attempt to add new symbol instances for all symbols in this list of sheet paths prefixed with aPrefix...
bool RepairPageNumbers()
Assign valid page numbers to sheet paths whose stored page number is missing or collides with an earl...
std::vector< KIID_PATH > GetPaths() const
int ShareReusedSheetRef(SCH_SYMBOL *aSymbol, const SCH_SHEET_PATH &aSource) const
Give every other instance of aSymbol's sheet the base reference and unit it has on aSource.
void BuildSheetList(SCH_SHEET *aSheet, bool aCheckIntegrity)
Build the list of sheets and their sheet path from aSheet.
SCH_SHEET_PATH FindSheetForScreen(const SCH_SCREEN *aScreen)
Return the first SCH_SHEET_PATH object (not necessarily the only one) using a particular screen.
void CheckForMissingSymbolInstances(const wxString &aProjectName)
wxString SourceRefSuffix(const SCH_SYMBOL &aSymbol, const KIID_PATH &aInstancePath) const
Return the suffix the source project composed into the instance of aSymbol at aInstancePath,...
bool HasPath(const KIID_PATH &aPath) const
SCH_SHEET_PATH * FindSheetForPath(const SCH_SHEET_PATH *aPath)
Return a pointer to the first SCH_SHEET_PATH object (not necessarily the only one) matching the provi...
void GetSymbols(SCH_REFERENCE_LIST &aReferences, SYMBOL_FILTER aSymbolFilter, bool aForceIncludeOrphanSymbols=false) const
Add a SCH_REFERENCE object to aReferences for each symbol in the list of sheets.
bool TestForRecursion(const SCH_SHEET_LIST &aSrcSheetHierarchy, const wxString &aDestFileName)
Test every SCH_SHEET_PATH in this SCH_SHEET_LIST to verify if adding the sheets stored in aSrcSheetHi...
Handle access to a stack of flattened SCH_SHEET objects by way of a path for creating a flattened sch...
void AppendSymbol(SCH_REFERENCE_LIST &aReferences, SCH_SYMBOL *aSymbol, SYMBOL_FILTER aSymbolFilter, bool aForceIncludeOrphanSymbols=false) const
Append a SCH_REFERENCE object to aReferences based on aSymbol.
bool IsSharedPath() const
Determine if this sheet path is shared in a complex hierarchy.
KIID_PATH m_path
The UUID path of m_sheets, kept in step with it by Rehash().
bool GetExcludedFromBOM() const
void Swap(SCH_SHEET_PATH &aOther) noexcept
const SCH_SHEET * GetSheet(unsigned aIndex) const
bool empty() const
Forwarded method from std::vector.
int ComparePageNum(const SCH_SHEET_PATH &aSheetPathToTest) const
Compare sheets by their page number.
size_t GetCurrentHash() const
void GetMultiUnitSymbols(SCH_MULTI_UNIT_REFERENCE_MAP &aRefList, SYMBOL_FILTER aSymbolFilter) const
Add a SCH_REFERENCE_LIST object to aRefList for each same-reference set of multi-unit parts in the sh...
void GetSymbols(SCH_REFERENCE_LIST &aReferences, SYMBOL_FILTER aSymbolFilter, bool aForceIncludeOrphanSymbols=false) const
Adds SCH_REFERENCE object to aReferences for each symbol in the sheet.
bool operator==(const SCH_SHEET_PATH &d1) const
KIID_PATH Path() const
Get the sheet path as an KIID_PATH.
bool TestForRecursion(const wxString &aSrcFileName, const wxString &aDestFileName)
Test the SCH_SHEET_PATH file names to check adding the sheet stored in the file aSrcFileName to the s...
void UpdateAllScreenReferences() const
Update all the symbol references for this sheet path.
void MakeFilePathRelativeToParentSheet()
Make the sheet file name relative to its parent sheet.
SCH_ITEM * ResolveItem(const KIID &aID) const
Fetch a SCH_ITEM by ID.
std::vector< SCH_SHEET * > m_sheets
SCH_SCREEN * LastScreen()
void AddNewSymbolInstances(const SCH_SHEET_PATH &aPrefixSheetPath, const wxString &aProjectName, const SCH_SHEET_LIST *aSourceSheets=nullptr)
Attempt to add new symbol instances for all symbols in this sheet path prefixed with aPrefixSheetPath...
int Cmp(const SCH_SHEET_PATH &aSheetPathToTest) const
Compare if this is the same sheet path as aSheetPathToTest.
void initFromOther(const SCH_SHEET_PATH &aOther)
wxString m_cached_page_number
wxString GetPageNumber() const
void RemoveSymbolInstances(const SCH_SHEET_PATH &aPrefixSheetPath)
void CheckForMissingSymbolInstances(const wxString &aProjectName)
bool IsContainedWithin(const SCH_SHEET_PATH &aSheetPathToTest) const
Check if this path is contained inside aSheetPathToTest.
SCH_SHEET * at(size_t aIndex) const
Forwarded method from std::vector.
const wxString & GetCachedPageNumber() const
void SetVirtualPageNumber(int aPageNumber)
Set the sheet instance virtual page number.
wxString PathHumanReadable(bool aUseShortRootName=true, bool aStripTrailingSeparator=false, bool aEscapeSheetNames=false) const
Return the sheet path in a human readable form made from the sheet names.
std::map< std::pair< wxString, wxString >, bool > m_recursion_test_cache
bool GetExcludedFromSim() const
wxString PathAsString() const
Return the path of time stamps which do not changes even when editing sheet parameters.
void AppendMultiUnitSymbol(SCH_MULTI_UNIT_REFERENCE_MAP &aRefList, SCH_SYMBOL *aSymbol, SYMBOL_FILTER aSymbolFilter) const
Append a SCH_REFERENCE_LIST object to aRefList based on aSymbol, storing same-reference set of multi-...
bool GetExcludedFromBoard() const
void SetPageNumber(const wxString &aPageNumber)
Set the sheet instance user definable page number.
SCH_SHEET_PATH & operator=(const SCH_SHEET_PATH &aOther)
int GetPageNumberAsInt() const
bool GetDNP() const
SCH_SHEET * Last() const
Return a pointer to the last SCH_SHEET of the list.
SCH_SHEET_PATH operator+(const SCH_SHEET_PATH &aOther)
int m_virtualPageNumber
Page numbers are maintained by the sheet load order.
void push_back(SCH_SHEET *aSheet)
Forwarded method from std::vector.
size_t size() const
Forwarded method from std::vector.
wxString ComposeRefSuffix() const
Compose the reference suffixes of every sheet in this path, from the root down.
int GetVirtualPageNumber() const
void pop_back()
Forwarded method from std::vector.
void InitializeAttributes(const SCH_SHEET &aSheet)
bool HasDifferentials(const SCH_SHEET &aSheet) const
Return true if the variant carries any differential against the base sheet values.
Sheet symbol placed in a schematic, and is the entry point for a sub schematic.
Definition sch_sheet.h:50
void SetFileName(const wxString &aFilename)
Definition sch_sheet.h:434
wxString GetFileName() const
Return the filename corresponding to this sheet.
Definition sch_sheet.h:428
bool getInstance(SCH_SHEET_INSTANCE &aInstance, const KIID_PATH &aSheetPath, bool aTestFromEnd=false) const
bool addInstance(const KIID_PATH &aInstance)
Add a new instance aSheetPath to the instance list.
wxString getPageNumber(const KIID_PATH &aParentPath) const
Return the sheet page number for aParentPath.
bool IsTopLevelSheet() const
Check if this sheet is a top-level sheet.
SCH_FIELD * GetField(FIELD_T aFieldType)
Return a mandatory field in this sheet.
wxString GetName() const
Definition sch_sheet.h:150
bool GetExcludedFromBOM(const SCH_SHEET_PATH *aInstance=nullptr, const wxString &aVariantName=wxEmptyString) const override
bool GetExcludedFromSim(const SCH_SHEET_PATH *aInstance=nullptr, const wxString &aVariantName=wxEmptyString) const override
SCH_SCREEN * GetScreen() const
Definition sch_sheet.h:187
static int ComparePageNum(const wxString &aPageNumberA, const wxString &aPageNumberB)
Compare page numbers of schematic sheets.
void setPageNumber(const KIID_PATH &aInstance, const wxString &aPageNumber)
Set the page number for the sheet instance aInstance.
bool IsVirtualRootSheet() const
bool GetDNP(const SCH_SHEET_PATH *aInstance=nullptr, const wxString &aVariantName=wxEmptyString) const override
Set or clear the 'Do Not Populate' flags.
bool GetExcludedFromBoard(const SCH_SHEET_PATH *aInstance=nullptr, const wxString &aVariantName=wxEmptyString) const override
Definition sch_sheet.h:519
PIN_MAP_INSTANCE_OVERRIDE m_PinMapOverride
Per-instance pin-to-pad map override for this variant (issue #2282).
std::optional< LIB_ID > m_SymbolOverride
Alternate library symbol to substitute for the base symbol in this variant, if any.
void InitializeAttributes(const SCH_SYMBOL &aSymbol)
bool HasDifferentials(const SCH_SYMBOL &aSymbol) const
Return true if the variant carries any differential against the base symbol values,...
Schematic symbol object.
Definition sch_symbol.h:76
void UpdatePrefix()
Set the prefix based on the current reference designator.
bool GetExcludedFromSim(const SCH_SHEET_PATH *aInstance=nullptr, const wxString &aVariantName=wxEmptyString) const override
const std::vector< SCH_SYMBOL_INSTANCE > & GetInstances() const
Definition sch_symbol.h:136
void RemoveInstance(const SCH_SHEET_PATH &aInstancePath)
PIN_MAP_INSTANCE_OVERRIDE GetPinMapOverride(const SCH_SHEET_PATH *aInstance=nullptr, const wxString &aVariantName=wxEmptyString) const
wxString GetBaseRef(const SCH_SHEET_PATH *aSheet, bool aIncludeUnit=false) const
bool GetExcludedFromBOM(const SCH_SHEET_PATH *aInstance=nullptr, const wxString &aVariantName=wxEmptyString) const override
void SetRefLacksSuffix(const KIID_PATH &aPath, bool aLacksSuffix=true)
Flag the reference loaded for aPath as saved without its sheet suffix.
void SetRef(const SCH_SHEET_PATH *aSheet, const wxString &aReference)
Set the base reference for the given sheet path for this symbol.
void SetFootprintFieldText(const wxString &aFootprint)
void AddHierarchicalReference(const KIID_PATH &aPath, const wxString &aRef, int aUnit)
Add a full hierarchical reference to this symbol.
const LIB_ID & GetLibId() const override
Definition sch_symbol.h:166
void SetValueFieldText(const wxString &aValue, const SCH_SHEET_PATH *aInstance=nullptr, const wxString &aVariantName=wxEmptyString)
bool GetExcludedFromPosFiles(const SCH_SHEET_PATH *aInstance=nullptr, const wxString &aVariantName=wxEmptyString) const override
bool GetInstance(SCH_SYMBOL_INSTANCE &aInstance, const KIID_PATH &aSheetPath, bool aTestFromEnd=false) const
bool GetExcludedFromBoard(const SCH_SHEET_PATH *aInstance=nullptr, const wxString &aVariantName=wxEmptyString) const override
int GetUnitSelection(const SCH_SHEET_PATH *aSheet) const
Return the instance-specific unit selection for the given sheet path.
int GetUnitCount() const override
Return the number of units per package of the symbol.
void SetUnitSelection(const SCH_SHEET_PATH *aSheet, int aUnitSelection)
Set the selected unit of this symbol on one sheet.
std::unique_ptr< LIB_SYMBOL > & GetLibSymbolRef()
Definition sch_symbol.h:185
virtual bool GetDNP(const SCH_SHEET_PATH *aInstance=nullptr, const wxString &aVariantName=wxEmptyString) const override
Set or clear the 'Do Not Populate' flag.
const wxString GetRef(const SCH_SHEET_PATH *aSheet, bool aIncludeUnit=false) const override
bool IsPower() const override
SCH_FIELD * GetField(FIELD_T aFieldType)
Return a mandatory field in this symbol.
bool m_ExcludedFromBOM
std::map< wxString, wxString > m_Fields
bool m_ExcludedFromPosFiles
bool m_ExcludedFromSim
bool m_ExcludedFromBoard
@ FOR_GUI
Definition common.h:93
static bool empty(const wxTextEntryBase *aCtrl)
#define _(s)
@ NO_RECURSE
Definition eda_item.h:52
const wxChar *const tracePathsAndFiles
Flag to enable path and file name debug output.
const wxChar *const traceSchSheetPaths
Flag to enable debug output of schematic symbol sheet path manipulation code.
static constexpr void hash_combine(std::size_t &seed)
This is a dummy function to take the final case of hash_combine below.
Definition hash.h:28
std::string source
KIID niluuid(0)
STL namespace.
Collection of utility functions for component reference designators (refdes)
@ AUTOPLACE_AUTO
Definition sch_item.h:75
static std::optional< wxString > claimPackageUnit(PACKAGE_UNITS &aPackages, const std::set< wxString > &aSuffixes, const wxString &aStored, const wxString &aLibId, int aUnit, const wxString &aOwnSuffix)
std::map< std::tuple< wxString, wxString, wxString >, std::set< int > > PACKAGE_UNITS
static bool stripRefSuffix(wxString &aRef, const wxString &aSuffix)
static bool matchesSymbolFilter(SCH_SYMBOL *aSymbol, const SCH_SHEET_PATH &aSheetPath, SYMBOL_FILTER aSymbolFilter)
Definition of the SCH_SHEET_PATH and SCH_SHEET_LIST classes for Eeschema.
std::map< wxString, SCH_REFERENCE_LIST > SCH_MULTI_UNIT_REFERENCE_MAP
Container to map reference designators for multi-unit parts.
SYMBOL_FILTER
@ SYMBOL_FILTER_ALL
@ SYMBOL_FILTER_POWER
wxString EscapeString(const wxString &aSource, ESCAPE_CONTEXT aContext)
The Escape/Unescape routines use HTML-entity-reference-style encoding to handle characters which are:...
@ CTX_NETNAME
A simple container for sheet instance information.
A simple container for schematic symbol instance information.
std::map< wxString, SCH_SYMBOL_VARIANT > m_Variants
A list of symbol variants.
bool m_RefLacksSuffix
Set at load when the saved reference lacked its sheet suffix, cleared when the reference changes.
size_t operator()(const SCH_SHEET_PATH &path) const
@ INTERSHEET_REFS
Global label cross-reference page numbers.
@ REFERENCE
Field Reference of part, i.e. "IC21".
std::string path
VECTOR2I end
wxLogTrace helper definitions.
@ SCH_SYMBOL_T
Definition typeinfo.h:168
@ SCH_SHAPE_T
Definition typeinfo.h:145
@ NOT_USED
the 3d code uses this value
Definition typeinfo.h:71
@ SCH_GLOBAL_LABEL_T
Definition typeinfo.h:164
VECTOR2< int32_t > VECTOR2I
Definition vector2d.h:708