diff --git a/common/dialogs/dialog_page_settings.cpp b/common/dialogs/dialog_page_settings.cpp index cbf4179aa3..ccb56ce47c 100644 --- a/common/dialogs/dialog_page_settings.cpp +++ b/common/dialogs/dialog_page_settings.cpp @@ -43,6 +43,7 @@ #include #include #include +#include #define MAX_PAGE_EXAMPLE_SIZE 200 @@ -460,24 +461,24 @@ void DIALOG_PAGES_SETTINGS::OnDateApplyClick( wxCommandEvent& event ) bool DIALOG_PAGES_SETTINGS::SavePageSettings() { - bool success = false; - + bool success = false; + wxString msg; wxString fileName = GetWksFileName(); if( fileName != BASE_SCREEN::m_DrawingSheetFileName ) { wxString fullFileName = DS_DATA_MODEL::ResolvePath( fileName, m_projectPath ); - if( !fullFileName.IsEmpty() && !wxFileExists( fullFileName ) ) + BASE_SCREEN::m_DrawingSheetFileName = fileName; + + if( !DS_DATA_MODEL::GetTheInstance().LoadDrawingSheet( fullFileName, &msg ) ) { - wxString msg; - msg.Printf( _( "Drawing sheet file '%s' not found." ), fullFileName ); - wxMessageBox( msg ); - return false; + DisplayErrorMessage( this, + wxString::Format( _( "Error loading drawing sheet '%s'." ), + fullFileName ), + msg ); } - BASE_SCREEN::m_DrawingSheetFileName = fileName; - DS_DATA_MODEL::GetTheInstance().LoadDrawingSheet( fullFileName ); m_localPrjConfigChanged = true; } @@ -774,8 +775,9 @@ void DIALOG_PAGES_SETTINGS::GetCustomSizeMilsFromDialog() void DIALOG_PAGES_SETTINGS::OnWksFileSelection( wxCommandEvent& event ) { wxFileName fn = GetWksFileName(); - wxString name = fn.GetFullName(); - wxString path; + wxString name = fn.GetFullName(); + wxString path; + wxString msg; if( fn.IsAbsolute() ) { @@ -785,7 +787,7 @@ void DIALOG_PAGES_SETTINGS::OnWksFileSelection( wxCommandEvent& event ) { wxFileName expanded( ExpandEnvVarSubstitutions( GetWksFileName(), &m_parentFrame->Prj() ) ); - if( expanded.IsAbsolute() ) + if( expanded.IsAbsolute() ) path = expanded.GetPath(); else path = m_projectPath; @@ -817,15 +819,21 @@ void DIALOG_PAGES_SETTINGS::OnWksFileSelection( wxCommandEvent& event ) std::unique_ptr ws = std::make_unique(); - if( ws->LoadDrawingSheet( fileName ) ) + if( !ws->LoadDrawingSheet( fileName, &msg ) ) { - delete m_drawingSheet; - - m_drawingSheet = ws.release(); - - SetWksFileName( shortFileName ); - - GetPageLayoutInfoFromDialog(); - UpdateDrawingSheetExample(); + DisplayErrorMessage( this, + wxString::Format( _( "Error loading drawing sheet '%s'.\n%s" ), + fileName ), + msg ); + return; } + + delete m_drawingSheet; + + m_drawingSheet = ws.release(); + + SetWksFileName( shortFileName ); + + GetPageLayoutInfoFromDialog(); + UpdateDrawingSheetExample(); } diff --git a/common/drawing_sheet/drawing_sheet_parser.cpp b/common/drawing_sheet/drawing_sheet_parser.cpp index ed24e8d185..03018bc302 100644 --- a/common/drawing_sheet/drawing_sheet_parser.cpp +++ b/common/drawing_sheet/drawing_sheet_parser.cpp @@ -959,30 +959,34 @@ void DS_DATA_MODEL::SetPageLayout( const char* aPageLayout, bool Append, const w } -bool DS_DATA_MODEL::LoadDrawingSheet( const wxString& aFullFileName, bool Append ) +bool DS_DATA_MODEL::LoadDrawingSheet( const wxString& aFullFileName, wxString* aMsg, bool aAppend ) { - wxString fullFileName = aFullFileName; - - if( !Append ) + if( !aAppend ) { - if( fullFileName.IsEmpty() ) + if( aFullFileName.IsEmpty() ) { SetDefaultLayout(); return true; // we assume its fine / default init } - if( !wxFileExists( fullFileName ) ) + if( !wxFileExists( aFullFileName ) ) { + if( aMsg ) + *aMsg = _( "File not found." ); + SetDefaultLayout(); return false; } } - wxFFile wksFile( fullFileName, wxS( "rb" ) ); + wxFFile wksFile( aFullFileName, wxS( "rb" ) ); if( ! wksFile.IsOpened() ) { - if( !Append ) + if( aMsg ) + *aMsg = _( "File could not be opened." ); + + if( !aAppend ) SetDefaultLayout(); return false; @@ -993,23 +997,36 @@ bool DS_DATA_MODEL::LoadDrawingSheet( const wxString& aFullFileName, bool Append if( wksFile.Read( buffer.get(), filelen ) != filelen ) { + if( aMsg ) + *aMsg = _( "Drawing sheet was not fully read." ); + return false; } else { buffer[filelen]=0; - if( ! Append ) + if( ! aAppend ) ClearList(); - DRAWING_SHEET_PARSER parser( buffer.get(), fullFileName ); + DRAWING_SHEET_PARSER parser( buffer.get(), aFullFileName ); try { parser.Parse( this ); } - catch( ... ) + catch( const IO_ERROR& ioe ) { + if( aMsg ) + *aMsg = ioe.What(); + + return false; + } + catch( const std::bad_alloc& ) + { + if( aMsg ) + *aMsg = _( "Ran out of memory." ); + return false; } } diff --git a/common/drawing_sheet/ds_draw_item.cpp b/common/drawing_sheet/ds_draw_item.cpp index f87280e2d0..7fedf7c5ec 100644 --- a/common/drawing_sheet/ds_draw_item.cpp +++ b/common/drawing_sheet/ds_draw_item.cpp @@ -552,7 +552,7 @@ void DS_DRAW_ITEM_LIST::BuildDrawItemsList( const PAGE_INFO& aPageInfo, // Build the basic layout shape, if the layout list is empty if( model.GetCount() == 0 && !model.VoidListAllowed() ) - model.LoadDrawingSheet(); + model.LoadDrawingSheet( wxEmptyString, nullptr ); model.SetupDrawEnvironment( aPageInfo, GetMilsToIUfactor() ); diff --git a/eeschema/eeschema_config.cpp b/eeschema/eeschema_config.cpp index 4dde935456..6226561554 100644 --- a/eeschema/eeschema_config.cpp +++ b/eeschema/eeschema_config.cpp @@ -74,7 +74,7 @@ bool SCH_EDIT_FRAME::LoadProjectSettings() wxString filename = DS_DATA_MODEL::ResolvePath( BASE_SCREEN::m_DrawingSheetFileName, Prj().GetProjectPath() ); - if( !DS_DATA_MODEL::GetTheInstance().LoadDrawingSheet( filename ) ) + if( !DS_DATA_MODEL::GetTheInstance().LoadDrawingSheet( filename, nullptr ) ) ShowInfoBarError( _( "Error loading drawing sheet." ), true ); PROJECT_LOCAL_SETTINGS& localSettings = Prj().GetLocalSettings(); diff --git a/eeschema/eeschema_jobs_handler.cpp b/eeschema/eeschema_jobs_handler.cpp index d25e27efb3..0ad6b7beef 100644 --- a/eeschema/eeschema_jobs_handler.cpp +++ b/eeschema/eeschema_jobs_handler.cpp @@ -113,11 +113,13 @@ void EESCHEMA_JOBS_HANDLER::InitRenderSettings( SCH_RENDER_SETTINGS* aRenderSett { wxString absolutePath = DS_DATA_MODEL::ResolvePath( path, aSch->Prj().GetProjectPath() ); + wxString msg; - if( !DS_DATA_MODEL::GetTheInstance().LoadDrawingSheet( absolutePath ) ) + if( !DS_DATA_MODEL::GetTheInstance().LoadDrawingSheet( absolutePath, &msg ) ) { m_reporter->Report( wxString::Format( _( "Error loading drawing sheet '%s'." ), - path ), + path ) + + wxS( "\n" ) + msg + wxS( "\n" ), RPT_SEVERITY_ERROR ); return false; } diff --git a/include/drawing_sheet/ds_data_model.h b/include/drawing_sheet/ds_data_model.h index c839cc5fcc..0cc128fa98 100644 --- a/include/drawing_sheet/ds_data_model.h +++ b/include/drawing_sheet/ds_data_model.h @@ -149,9 +149,10 @@ public: * @param aFullFileName is the custom drawing sheet file. If empty, load the file defined by * KICAD_WKSFILE and if its not defined, the default internal drawing * sheet. - * @param Append if true: do not delete old layout, and load only \a aFullFileName. + * @param aMsg [optional] if non-null, will be filled with any error messages. + * @param aAppend if true: do not delete old layout, and load only \a aFullFileName. */ - bool LoadDrawingSheet( const wxString& aFullFileName = wxEmptyString, bool Append = false ); + bool LoadDrawingSheet( const wxString& aFullFileName, wxString* aMsg, bool aAppend = false ); /** * Populate the list from a S expr description stored in a string. diff --git a/pagelayout_editor/files.cpp b/pagelayout_editor/files.cpp index 6df21e0034..8c71584bd9 100644 --- a/pagelayout_editor/files.cpp +++ b/pagelayout_editor/files.cpp @@ -235,13 +235,14 @@ bool PL_EDITOR_FRAME::LoadDrawingSheetFile( const wxString& aFullFileName ) { if( wxFileExists( aFullFileName ) ) { - bool loaded = false; + wxString msg; - loaded = DS_DATA_MODEL::GetTheInstance().LoadDrawingSheet( aFullFileName ); - - if( !loaded ) + if( !DS_DATA_MODEL::GetTheInstance().LoadDrawingSheet( aFullFileName, &msg ) ) { - ShowInfoBarError( _( "Error reading drawing sheet" ), true ); + DisplayErrorMessage( this, + wxString::Format( _( "Error loading drawing sheet '%s'." ), + aFullFileName ), + msg ); return false; } @@ -282,7 +283,7 @@ bool PL_EDITOR_FRAME::InsertDrawingSheetFile( const wxString& aFullFileName ) { const bool append = true; SaveCopyInUndoList(); - DS_DATA_MODEL::GetTheInstance().LoadDrawingSheet( aFullFileName, append ); + DS_DATA_MODEL::GetTheInstance().LoadDrawingSheet( aFullFileName, nullptr, append ); return true; } diff --git a/pagelayout_editor/pl_editor_frame.cpp b/pagelayout_editor/pl_editor_frame.cpp index 93d4b2b771..34d965378b 100644 --- a/pagelayout_editor/pl_editor_frame.cpp +++ b/pagelayout_editor/pl_editor_frame.cpp @@ -216,7 +216,7 @@ PL_EDITOR_FRAME::PL_EDITOR_FRAME( KIWAY* aKiway, wxWindow* aParent ) : DS_DATA_MODEL::GetTheInstance().AllowVoidList( true ); DS_DATA_MODEL::GetTheInstance().ClearList(); #else // start with the default KiCad layout - DS_DATA_MODEL::GetTheInstance().LoadDrawingSheet(); + DS_DATA_MODEL::GetTheInstance().LoadDrawingSheet( wxEmptyString, nullptr ); #endif OnNewDrawingSheet(); diff --git a/pcbnew/pcbnew_config.cpp b/pcbnew/pcbnew_config.cpp index 4c33ff11bf..415f46fcec 100644 --- a/pcbnew/pcbnew_config.cpp +++ b/pcbnew/pcbnew_config.cpp @@ -51,7 +51,7 @@ bool PCB_EDIT_FRAME::LoadProjectSettings() wxString filename = DS_DATA_MODEL::ResolvePath( BASE_SCREEN::m_DrawingSheetFileName, Prj().GetProjectPath()); - if( !DS_DATA_MODEL::GetTheInstance().LoadDrawingSheet( filename ) ) + if( !DS_DATA_MODEL::GetTheInstance().LoadDrawingSheet( filename, nullptr ) ) ShowInfoBarError( _( "Error loading drawing sheet." ), true ); // Load render settings that aren't stored in PCB_DISPLAY_OPTIONS diff --git a/pcbnew/pcbnew_jobs_handler.cpp b/pcbnew/pcbnew_jobs_handler.cpp index 70ad9ce65f..d12115703c 100644 --- a/pcbnew/pcbnew_jobs_handler.cpp +++ b/pcbnew/pcbnew_jobs_handler.cpp @@ -1553,11 +1553,13 @@ void PCBNEW_JOBS_HANDLER::loadOverrideDrawingSheet( BOARD* aBrd, const wxString& BASE_SCREEN::m_DrawingSheetFileName = path; wxString filename = DS_DATA_MODEL::ResolvePath( BASE_SCREEN::m_DrawingSheetFileName, aBrd->GetProject()->GetProjectPath() ); + wxString msg; - if( !DS_DATA_MODEL::GetTheInstance().LoadDrawingSheet( filename ) ) + if( !DS_DATA_MODEL::GetTheInstance().LoadDrawingSheet( filename, &msg ) ) { - m_reporter->Report( wxString::Format( _( "Error loading drawing sheet '%s'." ) + wxS( "\n" ), - path ), + m_reporter->Report( wxString::Format( _( "Error loading drawing sheet '%s'." ), + path ) + + wxS( "\n" ) + msg + wxS( "\n" ), RPT_SEVERITY_ERROR ); return false; } diff --git a/pcbnew/python/scripting/pcbnew_scripting_helpers.cpp b/pcbnew/python/scripting/pcbnew_scripting_helpers.cpp index aea45f7e01..17f585c754 100644 --- a/pcbnew/python/scripting/pcbnew_scripting_helpers.cpp +++ b/pcbnew/python/scripting/pcbnew_scripting_helpers.cpp @@ -184,7 +184,7 @@ BOARD* LoadBoard( wxString& aFileName, PCB_IO_MGR::PCB_FILE_T aFormat, bool aSet wxString filename = DS_DATA_MODEL::ResolvePath( BASE_SCREEN::m_DrawingSheetFileName, project->GetProjectPath() ); - if( !DS_DATA_MODEL::GetTheInstance().LoadDrawingSheet( filename ) ) + if( !DS_DATA_MODEL::GetTheInstance().LoadDrawingSheet( filename, nullptr ) ) wxFprintf( stderr, _( "Error loading drawing sheet." ) ); BOARD* brd = PCB_IO_MGR::Load( aFormat, aFileName );