KiCad PCB EDA Suite
Loading...
Searching...
No Matches
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 [email protected]
5 * Copyright (C) 1992-2021 KiCad Developers, see AUTHORS.txt for contributors.
6 * @author Brian Piccioni <[email protected]>
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 <pcb_group.h>
27#include <refdes_utils.h>
28#include <string_utils.h>
29#include <tool/tool_manager.h>
30#include <wx/filedlg.h>
32
33
35 PCB_TOOL_BASE( "pcbnew.ReannotateTool" ),
36 m_selectionTool( nullptr ),
37 m_frame( nullptr )
38{
39}
40
41
43{
44 // Find the selection tool, so they can cooperate
46
47 return true;
48}
49
50
52{
53 m_frame = getEditFrame<PCB_EDIT_FRAME>();
54}
55
56
58{
60 dialog.ShowModal();
61 return 0;
62}
63
64
66{
68
69 if( selection.Empty() )
70 return 0;
71
72 return ReannotateDuplicates( selection, std::vector<EDA_ITEM*>() );
73}
74
76 const std::vector<EDA_ITEM*>& aAdditionalFootprints )
77{
78 if( aSelectionToReannotate.Empty() )
79 return 0;
80
81 // 1. Build list of designators on the board & the additional footprints
82 FOOTPRINTS fpOnBoard = m_frame->GetBoard()->Footprints();
83 std::multimap<wxString, KIID> usedDesignatorsMap;
84
85 for( EDA_ITEM* item : aAdditionalFootprints )
86 {
87 if( item->Type() == PCB_FOOTPRINT_T )
88 fpOnBoard.push_back( static_cast<FOOTPRINT*>( item ) );
89
90 if( item->Type() == PCB_GROUP_T )
91 {
92 PCB_GROUP* group = static_cast<PCB_GROUP*>( item );
93
94 group->RunOnDescendants(
95 [&]( BOARD_ITEM* aGroupItem )
96 {
97 if( aGroupItem->Type() == PCB_FOOTPRINT_T )
98 fpOnBoard.push_back( static_cast<FOOTPRINT*>( aGroupItem ) );
99 } );
100 }
101 }
102
103 for( FOOTPRINT* fp : fpOnBoard )
104 usedDesignatorsMap.insert( { fp->GetReference(), fp->m_Uuid } );
105
106 // 2. Get a sorted list of footprints from the selection
107 FOOTPRINTS fpInSelection;
108
109 for( EDA_ITEM* item : aSelectionToReannotate )
110 {
111 if( item->Type() == PCB_FOOTPRINT_T )
112 fpInSelection.push_back( static_cast<FOOTPRINT*>( item ) );
113
114 if( item->Type() == PCB_GROUP_T )
115 {
116 PCB_GROUP* group = static_cast<PCB_GROUP*>( item );
117
118 group->RunOnDescendants(
119 [&]( BOARD_ITEM* aGroupItem )
120 {
121 if( aGroupItem->Type() == PCB_FOOTPRINT_T )
122 fpInSelection.push_back( static_cast<FOOTPRINT*>( aGroupItem ) );
123 } );
124 }
125 }
126
127 std::sort( fpInSelection.begin(), fpInSelection.end(),
128 []( const FOOTPRINT* aA, const FOOTPRINT* aB ) -> bool
129 {
130 int ii = StrNumCmp( aA->GetReference(), aB->GetReference(), true );
131
132 if( ii == 0 )
133 {
134 // Sort by position: x, then y
135 if( aA->GetPosition().y == aB->GetPosition().y )
136 {
137 if( aA->GetPosition().x == aB->GetPosition().x )
138 return aA->m_Uuid < aB->m_Uuid; // ensure a deterministic sort
139 else
140 return aA->GetPosition().x < aB->GetPosition().x;
141 }
142 else
143 {
144 return aA->GetPosition().y > aB->GetPosition().y;
145 }
146 }
147
148 return ii < 0;
149 } );
150
151 // 3. Iterate through the sorted list of footprints
152 for( FOOTPRINT* fp : fpInSelection )
153 {
154 wxString stem = UTIL::GetRefDesPrefix( fp->GetReference() );
155 int value = UTIL::GetRefDesNumber( fp->GetReference() );
156 bool duplicate = false;
157
158 while( usedDesignatorsMap.find( fp->GetReference() ) != usedDesignatorsMap.end() )
159 {
160 auto result = usedDesignatorsMap.equal_range( fp->GetReference() );
161
162 for( auto& it = result.first; it != result.second; it++ )
163 {
164 if( it->second != fp->m_Uuid )
165 {
166 duplicate = true;
167 break;
168 }
169 }
170
171 if( !duplicate )
172 break; // The only designator in the board with this reference is the selected one
173
174 if( value < 0 )
175 value = 1;
176 else
177 ++value;
178
179 fp->SetReference( stem + std::to_string( value ) );
180 }
181
182 if( duplicate )
183 usedDesignatorsMap.insert( { fp->GetReference(), fp->m_Uuid } );
184 }
185
186 return 0;
187}
188
189
191{
193}
A base class for any item which can be embedded within the BOARD container class, and therefore insta...
Definition: board_item.h:79
void setTransitions() override
This method is meant to be overridden in order to specify handlers for events.
void Reset(RESET_REASON aReason) override
Bring the tool to a known, initial state.
int ReannotateDuplicates(const PCB_SELECTION &aSelectionToReannotate, const std::vector< EDA_ITEM * > &aAdditionalFootprints)
PCB_SELECTION_TOOL * m_selectionTool
int ShowReannotateDialog(const TOOL_EVENT &aEvent)
bool Init() override
Init() is called once upon a registration of the tool.
const FOOTPRINTS & Footprints() const
Definition: board.h:331
int ShowModal() override
A base class for most all the KiCad significant classes used in schematics and boards.
Definition: eda_item.h:89
KICAD_T Type() const
Returns the type of object.
Definition: eda_item.h:101
static TOOL_ACTION boardReannotate
Definition: pcb_actions.h:548
BOARD * GetBoard() const
A set of BOARD_ITEMs (i.e., without duplicates).
Definition: pcb_group.h:52
The selection tool: currently supports:
PCB_SELECTION & GetSelection()
const PCB_SELECTION & selection() const
bool Empty() const
Checks if there is anything selected.
Definition: selection.h:110
TOOL_MANAGER * m_toolMgr
Definition: tool_base.h:218
RESET_REASON
Determine the reason of reset for a tool.
Definition: tool_base.h:78
Generic, UI-independent tool event.
Definition: tool_event.h:167
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).
wxString GetRefDesPrefix(const wxString &aRefDes)
Get the (non-numeric) prefix from a refdes - e.g.
int GetRefDesNumber(const wxString &aRefDes)
Get the numeric suffix from a refdes - e.g.
Class to handle a set of BOARD_ITEMs.
Collection of utility functions for component reference designators (refdes)
@ PCB_GROUP_T
class PCB_GROUP, a set of BOARD_ITEMs
Definition: typeinfo.h:110
@ PCB_FOOTPRINT_T
class FOOTPRINT, a footprint
Definition: typeinfo.h:86