KiCad PCB EDA Suite
placement_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) 2014-2016 CERN
5  * @author Maciej Suminski <maciej.suminski@cern.ch>
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * as published by the Free Software Foundation; either version 2
10  * of the License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, you may find one here:
19  * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
20  * or you may search the http://www.gnu.org website for the version 2 license,
21  * or you may write to the Free Software Foundation, Inc.,
22  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
23  */
24 
25 #include "placement_tool.h"
26 #include "pcb_actions.h"
27 #include "selection_tool.h"
28 #include <tool/tool_manager.h>
29 
30 #include <wxPcbStruct.h>
31 #include <class_board.h>
32 #include <ratsnest_data.h>
33 #include <board_commit.h>
34 #include <bitmaps.h>
35 
36 #include <confirm.h>
37 #include <menus_helpers.h>
38 
39 // Placement tool
40 TOOL_ACTION PCB_ACTIONS::alignTop( "pcbnew.AlignAndDistribute.alignTop",
41  AS_GLOBAL, 0,
42  _( "Align to Top" ),
43  _( "Aligns selected items to the top edge" ), up_xpm );
44 
45 TOOL_ACTION PCB_ACTIONS::alignBottom( "pcbnew.AlignAndDistribute.alignBottom",
46  AS_GLOBAL, 0,
47  _( "Align to Bottom" ),
48  _( "Aligns selected items to the bottom edge" ), down_xpm );
49 
50 TOOL_ACTION PCB_ACTIONS::alignLeft( "pcbnew.AlignAndDistribute.alignLeft",
51  AS_GLOBAL, 0,
52  _( "Align to Left" ),
53  _( "Aligns selected items to the left edge" ), left_xpm );
54 
55 TOOL_ACTION PCB_ACTIONS::alignRight( "pcbnew.AlignAndDistribute.alignRight",
56  AS_GLOBAL, 0,
57  _( "Align to Right" ),
58  _( "Aligns selected items to the right edge" ), right_xpm );
59 
60 TOOL_ACTION PCB_ACTIONS::distributeHorizontally( "pcbnew.AlignAndDistribute.distributeHorizontally",
61  AS_GLOBAL, 0,
62  _( "Distribute Horizontally" ),
63  _( "Distributes selected items along the horizontal axis" ), distribute_horizontal_xpm );
64 
65 TOOL_ACTION PCB_ACTIONS::distributeVertically( "pcbnew.AlignAndDistribute.distributeVertically",
66  AS_GLOBAL, 0,
67  _( "Distribute Vertically" ),
68  _( "Distributes selected items along the vertical axis" ), distribute_vertical_xpm );
69 
70 
72  TOOL_INTERACTIVE( "pcbnew.Placement" ), m_selectionTool( NULL ), m_placementMenu( NULL )
73 {
74 }
75 
77 {
78  delete m_placementMenu;
79 }
80 
81 
83 {
84  // Find the selection tool, so they can cooperate
85  m_selectionTool = static_cast<SELECTION_TOOL*>( m_toolMgr->FindTool( "pcbnew.InteractiveSelection" ) );
86 
87  if( !m_selectionTool )
88  {
89  DisplayError( NULL, wxT( "pcbnew.InteractiveSelection tool is not available" ) );
90  return false;
91  }
92 
93  // Create a context menu and make it available through selection tool
95  m_placementMenu->SetIcon( align_items_xpm );
96  m_placementMenu->SetTitle( _( "Align/distribute" ) );
97 
98  // Add all align/distribute commands
103  m_placementMenu->AppendSeparator();
106 
109 
110  return true;
111 }
112 
113 
115 {
116  const SELECTION& selection = m_selectionTool->GetSelection();
117 
118  if( selection.Size() <= 1 )
119  return 0;
120 
121  BOARD_COMMIT commit( getEditFrame<PCB_BASE_FRAME>() );
122  commit.StageItems( selection, CHT_MODIFY );
123 
124  // Compute the highest point of selection - it will be the edge of alignment
125  int top = selection.Front()->GetBoundingBox().GetY();
126 
127  for( int i = 1; i < selection.Size(); ++i )
128  {
129  int currentTop = selection[i]->GetBoundingBox().GetY();
130 
131  if( top > currentTop ) // Y decreases when going up
132  top = currentTop;
133  }
134 
135  // Move the selected items
136  for( auto i : selection )
137  {
138  auto item = static_cast<BOARD_ITEM*>( i );
139 
140  int difference = top - item->GetBoundingBox().GetY();
141 
142  item->Move( wxPoint( 0, difference ) );
143  }
144 
145  commit.Push( _( "Align to top" ) );
146 
147  return 0;
148 }
149 
150 
152 {
153  const SELECTION& selection = m_selectionTool->GetSelection();
154 
155  if( selection.Size() <= 1 )
156  return 0;
157 
158  BOARD_COMMIT commit( getEditFrame<PCB_BASE_FRAME>() );
159  commit.StageItems( selection, CHT_MODIFY );
160 
161  // Compute the lowest point of selection - it will be the edge of alignment
162  int bottom = selection.Front()->GetBoundingBox().GetBottom();
163 
164  for( int i = 1; i < selection.Size(); ++i )
165  {
166  int currentBottom = selection[i]->GetBoundingBox().GetBottom();
167 
168  if( bottom < currentBottom ) // Y increases when going down
169  bottom = currentBottom;
170  }
171 
172  // Move the selected items
173  for( auto i : selection )
174  {
175  auto item = static_cast<BOARD_ITEM*>( i );
176 
177  int difference = bottom - item->GetBoundingBox().GetBottom();
178 
179  item->Move( wxPoint( 0, difference ) );
180  }
181 
182  commit.Push( _( "Align to bottom" ) );
183 
184  return 0;
185 }
186 
187 
189 {
190  const SELECTION& selection = m_selectionTool->GetSelection();
191 
192  if( selection.Size() <= 1 )
193  return 0;
194 
195  BOARD_COMMIT commit( getEditFrame<PCB_BASE_FRAME>() );
196  commit.StageItems( selection, CHT_MODIFY );
197 
198  // Compute the leftmost point of selection - it will be the edge of alignment
199  int left = selection.Front()->GetBoundingBox().GetX();
200 
201  for( int i = 1; i < selection.Size(); ++i )
202  {
203  int currentLeft = selection[i]->GetBoundingBox().GetX();
204 
205  if( left > currentLeft ) // X decreases when going left
206  left = currentLeft;
207  }
208 
209  // Move the selected items
210  for( auto i : selection )
211  {
212  auto item = static_cast<BOARD_ITEM*>( i );
213 
214  int difference = left - item->GetBoundingBox().GetX();
215 
216  item->Move( wxPoint( difference, 0 ) );
217  }
218 
219  commit.Push( _( "Align to left" ) );
220 
221  return 0;
222 }
223 
224 
226 {
227  const SELECTION& selection = m_selectionTool->GetSelection();
228 
229  if( selection.Size() <= 1 )
230  return 0;
231 
232  BOARD_COMMIT commit( getEditFrame<PCB_BASE_FRAME>() );
233  commit.StageItems( selection, CHT_MODIFY );
234 
235  // Compute the rightmost point of selection - it will be the edge of alignment
236  int right = selection.Front()->GetBoundingBox().GetRight();
237 
238  for( int i = 1; i < selection.Size(); ++i )
239  {
240  int currentRight = selection[i]->GetBoundingBox().GetRight();
241 
242  if( right < currentRight ) // X increases when going right
243  right = currentRight;
244  }
245 
246  // Move the selected items
247  for( auto i : selection )
248  {
249  auto item = static_cast<BOARD_ITEM*>( i );
250 
251  int difference = right - item->GetBoundingBox().GetRight();
252 
253  item->Move( wxPoint( difference, 0 ) );
254  }
255 
256  commit.Push( _( "Align to right" ) );
257 
258  return 0;
259 }
260 
261 
262 static bool compareX( const BOARD_ITEM* aA, const BOARD_ITEM* aB )
263 {
264  return aA->GetBoundingBox().Centre().x < aB->GetBoundingBox().Centre().x;
265 }
266 
267 
268 static bool compareY( const BOARD_ITEM* aA, const BOARD_ITEM* aB )
269 {
270  return aA->GetBoundingBox().Centre().y < aB->GetBoundingBox().Centre().y;
271 }
272 
273 
275 {
276  const SELECTION& selection = m_selectionTool->GetSelection();
277 
278  if( selection.Size() <= 1 )
279  return 0;
280 
281  BOARD_COMMIT commit( getEditFrame<PCB_BASE_FRAME>() );
282  commit.StageItems( selection, CHT_MODIFY );
283 
284  // Prepare a list, so the items can be sorted by their X coordinate
285  std::vector<BOARD_ITEM*> itemsList;
286 
287  for( auto item : selection )
288  itemsList.push_back( static_cast<BOARD_ITEM*>( item ) );
289 
290  // Sort items by X coordinate
291  std::sort(itemsList.begin(), itemsList.end(), compareX );
292 
293  // Expected X coordinate for the next item (=minX)
294  int position = itemsList.front()->GetBoundingBox().Centre().x;
295 
296  // X coordinate for the last item
297  const int maxX = itemsList.back()->GetBoundingBox().Centre().x;
298 
299  // Distance between items
300  const int distance = ( maxX - position ) / ( itemsList.size() - 1 );
301 
302  for( auto item : itemsList )
303  {
304  int difference = position - item->GetBoundingBox().Centre().x;
305 
306  item->Move( wxPoint( difference, 0 ) );
307 
308  position += distance;
309  }
310 
311  commit.Push( _( "Distribute horizontally" ) );
312 
313  return 0;
314 }
315 
316 
318 {
319  const SELECTION& selection = m_selectionTool->GetSelection();
320 
321  if( selection.Size() <= 1 )
322  return 0;
323 
324  BOARD_COMMIT commit( getEditFrame<PCB_BASE_FRAME>() );
325  commit.StageItems( selection, CHT_MODIFY );
326 
327  // Prepare a list, so the items can be sorted by their Y coordinate
328  std::vector<BOARD_ITEM*> itemsList;
329 
330  for( auto item : selection )
331  itemsList.push_back( static_cast<BOARD_ITEM*>( item ) );
332 
333  // Sort items by Y coordinate
334  std::sort( itemsList.begin(), itemsList.end(), compareY );
335 
336  // Expected Y coordinate for the next item (=minY)
337  int position = (*itemsList.begin())->GetBoundingBox().Centre().y;
338 
339  // Y coordinate for the last item
340  const int maxY = (*itemsList.rbegin())->GetBoundingBox().Centre().y;
341 
342  // Distance between items
343  const int distance = ( maxY - position ) / ( itemsList.size() - 1 );
344 
345  for( auto item : itemsList )
346  {
347  int difference = position - item->GetBoundingBox().Centre().y;
348 
349  item->Move( wxPoint( 0, difference ) );
350 
351  position += distance;
352  }
353 
354  commit.Push( _( "Distribute vertically" ) );
355 
356  return 0;
357 }
358 
359 
361 {
366 
369 }
static TOOL_ACTION alignRight
Align items to the right edge of selection bounding box.
Definition: pcb_actions.h:226
int AlignLeft(const TOOL_EVENT &aEvent)
Function AlignLeft() Sets X coordinate of the selected items to the value of the left-most selected i...
virtual const EDA_RECT GetBoundingBox() const
Function GetBoundingBox returns the orthogonal, bounding box of this object for display purposes...
TOOL_BASE * FindTool(int aId) const
Function FindTool() Searches for a tool with given ID.
This file is part of the common library.
Class BOARD_ITEM is a base class for any item which can be embedded within the BOARD container class...
static SELECTION_CONDITION MoreThan(int aNumber)
Function MoreThan Creates a functor that tests if the number of selected items is greater than the va...
Class CONTEXT_MENU.
Definition: context_menu.h:44
virtual ~ALIGN_DISTRIBUTE_TOOL()
Class SELECTION_TOOL.
Class BOARD to handle a board.
Class that computes missing connections on a PCB.
CONDITIONAL_MENU & GetMenu()
Function GetMenu.
Definition: tool_menu.cpp:49
TOOL_MENU & GetToolMenu()
EDA_ITEM * Front() const
Definition: selection.h:144
TOOL_MANAGER * m_toolMgr
Definition: tool_base.h:213
int AlignBottom(const TOOL_EVENT &aEvent)
Function AlignBottom() Sets Y coordinate of the selected items to the value of the bottom-most select...
static TOOL_ACTION distributeVertically
Distributes items evenly along the vertical axis.
Definition: pcb_actions.h:232
CONTEXT_MENU * m_placementMenu
void Go(int(T::*aStateFunc)(const TOOL_EVENT &), const TOOL_EVENT_LIST &aConditions=TOOL_EVENT(TC_ANY, TA_ANY))
Function Go()
virtual void Push(const wxString &aMessage=wxT("A commit"), bool aCreateUndoEntry=true) override
Executes the changes.
static TOOL_ACTION alignBottom
Align items to the bottom edge of selection bounding box.
Definition: pcb_actions.h:220
wxMenuItem * Add(const wxString &aLabel, int aId, const BITMAP_OPAQUE *aIcon=NULL)
Function Add() Adds an entry to the menu.
static TOOL_ACTION distributeHorizontally
Distributes items evenly along the horizontal axis.
Definition: pcb_actions.h:229
static bool compareX(const BOARD_ITEM *aA, const BOARD_ITEM *aB)
int DistributeHorizontally(const TOOL_EVENT &aEvent)
Function DistributeHorizontally() Distributes the selected items along the X axis.
int AlignTop(const TOOL_EVENT &aEvent)
Function AlignTop() Sets Y coordinate of the selected items to the value of the top-most selected ite...
Class TOOL_EVENT.
Definition: tool_event.h:162
int DistributeVertically(const TOOL_EVENT &aEvent)
Function DistributeVertically() Distributes the selected items along the Y axis.
COMMIT & StageItems(const Range &aRange, CHANGE_TYPE aChangeType)
Definition: commit.h:116
static TOOL_ACTION alignLeft
Align items to the left edge of selection bounding box.
Definition: pcb_actions.h:223
SELECTION & GetSelection()
Function GetSelection()
EDA_RECT GetBoundingBox() const
int GetBottom() const
static TOOL_ACTION alignTop
Align items to the top edge of selection bounding box.
Definition: pcb_actions.h:217
wxPoint Centre() const
int GetRight() const
All active tools
Definition: tool_event.h:138
static bool compareY(const BOARD_ITEM *aA, const BOARD_ITEM *aB)
void SetIcon(const BITMAP_OPAQUE *aIcon)
Function SetIcon() Assigns an icon for the entry.
Definition: context_menu.h:74
void AddMenu(CONTEXT_MENU *aMenu, bool aExpand=false, const SELECTION_CONDITION &aCondition=SELECTION_CONDITIONS::ShowAlways, int aOrder=ANY_ORDER)
Function AddMenu()
void setTransitions() override
Sets up handlers for various events.
void SetTitle(const wxString &aTitle) override
Function SetTitle() Sets title for the context menu.
Class TOOL_ACTION.
Definition: tool_action.h:46
int GetX() const
int GetY() const
SELECTION_TOOL * m_selectionTool
int AlignRight(const TOOL_EVENT &aEvent)
Function AlignRight() Sets X coordinate of the selected items to the value of the right-most selected...
int Size() const
Returns the number of selected parts.
Definition: selection.h:112
bool Init() override
Function Init() Init() is called once upon a registration of the tool.
void DisplayError(wxWindow *parent, const wxString &text, int displaytime)
Function DisplayError displays an error or warning message box with aMessage.
Definition: confirm.cpp:71