KiCad PCB EDA Suite
board_reannotate_tool.cpp
Go to the documentation of this file.
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 (C) 1992-2021 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 
26 #include <refdes_utils.h>
27 #include <tool/tool_manager.h>
28 #include <wx/filedlg.h>
30 
31 
33  PCB_TOOL_BASE( "pcbnew.ReannotateTool" ),
34  m_selectionTool( NULL ),
35  m_frame( nullptr )
36 {
37 }
38 
39 
41 {
42  // Find the selection tool, so they can cooperate
44 
45  return true;
46 }
47 
48 
50 {
51  m_frame = getEditFrame<PCB_EDIT_FRAME>();
52 }
53 
54 
56 {
58  dialog.ShowModal();
59  return 0;
60 }
61 
62 
64 {
66 
67  if( selection.Empty() )
68  return 0;
69 
70  // 1. Build list of designators on the board
71  FOOTPRINTS fpOnBoard = m_frame->GetBoard()->Footprints();
72  std::multimap<wxString, KIID> usedDesignatorsMap;
73 
74  for( FOOTPRINT* fp : fpOnBoard )
75  usedDesignatorsMap.insert( { fp->GetReference(), fp->m_Uuid } );
76 
77  // 2. Get a sorted list of footprints from the selection
78  FOOTPRINTS fpInSelection;
79 
80  for( EDA_ITEM* item : selection )
81  {
82  if( item->Type() == PCB_FOOTPRINT_T )
83  fpInSelection.push_back( static_cast<FOOTPRINT*>( item ) );
84  }
85 
86  std::sort( fpInSelection.begin(), fpInSelection.end(),
87  []( const FOOTPRINT* aA, const FOOTPRINT* aB ) -> bool
88  {
89  int ii = UTIL::RefDesStringCompare( aA->GetReference(), aB->GetReference() );
90 
91  if( ii == 0 )
92  ii = aA->m_Uuid < aB->m_Uuid; // ensure a deterministic sort
93 
94  return ii < 0;
95  } );
96 
97  // 3. Iterate through the sorted list of footprints
98  for( FOOTPRINT* fp : fpInSelection )
99  {
100  wxString stem = UTIL::GetRefDesPrefix( fp->GetReference() );
101  int value = UTIL::GetRefDesNumber( fp->GetReference() );
102  bool duplicate = false;
103 
104  while( usedDesignatorsMap.find( fp->GetReference() ) != usedDesignatorsMap.end() )
105  {
106  auto result = usedDesignatorsMap.equal_range( fp->GetReference() );
107 
108  for( auto& it = result.first; it != result.second; it++ )
109  {
110  if( it->second != fp->m_Uuid )
111  {
112  duplicate = true;
113  break;
114  }
115  }
116 
117  if( !duplicate )
118  break; // The only designator in the board with this reference is the selected one
119 
120  if( value < 0 )
121  value = 1;
122  else
123  ++value;
124 
125  fp->SetReference( stem + std::to_string( value ) );
126  }
127 
128  if( duplicate )
129  usedDesignatorsMap.insert( { fp->GetReference(), fp->m_Uuid } );
130  }
131 
132  return 0;
133 }
134 
135 
137 {
139 }
void setTransitions() override
This method is meant to be overridden in order to specify handlers for events.
bool Init() override
Init() is called once upon a registration of the tool.
TOOL_MANAGER * m_toolMgr
Definition: tool_base.h:214
Collection of utility functions for component reference designators (refdes)
void Go(int(T::*aStateFunc)(const TOOL_EVENT &), const TOOL_EVENT_LIST &aConditions=TOOL_EVENT(TC_ANY, TA_ANY))
Define which state (aStateFunc) to go when a certain event arrives (aConditions).
PCB_SELECTION_TOOL * m_selectionTool
int RefDesStringCompare(const wxString &aFirst, const wxString &aSecond)
Acts just like the strcmp function but treats numbers within the string text correctly for sorting.
PCB_SELECTION & GetSelection()
Return the set of currently selected items.
const PCB_SELECTION & selection() const
#define NULL
static TOOL_ACTION boardReannotate
Definition: pcb_actions.h:442
FOOTPRINTS & Footprints()
Definition: board.h:233
Generic, UI-independent tool event.
Definition: tool_event.h:152
const wxString & GetReference() const
Definition: footprint.h:421
void Reset(RESET_REASON aReason) override
Bring the tool to a known, initial state.
bool Empty() const
Checks if there is anything selected.
Definition: selection.h:97
class FOOTPRINT, a footprint
Definition: typeinfo.h:88
const KIID m_Uuid
Definition: eda_item.h:475
The selection tool: currently supports:
A base class for most all the KiCad significant classes used in schematics and boards.
Definition: eda_item.h:100
RESET_REASON
Determine the reason of reset for a tool.
Definition: tool_base.h:77
wxString GetRefDesPrefix(const wxString &aRefDes)
Get the (non-numeric) prefix from a refdes - e.g.
int ShowReannotateDialog(const TOOL_EVENT &aEvent)
BOARD * GetBoard() const
int GetRefDesNumber(const wxString &aRefDes)
Get the numeric suffix from a refdes - e.g.