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, 2024 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
26#include <connection_graph.h>
27#include <locale_io.h>
28#include <schematic.h>
29#include <sch_edit_frame.h>
30#include <sch_label.h>
31#include <sch_io/sch_io.h>
32#include <sch_io/sch_io_mgr.h>
35
36#include <wx/app.h>
37
38
41
42
44{
45 s_SchEditFrame = aSchEditFrame;
46}
47
48
50{
52 {
53 if( s_SchEditFrame )
54 {
56 }
57 else
58 {
60 }
61 }
62
63 return s_SettingsManager;
64}
65
66
68{
69 // For some reasons, LoadProject() needs a C locale, so ensure we have the right locale
70 // This is mainly when running QA Python tests
72
74
75 if( !project )
76 {
77 GetSettingsManager()->LoadProject( "", aSetActive );
79 }
80
81 return project;
82}
83
84
85SCHEMATIC* EESCHEMA_HELPERS::LoadSchematic( const wxString& aFileName,
86 bool aSetActive,
87 bool aForceDefaultProject,
88 PROJECT* aProject )
89{
90 if( aFileName.EndsWith( FILEEXT::KiCadSchematicFileExtension ) )
91 return LoadSchematic( aFileName, SCH_IO_MGR::SCH_KICAD, aSetActive, aForceDefaultProject, aProject );
92 else if( aFileName.EndsWith( FILEEXT::LegacySchematicFileExtension ) )
93 return LoadSchematic( aFileName, SCH_IO_MGR::SCH_LEGACY, aSetActive, aForceDefaultProject, aProject );
94
95 // as fall back for any other kind use the legacy format
96 return LoadSchematic( aFileName, SCH_IO_MGR::SCH_LEGACY, aSetActive, aForceDefaultProject, aProject );
97}
98
99
100SCHEMATIC* EESCHEMA_HELPERS::LoadSchematic( const wxString& aFileName, SCH_IO_MGR::SCH_FILE_T aFormat,
101 bool aSetActive,
102 bool aForceDefaultProject,
103 PROJECT* aProject )
104{
105 wxFileName pro = aFileName;
106 pro.SetExt( FILEEXT::ProjectFileExtension );
107 pro.MakeAbsolute();
108 wxString projectPath = pro.GetFullPath();
109
110 // Ensure the "C" locale is temporary set, before reading any file
111 // It also avoid wxWidget alerts about locale issues, later, when using Python 3
113
114 PROJECT* project = aProject;
115
116 if( !project )
117 {
118 project = GetSettingsManager()->GetProject( projectPath );
119 }
120
121 if( !aForceDefaultProject )
122 {
123 if( !project )
124 {
125 if( wxFileExists( projectPath ) )
126 {
127 GetSettingsManager()->LoadProject( projectPath, aSetActive );
128 project = GetSettingsManager()->GetProject( projectPath );
129 }
130 }
131 else if( s_SchEditFrame && project == &GetSettingsManager()->Prj() )
132 {
133 // Project is already loaded? Then so is the board
134 return &s_SchEditFrame->Schematic();
135 }
136 }
137
138 // Board cannot be loaded without a project, so create the default project
139 if( !project || aForceDefaultProject )
140 project = GetDefaultProject( aSetActive );
141
142 IO_RELEASER<SCH_IO> pi( SCH_IO_MGR::FindPlugin( aFormat ) );
143
144 SCHEMATIC* schematic = new SCHEMATIC( project );
145
146 SCH_SHEET* rootSheet = new SCH_SHEET( schematic );
147 schematic->SetRoot( rootSheet );
148
149 SCH_SCREEN* rootScreen = new SCH_SCREEN( schematic );
150 const_cast<KIID&>( rootSheet->m_Uuid ) = rootScreen->GetUuid();
151 schematic->Root().SetScreen( rootScreen );
152
153
154 schematic->RootScreen()->SetFileName( wxEmptyString );
155
156 // Don't leave root page number empty
157 schematic->RootScreen()->SetPageNumber( wxT( "1" ) );
158
159 wxFileName schFile = aFileName;
160 schFile.MakeAbsolute();
161
162 try
163 {
164 schematic->SetRoot( pi->LoadSchematicFile( schFile.GetFullPath(), schematic ) );
165 }
166 catch( ... )
167 {
168 return nullptr;
169 }
170
171 SCH_SHEET_LIST sheetList = schematic->BuildSheetListSortedByPageNumbers();
172 SCH_SCREENS screens( schematic->Root() );
173
174 for( SCH_SCREEN* screen = screens.GetFirst(); screen; screen = screens.GetNext() )
175 screen->UpdateLocalLibSymbolLinks();
176
177 if( schematic->RootScreen()->GetFileFormatVersionAtLoad() < 20221002 )
178 sheetList.UpdateSymbolInstanceData( schematic->RootScreen()->GetSymbolInstances());
179
180 sheetList.UpdateSheetInstanceData( schematic->RootScreen()->GetSheetInstances());
181
182 if( schematic->RootScreen()->GetFileFormatVersionAtLoad() < 20230221 )
184
185 for( SCH_SCREEN* screen = screens.GetFirst(); screen; screen = screens.GetNext() )
186 screen->MigrateSimModels();
187
188 sheetList.AnnotatePowerSymbols();
189
190 schematic->ConnectionGraph()->Reset();
191
192 schematic->SetSheetNumberAndCount();
193 schematic->RecomputeIntersheetRefs( []( SCH_GLOBALLABEL* aGlobal )
194 {
195 for( SCH_FIELD& field : aGlobal->GetFields() )
196 field.ClearBoundingBoxCache();
197
198 aGlobal->ClearBoundingBoxCache();
199 } );
200
201 for( SCH_SHEET_PATH& sheet : sheetList )
202 {
203 sheet.UpdateAllScreenReferences();
204 sheet.LastScreen()->TestDanglingEnds( nullptr, nullptr );
205 }
206
207 schematic->ConnectionGraph()->Recalculate( sheetList, true );
208
209
210 return schematic;
211}
void SetPageNumber(const wxString &aPageNumber)
Definition: base_screen.h:79
void Recalculate(const SCH_SHEET_LIST &aSheetList, bool aUnconditional=false, std::function< void(SCH_ITEM *)> *aChangedItemHandler=nullptr)
Update the connection graph for the given list of sheets.
SETTINGS_MANAGER * GetSettingsManager() const
const KIID m_Uuid
Definition: eda_item.h:489
virtual void ClearBoundingBoxCache()
Definition: eda_text.cpp:550
static SETTINGS_MANAGER * s_SettingsManager
static void SetSchEditFrame(SCH_EDIT_FRAME *aSchEditFrame)
static SETTINGS_MANAGER * GetSettingsManager()
static SCHEMATIC * LoadSchematic(const wxString &aFileName, bool aSetActive, bool aForceDefaultProject, PROJECT *aProject=nullptr)
static SCH_EDIT_FRAME * s_SchEditFrame
static PROJECT * GetDefaultProject(bool aSetActive)
Definition: kiid.h:49
Instantiate the current locale within a scope in which you are expecting exceptions to be thrown.
Definition: locale_io.h:49
Container for project specific data.
Definition: project.h:64
Holds all the data relating to one schematic.
Definition: schematic.h:77
CONNECTION_GRAPH * ConnectionGraph() const override
Definition: schematic.h:162
void SetRoot(SCH_SHEET *aRootSheet)
Initialize the schematic with a new root sheet.
Definition: schematic.cpp:194
SCH_SCREEN * RootScreen() const
Helper to retrieve the screen of the root sheet.
Definition: schematic.cpp:208
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
SCH_SHEET & Root() const
Definition: schematic.h:121
SCH_SHEET_LIST BuildSheetListSortedByPageNumbers() const override
Definition: schematic.h:97
void SetSheetNumberAndCount()
Set the m_ScreenNumber and m_NumberOfScreens members for screens.
Definition: schematic.cpp:646
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:201
Container class that holds multiple SCH_SCREEN objects in a hierarchy.
Definition: sch_screen.h:712
SCH_SCREEN * GetNext()
void FixLegacyPowerSymbolMismatches()
Fix legacy power symbols that have mismatched value text fields and invisible power pin names.
SCH_SCREEN * GetFirst()
const KIID & GetUuid() const
Definition: sch_screen.h:531
void SetFileName(const wxString &aFileName)
Set the file name for this screen to aFileName.
Definition: sch_screen.cpp:118
const std::vector< SCH_SYMBOL_INSTANCE > & GetSymbolInstances() const
Definition: sch_screen.h:521
int GetFileFormatVersionAtLoad() const
Definition: sch_screen.h:128
const std::vector< SCH_SHEET_INSTANCE > & GetSheetInstances() const
Definition: sch_screen.h:526
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...
Sheet symbol placed in a schematic, and is the entry point for a sub schematic.
Definition: sch_sheet.h:57
void SetScreen(SCH_SCREEN *aScreen)
Set the SCH_SCREEN associated with this sheet to aScreen.
Definition: sch_sheet.cpp:173
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.
static const std::string LegacySchematicFileExtension
static const std::string ProjectFileExtension
static const std::string KiCadSchematicFileExtension
std::unique_ptr< T > IO_RELEASER
Helper to hold and release an IO_BASE object when exceptions are thrown.
Definition: io_mgr.h:33
PROJECT & Prj()
Definition: kicad.cpp:595
PROJECT * GetDefaultProject()
std::vector< FAB_LAYER_COLOR > dummy
Definition of file extensions used in Kicad.