You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

193 lines
6.2 KiB

  1. /*
  2. * This program source code file is part of KiCad, a free EDA CAD application.
  3. *
  4. * Copyright (C) 2020 Brian Piccioni brian@documenteddesigns.com
  5. * Copyright The KiCad Developers, see AUTHORS.txt for contributors.
  6. * @author Brian Piccioni <brian@documenteddesigns.com>
  7. *
  8. * This program is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU General Public License
  10. * as published by the Free Software Foundation; either version 2
  11. * of the License, or (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, you may find one here:
  20. * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
  21. * or you may search the http://www.gnu.org website for the version 2 license,
  22. * or you may write to the Free Software Foundation, Inc.,
  23. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
  24. */
  25. #include <pcb_group.h>
  26. #include <refdes_utils.h>
  27. #include <string_utils.h>
  28. #include <tool/tool_manager.h>
  29. #include <wx/filedlg.h>
  30. #include <tools/board_reannotate_tool.h>
  31. BOARD_REANNOTATE_TOOL::BOARD_REANNOTATE_TOOL() :
  32. PCB_TOOL_BASE( "pcbnew.ReannotateTool" ),
  33. m_selectionTool( nullptr ),
  34. m_frame( nullptr )
  35. {
  36. }
  37. bool BOARD_REANNOTATE_TOOL::Init()
  38. {
  39. // Find the selection tool, so they can cooperate
  40. m_selectionTool = m_toolMgr->GetTool<PCB_SELECTION_TOOL>();
  41. return true;
  42. }
  43. void BOARD_REANNOTATE_TOOL::Reset( RESET_REASON aReason )
  44. {
  45. m_frame = getEditFrame<PCB_EDIT_FRAME>();
  46. }
  47. int BOARD_REANNOTATE_TOOL::ShowReannotateDialog( const TOOL_EVENT& aEvent )
  48. {
  49. DIALOG_BOARD_REANNOTATE dialog( m_frame );
  50. dialog.ShowModal();
  51. return 0;
  52. }
  53. int BOARD_REANNOTATE_TOOL::ReannotateDuplicatesInSelection()
  54. {
  55. PCB_SELECTION& selection = m_selectionTool->GetSelection();
  56. if( selection.Empty() )
  57. return 0;
  58. return ReannotateDuplicates( selection, std::vector<EDA_ITEM*>() );
  59. }
  60. int BOARD_REANNOTATE_TOOL::ReannotateDuplicates( const PCB_SELECTION& aSelectionToReannotate,
  61. const std::vector<EDA_ITEM*>& aAdditionalFootprints )
  62. {
  63. if( aSelectionToReannotate.Empty() )
  64. return 0;
  65. // 1. Build list of designators on the board & the additional footprints
  66. FOOTPRINTS fpOnBoard = m_frame->GetBoard()->Footprints();
  67. std::multimap<wxString, KIID> usedDesignatorsMap;
  68. for( EDA_ITEM* item : aAdditionalFootprints )
  69. {
  70. if( item->Type() == PCB_FOOTPRINT_T )
  71. fpOnBoard.push_back( static_cast<FOOTPRINT*>( item ) );
  72. if( item->Type() == PCB_GROUP_T )
  73. {
  74. PCB_GROUP* group = static_cast<PCB_GROUP*>( item );
  75. group->RunOnDescendants(
  76. [&]( BOARD_ITEM* aGroupItem )
  77. {
  78. if( aGroupItem->Type() == PCB_FOOTPRINT_T )
  79. fpOnBoard.push_back( static_cast<FOOTPRINT*>( aGroupItem ) );
  80. } );
  81. }
  82. }
  83. for( FOOTPRINT* fp : fpOnBoard )
  84. usedDesignatorsMap.insert( { fp->GetReference(), fp->m_Uuid } );
  85. // 2. Get a sorted list of footprints from the selection
  86. FOOTPRINTS fpInSelection;
  87. for( EDA_ITEM* item : aSelectionToReannotate )
  88. {
  89. if( item->Type() == PCB_FOOTPRINT_T )
  90. fpInSelection.push_back( static_cast<FOOTPRINT*>( item ) );
  91. if( item->Type() == PCB_GROUP_T )
  92. {
  93. PCB_GROUP* group = static_cast<PCB_GROUP*>( item );
  94. group->RunOnDescendants(
  95. [&]( BOARD_ITEM* aGroupItem )
  96. {
  97. if( aGroupItem->Type() == PCB_FOOTPRINT_T )
  98. fpInSelection.push_back( static_cast<FOOTPRINT*>( aGroupItem ) );
  99. } );
  100. }
  101. }
  102. std::sort( fpInSelection.begin(), fpInSelection.end(),
  103. []( const FOOTPRINT* aA, const FOOTPRINT* aB ) -> bool
  104. {
  105. int ii = StrNumCmp( aA->GetReference(), aB->GetReference(), true );
  106. if( ii == 0 )
  107. {
  108. // Sort by position: x, then y
  109. if( aA->GetPosition().y == aB->GetPosition().y )
  110. {
  111. if( aA->GetPosition().x == aB->GetPosition().x )
  112. return aA->m_Uuid < aB->m_Uuid; // ensure a deterministic sort
  113. else
  114. return aA->GetPosition().x < aB->GetPosition().x;
  115. }
  116. else
  117. {
  118. return aA->GetPosition().y > aB->GetPosition().y;
  119. }
  120. }
  121. return ii < 0;
  122. } );
  123. // 3. Iterate through the sorted list of footprints
  124. for( FOOTPRINT* fp : fpInSelection )
  125. {
  126. wxString stem = UTIL::GetRefDesPrefix( fp->GetReference() );
  127. int value = UTIL::GetRefDesNumber( fp->GetReference() );
  128. bool duplicate = false;
  129. while( usedDesignatorsMap.find( fp->GetReference() ) != usedDesignatorsMap.end() )
  130. {
  131. auto result = usedDesignatorsMap.equal_range( fp->GetReference() );
  132. for( auto& it = result.first; it != result.second; it++ )
  133. {
  134. if( it->second != fp->m_Uuid )
  135. {
  136. duplicate = true;
  137. break;
  138. }
  139. }
  140. if( !duplicate )
  141. break; // The only designator in the board with this reference is the selected one
  142. if( value < 0 )
  143. value = 1;
  144. else
  145. ++value;
  146. fp->SetReference( stem + std::to_string( value ) );
  147. }
  148. if( duplicate )
  149. usedDesignatorsMap.insert( { fp->GetReference(), fp->m_Uuid } );
  150. }
  151. return 0;
  152. }
  153. void BOARD_REANNOTATE_TOOL::setTransitions()
  154. {
  155. Go( &BOARD_REANNOTATE_TOOL::ShowReannotateDialog, PCB_ACTIONS::boardReannotate.MakeEvent() );
  156. }