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 The 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
27
28#include <wx/filedlg.h>
29
30#include <footprint.h>
31#include <pcb_group.h>
32#include <refdes_utils.h>
33#include <string_utils.h>
34#include <tool/tool_manager.h>
35
36
38 PCB_TOOL_BASE( "pcbnew.ReannotateTool" ),
39 m_selectionTool( nullptr ),
40 m_frame( nullptr )
41{
42}
43
44
46{
47 // Find the selection tool, so they can cooperate
49
50 return true;
51}
52
53
58
59
61{
63 dialog.ShowModal();
64 return 0;
65}
66
67
69{
70 PCB_SELECTION& selection = m_selectionTool->GetSelection();
71
72 if( selection.Empty() )
73 return 0;
74
75 return ReannotateDuplicates( selection, std::vector<EDA_ITEM*>() );
76}
77
79 const std::vector<EDA_ITEM*>& aAdditionalFootprints )
80{
81 if( aSelectionToReannotate.Empty() )
82 return 0;
83
84 // 1. Build list of designators on the board & the additional footprints
85 FOOTPRINTS fpOnBoard = m_frame->GetBoard()->Footprints();
86 std::multimap<wxString, KIID> usedDesignatorsMap;
87
88 for( EDA_ITEM* item : aAdditionalFootprints )
89 {
90 if( item->Type() == PCB_FOOTPRINT_T )
91 fpOnBoard.push_back( static_cast<FOOTPRINT*>( item ) );
92
93 if( item->Type() == PCB_GROUP_T )
94 {
95 PCB_GROUP* group = static_cast<PCB_GROUP*>( item );
96
97 group->RunOnChildren(
98 [&]( BOARD_ITEM* aGroupItem )
99 {
100 if( aGroupItem->Type() == PCB_FOOTPRINT_T )
101 fpOnBoard.push_back( static_cast<FOOTPRINT*>( aGroupItem ) );
102 },
104 }
105 }
106
107 for( FOOTPRINT* fp : fpOnBoard )
108 usedDesignatorsMap.insert( { fp->GetReference(), fp->m_Uuid } );
109
110 // 2. Get a sorted list of footprints from the selection
111 FOOTPRINTS fpInSelection;
112
113 for( EDA_ITEM* item : aSelectionToReannotate )
114 {
115 if( item->Type() == PCB_FOOTPRINT_T )
116 fpInSelection.push_back( static_cast<FOOTPRINT*>( item ) );
117
118 if( item->Type() == PCB_GROUP_T )
119 {
120 PCB_GROUP* group = static_cast<PCB_GROUP*>( item );
121
122 group->RunOnChildren(
123 [&]( BOARD_ITEM* aGroupItem )
124 {
125 if( aGroupItem->Type() == PCB_FOOTPRINT_T )
126 fpInSelection.push_back( static_cast<FOOTPRINT*>( aGroupItem ) );
127 },
129 }
130 }
131
132 std::sort( fpInSelection.begin(), fpInSelection.end(),
133 []( const FOOTPRINT* aA, const FOOTPRINT* aB ) -> bool
134 {
135 int ii = StrNumCmp( aA->GetReference(), aB->GetReference(), true );
136
137 if( ii == 0 )
138 {
139 // Sort by position: x, then y
140 if( aA->GetPosition().y == aB->GetPosition().y )
141 {
142 if( aA->GetPosition().x == aB->GetPosition().x )
143 return aA->m_Uuid < aB->m_Uuid; // ensure a deterministic sort
144 else
145 return aA->GetPosition().x < aB->GetPosition().x;
146 }
147 else
148 {
149 return aA->GetPosition().y > aB->GetPosition().y;
150 }
151 }
152
153 return ii < 0;
154 } );
155
156 // 3. Iterate through the sorted list of footprints
157 for( FOOTPRINT* fp : fpInSelection )
158 {
159 wxString stem = UTIL::GetRefDesPrefix( fp->GetReference() );
160 int value = UTIL::GetRefDesNumber( fp->GetReference() );
161 bool duplicate = false;
162
163 while( usedDesignatorsMap.find( fp->GetReference() ) != usedDesignatorsMap.end() )
164 {
165 auto result = usedDesignatorsMap.equal_range( fp->GetReference() );
166
167 for( auto& it = result.first; it != result.second; it++ )
168 {
169 if( it->second != fp->m_Uuid )
170 {
171 duplicate = true;
172 break;
173 }
174 }
175
176 if( !duplicate )
177 break; // The only designator in the board with this reference is the selected one
178
179 if( value < 0 )
180 value = 1;
181 else
182 ++value;
183
184 fp->SetReference( stem + std::to_string( value ) );
185 }
186
187 if( duplicate )
188 usedDesignatorsMap.insert( { fp->GetReference(), fp->m_Uuid } );
189 }
190
191 return 0;
192}
193
194
A base class for any item which can be embedded within the BOARD container class, and therefore insta...
Definition board_item.h:84
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.
int ShowModal() override
A base class for most all the KiCad significant classes used in schematics and boards.
Definition eda_item.h:99
KICAD_T Type() const
Returns the type of object.
Definition eda_item.h:111
static TOOL_ACTION boardReannotate
A set of BOARD_ITEMs (i.e., without duplicates).
Definition pcb_group.h:53
The selection tool: currently supports:
PCB_TOOL_BASE(TOOL_ID aId, const std::string &aName)
Constructor.
const PCB_SELECTION & selection() const
bool Empty() const
Checks if there is anything selected.
Definition selection.h:115
T * getEditFrame() const
Return the application window object, casted to requested user type.
Definition tool_base.h:186
TOOL_MANAGER * m_toolMgr
Definition tool_base.h:220
RESET_REASON
Determine the reason of reset for a tool.
Definition tool_base.h:78
Generic, UI-independent tool event.
Definition tool_event.h:171
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).
@ RECURSE
Definition eda_item.h:52
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)
wxString result
Test unit parsing edge cases and error handling.
@ PCB_GROUP_T
class PCB_GROUP, a set of BOARD_ITEMs
Definition typeinfo.h:111
@ PCB_FOOTPRINT_T
class FOOTPRINT, a footprint
Definition typeinfo.h:86