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 <string_utils.h>
28 #include <tool/tool_manager.h>
29 #include <wx/filedlg.h>
31 
32 
34  PCB_TOOL_BASE( "pcbnew.ReannotateTool" ),
35  m_selectionTool( nullptr ),
36  m_frame( nullptr )
37 {
38 }
39 
40 
42 {
43  // Find the selection tool, so they can cooperate
45 
46  return true;
47 }
48 
49 
51 {
52  m_frame = getEditFrame<PCB_EDIT_FRAME>();
53 }
54 
55 
57 {
59  dialog.ShowModal();
60  return 0;
61 }
62 
63 
65 {
67 
68  if( selection.Empty() )
69  return 0;
70 
71  // 1. Build list of designators on the board
72  FOOTPRINTS fpOnBoard = m_frame->GetBoard()->Footprints();
73  std::multimap<wxString, KIID> usedDesignatorsMap;
74 
75  for( FOOTPRINT* fp : fpOnBoard )
76  usedDesignatorsMap.insert( { fp->GetReference(), fp->m_Uuid } );
77 
78  // 2. Get a sorted list of footprints from the selection
79  FOOTPRINTS fpInSelection;
80 
81  for( EDA_ITEM* item : selection )
82  {
83  if( item->Type() == PCB_FOOTPRINT_T )
84  fpInSelection.push_back( static_cast<FOOTPRINT*>( item ) );
85  }
86 
87  std::sort( fpInSelection.begin(), fpInSelection.end(),
88  []( const FOOTPRINT* aA, const FOOTPRINT* aB ) -> bool
89  {
90  int ii = StrNumCmp( aA->GetReference(), aB->GetReference(), true );
91 
92  if( ii == 0 )
93  ii = aA->m_Uuid < aB->m_Uuid; // ensure a deterministic sort
94 
95  return ii < 0;
96  } );
97 
98  // 3. Iterate through the sorted list of footprints
99  for( FOOTPRINT* fp : fpInSelection )
100  {
101  wxString stem = UTIL::GetRefDesPrefix( fp->GetReference() );
102  int value = UTIL::GetRefDesNumber( fp->GetReference() );
103  bool duplicate = false;
104 
105  while( usedDesignatorsMap.find( fp->GetReference() ) != usedDesignatorsMap.end() )
106  {
107  auto result = usedDesignatorsMap.equal_range( fp->GetReference() );
108 
109  for( auto& it = result.first; it != result.second; it++ )
110  {
111  if( it->second != fp->m_Uuid )
112  {
113  duplicate = true;
114  break;
115  }
116  }
117 
118  if( !duplicate )
119  break; // The only designator in the board with this reference is the selected one
120 
121  if( value < 0 )
122  value = 1;
123  else
124  ++value;
125 
126  fp->SetReference( stem + std::to_string( value ) );
127  }
128 
129  if( duplicate )
130  usedDesignatorsMap.insert( { fp->GetReference(), fp->m_Uuid } );
131  }
132 
133  return 0;
134 }
135 
136 
138 {
140 }
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
PCB_SELECTION & GetSelection()
Return the set of currently selected items.
const PCB_SELECTION & selection() const
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:430
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)
int StrNumCmp(const wxString &aString1, const wxString &aString2, bool aIgnoreCase)
Compare two strings with alphanumerical content.
BOARD * GetBoard() const
int GetRefDesNumber(const wxString &aRefDes)
Get the numeric suffix from a refdes - e.g.