KiCad PCB EDA Suite
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages Concepts
schematic.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 The KiCad Developers, see AUTHORS.txt for contributors.
5 *
6 * This program is free software: you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the
8 * Free Software Foundation, either version 3 of the License, or (at your
9 * option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License along
17 * with this program. If not, see <http://www.gnu.org/licenses/>.
18 */
19
20#include <bus_alias.h>
21#include <commit.h>
22#include <connection_graph.h>
23#include <core/ignore.h>
24#include <core/kicad_algo.h>
25#include <sch_collectors.h>
26#include <erc/erc_settings.h>
27#include <font/outline_font.h>
29#include <project.h>
32#include <schematic.h>
33#include <sch_junction.h>
34#include <sch_label.h>
35#include <sch_line.h>
36#include <sch_marker.h>
37#include <sch_screen.h>
38#include <sim/spice_settings.h>
39#include <sim/spice_value.h>
40
41#include <wx/log.h>
42
44
46 EDA_ITEM( nullptr, SCHEMATIC_T ),
47 m_project( nullptr ),
48 m_rootSheet( nullptr )
49{
53
54 SetProject( aPrj );
55
57 [&]( INSPECTABLE* aItem, PROPERTY_BASE* aProperty, COMMIT* aCommit )
58 {
59 // Special case: propagate value, footprint, and datasheet fields to other units
60 // of a given symbol if they aren't in the selection
61
62 SCH_FIELD* field = dynamic_cast<SCH_FIELD*>( aItem );
63
64 if( !field || !IsValid() )
65 return;
66
67 SCH_SYMBOL* symbol = dynamic_cast<SCH_SYMBOL*>( field->GetParent() );
68
69 if( !symbol || aProperty->Name() != _HKI( "Text" ) )
70 return;
71
72 // TODO(JE) This will need to get smarter to enable API access
73 SCH_SHEET_PATH sheetPath = CurrentSheet();
74
75 wxString newValue = aItem->Get<wxString>( aProperty );
76
77 if( field->GetId() == FIELD_T::REFERENCE )
78 {
79 symbol->SetRef( &sheetPath, newValue );
80
81 // The user might want to change all the units to the new ref. Or they
82 // might not. Since we have no way of knowing, we default to the most
83 // concrete action (change only the selected reference).
84 return;
85 }
86
87 wxString ref = symbol->GetRef( &sheetPath );
88 int unit = symbol->GetUnit();
89 LIB_ID libId = symbol->GetLibId();
90
91 for( SCH_SHEET_PATH& sheet : Hierarchy() )
92 {
93 std::vector<SCH_SYMBOL*> otherUnits;
94
95 CollectOtherUnits( ref, unit, libId, sheet, &otherUnits );
96
97 for( SCH_SYMBOL* otherUnit : otherUnits )
98 {
99 switch( field->GetId() )
100 {
101 case FIELD_T::VALUE:
102 case FIELD_T::FOOTPRINT:
103 case FIELD_T::DATASHEET:
104 {
105 if( aCommit )
106 aCommit->Modify( otherUnit, sheet.LastScreen() );
107
108 otherUnit->GetField( field->GetId() )->SetText( newValue );
109 break;
110 }
111
112 default:
113 break;
114 }
115 }
116 }
117 } );
118}
119
120
122{
124
125 delete m_currentSheet;
126 delete m_connectionGraph;
127
128 m_IsSchematicExists = false;
129}
130
131
133{
134 if( m_project )
135 {
137
138 // d'tor will save settings to file
139 delete project.m_ErcSettings;
140 project.m_ErcSettings = nullptr;
141
142 // d'tor will save settings to file
143 delete project.m_SchematicSettings;
144 project.m_SchematicSettings = nullptr;
145
146 m_project = nullptr; // clear the project, so we don't do this again when setting a new one
147 }
148
149 delete m_rootSheet;
150
151 m_rootSheet = nullptr;
152
155}
156
157
159{
160 if( m_project )
161 {
163
164 // d'tor will save settings to file
165 delete project.m_ErcSettings;
166 project.m_ErcSettings = nullptr;
167
168 // d'tor will save settings to file
169 delete project.m_SchematicSettings;
170 project.m_SchematicSettings = nullptr;
171 }
172
173 m_project = aPrj;
174
175 if( m_project )
176 {
178 project.m_ErcSettings = new ERC_SETTINGS( &project, "erc" );
179 project.m_SchematicSettings = new SCHEMATIC_SETTINGS( &project, "schematic" );
180
181 project.m_SchematicSettings->LoadFromFile();
182 project.m_SchematicSettings->m_NgspiceSettings->LoadFromFile();
183 project.m_ErcSettings->LoadFromFile();
184 }
185}
186
187
188void SCHEMATIC::SetRoot( SCH_SHEET* aRootSheet )
189{
190 wxCHECK_RET( aRootSheet, wxS( "Call to SetRoot with null SCH_SHEET!" ) );
191
192 m_rootSheet = aRootSheet;
193
196
199}
200
201
203{
204 return IsValid() ? m_rootSheet->GetScreen() : nullptr;
205}
206
207
209{
210 wxCHECK( !m_hierarchy.empty(), m_hierarchy );
211
212 return m_hierarchy;
213}
214
215
217{
219}
220
221
222void SCHEMATIC::GetContextualTextVars( wxArrayString* aVars ) const
223{
224 auto add =
225 [&]( const wxString& aVar )
226 {
227 if( !alg::contains( *aVars, aVar ) )
228 aVars->push_back( aVar );
229 };
230
231 add( wxT( "#" ) );
232 add( wxT( "##" ) );
233 add( wxT( "SHEETPATH" ) );
234 add( wxT( "SHEETNAME" ) );
235 add( wxT( "FILENAME" ) );
236 add( wxT( "FILEPATH" ) );
237 add( wxT( "PROJECTNAME" ) );
238
239 if( !CurrentSheet().empty() )
241
242 for( std::pair<wxString, wxString> entry : Prj().GetTextVars() )
243 add( entry.first );
244}
245
246
247bool SCHEMATIC::ResolveTextVar( const SCH_SHEET_PATH* aSheetPath, wxString* token,
248 int aDepth ) const
249{
250 wxCHECK( aSheetPath, false );
251
252 if( token->IsSameAs( wxT( "#" ) ) )
253 {
254 *token = aSheetPath->GetPageNumber();
255 return true;
256 }
257 else if( token->IsSameAs( wxT( "##" ) ) )
258 {
259 *token = wxString::Format( "%i", Root().CountSheets() );
260 return true;
261 }
262 else if( token->IsSameAs( wxT( "SHEETPATH" ) ) )
263 {
264 *token = aSheetPath->PathHumanReadable();
265 return true;
266 }
267 else if( token->IsSameAs( wxT( "SHEETNAME" ) ) )
268 {
269 *token = aSheetPath->Last()->GetName();
270 return true;
271 }
272 else if( token->IsSameAs( wxT( "FILENAME" ) ) )
273 {
274 wxFileName fn( GetFileName() );
275 *token = fn.GetFullName();
276 return true;
277 }
278 else if( token->IsSameAs( wxT( "FILEPATH" ) ) )
279 {
280 wxFileName fn( GetFileName() );
281 *token = fn.GetFullPath();
282 return true;
283 }
284 else if( token->IsSameAs( wxT( "PROJECTNAME" ) ) )
285 {
286 *token = Prj().GetProjectName();
287 return true;
288 }
289
290 if( aSheetPath->LastScreen()->GetTitleBlock().TextVarResolver( token, m_project ) )
291 return true;
292
293 if( Prj().TextVarResolver( token ) )
294 return true;
295
296 return false;
297}
298
299
301{
302 return IsValid() ? m_rootSheet->GetScreen()->GetFileName() : wxString( wxEmptyString );
303}
304
305
307{
308 wxASSERT( m_project );
310}
311
312
314{
315 wxASSERT( m_project );
317}
318
319
320std::vector<SCH_MARKER*> SCHEMATIC::ResolveERCExclusions()
321{
322 SCH_SHEET_LIST sheetList = Hierarchy();
323 ERC_SETTINGS& settings = ErcSettings();
324
325 // Migrate legacy marker exclusions to new format to ensure exclusion matching functions across
326 // file versions. Silently drops any legacy exclusions which can not be mapped to the new format
327 // without risking an incorrect exclusion - this is preferable to silently dropping
328 // new ERC errors / warnings due to an incorrect match between a legacy and new
329 // marker serialization format
330 std::set<wxString> migratedExclusions;
331
332 for( auto it = settings.m_ErcExclusions.begin(); it != settings.m_ErcExclusions.end(); )
333 {
334 SCH_MARKER* testMarker = SCH_MARKER::DeserializeFromString( sheetList, *it );
335
336 if( !testMarker )
337 {
338 it = settings.m_ErcExclusions.erase( it );
339 continue;
340 }
341
342 if( testMarker->IsLegacyMarker() )
343 {
344 const wxString settingsKey = testMarker->GetRCItem()->GetSettingsKey();
345
346 if( settingsKey != wxT( "pin_to_pin" )
347 && settingsKey != wxT( "hier_label_mismatch" )
348 && settingsKey != wxT( "different_unit_net" ) )
349 {
350 migratedExclusions.insert( testMarker->SerializeToString() );
351 }
352
353 it = settings.m_ErcExclusions.erase( it );
354 }
355 else
356 {
357 ++it;
358 }
359
360 delete testMarker;
361 }
362
363 settings.m_ErcExclusions.insert( migratedExclusions.begin(), migratedExclusions.end() );
364
365 // End of legacy exclusion removal / migrations
366
367 for( const SCH_SHEET_PATH& sheet : sheetList )
368 {
369 for( SCH_ITEM* item : sheet.LastScreen()->Items().OfType( SCH_MARKER_T ) )
370 {
371 SCH_MARKER* marker = static_cast<SCH_MARKER*>( item );
372 wxString serialized = marker->SerializeToString();
373 std::set<wxString>::iterator it = settings.m_ErcExclusions.find( serialized );
374
375 if( it != settings.m_ErcExclusions.end() )
376 {
377 marker->SetExcluded( true, settings.m_ErcExclusionComments[serialized] );
378 settings.m_ErcExclusions.erase( it );
379 }
380 }
381 }
382
383 std::vector<SCH_MARKER*> newMarkers;
384
385 for( const wxString& serialized : settings.m_ErcExclusions )
386 {
387 SCH_MARKER* marker = SCH_MARKER::DeserializeFromString( sheetList, serialized );
388
389 if( marker )
390 {
391 marker->SetExcluded( true, settings.m_ErcExclusionComments[serialized] );
392 newMarkers.push_back( marker );
393 }
394 }
395
396 settings.m_ErcExclusions.clear();
397
398 return newMarkers;
399}
400
401
402std::shared_ptr<BUS_ALIAS> SCHEMATIC::GetBusAlias( const wxString& aLabel ) const
403{
404 for( const SCH_SHEET_PATH& sheet : Hierarchy() )
405 {
406 for( const std::shared_ptr<BUS_ALIAS>& alias : sheet.LastScreen()->GetBusAliases() )
407 {
408 if( alias->GetName() == aLabel )
409 return alias;
410 }
411 }
412
413 return nullptr;
414}
415
416
418{
419 std::set<wxString> names;
420
421 for( const auto& [ key, subgraphList ] : m_connectionGraph->GetNetMap() )
422 {
423 CONNECTION_SUBGRAPH* firstSubgraph = subgraphList[0];
424
425 if( !firstSubgraph->GetDriverConnection()->IsBus()
427 {
428 names.insert( key.Name );
429 }
430 }
431
432 return names;
433}
434
435
436bool SCHEMATIC::ResolveCrossReference( wxString* token, int aDepth ) const
437{
438 wxString remainder;
439 wxString ref = token->BeforeFirst( ':', &remainder );
440 KIID_PATH path( ref );
441 KIID uuid = path.back();
442 SCH_SHEET_PATH sheetPath;
443 SCH_ITEM* refItem = GetItem( KIID( uuid ), &sheetPath );
444
445 if( path.size() > 1 )
446 {
447 path.pop_back();
448 sheetPath = Hierarchy().GetSheetPathByKIIDPath( path ).value_or( sheetPath );
449 }
450
451 if( refItem && refItem->Type() == SCH_SYMBOL_T )
452 {
453 SCH_SYMBOL* refSymbol = static_cast<SCH_SYMBOL*>( refItem );
454
455 if( refSymbol->ResolveTextVar( &sheetPath, &remainder, aDepth + 1 ) )
456 *token = remainder;
457 else
458 *token = refSymbol->GetRef( &sheetPath, true ) + wxS( ":" ) + remainder;
459
460 return true; // Cross-reference is resolved whether or not the actual textvar was
461 }
462 else if( refItem && refItem->Type() == SCH_SHEET_T )
463 {
464 SCH_SHEET* refSheet = static_cast<SCH_SHEET*>( refItem );
465
466 sheetPath.push_back( refSheet );
467
468 if( refSheet->ResolveTextVar( &sheetPath, &remainder, aDepth + 1 ) )
469 *token = remainder;
470
471 return true; // Cross-reference is resolved whether or not the actual textvar was
472 }
473
474 return false;
475}
476
477
478std::map<int, wxString> SCHEMATIC::GetVirtualPageToSheetNamesMap() const
479{
480 std::map<int, wxString> namesMap;
481
482 for( const SCH_SHEET_PATH& sheet : Hierarchy() )
483 {
484 if( sheet.size() == 1 )
485 namesMap[sheet.GetVirtualPageNumber()] = _( "<root sheet>" );
486 else
487 namesMap[sheet.GetVirtualPageNumber()] = sheet.Last()->GetName();
488 }
489
490 return namesMap;
491}
492
493
494std::map<int, wxString> SCHEMATIC::GetVirtualPageToSheetPagesMap() const
495{
496 std::map<int, wxString> pagesMap;
497
498 for( const SCH_SHEET_PATH& sheet : Hierarchy() )
499 pagesMap[sheet.GetVirtualPageNumber()] = sheet.GetPageNumber();
500
501 return pagesMap;
502}
503
504
505wxString SCHEMATIC::ConvertRefsToKIIDs( const wxString& aSource ) const
506{
507 wxString newbuf;
508 size_t sourceLen = aSource.length();
509
510 for( size_t i = 0; i < sourceLen; ++i )
511 {
512 if( aSource[i] == '$' && i + 1 < sourceLen && aSource[i+1] == '{' )
513 {
514 wxString token;
515 bool isCrossRef = false;
516 int nesting = 0;
517
518 for( i = i + 2; i < sourceLen; ++i )
519 {
520 if( aSource[i] == '{'
521 && ( aSource[i-1] == '_' || aSource[i-1] == '^' || aSource[i-1] == '~' ) )
522 {
523 nesting++;
524 }
525
526 if( aSource[i] == '}' )
527 {
528 nesting--;
529
530 if( nesting < 0 )
531 break;
532 }
533
534 if( aSource[i] == ':' )
535 isCrossRef = true;
536
537 token.append( aSource[i] );
538 }
539
540 if( isCrossRef )
541 {
542 wxString remainder;
543 wxString ref = token.BeforeFirst( ':', &remainder );
544 SCH_REFERENCE_LIST references;
545
546 Hierarchy().GetSymbols( references );
547
548 for( size_t jj = 0; jj < references.GetCount(); jj++ )
549 {
550 SCH_SYMBOL* refSymbol = references[ jj ].GetSymbol();
551
552 if( ref == refSymbol->GetRef( &references[ jj ].GetSheetPath(), true ) )
553 {
554 KIID_PATH path = references[ jj ].GetSheetPath().Path();
555 path.push_back( refSymbol->m_Uuid );
556
557 token = path.AsString() + wxS( ":" ) + remainder;
558 break;
559 }
560 }
561 }
562
563 newbuf.append( wxS( "${" ) + token + wxS( "}" ) );
564 }
565 else
566 {
567 newbuf.append( aSource[i] );
568 }
569 }
570
571 return newbuf;
572}
573
574
575wxString SCHEMATIC::ConvertKIIDsToRefs( const wxString& aSource ) const
576{
577 wxString newbuf;
578 size_t sourceLen = aSource.length();
579
580 for( size_t i = 0; i < sourceLen; ++i )
581 {
582 if( aSource[i] == '$' && i + 1 < sourceLen && aSource[i+1] == '{' )
583 {
584 wxString token;
585 bool isCrossRef = false;
586
587 for( i = i + 2; i < sourceLen; ++i )
588 {
589 if( aSource[i] == '}' )
590 break;
591
592 if( aSource[i] == ':' )
593 isCrossRef = true;
594
595 token.append( aSource[i] );
596 }
597
598 if( isCrossRef )
599 {
600 wxString remainder;
601 wxString ref = token.BeforeFirst( ':', &remainder );
602 KIID_PATH path( ref );
603 KIID uuid = path.back();
604 SCH_SHEET_PATH sheetPath;
605 SCH_ITEM* refItem = GetItem( uuid, &sheetPath );
606
607 if( path.size() > 1 )
608 {
609 path.pop_back();
610 sheetPath = Hierarchy().GetSheetPathByKIIDPath( path ).value_or( sheetPath );
611 }
612
613 if( refItem && refItem->Type() == SCH_SYMBOL_T )
614 {
615 SCH_SYMBOL* refSymbol = static_cast<SCH_SYMBOL*>( refItem );
616 token = refSymbol->GetRef( &sheetPath, true ) + wxS( ":" ) + remainder;
617 }
618 }
619
620 newbuf.append( wxS( "${" ) + token + wxS( "}" ) );
621 }
622 else
623 {
624 newbuf.append( aSource[i] );
625 }
626 }
627
628 return newbuf;
629}
630
631
633{
634 SCH_SCREENS screens( m_rootSheet );
635
637}
638
639
641{
642 // Filename is rootSheetName-sheetName-...-sheetName
643 // Note that we need to fetch the rootSheetName out of its filename, as the root SCH_SHEET's
644 // name is just a timestamp.
645
646 wxFileName rootFn( CurrentSheet().at( 0 )->GetFileName() );
647 wxString filename = rootFn.GetName();
648
649 for( unsigned i = 1; i < CurrentSheet().size(); i++ )
650 filename += wxT( "-" ) + CurrentSheet().at( i )->GetName();
651
652 return filename;
653}
654
655
657{
658 SCH_SCREEN* screen;
659 SCH_SCREENS s_list( Root() );
660
661 // Set the sheet count, and the sheet number (1 for root sheet)
662 int sheet_count = Root().CountSheets();
663 int sheet_number = 1;
664 const KIID_PATH& current_sheetpath = CurrentSheet().Path();
665
666 // @todo Remove all pseudo page number system is left over from prior to real page number
667 // implementation.
668 for( const SCH_SHEET_PATH& sheet : Hierarchy() )
669 {
670 if( sheet.Path() == current_sheetpath ) // Current sheet path found
671 break;
672
673 sheet_number++; // Not found, increment before this current path
674 }
675
676 for( screen = s_list.GetFirst(); screen != nullptr; screen = s_list.GetNext() )
677 screen->SetPageCount( sheet_count );
678
679 CurrentSheet().SetVirtualPageNumber( sheet_number );
680 CurrentSheet().LastScreen()->SetVirtualPageNumber( sheet_number );
681 CurrentSheet().LastScreen()->SetPageNumber( CurrentSheet().GetPageNumber() );
682}
683
684
686 const std::function<void( SCH_GLOBALLABEL* )>& aItemCallback )
687{
688 std::map<wxString, std::set<int>>& pageRefsMap = GetPageRefsMap();
689
690 pageRefsMap.clear();
691
692 for( const SCH_SHEET_PATH& sheet : Hierarchy() )
693 {
694 for( SCH_ITEM* item : sheet.LastScreen()->Items().OfType( SCH_GLOBAL_LABEL_T ) )
695 {
696 SCH_GLOBALLABEL* global = static_cast<SCH_GLOBALLABEL*>( item );
697 wxString resolvedLabel = global->GetShownText( &sheet, false );
698
699 pageRefsMap[ resolvedLabel ].insert( sheet.GetVirtualPageNumber() );
700 }
701 }
702
703 bool show = Settings().m_IntersheetRefsShow;
704
705 // Refresh all visible global labels. Note that we have to collect them first as the
706 // SCH_SCREEN::Update() call is going to invalidate the RTree iterator.
707
708 std::vector<SCH_GLOBALLABEL*> currentSheetGlobalLabels;
709
710 for( EDA_ITEM* item : CurrentSheet().LastScreen()->Items().OfType( SCH_GLOBAL_LABEL_T ) )
711 currentSheetGlobalLabels.push_back( static_cast<SCH_GLOBALLABEL*>( item ) );
712
713 for( SCH_GLOBALLABEL* globalLabel : currentSheetGlobalLabels )
714 {
715 std::vector<SCH_FIELD>& fields = globalLabel->GetFields();
716
717 fields[0].SetVisible( show );
718
719 if( show )
720 {
721 if( fields.size() == 1 && fields[0].GetTextPos() == globalLabel->GetPosition() )
722 globalLabel->AutoplaceFields( CurrentSheet().LastScreen(), AUTOPLACE_AUTO );
723
724 CurrentSheet().LastScreen()->Update( globalLabel );
725 aItemCallback( globalLabel );
726 }
727 }
728}
729
730
731wxString SCHEMATIC::GetOperatingPoint( const wxString& aNetName, int aPrecision,
732 const wxString& aRange )
733{
734 wxString spiceNetName( aNetName.Lower() );
736
737 if( spiceNetName == wxS( "gnd" ) || spiceNetName == wxS( "0" ) )
738 return wxEmptyString;
739
740 auto it = m_operatingPoints.find( spiceNetName );
741
742 if( it != m_operatingPoints.end() )
743 return SPICE_VALUE( it->second ).ToString( { aPrecision, aRange } );
744 else if( m_operatingPoints.empty() )
745 return wxS( "--" );
746 else
747 return wxS( "?" );
748}
749
750
752{
753 SCH_SCREENS screens( Root() );
754
755 for( SCH_SCREEN* screen = screens.GetFirst(); screen; screen = screens.GetNext() )
756 {
757 std::deque<EDA_ITEM*> allItems;
758
759 for( auto item : screen->Items() )
760 allItems.push_back( item );
761
762 // Add missing junctions and breakup wires as needed
763 for( const VECTOR2I& point : screen->GetNeededJunctions( allItems ) )
764 {
765 SCH_JUNCTION* junction = new SCH_JUNCTION( point );
766 screen->Append( junction );
767
768 // Breakup wires
769 for( SCH_LINE* wire : screen->GetBusesAndWires( point, true ) )
770 {
771 SCH_LINE* newSegment = wire->BreakAt( point );
772 screen->Append( newSegment );
773 }
774 }
775 }
776}
777
778
779void SCHEMATIC::OnItemsAdded( std::vector<SCH_ITEM*>& aNewItems )
780{
782}
783
784
785void SCHEMATIC::OnItemsRemoved( std::vector<SCH_ITEM*>& aRemovedItems )
786{
788}
789
790
791void SCHEMATIC::OnItemsChanged( std::vector<SCH_ITEM*>& aItems )
792{
794}
795
796
798{
800}
801
802
804{
805 if( !alg::contains( m_listeners, aListener ) )
806 m_listeners.push_back( aListener );
807}
808
809
811{
812 auto i = std::find( m_listeners.begin(), m_listeners.end(), aListener );
813
814 if( i != m_listeners.end() )
815 {
816 std::iter_swap( i, m_listeners.end() - 1 );
817 m_listeners.pop_back();
818 }
819}
820
821
823{
824 m_listeners.clear();
825}
826
827
829{
830 // Use a sorted sheetList to reduce file churn
831 SCH_SHEET_LIST sheetList = Hierarchy();
832 ERC_SETTINGS& ercSettings = ErcSettings();
833
834 ercSettings.m_ErcExclusions.clear();
835 ercSettings.m_ErcExclusionComments.clear();
836
837 for( unsigned i = 0; i < sheetList.size(); i++ )
838 {
839 for( SCH_ITEM* item : sheetList[i].LastScreen()->Items().OfType( SCH_MARKER_T ) )
840 {
841 SCH_MARKER* marker = static_cast<SCH_MARKER*>( item );
842
843 if( marker->IsExcluded() )
844 {
845 wxString serialized = marker->SerializeToString();
846 ercSettings.m_ErcExclusions.insert( serialized );
847 ercSettings.m_ErcExclusionComments[ serialized ] = marker->GetComment();
848 }
849 }
850 }
851}
852
853
855{
856 SCH_SHEET_LIST sheetList = Hierarchy();
857
858 for( SCH_MARKER* marker : ResolveERCExclusions() )
859 {
860 SCH_SHEET_PATH errorPath;
861 ignore_unused( sheetList.GetItem( marker->GetRCItem()->GetMainItemID(), &errorPath ) );
862
863 if( errorPath.LastScreen() )
864 errorPath.LastScreen()->Append( marker );
865 else
866 RootScreen()->Append( marker );
867 }
868}
869
870
872{
873 return static_cast<EMBEDDED_FILES*>( this );
874}
875
876
878{
879 return static_cast<const EMBEDDED_FILES*>( this );
880}
881
882
883std::set<KIFONT::OUTLINE_FONT*> SCHEMATIC::GetFonts() const
884{
885 std::set<KIFONT::OUTLINE_FONT*> fonts;
886
887 SCH_SHEET_LIST sheetList = Hierarchy();
888
889 for( const SCH_SHEET_PATH& sheet : sheetList )
890 {
891 for( SCH_ITEM* item : sheet.LastScreen()->Items() )
892 {
893 if( EDA_TEXT* text = dynamic_cast<EDA_TEXT*>( item ) )
894 {
895 KIFONT::FONT* font = text->GetFont();
896
897 if( !font || font->IsStroke() )
898 continue;
899
900 using EMBEDDING_PERMISSION = KIFONT::OUTLINE_FONT::EMBEDDING_PERMISSION;
901 auto* outline = static_cast<KIFONT::OUTLINE_FONT*>( font );
902
903 if( outline->GetEmbeddingPermission() == EMBEDDING_PERMISSION::EDITABLE
904 || outline->GetEmbeddingPermission() == EMBEDDING_PERMISSION::INSTALLABLE )
905 {
906 fonts.insert( outline );
907 }
908 }
909 }
910 }
911
912 return fonts;
913}
914
915
917{
918 std::set<KIFONT::OUTLINE_FONT*> fonts = GetFonts();
919
920 for( KIFONT::OUTLINE_FONT* font : fonts )
921 {
922 auto file = GetEmbeddedFiles()->AddFile( font->GetFileName(), false );
923
924 if( !file )
925 {
926 wxLogTrace( "EMBED", "Failed to add font file: %s", font->GetFileName() );
927 continue;
928 }
929
931 }
932}
933
934
935std::set<const SCH_SCREEN*> SCHEMATIC::GetSchematicsSharedByMultipleProjects() const
936{
937 std::set<const SCH_SCREEN*> retv;
938
939 wxCHECK( m_rootSheet, retv );
940
941 SCH_SHEET_LIST hierarchy( m_rootSheet );
942 SCH_SCREENS screens( m_rootSheet );
943
944 for( const SCH_SCREEN* screen = screens.GetFirst(); screen; screen = screens.GetNext() )
945 {
946 for( const SCH_ITEM* item : screen->Items().OfType( SCH_SYMBOL_T ) )
947 {
948 const SCH_SYMBOL* symbol = static_cast<const SCH_SYMBOL*>( item );
949
950 const std::vector<SCH_SYMBOL_INSTANCE> symbolInstances = symbol->GetInstances();
951
952 for( const SCH_SYMBOL_INSTANCE& instance : symbolInstances )
953 {
954 if( !hierarchy.HasPath( instance.m_Path ) )
955 {
956 retv.insert( screen );
957 break;
958 }
959 }
960
961 if( retv.count( screen ) )
962 break;
963 }
964 }
965
966 return retv;
967}
968
969
971{
972 wxCHECK( m_rootSheet, false );
973
974 SCH_SCREENS screens( m_rootSheet );
975
976 for( const SCH_SCREEN* screen = screens.GetFirst(); screen; screen = screens.GetNext() )
977 {
978 wxCHECK2( screen, continue );
979
980 if( screen->GetRefCount() > 1 )
981 return true;
982 }
983
984 return false;
985}
void SetPageCount(int aPageCount)
Definition: base_screen.cpp:63
void SetPageNumber(const wxString &aPageNumber)
Definition: base_screen.h:79
void SetVirtualPageNumber(int aPageNumber)
Definition: base_screen.h:76
Represent a set of changes (additions, deletions or modifications) of a data model (e....
Definition: commit.h:74
COMMIT & Modify(EDA_ITEM *aItem, BASE_SCREEN *aScreen=nullptr)
Modify a given item in the model.
Definition: commit.h:108
Calculate the connectivity of a schematic and generates netlists.
const NET_MAP & GetNetMap() const
A subgraph is a set of items that are electrically connected on a single sheet.
static PRIORITY GetDriverPriority(SCH_ITEM *aDriver)
Return the priority (higher is more important) of a candidate driver.
const SCH_CONNECTION * GetDriverConnection() const
A base class for most all the KiCad significant classes used in schematics and boards.
Definition: eda_item.h:95
const KIID m_Uuid
Definition: eda_item.h:494
KICAD_T Type() const
Returns the type of object.
Definition: eda_item.h:107
EDA_ITEM * GetParent() const
Definition: eda_item.h:109
A mix-in class (via multiple inheritance) that handles texts such as labels, parts,...
Definition: eda_text.h:80
EMBEDDED_FILE * AddFile(const wxFileName &aName, bool aOverwrite)
Load a file from disk and adds it to the collection.
Container for ERC settings.
Definition: erc_settings.h:135
std::map< wxString, wxString > m_ErcExclusionComments
Definition: erc_settings.h:214
std::set< wxString > m_ErcExclusions
Definition: erc_settings.h:213
Class that other classes need to inherit from, in order to be inspectable.
Definition: inspectable.h:37
wxAny Get(PROPERTY_BASE *aProperty) const
Definition: inspectable.h:100
FONT is an abstract base class for both outline and stroke fonts.
Definition: font.h:131
virtual bool IsStroke() const
Definition: font.h:138
Class OUTLINE_FONT implements outline font drawing.
Definition: outline_font.h:53
Definition: kiid.h:49
A logical library item identifier and consists of various portions much like a URI.
Definition: lib_id.h:49
bool IsExcluded() const
Definition: marker_base.h:93
std::shared_ptr< RC_ITEM > GetRCItem() const
Definition: marker_base.h:107
void SetExcluded(bool aExcluded, const wxString &aComment=wxEmptyString)
Definition: marker_base.h:94
wxString GetComment() const
Definition: marker_base.h:100
static void ConvertToSpiceMarkup(wxString *aNetName)
Remove formatting wrappers and replace illegal spice net name characters with underscores.
The backing store for a PROJECT, in JSON format.
Definition: project_file.h:73
ERC_SETTINGS * m_ErcSettings
Eeschema params.
Definition: project_file.h:148
SCHEMATIC_SETTINGS * m_SchematicSettings
Definition: project_file.h:151
Container for project specific data.
Definition: project.h:64
virtual const wxString GetProjectName() const
Return the short name of the project.
Definition: project.cpp:158
virtual PROJECT_FILE & GetProjectFile() const
Definition: project.h:203
const wxString & Name() const
Definition: property.h:217
static PROPERTY_MANAGER & Instance()
Definition: property_mgr.h:87
void UnregisterListeners(TYPE_ID aType)
Definition: property_mgr.h:280
void RegisterListener(TYPE_ID aType, PROPERTY_LISTENER aListenerFunc)
Registers a listener for the given type.
Definition: property_mgr.h:275
virtual void OnSchItemsRemoved(SCHEMATIC &aSch, std::vector< SCH_ITEM * > &aSchItem)
Definition: schematic.h:53
virtual void OnSchItemsChanged(SCHEMATIC &aSch, std::vector< SCH_ITEM * > &aSchItem)
Definition: schematic.h:54
virtual void OnSchSheetChanged(SCHEMATIC &aSch)
Definition: schematic.h:58
virtual void OnSchItemsAdded(SCHEMATIC &aSch, std::vector< SCH_ITEM * > &aSchItem)
Definition: schematic.h:52
These are loaded from Eeschema settings but then overwritten by the project settings.
PROJECT & Prj() const
Return a reference to the project this schematic is part of.
Definition: schematic.h:84
void Reset()
Initialize this schematic to a blank one, unloading anything existing.
Definition: schematic.cpp:132
std::set< const SCH_SCREEN * > GetSchematicsSharedByMultipleProjects() const
Return a list of schematic files in the current project that contain instance data for multiple proje...
Definition: schematic.cpp:935
void SetLegacySymbolInstanceData()
Update the symbol value and footprint instance data for legacy designs.
Definition: schematic.cpp:632
void OnItemsAdded(std::vector< SCH_ITEM * > &aNewItems)
Must be used if Add() is used using a BULK_x ADD_MODE to generate a change event for listeners.
Definition: schematic.cpp:779
CONNECTION_GRAPH * m_connectionGraph
Hold and calculate connectivity information of this schematic.
Definition: schematic.h:385
SCH_SHEET_LIST m_hierarchy
Cache of the entire schematic hierarchy sorted by sheet page number.
Definition: schematic.h:407
void ResolveERCExclusionsPostUpdate()
Update markers to match recorded exclusions.
Definition: schematic.cpp:854
SCH_SHEET_LIST BuildSheetListSortedByPageNumbers() const
Definition: schematic.h:89
void RemoveListener(SCHEMATIC_LISTENER *aListener)
Remove the specified listener.
Definition: schematic.cpp:810
bool IsComplexHierarchy() const
Test if the schematic is a complex hierarchy.
Definition: schematic.cpp:970
void OnSchSheetChanged()
Notify the schematic and its listeners that the current sheet has been changed.
Definition: schematic.cpp:797
wxString GetFileName() const
Helper to retrieve the filename from the root sheet screen.
Definition: schematic.cpp:300
SCH_SHEET_PATH * m_currentSheet
The sheet path of the sheet currently being edited or displayed.
Definition: schematic.h:382
wxString GetOperatingPoint(const wxString &aNetName, int aPrecision, const wxString &aRange)
Definition: schematic.cpp:731
void OnItemsRemoved(std::vector< SCH_ITEM * > &aRemovedItems)
Must be used if Remove() is used using a BULK_x REMOVE_MODE to generate a change event for listeners.
Definition: schematic.cpp:785
virtual ~SCHEMATIC()
Definition: schematic.cpp:121
std::shared_ptr< BUS_ALIAS > GetBusAlias(const wxString &aLabel) const
Return a pointer to a bus alias object for the given label, or null if one doesn't exist.
Definition: schematic.cpp:402
std::vector< SCH_MARKER * > ResolveERCExclusions()
Definition: schematic.cpp:320
void EmbedFonts() override
Embed fonts in the schematic.
Definition: schematic.cpp:916
SCHEMATIC_SETTINGS & Settings() const
Definition: schematic.cpp:306
wxString ConvertKIIDsToRefs(const wxString &aSource) const
Definition: schematic.cpp:575
void RecordERCExclusions()
Scan existing markers and record data from any that are Excluded.
Definition: schematic.cpp:828
SCH_SHEET_LIST Hierarchy() const
Return the full schematic flattened hierarchical sheet list.
Definition: schematic.cpp:208
std::map< wxString, std::set< int > > & GetPageRefsMap()
Definition: schematic.h:194
void FixupJunctions()
Add junctions to this schematic where required.
Definition: schematic.cpp:751
std::set< KIFONT::OUTLINE_FONT * > GetFonts() const override
Get a set of fonts used in the schematic.
Definition: schematic.cpp:883
SCH_ITEM * GetItem(const KIID &aID, SCH_SHEET_PATH *aPathOut=nullptr) const
Definition: schematic.h:112
wxString ConvertRefsToKIIDs(const wxString &aSource) const
Definition: schematic.cpp:505
void SetRoot(SCH_SHEET *aRootSheet)
Initialize the schematic with a new root sheet.
Definition: schematic.cpp:188
void SetProject(PROJECT *aPrj)
Definition: schematic.cpp:158
void AddListener(SCHEMATIC_LISTENER *aListener)
Add a listener to the schematic to receive calls whenever something on the schematic has been modifie...
Definition: schematic.cpp:803
std::map< int, wxString > GetVirtualPageToSheetPagesMap() const
Definition: schematic.cpp:494
EMBEDDED_FILES * GetEmbeddedFiles() override
Definition: schematic.cpp:871
PROJECT * m_project
Definition: schematic.h:370
SCH_SCREEN * RootScreen() const
Helper to retrieve the screen of the root sheet.
Definition: schematic.cpp:202
SCHEMATIC(PROJECT *aPrj)
Definition: schematic.cpp:45
bool ResolveTextVar(const SCH_SHEET_PATH *aSheetPath, wxString *token, int aDepth) const
Definition: schematic.cpp:247
std::set< wxString > GetNetClassAssignmentCandidates()
Return the set of netname candidates for netclass assignment.
Definition: schematic.cpp:417
void RecomputeIntersheetRefs(const std::function< void(SCH_GLOBALLABEL *)> &aItemCallback)
Update the schematic's page reference map for all global labels, and refresh the labels so that they ...
Definition: schematic.cpp:685
void InvokeListeners(Func &&aFunc, Args &&... args)
Definition: schematic.h:364
bool IsValid() const
A simple test if the schematic is loaded, not a complete one.
Definition: schematic.h:133
static bool m_IsSchematicExists
True if a SCHEMATIC exists, false if not.
Definition: schematic.h:352
void RemoveAllListeners()
Remove all listeners.
Definition: schematic.cpp:822
void GetContextualTextVars(wxArrayString *aVars) const
Definition: schematic.cpp:222
SCH_SHEET & Root() const
Definition: schematic.h:117
std::map< int, wxString > GetVirtualPageToSheetNamesMap() const
Definition: schematic.cpp:478
wxString GetUniqueFilenameForCurrentSheet()
Get the unique file name for the current sheet.
Definition: schematic.cpp:640
void SetSheetNumberAndCount()
Set the m_ScreenNumber and m_NumberOfScreens members for screens.
Definition: schematic.cpp:656
std::vector< SCHEMATIC_LISTENER * > m_listeners
Currently installed listeners.
Definition: schematic.h:412
SCH_SHEET_PATH & CurrentSheet() const
Definition: schematic.h:148
bool ResolveCrossReference(wxString *token, int aDepth) const
Resolves text vars that refer to other items.
Definition: schematic.cpp:436
std::map< wxString, double > m_operatingPoints
Simulation operating points for text variable substitution.
Definition: schematic.h:402
ERC_SETTINGS & ErcSettings() const
Definition: schematic.cpp:313
void RefreshHierarchy()
Definition: schematic.cpp:216
void OnItemsChanged(std::vector< SCH_ITEM * > &aItems)
Notify the schematic and its listeners that an item on the schematic has been modified in some way.
Definition: schematic.cpp:791
SCH_SHEET * m_rootSheet
The top-level sheet in this schematic hierarchy (or potentially the only one)
Definition: schematic.h:373
bool IsBus() const
FIELD_T GetId() const
Definition: sch_field.h:124
Base class for any item which can be embedded within the SCHEMATIC container class,...
Definition: sch_item.h:168
int GetUnit() const
Definition: sch_item.h:240
wxString GetShownText(const SCH_SHEET_PATH *aPath, bool aAllowExtraText, int aDepth=0) const override
Definition: sch_label.cpp:825
Segment description base class to describe items which have 2 end points (track, wire,...
Definition: sch_line.h:41
SCH_LINE * BreakAt(const VECTOR2I &aPoint)
Break this segment into two at the specified point.
Definition: sch_line.cpp:540
static SCH_MARKER * DeserializeFromString(const SCH_SHEET_LIST &aSheetList, const wxString &data)
Definition: sch_marker.cpp:143
wxString SerializeToString() const
Definition: sch_marker.cpp:91
bool IsLegacyMarker() const
Determine if this marker is legacy (i.e.
Definition: sch_marker.h:121
Container to create a flattened list of symbols because in a complex hierarchy, a symbol can be used ...
size_t GetCount() const
Container class that holds multiple SCH_SCREEN objects in a hierarchy.
Definition: sch_screen.h:734
SCH_SCREEN * GetNext()
SCH_SCREEN * GetFirst()
void SetLegacySymbolInstanceData()
Update the symbol value and footprint instance data for legacy designs.
void Append(SCH_ITEM *aItem, bool aUpdateLibSymbol=true)
Definition: sch_screen.cpp:155
const wxString & GetFileName() const
Definition: sch_screen.h:147
const TITLE_BLOCK & GetTitleBlock() const
Definition: sch_screen.h:158
void Update(SCH_ITEM *aItem, bool aUpdateLibSymbol=true)
Update aItem's bounding box in the tree.
Definition: sch_screen.cpp:318
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 * GetItem(const KIID &aID, SCH_SHEET_PATH *aPathOut=nullptr) const
Fetch a SCH_ITEM by ID.
void GetSymbols(SCH_REFERENCE_LIST &aReferences, bool aIncludePowerSymbols=true, bool aForceIncludeOrphanSymbols=false) const
Add a SCH_REFERENCE object to aReferences for each symbol in the list of sheets.
bool HasPath(const KIID_PATH &aPath) const
Handle access to a stack of flattened SCH_SHEET objects by way of a path for creating a flattened sch...
wxString PathHumanReadable(bool aUseShortRootName=true, bool aStripTrailingSeparator=false) const
Return the sheet path in a human readable form made from the sheet names.
KIID_PATH Path() const
Get the sheet path as an KIID_PATH.
SCH_SCREEN * LastScreen()
wxString GetPageNumber() const
SCH_SHEET * at(size_t aIndex) const
Forwarded method from std::vector.
void SetVirtualPageNumber(int aPageNumber)
Set the sheet instance virtual page number.
SCH_SHEET * Last() const
Return a pointer to the last SCH_SHEET of the list.
void push_back(SCH_SHEET *aSheet)
Forwarded method from std::vector.
void clear()
Forwarded method from std::vector.
size_t size() const
Forwarded method from std::vector.
Sheet symbol placed in a schematic, and is the entry point for a sub schematic.
Definition: sch_sheet.h:47
wxString GetName() const
Definition: sch_sheet.h:113
int CountSheets() const
Count the number of sheets found in "this" sheet including all of the subsheets.
Definition: sch_sheet.cpp:830
SCH_SCREEN * GetScreen() const
Definition: sch_sheet.h:116
bool ResolveTextVar(const SCH_SHEET_PATH *aPath, wxString *token, int aDepth=0) const
Resolve any references to system tokens supported by the sheet.
Definition: sch_sheet.cpp:220
Schematic symbol object.
Definition: sch_symbol.h:75
const std::vector< SCH_SYMBOL_INSTANCE > & GetInstances() const
Definition: sch_symbol.h:134
void SetRef(const SCH_SHEET_PATH *aSheet, const wxString &aReference)
Set the reference for the given sheet path for this symbol.
Definition: sch_symbol.cpp:653
bool ResolveTextVar(const SCH_SHEET_PATH *aPath, wxString *token, int aDepth=0) const
Resolve any references to system tokens supported by the symbol.
const LIB_ID & GetLibId() const override
Definition: sch_symbol.h:164
const wxString GetRef(const SCH_SHEET_PATH *aSheet, bool aIncludeUnit=false) const override
Definition: sch_symbol.cpp:611
Helper class to recognize Spice formatted values.
Definition: spice_value.h:56
wxString ToString() const
Return string value as when converting double to string (e.g.
bool TextVarResolver(wxString *aToken, const PROJECT *aProject, int aFlags=0) const
static void GetContextualTextVars(wxArrayString *aVars)
Definition: title_block.cpp:75
wxString GetTextVars(const wxString &aSource)
Returns any variables unexpanded, e.g.
Definition: common.cpp:121
#define _HKI(x)
static bool empty(const wxTextEntryBase *aCtrl)
#define _(s)
void ignore_unused(const T &)
Definition: ignore.h:24
bool contains(const _Container &__container, _Value __value)
Returns true if the container contains the given value.
Definition: kicad_algo.h:100
#define TYPE_HASH(x)
Definition: property.h:71
void CollectOtherUnits(const wxString &aRef, int aUnit, const LIB_ID &aLibId, SCH_SHEET_PATH &aSheet, std::vector< SCH_SYMBOL * > *otherUnits)
@ AUTOPLACE_AUTO
Definition: sch_item.h:71
A simple container for schematic symbol instance information.
@ SCH_SYMBOL_T
Definition: typeinfo.h:172
@ SCH_SHEET_T
Definition: typeinfo.h:174
@ SCH_MARKER_T
Definition: typeinfo.h:158
@ SCHEMATIC_T
Definition: typeinfo.h:203
@ SCH_GLOBAL_LABEL_T
Definition: typeinfo.h:168