From 3f31d48b5e04a5348b056e58c3b25020929ead4f Mon Sep 17 00:00:00 2001 From: Jeff Young Date: Sat, 29 Feb 2020 00:05:50 +0000 Subject: [PATCH] Use a more friendly method of ensuring unique sheet names. Also fixes a bug where we were failing to respect the forceKeepAnnotations flag when processing content nested in a sheet. Fixes https://gitlab.com/kicad/code/kicad/issues/3681 --- eeschema/tools/sch_editor_control.cpp | 22 +++++++++++++++++++--- 1 file changed, 19 insertions(+), 3 deletions(-) diff --git a/eeschema/tools/sch_editor_control.cpp b/eeschema/tools/sch_editor_control.cpp index bcf90e88a5..92deb6bd8a 100644 --- a/eeschema/tools/sch_editor_control.cpp +++ b/eeschema/tools/sch_editor_control.cpp @@ -940,6 +940,18 @@ int SCH_EDITOR_CONTROL::Copy( const TOOL_EVENT& aEvent ) } +bool sheetNameExists( const SCH_SHEET_LIST& hierarchy, const wxString& aName ) +{ + for( const SCH_SHEET_PATH& sheet : hierarchy ) + { + if( sheet.Last()->GetName() == aName ) + return true; + } + + return false; +} + + int SCH_EDITOR_CONTROL::Paste( const TOOL_EVENT& aEvent ) { wxTextEntry* textEntry = dynamic_cast( wxWindow::FindFocus() ); @@ -1078,9 +1090,13 @@ int SCH_EDITOR_CONTROL::Paste( const TOOL_EVENT& aEvent ) wxFileName fn = sheet->GetFileName(); SCH_SCREEN* existingScreen = nullptr; bool dropSheetAnnotations = false; + wxString sheetName = sheet->GetName(); + int uniquifier = 1; - // Duplicate sheet names are not valid. Generate new UUID-based sheet names. - sheet->SetName( wxString::Format( wxT( "Sheet%s" ), sheet->m_Uuid.AsString() ) ); + while( sheetNameExists( hierarchy, sheetName ) ) + sheetName = sheet->GetName() << uniquifier++; + + sheet->SetName( sheetName ); sheet->SetParent( g_CurrentSheet->Last() ); sheet->SetScreen( nullptr ); @@ -1093,7 +1109,7 @@ int SCH_EDITOR_CONTROL::Paste( const TOOL_EVENT& aEvent ) } if( g_RootSheet->SearchHierarchy( fn.GetFullPath( wxPATH_UNIX ), &existingScreen ) ) - dropSheetAnnotations = true; + dropSheetAnnotations = !forceKeepAnnotations; else searchSupplementaryClipboard( sheet->GetFileName(), &existingScreen );