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{
225}
226
227
228void SCHEMATIC::GetContextualTextVars( wxArrayString* aVars ) const
229{
230 auto add =
231 [&]( const wxString& aVar )
232 {
233 if( !alg::contains( *aVars, aVar ) )
234 aVars->push_back( aVar );
235 };
236
237 add( wxT( "#" ) );
238 add( wxT( "##" ) );
239 add( wxT( "SHEETPATH" ) );
240 add( wxT( "SHEETNAME" ) );
241 add( wxT( "FILENAME" ) );
242 add( wxT( "FILEPATH" ) );
243 add( wxT( "PROJECTNAME" ) );
244
245 if( !CurrentSheet().empty() )
247
248 for( std::pair<wxString, wxString> entry : Prj().GetTextVars() )
249 add( entry.first );
250}
251
252
253bool SCHEMATIC::ResolveTextVar( const SCH_SHEET_PATH* aSheetPath, wxString* token,
254 int aDepth ) const
255{
256 wxCHECK( aSheetPath, false );
257
258 if( token->IsSameAs( wxT( "#" ) ) )
259 {
260 *token = aSheetPath->GetPageNumber();
261 return true;
262 }
263 else if( token->IsSameAs( wxT( "##" ) ) )
264 {
265 *token = wxString::Format( "%i", Root().CountSheets() );
266 return true;
267 }
268 else if( token->IsSameAs( wxT( "SHEETPATH" ) ) )
269 {
270 *token = aSheetPath->PathHumanReadable();
271 return true;
272 }
273 else if( token->IsSameAs( wxT( "SHEETNAME" ) ) )
274 {
275 *token = aSheetPath->Last()->GetName();
276 return true;
277 }
278 else if( token->IsSameAs( wxT( "FILENAME" ) ) )
279 {
280 wxFileName fn( GetFileName() );
281 *token = fn.GetFullName();
282 return true;
283 }
284 else if( token->IsSameAs( wxT( "FILEPATH" ) ) )
285 {
286 wxFileName fn( GetFileName() );
287 *token = fn.GetFullPath();
288 return true;
289 }
290 else if( token->IsSameAs( wxT( "PROJECTNAME" ) ) )
291 {
292 *token = Prj().GetProjectName();
293 return true;
294 }
295
296 if( aSheetPath->LastScreen()->GetTitleBlock().TextVarResolver( token, m_project ) )
297 return true;
298
299 if( Prj().TextVarResolver( token ) )
300 return true;
301
302 return false;
303}
304
305
307{
308 return IsValid() ? m_rootSheet->GetScreen()->GetFileName() : wxString( wxEmptyString );
309}
310
311
313{
314 wxASSERT( m_project );
316}
317
318
320{
321 wxASSERT( m_project );
323}
324
325
326std::vector<SCH_MARKER*> SCHEMATIC::ResolveERCExclusions()
327{
328 SCH_SHEET_LIST sheetList = Hierarchy();
329 ERC_SETTINGS& settings = ErcSettings();
330
331 // Migrate legacy marker exclusions to new format to ensure exclusion matching functions across
332 // file versions. Silently drops any legacy exclusions which can not be mapped to the new format
333 // without risking an incorrect exclusion - this is preferable to silently dropping
334 // new ERC errors / warnings due to an incorrect match between a legacy and new
335 // marker serialization format
336 std::set<wxString> migratedExclusions;
337
338 for( auto it = settings.m_ErcExclusions.begin(); it != settings.m_ErcExclusions.end(); )
339 {
340 SCH_MARKER* testMarker = SCH_MARKER::DeserializeFromString( sheetList, *it );
341
342 if( !testMarker )
343 {
344 it = settings.m_ErcExclusions.erase( it );
345 continue;
346 }
347
348 if( testMarker->IsLegacyMarker() )
349 {
350 const wxString settingsKey = testMarker->GetRCItem()->GetSettingsKey();
351
352 if( settingsKey != wxT( "pin_to_pin" )
353 && settingsKey != wxT( "hier_label_mismatch" )
354 && settingsKey != wxT( "different_unit_net" ) )
355 {
356 migratedExclusions.insert( testMarker->SerializeToString() );
357 }
358
359 it = settings.m_ErcExclusions.erase( it );
360 }
361 else
362 {
363 ++it;
364 }
365
366 delete testMarker;
367 }
368
369 settings.m_ErcExclusions.insert( migratedExclusions.begin(), migratedExclusions.end() );
370
371 // End of legacy exclusion removal / migrations
372
373 for( const SCH_SHEET_PATH& sheet : sheetList )
374 {
375 for( SCH_ITEM* item : sheet.LastScreen()->Items().OfType( SCH_MARKER_T ) )
376 {
377 SCH_MARKER* marker = static_cast<SCH_MARKER*>( item );
378 wxString serialized = marker->SerializeToString();
379 std::set<wxString>::iterator it = settings.m_ErcExclusions.find( serialized );
380
381 if( it != settings.m_ErcExclusions.end() )
382 {
383 marker->SetExcluded( true, settings.m_ErcExclusionComments[serialized] );
384 settings.m_ErcExclusions.erase( it );
385 }
386 }
387 }
388
389 std::vector<SCH_MARKER*> newMarkers;
390
391 for( const wxString& serialized : settings.m_ErcExclusions )
392 {
393 SCH_MARKER* marker = SCH_MARKER::DeserializeFromString( sheetList, serialized );
394
395 if( marker )
396 {
397 marker->SetExcluded( true, settings.m_ErcExclusionComments[serialized] );
398 newMarkers.push_back( marker );
399 }
400 }
401
402 settings.m_ErcExclusions.clear();
403
404 return newMarkers;
405}
406
407
408std::shared_ptr<BUS_ALIAS> SCHEMATIC::GetBusAlias( const wxString& aLabel ) const
409{
410 for( const SCH_SHEET_PATH& sheet : Hierarchy() )
411 {
412 for( const std::shared_ptr<BUS_ALIAS>& alias : sheet.LastScreen()->GetBusAliases() )
413 {
414 if( alias->GetName() == aLabel )
415 return alias;
416 }
417 }
418
419 return nullptr;
420}
421
422
424{
425 std::set<wxString> names;
426
427 for( const auto& [ key, subgraphList ] : m_connectionGraph->GetNetMap() )
428 {
429 CONNECTION_SUBGRAPH* firstSubgraph = subgraphList[0];
430
431 if( !firstSubgraph->GetDriverConnection()->IsBus()
433 {
434 names.insert( key.Name );
435 }
436 }
437
438 return names;
439}
440
441
442bool SCHEMATIC::ResolveCrossReference( wxString* token, int aDepth ) const
443{
444 wxString remainder;
445 wxString ref = token->BeforeFirst( ':', &remainder );
446 SCH_SHEET_PATH sheetPath;
447 SCH_ITEM* refItem = GetItem( KIID( ref ), &sheetPath );
448
449 if( refItem && refItem->Type() == SCH_SYMBOL_T )
450 {
451 SCH_SYMBOL* refSymbol = static_cast<SCH_SYMBOL*>( refItem );
452
453 if( refSymbol->ResolveTextVar( &sheetPath, &remainder, aDepth + 1 ) )
454 *token = remainder;
455 else
456 *token = refSymbol->GetRef( &sheetPath, true ) + wxS( ":" ) + remainder;
457
458 return true; // Cross-reference is resolved whether or not the actual textvar was
459 }
460 else if( refItem && refItem->Type() == SCH_SHEET_T )
461 {
462 SCH_SHEET* refSheet = static_cast<SCH_SHEET*>( refItem );
463
464 sheetPath.push_back( refSheet );
465
466 if( refSheet->ResolveTextVar( &sheetPath, &remainder, aDepth + 1 ) )
467 *token = remainder;
468
469 return true; // Cross-reference is resolved whether or not the actual textvar was
470 }
471
472 return false;
473}
474
475
476std::map<int, wxString> SCHEMATIC::GetVirtualPageToSheetNamesMap() const
477{
478 std::map<int, wxString> namesMap;
479
480 for( const SCH_SHEET_PATH& sheet : Hierarchy() )
481 {
482 if( sheet.size() == 1 )
483 namesMap[sheet.GetVirtualPageNumber()] = _( "<root sheet>" );
484 else
485 namesMap[sheet.GetVirtualPageNumber()] = sheet.Last()->GetName();
486 }
487
488 return namesMap;
489}
490
491
492std::map<int, wxString> SCHEMATIC::GetVirtualPageToSheetPagesMap() const
493{
494 std::map<int, wxString> pagesMap;
495
496 for( const SCH_SHEET_PATH& sheet : Hierarchy() )
497 pagesMap[sheet.GetVirtualPageNumber()] = sheet.GetPageNumber();
498
499 return pagesMap;
500}
501
502
503wxString SCHEMATIC::ConvertRefsToKIIDs( const wxString& aSource ) const
504{
505 wxString newbuf;
506 size_t sourceLen = aSource.length();
507
508 for( size_t i = 0; i < sourceLen; ++i )
509 {
510 if( aSource[i] == '$' && i + 1 < sourceLen && aSource[i+1] == '{' )
511 {
512 wxString token;
513 bool isCrossRef = false;
514 int nesting = 0;
515
516 for( i = i + 2; i < sourceLen; ++i )
517 {
518 if( aSource[i] == '{'
519 && ( aSource[i-1] == '_' || aSource[i-1] == '^' || aSource[i-1] == '~' ) )
520 {
521 nesting++;
522 }
523
524 if( aSource[i] == '}' )
525 {
526 nesting--;
527
528 if( nesting < 0 )
529 break;
530 }
531
532 if( aSource[i] == ':' )
533 isCrossRef = true;
534
535 token.append( aSource[i] );
536 }
537
538 if( isCrossRef )
539 {
540 wxString remainder;
541 wxString ref = token.BeforeFirst( ':', &remainder );
542 SCH_REFERENCE_LIST references;
543
544 Hierarchy().GetSymbols( references );
545
546 for( size_t jj = 0; jj < references.GetCount(); jj++ )
547 {
548 SCH_SYMBOL* refSymbol = references[ jj ].GetSymbol();
549
550 if( ref == refSymbol->GetRef( &references[ jj ].GetSheetPath(), true ) )
551 {
552 token = refSymbol->m_Uuid.AsString() + wxS( ":" ) + remainder;
553 break;
554 }
555 }
556 }
557
558 newbuf.append( wxS( "${" ) + token + wxS( "}" ) );
559 }
560 else
561 {
562 newbuf.append( aSource[i] );
563 }
564 }
565
566 return newbuf;
567}
568
569
570wxString SCHEMATIC::ConvertKIIDsToRefs( const wxString& aSource ) const
571{
572 wxString newbuf;
573 size_t sourceLen = aSource.length();
574
575 for( size_t i = 0; i < sourceLen; ++i )
576 {
577 if( aSource[i] == '$' && i + 1 < sourceLen && aSource[i+1] == '{' )
578 {
579 wxString token;
580 bool isCrossRef = false;
581
582 for( i = i + 2; i < sourceLen; ++i )
583 {
584 if( aSource[i] == '}' )
585 break;
586
587 if( aSource[i] == ':' )
588 isCrossRef = true;
589
590 token.append( aSource[i] );
591 }
592
593 if( isCrossRef )
594 {
595 wxString remainder;
596 wxString ref = token.BeforeFirst( ':', &remainder );
597
598 SCH_SHEET_PATH refSheetPath;
599 SCH_ITEM* refItem = GetItem( KIID( ref ), &refSheetPath );
600
601 if( refItem && refItem->Type() == SCH_SYMBOL_T )
602 {
603 SCH_SYMBOL* refSymbol = static_cast<SCH_SYMBOL*>( refItem );
604 token = refSymbol->GetRef( &refSheetPath, true ) + wxS( ":" ) + remainder;
605 }
606 }
607
608 newbuf.append( wxS( "${" ) + token + wxS( "}" ) );
609 }
610 else
611 {
612 newbuf.append( aSource[i] );
613 }
614 }
615
616 return newbuf;
617}
618
619
621{
622 SCH_SCREENS screens( m_rootSheet );
623
625}
626
627
629{
630 // Filename is rootSheetName-sheetName-...-sheetName
631 // Note that we need to fetch the rootSheetName out of its filename, as the root SCH_SHEET's
632 // name is just a timestamp.
633
634 wxFileName rootFn( CurrentSheet().at( 0 )->GetFileName() );
635 wxString filename = rootFn.GetName();
636
637 for( unsigned i = 1; i < CurrentSheet().size(); i++ )
638 filename += wxT( "-" ) + CurrentSheet().at( i )->GetName();
639
640 return filename;
641}
642
643
645{
646 SCH_SCREEN* screen;
647 SCH_SCREENS s_list( Root() );
648
649 // Set the sheet count, and the sheet number (1 for root sheet)
650 int sheet_count = Root().CountSheets();
651 int sheet_number = 1;
652 const KIID_PATH& current_sheetpath = CurrentSheet().Path();
653
654 // @todo Remove all pseudo page number system is left over from prior to real page number
655 // implementation.
656 for( const SCH_SHEET_PATH& sheet : Hierarchy() )
657 {
658 if( sheet.Path() == current_sheetpath ) // Current sheet path found
659 break;
660
661 sheet_number++; // Not found, increment before this current path
662 }
663
664 for( screen = s_list.GetFirst(); screen != nullptr; screen = s_list.GetNext() )
665 screen->SetPageCount( sheet_count );
666
667 CurrentSheet().SetVirtualPageNumber( sheet_number );
668 CurrentSheet().LastScreen()->SetVirtualPageNumber( sheet_number );
669 CurrentSheet().LastScreen()->SetPageNumber( CurrentSheet().GetPageNumber() );
670}
671
672
673void SCHEMATIC::RecomputeIntersheetRefs( const std::function<void( SCH_GLOBALLABEL* )>& aItemCallback )
674{
675 std::map<wxString, std::set<int>>& pageRefsMap = GetPageRefsMap();
676
677 pageRefsMap.clear();
678
679 for( const SCH_SHEET_PATH& sheet : Hierarchy() )
680 {
681 for( SCH_ITEM* item : sheet.LastScreen()->Items().OfType( SCH_GLOBAL_LABEL_T ) )
682 {
683 SCH_GLOBALLABEL* global = static_cast<SCH_GLOBALLABEL*>( item );
684 wxString resolvedLabel = global->GetShownText( &sheet, false );
685
686 pageRefsMap[ resolvedLabel ].insert( sheet.GetVirtualPageNumber() );
687 }
688 }
689
690 bool show = Settings().m_IntersheetRefsShow;
691
692 // Refresh all visible global labels. Note that we have to collect them first as the
693 // SCH_SCREEN::Update() call is going to invalidate the RTree iterator.
694
695 std::vector<SCH_GLOBALLABEL*> currentSheetGlobalLabels;
696
697 for( EDA_ITEM* item : CurrentSheet().LastScreen()->Items().OfType( SCH_GLOBAL_LABEL_T ) )
698 currentSheetGlobalLabels.push_back( static_cast<SCH_GLOBALLABEL*>( item ) );
699
700 for( SCH_GLOBALLABEL* globalLabel : currentSheetGlobalLabels )
701 {
702 std::vector<SCH_FIELD>& fields = globalLabel->GetFields();
703
704 fields[0].SetVisible( show );
705
706 if( show )
707 {
708 if( fields.size() == 1 && fields[0].GetTextPos() == globalLabel->GetPosition() )
709 globalLabel->AutoplaceFields( CurrentSheet().LastScreen(), false );
710
711 CurrentSheet().LastScreen()->Update( globalLabel );
712 aItemCallback( globalLabel );
713 }
714 }
715}
716
717
718wxString SCHEMATIC::GetOperatingPoint( const wxString& aNetName, int aPrecision,
719 const wxString& aRange )
720{
721 wxString spiceNetName( aNetName.Lower() );
723
724 if( spiceNetName == wxS( "gnd" ) || spiceNetName == wxS( "0" ) )
725 return wxEmptyString;
726
727 auto it = m_operatingPoints.find( spiceNetName );
728
729 if( it != m_operatingPoints.end() )
730 return SPICE_VALUE( it->second ).ToString( { aPrecision, aRange } );
731 else if( m_operatingPoints.empty() )
732 return wxS( "--" );
733 else
734 return wxS( "?" );
735}
736
737
739{
740 SCH_SCREENS screens( Root() );
741
742 for( SCH_SCREEN* screen = screens.GetFirst(); screen; screen = screens.GetNext() )
743 {
744 std::deque<EDA_ITEM*> allItems;
745
746 for( auto item : screen->Items() )
747 allItems.push_back( item );
748
749 // Add missing junctions and breakup wires as needed
750 for( const VECTOR2I& point : screen->GetNeededJunctions( allItems ) )
751 {
752 SCH_JUNCTION* junction = new SCH_JUNCTION( point );
753 screen->Append( junction );
754
755 // Breakup wires
756 for( SCH_LINE* wire : screen->GetBusesAndWires( point, true ) )
757 {
758 SCH_LINE* newSegment = wire->BreakAt( point );
759 screen->Append( newSegment );
760 }
761 }
762 }
763}
764
765
766void SCHEMATIC::OnItemsAdded( std::vector<SCH_ITEM*>& aNewItems )
767{
769}
770
771
772void SCHEMATIC::OnItemsRemoved( std::vector<SCH_ITEM*>& aRemovedItems )
773{
775}
776
777
778void SCHEMATIC::OnItemsChanged( std::vector<SCH_ITEM*>& aItems )
779{
781}
782
783
785{
787}
788
789
791{
792 if( !alg::contains( m_listeners, aListener ) )
793 m_listeners.push_back( aListener );
794}
795
796
798{
799 auto i = std::find( m_listeners.begin(), m_listeners.end(), aListener );
800
801 if( i != m_listeners.end() )
802 {
803 std::iter_swap( i, m_listeners.end() - 1 );
804 m_listeners.pop_back();
805 }
806}
807
808
810{
811 m_listeners.clear();
812}
813
814
816{
817 // Use a sorted sheetList to reduce file churn
818 SCH_SHEET_LIST sheetList = Hierarchy();
819 ERC_SETTINGS& ercSettings = ErcSettings();
820
821 ercSettings.m_ErcExclusions.clear();
822 ercSettings.m_ErcExclusionComments.clear();
823
824 for( unsigned i = 0; i < sheetList.size(); i++ )
825 {
826 for( SCH_ITEM* item : sheetList[i].LastScreen()->Items().OfType( SCH_MARKER_T ) )
827 {
828 SCH_MARKER* marker = static_cast<SCH_MARKER*>( item );
829
830 if( marker->IsExcluded() )
831 {
832 wxString serialized = marker->SerializeToString();
833 ercSettings.m_ErcExclusions.insert( serialized );
834 ercSettings.m_ErcExclusionComments[ serialized ] = marker->GetComment();
835 }
836 }
837 }
838}
839
840
842{
843 SCH_SHEET_LIST sheetList = Hierarchy();
844
845 for( SCH_MARKER* marker : ResolveERCExclusions() )
846 {
847 SCH_SHEET_PATH errorPath;
848 ignore_unused( sheetList.GetItem( marker->GetRCItem()->GetMainItemID(), &errorPath ) );
849
850 if( errorPath.LastScreen() )
851 errorPath.LastScreen()->Append( marker );
852 else
853 RootScreen()->Append( marker );
854 }
855}
856
857
859{
860 return static_cast<EMBEDDED_FILES*>( this );
861}
862
863
865{
866 return static_cast<const EMBEDDED_FILES*>( this );
867}
868
869
871{
872 std::set<KIFONT::OUTLINE_FONT*> fonts;
873
874 SCH_SHEET_LIST sheetList = Hierarchy();
875
876 for( const SCH_SHEET_PATH& sheet : sheetList )
877 {
878 for( SCH_ITEM* item : sheet.LastScreen()->Items() )
879 {
880 if( EDA_TEXT* text = dynamic_cast<EDA_TEXT*>( item ) )
881 {
882 KIFONT::FONT* font = text->GetFont();
883
884 if( !font || font->IsStroke() )
885 continue;
886
887 using EMBEDDING_PERMISSION = KIFONT::OUTLINE_FONT::EMBEDDING_PERMISSION;
888 auto* outline = static_cast<KIFONT::OUTLINE_FONT*>( font );
889
890 if( outline->GetEmbeddingPermission() == EMBEDDING_PERMISSION::EDITABLE
891 || outline->GetEmbeddingPermission() == EMBEDDING_PERMISSION::INSTALLABLE )
892 {
893 fonts.insert( outline );
894 }
895 }
896 }
897 }
898
899 for( KIFONT::OUTLINE_FONT* font : fonts )
900 {
901 auto file = GetEmbeddedFiles()->AddFile( font->GetFileName(), false );
902
903 if( !file )
904 {
905 wxLogTrace( "EMBED", "Failed to add font file: %s", font->GetFileName() );
906 continue;
907 }
908
910 }
911}
912
913
914std::set<const SCH_SCREEN*> SCHEMATIC::GetSchematicsSharedByMultipleProjects() const
915{
916 std::set<const SCH_SCREEN*> retv;
917
918 wxCHECK( m_rootSheet, retv );
919
920 SCH_SHEET_LIST hierarchy( m_rootSheet );
921 SCH_SCREENS screens( m_rootSheet );
922
923 for( const SCH_SCREEN* screen = screens.GetFirst(); screen; screen = screens.GetNext() )
924 {
925 for( const SCH_ITEM* item : screen->Items().OfType( SCH_SYMBOL_T ) )
926 {
927 const SCH_SYMBOL* symbol = static_cast<const SCH_SYMBOL*>( item );
928
929 const std::vector<SCH_SYMBOL_INSTANCE> symbolInstances = symbol->GetInstances();
930
931 for( const SCH_SYMBOL_INSTANCE& instance : symbolInstances )
932 {
933 if( !hierarchy.HasPath( instance.m_Path ) )
934 {
935 retv.insert( screen );
936 break;
937 }
938 }
939
940 if( retv.count( screen ) )
941 break;
942 }
943 }
944
945 return retv;
946}
947
948
950{
951 wxCHECK( m_rootSheet, false );
952
953 SCH_SCREENS screens( m_rootSheet );
954
955 for( const SCH_SCREEN* screen = screens.GetFirst(); screen; screen = screens.GetNext() )
956 {
957 wxCHECK2( screen, continue );
958
959 if( screen->GetRefCount() > 1 )
960 return true;
961 }
962
963 return false;
964}
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:80
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: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: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:914
void SetLegacySymbolInstanceData()
Update the symbol value and footprint instance data for legacy designs.
Definition: schematic.cpp:620
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:766
CONNECTION_GRAPH * m_connectionGraph
Holds and calculates connectivity information of this schematic.
Definition: schematic.h:372
SCH_SHEET_LIST m_hierarchy
Cache of the entire schematic hierarchy sorted by sheet page number.
Definition: schematic.h:393
SCH_SHEET_PATH & CurrentSheet() const override
Definition: schematic.h:156
void ResolveERCExclusionsPostUpdate()
Update markers to match recorded exclusions.
Definition: schematic.cpp:841
void RemoveListener(SCHEMATIC_LISTENER *aListener)
Remove the specified listener.
Definition: schematic.cpp:797
bool IsComplexHierarchy() const
Test if the schematic is a complex hierarchy.
Definition: schematic.cpp:949
void OnSchSheetChanged()
Notify the schematic and its listeners that the current sheet has been changed.
Definition: schematic.cpp:784
SCH_SHEET_PATH * m_currentSheet
The sheet path of the sheet currently being edited or displayed.
Definition: schematic.h:369
wxString GetOperatingPoint(const wxString &aNetName, int aPrecision, const wxString &aRange)
Definition: schematic.cpp:718
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:772
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:408
std::vector< SCH_MARKER * > ResolveERCExclusions()
Definition: schematic.cpp:326
wxString GetFileName() const override
Helper to retrieve the filename from the root sheet screen.
Definition: schematic.cpp:306
void EmbedFonts() override
Embed fonts in the schematic.
Definition: schematic.cpp:870
SCHEMATIC_SETTINGS & Settings() const
Definition: schematic.cpp:312
wxString ConvertKIIDsToRefs(const wxString &aSource) const
Definition: schematic.cpp:570
void RecordERCExclusions()
Scan existing markers and record data from any that are Excluded.
Definition: schematic.cpp:815
std::map< wxString, std::set< int > > & GetPageRefsMap()
Definition: schematic.h:200
void FixupJunctions()
Add junctions to this schematic where required.
Definition: schematic.cpp:738
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:120
wxString ConvertRefsToKIIDs(const wxString &aSource) const
Definition: schematic.cpp:503
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:790
std::map< int, wxString > GetVirtualPageToSheetPagesMap() const
Definition: schematic.cpp:492
EMBEDDED_FILES * GetEmbeddedFiles() override
Definition: schematic.cpp:858
PROJECT * m_project
Definition: schematic.h:358
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:253
std::set< wxString > GetNetClassAssignmentCandidates()
Return the set of netname candidates for netclass assignment.
Definition: schematic.cpp:423
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:673
void InvokeListeners(Func &&aFunc, Args &&... args)
Definition: schematic.h:352
bool IsValid() const
A simple test if the schematic is loaded, not a complete one.
Definition: schematic.h:141
static bool m_IsSchematicExists
True if a SCHEMATIC exists, false if not.
Definition: schematic.h:342
void RemoveAllListeners()
Remove all listeners.
Definition: schematic.cpp:809
void GetContextualTextVars(wxArrayString *aVars) const
Definition: schematic.cpp:228
SCH_SHEET & Root() const
Definition: schematic.h:125
std::map< int, wxString > GetVirtualPageToSheetNamesMap() const
Definition: schematic.cpp:476
SCH_SHEET_LIST BuildSheetListSortedByPageNumbers() const override
Definition: schematic.h:97
wxString GetUniqueFilenameForCurrentSheet()
Definition: schematic.cpp:628
void SetSheetNumberAndCount()
Set the m_ScreenNumber and m_NumberOfScreens members for screens.
Definition: schematic.cpp:644
std::vector< SCHEMATIC_LISTENER * > m_listeners
Currently installed listeners.
Definition: schematic.h:398
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:442
std::map< wxString, double > m_operatingPoints
Simulation operating points for text variable substitution.
Definition: schematic.h:388
ERC_SETTINGS & ErcSettings() const
Definition: schematic.cpp:319
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:778
SCH_SHEET * m_rootSheet
The top-level sheet in this schematic hierarchy (or potentially the only one)
Definition: schematic.h:361
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:579
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:838
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, int aFlags=0) const
Definition: title_block.cpp:95
static void GetContextualTextVars(wxArrayString *aVars)
Definition: title_block.cpp:73
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