KiCad PCB EDA Suite
global_edit_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) 2019-2020 KiCad Developers, see AUTHORS.TXT for contributors.
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2
9  * of the License, or (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, you may find one here:
18  * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
19  * or you may search the http://www.gnu.org website for the version 2 license,
20  * or you may write to the Free Software Foundation, Inc.,
21  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
22  */
23 
24 #include <class_track.h>
25 #include <tool/tool_manager.h>
26 #include <tools/pcb_actions.h>
27 #include <tools/edit_tool.h>
32 #include <tools/global_edit_tool.h>
33 #include <board_commit.h>
35 
37  PCB_TOOL_BASE( "pcbnew.GlobalEdit" ),
38  m_selectionTool( nullptr )
39 {
40 }
41 
42 
44 {
45  if( aReason != RUN )
46  m_commit = std::make_unique<BOARD_COMMIT>( this );
47 }
48 
49 
51 {
52  // Find the selection tool, so they can cooperate
53  m_selectionTool = static_cast<SELECTION_TOOL*>( m_toolMgr->FindTool( "pcbnew.InteractiveSelection" ) );
54  wxASSERT_MSG( m_selectionTool, "pcbnew.InteractiveSelection tool is not available" );
55 
56  return true;
57 }
58 
59 
61 {
63  MODULE* mod = nullptr;
64  bool updateMode = false;
65  bool currentMode = false;
66 
67  if( aEvent.HasPosition() )
69 
70  if( !selection.Empty() )
71  mod = selection.FirstOfKind<MODULE>();
72 
73  if( aEvent.IsAction( &PCB_ACTIONS::updateFootprint ) )
74  {
75  updateMode = true;
76  currentMode = true;
77  }
78  else if( aEvent.IsAction( &PCB_ACTIONS::updateFootprints ) )
79  {
80  updateMode = true;
81  currentMode = false;
82  }
83  else if( aEvent.IsAction( &PCB_ACTIONS::changeFootprint ) )
84  {
85  updateMode = false;
86  currentMode = true;
87  }
88  else if( aEvent.IsAction( &PCB_ACTIONS::changeFootprints ) )
89  {
90  updateMode = false;
91  currentMode = false;
92  }
93  else
94  wxFAIL_MSG( "ExchangeFootprints: unexpected action" );
95 
96  // Footprint exchange could remove footprints, so they have to be
97  // removed from the selection first
99 
100  // invoke the exchange dialog process
101  {
102  PCB_EDIT_FRAME* editFrame = getEditFrame<PCB_EDIT_FRAME>();
103  DIALOG_EXCHANGE_FOOTPRINTS dialog( editFrame, mod, updateMode, currentMode );
104  dialog.ShowQuasiModal();
105  }
106 
107  return 0;
108 }
109 
110 
112 {
113  if( new_layer[ aItem->GetLayer() ] != aItem->GetLayer() )
114  {
115  m_commit->Modify( aItem );
116  aItem->SetLayer( new_layer[ aItem->GetLayer() ] );
117  frame()->GetCanvas()->GetView()->Update( aItem, KIGFX::GEOMETRY );
118  return true;
119  }
120 
121  return false;
122 }
123 
124 
126 {
127  PCB_LAYER_ID new_layer[PCB_LAYER_ID_COUNT];
128 
129  DIALOG_SWAP_LAYERS dlg( frame(), new_layer );
130 
131  if( dlg.ShowModal() != wxID_OK )
132  return 0;
133 
134  bool hasChanges = false;
135 
136  // Change tracks.
137  for( auto segm : frame()->GetBoard()->Tracks() )
138  {
139  if( segm->Type() == PCB_VIA_T )
140  {
141  VIA* via = (VIA*) segm;
142  PCB_LAYER_ID top_layer, bottom_layer;
143 
144  if( via->GetViaType() == VIATYPE::THROUGH )
145  continue;
146 
147  via->LayerPair( &top_layer, &bottom_layer );
148 
149  if( new_layer[bottom_layer] != bottom_layer || new_layer[top_layer] != top_layer )
150  {
151  m_commit->Modify( via );
152  via->SetLayerPair( new_layer[top_layer], new_layer[bottom_layer] );
153  frame()->GetCanvas()->GetView()->Update( via, KIGFX::GEOMETRY );
154  hasChanges = true;
155  }
156  }
157  else
158  {
159  hasChanges |= swapBoardItem( segm, new_layer );
160  }
161  }
162 
163  for( BOARD_ITEM* zone : frame()->GetBoard()->Zones() )
164  hasChanges |= swapBoardItem( zone, new_layer );
165 
166  for( BOARD_ITEM* drawing : frame()->GetBoard()->Drawings() )
167  hasChanges |= swapBoardItem( drawing, new_layer );
168 
169  if( hasChanges )
170  {
171  frame()->OnModify();
172  m_commit->Push( "Layers moved" );
173  frame()->GetCanvas()->Refresh();
174  }
175 
176  return 0;
177 }
178 
179 
181 {
182  PCB_EDIT_FRAME* editFrame = getEditFrame<PCB_EDIT_FRAME>();
183  DIALOG_CLEANUP_TRACKS_AND_VIAS dlg( editFrame );
184 
185  dlg.ShowModal();
186  return 0;
187 }
188 
189 
191 {
192  PCB_EDIT_FRAME* editFrame = getEditFrame<PCB_EDIT_FRAME>();
193  DIALOG_CLEANUP_GRAPHICS dlg( editFrame, false );
194 
195  dlg.ShowModal();
196  return 0;
197 }
198 
199 
201 {
202  PCB_EDIT_FRAME* editFrame = getEditFrame<PCB_EDIT_FRAME>();
204  []( const VECTOR2I& aPt, GENERAL_COLLECTOR& aCollector, SELECTION_TOOL* sTool )
205  {
206  EditToolSelectionFilter( aCollector, EXCLUDE_TRANSIENTS, sTool );
207  } );
208  DIALOG_UNUSED_PAD_LAYERS dlg( editFrame, selection, *m_commit );
209 
210  dlg.ShowModal();
211 
212  return 0;
213 }
214 
215 
217 {
222 
224 
231 }
232 
233 
static TOOL_ACTION selectionClear
Clears the current selection.
Definition: pcb_actions.h:62
void LayerPair(PCB_LAYER_ID *top_layer, PCB_LAYER_ID *bottom_layer) const
Function LayerPair Return the 2 layers used by the via (the via actually uses all layers between thes...
static TOOL_ACTION globalDeletions
Definition: pcb_actions.h:315
void setTransitions() override
Sets up handlers for various events.
bool swapBoardItem(BOARD_ITEM *aItem, PCB_LAYER_ID *new_layer)
void EditToolSelectionFilter(GENERAL_COLLECTOR &aCollector, int aFlags, SELECTION_TOOL *selectionTool)
Definition: edit_tool.cpp:64
virtual void Update(VIEW_ITEM *aItem, int aUpdateFlags) override
For dynamic VIEWs, informs the associated VIEW that the graphical representation of this item has cha...
Definition: pcb_view.cpp:93
virtual void SetLayer(PCB_LAYER_ID aLayer)
Function SetLayer sets the layer this item is on.
BOARD_ITEM is a base class for any item which can be embedded within the BOARD container class,...
static TOOL_ACTION cleanupGraphics
Definition: pcb_actions.h:317
SELECTION_TOOL.
PCB_DRAW_PANEL_GAL * GetCanvas() const override
Return a pointer to GAL-based canvas of given EDA draw frame.
Tool is invoked after being inactive.
Definition: tool_base.h:81
TOOL_MANAGER * m_toolMgr
Definition: tool_base.h:219
bool RunAction(const std::string &aActionName, bool aNow=false, T aParam=NULL)
Function RunAction() Runs the specified action.
Definition: tool_manager.h:141
static TOOL_ACTION changeFootprint
Definition: pcb_actions.h:320
static TOOL_ACTION updateFootprint
Definition: pcb_actions.h:318
static TOOL_ACTION swapLayers
Definition: pcb_actions.h:322
TOOL_BASE * FindTool(int aId) const
Function FindTool() Searches for a tool with given ID.
static TOOL_ACTION changeFootprints
Definition: pcb_actions.h:321
static TOOL_ACTION editTracksAndVias
Definition: pcb_actions.h:313
void Go(int(T::*aStateFunc)(const TOOL_EVENT &), const TOOL_EVENT_LIST &aConditions=TOOL_EVENT(TC_ANY, TA_ANY))
Function Go()
A single base class (TRACK) represents both tracks and vias, with subclasses for curved tracks (ARC) ...
bool IsAction(const TOOL_ACTION *aAction) const
Function IsAction() Tests if the event contains an action issued upon activation of the given TOOL_AC...
Definition: tool_event.cpp:67
PCBNEW_SELECTION & GetSelection()
Function GetSelection()
PCB_BASE_EDIT_FRAME * frame() const
static TOOL_ACTION cleanupTracksAndVias
Definition: pcb_actions.h:316
int SwapLayers(const TOOL_EVENT &aEvent)
int EditTracksAndVias(const TOOL_EVENT &aEvent)
PCB_LAYER_ID
A quick note on layer IDs:
const PCBNEW_SELECTION & selection() const
int RemoveUnusedPads(const TOOL_EVENT &aEvent)
static void FootprintFilter(const VECTOR2I &, GENERAL_COLLECTOR &aCollector, SELECTION_TOOL *sTool)
Function FootprintFilter() A selection filter which prunes the selection to contain only items of typ...
Definition: edit_tool.cpp:1693
int ShowQuasiModal()
TOOL_EVENT.
Definition: tool_event.h:171
void SetLayerPair(PCB_LAYER_ID aTopLayer, PCB_LAYER_ID aBottomLayer)
Function SetLayerPair For a via m_Layer contains the top layer, the other layer is in m_BottomLayer.
virtual KIGFX::PCB_VIEW * GetView() const override
Function GetView() Returns a pointer to the VIEW instance used in the panel.
virtual void Refresh(bool aEraseBackground=true, const wxRect *aRect=NULL) override
Update the board display after modifying it by a python script (note: it is automatically called by a...
#define EXCLUDE_TRANSIENTS
Definition: edit_tool.h:55
bool Empty() const
Checks if there is anything selected.
Definition: selection.h:121
BOARD * GetBoard()
static TOOL_ACTION updateFootprints
Definition: pcb_actions.h:319
static TOOL_ACTION removeUnusedPads
Definition: pcb_actions.h:323
bool Init() override
Function Init() Init() is called once upon a registration of the tool.
virtual void OnModify()
Function OnModify Must be called after a change in order to set the "modify" flag of the current scre...
void Reset(RESET_REASON aReason) override
Function Reset() Brings the tool to a known, initial state.
PCBNEW_SELECTION & RequestSelection(CLIENT_SELECTION_FILTER aClientFilter, std::vector< BOARD_ITEM * > *aFiltered=nullptr, bool aConfirmLockedItems=false)
Function RequestSelection()
static TOOL_ACTION editTextAndGraphics
Definition: pcb_actions.h:314
int GlobalDeletions(const TOOL_EVENT &aEvent)
int CleanupGraphics(const TOOL_EVENT &aEvent)
Used when the right click button is pressed, or when the select tool is in effect.
Definition: collectors.h:240
SELECTION_TOOL * m_selectionTool
PCB_EDIT_FRAME is the main frame for Pcbnew.
VIATYPE GetViaType() const
Definition: class_track.h:384
int ExchangeFootprints(const TOOL_EVENT &aEvent)
Function ExchangeFootprints()
RESET_REASON
Determines the reason of reset for a tool
Definition: tool_base.h:79
T * FirstOfKind() const
Definition: selection.h:195
int CleanupTracksAndVias(const TOOL_EVENT &aEvent)
class VIA, a via (like a track segment on a copper layer)
Definition: typeinfo.h:97
bool HasPosition() const
Returns if it this event has a valid position (true for mouse events and context-menu or hotkey-based...
Definition: tool_event.h:260
std::unique_ptr< BOARD_COMMIT > m_commit
BOARD * GetBoard() const
virtual PCB_LAYER_ID GetLayer() const
Function GetLayer returns the primary layer this item is on.
TRACKS & Tracks()
Definition: class_board.h:281
Color has changed.
Definition: view_item.h:59