KiCad PCB EDA Suite
Loading...
Searching...
No Matches
eeschema_helpers.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) 2022 KiCad Developers, see AUTHORS.TXT for contributors.
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version 3
9 * of the License, or (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU 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, you may find one here:
18 * http://www.gnu.org/licenses/gpl-3.0.html
19 * or you may search the http://www.gnu.org website for the version 3 license,
20 * or you may write to the Free Software Foundation, Inc.,
21 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
22 */
23
24#include <eeschema_helpers.h>
25#include <sch_edit_frame.h>
28#include <schematic.h>
29#include <sch_io_mgr.h>
30#include <locale_io.h>
31#include <wx/app.h>
32#include <sch_label.h>
33#include <connection_graph.h>
34
37
38
40{
41 s_SchEditFrame = aSchEditFrame;
42}
43
44
46{
48 {
49 if( s_SchEditFrame )
50 {
52 }
53 else
54 {
56 }
57 }
58
59 return s_SettingsManager;
60}
61
62
64{
65 // For some reasons, LoadProject() needs a C locale, so ensure we have the right locale
66 // This is mainly when running QA Python tests
68
70
71 if( !project )
72 {
75 }
76
77 return project;
78}
79
80
82{
83 if( aFileName.EndsWith( KiCadSchematicFileExtension ) )
84 return LoadSchematic( aFileName, SCH_IO_MGR::SCH_KICAD );
85 else if( aFileName.EndsWith( LegacySchematicFileExtension ) )
86 return LoadSchematic( aFileName, SCH_IO_MGR::SCH_LEGACY );
87
88 // as fall back for any other kind use the legacy format
89 return LoadSchematic( aFileName, SCH_IO_MGR::SCH_LEGACY );
90}
91
92
93SCHEMATIC* EESCHEMA_HELPERS::LoadSchematic( wxString& aFileName, SCH_IO_MGR::SCH_FILE_T aFormat )
94{
95 wxFileName pro = aFileName;
96 pro.SetExt( ProjectFileExtension );
97 pro.MakeAbsolute();
98 wxString projectPath = pro.GetFullPath();
99
100 // Ensure the "C" locale is temporary set, before reading any file
101 // It also avoid wxWidget alerts about locale issues, later, when using Python 3
103
104 PROJECT* project = GetSettingsManager()->GetProject( projectPath );
105
106 if( !project )
107 {
108 if( wxFileExists( projectPath ) )
109 {
110 GetSettingsManager()->LoadProject( projectPath, false );
111 project = GetSettingsManager()->GetProject( projectPath );
112 }
113 }
114 else if( s_SchEditFrame && project == &GetSettingsManager()->Prj() )
115 {
116 // Project is already loaded? Then so is the board
117 return &s_SchEditFrame->Schematic();
118 }
119
120 // Board cannot be loaded without a project, so create the default project
121 if( !project )
123
124 SCH_PLUGIN* plugin = SCH_IO_MGR::FindPlugin( aFormat );
126
127 SCHEMATIC* schematic = new SCHEMATIC( project );
128
129 try
130 {
131 schematic->SetRoot( pi->Load( aFileName, schematic ) );
132 }
133 catch( ... )
134 {
135 return nullptr;
136 }
137
138 SCH_SHEET_LIST sheetList = schematic->GetSheets();
139 SCH_SCREENS screens( schematic->Root() );
140
141 for( SCH_SCREEN* screen = screens.GetFirst(); screen; screen = screens.GetNext() )
142 screen->UpdateLocalLibSymbolLinks();
143
144 if( schematic->RootScreen()->GetFileFormatVersionAtLoad() < 20221002 )
145 sheetList.UpdateSymbolInstanceData( schematic->RootScreen()->GetSymbolInstances());
146
147 sheetList.UpdateSheetInstanceData( schematic->RootScreen()->GetSheetInstances());
148
149 if( schematic->RootScreen()->GetFileFormatVersionAtLoad() < 20230221 )
151
152 for( SCH_SCREEN* screen = screens.GetFirst(); screen; screen = screens.GetNext() )
153 screen->MigrateSimModels();
154
155 sheetList.AnnotatePowerSymbols();
156
157 schematic->ConnectionGraph()->Reset();
158
159 schematic->SetSheetNumberAndCount();
160 schematic->RecomputeIntersheetRefs( []( SCH_GLOBALLABEL* aGlobal )
161 {
162 for( SCH_FIELD& field : aGlobal->GetFields() )
163 field.ClearBoundingBoxCache();
164
165 aGlobal->ClearBoundingBoxCache();
166 } );
167
168 for( SCH_SHEET_PATH& sheet : sheetList )
169 {
170 sheet.UpdateAllScreenReferences();
171 sheet.LastScreen()->TestDanglingEnds( nullptr, nullptr );
172 }
173
174 schematic->ConnectionGraph()->Recalculate( sheetList, true );
175
176
177 return schematic;
178}
void Recalculate(const SCH_SHEET_LIST &aSheetList, bool aUnconditional=false, std::function< void(SCH_ITEM *)> *aChangedItemHandler=nullptr)
Updates the connection graph for the given list of sheets.
SETTINGS_MANAGER * GetSettingsManager() const
virtual void ClearBoundingBoxCache()
Definition: eda_text.cpp:464
static PROJECT * GetDefaultProject()
static SETTINGS_MANAGER * s_SettingsManager
static SCHEMATIC * LoadSchematic(wxString &aFileName)
static void SetSchEditFrame(SCH_EDIT_FRAME *aSchEditFrame)
static SETTINGS_MANAGER * GetSettingsManager()
static SCH_EDIT_FRAME * s_SchEditFrame
Instantiate the current locale within a scope in which you are expecting exceptions to be thrown.
Definition: locale_io.h:41
Container for project specific data.
Definition: project.h:64
Holds all the data relating to one schematic.
Definition: schematic.h:72
CONNECTION_GRAPH * ConnectionGraph() const override
Definition: schematic.h:143
SCH_SHEET_LIST GetSheets() const override
Builds and returns an updated schematic hierarchy TODO: can this be cached?
Definition: schematic.h:97
void SetRoot(SCH_SHEET *aRootSheet)
Initialize the schematic with a new root sheet.
Definition: schematic.cpp:109
SCH_SCREEN * RootScreen() const
Helper to retrieve the screen of the root sheet.
Definition: schematic.cpp:122
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:568
SCH_SHEET & Root() const
Definition: schematic.h:102
void SetSheetNumberAndCount()
Set the m_ScreenNumber and m_NumberOfScreens members for screens.
Definition: schematic.cpp:539
Schematic editor (Eeschema) main window.
SCHEMATIC & Schematic() const
Instances are attached to a symbol or sheet and provide a place for the symbol's value,...
Definition: sch_field.h:51
std::vector< SCH_FIELD > & GetFields()
Definition: sch_label.h:90
Helper object to release a SCH_PLUGIN in the context of a potential thrown exception through its dest...
Definition: sch_io_mgr.h:536
Base class that schematic file and library loading and saving plugins should derive from.
Definition: sch_io_mgr.h:156
virtual SCH_SHEET * Load(const wxString &aFileName, SCHEMATIC *aSchematic, SCH_SHEET *aAppendToMe=nullptr, const STRING_UTF8_MAP *aProperties=nullptr)
Load information from some input file format that this SCH_PLUGIN implementation knows about,...
Definition: sch_plugin.cpp:41
Container class that holds multiple SCH_SCREEN objects in a hierarchy.
Definition: sch_screen.h:662
SCH_SCREEN * GetNext()
void FixLegacyPowerSymbolMismatches()
Fix legacy power symbols that have mismatched value text fields and invisible power pin names.
SCH_SCREEN * GetFirst()
const std::vector< SCH_SYMBOL_INSTANCE > & GetSymbolInstances() const
Definition: sch_screen.h:516
int GetFileFormatVersionAtLoad() const
Definition: sch_screen.h:129
const std::vector< SCH_SHEET_INSTANCE > & GetSheetInstances() const
Definition: sch_screen.h:521
A container for handling SCH_SHEET_PATH objects in a flattened hierarchy.
void UpdateSheetInstanceData(const std::vector< SCH_SHEET_INSTANCE > &aSheetInstances)
Update all of the sheet instance information using aSheetInstances.
void AnnotatePowerSymbols()
Silently annotate the not yet annotated power symbols of the entire hierarchy of the sheet path list.
void UpdateSymbolInstanceData(const std::vector< SCH_SYMBOL_INSTANCE > &aSymbolInstances)
Update all of the symbol instance information using aSymbolInstances.
Handle access to a stack of flattened SCH_SHEET objects by way of a path for creating a flattened sch...
bool LoadProject(const wxString &aFullPath, bool aSetActive=true)
Loads a project or sets up a new project with a specified path.
PROJECT * GetProject(const wxString &aFullPath) const
Retrieves a loaded project by name.
const std::string LegacySchematicFileExtension
const std::string ProjectFileExtension
const std::string KiCadSchematicFileExtension
PROJECT & Prj()
Definition: kicad.cpp:565
std::vector< FAB_LAYER_COLOR > dummy
Definition of file extensions used in Kicad.