From be51be22a7ad726234dcbf695aafbd3dff86cc1b Mon Sep 17 00:00:00 2001 From: Roberto Fernandez Bautista Date: Mon, 22 Mar 2021 20:55:44 +0000 Subject: [PATCH] Update Page Number when Duplicating or Pasting sheets Fixes https://gitlab.com/kicad/code/kicad/-/issues/7872 Also, update hierarchy before renaming pasted sheets Avoids duplicate sheet names when pasting multiple sheets --- eeschema/sch_sheet_path.cpp | 16 ++++++++++++++-- eeschema/sch_sheet_path.h | 2 ++ eeschema/tools/sch_edit_tool.cpp | 11 +++++++++++ eeschema/tools/sch_editor_control.cpp | 11 +++++++++++ 4 files changed, 38 insertions(+), 2 deletions(-) diff --git a/eeschema/sch_sheet_path.cpp b/eeschema/sch_sheet_path.cpp index f7ae3c07cb..dbb2b4ef97 100644 --- a/eeschema/sch_sheet_path.cpp +++ b/eeschema/sch_sheet_path.cpp @@ -482,8 +482,8 @@ void SCH_SHEET_LIST::BuildSheetList( SCH_SHEET* aSheet, bool aCheckIntegrity ) void SCH_SHEET_LIST::SortByPageNumbers( bool aUpdateVirtualPageNums ) { - std::sort( begin(), end(), - []( SCH_SHEET_PATH a, SCH_SHEET_PATH b ) -> bool + std::sort( begin(), end(), + []( SCH_SHEET_PATH a, SCH_SHEET_PATH b ) -> bool { wxString pageA = a.GetPageNumber(); wxString pageB = b.GetPageNumber(); @@ -515,6 +515,18 @@ bool SCH_SHEET_LIST::NameExists( const wxString& aSheetName ) const } +bool SCH_SHEET_LIST::PageNumberExists( const wxString& aPageNumber ) const +{ + for( const SCH_SHEET_PATH& sheet : *this ) + { + if( sheet.Last()->GetPageNumber( sheet ) == aPageNumber ) + return true; + } + + return false; +} + + bool SCH_SHEET_LIST::IsModified() const { for( const SCH_SHEET_PATH& sheet : *this ) diff --git a/eeschema/sch_sheet_path.h b/eeschema/sch_sheet_path.h index 38a1d77e1c..56364c37a3 100644 --- a/eeschema/sch_sheet_path.h +++ b/eeschema/sch_sheet_path.h @@ -457,6 +457,8 @@ public: bool NameExists( const wxString& aSheetName ) const; + bool PageNumberExists( const wxString& aPageNumber ) const; + /** * Update all of the symbol instance information using \a aSymbolInstances. * WARNING: Do not call this on anything other than the full hierarchy. diff --git a/eeschema/tools/sch_edit_tool.cpp b/eeschema/tools/sch_edit_tool.cpp index dff289fd68..8e56a25e9b 100644 --- a/eeschema/tools/sch_edit_tool.cpp +++ b/eeschema/tools/sch_edit_tool.cpp @@ -861,6 +861,17 @@ int SCH_EDIT_TOOL::Duplicate( const TOOL_EVENT& aEvent ) nameField.SetText( candidateName ); sheet->SetParent( m_frame->GetCurrentSheet().Last() ); + + SCH_SHEET_PATH sheetpath = m_frame->GetCurrentSheet(); + sheetpath.push_back( sheet ); + int page = 1; + wxString pageNum = wxString::Format( "%d", page ); + + while( hierarchy.PageNumberExists( pageNum ) ) + pageNum = wxString::Format( "%d", ++page ); + + sheet->AddInstance( sheetpath.Path() ); + sheet->SetPageNumber( sheetpath, pageNum ); m_frame->AddToScreen( sheet, m_frame->GetScreen() ); copiedSheets = true; diff --git a/eeschema/tools/sch_editor_control.cpp b/eeschema/tools/sch_editor_control.cpp index 5a69201ea1..e268967e07 100644 --- a/eeschema/tools/sch_editor_control.cpp +++ b/eeschema/tools/sch_editor_control.cpp @@ -1485,6 +1485,10 @@ int SCH_EDITOR_CONTROL::Paste( const TOOL_EVENT& aEvent ) int uniquifier = std::max( 0, wxAtoi( number ) ) + 1; + // Ensure we have latest hierarchy, as we may have added a sheet in the previous + // iteration + hierarchy = m_frame->Schematic().GetSheets(); + while( hierarchy.NameExists( candidateName ) ) candidateName = wxString::Format( wxT( "%s%d" ), baseName, uniquifier++ ); @@ -1531,7 +1535,14 @@ int SCH_EDITOR_CONTROL::Paste( const TOOL_EVENT& aEvent ) SCH_SHEET_PATH pastePath = pasteRoot; pastePath.push_back( sheet ); + int page = 1; + wxString pageNum = wxString::Format( "%d", page ); + + while( hierarchy.PageNumberExists( pageNum ) ) + pageNum = wxString::Format( "%d", ++page ); + sheet->AddInstance( pastePath.Path() ); + sheet->SetPageNumber( pastePath, pageNum ); updatePastedInstances( pastePath, clipPath, sheet, forceKeepAnnotations ); }