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 
70 
71  menu.AddSeparator( 400 );
73  menu.AddItem( PCB_ACTIONS::explodePadToShapes, singlePadSel, 400 );
74  menu.AddItem( PCB_ACTIONS::copyPadSettings, singlePadSel, 400 );
75  menu.AddItem( PCB_ACTIONS::applyPadSettings, padSel, 400 );
76  menu.AddItem( PCB_ACTIONS::pushPadSettings, singlePadSel, 400 );
78  }
79 
80  return true;
81 }
82 
83 
85 {
86  auto& selTool = *m_toolMgr->GetTool<SELECTION_TOOL>();
87  const auto& selection = selTool.GetSelection();
88  const D_PAD& masterPad = frame()->GetDesignSettings().m_Pad_Master;
89 
90  BOARD_COMMIT commit( frame() );
91 
92  // for every selected pad, paste global settings
93  for( auto item : selection )
94  {
95  if( item->Type() == PCB_PAD_T )
96  {
97  commit.Modify( item );
98  static_cast<D_PAD&>( *item ).ImportSettingsFrom( masterPad );
99  }
100  }
101 
102  commit.Push( _( "Paste Pad Properties" ) );
103 
105  frame()->Refresh();
106 
107  return 0;
108 }
109 
110 
112 {
113  auto& selTool = *m_toolMgr->GetTool<SELECTION_TOOL>();
114  const auto& selection = selTool.GetSelection();
115 
116  D_PAD& masterPad = frame()->GetDesignSettings().m_Pad_Master;
117 
118  // can only copy from a single pad
119  if( selection.Size() == 1 )
120  {
121  auto item = selection[0];
122 
123  if( item->Type() == PCB_PAD_T )
124  {
125  const auto& selPad = static_cast<const D_PAD&>( *item );
126  masterPad.ImportSettingsFrom( selPad );
127  m_padCopied = true;
128  }
129  }
130 
131  return 0;
132 }
133 
134 
135 static void doPushPadProperties( BOARD& board, const D_PAD& aSrcPad, BOARD_COMMIT& commit,
136  bool aSameFootprints,
137  bool aPadShapeFilter,
138  bool aPadOrientFilter,
139  bool aPadLayerFilter,
140  bool aPadTypeFilter )
141 {
142  const MODULE* moduleRef = aSrcPad.GetParent();
143 
144  double pad_orient = aSrcPad.GetOrientation() - moduleRef->GetOrientation();
145 
146  for( auto module : board.Modules() )
147  {
148  if( !aSameFootprints && ( module != moduleRef ) )
149  continue;
150 
151  if( module->GetFPID() != moduleRef->GetFPID() )
152  continue;
153 
154  for( auto pad : module->Pads() )
155  {
156  if( aPadShapeFilter && ( pad->GetShape() != aSrcPad.GetShape() ) )
157  continue;
158 
159  double currpad_orient = pad->GetOrientation() - module->GetOrientation();
160 
161  if( aPadOrientFilter && ( currpad_orient != pad_orient ) )
162  continue;
163 
164  if( aPadLayerFilter && ( pad->GetLayerSet() != aSrcPad.GetLayerSet() ) )
165  continue;
166 
167  if( aPadTypeFilter && ( pad->GetAttribute() != aSrcPad.GetAttribute() ) )
168  continue;
169 
170  // Special-case for aperture pads
171  if( aPadTypeFilter && pad->GetAttribute() == PAD_ATTRIB_CONN )
172  {
173  if( pad->IsAperturePad() != aSrcPad.IsAperturePad() )
174  continue;
175  }
176 
177  commit.Modify( pad );
178 
179  // Apply source pad settings to this pad
180  pad->ImportSettingsFrom( aSrcPad );
181  }
182  }
183 }
184 
185 
187 {
188  auto& selTool = *m_toolMgr->GetTool<SELECTION_TOOL>();
189  const auto& selection = selTool.GetSelection();
190  D_PAD* srcPad;
191 
192  if( selection.Size() == 1 && selection[0]->Type() == PCB_PAD_T )
193  srcPad = static_cast<D_PAD*>( selection[0] );
194  else
195  return 0;
196 
197  MODULE* module = srcPad->GetParent();
198 
199  if( !module )
200  return 0;
201 
202  frame()->SetMsgPanel( module );
203 
205  int dialogRet = dlg.ShowModal();
206 
207  if( dialogRet == wxID_CANCEL )
208  return 0;
209 
210  const bool edit_Same_Modules = (dialogRet == 1);
211 
212  BOARD_COMMIT commit( frame() );
213 
214  doPushPadProperties( *getModel<BOARD>(), *srcPad, commit, edit_Same_Modules,
219 
220  commit.Push( _( "Push Pad Settings" ) );
221 
223  frame()->Refresh();
224 
225  return 0;
226 }
227 
228 
230 {
231  if( !board()->GetFirstModule() || board()->GetFirstModule()->Pads().empty() )
232  return 0;
233 
234  DIALOG_ENUM_PADS settingsDlg( frame() );
235 
236  if( settingsDlg.ShowModal() != wxID_OK )
237  return 0;
238 
239  std::string tool = aEvent.GetCommandStr().get();
240  frame()->PushTool( tool );
241  Activate();
242 
243  GENERAL_COLLECTOR collector;
244  const KICAD_T types[] = { PCB_PAD_T, EOT };
245 
247  guide.SetIgnoreMTextsMarkedNoShow( true );
248  guide.SetIgnoreMTextsOnBack( true );
249  guide.SetIgnoreMTextsOnFront( true );
250  guide.SetIgnoreModulesVals( true );
251  guide.SetIgnoreModulesRefs( true );
252 
253  int seqPadNum = settingsDlg.GetStartNumber();
254  wxString padPrefix = settingsDlg.GetPrefix();
255  std::deque<int> storedPadNumbers;
256 
258  getViewControls()->ShowCursor( true );
259 
261  VECTOR2I oldCursorPos; // store the previous mouse cursor position, during mouse drag
262  std::list<D_PAD*> selectedPads;
263  BOARD_COMMIT commit( frame() );
264  std::map<wxString, std::pair<int, wxString>> oldNames;
265  bool isFirstPoint = true; // used to be sure oldCursorPos will be initialized at least once.
266 
267  STATUS_TEXT_POPUP statusPopup( frame() );
268  wxString msg = _( "Click on pad %s%d\nPress <esc> to cancel or double-click to commit" );
269  statusPopup.SetText( wxString::Format( msg, padPrefix, seqPadNum ) );
270  statusPopup.Popup();
271  statusPopup.Move( wxGetMousePosition() + wxPoint( 20, 20 ) );
272 
273  while( TOOL_EVENT* evt = Wait() )
274  {
275  frame()->GetCanvas()->SetCurrentCursor( wxCURSOR_BULLSEYE );
276 
277  if( evt->IsCancelInteractive() )
278  {
280  commit.Revert();
281 
282  frame()->PopTool( tool );
283  break;
284  }
285 
286  else if( evt->IsActivate() )
287  {
288  commit.Push( _( "Renumber pads" ) );
289 
290  frame()->PopTool( tool );
291  break;
292  }
293 
294  else if( evt->IsDrag( BUT_LEFT ) || evt->IsClick( BUT_LEFT ) )
295  {
296  selectedPads.clear();
297  VECTOR2I cursorPos = getViewControls()->GetCursorPosition();
298 
299  // Be sure the old cursor mouse position was initialized:
300  if( isFirstPoint )
301  {
302  oldCursorPos = cursorPos;
303  isFirstPoint = false;
304  }
305 
306  // wxWidgets deliver mouse move events not frequently enough, resulting in skipping
307  // pads if the user moves cursor too fast. To solve it, create a line that approximates
308  // the mouse move and search pads that are on the line.
309  int distance = ( cursorPos - oldCursorPos ).EuclideanNorm();
310  // Search will be made every 0.1 mm:
311  int segments = distance / int( 0.1*IU_PER_MM ) + 1;
312  const wxPoint line_step( ( cursorPos - oldCursorPos ) / segments );
313 
314  collector.Empty();
315 
316  for( int j = 0; j < segments; ++j )
317  {
318  wxPoint testpoint( cursorPos.x - j * line_step.x, cursorPos.y - j * line_step.y );
319  collector.Collect( board(), types, testpoint, guide );
320 
321  for( int i = 0; i < collector.GetCount(); ++i )
322  selectedPads.push_back( static_cast<D_PAD*>( collector[i] ) );
323  }
324 
325  selectedPads.unique();
326 
327  for( D_PAD* pad : selectedPads )
328  {
329  // If pad was not selected, then enumerate it
330  if( !pad->IsSelected() )
331  {
332  commit.Modify( pad );
333 
334  // Rename pad and store the old name
335  int newval;
336 
337  if( storedPadNumbers.size() > 0 )
338  {
339  newval = storedPadNumbers.front();
340  storedPadNumbers.pop_front();
341  }
342  else
343  newval = seqPadNum++;
344 
345  wxString newName = wxString::Format( wxT( "%s%d" ), padPrefix, newval );
346  oldNames[newName] = { newval, pad->GetName() };
347  pad->SetName( newName );
348  pad->SetSelected();
349  getView()->Update( pad );
350 
351  // Ensure the popup text shows the correct next value
352  if( storedPadNumbers.size() > 0 )
353  newval = storedPadNumbers.front();
354  else
355  newval = seqPadNum;
356 
357  statusPopup.SetText( wxString::Format( msg, padPrefix, newval ) );
358  }
359 
360  // ... or restore the old name if it was enumerated and clicked again
361  else if( pad->IsSelected() && evt->IsClick( BUT_LEFT ) )
362  {
363  auto it = oldNames.find( pad->GetName() );
364  wxASSERT( it != oldNames.end() );
365 
366  if( it != oldNames.end() )
367  {
368  storedPadNumbers.push_back( it->second.first );
369  pad->SetName( it->second.second );
370  oldNames.erase( it );
371 
372  int newval = storedPadNumbers.front();
373 
374  statusPopup.SetText( wxString::Format( msg, padPrefix, newval ) );
375  }
376 
377  pad->ClearSelected();
378  getView()->Update( pad );
379  }
380  }
381  }
382 
383  else if( ( evt->IsKeyPressed() && evt->KeyCode() == WXK_RETURN ) ||
384  evt->IsDblClick( BUT_LEFT ) )
385  {
386  commit.Push( _( "Renumber pads" ) );
387  frame()->PopTool( tool );
388  break;
389  }
390 
391  else if( evt->IsClick( BUT_RIGHT ) )
392  {
394  }
395 
396  else
397  evt->SetPassEvent();
398 
399  // Prepare the next loop by updating the old cursor mouse position
400  // to this last mouse cursor position
401  oldCursorPos = getViewControls()->GetCursorPosition();
402  statusPopup.Move( wxGetMousePosition() + wxPoint( 20, 20 ) );
403  }
404 
405  for( auto p : board()->GetFirstModule()->Pads() )
406  {
407  p->ClearSelected();
408  view->Update( p );
409  }
410 
411  statusPopup.Hide();
412  return 0;
413 }
414 
415 
417 {
421 
423 }
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:241
double GetOrientation() const
Definition: class_module.h:202
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:474
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:362
TOOL_MANAGER * m_toolMgr
Definition: tool_base.h:219
static TOOL_ACTION enumeratePads
Tool for quick pad enumeration.
Definition: pcb_actions.h:365
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:206
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:429
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:382
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:186
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:376
MODULES & Modules()
Definition: class_board.h:236
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:168
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:426
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:379
int pastePadProperties(const TOOL_EVENT &aEvent)
Apply pad settings from board design settings to a pad
Definition: pad_tool.cpp:84
KIGFX::PCB_VIEW * view() const
virtual void Popup(wxWindow *aFocus=nullptr)
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:229
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:455
static TOOL_ACTION createPadFromShapes
Definition: pcb_actions.h:361
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:135
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:406
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:1467
~PAD_TOOL()
Definition: pad_tool.cpp:49
size_t i
Definition: json11.cpp:597
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:433
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:221
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:416
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:111