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 The 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 <algorithm>
27
28#include <connection_graph.h>
29#include <locale_io.h>
31#include <schematic.h>
32#include <sch_commit.h>
33#include <sch_edit_frame.h>
34#include <sch_label.h>
35#include <sch_io/sch_io.h>
36#include <sch_io/sch_io_mgr.h>
39#include <tool/tool_manager.h>
40#include <kiface_base.h>
41
42#include <wx/app.h>
43
44
46
47
49{
50 s_SchEditFrame = aSchEditFrame;
51}
52
53
54SCHEMATIC* EESCHEMA_HELPERS::LoadSchematic( const wxString& aFileName, bool aSetActive,
55 bool aForceDefaultProject, PROJECT* aProject, bool aCalculateConnectivity )
56{
57 if( aFileName.EndsWith( FILEEXT::KiCadSchematicFileExtension ) )
58 return LoadSchematic( aFileName, SCH_IO_MGR::SCH_KICAD, aSetActive, aForceDefaultProject,
59 aProject, aCalculateConnectivity );
60 else if( aFileName.EndsWith( FILEEXT::LegacySchematicFileExtension ) )
61 return LoadSchematic( aFileName, SCH_IO_MGR::SCH_LEGACY, aSetActive, aForceDefaultProject,
62 aProject, aCalculateConnectivity );
63
64 // as fall back for any other kind use the legacy format
65 return LoadSchematic( aFileName, SCH_IO_MGR::SCH_LEGACY, aSetActive, aForceDefaultProject, aProject,
66 aCalculateConnectivity );
67}
68
69
70SCHEMATIC* EESCHEMA_HELPERS::LoadSchematic( const wxString& aFileName,
71 SCH_IO_MGR::SCH_FILE_T aFormat,
72 bool aSetActive,
73 bool aForceDefaultProject,
74 PROJECT* aProject,
75 bool aCalculateConnectivity )
76{
77 wxFileName pro = aFileName;
79 pro.MakeAbsolute();
80 wxString projectPath = pro.GetFullPath();
81
82 // Ensure the "C" locale is temporary set, before reading any file
83 // It also avoid wxWidget alerts about locale issues, later, when using Python 3
85
86 PROJECT* project = aProject;
88
89 if( !project )
90 {
91 project = mgr.GetProject( projectPath );
92 }
93
94 if( !aForceDefaultProject )
95 {
96 if( !project )
97 {
98 if( wxFileExists( projectPath ) )
99 {
100 mgr.LoadProject( projectPath, aSetActive );
101 project = mgr.GetProject( projectPath );
102 }
103 }
104 else if( s_SchEditFrame && project == &mgr.Prj() )
105 {
106 // Project is already loaded? Then so is the board
107 return &s_SchEditFrame->Schematic();
108 }
109 }
110
111 // Board cannot be loaded without a project, so create the default project
112 if( !project || aForceDefaultProject )
113 project = &mgr.Prj();
114
115 IO_RELEASER<SCH_IO> pi( SCH_IO_MGR::FindPlugin( aFormat ) );
116
117 SCHEMATIC* schematic = new SCHEMATIC( project );
118 schematic->CreateDefaultScreens();
119
120 wxFileName schFile = aFileName;
121 schFile.MakeAbsolute();
122
123 try
124 {
125 SCH_SHEET* rootSheet = pi->LoadSchematicFile( schFile.GetFullPath(), schematic );
126
127 if( !rootSheet )
128 return nullptr;
129
130 std::vector<SCH_SHEET*> topLevelSheets = schematic->GetTopLevelSheets();
131 bool rootIsTopLevel = std::find( topLevelSheets.begin(), topLevelSheets.end(), rootSheet )
132 != topLevelSheets.end();
133 bool rootIsVirtualRoot = rootSheet == &schematic->Root() || rootSheet->IsVirtualRootSheet();
134
135 if( !rootIsTopLevel && !rootIsVirtualRoot )
136 schematic->SetTopLevelSheets( { rootSheet } );
137
138 // Make ${SHEETNAME} work on the root sheet until we properly support naming the root
139 // sheet. Prefer the display name from the matching schematic.top_level_sheets entry in
140 // the project file so CLI/API exports show the same name the GUI does.
141 if( rootSheet->GetName().IsEmpty() )
142 {
143 wxString rootName = _( "Root" );
144
145 for( const TOP_LEVEL_SHEET_INFO& info : project->GetProjectFile().GetTopLevelSheets() )
146 {
147 wxFileName candidate( project->GetProjectPath(), info.filename );
148
149 if( candidate.SameAs( schFile ) && !info.name.IsEmpty() )
150 {
151 rootName = info.name;
152 break;
153 }
154 }
155
156 rootSheet->SetName( rootName );
157 }
158 }
159 catch( ... )
160 {
161 return nullptr;
162 }
163
164 SCH_SHEET_LIST sheetList = schematic->BuildSheetListSortedByPageNumbers();
165 SCH_SCREENS screens( schematic->Root() );
166
167 for( SCH_SCREEN* screen = screens.GetFirst(); screen; screen = screens.GetNext() )
168 screen->UpdateLocalLibSymbolLinks();
169
170 if( schematic->RootScreen()->GetFileFormatVersionAtLoad() < 20221002 )
171 sheetList.UpdateSymbolInstanceData( schematic->RootScreen()->GetSymbolInstances());
172
173 sheetList.UpdateSheetInstanceData( schematic->RootScreen()->GetSheetInstances());
174
175 if( schematic->RootScreen()->GetFileFormatVersionAtLoad() < 20230221 )
177
178 for( SCH_SCREEN* screen = screens.GetFirst(); screen; screen = screens.GetNext() )
179 screen->MigrateSimModels();
180
181 schematic->LoadVariants();
182
183 wxString projectName = project->GetProjectName();
184
185 if( projectName.IsEmpty() )
186 projectName = schFile.GetName();
187
188 // Check must run before pruning so variant data on a stale instance path is migrated
189 // onto the new instance before the orphan is removed.
190 sheetList.CheckForMissingSymbolInstances( projectName );
191 screens.PruneOrphanedSymbolInstances( projectName, sheetList );
192 screens.PruneOrphanedSheetInstances( projectName, sheetList );
193
194 sheetList.AnnotatePowerSymbols();
195
196 schematic->ConnectionGraph()->Reset();
197
198 TOOL_MANAGER* toolManager = new TOOL_MANAGER;
199 toolManager->SetEnvironment( schematic, nullptr, nullptr, Kiface().KifaceSettings(), nullptr );
200
201 if( aCalculateConnectivity )
202 {
203 SCH_COMMIT dummyCommit( toolManager );
204 schematic->RecalculateConnections( &dummyCommit, GLOBAL_CLEANUP, toolManager );
205 }
206
207 schematic->ResolveERCExclusionsPostUpdate();
208
209 schematic->SetSheetNumberAndCount();
210 schematic->RecomputeIntersheetRefs();
211
212 for( SCH_SHEET_PATH& sheet : sheetList )
213 {
214 sheet.UpdateAllScreenReferences();
215 sheet.LastScreen()->TestDanglingEnds( nullptr, nullptr );
216 }
217
218 if( aCalculateConnectivity )
219 schematic->ConnectionGraph()->Recalculate( sheetList, true );
220
221 return schematic;
222}
KIFACE_BASE & Kiface()
Global KIFACE_BASE "get" accessor.
static void SetSchEditFrame(SCH_EDIT_FRAME *aSchEditFrame)
static SCH_EDIT_FRAME * s_SchEditFrame
static SCHEMATIC * LoadSchematic(const wxString &aFileName, bool aSetActive, bool aForceDefaultProject, PROJECT *aProject=nullptr, bool aCalculateConnectivity=true)
Instantiate the current locale within a scope in which you are expecting exceptions to be thrown.
Definition locale_io.h:41
virtual SETTINGS_MANAGER & GetSettingsManager() const
Definition pgm_base.h:130
Container for project specific data.
Definition project.h:66
Holds all the data relating to one schematic.
Definition schematic.h:89
Schematic editor (Eeschema) main window.
Container class that holds multiple SCH_SCREEN objects in a hierarchy.
Definition sch_screen.h:750
SCH_SCREEN * GetNext()
void FixLegacyPowerSymbolMismatches()
Fix legacy power symbols that have mismatched value text fields and invisible power pin names.
SCH_SCREEN * GetFirst()
void PruneOrphanedSheetInstances(const wxString &aProjectName, const SCH_SHEET_LIST &aValidSheetPaths)
void PruneOrphanedSymbolInstances(const wxString &aProjectName, const SCH_SHEET_LIST &aValidSheetPaths)
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.
void CheckForMissingSymbolInstances(const wxString &aProjectName)
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:48
wxString GetName() const
Definition sch_sheet.h:140
void SetName(const wxString &aName)
Definition sch_sheet.h:141
bool IsVirtualRootSheet() const
bool LoadProject(const wxString &aFullPath, bool aSetActive=true)
Load a project or sets up a new project with a specified path.
PROJECT * GetProject(const wxString &aFullPath) const
Retrieve a loaded project by name.
PROJECT & Prj() const
A helper while we are not MDI-capable – return the one and only project.
Master controller class:
void SetEnvironment(EDA_ITEM *aModel, KIGFX::VIEW *aView, KIGFX::VIEW_CONTROLS *aViewControls, APP_SETTINGS_BASE *aSettings, TOOLS_HOLDER *aFrame)
Set the work environment (model, view, view controls and the parent window).
#define _(s)
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
PGM_BASE & Pgm()
The global program "get" accessor.
@ GLOBAL_CLEANUP
Definition schematic.h:78
std::vector< FAB_LAYER_COLOR > dummy
Information about a top-level schematic sheet.
Definition of file extensions used in Kicad.