diff --git a/eeschema/lib_symbol.cpp b/eeschema/lib_symbol.cpp index 33c0a23d86..755436a9db 100644 --- a/eeschema/lib_symbol.cpp +++ b/eeschema/lib_symbol.cpp @@ -1153,6 +1153,27 @@ bool LIB_SYMBOL::HasConversion() const return false; } +int LIB_SYMBOL::GetMaxPinNumber() const +{ + int maxPinNumber = 0; + LIB_SYMBOL_SPTR parent = m_parent.lock(); + const LIB_ITEMS_CONTAINER& drawItems = parent ? parent->m_drawings : m_drawings; + + for( const LIB_ITEM& item : drawItems[LIB_PIN_T] ) + { + const LIB_PIN* pin = static_cast( &item ); + long currentPinNumber = 0; + bool isNum = pin->GetNumber().ToLong( ¤tPinNumber ); + + if( isNum && currentPinNumber > maxPinNumber ) + { + maxPinNumber = currentPinNumber; + } + } + + return maxPinNumber; +} + void LIB_SYMBOL::ClearTempFlags() { diff --git a/eeschema/lib_symbol.h b/eeschema/lib_symbol.h index 294eb7c3d7..5d01ce3782 100644 --- a/eeschema/lib_symbol.h +++ b/eeschema/lib_symbol.h @@ -453,6 +453,12 @@ public: */ bool HasConversion() const; + /** + * @return the highest pin number of the symbol's pins. + * If none of the pins has pin number assigned it returns 0. + */ + int GetMaxPinNumber() const; + /** * Clears the status flag all draw objects in this symbol. */ diff --git a/eeschema/tools/symbol_editor_edit_tool.cpp b/eeschema/tools/symbol_editor_edit_tool.cpp index d0c3382afc..27ac2664cd 100644 --- a/eeschema/tools/symbol_editor_edit_tool.cpp +++ b/eeschema/tools/symbol_editor_edit_tool.cpp @@ -605,6 +605,17 @@ void SYMBOL_EDITOR_EDIT_TOOL::editSymbolProperties() } } +void SYMBOL_EDITOR_EDIT_TOOL::handlePinDuplication( LIB_PIN* aOldPin, LIB_PIN* aNewPin, + int& aSymbolLastPinNumber ) +{ + if( !aNewPin->GetNumber().IsEmpty() ) + { + // when duplicating a pin in symbol editor, assigning identical pin number + // to the old one does not makes any sense, so assign the next unassigned number to it + aSymbolLastPinNumber++; + aNewPin->SetNumber( wxString::Format( wxT( "%i" ), aSymbolLastPinNumber ) ); + } +} int SYMBOL_EDITOR_EDIT_TOOL::PinTable( const TOOL_EVENT& aEvent ) { @@ -803,11 +814,24 @@ int SYMBOL_EDITOR_EDIT_TOOL::Duplicate( const TOOL_EVENT& aEvent ) saveCopyInUndoList( m_frame->GetCurSymbol(), UNDO_REDO::LIBEDIT ); EDA_ITEMS newItems; + int symbolLastPinNumber = -1; for( unsigned ii = 0; ii < selection.GetSize(); ++ii ) { LIB_ITEM* oldItem = static_cast( selection.GetItem( ii ) ); LIB_ITEM* newItem = (LIB_ITEM*) oldItem->Clone(); + + if( oldItem->Type() == LIB_PIN_T ) + { + if( symbolLastPinNumber == -1 ) + { + symbolLastPinNumber = symbol->GetMaxPinNumber(); + } + + handlePinDuplication( static_cast( oldItem ), + static_cast( newItem ), symbolLastPinNumber ); + } + oldItem->ClearFlags( IS_NEW | IS_PASTED | SELECTED ); newItem->SetFlags( IS_NEW | IS_PASTED | SELECTED ); newItem->SetParent( symbol ); diff --git a/eeschema/tools/symbol_editor_edit_tool.h b/eeschema/tools/symbol_editor_edit_tool.h index b617969e91..31e5906121 100644 --- a/eeschema/tools/symbol_editor_edit_tool.h +++ b/eeschema/tools/symbol_editor_edit_tool.h @@ -69,6 +69,7 @@ private: void editTextBoxProperties( LIB_ITEM* aItem ); void editFieldProperties( LIB_FIELD* aField ); void editSymbolProperties(); + void handlePinDuplication(LIB_PIN* aOldPin, LIB_PIN* aNewPin, int &aSymbolLastPinNumber ); ///< Set up handlers for various events. void setTransitions() override;