Only append number to name if it already exists.

Fixes https://gitlab.com/kicad/code/kicad/issues/4130
This commit is contained in:
Jeff Young 2020-03-30 17:23:21 +01:00
parent 58e03be23c
commit add4c3d4ea
2 changed files with 6 additions and 9 deletions

View File

@ -506,7 +506,7 @@ private:
bool isCurrentPart( const LIB_ID& aLibId ) const; bool isCurrentPart( const LIB_ID& aLibId ) const;
///> Renames LIB_PART aliases to avoid conflicts before adding a component to a library ///> Renames LIB_PART aliases to avoid conflicts before adding a component to a library
void fixDuplicateAliases( LIB_PART* aPart, const wxString& aLibrary ); void ensureUniqueName( LIB_PART* aPart, const wxString& aLibrary );
DECLARE_EVENT_TABLE() DECLARE_EVENT_TABLE()
}; };

View File

@ -667,7 +667,7 @@ void LIB_EDIT_FRAME::DuplicatePart( bool aFromClipboard )
if( !newPart ) if( !newPart )
return; return;
fixDuplicateAliases( newPart, lib ); ensureUniqueName( newPart, lib );
m_libMgr->UpdatePart( newPart, lib ); m_libMgr->UpdatePart( newPart, lib );
SyncLibraries( false ); SyncLibraries( false );
m_treePane->GetLibTree()->SelectLibId( LIB_ID( lib, newPart->GetName() ) ); m_treePane->GetLibTree()->SelectLibId( LIB_ID( lib, newPart->GetName() ) );
@ -676,19 +676,16 @@ void LIB_EDIT_FRAME::DuplicatePart( bool aFromClipboard )
} }
void LIB_EDIT_FRAME::fixDuplicateAliases( LIB_PART* aPart, const wxString& aLibrary ) void LIB_EDIT_FRAME::ensureUniqueName( LIB_PART* aPart, const wxString& aLibrary )
{ {
wxCHECK( aPart, /* void */ ); wxCHECK( aPart, /* void */ );
int i = 1; int i = 1;
wxString newName; wxString newName = aPart->GetName();
// Append a number to the name until the name is unique in the library. // Append a number to the name until the name is unique in the library.
do while( m_libMgr->PartExists( newName, aLibrary ) )
{ newName.Printf( "%s_%d", aPart->GetName(), i++ );
newName.Printf( "%s_%d", aPart->GetName(), i );
i++;
} while( m_libMgr->PartExists( newName, aLibrary ) );
aPart->SetName( newName ); aPart->SetName( newName );
} }