Fix issues calling SaveBoard on board loaded from PcbNew frame

KiCad cannot have more than one board/project open, so disallow
the Python APIs for loading additional boards when the frame is
present.  Inherit the SETTINGS_MANAGER from the frame when present
instead of creating a new one that won't have the loaded project.
This commit is contained in:
Jon Evans 2021-08-14 09:18:11 -04:00
parent bff247b08a
commit 8eba0b356b
2 changed files with 37 additions and 7 deletions

View File

@ -74,6 +74,10 @@ void ScriptingSetPcbEditFrame( PCB_EDIT_FRAME* aPcbEditFrame )
BOARD* LoadBoard( wxString& aFileName ) BOARD* LoadBoard( wxString& aFileName )
{ {
// Loading a new board is not possible if running inside KiCad
if( s_PcbEditFrame )
return nullptr;
if( aFileName.EndsWith( KiCadPcbFileExtension ) ) if( aFileName.EndsWith( KiCadPcbFileExtension ) )
return LoadBoard( aFileName, IO_MGR::KICAD_SEXP ); return LoadBoard( aFileName, IO_MGR::KICAD_SEXP );
else if( aFileName.EndsWith( LegacyPcbFileExtension ) ) else if( aFileName.EndsWith( LegacyPcbFileExtension ) )
@ -88,9 +92,16 @@ SETTINGS_MANAGER* GetSettingsManager()
{ {
if( !s_SettingsManager ) if( !s_SettingsManager )
{ {
// Ensure wx system settings stuff is available if( s_PcbEditFrame )
static_cast<void>( wxTheApp ); {
s_SettingsManager = new SETTINGS_MANAGER( true ); s_SettingsManager = s_PcbEditFrame->GetSettingsManager();
}
else
{
// Ensure wx system settings stuff is available
static_cast<void>( wxTheApp );
s_SettingsManager = new SETTINGS_MANAGER( true );
}
} }
return s_SettingsManager; return s_SettingsManager;
@ -117,6 +128,9 @@ PROJECT* GetDefaultProject()
BOARD* LoadBoard( wxString& aFileName, IO_MGR::PCB_FILE_T aFormat ) 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; wxFileName pro = aFileName;
pro.SetExt( ProjectFileExtension ); pro.SetExt( ProjectFileExtension );
pro.MakeAbsolute(); pro.MakeAbsolute();
@ -179,6 +193,10 @@ BOARD* LoadBoard( wxString& aFileName, IO_MGR::PCB_FILE_T aFormat )
BOARD* CreateEmptyBoard() BOARD* CreateEmptyBoard()
{ {
// Creating a new board is not possible if running inside KiCad
if( s_PcbEditFrame )
return nullptr;
BOARD* brd = new BOARD(); BOARD* brd = new BOARD();
brd->SetProject( GetDefaultProject() ); brd->SetProject( GetDefaultProject() );
@ -192,14 +210,21 @@ bool SaveBoard( wxString& aFileName, BOARD* aBoard, IO_MGR::PCB_FILE_T aFormat )
aBoard->BuildConnectivity(); aBoard->BuildConnectivity();
aBoard->SynchronizeNetsAndNetClasses(); aBoard->SynchronizeNetsAndNetClasses();
IO_MGR::Save( aFormat, aFileName, aBoard, nullptr ); try
{
IO_MGR::Save( aFormat, aFileName, aBoard, nullptr );
}
catch( ... )
{
return false;
}
wxFileName pro = aFileName; wxFileName pro = aFileName;
pro.SetExt( ProjectFileExtension ); pro.SetExt( ProjectFileExtension );
pro.MakeAbsolute(); pro.MakeAbsolute();
wxString projectPath = pro.GetFullPath(); wxString projectPath = pro.GetFullPath();
GetSettingsManager()->SaveProject( pro.GetFullPath() ); GetSettingsManager()->SaveProjectAs( pro.GetFullPath() );
return true; return true;
} }

View File

@ -54,8 +54,13 @@ SETTINGS_MANAGER* GetSettingsManager();
*/ */
BOARD* CreateEmptyBoard(); 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 ); bool SaveBoard( wxString& aFileName, BOARD* aBoard );
/** /**