2014-07-09 12:23:13 +00:00
|
|
|
/*
|
|
|
|
* This program source code file is part of KiCad, a free EDA CAD application.
|
|
|
|
*
|
2016-06-21 15:06:28 +00:00
|
|
|
* Copyright (C) 2014-2016 CERN
|
2014-07-09 12:23:13 +00:00
|
|
|
* @author Maciej Suminski <maciej.suminski@cern.ch>
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU General Public License
|
|
|
|
* as published by the Free Software Foundation; either version 2
|
|
|
|
* of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, you may find one here:
|
|
|
|
* http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
|
|
|
|
* or you may search the http://www.gnu.org website for the version 2 license,
|
|
|
|
* or you may write to the Free Software Foundation, Inc.,
|
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "placement_tool.h"
|
2017-02-21 12:42:08 +00:00
|
|
|
#include "pcb_actions.h"
|
2014-07-09 12:23:13 +00:00
|
|
|
#include "selection_tool.h"
|
2015-05-05 18:39:41 +00:00
|
|
|
#include <tool/tool_manager.h>
|
2014-07-09 12:23:13 +00:00
|
|
|
|
|
|
|
#include <wxPcbStruct.h>
|
|
|
|
#include <class_board.h>
|
|
|
|
#include <ratsnest_data.h>
|
2016-06-21 15:06:28 +00:00
|
|
|
#include <board_commit.h>
|
2017-02-20 18:10:20 +00:00
|
|
|
#include <bitmaps.h>
|
2014-07-09 12:23:13 +00:00
|
|
|
|
|
|
|
#include <confirm.h>
|
2016-12-20 16:50:29 +00:00
|
|
|
#include <menus_helpers.h>
|
2014-07-09 12:23:13 +00:00
|
|
|
|
2017-02-20 18:10:20 +00:00
|
|
|
// Placement tool
|
2017-04-18 15:32:05 +00:00
|
|
|
TOOL_ACTION PCB_ACTIONS::alignTop( "pcbnew.AlignAndDistribute.alignTop",
|
2017-02-20 18:10:20 +00:00
|
|
|
AS_GLOBAL, 0,
|
|
|
|
_( "Align to Top" ),
|
|
|
|
_( "Aligns selected items to the top edge" ), up_xpm );
|
|
|
|
|
2017-04-18 15:32:05 +00:00
|
|
|
TOOL_ACTION PCB_ACTIONS::alignBottom( "pcbnew.AlignAndDistribute.alignBottom",
|
2017-02-20 18:10:20 +00:00
|
|
|
AS_GLOBAL, 0,
|
|
|
|
_( "Align to Bottom" ),
|
|
|
|
_( "Aligns selected items to the bottom edge" ), down_xpm );
|
|
|
|
|
2017-04-18 15:32:05 +00:00
|
|
|
TOOL_ACTION PCB_ACTIONS::alignLeft( "pcbnew.AlignAndDistribute.alignLeft",
|
2017-02-20 18:10:20 +00:00
|
|
|
AS_GLOBAL, 0,
|
|
|
|
_( "Align to Left" ),
|
|
|
|
_( "Aligns selected items to the left edge" ), left_xpm );
|
|
|
|
|
2017-04-18 15:32:05 +00:00
|
|
|
TOOL_ACTION PCB_ACTIONS::alignRight( "pcbnew.AlignAndDistribute.alignRight",
|
2017-02-20 18:10:20 +00:00
|
|
|
AS_GLOBAL, 0,
|
|
|
|
_( "Align to Right" ),
|
|
|
|
_( "Aligns selected items to the right edge" ), right_xpm );
|
|
|
|
|
2017-04-18 15:32:05 +00:00
|
|
|
TOOL_ACTION PCB_ACTIONS::distributeHorizontally( "pcbnew.AlignAndDistribute.distributeHorizontally",
|
2017-02-20 18:10:20 +00:00
|
|
|
AS_GLOBAL, 0,
|
|
|
|
_( "Distribute Horizontally" ),
|
|
|
|
_( "Distributes selected items along the horizontal axis" ), distribute_horizontal_xpm );
|
|
|
|
|
2017-04-18 15:32:05 +00:00
|
|
|
TOOL_ACTION PCB_ACTIONS::distributeVertically( "pcbnew.AlignAndDistribute.distributeVertically",
|
2017-02-20 18:10:20 +00:00
|
|
|
AS_GLOBAL, 0,
|
|
|
|
_( "Distribute Vertically" ),
|
|
|
|
_( "Distributes selected items along the vertical axis" ), distribute_vertical_xpm );
|
|
|
|
|
|
|
|
|
2017-04-18 15:32:05 +00:00
|
|
|
ALIGN_DISTRIBUTE_TOOL::ALIGN_DISTRIBUTE_TOOL() :
|
2015-07-24 07:42:46 +00:00
|
|
|
TOOL_INTERACTIVE( "pcbnew.Placement" ), m_selectionTool( NULL ), m_placementMenu( NULL )
|
2014-07-09 12:23:13 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2017-04-18 15:32:05 +00:00
|
|
|
ALIGN_DISTRIBUTE_TOOL::~ALIGN_DISTRIBUTE_TOOL()
|
2014-07-09 12:23:13 +00:00
|
|
|
{
|
2015-07-24 07:42:46 +00:00
|
|
|
delete m_placementMenu;
|
2014-07-09 12:23:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-04-18 15:32:05 +00:00
|
|
|
bool ALIGN_DISTRIBUTE_TOOL::Init()
|
2014-07-09 12:23:13 +00:00
|
|
|
{
|
|
|
|
// Find the selection tool, so they can cooperate
|
|
|
|
m_selectionTool = static_cast<SELECTION_TOOL*>( m_toolMgr->FindTool( "pcbnew.InteractiveSelection" ) );
|
|
|
|
|
|
|
|
if( !m_selectionTool )
|
|
|
|
{
|
|
|
|
DisplayError( NULL, wxT( "pcbnew.InteractiveSelection tool is not available" ) );
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2014-07-09 12:23:13 +00:00
|
|
|
// Create a context menu and make it available through selection tool
|
2015-07-24 07:42:46 +00:00
|
|
|
m_placementMenu = new CONTEXT_MENU;
|
2016-12-20 16:50:29 +00:00
|
|
|
m_placementMenu->SetIcon( align_items_xpm );
|
2017-01-23 13:47:49 +00:00
|
|
|
m_placementMenu->SetTitle( _( "Align/distribute" ) );
|
2016-12-20 16:50:29 +00:00
|
|
|
|
2017-01-23 09:59:56 +00:00
|
|
|
// Add all align/distribute commands
|
2017-02-21 12:42:08 +00:00
|
|
|
m_placementMenu->Add( PCB_ACTIONS::alignTop );
|
|
|
|
m_placementMenu->Add( PCB_ACTIONS::alignBottom );
|
|
|
|
m_placementMenu->Add( PCB_ACTIONS::alignLeft );
|
|
|
|
m_placementMenu->Add( PCB_ACTIONS::alignRight );
|
2015-07-24 07:42:46 +00:00
|
|
|
m_placementMenu->AppendSeparator();
|
2017-02-21 12:42:08 +00:00
|
|
|
m_placementMenu->Add( PCB_ACTIONS::distributeHorizontally );
|
|
|
|
m_placementMenu->Add( PCB_ACTIONS::distributeVertically );
|
2016-12-20 16:50:29 +00:00
|
|
|
|
2017-01-23 13:47:49 +00:00
|
|
|
m_selectionTool->GetToolMenu().GetMenu().AddMenu( m_placementMenu, false,
|
2017-01-12 10:07:52 +00:00
|
|
|
SELECTION_CONDITIONS::MoreThan( 1 ) );
|
2014-07-09 12:23:13 +00:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-04-18 15:32:05 +00:00
|
|
|
int ALIGN_DISTRIBUTE_TOOL::AlignTop( const TOOL_EVENT& aEvent )
|
2014-07-09 12:23:13 +00:00
|
|
|
{
|
2014-07-09 13:10:32 +00:00
|
|
|
const SELECTION& selection = m_selectionTool->GetSelection();
|
2014-07-09 12:23:13 +00:00
|
|
|
|
2016-06-21 15:06:28 +00:00
|
|
|
if( selection.Size() <= 1 )
|
|
|
|
return 0;
|
2014-07-09 12:23:13 +00:00
|
|
|
|
2016-06-21 15:06:28 +00:00
|
|
|
BOARD_COMMIT commit( getEditFrame<PCB_BASE_FRAME>() );
|
2016-11-04 21:29:47 +00:00
|
|
|
commit.StageItems( selection, CHT_MODIFY );
|
2014-07-09 12:23:13 +00:00
|
|
|
|
2016-06-21 15:06:28 +00:00
|
|
|
// Compute the highest point of selection - it will be the edge of alignment
|
2016-11-04 21:29:47 +00:00
|
|
|
int top = selection.Front()->GetBoundingBox().GetY();
|
2014-07-09 12:23:13 +00:00
|
|
|
|
2016-06-21 15:06:28 +00:00
|
|
|
for( int i = 1; i < selection.Size(); ++i )
|
|
|
|
{
|
2016-11-04 21:29:47 +00:00
|
|
|
int currentTop = selection[i]->GetBoundingBox().GetY();
|
2014-07-09 12:23:13 +00:00
|
|
|
|
2016-06-21 15:06:28 +00:00
|
|
|
if( top > currentTop ) // Y decreases when going up
|
|
|
|
top = currentTop;
|
|
|
|
}
|
2014-07-09 12:23:13 +00:00
|
|
|
|
2016-06-21 15:06:28 +00:00
|
|
|
// Move the selected items
|
2017-02-28 03:04:44 +00:00
|
|
|
for( auto i : selection )
|
2016-06-21 15:06:28 +00:00
|
|
|
{
|
2017-02-28 03:04:44 +00:00
|
|
|
auto item = static_cast<BOARD_ITEM*>( i );
|
|
|
|
|
2016-06-21 15:06:28 +00:00
|
|
|
int difference = top - item->GetBoundingBox().GetY();
|
2014-07-09 12:23:13 +00:00
|
|
|
|
2016-06-21 15:06:28 +00:00
|
|
|
item->Move( wxPoint( 0, difference ) );
|
2014-07-09 12:23:13 +00:00
|
|
|
}
|
|
|
|
|
2016-06-21 15:06:28 +00:00
|
|
|
commit.Push( _( "Align to top" ) );
|
|
|
|
|
2014-07-09 12:23:13 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-04-18 15:32:05 +00:00
|
|
|
int ALIGN_DISTRIBUTE_TOOL::AlignBottom( const TOOL_EVENT& aEvent )
|
2014-07-09 12:23:13 +00:00
|
|
|
{
|
2014-07-09 13:10:32 +00:00
|
|
|
const SELECTION& selection = m_selectionTool->GetSelection();
|
2014-07-09 12:23:13 +00:00
|
|
|
|
2016-06-21 15:06:28 +00:00
|
|
|
if( selection.Size() <= 1 )
|
|
|
|
return 0;
|
2014-07-09 12:23:13 +00:00
|
|
|
|
2016-06-21 15:06:28 +00:00
|
|
|
BOARD_COMMIT commit( getEditFrame<PCB_BASE_FRAME>() );
|
2016-11-04 21:29:47 +00:00
|
|
|
commit.StageItems( selection, CHT_MODIFY );
|
2014-07-09 12:23:13 +00:00
|
|
|
|
2016-06-21 15:06:28 +00:00
|
|
|
// Compute the lowest point of selection - it will be the edge of alignment
|
2016-11-04 21:29:47 +00:00
|
|
|
int bottom = selection.Front()->GetBoundingBox().GetBottom();
|
2014-07-09 12:23:13 +00:00
|
|
|
|
2016-06-21 15:06:28 +00:00
|
|
|
for( int i = 1; i < selection.Size(); ++i )
|
|
|
|
{
|
2016-11-04 21:29:47 +00:00
|
|
|
int currentBottom = selection[i]->GetBoundingBox().GetBottom();
|
2014-07-09 12:23:13 +00:00
|
|
|
|
2016-06-21 15:06:28 +00:00
|
|
|
if( bottom < currentBottom ) // Y increases when going down
|
|
|
|
bottom = currentBottom;
|
|
|
|
}
|
2014-07-09 12:23:13 +00:00
|
|
|
|
2016-06-21 15:06:28 +00:00
|
|
|
// Move the selected items
|
2017-02-28 03:04:44 +00:00
|
|
|
for( auto i : selection )
|
2016-06-21 15:06:28 +00:00
|
|
|
{
|
2017-02-28 03:04:44 +00:00
|
|
|
auto item = static_cast<BOARD_ITEM*>( i );
|
|
|
|
|
2016-06-21 15:06:28 +00:00
|
|
|
int difference = bottom - item->GetBoundingBox().GetBottom();
|
2014-07-09 12:23:13 +00:00
|
|
|
|
2016-06-21 15:06:28 +00:00
|
|
|
item->Move( wxPoint( 0, difference ) );
|
2014-07-09 12:23:13 +00:00
|
|
|
}
|
|
|
|
|
2016-06-21 15:06:28 +00:00
|
|
|
commit.Push( _( "Align to bottom" ) );
|
|
|
|
|
2014-07-09 12:23:13 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-04-18 15:32:05 +00:00
|
|
|
int ALIGN_DISTRIBUTE_TOOL::AlignLeft( const TOOL_EVENT& aEvent )
|
2014-07-09 12:23:13 +00:00
|
|
|
{
|
2014-07-09 13:10:32 +00:00
|
|
|
const SELECTION& selection = m_selectionTool->GetSelection();
|
2014-07-09 12:23:13 +00:00
|
|
|
|
2016-06-21 15:06:28 +00:00
|
|
|
if( selection.Size() <= 1 )
|
|
|
|
return 0;
|
2014-07-09 12:23:13 +00:00
|
|
|
|
2016-06-21 15:06:28 +00:00
|
|
|
BOARD_COMMIT commit( getEditFrame<PCB_BASE_FRAME>() );
|
2016-11-04 21:29:47 +00:00
|
|
|
commit.StageItems( selection, CHT_MODIFY );
|
2014-07-09 12:23:13 +00:00
|
|
|
|
2016-06-21 15:06:28 +00:00
|
|
|
// Compute the leftmost point of selection - it will be the edge of alignment
|
2016-11-04 21:29:47 +00:00
|
|
|
int left = selection.Front()->GetBoundingBox().GetX();
|
2014-07-09 12:23:13 +00:00
|
|
|
|
2016-06-21 15:06:28 +00:00
|
|
|
for( int i = 1; i < selection.Size(); ++i )
|
|
|
|
{
|
2016-11-04 21:29:47 +00:00
|
|
|
int currentLeft = selection[i]->GetBoundingBox().GetX();
|
2014-07-09 12:23:13 +00:00
|
|
|
|
2016-06-21 15:06:28 +00:00
|
|
|
if( left > currentLeft ) // X decreases when going left
|
|
|
|
left = currentLeft;
|
|
|
|
}
|
2014-07-09 12:23:13 +00:00
|
|
|
|
2016-06-21 15:06:28 +00:00
|
|
|
// Move the selected items
|
2017-02-28 03:04:44 +00:00
|
|
|
for( auto i : selection )
|
2016-06-21 15:06:28 +00:00
|
|
|
{
|
2017-02-28 03:04:44 +00:00
|
|
|
auto item = static_cast<BOARD_ITEM*>( i );
|
|
|
|
|
2016-06-21 15:06:28 +00:00
|
|
|
int difference = left - item->GetBoundingBox().GetX();
|
2014-07-09 12:23:13 +00:00
|
|
|
|
2016-06-21 15:06:28 +00:00
|
|
|
item->Move( wxPoint( difference, 0 ) );
|
2014-07-09 12:23:13 +00:00
|
|
|
}
|
|
|
|
|
2016-06-21 15:06:28 +00:00
|
|
|
commit.Push( _( "Align to left" ) );
|
|
|
|
|
2014-07-09 12:23:13 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-04-18 15:32:05 +00:00
|
|
|
int ALIGN_DISTRIBUTE_TOOL::AlignRight( const TOOL_EVENT& aEvent )
|
2014-07-09 12:23:13 +00:00
|
|
|
{
|
2014-07-09 13:10:32 +00:00
|
|
|
const SELECTION& selection = m_selectionTool->GetSelection();
|
2014-07-09 12:23:13 +00:00
|
|
|
|
2016-06-21 15:06:28 +00:00
|
|
|
if( selection.Size() <= 1 )
|
|
|
|
return 0;
|
2014-07-09 12:23:13 +00:00
|
|
|
|
2016-06-21 15:06:28 +00:00
|
|
|
BOARD_COMMIT commit( getEditFrame<PCB_BASE_FRAME>() );
|
2016-11-04 21:29:47 +00:00
|
|
|
commit.StageItems( selection, CHT_MODIFY );
|
2014-07-09 12:23:13 +00:00
|
|
|
|
2016-06-21 15:06:28 +00:00
|
|
|
// Compute the rightmost point of selection - it will be the edge of alignment
|
2016-11-04 21:29:47 +00:00
|
|
|
int right = selection.Front()->GetBoundingBox().GetRight();
|
2014-07-09 12:23:13 +00:00
|
|
|
|
2016-06-21 15:06:28 +00:00
|
|
|
for( int i = 1; i < selection.Size(); ++i )
|
|
|
|
{
|
2016-11-04 21:29:47 +00:00
|
|
|
int currentRight = selection[i]->GetBoundingBox().GetRight();
|
2014-07-09 12:23:13 +00:00
|
|
|
|
2016-06-21 15:06:28 +00:00
|
|
|
if( right < currentRight ) // X increases when going right
|
|
|
|
right = currentRight;
|
|
|
|
}
|
2014-07-09 12:23:13 +00:00
|
|
|
|
2016-06-21 15:06:28 +00:00
|
|
|
// Move the selected items
|
2017-02-28 03:04:44 +00:00
|
|
|
for( auto i : selection )
|
2016-06-21 15:06:28 +00:00
|
|
|
{
|
2017-02-28 03:04:44 +00:00
|
|
|
auto item = static_cast<BOARD_ITEM*>( i );
|
|
|
|
|
2016-06-21 15:06:28 +00:00
|
|
|
int difference = right - item->GetBoundingBox().GetRight();
|
2014-07-09 12:23:13 +00:00
|
|
|
|
2016-06-21 15:06:28 +00:00
|
|
|
item->Move( wxPoint( difference, 0 ) );
|
2014-07-09 12:23:13 +00:00
|
|
|
}
|
|
|
|
|
2016-06-21 15:06:28 +00:00
|
|
|
commit.Push( _( "Align to right" ) );
|
|
|
|
|
2014-07-09 12:23:13 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static bool compareX( const BOARD_ITEM* aA, const BOARD_ITEM* aB )
|
|
|
|
{
|
|
|
|
return aA->GetBoundingBox().Centre().x < aB->GetBoundingBox().Centre().x;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static bool compareY( const BOARD_ITEM* aA, const BOARD_ITEM* aB )
|
|
|
|
{
|
|
|
|
return aA->GetBoundingBox().Centre().y < aB->GetBoundingBox().Centre().y;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-04-18 15:32:05 +00:00
|
|
|
int ALIGN_DISTRIBUTE_TOOL::DistributeHorizontally( const TOOL_EVENT& aEvent )
|
2014-07-09 12:23:13 +00:00
|
|
|
{
|
2014-07-09 13:10:32 +00:00
|
|
|
const SELECTION& selection = m_selectionTool->GetSelection();
|
2014-07-09 12:23:13 +00:00
|
|
|
|
2016-06-21 15:06:28 +00:00
|
|
|
if( selection.Size() <= 1 )
|
|
|
|
return 0;
|
2014-07-09 12:23:13 +00:00
|
|
|
|
2016-06-21 15:06:28 +00:00
|
|
|
BOARD_COMMIT commit( getEditFrame<PCB_BASE_FRAME>() );
|
2016-11-04 21:29:47 +00:00
|
|
|
commit.StageItems( selection, CHT_MODIFY );
|
2014-07-09 12:23:13 +00:00
|
|
|
|
2016-06-21 15:06:28 +00:00
|
|
|
// Prepare a list, so the items can be sorted by their X coordinate
|
2016-11-04 21:29:47 +00:00
|
|
|
std::vector<BOARD_ITEM*> itemsList;
|
2016-12-09 11:04:32 +00:00
|
|
|
|
|
|
|
for( auto item : selection )
|
2017-02-28 03:04:44 +00:00
|
|
|
itemsList.push_back( static_cast<BOARD_ITEM*>( item ) );
|
2014-07-09 12:23:13 +00:00
|
|
|
|
2016-06-21 15:06:28 +00:00
|
|
|
// Sort items by X coordinate
|
2016-11-04 21:29:47 +00:00
|
|
|
std::sort(itemsList.begin(), itemsList.end(), compareX );
|
2014-07-09 12:23:13 +00:00
|
|
|
|
2016-06-21 15:06:28 +00:00
|
|
|
// Expected X coordinate for the next item (=minX)
|
2016-11-04 21:29:47 +00:00
|
|
|
int position = itemsList.front()->GetBoundingBox().Centre().x;
|
2014-07-09 12:23:13 +00:00
|
|
|
|
2016-06-21 15:06:28 +00:00
|
|
|
// X coordinate for the last item
|
2016-11-04 21:29:47 +00:00
|
|
|
const int maxX = itemsList.back()->GetBoundingBox().Centre().x;
|
2014-07-09 12:23:13 +00:00
|
|
|
|
2016-06-21 15:06:28 +00:00
|
|
|
// Distance between items
|
|
|
|
const int distance = ( maxX - position ) / ( itemsList.size() - 1 );
|
2014-07-09 12:23:13 +00:00
|
|
|
|
2016-11-04 21:29:47 +00:00
|
|
|
for( auto item : itemsList )
|
2016-06-21 15:06:28 +00:00
|
|
|
{
|
|
|
|
int difference = position - item->GetBoundingBox().Centre().x;
|
2014-07-09 12:23:13 +00:00
|
|
|
|
2016-06-21 15:06:28 +00:00
|
|
|
item->Move( wxPoint( difference, 0 ) );
|
2014-07-09 12:23:13 +00:00
|
|
|
|
2016-06-21 15:06:28 +00:00
|
|
|
position += distance;
|
2014-07-09 12:23:13 +00:00
|
|
|
}
|
|
|
|
|
2016-06-21 15:06:28 +00:00
|
|
|
commit.Push( _( "Distribute horizontally" ) );
|
|
|
|
|
2014-07-09 12:23:13 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-04-18 15:32:05 +00:00
|
|
|
int ALIGN_DISTRIBUTE_TOOL::DistributeVertically( const TOOL_EVENT& aEvent )
|
2014-07-09 12:23:13 +00:00
|
|
|
{
|
2014-07-09 13:10:32 +00:00
|
|
|
const SELECTION& selection = m_selectionTool->GetSelection();
|
2014-07-09 12:23:13 +00:00
|
|
|
|
2016-06-21 15:06:28 +00:00
|
|
|
if( selection.Size() <= 1 )
|
|
|
|
return 0;
|
2014-07-09 12:23:13 +00:00
|
|
|
|
2016-06-21 15:06:28 +00:00
|
|
|
BOARD_COMMIT commit( getEditFrame<PCB_BASE_FRAME>() );
|
2016-11-04 21:29:47 +00:00
|
|
|
commit.StageItems( selection, CHT_MODIFY );
|
2014-07-09 12:23:13 +00:00
|
|
|
|
2016-06-21 15:06:28 +00:00
|
|
|
// Prepare a list, so the items can be sorted by their Y coordinate
|
2016-11-04 21:29:47 +00:00
|
|
|
std::vector<BOARD_ITEM*> itemsList;
|
2016-12-09 11:04:32 +00:00
|
|
|
|
|
|
|
for( auto item : selection )
|
2017-02-28 03:04:44 +00:00
|
|
|
itemsList.push_back( static_cast<BOARD_ITEM*>( item ) );
|
2014-07-09 12:23:13 +00:00
|
|
|
|
2016-06-21 15:06:28 +00:00
|
|
|
// Sort items by Y coordinate
|
2016-12-09 11:04:32 +00:00
|
|
|
std::sort( itemsList.begin(), itemsList.end(), compareY );
|
2014-07-09 12:23:13 +00:00
|
|
|
|
2016-06-21 15:06:28 +00:00
|
|
|
// Expected Y coordinate for the next item (=minY)
|
|
|
|
int position = (*itemsList.begin())->GetBoundingBox().Centre().y;
|
2014-07-09 12:23:13 +00:00
|
|
|
|
2016-06-21 15:06:28 +00:00
|
|
|
// Y coordinate for the last item
|
|
|
|
const int maxY = (*itemsList.rbegin())->GetBoundingBox().Centre().y;
|
2014-07-09 12:23:13 +00:00
|
|
|
|
2016-06-21 15:06:28 +00:00
|
|
|
// Distance between items
|
|
|
|
const int distance = ( maxY - position ) / ( itemsList.size() - 1 );
|
2014-07-09 12:23:13 +00:00
|
|
|
|
2016-11-04 21:29:47 +00:00
|
|
|
for( auto item : itemsList )
|
2016-06-21 15:06:28 +00:00
|
|
|
{
|
|
|
|
int difference = position - item->GetBoundingBox().Centre().y;
|
2014-07-09 12:23:13 +00:00
|
|
|
|
2016-06-21 15:06:28 +00:00
|
|
|
item->Move( wxPoint( 0, difference ) );
|
2014-07-09 12:23:13 +00:00
|
|
|
|
2016-06-21 15:06:28 +00:00
|
|
|
position += distance;
|
2014-07-09 12:23:13 +00:00
|
|
|
}
|
|
|
|
|
2016-06-21 15:06:28 +00:00
|
|
|
commit.Push( _( "Distribute vertically" ) );
|
|
|
|
|
2014-07-09 12:23:13 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-07-31 12:30:51 +00:00
|
|
|
void ALIGN_DISTRIBUTE_TOOL::setTransitions()
|
2014-07-09 12:23:13 +00:00
|
|
|
{
|
2017-04-18 15:32:05 +00:00
|
|
|
Go( &ALIGN_DISTRIBUTE_TOOL::AlignTop, PCB_ACTIONS::alignTop.MakeEvent() );
|
|
|
|
Go( &ALIGN_DISTRIBUTE_TOOL::AlignBottom, PCB_ACTIONS::alignBottom.MakeEvent() );
|
|
|
|
Go( &ALIGN_DISTRIBUTE_TOOL::AlignLeft, PCB_ACTIONS::alignLeft.MakeEvent() );
|
|
|
|
Go( &ALIGN_DISTRIBUTE_TOOL::AlignRight, PCB_ACTIONS::alignRight.MakeEvent() );
|
2014-07-09 12:23:13 +00:00
|
|
|
|
2017-04-18 15:32:05 +00:00
|
|
|
Go( &ALIGN_DISTRIBUTE_TOOL::DistributeHorizontally, PCB_ACTIONS::distributeHorizontally.MakeEvent() );
|
|
|
|
Go( &ALIGN_DISTRIBUTE_TOOL::DistributeVertically, PCB_ACTIONS::distributeVertically.MakeEvent() );
|
2014-07-09 12:23:13 +00:00
|
|
|
}
|