From 4fcc2ac7e2888cec0da8c7439280cf6f65cebfc7 Mon Sep 17 00:00:00 2001 From: Seth Hillbrand Date: Mon, 15 Sep 2025 16:48:48 -0700 Subject: [PATCH] Ensure new symbols obey the observed sheet number Internal numerology will not make sense when annotating, so use the observed sheet number and let the algo ensure that numbers are not duplicated Fixes https://gitlab.com/kicad/code/kicad/-/issues/21637 Fixes https://gitlab.com/kicad/code/kicad/-/issues/19138 Fixes https://gitlab.com/kicad/code/kicad/-/issues/16632 --- eeschema/sch_reference_list.cpp | 6 +++--- eeschema/sch_reference_list.h | 4 ++-- eeschema/sch_sheet_path.cpp | 15 +++++++++++++-- eeschema/sch_sheet_path.h | 2 ++ eeschema/tools/sch_editor_control.cpp | 6 +++--- qa/tests/eeschema/test_sch_reference_list.cpp | 2 +- 6 files changed, 24 insertions(+), 11 deletions(-) diff --git a/eeschema/sch_reference_list.cpp b/eeschema/sch_reference_list.cpp index d7c00b82fd..fd2a582c96 100644 --- a/eeschema/sch_reference_list.cpp +++ b/eeschema/sch_reference_list.cpp @@ -327,7 +327,7 @@ void SCH_REFERENCE_LIST::ReannotateByOptions( ANNOTATE_ORDER_T aSort wxS( "Attempting to annotate item on sheet not part of the " "hierarchy?" ) ); - ref.SetSheetNumber( path->GetVirtualPageNumber() ); + ref.SetSheetNumber( path->GetPageNumberAsInt() ); } // Never lock unassigned references @@ -344,9 +344,9 @@ void SCH_REFERENCE_LIST::ReannotateByOptions( ANNOTATE_ORDER_T aSort } -void SCH_REFERENCE_LIST::ReannotateDuplicates( const SCH_REFERENCE_LIST& aAdditionalReferences ) +void SCH_REFERENCE_LIST::ReannotateDuplicates( const SCH_REFERENCE_LIST& aAdditionalReferences, ANNOTATE_ALGO_T aAlgoOption ) { - ReannotateByOptions( UNSORTED, INCREMENTAL_BY_REF, 0, aAdditionalReferences, true, nullptr ); + ReannotateByOptions( UNSORTED, aAlgoOption, 0, aAdditionalReferences, true, nullptr ); } diff --git a/eeschema/sch_reference_list.h b/eeschema/sch_reference_list.h index 6189318d47..34fec21639 100644 --- a/eeschema/sch_reference_list.h +++ b/eeschema/sch_reference_list.h @@ -390,11 +390,11 @@ public: * @note Do not use as a general reannotation method. * * Replaces any duplicate reference designators with the next available number after the - * present number regardless of configured annotation options. + * present number obeying the current annotation style. * * Multi-unit symbols are reannotated together. */ - void ReannotateDuplicates( const SCH_REFERENCE_LIST& aAdditionalReferences ); + void ReannotateDuplicates( const SCH_REFERENCE_LIST& aAdditionalReferences, ANNOTATE_ALGO_T aAlgoOption ); /** * Annotate the references by the provided options. diff --git a/eeschema/sch_sheet_path.cpp b/eeschema/sch_sheet_path.cpp index 2a4dc2076d..4d0ee3c33e 100644 --- a/eeschema/sch_sheet_path.cpp +++ b/eeschema/sch_sheet_path.cpp @@ -459,7 +459,7 @@ void SCH_SHEET_PATH::AppendSymbol( SCH_REFERENCE_LIST& aReferences, SCH_SYMBOL* { SCH_REFERENCE schReference( aSymbol, *this ); - schReference.SetSheetNumber( m_virtualPageNumber ); + schReference.SetSheetNumber( GetPageNumberAsInt() ); aReferences.AddItem( schReference ); } } @@ -491,7 +491,7 @@ void SCH_SHEET_PATH::AppendMultiUnitSymbol( SCH_MULTI_UNIT_REFERENCE_MAP& aRefLi if( symbol && symbol->GetUnitCount() > 1 ) { SCH_REFERENCE schReference = SCH_REFERENCE( aSymbol, *this ); - schReference.SetSheetNumber( m_virtualPageNumber ); + schReference.SetSheetNumber( GetPageNumberAsInt() ); wxString reference_str = schReference.GetRef(); // Never lock unassigned references @@ -601,6 +601,17 @@ wxString SCH_SHEET_PATH::GetPageNumber() const return sheet->getPageNumber( tmpPath ); } +int SCH_SHEET_PATH::GetPageNumberAsInt() const +{ + long page; + wxString pageStr = GetPageNumber(); + + if( pageStr.ToLong( &page ) ) + return (int) page; + + return GetVirtualPageNumber(); +} + void SCH_SHEET_PATH::SetPageNumber( const wxString& aPageNumber ) { diff --git a/eeschema/sch_sheet_path.h b/eeschema/sch_sheet_path.h index 9325b4bbf7..f6bfbe5b3c 100644 --- a/eeschema/sch_sheet_path.h +++ b/eeschema/sch_sheet_path.h @@ -206,6 +206,8 @@ public: wxString GetPageNumber() const; + int GetPageNumberAsInt() const; + const SCH_SHEET* GetSheet( unsigned aIndex ) const { SCH_SHEET* retv = nullptr; diff --git a/eeschema/tools/sch_editor_control.cpp b/eeschema/tools/sch_editor_control.cpp index ee4e3cafde..74e31203c7 100644 --- a/eeschema/tools/sch_editor_control.cpp +++ b/eeschema/tools/sch_editor_control.cpp @@ -1895,7 +1895,7 @@ int SCH_EDITOR_CONTROL::Paste( const TOOL_EVENT& aEvent ) if( libSymbol ) { SCH_REFERENCE schReference( symbol, sheetPath ); - schReference.SetSheetNumber( sheetPath.GetVirtualPageNumber() ); + schReference.SetSheetNumber( sheetPath.GetPageNumberAsInt() ); pastedSymbols[sheetPath].AddItem( schReference ); } } @@ -2103,7 +2103,7 @@ int SCH_EDITOR_CONTROL::Paste( const TOOL_EVENT& aEvent ) if( pasteMode == PASTE_MODE::UNIQUE_ANNOTATIONS ) { - annotatedSymbols[path].ReannotateDuplicates( existingRefs ); + annotatedSymbols[path].ReannotateDuplicates( existingRefs, annotateAlgo ); } else { @@ -2124,7 +2124,7 @@ int SCH_EDITOR_CONTROL::Paste( const TOOL_EVENT& aEvent ) if( pasteMode == PASTE_MODE::UNIQUE_ANNOTATIONS ) { - annotatedSymbols[pastedSheetPath].ReannotateDuplicates( existingRefs ); + annotatedSymbols[pastedSheetPath].ReannotateDuplicates( existingRefs, annotateAlgo ); } else { diff --git a/qa/tests/eeschema/test_sch_reference_list.cpp b/qa/tests/eeschema/test_sch_reference_list.cpp index 6135896e87..6aac1dea3b 100644 --- a/qa/tests/eeschema/test_sch_reference_list.cpp +++ b/qa/tests/eeschema/test_sch_reference_list.cpp @@ -289,7 +289,7 @@ BOOST_AUTO_TEST_CASE( ReannotateDuplicates ) loadTestCase( c.m_SchematicRelativePath, c.m_ExpectedReannotations ); m_refsToReannotate.SetRefDesTracker( m_schematic->Settings().m_refDesTracker ); - m_refsToReannotate.ReannotateDuplicates( getAdditionalRefs() ); + m_refsToReannotate.ReannotateDuplicates( getAdditionalRefs(), INCREMENTAL_BY_REF ); m_refsToReannotate.UpdateAnnotation(); checkAnnotation( c.m_ExpectedReannotations );