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;
///> 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()
};

View File

@ -667,7 +667,7 @@ void LIB_EDIT_FRAME::DuplicatePart( bool aFromClipboard )
if( !newPart )
return;
fixDuplicateAliases( newPart, lib );
ensureUniqueName( newPart, lib );
m_libMgr->UpdatePart( newPart, lib );
SyncLibraries( false );
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 */ );
int i = 1;
wxString newName;
wxString newName = aPart->GetName();
// Append a number to the name until the name is unique in the library.
do
{
newName.Printf( "%s_%d", aPart->GetName(), i );
i++;
} while( m_libMgr->PartExists( newName, aLibrary ) );
while( m_libMgr->PartExists( newName, aLibrary ) )
newName.Printf( "%s_%d", aPart->GetName(), i++ );
aPart->SetName( newName );
}