diff --git a/pcbnew/python/scripting/pcbnew_scripting_helpers.cpp b/pcbnew/python/scripting/pcbnew_scripting_helpers.cpp index baa7dd6e5f..6ebd0bb69f 100644 --- a/pcbnew/python/scripting/pcbnew_scripting_helpers.cpp +++ b/pcbnew/python/scripting/pcbnew_scripting_helpers.cpp @@ -74,6 +74,10 @@ void ScriptingSetPcbEditFrame( PCB_EDIT_FRAME* aPcbEditFrame ) BOARD* LoadBoard( wxString& aFileName ) { + // Loading a new board is not possible if running inside KiCad + if( s_PcbEditFrame ) + return nullptr; + if( aFileName.EndsWith( KiCadPcbFileExtension ) ) return LoadBoard( aFileName, IO_MGR::KICAD_SEXP ); else if( aFileName.EndsWith( LegacyPcbFileExtension ) ) @@ -88,9 +92,16 @@ SETTINGS_MANAGER* GetSettingsManager() { if( !s_SettingsManager ) { - // Ensure wx system settings stuff is available - static_cast( wxTheApp ); - s_SettingsManager = new SETTINGS_MANAGER( true ); + if( s_PcbEditFrame ) + { + s_SettingsManager = s_PcbEditFrame->GetSettingsManager(); + } + else + { + // Ensure wx system settings stuff is available + static_cast( wxTheApp ); + s_SettingsManager = new SETTINGS_MANAGER( true ); + } } return s_SettingsManager; @@ -117,6 +128,9 @@ PROJECT* GetDefaultProject() BOARD* LoadBoard( wxString& aFileName, IO_MGR::PCB_FILE_T aFormat ) { + // Loading a new board is not possible if running inside KiCad + wxASSERT( !s_PcbEditFrame ); + wxFileName pro = aFileName; pro.SetExt( ProjectFileExtension ); pro.MakeAbsolute(); @@ -179,6 +193,10 @@ BOARD* LoadBoard( wxString& aFileName, IO_MGR::PCB_FILE_T aFormat ) BOARD* CreateEmptyBoard() { + // Creating a new board is not possible if running inside KiCad + if( s_PcbEditFrame ) + return nullptr; + BOARD* brd = new BOARD(); brd->SetProject( GetDefaultProject() ); @@ -192,14 +210,21 @@ bool SaveBoard( wxString& aFileName, BOARD* aBoard, IO_MGR::PCB_FILE_T aFormat ) aBoard->BuildConnectivity(); aBoard->SynchronizeNetsAndNetClasses(); - IO_MGR::Save( aFormat, aFileName, aBoard, nullptr ); + try + { + IO_MGR::Save( aFormat, aFileName, aBoard, nullptr ); + } + catch( ... ) + { + return false; + } wxFileName pro = aFileName; pro.SetExt( ProjectFileExtension ); pro.MakeAbsolute(); wxString projectPath = pro.GetFullPath(); - GetSettingsManager()->SaveProject( pro.GetFullPath() ); + GetSettingsManager()->SaveProjectAs( pro.GetFullPath() ); return true; } diff --git a/pcbnew/python/scripting/pcbnew_scripting_helpers.h b/pcbnew/python/scripting/pcbnew_scripting_helpers.h index cdb49734ed..a4ab2efdc4 100644 --- a/pcbnew/python/scripting/pcbnew_scripting_helpers.h +++ b/pcbnew/python/scripting/pcbnew_scripting_helpers.h @@ -54,8 +54,13 @@ SETTINGS_MANAGER* GetSettingsManager(); */ BOARD* CreateEmptyBoard(); -// Boards can be saved only as .kicad_pcb file format, -// so no option to choose the file format. +/** + * Saves a copy of the given board and its associated project to the given path. + * Boards can only be saved in KiCad native format. + * @param aFileName is the full path to save a copy to. + * @param aBoard is a pointer to a loaded BOARD to save. + * @return true if the save was completed. + */ bool SaveBoard( wxString& aFileName, BOARD* aBoard ); /**