From f7cc6d1e1e1a357fc2c8e1581432f3c2bc98ee16 Mon Sep 17 00:00:00 2001 From: Jeff Young Date: Tue, 13 Apr 2021 17:41:23 +0100 Subject: [PATCH] Fix save as and save copy as for project files. --- common/settings/settings_manager.cpp | 16 +++++ include/settings/settings_manager.h | 5 ++ pcbnew/files.cpp | 91 ++++++---------------------- 3 files changed, 41 insertions(+), 71 deletions(-) diff --git a/common/settings/settings_manager.cpp b/common/settings/settings_manager.cpp index 6224f32a16..4f8c7ab8ff 100644 --- a/common/settings/settings_manager.cpp +++ b/common/settings/settings_manager.cpp @@ -932,6 +932,22 @@ void SETTINGS_MANAGER::SaveProjectAs( const wxString& aFullPath ) } +void SETTINGS_MANAGER::SaveProjectCopy( const wxString& aFullPath ) +{ + PROJECT_FILE* project = m_project_files.at( Prj().GetProjectFullName() ); + wxString oldName = project->GetFilename(); + wxFileName fn( aFullPath ); + + project->SetFilename( fn.GetName() ); + project->SaveToFile( fn.GetPath() ); + project->SetFilename( oldName ); + + Prj().GetLocalSettings().SetFilename( fn.GetName() ); + Prj().GetLocalSettings().SaveToFile( fn.GetPath() ); + Prj().GetLocalSettings().SetFilename( oldName ); +} + + bool SETTINGS_MANAGER::loadProjectFile( PROJECT& aProject ) { wxFileName fullFn( aProject.GetProjectFullName() ); diff --git a/include/settings/settings_manager.h b/include/settings/settings_manager.h index 9690e10324..3d19ed6d19 100644 --- a/include/settings/settings_manager.h +++ b/include/settings/settings_manager.h @@ -278,6 +278,11 @@ public: */ void SaveProjectAs( const wxString& aFullPath ); + /** + * Saves a copy of the current project under the given path + */ + void SaveProjectCopy( const wxString& aFullPath ); + /** * @return the full path to where project backups should be stored */ diff --git a/pcbnew/files.cpp b/pcbnew/files.cpp index 4fa5691d73..2881e0e242 100644 --- a/pcbnew/files.cpp +++ b/pcbnew/files.cpp @@ -932,54 +932,29 @@ bool PCB_EDIT_FRAME::SavePcbFile( const wxString& aFileName, bool addToHistory, // TODO: this will break if we ever go multi-board wxFileName projectFile( pcbFileName ); - bool projectFileExists = false; projectFile.SetExt( ProjectFileExtension ); - projectFileExists = projectFile.FileExists(); - if( aChangeProject && !projectFileExists ) + if( !projectFile.FileExists() && aChangeProject ) + GetSettingsManager()->SaveProjectAs( projectFile.GetFullPath() ); + + if( projectFile.FileExists() ) { - // If this is a new board, project filename won't be set yet - if( projectFile.GetFullPath() != Prj().GetProjectFullName() ) - { - GetBoard()->ClearProject(); + // Save various DRC parameters, such as violation severities (which may have been + // edited via the DRC dialog as well as the Board Setup dialog), DRC exclusions, etc. + SaveProjectSettings(); - SETTINGS_MANAGER* mgr = GetSettingsManager(); - - mgr->SaveProject( Prj().GetProjectFullName() ); - mgr->UnloadProject( &Prj() ); - - // If no project to load then initialize project text vars with board properties - if( !mgr->LoadProject( projectFile.GetFullPath() ) ) - { - Prj().GetTextVars() = GetBoard()->GetProperties(); - - // TODO: save netclasses from currentProject to new project... - } - - GetBoard()->SetProject( &Prj() ); - } + GetBoard()->SynchronizeProperties(); + GetBoard()->SynchronizeNetsAndNetClasses(); } - if( projectFileExists ) - GetBoard()->SynchronizeProperties(); - wxFileName tempFile( aFileName ); + wxString upperTxt; + wxString lowerTxt; + tempFile.SetName( wxT( "." ) + tempFile.GetName() ); tempFile.SetExt( tempFile.GetExt() + wxT( "$" ) ); - GetBoard()->SynchronizeNetsAndNetClasses(); - - // Save various DRC parameters, such as violation severities (which may have been - // edited via the DRC dialog as well as the Board Setup dialog), DRC exclusions, etc. - SaveProjectSettings(); - - GetSettingsManager()->SaveProject(); - - - wxString upperTxt; - wxString lowerTxt; - try { PLUGIN::RELEASER pi( IO_MGR::PluginFind( IO_MGR::KICAD_SEXP ) ); @@ -1080,6 +1055,10 @@ bool PCB_EDIT_FRAME::SavePcbCopy( const wxString& aFileName, bool aCreateProject return false; } + // Save various DRC parameters, such as violation severities (which may have been + // edited via the DRC dialog as well as the Board Setup dialog), DRC exclusions, etc. + SaveProjectSettings(); + GetBoard()->SynchronizeNetsAndNetClasses(); try @@ -1100,42 +1079,12 @@ bool PCB_EDIT_FRAME::SavePcbCopy( const wxString& aFileName, bool aCreateProject return false; } - if( aCreateProject ) - { - wxFileName projectFile( pcbFileName ); - projectFile.SetExt( ProjectFileExtension ); + wxFileName projectFile( pcbFileName ); - if( !projectFile.FileExists() ) - { - wxString currentProject = Prj().GetProjectFullName(); + projectFile.SetExt( ProjectFileExtension ); - SETTINGS_MANAGER* mgr = GetSettingsManager(); - - // Shelve the current project - GetBoard()->ClearProject(); - - mgr->SaveProject( currentProject ); - mgr->UnloadProject( &Prj() ); - - // Create a new project for the saved-as-copy - - // If no project to load then initialize project text vars with board properties - if( !mgr->LoadProject( projectFile.GetFullPath() ) ) - { - Prj().GetTextVars() = GetBoard()->GetProperties(); - - // TODO: save netclasses from currentProject to new project... - } - - mgr->SaveProject(); - - // Now go back to our own project - mgr->UnloadProject( &Prj() ); - mgr->LoadProject( currentProject ); - - GetBoard()->SetProject( &Prj() ); - } - } + if( aCreateProject && !projectFile.FileExists() ) + GetSettingsManager()->SaveProjectCopy( projectFile.GetFullPath() ); DisplayInfoMessage( this, wxString::Format( _( "Board copied to:\n\"%s\"" ), pcbFileName.GetFullPath() ) );