Error reporting for drawing sheet loading.

This commit is contained in:
Jeff Young 2024-06-24 20:22:05 +01:00
parent 26b86b3922
commit 080cbbe0dd
11 changed files with 81 additions and 50 deletions

View File

@ -43,6 +43,7 @@
#include <wx/filedlg.h> #include <wx/filedlg.h>
#include <wx/dcmemory.h> #include <wx/dcmemory.h>
#include <wx/msgdlg.h> #include <wx/msgdlg.h>
#include <confirm.h>
#define MAX_PAGE_EXAMPLE_SIZE 200 #define MAX_PAGE_EXAMPLE_SIZE 200
@ -460,24 +461,24 @@ void DIALOG_PAGES_SETTINGS::OnDateApplyClick( wxCommandEvent& event )
bool DIALOG_PAGES_SETTINGS::SavePageSettings() bool DIALOG_PAGES_SETTINGS::SavePageSettings()
{ {
bool success = false; bool success = false;
wxString msg;
wxString fileName = GetWksFileName(); wxString fileName = GetWksFileName();
if( fileName != BASE_SCREEN::m_DrawingSheetFileName ) if( fileName != BASE_SCREEN::m_DrawingSheetFileName )
{ {
wxString fullFileName = DS_DATA_MODEL::ResolvePath( fileName, m_projectPath ); 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; DisplayErrorMessage( this,
msg.Printf( _( "Drawing sheet file '%s' not found." ), fullFileName ); wxString::Format( _( "Error loading drawing sheet '%s'." ),
wxMessageBox( msg ); fullFileName ),
return false; msg );
} }
BASE_SCREEN::m_DrawingSheetFileName = fileName;
DS_DATA_MODEL::GetTheInstance().LoadDrawingSheet( fullFileName );
m_localPrjConfigChanged = true; m_localPrjConfigChanged = true;
} }
@ -774,8 +775,9 @@ void DIALOG_PAGES_SETTINGS::GetCustomSizeMilsFromDialog()
void DIALOG_PAGES_SETTINGS::OnWksFileSelection( wxCommandEvent& event ) void DIALOG_PAGES_SETTINGS::OnWksFileSelection( wxCommandEvent& event )
{ {
wxFileName fn = GetWksFileName(); wxFileName fn = GetWksFileName();
wxString name = fn.GetFullName(); wxString name = fn.GetFullName();
wxString path; wxString path;
wxString msg;
if( fn.IsAbsolute() ) if( fn.IsAbsolute() )
{ {
@ -785,7 +787,7 @@ void DIALOG_PAGES_SETTINGS::OnWksFileSelection( wxCommandEvent& event )
{ {
wxFileName expanded( ExpandEnvVarSubstitutions( GetWksFileName(), &m_parentFrame->Prj() ) ); wxFileName expanded( ExpandEnvVarSubstitutions( GetWksFileName(), &m_parentFrame->Prj() ) );
if( expanded.IsAbsolute() ) if( expanded.IsAbsolute() )
path = expanded.GetPath(); path = expanded.GetPath();
else else
path = m_projectPath; path = m_projectPath;
@ -817,15 +819,21 @@ void DIALOG_PAGES_SETTINGS::OnWksFileSelection( wxCommandEvent& event )
std::unique_ptr<DS_DATA_MODEL> ws = std::make_unique<DS_DATA_MODEL>(); std::unique_ptr<DS_DATA_MODEL> ws = std::make_unique<DS_DATA_MODEL>();
if( ws->LoadDrawingSheet( fileName ) ) if( !ws->LoadDrawingSheet( fileName, &msg ) )
{ {
delete m_drawingSheet; DisplayErrorMessage( this,
wxString::Format( _( "Error loading drawing sheet '%s'.\n%s" ),
m_drawingSheet = ws.release(); fileName ),
msg );
SetWksFileName( shortFileName ); return;
GetPageLayoutInfoFromDialog();
UpdateDrawingSheetExample();
} }
delete m_drawingSheet;
m_drawingSheet = ws.release();
SetWksFileName( shortFileName );
GetPageLayoutInfoFromDialog();
UpdateDrawingSheetExample();
} }

View File

@ -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( !aAppend )
if( !Append )
{ {
if( fullFileName.IsEmpty() ) if( aFullFileName.IsEmpty() )
{ {
SetDefaultLayout(); SetDefaultLayout();
return true; // we assume its fine / default init return true; // we assume its fine / default init
} }
if( !wxFileExists( fullFileName ) ) if( !wxFileExists( aFullFileName ) )
{ {
if( aMsg )
*aMsg = _( "File not found." );
SetDefaultLayout(); SetDefaultLayout();
return false; return false;
} }
} }
wxFFile wksFile( fullFileName, wxS( "rb" ) ); wxFFile wksFile( aFullFileName, wxS( "rb" ) );
if( ! wksFile.IsOpened() ) if( ! wksFile.IsOpened() )
{ {
if( !Append ) if( aMsg )
*aMsg = _( "File could not be opened." );
if( !aAppend )
SetDefaultLayout(); SetDefaultLayout();
return false; return false;
@ -993,23 +997,36 @@ bool DS_DATA_MODEL::LoadDrawingSheet( const wxString& aFullFileName, bool Append
if( wksFile.Read( buffer.get(), filelen ) != filelen ) if( wksFile.Read( buffer.get(), filelen ) != filelen )
{ {
if( aMsg )
*aMsg = _( "Drawing sheet was not fully read." );
return false; return false;
} }
else else
{ {
buffer[filelen]=0; buffer[filelen]=0;
if( ! Append ) if( ! aAppend )
ClearList(); ClearList();
DRAWING_SHEET_PARSER parser( buffer.get(), fullFileName ); DRAWING_SHEET_PARSER parser( buffer.get(), aFullFileName );
try try
{ {
parser.Parse( this ); 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; return false;
} }
} }

View File

@ -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 // Build the basic layout shape, if the layout list is empty
if( model.GetCount() == 0 && !model.VoidListAllowed() ) if( model.GetCount() == 0 && !model.VoidListAllowed() )
model.LoadDrawingSheet(); model.LoadDrawingSheet( wxEmptyString, nullptr );
model.SetupDrawEnvironment( aPageInfo, GetMilsToIUfactor() ); model.SetupDrawEnvironment( aPageInfo, GetMilsToIUfactor() );

View File

@ -74,7 +74,7 @@ bool SCH_EDIT_FRAME::LoadProjectSettings()
wxString filename = DS_DATA_MODEL::ResolvePath( BASE_SCREEN::m_DrawingSheetFileName, wxString filename = DS_DATA_MODEL::ResolvePath( BASE_SCREEN::m_DrawingSheetFileName,
Prj().GetProjectPath() ); Prj().GetProjectPath() );
if( !DS_DATA_MODEL::GetTheInstance().LoadDrawingSheet( filename ) ) if( !DS_DATA_MODEL::GetTheInstance().LoadDrawingSheet( filename, nullptr ) )
ShowInfoBarError( _( "Error loading drawing sheet." ), true ); ShowInfoBarError( _( "Error loading drawing sheet." ), true );
PROJECT_LOCAL_SETTINGS& localSettings = Prj().GetLocalSettings(); PROJECT_LOCAL_SETTINGS& localSettings = Prj().GetLocalSettings();

View File

@ -113,11 +113,13 @@ void EESCHEMA_JOBS_HANDLER::InitRenderSettings( SCH_RENDER_SETTINGS* aRenderSett
{ {
wxString absolutePath = DS_DATA_MODEL::ResolvePath( path, wxString absolutePath = DS_DATA_MODEL::ResolvePath( path,
aSch->Prj().GetProjectPath() ); 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'." ), m_reporter->Report( wxString::Format( _( "Error loading drawing sheet '%s'." ),
path ), path )
+ wxS( "\n" ) + msg + wxS( "\n" ),
RPT_SEVERITY_ERROR ); RPT_SEVERITY_ERROR );
return false; return false;
} }

View File

@ -149,9 +149,10 @@ public:
* @param aFullFileName is the custom drawing sheet file. If empty, load the file defined by * @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 * KICAD_WKSFILE and if its not defined, the default internal drawing
* sheet. * 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. * Populate the list from a S expr description stored in a string.

View File

@ -235,13 +235,14 @@ bool PL_EDITOR_FRAME::LoadDrawingSheetFile( const wxString& aFullFileName )
{ {
if( wxFileExists( aFullFileName ) ) if( wxFileExists( aFullFileName ) )
{ {
bool loaded = false; wxString msg;
loaded = DS_DATA_MODEL::GetTheInstance().LoadDrawingSheet( aFullFileName ); if( !DS_DATA_MODEL::GetTheInstance().LoadDrawingSheet( aFullFileName, &msg ) )
if( !loaded )
{ {
ShowInfoBarError( _( "Error reading drawing sheet" ), true ); DisplayErrorMessage( this,
wxString::Format( _( "Error loading drawing sheet '%s'." ),
aFullFileName ),
msg );
return false; return false;
} }
@ -282,7 +283,7 @@ bool PL_EDITOR_FRAME::InsertDrawingSheetFile( const wxString& aFullFileName )
{ {
const bool append = true; const bool append = true;
SaveCopyInUndoList(); SaveCopyInUndoList();
DS_DATA_MODEL::GetTheInstance().LoadDrawingSheet( aFullFileName, append ); DS_DATA_MODEL::GetTheInstance().LoadDrawingSheet( aFullFileName, nullptr, append );
return true; return true;
} }

View File

@ -216,7 +216,7 @@ PL_EDITOR_FRAME::PL_EDITOR_FRAME( KIWAY* aKiway, wxWindow* aParent ) :
DS_DATA_MODEL::GetTheInstance().AllowVoidList( true ); DS_DATA_MODEL::GetTheInstance().AllowVoidList( true );
DS_DATA_MODEL::GetTheInstance().ClearList(); DS_DATA_MODEL::GetTheInstance().ClearList();
#else // start with the default KiCad layout #else // start with the default KiCad layout
DS_DATA_MODEL::GetTheInstance().LoadDrawingSheet(); DS_DATA_MODEL::GetTheInstance().LoadDrawingSheet( wxEmptyString, nullptr );
#endif #endif
OnNewDrawingSheet(); OnNewDrawingSheet();

View File

@ -51,7 +51,7 @@ bool PCB_EDIT_FRAME::LoadProjectSettings()
wxString filename = DS_DATA_MODEL::ResolvePath( BASE_SCREEN::m_DrawingSheetFileName, wxString filename = DS_DATA_MODEL::ResolvePath( BASE_SCREEN::m_DrawingSheetFileName,
Prj().GetProjectPath()); Prj().GetProjectPath());
if( !DS_DATA_MODEL::GetTheInstance().LoadDrawingSheet( filename ) ) if( !DS_DATA_MODEL::GetTheInstance().LoadDrawingSheet( filename, nullptr ) )
ShowInfoBarError( _( "Error loading drawing sheet." ), true ); ShowInfoBarError( _( "Error loading drawing sheet." ), true );
// Load render settings that aren't stored in PCB_DISPLAY_OPTIONS // Load render settings that aren't stored in PCB_DISPLAY_OPTIONS

View File

@ -1553,11 +1553,13 @@ void PCBNEW_JOBS_HANDLER::loadOverrideDrawingSheet( BOARD* aBrd, const wxString&
BASE_SCREEN::m_DrawingSheetFileName = path; BASE_SCREEN::m_DrawingSheetFileName = path;
wxString filename = DS_DATA_MODEL::ResolvePath( BASE_SCREEN::m_DrawingSheetFileName, wxString filename = DS_DATA_MODEL::ResolvePath( BASE_SCREEN::m_DrawingSheetFileName,
aBrd->GetProject()->GetProjectPath() ); 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" ), m_reporter->Report( wxString::Format( _( "Error loading drawing sheet '%s'." ),
path ), path )
+ wxS( "\n" ) + msg + wxS( "\n" ),
RPT_SEVERITY_ERROR ); RPT_SEVERITY_ERROR );
return false; return false;
} }

View File

@ -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, wxString filename = DS_DATA_MODEL::ResolvePath( BASE_SCREEN::m_DrawingSheetFileName,
project->GetProjectPath() ); project->GetProjectPath() );
if( !DS_DATA_MODEL::GetTheInstance().LoadDrawingSheet( filename ) ) if( !DS_DATA_MODEL::GetTheInstance().LoadDrawingSheet( filename, nullptr ) )
wxFprintf( stderr, _( "Error loading drawing sheet." ) ); wxFprintf( stderr, _( "Error loading drawing sheet." ) );
BOARD* brd = PCB_IO_MGR::Load( aFormat, aFileName ); BOARD* brd = PCB_IO_MGR::Load( aFormat, aFileName );