kicad/pcbnew/tools/placement_tool.cpp

325 lines
9.7 KiB
C++
Raw Normal View History

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"
#include "common_actions.h"
#include "selection_tool.h"
#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>
2014-07-09 12:23:13 +00:00
#include <confirm.h>
PLACEMENT_TOOL::PLACEMENT_TOOL() :
TOOL_INTERACTIVE( "pcbnew.Placement" ), m_selectionTool( NULL ), m_placementMenu( NULL )
2014-07-09 12:23:13 +00:00
{
}
PLACEMENT_TOOL::~PLACEMENT_TOOL()
{
delete m_placementMenu;
2014-07-09 12:23:13 +00:00
}
bool PLACEMENT_TOOL::Init()
{
// 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
m_placementMenu = new CONTEXT_MENU;
m_placementMenu->Add( COMMON_ACTIONS::alignTop );
m_placementMenu->Add( COMMON_ACTIONS::alignBottom );
m_placementMenu->Add( COMMON_ACTIONS::alignLeft );
m_placementMenu->Add( COMMON_ACTIONS::alignRight );
m_placementMenu->AppendSeparator();
m_placementMenu->Add( COMMON_ACTIONS::distributeHorizontally );
m_placementMenu->Add( COMMON_ACTIONS::distributeVertically );
m_selectionTool->GetMenu().AddMenu( m_placementMenu, _( "Align/distribute" ), false,
SELECTION_CONDITIONS::MoreThan( 1 ) );
2014-07-09 12:23:13 +00:00
return true;
}
int PLACEMENT_TOOL::AlignTop( const TOOL_EVENT& aEvent )
2014-07-09 12:23:13 +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>() );
commit.Stage( selection.items, UR_CHANGED );
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
int top = selection.Item<BOARD_ITEM>( 0 )->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 )
{
int currentTop = selection.Item<BOARD_ITEM>( 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
for( int i = 0; i < selection.Size(); ++i )
{
BOARD_ITEM* item = selection.Item<BOARD_ITEM>( i );
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;
}
int PLACEMENT_TOOL::AlignBottom( const TOOL_EVENT& aEvent )
2014-07-09 12:23:13 +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>() );
commit.Stage( selection.items, UR_CHANGED );
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
int bottom = selection.Item<BOARD_ITEM>( 0 )->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 )
{
int currentBottom = selection.Item<BOARD_ITEM>( 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
for( int i = 0; i < selection.Size(); ++i )
{
BOARD_ITEM* item = selection.Item<BOARD_ITEM>( i );
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;
}
int PLACEMENT_TOOL::AlignLeft( const TOOL_EVENT& aEvent )
2014-07-09 12:23:13 +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>() );
commit.Stage( selection.items, UR_CHANGED );
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
int left = selection.Item<BOARD_ITEM>( 0 )->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 )
{
int currentLeft = selection.Item<BOARD_ITEM>( 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
for( int i = 0; i < selection.Size(); ++i )
{
BOARD_ITEM* item = selection.Item<BOARD_ITEM>( i );
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;
}
int PLACEMENT_TOOL::AlignRight( const TOOL_EVENT& aEvent )
2014-07-09 12:23:13 +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>() );
commit.Stage( selection.items, UR_CHANGED );
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
int right = selection.Item<BOARD_ITEM>( 0 )->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 )
{
int currentRight = selection.Item<BOARD_ITEM>( 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
for( int i = 0; i < selection.Size(); ++i )
{
BOARD_ITEM* item = selection.Item<BOARD_ITEM>( i );
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;
}
int PLACEMENT_TOOL::DistributeHorizontally( const TOOL_EVENT& aEvent )
2014-07-09 12:23:13 +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>() );
commit.Stage( selection.items, UR_CHANGED );
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
std::list<BOARD_ITEM*> itemsList;
for( int i = 0; i < selection.Size(); ++i )
itemsList.push_back( selection.Item<BOARD_ITEM>( i ) );
2014-07-09 12:23:13 +00:00
2016-06-21 15:06:28 +00:00
// Sort items by X coordinate
itemsList.sort( 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)
int position = (*itemsList.begin())->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
const int maxX = (*itemsList.rbegin())->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-06-21 15:06:28 +00:00
for( BOARD_ITEM* item : itemsList )
{
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;
}
int PLACEMENT_TOOL::DistributeVertically( const TOOL_EVENT& aEvent )
2014-07-09 12:23:13 +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>() );
commit.Stage( selection.items, UR_CHANGED );
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
std::list<BOARD_ITEM*> itemsList;
for( int i = 0; i < selection.Size(); ++i )
itemsList.push_back( selection.Item<BOARD_ITEM>( i ) );
2014-07-09 12:23:13 +00:00
2016-06-21 15:06:28 +00:00
// Sort items by Y coordinate
itemsList.sort( 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-06-21 15:06:28 +00:00
for( BOARD_ITEM* item : itemsList )
{
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;
}
void PLACEMENT_TOOL::SetTransitions()
2014-07-09 12:23:13 +00:00
{
Go( &PLACEMENT_TOOL::AlignTop, COMMON_ACTIONS::alignTop.MakeEvent() );
Go( &PLACEMENT_TOOL::AlignBottom, COMMON_ACTIONS::alignBottom.MakeEvent() );
Go( &PLACEMENT_TOOL::AlignLeft, COMMON_ACTIONS::alignLeft.MakeEvent() );
Go( &PLACEMENT_TOOL::AlignRight, COMMON_ACTIONS::alignRight.MakeEvent() );
Go( &PLACEMENT_TOOL::DistributeHorizontally, COMMON_ACTIONS::distributeHorizontally.MakeEvent() );
Go( &PLACEMENT_TOOL::DistributeVertically, COMMON_ACTIONS::distributeVertically.MakeEvent() );
}