KiCad PCB EDA Suite
Loading...
Searching...
No Matches
sch_api_save.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 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 <api/sch_api_save.h>
21
22#include <base_screen.h>
23#include <common.h>
24#include <kiplatform/io.h>
25#include <pgm_base.h>
26#include <project.h>
28#include <sch_io/sch_io.h>
29#include <sch_io/sch_io_mgr.h>
30#include <sch_screen.h>
31#include <sch_sheet.h>
32#include <sch_sheet_path.h>
33#include <schematic.h>
36
37#include <wx/filename.h>
38#include <wx/log.h>
39
40
41namespace SCH_API_SAVE
42{
43
44bool SaveSheetToFile( SCH_SHEET* aSheet, SCHEMATIC& aSchematic, const wxString& aPath )
45{
46 wxCHECK( aSheet, false );
47
48 if( aPath.IsEmpty() )
49 return false;
50
51 wxFileName schematicFileName( aPath );
52 schematicFileName.MakeAbsolute();
53
54 if( !schematicFileName.DirExists() && !wxMkdir( schematicFileName.GetPath() ) )
55 return false;
56
57 if( schematicFileName.FileExists() && !schematicFileName.IsFileWritable() )
58 return false;
59
60 if( schematicFileName.FileExists() )
61 KIPLATFORM::IO::MakeWriteable( schematicFileName.GetFullPath() );
62
63 SCH_IO_MGR::SCH_FILE_T pluginType = SCH_IO_MGR::GuessPluginTypeFromSchPath( schematicFileName.GetFullPath() );
64
65 if( pluginType == SCH_IO_MGR::SCH_FILE_UNKNOWN )
66 pluginType = SCH_IO_MGR::SCH_KICAD;
67
68 IO_RELEASER<SCH_IO> pi( SCH_IO_MGR::FindPlugin( pluginType ) );
69
70 try
71 {
72 pi->SaveSchematicFile( schematicFileName.GetFullPath(), aSheet, &aSchematic );
73 return true;
74 }
75 catch( const IO_ERROR& ioe )
76 {
77 wxLogTrace( wxS( "KI_TRACE_API" ), wxS( "SaveSheetToFile failed: %s" ), ioe.What() );
78 return false;
79 }
80}
81
82
83namespace
84{
85
86void syncProjectFile( SCHEMATIC& aSchematic, PROJECT& aProject, SCH_SCREEN* aRootScreen )
87{
88 aSchematic.RecordERCExclusions();
89
91
93
94 std::vector<TOP_LEVEL_SHEET_INFO> topLevelSheets = aSchematic.TopLevelSheetInfos();
95
96 if( !topLevelSheets.empty() )
97 aProject.GetProjectFile().GetTopLevelSheets() = std::move( topLevelSheets );
98
99 std::vector<FILE_INFO_PAIR>& sheets = aProject.GetProjectFile().GetSheets();
100 sheets.clear();
101
102 if( !aSchematic.HasHierarchy() )
103 aSchematic.RefreshHierarchy();
104
105 for( SCH_SHEET_PATH& sheetPath : aSchematic.Hierarchy() )
106 {
107 SCH_SHEET* sheet = sheetPath.Last();
108
109 wxCHECK2( sheet, continue );
110
111 if( !sheet->IsVirtualRootSheet() )
112 sheets.emplace_back( std::make_pair( sheet->m_Uuid, sheet->GetName() ) );
113 }
114}
115
116
117bool saveScreens( SCHEMATIC& aSchematic )
118{
119 SCH_SCREEN* rootScreen = aSchematic.RootScreen();
120
121 if( !rootScreen || rootScreen->GetFileName().IsEmpty() )
122 return false;
123
124 SCH_SCREENS screens( aSchematic.Root() );
125 screens.BuildClientSheetPathList();
126
127 bool success = true;
128
129 for( size_t i = 0; i < screens.GetCount(); i++ )
130 {
131 SCH_SCREEN* screen = screens.GetScreen( i );
132
133 wxCHECK2( screen, continue );
134
135 wxFileName fileName = screen->GetFileName();
136
137 if( !fileName.IsOk() )
138 continue;
139
140 std::vector<SCH_SHEET_PATH>& sheets = screen->GetClientSheetPaths();
141
142 if( sheets.size() == 1 )
143 screen->SetVirtualPageNumber( 1 );
144 else
145 screen->SetVirtualPageNumber( 0 );
146
147 success &= SaveSheetToFile( screens.GetSheet( i ), aSchematic, fileName.GetFullPath() );
148
149 if( success )
150 screen->SetContentModified( false );
151 }
152
153 return success;
154}
155
156} // namespace
157
158
159void UpdateProjectFile( SCHEMATIC& aSchematic, PROJECT& aProject )
160{
161 SCH_SCREEN* rootScreen = aSchematic.RootScreen();
162
163 // The first top-level sheet need not be named after the project
164 wxFileName projectFile( aProject.GetProjectFullName() );
165
166 if( !rootScreen || !projectFile.HasName() || !projectFile.IsOk() )
167 return;
168
169 syncProjectFile( aSchematic, aProject, rootScreen );
170 Pgm().GetSettingsManager().SaveProject( projectFile.GetFullPath(), &aProject );
171}
172
173
174bool SaveSchematic( SCHEMATIC& aSchematic, PROJECT& aProject )
175{
176 if( !saveScreens( aSchematic ) )
177 return false;
178
179 UpdateProjectFile( aSchematic, aProject );
180 return true;
181}
182
183
184bool SaveSchematicCopy( SCHEMATIC& aSchematic, PROJECT& aProject, const wxString& aFileName, bool aCreateProject )
185{
186 wxFileName schematicFileName( aFileName );
187 schematicFileName.MakeAbsolute();
188
189 if( !schematicFileName.IsOk() || !schematicFileName.IsDirWritable() )
190 return false;
191
192 // Root() is the virtual root, whose screen only holds the top-level sheets
193 SCH_SHEET* rootSheet = aSchematic.GetTopLevelSheet();
194 SCH_SCREEN* rootScreen = aSchematic.RootScreen();
195
196 if( !rootSheet || !rootScreen )
197 return false;
198
199 if( schematicFileName.GetFullPath() == rootScreen->GetFileName() )
200 return SaveSchematic( aSchematic, aProject );
201
202 if( !SaveSheetToFile( rootSheet, aSchematic, schematicFileName.GetFullPath() ) )
203 return false;
204
205 if( aCreateProject )
206 {
207 wxFileName projectFile( schematicFileName );
208 projectFile.SetExt( FILEEXT::ProjectFileExtension );
209
210 if( !projectFile.FileExists() )
211 {
212 if( !Pgm().GetSettingsManager().SaveProjectCopy( projectFile.GetFullPath() ) )
213 return false;
214 }
215 }
216
217 return true;
218}
219
220
221bool SaveSchematicAs( SCHEMATIC& aSchematic, PROJECT& aProject, const wxString& aFileName )
222{
223 wxFileName schematicFileName( EnsureFileExtension( aFileName, FILEEXT::KiCadSchematicFileExtension ) );
224 schematicFileName.MakeAbsolute();
225
226 if( !schematicFileName.IsOk() || schematicFileName.GetName().IsEmpty() )
227 return false;
228
229 if( ( !schematicFileName.DirExists() && !schematicFileName.Mkdir() ) || !schematicFileName.IsDirWritable() )
230 return false;
231
232 aSchematic.Root().SetFileName( schematicFileName.GetFullName() );
233 aSchematic.RootScreen()->SetFileName( schematicFileName.GetFullPath() );
234
235 // Saving the old project file here would rewrite the project we are leaving
236 if( !saveScreens( aSchematic ) )
237 return false;
238
239 syncProjectFile( aSchematic, aProject, aSchematic.RootScreen() );
240 aProject.SetReadOnly( false );
241
242 wxFileName projectFile( schematicFileName );
243 projectFile.SetExt( FILEEXT::ProjectFileExtension );
244
245 Pgm().GetSettingsManager().SaveProjectAs( projectFile.GetFullPath(), &aProject );
246
247 return true;
248}
249
250} // namespace SCH_API_SAVE
BASE_SCREEN class implementation.
void SetVirtualPageNumber(int aPageNumber)
Definition base_screen.h:72
static wxString m_DrawingSheetFileName
the name of the drawing sheet file, or empty to use the default drawing sheet
Definition base_screen.h:81
void SetContentModified(bool aModified=true)
Definition base_screen.h:55
const KIID m_Uuid
Definition eda_item.h:599
Hold an error message and may be used when throwing exceptions containing meaningful error messages.
virtual const wxString What() const
A composite of Problem() and Where()
virtual SETTINGS_MANAGER & GetSettingsManager() const
Definition pgm_base.h:124
std::vector< FILE_INFO_PAIR > & GetSheets()
SCHEMATIC_SETTINGS * m_SchematicSettings
struct IP2581_BOM m_IP2581Bom
Layer pair list for the board.
std::vector< TOP_LEVEL_SHEET_INFO > & GetTopLevelSheets()
Container for project specific data.
Definition project.h:63
virtual void SetReadOnly(bool aReadOnly=true)
Definition project.h:161
virtual const wxString GetProjectFullName() const
Return the full path and name of the project.
Definition project.cpp:271
virtual PROJECT_FILE & GetProjectFile() const
Definition project.h:201
Holds all the data relating to one schematic.
Definition schematic.h:155
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.
bool HasHierarchy() const
Check if the hierarchy has been built.
Definition schematic.h:204
SCH_SHEET * GetTopLevelSheet(int aIndex=0) const
SCH_SCREEN * RootScreen() const
Helper to retrieve the screen of the root sheet.
SCH_SHEET & Root() const
Definition schematic.h:282
std::vector< TOP_LEVEL_SHEET_INFO > TopLevelSheetInfos() const
The top-level sheets as the project file records them, with file names relative to the project.
void RefreshHierarchy()
static SCH_FILE_T GuessPluginTypeFromSchPath(const wxString &aSchematicPath, int aCtl=0)
Return a plugin type given a schematic using the file extension of aSchematicPath.
std::vector< SCH_SHEET_PATH > & GetClientSheetPaths()
Return the number of times this screen is used.
Definition sch_screen.h:226
const wxString & GetFileName() const
Definition sch_screen.h:157
void SetFileName(const wxString &aFileName)
Set the file name for this screen to aFileName.
TITLE_BLOCK & GetTitleBlock()
Definition sch_screen.h:168
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:50
void SetFileName(const wxString &aFilename)
Definition sch_sheet.h:434
wxString GetName() const
Definition sch_sheet.h:150
bool IsVirtualRootSheet() const
void SaveProjectAs(const wxString &aFullPath, PROJECT *aProject=nullptr)
Set the currently loaded project path and saves it (pointers remain valid).
bool SaveProject(const wxString &aFullPath=wxEmptyString, PROJECT *aProject=nullptr)
Save a loaded project.
const wxString & GetRevision() const
Definition title_block.h:83
wxString EnsureFileExtension(const wxString &aFilename, const wxString &aExtension)
It's annoying to throw up nag dialogs when the extension isn't right.
Definition common.cpp:880
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
bool MakeWriteable(const wxString &aFilePath)
Ensures that a file has write permissions.
Definition unix/io.cpp:78
bool SaveSheetToFile(SCH_SHEET *aSheet, SCHEMATIC &aSchematic, const wxString &aPath)
Write a single sheet to disk via SCH_IO.
bool SaveSchematic(SCHEMATIC &aSchematic, PROJECT &aProject)
Save every screen in the hierarchy to its current path, then update the project file.
void UpdateProjectFile(SCHEMATIC &aSchematic, PROJECT &aProject)
Sync schematic metadata into the project file (.kicad_pro) and save it.
bool SaveSchematicCopy(SCHEMATIC &aSchematic, PROJECT &aProject, const wxString &aFileName, bool aCreateProject)
Save the root schematic to aFileName without changing the open document.
bool SaveSchematicAs(SCHEMATIC &aSchematic, PROJECT &aProject, const wxString &aFileName)
Save the schematic to aFileName and adopt it: the root sheet and screen are re-homed to the new path,...
PGM_BASE & Pgm()
The global program "get" accessor.
see class PGM_BASE
Definition of the SCH_SHEET_PATH and SCH_SHEET_LIST classes for Eeschema.
wxString schRevision
Auto-propagated schematic title block revision.
Definition of file extensions used in Kicad.