KiCad PCB EDA Suite
Loading...
Searching...
No Matches
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 (C) 2020-2023, 2024 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 <ee_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 wxString ref = symbol->GetRef( &sheetPath );
78 int unit = symbol->GetUnit();
79 LIB_ID libId = symbol->GetLibId();
80
81 for( SCH_SHEET_PATH& sheet : Hierarchy() )
82 {
83 std::vector<SCH_SYMBOL*> otherUnits;
84
85 CollectOtherUnits( ref, unit, libId, sheet, &otherUnits );
86
87 for( SCH_SYMBOL* otherUnit : otherUnits )
88 {
89 switch( field->GetId() )
90 {
91 case VALUE_FIELD:
92 {
93 if( aCommit )
94 aCommit->Modify( otherUnit, sheet.LastScreen() );
95
96 otherUnit->SetValueFieldText( newValue );
97 break;
98 }
99
100 case FOOTPRINT_FIELD:
101 {
102 if( aCommit )
103 aCommit->Modify( otherUnit, sheet.LastScreen() );
104
105 otherUnit->SetFootprintFieldText( newValue );
106 break;
107 }
108
109 case DATASHEET_FIELD:
110 {
111 if( aCommit )
112 aCommit->Modify( otherUnit, sheet.LastScreen() );
113
114 otherUnit->GetField( DATASHEET_FIELD )->SetText( newValue );
115 break;
116 }
117
118 default:
119 break;
120 }
121 }
122 }
123 } );
124}
125
126
128{
130
131 delete m_currentSheet;
132 delete m_connectionGraph;
133
134 m_IsSchematicExists = false;
135}
136
137
139{
140 if( m_project )
141 {
143
144 // d'tor will save settings to file
145 delete project.m_ErcSettings;
146 project.m_ErcSettings = nullptr;
147
148 // d'tor will save settings to file
149 delete project.m_SchematicSettings;
150 project.m_SchematicSettings = nullptr;
151
152 m_project = nullptr; // clear the project, so we don't do this again when setting a new one
153 }
154
155 delete m_rootSheet;
156
157 m_rootSheet = nullptr;
158
161}
162
163
165{
166 if( m_project )
167 {
169
170 // d'tor will save settings to file
171 delete project.m_ErcSettings;
172 project.m_ErcSettings = nullptr;
173
174 // d'tor will save settings to file
175 delete project.m_SchematicSettings;
176 project.m_SchematicSettings = nullptr;
177 }
178
179 m_project = aPrj;
180
181 if( m_project )
182 {
184 project.m_ErcSettings = new ERC_SETTINGS( &project, "erc" );
185 project.m_SchematicSettings = new SCHEMATIC_SETTINGS( &project, "schematic" );
186
187 project.m_SchematicSettings->LoadFromFile();
188 project.m_SchematicSettings->m_NgspiceSettings->LoadFromFile();
189 project.m_ErcSettings->LoadFromFile();
190 }
191}
192
193
194void SCHEMATIC::SetRoot( SCH_SHEET* aRootSheet )
195{
196 wxCHECK_RET( aRootSheet, wxS( "Call to SetRoot with null SCH_SHEET!" ) );
197
198 m_rootSheet = aRootSheet;
199
202
205}
206
207
209{
210 return IsValid() ? m_rootSheet->GetScreen() : nullptr;
211}
212
213
215{
216 wxCHECK( !m_hierarchy.empty(), m_hierarchy );
217
218 return m_hierarchy;
219}
220
221
223{
224 wxLogDebug( wxS( "Refreshing schematic heirarchy." ) );
225
227}
228
229
230void SCHEMATIC::GetContextualTextVars( wxArrayString* aVars ) const
231{
232 auto add =
233 [&]( const wxString& aVar )
234 {
235 if( !alg::contains( *aVars, aVar ) )
236 aVars->push_back( aVar );
237 };
238
239 add( wxT( "#" ) );
240 add( wxT( "##" ) );
241 add( wxT( "SHEETPATH" ) );
242 add( wxT( "SHEETNAME" ) );
243 add( wxT( "FILENAME" ) );
244 add( wxT( "FILEPATH" ) );
245 add( wxT( "PROJECTNAME" ) );
246
247 if( !CurrentSheet().empty() )
249
250 for( std::pair<wxString, wxString> entry : Prj().GetTextVars() )
251 add( entry.first );
252}
253
254
255bool SCHEMATIC::ResolveTextVar( const SCH_SHEET_PATH* aSheetPath, wxString* token,
256 int aDepth ) const
257{
258 wxCHECK( aSheetPath, false );
259
260 if( token->IsSameAs( wxT( "#" ) ) )
261 {
262 *token = aSheetPath->GetPageNumber();
263 return true;
264 }
265 else if( token->IsSameAs( wxT( "##" ) ) )
266 {
267 *token = wxString::Format( "%i", Root().CountSheets() );
268 return true;
269 }
270 else if( token->IsSameAs( wxT( "SHEETPATH" ) ) )
271 {
272 *token = aSheetPath->PathHumanReadable();
273 return true;
274 }
275 else if( token->IsSameAs( wxT( "SHEETNAME" ) ) )
276 {
277 *token = aSheetPath->Last()->GetName();
278 return true;
279 }
280 else if( token->IsSameAs( wxT( "FILENAME" ) ) )
281 {
282 wxFileName fn( GetFileName() );
283 *token = fn.GetFullName();
284 return true;
285 }
286 else if( token->IsSameAs( wxT( "FILEPATH" ) ) )
287 {
288 wxFileName fn( GetFileName() );
289 *token = fn.GetFullPath();
290 return true;
291 }
292 else if( token->IsSameAs( wxT( "PROJECTNAME" ) ) )
293 {
294 *token = Prj().GetProjectName();
295 return true;
296 }
297
298 if( aSheetPath->LastScreen()->GetTitleBlock().TextVarResolver( token, m_project ) )
299 return true;
300
301 if( Prj().TextVarResolver( token ) )
302 return true;
303
304 return false;
305}
306
307
309{
310 return IsValid() ? m_rootSheet->GetScreen()->GetFileName() : wxString( wxEmptyString );
311}
312
313
315{
316 wxASSERT( m_project );
318}
319
320
322{
323 wxASSERT( m_project );
325}
326
327
328std::vector<SCH_MARKER*> SCHEMATIC::ResolveERCExclusions()
329{
330 SCH_SHEET_LIST sheetList = Hierarchy();
331 ERC_SETTINGS& settings = ErcSettings();
332
333 // Migrate legacy marker exclusions to new format to ensure exclusion matching functions across
334 // file versions. Silently drops any legacy exclusions which can not be mapped to the new format
335 // without risking an incorrect exclusion - this is preferable to silently dropping
336 // new ERC errors / warnings due to an incorrect match between a legacy and new
337 // marker serialization format
338 std::set<wxString> migratedExclusions;
339
340 for( auto it = settings.m_ErcExclusions.begin(); it != settings.m_ErcExclusions.end(); )
341 {
342 SCH_MARKER* testMarker = SCH_MARKER::DeserializeFromString( sheetList, *it );
343
344 if( !testMarker )
345 {
346 it = settings.m_ErcExclusions.erase( it );
347 continue;
348 }
349
350 if( testMarker->IsLegacyMarker() )
351 {
352 const wxString settingsKey = testMarker->GetRCItem()->GetSettingsKey();
353
354 if( settingsKey != wxT( "pin_to_pin" )
355 && settingsKey != wxT( "hier_label_mismatch" )
356 && settingsKey != wxT( "different_unit_net" ) )
357 {
358 migratedExclusions.insert( testMarker->SerializeToString() );
359 }
360
361 it = settings.m_ErcExclusions.erase( it );
362 }
363 else
364 {
365 ++it;
366 }
367
368 delete testMarker;
369 }
370
371 settings.m_ErcExclusions.insert( migratedExclusions.begin(), migratedExclusions.end() );
372
373 // End of legacy exclusion removal / migrations
374
375 for( const SCH_SHEET_PATH& sheet : sheetList )
376 {
377 for( SCH_ITEM* item : sheet.LastScreen()->Items().OfType( SCH_MARKER_T ) )
378 {
379 SCH_MARKER* marker = static_cast<SCH_MARKER*>( item );
380 wxString serialized = marker->SerializeToString();
381 std::set<wxString>::iterator it = settings.m_ErcExclusions.find( serialized );
382
383 if( it != settings.m_ErcExclusions.end() )
384 {
385 marker->SetExcluded( true, settings.m_ErcExclusionComments[serialized] );
386 settings.m_ErcExclusions.erase( it );
387 }
388 }
389 }
390
391 std::vector<SCH_MARKER*> newMarkers;
392
393 for( const wxString& serialized : settings.m_ErcExclusions )
394 {
395 SCH_MARKER* marker = SCH_MARKER::DeserializeFromString( sheetList, serialized );
396
397 if( marker )
398 {
399 marker->SetExcluded( true, settings.m_ErcExclusionComments[serialized] );
400 newMarkers.push_back( marker );
401 }
402 }
403
404 settings.m_ErcExclusions.clear();
405
406 return newMarkers;
407}
408
409
410std::shared_ptr<BUS_ALIAS> SCHEMATIC::GetBusAlias( const wxString& aLabel ) const
411{
412 for( const SCH_SHEET_PATH& sheet : Hierarchy() )
413 {
414 for( const std::shared_ptr<BUS_ALIAS>& alias : sheet.LastScreen()->GetBusAliases() )
415 {
416 if( alias->GetName() == aLabel )
417 return alias;
418 }
419 }
420
421 return nullptr;
422}
423
424
426{
427 std::set<wxString> names;
428
429 for( const auto& [ key, subgraphList ] : m_connectionGraph->GetNetMap() )
430 {
431 CONNECTION_SUBGRAPH* firstSubgraph = subgraphList[0];
432
433 if( !firstSubgraph->GetDriverConnection()->IsBus()
435 {
436 names.insert( key.Name );
437 }
438 }
439
440 return names;
441}
442
443
444bool SCHEMATIC::ResolveCrossReference( wxString* token, int aDepth ) const
445{
446 wxString remainder;
447 wxString ref = token->BeforeFirst( ':', &remainder );
448 SCH_SHEET_PATH sheetPath;
449 SCH_ITEM* refItem = GetItem( KIID( ref ), &sheetPath );
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 token = refSymbol->m_Uuid.AsString() + wxS( ":" ) + remainder;
555 break;
556 }
557 }
558 }
559
560 newbuf.append( wxS( "${" ) + token + wxS( "}" ) );
561 }
562 else
563 {
564 newbuf.append( aSource[i] );
565 }
566 }
567
568 return newbuf;
569}
570
571
572wxString SCHEMATIC::ConvertKIIDsToRefs( const wxString& aSource ) const
573{
574 wxString newbuf;
575 size_t sourceLen = aSource.length();
576
577 for( size_t i = 0; i < sourceLen; ++i )
578 {
579 if( aSource[i] == '$' && i + 1 < sourceLen && aSource[i+1] == '{' )
580 {
581 wxString token;
582 bool isCrossRef = false;
583
584 for( i = i + 2; i < sourceLen; ++i )
585 {
586 if( aSource[i] == '}' )
587 break;
588
589 if( aSource[i] == ':' )
590 isCrossRef = true;
591
592 token.append( aSource[i] );
593 }
594
595 if( isCrossRef )
596 {
597 wxString remainder;
598 wxString ref = token.BeforeFirst( ':', &remainder );
599
600 SCH_SHEET_PATH refSheetPath;
601 SCH_ITEM* refItem = GetItem( KIID( ref ), &refSheetPath );
602
603 if( refItem && refItem->Type() == SCH_SYMBOL_T )
604 {
605 SCH_SYMBOL* refSymbol = static_cast<SCH_SYMBOL*>( refItem );
606 token = refSymbol->GetRef( &refSheetPath, true ) + wxS( ":" ) + remainder;
607 }
608 }
609
610 newbuf.append( wxS( "${" ) + token + wxS( "}" ) );
611 }
612 else
613 {
614 newbuf.append( aSource[i] );
615 }
616 }
617
618 return newbuf;
619}
620
621
623{
624 SCH_SCREENS screens( m_rootSheet );
625
627}
628
629
631{
632 // Filename is rootSheetName-sheetName-...-sheetName
633 // Note that we need to fetch the rootSheetName out of its filename, as the root SCH_SHEET's
634 // name is just a timestamp.
635
636 wxFileName rootFn( CurrentSheet().at( 0 )->GetFileName() );
637 wxString filename = rootFn.GetName();
638
639 for( unsigned i = 1; i < CurrentSheet().size(); i++ )
640 filename += wxT( "-" ) + CurrentSheet().at( i )->GetName();
641
642 return filename;
643}
644
645
647{
648 SCH_SCREEN* screen;
649 SCH_SCREENS s_list( Root() );
650
651 // Set the sheet count, and the sheet number (1 for root sheet)
652 int sheet_count = Root().CountSheets();
653 int sheet_number = 1;
654 const KIID_PATH& current_sheetpath = CurrentSheet().Path();
655
656 // @todo Remove all pseudo page number system is left over from prior to real page number
657 // implementation.
658 for( const SCH_SHEET_PATH& sheet : Hierarchy() )
659 {
660 if( sheet.Path() == current_sheetpath ) // Current sheet path found
661 break;
662
663 sheet_number++; // Not found, increment before this current path
664 }
665
666 for( screen = s_list.GetFirst(); screen != nullptr; screen = s_list.GetNext() )
667 screen->SetPageCount( sheet_count );
668
669 CurrentSheet().SetVirtualPageNumber( sheet_number );
670 CurrentSheet().LastScreen()->SetVirtualPageNumber( sheet_number );
671 CurrentSheet().LastScreen()->SetPageNumber( CurrentSheet().GetPageNumber() );
672}
673
674
675void SCHEMATIC::RecomputeIntersheetRefs( const std::function<void( SCH_GLOBALLABEL* )>& aItemCallback )
676{
677 std::map<wxString, std::set<int>>& pageRefsMap = GetPageRefsMap();
678
679 pageRefsMap.clear();
680
681 for( const SCH_SHEET_PATH& sheet : Hierarchy() )
682 {
683 for( SCH_ITEM* item : sheet.LastScreen()->Items().OfType( SCH_GLOBAL_LABEL_T ) )
684 {
685 SCH_GLOBALLABEL* global = static_cast<SCH_GLOBALLABEL*>( item );
686 wxString resolvedLabel = global->GetShownText( &sheet, false );
687
688 pageRefsMap[ resolvedLabel ].insert( sheet.GetVirtualPageNumber() );
689 }
690 }
691
692 bool show = Settings().m_IntersheetRefsShow;
693
694 // Refresh all visible global labels. Note that we have to collect them first as the
695 // SCH_SCREEN::Update() call is going to invalidate the RTree iterator.
696
697 std::vector<SCH_GLOBALLABEL*> currentSheetGlobalLabels;
698
699 for( EDA_ITEM* item : CurrentSheet().LastScreen()->Items().OfType( SCH_GLOBAL_LABEL_T ) )
700 currentSheetGlobalLabels.push_back( static_cast<SCH_GLOBALLABEL*>( item ) );
701
702 for( SCH_GLOBALLABEL* globalLabel : currentSheetGlobalLabels )
703 {
704 std::vector<SCH_FIELD>& fields = globalLabel->GetFields();
705
706 fields[0].SetVisible( show );
707
708 if( show )
709 {
710 if( fields.size() == 1 && fields[0].GetTextPos() == globalLabel->GetPosition() )
711 globalLabel->AutoplaceFields( CurrentSheet().LastScreen(), false );
712
713 CurrentSheet().LastScreen()->Update( globalLabel );
714 aItemCallback( globalLabel );
715 }
716 }
717}
718
719
720wxString SCHEMATIC::GetOperatingPoint( const wxString& aNetName, int aPrecision,
721 const wxString& aRange )
722{
723 wxString spiceNetName( aNetName.Lower() );
725
726 if( spiceNetName == wxS( "gnd" ) || spiceNetName == wxS( "0" ) )
727 return wxEmptyString;
728
729 auto it = m_operatingPoints.find( spiceNetName );
730
731 if( it != m_operatingPoints.end() )
732 return SPICE_VALUE( it->second ).ToString( { aPrecision, aRange } );
733 else if( m_operatingPoints.empty() )
734 return wxS( "--" );
735 else
736 return wxS( "?" );
737}
738
739
741{
742 SCH_SCREENS screens( Root() );
743
744 for( SCH_SCREEN* screen = screens.GetFirst(); screen; screen = screens.GetNext() )
745 {
746 std::deque<EDA_ITEM*> allItems;
747
748 for( auto item : screen->Items() )
749 allItems.push_back( item );
750
751 // Add missing junctions and breakup wires as needed
752 for( const VECTOR2I& point : screen->GetNeededJunctions( allItems ) )
753 {
754 SCH_JUNCTION* junction = new SCH_JUNCTION( point );
755 screen->Append( junction );
756
757 // Breakup wires
758 for( SCH_LINE* wire : screen->GetBusesAndWires( point, true ) )
759 {
760 SCH_LINE* newSegment = wire->BreakAt( point );
761 screen->Append( newSegment );
762 }
763 }
764 }
765}
766
767
768void SCHEMATIC::OnItemsAdded( std::vector<SCH_ITEM*>& aNewItems )
769{
771}
772
773
774void SCHEMATIC::OnItemsRemoved( std::vector<SCH_ITEM*>& aRemovedItems )
775{
777}
778
779
780void SCHEMATIC::OnItemsChanged( std::vector<SCH_ITEM*>& aItems )
781{
783}
784
785
787{
789}
790
791
793{
794 if( !alg::contains( m_listeners, aListener ) )
795 m_listeners.push_back( aListener );
796}
797
798
800{
801 auto i = std::find( m_listeners.begin(), m_listeners.end(), aListener );
802
803 if( i != m_listeners.end() )
804 {
805 std::iter_swap( i, m_listeners.end() - 1 );
806 m_listeners.pop_back();
807 }
808}
809
810
812{
813 m_listeners.clear();
814}
815
816
818{
819 // Use a sorted sheetList to reduce file churn
820 SCH_SHEET_LIST sheetList = Hierarchy();
821 ERC_SETTINGS& ercSettings = ErcSettings();
822
823 ercSettings.m_ErcExclusions.clear();
824 ercSettings.m_ErcExclusionComments.clear();
825
826 for( unsigned i = 0; i < sheetList.size(); i++ )
827 {
828 for( SCH_ITEM* item : sheetList[i].LastScreen()->Items().OfType( SCH_MARKER_T ) )
829 {
830 SCH_MARKER* marker = static_cast<SCH_MARKER*>( item );
831
832 if( marker->IsExcluded() )
833 {
834 wxString serialized = marker->SerializeToString();
835 ercSettings.m_ErcExclusions.insert( serialized );
836 ercSettings.m_ErcExclusionComments[ serialized ] = marker->GetComment();
837 }
838 }
839 }
840}
841
842
844{
845 SCH_SHEET_LIST sheetList = Hierarchy();
846
847 for( SCH_MARKER* marker : ResolveERCExclusions() )
848 {
849 SCH_SHEET_PATH errorPath;
850 ignore_unused( sheetList.GetItem( marker->GetRCItem()->GetMainItemID(), &errorPath ) );
851
852 if( errorPath.LastScreen() )
853 errorPath.LastScreen()->Append( marker );
854 else
855 RootScreen()->Append( marker );
856 }
857}
858
859
861{
862 return static_cast<EMBEDDED_FILES*>( this );
863}
864
865
867{
868 return static_cast<const EMBEDDED_FILES*>( this );
869}
870
871
873{
874 std::set<KIFONT::OUTLINE_FONT*> fonts;
875
876 SCH_SHEET_LIST sheetList = Hierarchy();
877
878 for( const SCH_SHEET_PATH& sheet : sheetList )
879 {
880 for( SCH_ITEM* item : sheet.LastScreen()->Items() )
881 {
882 if( EDA_TEXT* text = dynamic_cast<EDA_TEXT*>( item ) )
883 {
884 KIFONT::FONT* font = text->GetFont();
885
886 if( !font || font->IsStroke() )
887 continue;
888
889 using EMBEDDING_PERMISSION = KIFONT::OUTLINE_FONT::EMBEDDING_PERMISSION;
890 auto* outline = static_cast<KIFONT::OUTLINE_FONT*>( font );
891
892 if( outline->GetEmbeddingPermission() == EMBEDDING_PERMISSION::EDITABLE
893 || outline->GetEmbeddingPermission() == EMBEDDING_PERMISSION::INSTALLABLE )
894 {
895 fonts.insert( outline );
896 }
897 }
898 }
899 }
900
901 for( KIFONT::OUTLINE_FONT* font : fonts )
902 {
903 auto file = GetEmbeddedFiles()->AddFile( font->GetFileName(), false );
904
905 if( !file )
906 {
907 wxLogTrace( "EMBED", "Failed to add font file: %s", font->GetFileName() );
908 continue;
909 }
910
912 }
913}
914
915
916std::set<const SCH_SCREEN*> SCHEMATIC::GetSchematicsSharedByMultipleProjects() const
917{
918 std::set<const SCH_SCREEN*> retv;
919
920 wxCHECK( m_rootSheet, retv );
921
922 SCH_SHEET_LIST hierarchy( m_rootSheet );
923 SCH_SCREENS screens( m_rootSheet );
924
925 for( const SCH_SCREEN* screen = screens.GetFirst(); screen; screen = screens.GetNext() )
926 {
927 for( const SCH_ITEM* item : screen->Items().OfType( SCH_SYMBOL_T ) )
928 {
929 const SCH_SYMBOL* symbol = static_cast<const SCH_SYMBOL*>( item );
930
931 const std::vector<SCH_SYMBOL_INSTANCE> symbolInstances = symbol->GetInstances();
932
933 for( const SCH_SYMBOL_INSTANCE& instance : symbolInstances )
934 {
935 if( !hierarchy.HasPath( instance.m_Path ) )
936 {
937 retv.insert( screen );
938 break;
939 }
940 }
941
942 if( retv.count( screen ) )
943 break;
944 }
945 }
946
947 return retv;
948}
949
950
952{
953 wxCHECK( m_rootSheet, false );
954
955 SCH_SCREENS screens( m_rootSheet );
956
957 for( const SCH_SCREEN* screen = screens.GetFirst(); screen; screen = screens.GetNext() )
958 {
959 wxCHECK2( screen, continue );
960
961 if( screen->GetRefCount() > 1 )
962 return true;
963 }
964
965 return false;
966}
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)
Create an undo entry for an item that has been already modified.
Definition: commit.h:105
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:89
const KIID m_Uuid
Definition: eda_item.h:489
KICAD_T Type() const
Returns the type of object.
Definition: eda_item.h:101
EDA_ITEM * GetParent() const
Definition: eda_item.h:103
A mix-in class (via multiple inheritance) that handles texts such as labels, parts,...
Definition: eda_text.h:79
EMBEDDED_FILE * AddFile(const wxFileName &aName, bool aOverwrite)
Loads a file from disk and adds it to the collection.
Container for ERC settings.
Definition: erc_settings.h:128
std::map< wxString, wxString > m_ErcExclusionComments
Definition: erc_settings.h:189
std::set< wxString > m_ErcExclusions
Definition: erc_settings.h:188
Class that other classes need to inherit from, in order to be inspectable.
Definition: inspectable.h:36
wxAny Get(PROPERTY_BASE *aProperty) const
Definition: inspectable.h:99
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
wxString AsString() const
Definition: kiid.cpp:238
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:98
std::shared_ptr< RC_ITEM > GetRCItem() const
Definition: marker_base.h:112
void SetExcluded(bool aExcluded, const wxString &aComment=wxEmptyString)
Definition: marker_base.h:99
wxString GetComment() const
Definition: marker_base.h:105
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:72
ERC_SETTINGS * m_ErcSettings
Eeschema params.
Definition: project_file.h:137
SCHEMATIC_SETTINGS * m_SchematicSettings
Definition: project_file.h:140
Container for project specific data.
Definition: project.h:64
virtual const wxString GetProjectName() const
Return the short name of the project.
Definition: project.cpp:147
virtual PROJECT_FILE & GetProjectFile() const
Definition: project.h:200
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:62
virtual void OnSchItemsChanged(SCHEMATIC &aSch, std::vector< SCH_ITEM * > &aSchItem)
Definition: schematic.h:63
virtual void OnSchSheetChanged(SCHEMATIC &aSch)
Definition: schematic.h:66
virtual void OnSchItemsAdded(SCHEMATIC &aSch, std::vector< SCH_ITEM * > &aSchItem)
Definition: schematic.h:61
These are loaded from Eeschema settings but then overwritten by the project settings.
void Reset()
Initialize this schematic to a blank one, unloading anything existing.
Definition: schematic.cpp:138
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:916
void SetLegacySymbolInstanceData()
Update the symbol value and footprint instance data for legacy designs.
Definition: schematic.cpp:622
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:768
CONNECTION_GRAPH * m_connectionGraph
Holds and calculates connectivity information of this schematic.
Definition: schematic.h:368
SCH_SHEET_LIST m_hierarchy
Cache of the entire schematic hierarchy sorted by sheet page number.
Definition: schematic.h:389
SCH_SHEET_PATH & CurrentSheet() const override
Definition: schematic.h:152
void ResolveERCExclusionsPostUpdate()
Update markers to match recorded exclusions.
Definition: schematic.cpp:843
void RemoveListener(SCHEMATIC_LISTENER *aListener)
Remove the specified listener.
Definition: schematic.cpp:799
bool IsComplexHierarchy() const
Test if the schematic is a complex hierarchy.
Definition: schematic.cpp:951
void OnSchSheetChanged()
Notify the schematic and its listeners that the current sheet has been changed.
Definition: schematic.cpp:786
SCH_SHEET_PATH * m_currentSheet
The sheet path of the sheet currently being edited or displayed.
Definition: schematic.h:365
wxString GetOperatingPoint(const wxString &aNetName, int aPrecision, const wxString &aRange)
Definition: schematic.cpp:720
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:774
virtual ~SCHEMATIC()
Definition: schematic.cpp:127
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:410
std::vector< SCH_MARKER * > ResolveERCExclusions()
Definition: schematic.cpp:328
wxString GetFileName() const override
Helper to retrieve the filename from the root sheet screen.
Definition: schematic.cpp:308
void EmbedFonts() override
Embed fonts in the schematic.
Definition: schematic.cpp:872
SCHEMATIC_SETTINGS & Settings() const
Definition: schematic.cpp:314
wxString ConvertKIIDsToRefs(const wxString &aSource) const
Definition: schematic.cpp:572
void RecordERCExclusions()
Scan existing markers and record data from any that are Excluded.
Definition: schematic.cpp:817
std::map< wxString, std::set< int > > & GetPageRefsMap()
Definition: schematic.h:196
void FixupJunctions()
Add junctions to this schematic where required.
Definition: schematic.cpp:740
SCH_SHEET_LIST Hierarchy() const override
Return the full schematic flattened hierarchical sheet list.
Definition: schematic.cpp:214
SCH_ITEM * GetItem(const KIID &aID, SCH_SHEET_PATH *aPathOut=nullptr) const
Definition: schematic.h:116
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:194
void SetProject(PROJECT *aPrj)
Definition: schematic.cpp:164
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:792
std::map< int, wxString > GetVirtualPageToSheetPagesMap() const
Definition: schematic.cpp:494
EMBEDDED_FILES * GetEmbeddedFiles() override
Definition: schematic.cpp:860
PROJECT * m_project
Definition: schematic.h:354
SCH_SCREEN * RootScreen() const
Helper to retrieve the screen of the root sheet.
Definition: schematic.cpp:208
SCHEMATIC(PROJECT *aPrj)
Definition: schematic.cpp:45
bool ResolveTextVar(const SCH_SHEET_PATH *aSheetPath, wxString *token, int aDepth) const
Definition: schematic.cpp:255
std::set< wxString > GetNetClassAssignmentCandidates()
Return the set of netname candidates for netclass assignment.
Definition: schematic.cpp:425
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:675
void InvokeListeners(Func &&aFunc, Args &&... args)
Definition: schematic.h:348
bool IsValid() const
A simple test if the schematic is loaded, not a complete one.
Definition: schematic.h:137
static bool m_IsSchematicExists
True if a SCHEMATIC exists, false if not.
Definition: schematic.h:338
void RemoveAllListeners()
Remove all listeners.
Definition: schematic.cpp:811
void GetContextualTextVars(wxArrayString *aVars) const
Definition: schematic.cpp:230
SCH_SHEET & Root() const
Definition: schematic.h:121
std::map< int, wxString > GetVirtualPageToSheetNamesMap() const
Definition: schematic.cpp:478
SCH_SHEET_LIST BuildSheetListSortedByPageNumbers() const override
Definition: schematic.h:97
wxString GetUniqueFilenameForCurrentSheet()
Definition: schematic.cpp:630
void SetSheetNumberAndCount()
Set the m_ScreenNumber and m_NumberOfScreens members for screens.
Definition: schematic.cpp:646
std::vector< SCHEMATIC_LISTENER * > m_listeners
Currently installed listeners.
Definition: schematic.h:394
PROJECT & Prj() const override
Return a reference to the project this schematic is part of.
Definition: schematic.h:92
bool ResolveCrossReference(wxString *token, int aDepth) const
Resolves text vars that refer to other items.
Definition: schematic.cpp:444
std::map< wxString, double > m_operatingPoints
Simulation operating points for text variable substitution.
Definition: schematic.h:384
ERC_SETTINGS & ErcSettings() const
Definition: schematic.cpp:321
void RefreshHierarchy()
Definition: schematic.cpp:222
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:780
SCH_SHEET * m_rootSheet
The top-level sheet in this schematic hierarchy (or potentially the only one)
Definition: schematic.h:357
bool IsBus() const
Instances are attached to a symbol or sheet and provide a place for the symbol's value,...
Definition: sch_field.h:51
int GetId() const
Definition: sch_field.h:133
Base class for any item which can be embedded within the SCHEMATIC container class,...
Definition: sch_item.h:166
int GetUnit() const
Definition: sch_item.h:229
wxString GetShownText(const SCH_SHEET_PATH *aPath, bool aAllowExtraText, int aDepth=0) const override
Definition: sch_label.cpp:816
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:583
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
Determines if this marker is legacy (i.e.
Definition: sch_marker.h:123
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:712
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:153
const wxString & GetFileName() const
Definition: sch_screen.h:143
const TITLE_BLOCK & GetTitleBlock() const
Definition: sch_screen.h:154
void Update(SCH_ITEM *aItem, bool aUpdateLibSymbol=true)
Update aItem's bounding box in the tree.
Definition: sch_screen.cpp:316
A container for handling SCH_SHEET_PATH objects in a flattened hierarchy.
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:57
wxString GetName() const
Definition: sch_sheet.h:107
int CountSheets() const
Count the number of sheets found in "this" sheet including all of the subsheets.
Definition: sch_sheet.cpp:845
SCH_SCREEN * GetScreen() const
Definition: sch_sheet.h:110
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:254
Schematic symbol object.
Definition: sch_symbol.h:104
const std::vector< SCH_SYMBOL_INSTANCE > & GetInstances() const
Definition: sch_symbol.h:163
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:193
const wxString GetRef(const SCH_SHEET_PATH *aSheet, bool aIncludeUnit=false) const override
Definition: sch_symbol.cpp:737
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) const
Definition: title_block.cpp:96
static void GetContextualTextVars(wxArrayString *aVars)
Definition: title_block.cpp:74
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 CollectOtherUnits(const wxString &aRef, int aUnit, const LIB_ID &aLibId, SCH_SHEET_PATH &aSheet, std::vector< SCH_SYMBOL * > *otherUnits)
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
A simple container for schematic symbol instance information.
@ DATASHEET_FIELD
name of datasheet
@ FOOTPRINT_FIELD
Field Name Module PCB, i.e. "16DIP300".
@ VALUE_FIELD
Field Value of part, i.e. "3.3K".
@ 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