KiCad PCB EDA Suite
Loading...
Searching...
No Matches
test_saveas_copy_subsheets.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 2
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, see <https://www.gnu.org/licenses/>.
18 */
19
24
26#include "eeschema_test_utils.h"
27
31#include <pgm_base.h>
32#include <schematic.h>
34#include <title_block.h>
35#include <sch_sheet.h>
36#include <sch_sheet_path.h>
37#include <sch_symbol.h>
39#include <wx/filename.h>
40#include <wx/ffile.h>
41#include <wx/filefn.h>
42
44{
45public:
47
48 wxFileName GetSchematicPath( const wxString& aRelativePath );
49
50 wxFileName m_baseDir;
51 wxFileName m_srcDir;
52 wxFileName m_externalDir;
53};
54
56{
57 wxFileName tmp( wxFileName::CreateTempFileName( wxS( "saveas" ) ) );
58 wxString dirPath = tmp.GetPath(); // /tmp
59 wxString dirName = tmp.GetName(); // saveasXXXX
60 wxRemoveFile( tmp.GetFullPath() );
61
62 wxFileName base( dirPath, wxEmptyString ); // represents /tmp
63 base.AppendDir( dirName ); // now /tmp/saveasXXXX
64 base.Mkdir(); // create the directory
65 m_baseDir = base;
66
68 m_srcDir.AppendDir( wxS( "src" ) );
69 m_srcDir.Mkdir();
70
72 m_externalDir.AppendDir( wxS( "external" ) );
73 m_externalDir.Mkdir();
74
75 wxFileName dataDir( KI_TEST::GetEeschemaTestDataDir() );
76
77 wxCopyFile( dataDir.GetFullPath() + wxS( "/issue13212.kicad_sch" ),
78 m_srcDir.GetFullPath() + wxS( "/issue13212.kicad_sch" ) );
79 wxCopyFile( dataDir.GetFullPath() + wxS( "/issue13212_subsheet_1.kicad_sch" ),
80 m_srcDir.GetFullPath() + wxS( "/issue13212_subsheet_1.kicad_sch" ) );
81 wxCopyFile( dataDir.GetFullPath() + wxS( "/issue13212_subsheet_2.kicad_sch" ),
82 m_externalDir.GetFullPath() + wxS( "/issue13212_subsheet_2.kicad_sch" ) );
83
84 wxFileName rootFile( m_srcDir );
85 rootFile.SetFullName( wxS( "issue13212.kicad_sch" ) );
86
87 wxFFile file( rootFile.GetFullPath(), wxS( "rb" ) );
88 wxString content;
89 file.ReadAll( &content );
90 file.Close();
91 content.Replace( wxS( "issue13212_subsheet_2.kicad_sch" ),
92 wxS( "../external/issue13212_subsheet_2.kicad_sch" ) );
93 wxFFile outFile( rootFile.GetFullPath(), wxS( "wb" ) );
94 outFile.Write( content );
95 outFile.Close();
96}
97
98wxFileName SAVEAS_SUBSHEET_FIXTURE::GetSchematicPath( const wxString& aRelativePath )
99{
100 wxFileName fn( m_srcDir );
101 fn.SetName( aRelativePath );
103 return fn;
104}
105
106static bool ScreensContain( SCH_SCREENS& aScreens, SCH_SCREEN* aScreen )
107{
108 for( size_t i = 0; i < aScreens.GetCount(); i++ )
109 {
110 if( aScreens.GetScreen( i ) == aScreen )
111 return true;
112 }
113
114 return false;
115}
116
117BOOST_FIXTURE_TEST_SUITE( SaveAsSubsheetCopy, SAVEAS_SUBSHEET_FIXTURE )
118
119BOOST_AUTO_TEST_CASE( CopyInternalReferenceExternal )
120{
121 LoadSchematic( GetSchematicPath( wxS( "issue13212" ) ) );
122
123 SCH_SCREENS screens( m_schematic->Root() );
124 wxFileName srcRoot = GetSchematicPath( wxS( "issue13212" ) );
125
126 wxFileName destRoot( m_baseDir );
127 destRoot.AppendDir( wxS( "new" ) );
128 destRoot.AppendDir( wxS( "location" ) );
129 destRoot.SetName( wxS( "issue13212" ) );
130 destRoot.SetExt( FILEEXT::KiCadSchematicFileExtension );
131 { wxFileName destDir( destRoot ); destDir.SetFullName( wxEmptyString ); if( !destDir.DirExists() ) destDir.Mkdir( 0777, wxPATH_MKDIR_FULL ); }
132
133 std::unordered_map<SCH_SCREEN*, wxString> filenameMap;
134 wxString msg;
135
136 m_schematic->Root().SetFileName( destRoot.GetFullName() );
137 m_schematic->RootScreen()->SetFileName( destRoot.GetFullPath() );
138
139 bool ok = PrepareSaveAsFiles( *m_schematic, screens, srcRoot, destRoot, false, true,
140 false, filenameMap, msg );
141 BOOST_CHECK( ok );
142
143 SCH_SCREEN* internal = nullptr;
144 SCH_SCREEN* external = nullptr;
145
146 for( size_t i = 0; i < screens.GetCount(); i++ )
147 {
148 SCH_SCREEN* s = screens.GetScreen( i );
149
150 if( wxString( s->GetFileName() ).EndsWith( wxS( "issue13212_subsheet_1.kicad_sch" ) ) )
151 internal = s;
152 else if( wxString( s->GetFileName() ).EndsWith( wxS( "issue13212_subsheet_2.kicad_sch" ) ) )
153 external = s;
154 }
155
156 wxFileName internalExpected( destRoot.GetPath(), wxS( "issue13212_subsheet_1.kicad_sch" ) );
157 BOOST_CHECK_EQUAL( internal->GetFileName(), internalExpected.GetFullPath() );
158
159 wxFileName externalExpected( m_externalDir.GetFullPath(), wxS( "issue13212_subsheet_2.kicad_sch" ) );
160 BOOST_CHECK_EQUAL( external->GetFileName(), externalExpected.GetFullPath() );
161
162 SCH_SHEET_LIST sheetList = m_schematic->BuildSheetListSortedByPageNumbers();
163
164 wxString externalSheetPath;
165
166 for( const SCH_SHEET_PATH& path : sheetList )
167 {
168 if( path.Last()->GetFileName().Contains( wxS( "issue13212_subsheet_2" ) ) )
169 externalSheetPath = path.Last()->GetFileName();
170 }
171
172 BOOST_CHECK_EQUAL( externalSheetPath, wxS( "../../external/issue13212_subsheet_2.kicad_sch" ) );
173}
174
175BOOST_AUTO_TEST_CASE( CopyIncludingExternal )
176{
177 LoadSchematic( GetSchematicPath( wxS( "issue13212" ) ) );
178
179 SCH_SCREENS screens( m_schematic->Root() );
180 wxFileName srcRoot = GetSchematicPath( wxS( "issue13212" ) );
181
182 wxFileName destRoot( m_baseDir );
183 destRoot.AppendDir( wxS( "destall" ) );
184 destRoot.SetName( wxS( "issue13212" ) );
185 destRoot.SetExt( FILEEXT::KiCadSchematicFileExtension );
186 { wxFileName destDir( destRoot ); destDir.SetFullName( wxEmptyString ); if( !destDir.DirExists() ) destDir.Mkdir( 0777, wxPATH_MKDIR_FULL ); }
187
188 std::unordered_map<SCH_SCREEN*, wxString> filenameMap;
189 wxString msg;
190
191 m_schematic->Root().SetFileName( destRoot.GetFullName() );
192 m_schematic->RootScreen()->SetFileName( destRoot.GetFullPath() );
193
194 bool ok = PrepareSaveAsFiles( *m_schematic, screens, srcRoot, destRoot, false, true,
195 true, filenameMap, msg );
196 BOOST_CHECK( ok );
197
198 SCH_SCREEN* external = nullptr;
199
200 for( size_t i = 0; i < screens.GetCount(); i++ )
201 {
202 SCH_SCREEN* s = screens.GetScreen( i );
203
204 if( wxString( s->GetFileName() ).EndsWith( wxS( "issue13212_subsheet_2.kicad_sch" ) ) )
205 external = s;
206 }
207
208 wxFileName externalExpected( destRoot.GetPath(), wxS( "issue13212_subsheet_2.kicad_sch" ) );
209 BOOST_CHECK_EQUAL( external->GetFileName(), externalExpected.GetFullPath() );
210}
211
212BOOST_AUTO_TEST_CASE( NoCopyKeepsOriginalPaths )
213{
214 LoadSchematic( GetSchematicPath( wxS( "issue13212" ) ) );
215
216 SCH_SCREENS screens( m_schematic->Root() );
217 wxFileName srcRoot = GetSchematicPath( wxS( "issue13212" ) );
218
219 wxFileName destRoot( m_baseDir );
220 destRoot.AppendDir( wxS( "nocopy" ) );
221 destRoot.SetName( wxS( "issue13212" ) );
222 destRoot.SetExt( FILEEXT::KiCadSchematicFileExtension );
223 { wxFileName destDir( destRoot ); destDir.SetFullName( wxEmptyString ); if( !destDir.DirExists() ) destDir.Mkdir( 0777, wxPATH_MKDIR_FULL ); }
224
225 std::unordered_map<SCH_SCREEN*, wxString> filenameMap;
226 wxString msg;
227
228 m_schematic->Root().SetFileName( destRoot.GetFullName() );
229 m_schematic->RootScreen()->SetFileName( destRoot.GetFullPath() );
230
231 bool ok = PrepareSaveAsFiles( *m_schematic, screens, srcRoot, destRoot, false, false,
232 false, filenameMap, msg );
233 BOOST_CHECK( ok );
234
235 SCH_SCREEN* internal = nullptr;
236
237 for( size_t i = 0; i < screens.GetCount(); i++ )
238 {
239 SCH_SCREEN* s = screens.GetScreen( i );
240
241 if( wxString( s->GetFileName() ).EndsWith( wxS( "issue13212_subsheet_1.kicad_sch" ) ) )
242 internal = s;
243 }
244
245 wxFileName internalExpected( m_srcDir.GetFullPath(), wxS( "issue13212_subsheet_1.kicad_sch" ) );
246 BOOST_CHECK_EQUAL( internal->GetFileName(), internalExpected.GetFullPath() );
247}
248
249// A destination on the virtual root's screen becomes a nameless path that the callers stamp
250// an extension onto, writing a stray ".kicad_sch" beside the real schematic
251BOOST_AUTO_TEST_CASE( VirtualRootScreenGetsNoDestination )
252{
253 LoadSchematic( GetSchematicPath( wxS( "issue13212" ) ) );
254
255 SCH_SCREENS screens( m_schematic->Root() );
256 wxFileName srcRoot = GetSchematicPath( wxS( "issue13212" ) );
257
258 wxFileName destRoot( m_baseDir );
259 destRoot.AppendDir( wxS( "virtualroot" ) );
260 destRoot.SetName( wxS( "issue13212" ) );
261 destRoot.SetExt( FILEEXT::KiCadSchematicFileExtension );
262
263 wxFileName destDir( destRoot );
264 destDir.SetFullName( wxEmptyString );
265 BOOST_REQUIRE( destDir.DirExists() || destDir.Mkdir( 0777, wxPATH_MKDIR_FULL ) );
266
267 SCH_SCREEN* virtualRootScreen = m_schematic->Root().GetScreen();
268 BOOST_REQUIRE( virtualRootScreen );
269 BOOST_REQUIRE( virtualRootScreen != m_schematic->RootScreen() );
270 BOOST_REQUIRE( ScreensContain( screens, virtualRootScreen ) );
271
272 std::unordered_map<SCH_SCREEN*, wxString> filenameMap;
273 wxString msg;
274
275 m_schematic->Root().SetFileName( destRoot.GetFullName() );
276 m_schematic->RootScreen()->SetFileName( destRoot.GetFullPath() );
277
278 BOOST_CHECK( PrepareSaveAsFiles( *m_schematic, screens, srcRoot, destRoot, false, true,
279 false, filenameMap, msg ) );
280
281 BOOST_CHECK_EQUAL( virtualRootScreen->GetFileName(), wxString() );
282}
283
284BOOST_AUTO_TEST_CASE( VirtualRootScreenGetsNoDestinationOnCopy )
285{
286 LoadSchematic( GetSchematicPath( wxS( "issue13212" ) ) );
287
288 SCH_SCREENS screens( m_schematic->Root() );
289 wxFileName srcRoot = GetSchematicPath( wxS( "issue13212" ) );
290
291 wxFileName destRoot( m_baseDir );
292 destRoot.AppendDir( wxS( "virtualrootcopy" ) );
293 destRoot.SetName( wxS( "issue13212" ) );
294 destRoot.SetExt( FILEEXT::KiCadSchematicFileExtension );
295
296 wxFileName destDir( destRoot );
297 destDir.SetFullName( wxEmptyString );
298 BOOST_REQUIRE( destDir.DirExists() || destDir.Mkdir( 0777, wxPATH_MKDIR_FULL ) );
299
300 SCH_SCREEN* virtualRootScreen = m_schematic->Root().GetScreen();
301 BOOST_REQUIRE( virtualRootScreen );
302 BOOST_REQUIRE( virtualRootScreen != m_schematic->RootScreen() );
303 BOOST_REQUIRE( ScreensContain( screens, virtualRootScreen ) );
304
305 std::unordered_map<SCH_SCREEN*, wxString> filenameMap;
306 wxString msg;
307
308 filenameMap[m_schematic->RootScreen()] = destRoot.GetFullPath();
309
310 BOOST_CHECK( PrepareSaveAsFiles( *m_schematic, screens, srcRoot, destRoot, true, true,
311 false, filenameMap, msg ) );
312
313 BOOST_CHECK( !filenameMap.contains( virtualRootScreen ) );
314}
315
316// The API save-copy took Root() as the sheet to write, so it emitted the virtual root's
317// container screen and the copy came out valid but empty
318BOOST_AUTO_TEST_CASE( ApiSaveCopyWritesTheTopLevelSheet )
319{
320 LoadSchematic( GetSchematicPath( wxS( "issue13212" ) ) );
321
322 std::vector<wxString> references;
323
324 for( SCH_ITEM* item : m_schematic->RootScreen()->Items().OfType( SCH_SYMBOL_T ) )
325 references.push_back( static_cast<SCH_SYMBOL*>( item )->GetField( FIELD_T::REFERENCE )->GetText() );
326
327 BOOST_REQUIRE( !references.empty() );
328
329 wxFileName destFile( m_baseDir );
330 destFile.AppendDir( wxS( "apicopy" ) );
331 destFile.SetName( wxS( "copy" ) );
332 destFile.SetExt( FILEEXT::KiCadSchematicFileExtension );
333
334 wxFileName destDir( destFile );
335 destDir.SetFullName( wxEmptyString );
336 BOOST_REQUIRE( destDir.DirExists() || destDir.Mkdir( 0777, wxPATH_MKDIR_FULL ) );
337
338 BOOST_REQUIRE( SCH_API_SAVE::SaveSchematicCopy( *m_schematic, m_schematic->Project(),
339 destFile.GetFullPath(), false ) );
340
341 wxFFile written( destFile.GetFullPath(), wxS( "rb" ) );
342 BOOST_REQUIRE( written.IsOpened() );
343
344 wxString content;
345 written.ReadAll( &content );
346
347 for( const wxString& reference : references )
348 BOOST_CHECK( content.Contains( wxS( "\"" ) + reference + wxS( "\"" ) ) );
349}
350
351// The API took the project file from the first top-level sheet, so a project named otherwise
352// kept its stale settings
353BOOST_AUTO_TEST_CASE( ApiSaveWritesTheLoadedProjectFile )
354{
355 wxFileName dataDir( KI_TEST::GetEeschemaTestDataDir() );
356 dataDir.AppendDir( wxS( "variants" ) );
357
358 for( const wxString& name : { wxS( "variants.kicad_pro" ), wxS( "variants.kicad_sch" ),
359 wxS( "pic_sockets.kicad_sch" ) } )
360 {
361 BOOST_REQUIRE( wxCopyFile( dataDir.GetPathWithSep() + name, m_baseDir.GetPathWithSep() + name ) );
362 }
363
364 wxFileName projectFile( m_baseDir.GetPath(), wxS( "variants" ), FILEEXT::ProjectFileExtension );
365 wxFileName schematicFile( m_baseDir.GetPath(), wxS( "variants" ), FILEEXT::KiCadSchematicFileExtension );
366
367 // The API opens projects through the global manager
369 bool loaded = manager.LoadProject( projectFile.GetFullPath(), false );
370 PROJECT* project = manager.GetProject( projectFile.GetFullPath() );
371
372 // A failed check must not leave the project in the global manager
373 auto unload = [&manager]( PROJECT* aProject ) { manager.UnloadProject( aProject, false ); };
374 std::unique_ptr<PROJECT, decltype( unload )> unloadGuard( project, unload );
375
376 BOOST_REQUIRE( loaded && project );
377
378 std::unique_ptr<SCHEMATIC> schematic(
379 EESCHEMA_HELPERS::LoadSchematic( schematicFile.GetFullPath(), false, false, project ) );
380 BOOST_REQUIRE( schematic );
381
382 SCH_SCREEN* firstScreen = schematic->RootScreen();
383 firstScreen->SetFileName( m_baseDir.GetPathWithSep() + wxS( "first_sheet.kicad_sch" ) );
384
385 TITLE_BLOCK titleBlock = firstScreen->GetTitleBlock();
386 titleBlock.SetRevision( wxS( "api-save-revision" ) );
387 firstScreen->SetTitleBlock( titleBlock );
388
390
391 wxFFile written( projectFile.GetFullPath(), wxS( "rb" ) );
392 wxString content;
393 BOOST_REQUIRE( written.IsOpened() && written.ReadAll( &content ) );
394 BOOST_CHECK( content.Contains( wxS( "api-save-revision" ) ) );
395}
396
const char * name
static SCHEMATIC * LoadSchematic(const wxString &aFileName, bool aSetActive, bool aForceDefaultProject, PROJECT *aProject=nullptr, bool aCalculateConnectivity=true, REPORTER *aRootReporter=nullptr)
A generic fixture for loading schematics and associated settings for qa tests.
virtual SETTINGS_MANAGER & GetSettingsManager() const
Definition pgm_base.h:124
Container for project specific data.
Definition project.h:63
wxFileName GetSchematicPath(const wxString &aRelativePath)
Base class for any item which can be embedded within the SCHEMATIC container class,...
Definition sch_item.h:170
Container class that holds multiple SCH_SCREEN objects in a hierarchy.
Definition sch_screen.h:826
SCH_SCREEN * GetScreen(unsigned int aIndex) const
size_t GetCount() const
Definition sch_screen.h:831
void SetTitleBlock(const TITLE_BLOCK &aTitleBlock)
Definition sch_screen.h:171
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
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...
Schematic symbol object.
Definition sch_symbol.h:76
PROJECT * GetProject(const wxString &aFullPath) const
Retrieve a loaded project by name.
bool UnloadProject(PROJECT *aProject, bool aSave=true)
Save, unload and unregister the given PROJECT.
bool LoadProject(const wxString &aFullPath, bool aSetActive=true, bool aOverrideLock=false)
Load a project or sets up a new project with a specified path.
Hold the information shown in the lower right corner of a plot, printout, or editing view.
Definition title_block.h:38
void SetRevision(const wxString &aRevision)
Definition title_block.h:78
bool PrepareSaveAsFiles(SCHEMATIC &aSchematic, SCH_SCREENS &aScreens, const wxFileName &aOldRoot, const wxFileName &aNewRoot, bool aSaveCopy, bool aCopySubsheets, bool aIncludeExternSheets, std::unordered_map< SCH_SCREEN *, wxString > &aFilenameMap, wxString &aErrorMsg)
static const std::string ProjectFileExtension
static const std::string KiCadSchematicFileExtension
std::string GetEeschemaTestDataDir()
Get the configured location of Eeschema test data.
bool SaveSchematic(SCHEMATIC &aSchematic, PROJECT &aProject)
Save every screen in the hierarchy to its current path, then update the project file.
bool SaveSchematicCopy(SCHEMATIC &aSchematic, PROJECT &aProject, const wxString &aFileName, bool aCreateProject)
Save the root schematic to aFileName without changing the open document.
PGM_BASE & Pgm()
The global program "get" accessor.
see class PGM_BASE
static void LoadSchematic(SCHEMATIC *aSchematic, SCH_SHEET *aRootSheet, const wxString &aFileName, REPORTER *aReporter)
Definition of the SCH_SHEET_PATH and SCH_SHEET_LIST classes for Eeschema.
@ REFERENCE
Field Reference of part, i.e. "IC21".
BOOST_AUTO_TEST_CASE(HorizontalAlignment)
BOOST_CHECK(res.column_datatype(0)==SQL_NUMERIC||res.column_datatype(0)==SQL_DECIMAL)
BOOST_CHECK_EQUAL(std::any_cast< std::string >(rows[0].at("val")), "12345678901234567.89")
BOOST_REQUIRE(res.columns()==1)
BOOST_AUTO_TEST_SUITE_END()
std::string path
BOOST_AUTO_TEST_CASE(CopyInternalReferenceExternal)
static bool ScreensContain(SCH_SCREENS &aScreens, SCH_SCREEN *aScreen)
@ SCH_SYMBOL_T
Definition typeinfo.h:168
Definition of file extensions used in Kicad.