KiCad PCB EDA Suite
Loading...
Searching...
No Matches
schematic.h
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
17 * along with this program. If not, see <https://www.gnu.org/licenses/>.
18 */
19
20#ifndef KICAD_SCHEMATIC_H
21#define KICAD_SCHEMATIC_H
22
23#include <eda_item.h>
24#include <embedded_files.h>
26#include <schematic_holder.h>
27#include <sch_sheet_path.h>
28#include <schematic_settings.h>
29#include <project.h>
30
31
33class BUS_ALIAS;
35class EDA_BASE_FRAME;
36class ERC_SETTINGS;
37class PROJECT;
38class SCH_COMMIT;
39class SCH_LINE;
40class SCH_SCREEN;
41class SCH_SHEET;
42class SCH_SHEET_LIST;
43class SCH_GLOBALLABEL;
44class SCH_REFERENCE;
46class TOOL_MANAGER;
48
49namespace KIFONT
50{
51class OUTLINE_FONT;
52}
53
54namespace KIGFX
55{
56class SCH_VIEW;
57}
58
59
60class SCHEMATIC;
61
63{
64public:
66 virtual void OnSchItemsAdded( SCHEMATIC& aSch, std::vector<SCH_ITEM*>& aSchItem ) {}
67 virtual void OnSchItemsRemoved( SCHEMATIC& aSch, std::vector<SCH_ITEM*>& aSchItem ) {}
68 virtual void OnSchItemsChanged( SCHEMATIC& aSch, std::vector<SCH_ITEM*>& aSchItem ) {}
69
70 // This is called when the user changes to a new sheet, not when a sheet is altered.
71 // Sheet alteration events will call OnSchItems*
72 virtual void OnSchSheetChanged( SCHEMATIC& aSch ) {}
73};
74
81
89class SCHEMATIC : public EDA_ITEM, public EMBEDDED_FILES, public PROJECT::_ELEM
90{
91public:
92 SCHEMATIC( PROJECT* aPrj );
93
94 virtual ~SCHEMATIC();
95
96 virtual wxString GetClass() const override
97 {
98 return wxT( "SCHEMATIC" );
99 }
100
102 void Reset();
103
105 PROJECT& Project() const { return *m_project; }
106 void SetProject( PROJECT* aPrj );
107
108 const std::map<wxString, wxString>* GetProperties() { return &m_properties; }
109
111
113
118
124 bool HasHierarchy() const { return !m_hierarchy.empty(); }
125
126 void RefreshHierarchy();
127
128 SCH_ITEM* ResolveItem( const KIID& aID, SCH_SHEET_PATH* aPathOut = nullptr,
129 bool aAllowNullptrReturn = false ) const
130 {
131 return m_hierarchy.ResolveItem( aID, aPathOut, aAllowNullptrReturn );
132 }
133
135 {
136 return *m_rootSheet;
137 }
138
144 std::vector<SCH_SHEET*> GetTopLevelSheets() const;
145
146 SCH_SHEET* GetTopLevelSheet( int aIndex = 0 ) const;
147
148 void SetTopLevelSheets( const std::vector<SCH_SHEET*>& aSheets );
149
155 void AddTopLevelSheet( SCH_SHEET* aSheet );
156
163 bool RemoveTopLevelSheet( SCH_SHEET* aSheet );
164
171 bool IsTopLevelSheet( const SCH_SHEET* aSheet ) const;
172
174 bool IsValid() const
175 {
176 return m_project && m_rootSheet != nullptr;
177 }
178
180 SCH_SCREEN* RootScreen() const;
181
182 void GetContextualTextVars( wxArrayString* aVars ) const;
183
184 bool ResolveTextVar( const SCH_SHEET_PATH* aSheetPath, wxString* token, int aDepth ) const;
185
187 wxString GetFileName() const;
188
190 {
191 return *m_currentSheet;
192 }
193
194 void SetCurrentSheet( const SCH_SHEET_PATH& aPath )
195 {
196 *m_currentSheet = aPath;
197 }
198
200
202 {
203 return m_connectionGraph;
204 }
205
206 const wxString& GetHighlightedNetChain() const { return m_highlightedNetChain; }
207 void SetHighlightedNetChain( const wxString& aNetChain ) { m_highlightedNetChain = aNetChain; }
208
210
211 ERC_SETTINGS& ErcSettings() const;
212
213 std::vector<SCH_MARKER*> ResolveERCExclusions();
214
216 const EMBEDDED_FILES* GetEmbeddedFiles() const;
217
222 std::shared_ptr<BUS_ALIAS> GetBusAlias( const wxString& aLabel ) const;
223
224 void AddBusAlias( std::shared_ptr<BUS_ALIAS> aAlias );
225
226 void SetBusAliases( const std::vector<std::shared_ptr<BUS_ALIAS>>& aAliases );
227
228 const std::vector<std::shared_ptr<BUS_ALIAS>>& GetAllBusAliases() const
229 {
230 return m_busAliases;
231 }
232
239 std::set<wxString> GetNetClassAssignmentCandidates();
240
247 bool ResolveCrossReference( wxString* token, int aDepth ) const;
248
249 std::map<wxString, std::set<int>>& GetPageRefsMap() { return m_labelToPageRefsMap; }
250
251 std::map<int, wxString> GetVirtualPageToSheetNamesMap() const;
252 std::map<int, wxString> GetVirtualPageToSheetPagesMap() const;
253
254 wxString ConvertRefsToKIIDs( const wxString& aSource ) const;
255 wxString ConvertKIIDsToRefs( const wxString& aSource ) const;
256
265
278
285
291
296 {
297 m_operatingPoints.clear();
298 }
299
305 void SetOperatingPoint( const wxString& aNetChain, double aValue )
306 {
307 m_operatingPoints[ aNetChain ] = aValue;
308 }
309
310 wxString GetOperatingPoint( const wxString& aNetName, int aPrecision, const wxString& aRange );
311
319
323 void RecordERCExclusions();
324
329
334 void OnItemsAdded( std::vector<SCH_ITEM*>& aNewItems );
335
340 void OnItemsRemoved( std::vector<SCH_ITEM*>& aRemovedItems );
341
346 void OnItemsChanged( std::vector<SCH_ITEM*>& aItems );
347
354 void OnSchSheetChanged();
355
365 void AddListener( SCHEMATIC_LISTENER* aListener );
366
372 void RemoveListener( SCHEMATIC_LISTENER* aListener );
373
377 void RemoveAllListeners();
378
379 void RunOnNestedEmbeddedFiles( const std::function<void( EMBEDDED_FILES* )>& aFunction ) override;
380
384 void EmbedFonts() override;
385
389 std::set<KIFONT::OUTLINE_FONT*> GetFonts() const override;
390
397 std::set<const SCH_SCREEN*> GetSchematicsSharedByMultipleProjects() const;
398
404 bool IsComplexHierarchy() const;
405
407
415 void CleanUp( SCH_COMMIT* aCommit, SCH_SCREEN* aScreen = nullptr );
416
420 void RecalculateConnections( SCH_COMMIT* aCommit, SCH_CLEANUP_FLAGS aCleanupFlags,
421 TOOL_MANAGER* aToolManager,
422 PROGRESS_REPORTER* aProgressReporter = nullptr,
423 KIGFX::SCH_VIEW* aSchView = nullptr,
424 std::function<void( SCH_ITEM* )>* aChangedItemHandler = nullptr,
425 PICKED_ITEMS_LIST* aLastChangeList = nullptr );
426
434
441 bool Contains( const SCH_REFERENCE& aRef ) const;
442
444
455 wxArrayString GetVariantNamesForUI() const;
456
462 wxString GetCurrentVariant() const;
463
464 void SetCurrentVariant( const wxString& aVariantName );
465
471 void DeleteVariant( const wxString& aVariantName, SCH_COMMIT* aCommit = nullptr );
472
473 void AddVariant( const wxString& aVariantName );
474
484 void RenameVariant( const wxString& aOldName, const wxString& aNewName,
485 SCH_COMMIT* aCommit = nullptr );
486
497 void CopyVariant( const wxString& aSourceVariant, const wxString& aNewVariant,
498 SCH_COMMIT* aCommit = nullptr );
499
503 const std::set<wxString>& GetVariantNames() const { return m_variantNames; }
504
511 wxString GetVariantDescription( const wxString& aVariantName ) const;
512
519 void SetVariantDescription( const wxString& aVariantName, const wxString& aDescription );
520
527 void LoadVariants();
528
533
534#if defined(DEBUG)
535 void Show( int nestLevel, std::ostream& os ) const override {}
536#endif
537
539
549 void SaveToHistory( const wxString& aProjectPath, std::vector<HISTORY_FILE_DATA>& aFileData );
550
551private:
552 friend class SCH_EDIT_FRAME;
553
554 template <typename Func, typename... Args>
555 void InvokeListeners( Func&& aFunc, Args&&... args )
556 {
557 for( auto&& l : m_listeners )
558 ( l->*aFunc )( std::forward<Args>( args )... );
559 }
560
561 void ensureVirtualRoot();
564 void rebuildHierarchyState( bool aResetConnectionGraph );
565
567
570
572 std::vector<SCH_SHEET*> m_topLevelSheets;
573
582
585
587
593 std::map<wxString, std::set<int>> m_labelToPageRefsMap;
594
598 std::map<wxString, wxString> m_properties;
599
603 std::map<wxString, double> m_operatingPoints;
604
609
613 std::vector<SCHEMATIC_LISTENER*> m_listeners;
614
619
621
623
624 std::vector<std::shared_ptr<BUS_ALIAS>> m_busAliases;
625
627
628 std::set<wxString> m_variantNames;
629
633
636 std::unique_ptr<class SCHEMATIC_TEXT_VAR_ADAPTER> m_textVarAdapter;
637
642
643public:
645};
646
647#endif
Calculate the connectivity of a schematic and generates netlists.
The base frame for deriving all KiCad main window classes.
EDA_ITEM(EDA_ITEM *parent, KICAD_T idType, bool isSCH_ITEM=false, bool isBOARD_ITEM=false)
Definition eda_item.cpp:37
EMBEDDED_FILES()=default
Container for ERC settings.
Class OUTLINE_FONT implements outline font drawing.
Definition kiid.h:44
A holder to handle information on schematic or board items.
A progress reporter interface for use in multi-threaded environments.
A PROJECT can hold stuff it knows nothing about, in the form of _ELEM derivatives.
Definition project.h:89
Container for project specific data.
Definition project.h:62
ELEM
The set of #_ELEMs that a PROJECT can hold.
Definition project.h:68
Move-only RAII wrapper around a single listener registration.
This is a bridge class to help the schematic be able to affect SCH_EDIT_FRAME without doing anything ...
virtual void OnSchItemsRemoved(SCHEMATIC &aSch, std::vector< SCH_ITEM * > &aSchItem)
Definition schematic.h:67
virtual ~SCHEMATIC_LISTENER()
Definition schematic.h:65
virtual void OnSchItemsChanged(SCHEMATIC &aSch, std::vector< SCH_ITEM * > &aSchItem)
Definition schematic.h:68
virtual void OnSchSheetChanged(SCHEMATIC &aSch)
Definition schematic.h:72
virtual void OnSchItemsAdded(SCHEMATIC &aSch, std::vector< SCH_ITEM * > &aSchItem)
Definition schematic.h:66
These are loaded from Eeschema settings but then overwritten by the project settings.
Bridges SCHEMATIC's listener stream into the generic TEXT_VAR_TRACKER.
Holds all the data relating to one schematic.
Definition schematic.h:90
void SetCurrentVariant(const wxString &aVariantName)
void SetCurrentSheet(const SCH_SHEET_PATH &aPath)
Definition schematic.h:194
bool m_settingTopLevelSheets
Re-entry guard to prevent infinite recursion between ensureDefaultTopLevelSheet and RefreshHierarchy ...
Definition schematic.h:632
void Reset()
Initialize this schematic to a blank one, unloading anything existing.
std::set< const SCH_SCREEN * > GetSchematicsSharedByMultipleProjects() const
Return a list of schematic files in the current project that contain instance data for multiple proje...
void CreateDefaultScreens()
void SetLegacySymbolInstanceData()
Update the symbol value and footprint instance data for legacy designs.
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.
CONNECTION_GRAPH * m_connectionGraph
Hold and calculate connectivity information of this schematic.
Definition schematic.h:584
virtual wxString GetClass() const override
Return the class name.
Definition schematic.h:96
bool IsTopLevelSheet(const SCH_SHEET *aSheet) const
Check if a sheet is a top-level sheet (direct child of virtual root).
void loadBusAliasesFromProject()
void AddTopLevelSheet(SCH_SHEET *aSheet)
Add a new top-level sheet to the schematic.
SCH_SHEET_LIST m_hierarchy
Cache of the entire schematic hierarchy sorted by sheet page number.
Definition schematic.h:608
void rebuildHierarchyState(bool aResetConnectionGraph)
void ResolveERCExclusionsPostUpdate()
Update markers to match recorded exclusions.
void DeleteVariant(const wxString &aVariantName, SCH_COMMIT *aCommit=nullptr)
Delete all information for aVariantName.
void RecomputeIntersheetRefs()
Update the schematic's page reference map for all global labels, and refresh the labels so that they ...
void CacheExistingAnnotation()
Store all existing annotations in the REFDES_TRACKER.
wxString GetVariantDescription(const wxString &aVariantName) const
Return the description for a variant.
void LoadVariants()
This is a throw away method for variant testing.
SCH_SHEET_LIST BuildSheetListSortedByPageNumbers() const
void RemoveListener(SCHEMATIC_LISTENER *aListener)
Remove the specified listener.
const std::vector< std::shared_ptr< BUS_ALIAS > > & GetAllBusAliases() const
Definition schematic.h:228
const wxString & GetHighlightedNetChain() const
Definition schematic.h:206
bool IsComplexHierarchy() const
Test if the schematic is a complex hierarchy.
void OnSchSheetChanged()
Notify the schematic and its listeners that the current sheet has been changed.
wxString GetFileName() const
Helper to retrieve the filename from the root sheet screen.
void SetSchematicHolder(SCHEMATIC_HOLDER *aHolder)
Definition schematic.h:406
SCH_SHEET_PATH * m_currentSheet
The sheet path of the sheet currently being edited or displayed.
Definition schematic.h:581
std::vector< SCH_SHEET * > m_topLevelSheets
List of top-level sheets (direct children of virtual root)
Definition schematic.h:572
wxString GetOperatingPoint(const wxString &aNetName, int aPrecision, const wxString &aRange)
void CleanUp(SCH_COMMIT *aCommit, SCH_SCREEN *aScreen=nullptr)
Perform routine schematic cleaning including breaking wire and buses and deleting identical objects s...
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.
void AddVariant(const wxString &aVariantName)
int FixupJunctionsAfterImport()
Add junctions to this schematic where required.
virtual ~SCHEMATIC()
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.
void CopyVariant(const wxString &aSourceVariant, const wxString &aNewVariant, SCH_COMMIT *aCommit=nullptr)
Copy a variant from aSourceVariant to aNewVariant.
std::vector< SCH_MARKER * > ResolveERCExclusions()
void SaveToHistory(const wxString &aProjectPath, std::vector< HISTORY_FILE_DATA > &aFileData)
Serialize schematic sheets into HISTORY_FILE_DATA for non-blocking history commit.
void EmbedFonts() override
Embed fonts in the schematic.
SCHEMATIC_SETTINGS & Settings() const
void ClearOperatingPoints()
Clear operating points from a .op simulation.
Definition schematic.h:295
SCH_SCREEN * GetCurrentScreen() const
Definition schematic.h:199
wxString ConvertKIIDsToRefs(const wxString &aSource) const
SCH_ITEM * ResolveItem(const KIID &aID, SCH_SHEET_PATH *aPathOut=nullptr, bool aAllowNullptrReturn=false) const
Definition schematic.h:128
void ensureVirtualRoot()
void ensureCurrentSheetIsTopLevel()
void RecordERCExclusions()
Scan existing markers and record data from any that are Excluded.
SCH_SHEET_LIST Hierarchy() const
Return the full schematic flattened hierarchical sheet list.
wxString m_currentVariant
Definition schematic.h:626
std::map< wxString, std::set< int > > & GetPageRefsMap()
Definition schematic.h:249
SCH_SHEET_LIST BuildUnorderedSheetList() const
void RenameVariant(const wxString &aOldName, const wxString &aNewName, SCH_COMMIT *aCommit=nullptr)
Rename a variant from aOldName to aNewName.
class SCHEMATIC_TEXT_VAR_ADAPTER * GetTextVarAdapter() const
Definition schematic.h:644
void SetHighlightedNetChain(const wxString &aNetChain)
Definition schematic.h:207
PROJECT & Project() const
Return a reference to the project this schematic is part of.
Definition schematic.h:105
std::map< wxString, std::set< int > > m_labelToPageRefsMap
Holds a map of labels to the page sequence (virtual page number) that they appear on.
Definition schematic.h:593
bool HasHierarchy() const
Check if the hierarchy has been built.
Definition schematic.h:124
std::set< KIFONT::OUTLINE_FONT * > GetFonts() const override
Get a set of fonts used in the schematic.
SCH_SHEET * GetTopLevelSheet(int aIndex=0) const
bool RemoveTopLevelSheet(SCH_SHEET *aSheet)
Remove a top-level sheet from the schematic.
bool Contains(const SCH_REFERENCE &aRef) const
Check if the schematic contains the specified reference.
wxString ConvertRefsToKIIDs(const wxString &aSource) const
void SetProject(PROJECT *aPrj)
wxString GetCurrentVariant() const
Return the current variant being edited.
void AddListener(SCHEMATIC_LISTENER *aListener)
Add a listener to the schematic to receive calls whenever something on the schematic has been modifie...
std::map< int, wxString > GetVirtualPageToSheetPagesMap() const
EMBEDDED_FILES * GetEmbeddedFiles() override
PROJECT * m_project
Definition schematic.h:566
CONNECTION_GRAPH * ConnectionGraph() const
Definition schematic.h:201
SCH_SCREEN * RootScreen() const
Helper to retrieve the screen of the root sheet.
SCHEMATIC(PROJECT *aPrj)
Definition schematic.cpp:72
bool ResolveTextVar(const SCH_SHEET_PATH *aSheetPath, wxString *token, int aDepth) const
std::set< wxString > GetNetClassAssignmentCandidates()
Return the set of netname candidates for netclass assignment.
std::vector< std::shared_ptr< BUS_ALIAS > > m_busAliases
Definition schematic.h:624
void InvokeListeners(Func &&aFunc, Args &&... args)
Definition schematic.h:555
bool IsValid() const
A simple test if the schematic is loaded, not a complete one.
Definition schematic.h:174
void updateProjectBusAliases()
void SetVariantDescription(const wxString &aVariantName, const wxString &aDescription)
Set the description for a variant.
static bool m_IsSchematicExists
True if a SCHEMATIC exists, false if not.
Definition schematic.h:532
void RemoveAllListeners()
Remove all listeners.
void SetTopLevelSheets(const std::vector< SCH_SHEET * > &aSheets)
std::unique_ptr< class SCHEMATIC_TEXT_VAR_ADAPTER > m_textVarAdapter
Reactive text-variable dependency adapter.
Definition schematic.h:636
PROPERTY_LISTENER_SUBSCRIPTION m_fieldListenerSubscription
PROPERTY_MANAGER listener subscription installed in the ctor.
Definition schematic.h:641
const std::set< wxString > & GetVariantNames() const
Return the set of variant names (without the default placeholder).
Definition schematic.h:503
const std::map< wxString, wxString > * GetProperties()
Definition schematic.h:108
void GetContextualTextVars(wxArrayString *aVars) const
void SetOperatingPoint(const wxString &aNetChain, double aValue)
Set operating points from a .op simulation.
Definition schematic.h:305
friend class SCH_EDIT_FRAME
Definition schematic.h:552
void ensureDefaultTopLevelSheet()
SCH_SHEET & Root() const
Definition schematic.h:134
std::map< int, wxString > GetVirtualPageToSheetNamesMap() const
void AddBusAlias(std::shared_ptr< BUS_ALIAS > aAlias)
std::vector< SCH_SHEET * > GetTopLevelSheets() const
Get the list of top-level sheets.
wxArrayString GetVariantNamesForUI() const
Return an array of variant names for using in wxWidgets UI controls.
void RunOnNestedEmbeddedFiles(const std::function< void(EMBEDDED_FILES *)> &aFunction) override
Provide access to nested embedded files, such as symbols in schematics and footprints in boards.
SCHEMATIC_HOLDER * m_schematicHolder
What currently "Holds" the schematic, i.e.
Definition schematic.h:618
wxString GetUniqueFilenameForCurrentSheet()
Get the unique file name for the current sheet.
void SetSheetNumberAndCount()
Set the m_ScreenNumber and m_NumberOfScreens members for screens.
void SetBusAliases(const std::vector< std::shared_ptr< BUS_ALIAS > > &aAliases)
std::vector< SCHEMATIC_LISTENER * > m_listeners
Currently installed listeners.
Definition schematic.h:613
SCH_SHEET_PATH & CurrentSheet() const
Definition schematic.h:189
bool ResolveCrossReference(wxString *token, int aDepth) const
Resolves text vars that refer to other items.
void RecalculateConnections(SCH_COMMIT *aCommit, SCH_CLEANUP_FLAGS aCleanupFlags, TOOL_MANAGER *aToolManager, PROGRESS_REPORTER *aProgressReporter=nullptr, KIGFX::SCH_VIEW *aSchView=nullptr, std::function< void(SCH_ITEM *)> *aChangedItemHandler=nullptr, PICKED_ITEMS_LIST *aLastChangeList=nullptr)
Generate the connection data for the entire schematic hierarchy.
wxString m_highlightedNetChain
Definition schematic.h:586
std::map< wxString, double > m_operatingPoints
Simulation operating points for text variable substitution.
Definition schematic.h:603
std::map< wxString, wxString > m_properties
Properties for text variable substitution (and perhaps other uses in future).
Definition schematic.h:598
ERC_SETTINGS & ErcSettings() const
std::set< wxString > m_variantNames
Definition schematic.h:628
void RefreshHierarchy()
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.
PROJECT::ELEM ProjectElementType() override
Definition schematic.h:538
SCH_SHEET * m_rootSheet
The virtual root sheet (has no screen, contains all top-level sheets)
Definition schematic.h:569
Base class for any item which can be embedded within the SCHEMATIC container class,...
Definition sch_item.h:162
Segment description base class to describe items which have 2 end points (track, wire,...
Definition sch_line.h:38
A helper to define a symbol's reference designator in a schematic.
A container for handling SCH_SHEET_PATH objects in a flattened hierarchy.
Handle access to a stack of flattened SCH_SHEET objects by way of a path for creating a flattened sch...
SCH_SCREEN * LastScreen()
Sheet symbol placed in a schematic, and is the entry point for a sub schematic.
Definition sch_sheet.h:44
Master controller class:
The Cairo implementation of the graphics abstraction layer.
Definition eda_group.h:29
Definition of the SCH_SHEET_PATH and SCH_SHEET_LIST classes for Eeschema.
SCH_CLEANUP_FLAGS
Definition schematic.h:76
@ LOCAL_CLEANUP
Definition schematic.h:78
@ NO_CLEANUP
Definition schematic.h:77
@ GLOBAL_CLEANUP
Definition schematic.h:79
Data produced by a registered saver on the UI thread, consumed by either the background local-history...