KiCad PCB EDA Suite
pad_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) 2017-2019 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 
25 #include "pad_tool.h"
26 #include <class_draw_panel_gal.h>
27 #include <view/view_controls.h>
28 #include <view/view.h>
29 #include <tool/tool_manager.h>
30 #include <bitmaps.h>
31 #include <class_board_item.h>
32 #include <class_module.h>
33 #include <board_commit.h>
35 #include <tools/pcb_actions.h>
36 #include <tools/selection_tool.h>
38 #include <tools/edit_tool.h>
40 #include "pcbnew_id.h"
41 
42 
44  PCB_TOOL_BASE( "pcbnew.PadTool" ),
45  m_padCopied( false )
46 {}
47 
48 
50 {}
51 
52 
54 {
55  m_padCopied = false;
56 }
57 
58 
60 {
62 
63  if( selTool )
64  {
65  // Add context menu entries that are displayed when selection tool is active
66  CONDITIONAL_MENU& menu = selTool->GetToolMenu().GetMenu();
67 
71 
72  menu.AddSeparator( 400 );
73 
74  if( m_editModules )
75  {
77  menu.AddItem( PCB_ACTIONS::explodePadToShapes, singlePadSel, 400 );
78  }
79 
80  menu.AddItem( PCB_ACTIONS::copyPadSettings, singlePadSel, 400 );
81  menu.AddItem( PCB_ACTIONS::applyPadSettings, padSel, 400 );
82  menu.AddItem( PCB_ACTIONS::pushPadSettings, singlePadSel, 400 );
84  }
85 
86  return true;
87 }
88 
89 
91 {
92  auto& selTool = *m_toolMgr->GetTool<SELECTION_TOOL>();
93  const auto& selection = selTool.GetSelection();
94  const D_PAD& masterPad = frame()->GetDesignSettings().m_Pad_Master;
95 
96  BOARD_COMMIT commit( frame() );
97 
98  // for every selected pad, paste global settings
99  for( auto item : selection )
100  {
101  if( item->Type() == PCB_PAD_T )
102  {
103  commit.Modify( item );
104  static_cast<D_PAD&>( *item ).ImportSettingsFrom( masterPad );
105  }
106  }
107 
108  commit.Push( _( "Paste Pad Properties" ) );
109 
111  frame()->Refresh();
112 
113  return 0;
114 }
115 
116 
118 {
119  auto& selTool = *m_toolMgr->GetTool<SELECTION_TOOL>();
120  const auto& selection = selTool.GetSelection();
121 
122  D_PAD& masterPad = frame()->GetDesignSettings().m_Pad_Master;
123 
124  // can only copy from a single pad
125  if( selection.Size() == 1 )
126  {
127  auto item = selection[0];
128 
129  if( item->Type() == PCB_PAD_T )
130  {
131  const auto& selPad = static_cast<const D_PAD&>( *item );
132  masterPad.ImportSettingsFrom( selPad );
133  m_padCopied = true;
134  }
135  }
136 
137  return 0;
138 }
139 
140 
141 static void doPushPadProperties( BOARD& board, const D_PAD& aSrcPad, BOARD_COMMIT& commit,
142  bool aSameFootprints,
143  bool aPadShapeFilter,
144  bool aPadOrientFilter,
145  bool aPadLayerFilter,
146  bool aPadTypeFilter )
147 {
148  const MODULE* moduleRef = aSrcPad.GetParent();
149 
150  double pad_orient = aSrcPad.GetOrientation() - moduleRef->GetOrientation();
151 
152  for( auto module : board.Modules() )
153  {
154  if( !aSameFootprints && ( module != moduleRef ) )
155  continue;
156 
157  if( module->GetFPID() != moduleRef->GetFPID() )
158  continue;
159 
160  for( auto pad : module->Pads() )
161  {
162  if( aPadShapeFilter && ( pad->GetShape() != aSrcPad.GetShape() ) )
163  continue;
164 
165  double currpad_orient = pad->GetOrientation() - module->GetOrientation();
166 
167  if( aPadOrientFilter && ( currpad_orient != pad_orient ) )
168  continue;
169 
170  if( aPadLayerFilter && ( pad->GetLayerSet() != aSrcPad.GetLayerSet() ) )
171  continue;
172 
173  if( aPadTypeFilter && ( pad->GetAttribute() != aSrcPad.GetAttribute() ) )
174  continue;
175 
176  // Special-case for aperture pads
177  if( aPadTypeFilter && pad->GetAttribute() == PAD_ATTRIB_CONN )
178  {
179  if( pad->IsAperturePad() != aSrcPad.IsAperturePad() )
180  continue;
181  }
182 
183  commit.Modify( pad );
184 
185  // Apply source pad settings to this pad
186  pad->ImportSettingsFrom( aSrcPad );
187  }
188  }
189 }
190 
191 
193 {
194  auto& selTool = *m_toolMgr->GetTool<SELECTION_TOOL>();
195  const auto& selection = selTool.GetSelection();
196  D_PAD* srcPad;
197 
198  if( selection.Size() == 1 && selection[0]->Type() == PCB_PAD_T )
199  srcPad = static_cast<D_PAD*>( selection[0] );
200  else
201  return 0;
202 
203  MODULE* module = srcPad->GetParent();
204 
205  if( !module )
206  return 0;
207 
208  frame()->SetMsgPanel( module );
209 
211  int dialogRet = dlg.ShowModal();
212 
213  if( dialogRet == wxID_CANCEL )
214  return 0;
215 
216  const bool edit_Same_Modules = (dialogRet == 1);
217 
218  BOARD_COMMIT commit( frame() );
219 
220  doPushPadProperties( *getModel<BOARD>(), *srcPad, commit, edit_Same_Modules,
225 
226  commit.Push( _( "Push Pad Settings" ) );
227 
229  frame()->Refresh();
230 
231  return 0;
232 }
233 
234 
236 {
237  if( !board()->GetFirstModule() || board()->GetFirstModule()->Pads().empty() )
238  return 0;
239 
240  DIALOG_ENUM_PADS settingsDlg( frame() );
241 
242  if( settingsDlg.ShowModal() != wxID_OK )
243  return 0;
244 
245  std::string tool = aEvent.GetCommandStr().get();
246  frame()->PushTool( tool );
247  Activate();
248 
249  GENERAL_COLLECTOR collector;
250  const KICAD_T types[] = { PCB_PAD_T, EOT };
251 
253  guide.SetIgnoreMTextsMarkedNoShow( true );
254  guide.SetIgnoreMTextsOnBack( true );
255  guide.SetIgnoreMTextsOnFront( true );
256  guide.SetIgnoreModulesVals( true );
257  guide.SetIgnoreModulesRefs( true );
258 
259  int seqPadNum = settingsDlg.GetStartNumber();
260  wxString padPrefix = settingsDlg.GetPrefix();
261  std::deque<int> storedPadNumbers;
262 
264  getViewControls()->ShowCursor( true );
265 
267  VECTOR2I oldCursorPos; // store the previous mouse cursor position, during mouse drag
268  std::list<D_PAD*> selectedPads;
269  BOARD_COMMIT commit( frame() );
270  std::map<wxString, std::pair<int, wxString>> oldNames;
271  bool isFirstPoint = true; // used to be sure oldCursorPos will be initialized at least once.
272 
273  STATUS_TEXT_POPUP statusPopup( frame() );
274  wxString msg = _( "Click on pad %s%d\nPress <esc> to cancel or double-click to commit" );
275  statusPopup.SetText( wxString::Format( msg, padPrefix, seqPadNum ) );
276  statusPopup.Popup();
277  statusPopup.Move( wxGetMousePosition() + wxPoint( 20, 20 ) );
278 
279  while( TOOL_EVENT* evt = Wait() )
280  {
281  frame()->GetCanvas()->SetCurrentCursor( wxCURSOR_BULLSEYE );
282 
283  if( evt->IsCancelInteractive() )
284  {
286  commit.Revert();
287 
288  frame()->PopTool( tool );
289  break;
290  }
291 
292  else if( evt->IsActivate() )
293  {
294  commit.Push( _( "Renumber pads" ) );
295 
296  frame()->PopTool( tool );
297  break;
298  }
299 
300  else if( evt->IsDrag( BUT_LEFT ) || evt->IsClick( BUT_LEFT ) )
301  {
302  selectedPads.clear();
303  VECTOR2I cursorPos = getViewControls()->GetCursorPosition();
304 
305  // Be sure the old cursor mouse position was initialized:
306  if( isFirstPoint )
307  {
308  oldCursorPos = cursorPos;
309  isFirstPoint = false;
310  }
311 
312  // wxWidgets deliver mouse move events not frequently enough, resulting in skipping
313  // pads if the user moves cursor too fast. To solve it, create a line that approximates
314  // the mouse move and search pads that are on the line.
315  int distance = ( cursorPos - oldCursorPos ).EuclideanNorm();
316  // Search will be made every 0.1 mm:
317  int segments = distance / int( 0.1*IU_PER_MM ) + 1;
318  const wxPoint line_step( ( cursorPos - oldCursorPos ) / segments );
319 
320  collector.Empty();
321 
322  for( int j = 0; j < segments; ++j )
323  {
324  wxPoint testpoint( cursorPos.x - j * line_step.x, cursorPos.y - j * line_step.y );
325  collector.Collect( board(), types, testpoint, guide );
326 
327  for( int i = 0; i < collector.GetCount(); ++i )
328  selectedPads.push_back( static_cast<D_PAD*>( collector[i] ) );
329  }
330 
331  selectedPads.unique();
332 
333  for( D_PAD* pad : selectedPads )
334  {
335  // If pad was not selected, then enumerate it
336  if( !pad->IsSelected() )
337  {
338  commit.Modify( pad );
339 
340  // Rename pad and store the old name
341  int newval;
342 
343  if( storedPadNumbers.size() > 0 )
344  {
345  newval = storedPadNumbers.front();
346  storedPadNumbers.pop_front();
347  }
348  else
349  newval = seqPadNum++;
350 
351  wxString newName = wxString::Format( wxT( "%s%d" ), padPrefix, newval );
352  oldNames[newName] = { newval, pad->GetName() };
353  pad->SetName( newName );
354  pad->SetSelected();
355  getView()->Update( pad );
356 
357  // Ensure the popup text shows the correct next value
358  if( storedPadNumbers.size() > 0 )
359  newval = storedPadNumbers.front();
360  else
361  newval = seqPadNum;
362 
363  statusPopup.SetText( wxString::Format( msg, padPrefix, newval ) );
364  }
365 
366  // ... or restore the old name if it was enumerated and clicked again
367  else if( pad->IsSelected() && evt->IsClick( BUT_LEFT ) )
368  {
369  auto it = oldNames.find( pad->GetName() );
370  wxASSERT( it != oldNames.end() );
371 
372  if( it != oldNames.end() )
373  {
374  storedPadNumbers.push_back( it->second.first );
375  pad->SetName( it->second.second );
376  oldNames.erase( it );
377 
378  int newval = storedPadNumbers.front();
379 
380  statusPopup.SetText( wxString::Format( msg, padPrefix, newval ) );
381  }
382 
383  pad->ClearSelected();
384  getView()->Update( pad );
385  }
386  }
387  }
388 
389  else if( ( evt->IsKeyPressed() && evt->KeyCode() == WXK_RETURN ) ||
390  evt->IsDblClick( BUT_LEFT ) )
391  {
392  commit.Push( _( "Renumber pads" ) );
393  frame()->PopTool( tool );
394  break;
395  }
396 
397  else if( evt->IsClick( BUT_RIGHT ) )
398  {
400  }
401 
402  else
403  evt->SetPassEvent();
404 
405  // Prepare the next loop by updating the old cursor mouse position
406  // to this last mouse cursor position
407  oldCursorPos = getViewControls()->GetCursorPosition();
408  statusPopup.Move( wxGetMousePosition() + wxPoint( 20, 20 ) );
409  }
410 
411  for( auto p : board()->GetFirstModule()->Pads() )
412  {
413  p->ClearSelected();
414  view->Update( p );
415  }
416 
417  statusPopup.Hide();
418  return 0;
419 }
420 
421 
423 {
427 
429 }
double EuclideanNorm(const wxPoint &vector)
Euclidean norm of a 2D vector.
Definition: trigo.h:123
void Empty()
Function Empty sets the list to empty.
Definition: collector.h:122
static TOOL_ACTION selectionClear
Clears the current selection.
Definition: pcb_actions.h:73
virtual void ShowCursor(bool aEnabled)
Function ShowCursor() Enables or disables display of cursor.
static bool ShowAlways(const SELECTION &aSelection)
Function ShowAlways The default condition function (always returns true).
virtual void PushTool(const std::string &actionName)
NB: the definition of "tool" is different at the user level.
KIGFX::VIEW * GetView() const
Definition: tool_manager.h:250
double GetOrientation() const
Definition: class_module.h:218
COMMIT & Modify(EDA_ITEM *aItem)
Modifies a given item in the model.
Definition: commit.h:103
TOOL_MENU m_menu
functions below are not yet implemented - their interface may change
virtual BOARD_DESIGN_SETTINGS & GetDesignSettings() const
Function GetDesignSettings returns the BOARD_DESIGN_SETTINGS for the BOARD owned by this frame.
TOOL_EVENT * Wait(const TOOL_EVENT_LIST &aEventList=TOOL_EVENT(TC_ANY, TA_ANY))
Function Wait()
BOARD * board() const
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:91
void SetCurrentCursor(wxStockCursor aStockCursorID)
Function SetCurrentCursor Set the current cursor shape for this panel.
VIEW_CONTROLS class definition.
void Collect(BOARD_ITEM *aItem, const KICAD_T aScanList[], const wxPoint &aRefPos, const COLLECTORS_GUIDE &aGuide)
Scan a BOARD_ITEM using this class's Inspector method, which does the collection.
Definition: collectors.cpp:479
Classes BOARD_ITEM and BOARD_CONNECTED_ITEM.
void SetIgnoreModulesVals(bool ignore)
Definition: collectors.h:589
Class SELECTION_TOOL.
Class STATUS_TEXT_POPUP.
Definition: status_popup.h:79
virtual void Revert() override
Revertes the commit by restoring the modifed items state.
PCB_DRAW_PANEL_GAL * GetCanvas() const override
Return a pointer to GAL-based canvas of given EDA draw frame.
CONDITIONAL_MENU & GetMenu()
Function GetMenu.
Definition: tool_menu.cpp:46
PAD_TOOL()
Definition: pad_tool.cpp:43
static TOOL_ACTION explodePadToShapes
Definition: pcb_actions.h:360
TOOL_MANAGER * m_toolMgr
Definition: tool_base.h:219
static TOOL_ACTION enumeratePads
Tool for quick pad enumeration.
Definition: pcb_actions.h:363
TOOL_MENU & GetToolMenu()
void SetIgnoreModulesRefs(bool ignore)
Definition: collectors.h:595
bool RunAction(const std::string &aActionName, bool aNow=false, T aParam=NULL)
Function RunAction() Runs the specified action.
Definition: tool_manager.h:109
static SELECTION_CONDITION HasType(KICAD_T aType)
Function HasType Creates a functor that tests if among the selected items there is at least one of a ...
bool m_padCopied
Flag to indicate there are valid settings stored in the Master Pad object
Definition: pad_tool.h:72
class D_PAD, a pad in a footprint
Definition: typeinfo.h:90
const LIB_ID & GetFPID() const
Definition: class_module.h:222
static SELECTION_CONDITION Count(int aNumber)
Function Count Creates a functor that tests if the number of selected items is equal to the value giv...
void Reset(RESET_REASON aReason) override
React to model/view changes
Definition: pad_tool.cpp:53
static bool NotEmpty(const SELECTION &aSelection)
Function NotEmpty Tests if there are any items selected.
void Go(int(T::*aStateFunc)(const TOOL_EVENT &), const TOOL_EVENT_LIST &aConditions=TOOL_EVENT(TC_ANY, TA_ANY))
Function Go()
Implementing DIALOG_ENUM_PADS_BASE.
search types array terminator (End Of Types)
Definition: typeinfo.h:82
KICAD_T
Enum KICAD_T is the set of class identification values, stored in EDA_ITEM::m_StructType.
Definition: typeinfo.h:78
static const TOOL_EVENT SelectedItemsModified
Definition: actions.h:201
PAD_ATTR_T GetAttribute() const
Definition: class_pad.h:445
PCB_BASE_EDIT_FRAME * frame() const
static TOOL_ACTION pushPadSettings
Copy the current pad's settings to other pads in the module or on the board.
Definition: pcb_actions.h:380
int GetCount() const
Function GetCount returns the number of objects in the list.
Definition: collector.h:113
GENERAL_COLLECTORS_GUIDE GetCollectorsGuide()
Function GetCollectorsGuide.
const PCBNEW_SELECTION & selection() const
int pushPadSettings(const TOOL_EVENT &aEvent)
Push pad settings from a pad to other pads on board or module
Definition: pad_tool.cpp:192
void SetIgnoreMTextsOnBack(bool ignore)
Definition: collectors.h:547
static TOOL_ACTION copyPadSettings
Copy the selected pad's settings to the board design settings.
Definition: pcb_actions.h:374
MODULES & Modules()
Definition: class_board.h:227
void SetMsgPanel(const std::vector< MSG_PANEL_ITEM > &aList)
Clear the message panel and populates it with the contents of aList.
bool ProcessEvent(const TOOL_EVENT &aEvent)
Propagates an event to tools that requested events of matching type(s).
void SetIgnoreMTextsOnFront(bool ignore)
Definition: collectors.h:553
Class TOOL_EVENT.
Definition: tool_event.h:171
LSET GetLayerSet() const override
Function GetLayerSet returns a "layer mask", which is a bitmap of all layers on which the TRACK segme...
Definition: class_pad.h:442
wxString GetPrefix() const
Returns common prefix for all enumerated pads.
static TOOL_ACTION applyPadSettings
Copy the default pad settings to the selected pad.
Definition: pcb_actions.h:377
int pastePadProperties(const TOOL_EVENT &aEvent)
Apply pad settings from board design settings to a pad
Definition: pad_tool.cpp:90
KIGFX::PCB_VIEW * view() const
virtual void Popup(wxWindow *aFocus=nullptr)
std::function< bool(const SELECTION &)> SELECTION_CONDITION
Functor type that checks a specific condition for selected items.
virtual void Move(const wxPoint &aWhere)
void SetIgnoreMTextsMarkedNoShow(bool ignore)
Definition: collectors.h:541
virtual void Update(VIEW_ITEM *aItem, int aUpdateFlags)
For dynamic VIEWs, informs the associated VIEW that the graphical representation of this item has cha...
Definition: view.cpp:1540
int EnumeratePads(const TOOL_EVENT &aEvent)
Function EnumeratePads() Tool for quick pad enumeration.
Definition: pad_tool.cpp:235
Like smd, does not appear on the solder paste layer (default) note also has a special attribute in Ge...
Definition: pad_shapes.h:63
#define _(s)
Subclass of DIALOG_ENUM_PADS_BASE, which is generated by wxFormBuilder.
KIGFX::VIEW * getView() const
Function getView()
Definition: tool_base.cpp:36
void AddSeparator(int aOrder=ANY_ORDER)
Function AddSeparator()
MODULE * GetParent() const
Definition: class_pad.h:167
OPT< std::string > GetCommandStr() const
Definition: tool_event.h:463
static TOOL_ACTION createPadFromShapes
Definition: pcb_actions.h:359
static void doPushPadProperties(BOARD &board, const D_PAD &aSrcPad, BOARD_COMMIT &commit, bool aSameFootprints, bool aPadShapeFilter, bool aPadOrientFilter, bool aPadLayerFilter, bool aPadTypeFilter)
Definition: pad_tool.cpp:141
void Format(OUTPUTFORMATTER *out, int aNestLevel, int aCtl, CPTREE &aTree)
Function Format outputs a PTREE into s-expression format via an OUTPUTFORMATTER derivative.
Definition: ptree.cpp:205
Class BOARD holds information pertinent to a Pcbnew printed circuit board.
Definition: class_board.h:161
MODULE * module() const
double GetOrientation() const
Function GetOrientation returns the rotation angle of the pad in tenths of degrees,...
Definition: class_pad.h:422
Used when the right click button is pressed, or when the select tool is in effect.
Definition: collectors.h:240
void ImportSettingsFrom(const D_PAD &aMasterPad)
Imports the pad settings from aMasterPad.
Definition: class_pad.cpp:1463
~PAD_TOOL()
Definition: pad_tool.cpp:49
size_t i
Definition: json11.cpp:649
static SELECTION_CONDITION OnlyType(KICAD_T aType)
Function OnlyType Creates a functor that tests if the selected items are only of given type.
D_PAD m_Pad_Master
A dummy pad to store all default parameters.
static float distance(const SFVEC2UI &a, const SFVEC2UI &b)
int Size() const
Returns the number of selected parts.
Definition: selection.h:125
static bool empty(const wxTextEntryBase *aCtrl)
bool IsAperturePad() const
Definition: class_pad.h:449
virtual void Push(const wxString &aMessage=wxT("A commit"), bool aCreateUndoEntry=true, bool aSetDirtyBit=true) override
Executes the changes.
RESET_REASON
Determines the reason of reset for a tool
Definition: tool_base.h:79
virtual void PopTool(const std::string &actionName)
void Activate()
Function Activate() Runs the tool.
int GetStartNumber() const
Returns the starting number that is going to be used for the first enumerated pad.
PAD_SHAPE_T GetShape() const
Function GetShape.
Definition: class_pad.h:237
Module description (excepted pads)
KIGFX::VIEW_CONTROLS * getViewControls() const
Function getViewControls()
Definition: tool_base.cpp:42
Class VIEW.
Definition: view.h:61
A general implementation of a COLLECTORS_GUIDE.
Definition: collectors.h:385
bool Init() override
Basic initalization
Definition: pad_tool.cpp:59
void AddItem(const TOOL_ACTION &aAction, const SELECTION_CONDITION &aCondition, int aOrder=ANY_ORDER)
Function AddItem()
void setTransitions() override
Bind handlers to corresponding TOOL_ACTIONs
Definition: pad_tool.cpp:422
void ShowContextMenu(SELECTION &aSelection)
Function ShowContextMenu.
Definition: tool_menu.cpp:59
void SetText(const wxString &aText)
Display a text.
VECTOR2D GetCursorPosition() const
Returns the current cursor position in world coordinates.
int copyPadSettings(const TOOL_EVENT &aEvent)
Copy pad settings from a pad to the board design settings
Definition: pad_tool.cpp:117