From c7fa5c567fd9388f019800f49b968073e2523858 Mon Sep 17 00:00:00 2001 From: Jeff Young Date: Fri, 31 May 2019 23:03:27 +0100 Subject: [PATCH] Load sheet content after a paste. Fixes: lp:1830633 * https://bugs.launchpad.net/kicad/+bug/1830633 --- eeschema/sch_edit_frame.h | 5 +- eeschema/sheet.cpp | 83 +++++++++++++++++---------- eeschema/tools/sch_editor_control.cpp | 2 +- 3 files changed, 57 insertions(+), 33 deletions(-) diff --git a/eeschema/sch_edit_frame.h b/eeschema/sch_edit_frame.h index 566377b5dd..a3aa352037 100644 --- a/eeschema/sch_edit_frame.h +++ b/eeschema/sch_edit_frame.h @@ -874,7 +874,10 @@ public: */ bool EditSheet( SCH_SHEET* aSheet, SCH_SHEET_PATH* aHierarchy, bool* aClearAnnotationNewItems ); - void InitSheet( SCH_SHEET* aSheet, const wxString& aFilename ); + void InitSheet( SCH_SHEET* aSheet, const wxString& aNewFilename ); + + void LoadSheetFromFile( SCH_SHEET* aSheet, SCH_SHEET_PATH* aHierarchy, + const wxString& aExistingFilename ); wxPoint GetLastSheetPinPosition() const { return m_lastSheetPinPosition; } diff --git a/eeschema/sheet.cpp b/eeschema/sheet.cpp index 85953f43c8..245b50d015 100644 --- a/eeschema/sheet.cpp +++ b/eeschema/sheet.cpp @@ -38,12 +38,59 @@ #include -void SCH_EDIT_FRAME::InitSheet( SCH_SHEET* aSheet, const wxString& aFilename ) +void SCH_EDIT_FRAME::InitSheet( SCH_SHEET* aSheet, const wxString& aNewFilename ) { aSheet->SetScreen( new SCH_SCREEN( &Kiway() ) ); aSheet->GetScreen()->SetModify(); aSheet->GetScreen()->SetMaxUndoItems( m_UndoRedoCountMax ); - aSheet->GetScreen()->SetFileName( aFilename ); + aSheet->GetScreen()->SetFileName( aNewFilename ); +} + + +void SCH_EDIT_FRAME::LoadSheetFromFile( SCH_SHEET* aSheet, SCH_SHEET_PATH* aHierarchy, + const wxString& aExistingFilename ) +{ + SCH_PLUGIN::SCH_PLUGIN_RELEASER pi( SCH_IO_MGR::FindPlugin( SCH_IO_MGR::SCH_LEGACY ) ); + + wxFileName fileName( aExistingFilename ); + + if( !fileName.IsAbsolute() ) + { + const SCH_SCREEN* currentScreen = aHierarchy->LastScreen(); + wxFileName currentSheetFileName = currentScreen->GetFileName(); + fileName.Normalize( wxPATH_NORM_ALL, currentSheetFileName.GetPath() ); + } + + wxString fullFilename = fileName.GetFullPath(); + + try + { + pi->Load( fullFilename, &Kiway(), aSheet ); + + if( !pi->GetError().IsEmpty() ) + { + DisplayErrorMessage( this, _( "The entire schematic could not be loaded.\n" + "Errors occurred loading hierarchical sheets." ), + pi->GetError() ); + } + } + catch( const IO_ERROR& ioe ) + { + wxString msg; + + msg.Printf( _( "Error occurred loading schematic file \"%s\"." ), fullFilename ); + DisplayErrorMessage( this, msg, ioe.What() ); + + msg.Printf( _( "Failed to load schematic \"%s\"" ), fullFilename ); + AppendMsgPanel( wxEmptyString, msg, CYAN ); + + return; + } + + SCH_SCREEN* screen = aSheet->GetScreen(); + + if( screen ) + screen->UpdateSymbolLinks( true ); } @@ -98,8 +145,6 @@ bool SCH_EDIT_FRAME::EditSheet( SCH_SHEET* aSheet, SCH_SHEET_PATH* aHierarchy, // Inside Eeschema, filenames are stored using unix notation newFilename.Replace( wxT( "\\" ), wxT( "/" ) ); - SCH_PLUGIN::SCH_PLUGIN_RELEASER pi( SCH_IO_MGR::FindPlugin( SCH_IO_MGR::SCH_LEGACY ) ); - if( aSheet->GetScreen() == NULL ) // New sheet. { if( useScreen || loadFromFile ) // Load from existing file. @@ -187,6 +232,8 @@ bool SCH_EDIT_FRAME::EditSheet( SCH_SHEET* aSheet, SCH_SHEET_PATH* aHierarchy, if( renameFile ) { + SCH_PLUGIN::SCH_PLUGIN_RELEASER pi( SCH_IO_MGR::FindPlugin( SCH_IO_MGR::SCH_LEGACY ) ); + // If the the associated screen is shared by more than one sheet, do not // change the filename of the corresponding screen here. // (a new screen will be created later) @@ -230,33 +277,7 @@ bool SCH_EDIT_FRAME::EditSheet( SCH_SHEET* aSheet, SCH_SHEET_PATH* aHierarchy, } else if( loadFromFile ) { - try - { - aSheet = pi->Load( newFilename, &Kiway(), aSheet ); - - if( !pi->GetError().IsEmpty() ) - { - DisplayErrorMessage( this, - _( "The entire schematic could not be loaded.\n" - "Errors occurred loading hierarchical sheets." ), - pi->GetError() ); - } - } - catch( const IO_ERROR& ioe ) - { - msg.Printf( _( "Error occurred loading schematic file \"%s\"." ), newFilename ); - DisplayErrorMessage( this, msg, ioe.What() ); - - msg.Printf( _( "Failed to load schematic \"%s\"" ), newFilename ); - AppendMsgPanel( wxEmptyString, msg, CYAN ); - - return false; - } - - SCH_SCREEN* screen = aSheet->GetScreen(); - - if( screen ) - screen->UpdateSymbolLinks( true ); + LoadSheetFromFile( aSheet, aHierarchy, newFilename ); } aSheet->SetFileNameSize( dlg.GetFileNameTextSize() ); diff --git a/eeschema/tools/sch_editor_control.cpp b/eeschema/tools/sch_editor_control.cpp index 22a160c8fa..5e820d9945 100644 --- a/eeschema/tools/sch_editor_control.cpp +++ b/eeschema/tools/sch_editor_control.cpp @@ -972,7 +972,7 @@ int SCH_EDITOR_CONTROL::Paste( const TOOL_EVENT& aEvent ) else if( item->Type() == SCH_SHEET_T ) { SCH_SHEET* sheet = (SCH_SHEET*) item; - m_frame->InitSheet( sheet, sheet->GetFileName() ); + m_frame->LoadSheetFromFile( sheet, g_CurrentSheet, sheet->GetFileName() ); } item->SetFlags( IS_NEW | IS_PASTED | IS_MOVED );