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
|
|
|
|
*/
|
2018-01-31 21:24:32 +00:00
|
|
|
#include "tool/selection.h"
|
2014-07-09 12:23:13 +00:00
|
|
|
#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"
|
2018-03-09 06:25:06 +00:00
|
|
|
#include "edit_tool.h"
|
2015-05-05 18:39:41 +00:00
|
|
|
#include <tool/tool_manager.h>
|
2014-07-09 12:23:13 +00:00
|
|
|
|
2018-01-29 20:58:58 +00:00
|
|
|
#include <pcb_edit_frame.h>
|
2014-07-09 12:23:13 +00:00
|
|
|
#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
|
|
|
|
2017-04-18 15:32:05 +00:00
|
|
|
ALIGN_DISTRIBUTE_TOOL::ALIGN_DISTRIBUTE_TOOL() :
|
2018-10-10 05:00:07 +00:00
|
|
|
TOOL_INTERACTIVE( "pcbnew.Placement" ), m_selectionTool( NULL ), m_placementMenu( NULL ),
|
|
|
|
m_frame( 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
|
2019-05-13 20:42:40 +00:00
|
|
|
m_selectionTool = m_toolMgr->GetTool<SELECTION_TOOL>();
|
2014-07-09 12:23:13 +00:00
|
|
|
|
2020-04-22 15:31:30 +00:00
|
|
|
wxASSERT_MSG( m_selectionTool, "pcbnew.InteractiveSelection tool is not available" );
|
2014-07-09 12:23:13 +00:00
|
|
|
|
2018-10-10 05:00:07 +00:00
|
|
|
m_frame = getEditFrame<PCB_BASE_FRAME>();
|
|
|
|
|
2014-07-09 12:23:13 +00:00
|
|
|
// Create a context menu and make it available through selection tool
|
2019-06-15 00:29:42 +00:00
|
|
|
m_placementMenu = new ACTION_MENU( true );
|
2019-06-09 22:32:08 +00:00
|
|
|
m_placementMenu->SetTool( this );
|
2016-12-20 16:50:29 +00:00
|
|
|
m_placementMenu->SetIcon( align_items_xpm );
|
2018-02-09 16:28:33 +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 );
|
2018-02-08 16:01:36 +00:00
|
|
|
m_placementMenu->Add( PCB_ACTIONS::alignCenterX );
|
|
|
|
m_placementMenu->Add( PCB_ACTIONS::alignCenterY );
|
2020-04-22 15:31:30 +00:00
|
|
|
|
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
|
|
|
|
2019-05-13 20:42:40 +00:00
|
|
|
CONDITIONAL_MENU& selToolMenu = m_selectionTool->GetToolMenu().GetMenu();
|
|
|
|
selToolMenu.AddMenu( m_placementMenu, SELECTION_CONDITIONS::MoreThan( 1 ) );
|
2014-07-09 12:23:13 +00:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-12-14 12:22:00 +00:00
|
|
|
template <class T>
|
|
|
|
ALIGNMENT_RECTS GetBoundingBoxes( const T &sel )
|
2018-01-31 21:24:32 +00:00
|
|
|
{
|
2018-12-14 12:22:00 +00:00
|
|
|
ALIGNMENT_RECTS rects;
|
2018-01-31 21:24:32 +00:00
|
|
|
|
2018-12-14 12:22:00 +00:00
|
|
|
for( auto item : sel )
|
|
|
|
{
|
|
|
|
BOARD_ITEM* boardItem = static_cast<BOARD_ITEM*>( item );
|
2018-02-17 10:36:41 +00:00
|
|
|
|
2018-12-14 12:22:00 +00:00
|
|
|
if( item->Type() == PCB_MODULE_T )
|
|
|
|
rects.emplace_back( std::make_pair( boardItem, static_cast<MODULE*>( item )->GetFootprintRect() ) );
|
|
|
|
else
|
|
|
|
rects.emplace_back( std::make_pair( boardItem, item->GetBoundingBox() ) );
|
|
|
|
}
|
|
|
|
|
|
|
|
return rects;
|
2018-01-31 21:24:32 +00:00
|
|
|
}
|
|
|
|
|
2018-02-17 10:36:41 +00:00
|
|
|
|
2018-12-14 12:22:00 +00:00
|
|
|
template< typename T >
|
|
|
|
int ALIGN_DISTRIBUTE_TOOL::selectTarget( ALIGNMENT_RECTS& aItems, ALIGNMENT_RECTS& aLocked, T aGetValue )
|
2018-01-31 21:24:32 +00:00
|
|
|
{
|
2018-12-14 12:22:00 +00:00
|
|
|
wxPoint curPos( getViewControls()->GetCursorPosition().x, getViewControls()->GetCursorPosition().y );
|
2018-01-31 21:24:32 +00:00
|
|
|
|
2018-12-14 12:22:00 +00:00
|
|
|
// after sorting, the fist item acts as the target for all others
|
|
|
|
// unless we have a locked item, in which case we use that for the target
|
|
|
|
int target = !aLocked.size() ? aGetValue( aItems.front() ): aGetValue( aLocked.front() );
|
2018-02-17 10:36:41 +00:00
|
|
|
|
2020-01-07 04:01:21 +00:00
|
|
|
// We take the first target that overlaps our cursor.
|
|
|
|
// This is deterministic because we assume sorted arrays
|
|
|
|
for( auto item : aLocked )
|
2018-12-14 12:22:00 +00:00
|
|
|
{
|
2020-01-07 04:01:21 +00:00
|
|
|
if( item.second.Contains( curPos ) )
|
|
|
|
return aGetValue( item );
|
|
|
|
}
|
2018-02-17 10:36:41 +00:00
|
|
|
|
2020-01-07 04:01:21 +00:00
|
|
|
// If there are locked items, prefer aligning to them over
|
|
|
|
// aligning to the cursor as they do not move
|
|
|
|
if( aLocked.empty() )
|
|
|
|
{
|
|
|
|
for( auto item : aItems )
|
2018-12-14 12:22:00 +00:00
|
|
|
{
|
2020-01-07 04:01:21 +00:00
|
|
|
if( item.second.Contains( curPos ) )
|
|
|
|
return aGetValue( item );
|
2018-12-14 12:22:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return target;
|
2018-02-08 16:01:36 +00:00
|
|
|
}
|
|
|
|
|
2018-02-17 10:36:41 +00:00
|
|
|
|
2018-12-14 12:22:00 +00:00
|
|
|
template< typename T >
|
|
|
|
size_t ALIGN_DISTRIBUTE_TOOL::GetSelections( ALIGNMENT_RECTS& aItems, ALIGNMENT_RECTS& aLocked, T aCompare )
|
2018-01-31 21:24:32 +00:00
|
|
|
{
|
|
|
|
|
2019-06-08 21:48:22 +00:00
|
|
|
PCBNEW_SELECTION& selection = m_selectionTool->RequestSelection(
|
2018-12-14 12:22:00 +00:00
|
|
|
[]( const VECTOR2I& aPt, GENERAL_COLLECTOR& aCollector )
|
|
|
|
{ EditToolSelectionFilter( aCollector, EXCLUDE_TRANSIENTS ); } );
|
2018-02-17 10:36:41 +00:00
|
|
|
|
2018-12-14 12:22:00 +00:00
|
|
|
if( selection.Size() <= 1 )
|
|
|
|
return 0;
|
2018-01-31 21:24:32 +00:00
|
|
|
|
2018-12-14 12:22:00 +00:00
|
|
|
std::vector<BOARD_ITEM*> lockedItems;
|
|
|
|
selection = m_selectionTool->RequestSelection(
|
|
|
|
[]( const VECTOR2I& aPt, GENERAL_COLLECTOR& aCollector )
|
|
|
|
{ EditToolSelectionFilter( aCollector, EXCLUDE_LOCKED ); }, &lockedItems );
|
2018-01-31 21:24:32 +00:00
|
|
|
|
2018-12-14 12:22:00 +00:00
|
|
|
aItems = GetBoundingBoxes( selection );
|
|
|
|
aLocked = GetBoundingBoxes( lockedItems );
|
|
|
|
std::sort( aItems.begin(), aItems.end(), aCompare );
|
|
|
|
std::sort( aLocked.begin(), aLocked.end(), aCompare );
|
2018-02-17 10:36:41 +00:00
|
|
|
|
2018-12-14 12:22:00 +00:00
|
|
|
return aItems.size();
|
2018-01-31 21:24:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
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
|
|
|
{
|
2018-12-14 12:22:00 +00:00
|
|
|
ALIGNMENT_RECTS itemsToAlign;
|
|
|
|
ALIGNMENT_RECTS locked_items;
|
2014-07-09 12:23:13 +00:00
|
|
|
|
2018-12-14 12:22:00 +00:00
|
|
|
if( !GetSelections( itemsToAlign, locked_items, []( const ALIGNMENT_RECT left, const ALIGNMENT_RECT right)
|
|
|
|
{ return ( left.second.GetTop() < right.second.GetTop() ); } ) )
|
2016-06-21 15:06:28 +00:00
|
|
|
return 0;
|
2014-07-09 12:23:13 +00:00
|
|
|
|
2018-10-10 05:00:07 +00:00
|
|
|
BOARD_COMMIT commit( m_frame );
|
2018-12-14 12:22:00 +00:00
|
|
|
commit.StageItems( m_selectionTool->GetSelection(), CHT_MODIFY );
|
|
|
|
auto targetTop = selectTarget( itemsToAlign, locked_items, []( const ALIGNMENT_RECT& aVal )
|
|
|
|
{ return aVal.second.GetTop(); } );
|
2014-07-09 12:23:13 +00:00
|
|
|
|
2016-06-21 15:06:28 +00:00
|
|
|
// Move the selected items
|
2018-02-08 14:47:24 +00:00
|
|
|
for( auto& i : itemsToAlign )
|
2016-06-21 15:06:28 +00:00
|
|
|
{
|
2018-12-14 12:22:00 +00:00
|
|
|
int difference = targetTop - i.second.GetTop();
|
2018-02-28 23:13:21 +00:00
|
|
|
BOARD_ITEM* item = i.first;
|
|
|
|
|
|
|
|
// Don't move a pad by itself unless editing the footprint
|
2019-09-05 22:00:47 +00:00
|
|
|
if( item->Type() == PCB_PAD_T && m_frame->IsType( FRAME_PCB_EDITOR ) )
|
2018-02-28 23:13:21 +00:00
|
|
|
item = item->GetParent();
|
|
|
|
|
|
|
|
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
|
|
|
{
|
2018-12-14 12:22:00 +00:00
|
|
|
ALIGNMENT_RECTS itemsToAlign;
|
|
|
|
ALIGNMENT_RECTS locked_items;
|
2014-07-09 12:23:13 +00:00
|
|
|
|
2018-12-14 12:22:00 +00:00
|
|
|
if( !GetSelections( itemsToAlign, locked_items, []( const ALIGNMENT_RECT left, const ALIGNMENT_RECT right)
|
|
|
|
{ return ( left.second.GetBottom() < right.second.GetBottom() ); } ) )
|
2016-06-21 15:06:28 +00:00
|
|
|
return 0;
|
2014-07-09 12:23:13 +00:00
|
|
|
|
2018-10-10 05:00:07 +00:00
|
|
|
BOARD_COMMIT commit( m_frame );
|
2018-12-14 12:22:00 +00:00
|
|
|
commit.StageItems( m_selectionTool->GetSelection(), CHT_MODIFY );
|
|
|
|
auto targetBottom = selectTarget( itemsToAlign, locked_items, []( const ALIGNMENT_RECT& aVal )
|
|
|
|
{ return aVal.second.GetBottom(); } );
|
2014-07-09 12:23:13 +00:00
|
|
|
|
2016-06-21 15:06:28 +00:00
|
|
|
// Move the selected items
|
2018-02-08 14:47:24 +00:00
|
|
|
for( auto& i : itemsToAlign )
|
2016-06-21 15:06:28 +00:00
|
|
|
{
|
2018-02-08 14:47:24 +00:00
|
|
|
int difference = targetBottom - i.second.GetBottom();
|
2018-02-28 23:13:21 +00:00
|
|
|
BOARD_ITEM* item = i.first;
|
|
|
|
|
|
|
|
// Don't move a pad by itself unless editing the footprint
|
2019-09-05 22:00:47 +00:00
|
|
|
if( item->Type() == PCB_PAD_T && m_frame->IsType( FRAME_PCB_EDITOR ) )
|
2018-02-28 23:13:21 +00:00
|
|
|
item = item->GetParent();
|
|
|
|
|
|
|
|
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 )
|
2017-12-09 19:16:27 +00:00
|
|
|
{
|
|
|
|
// Because this tool uses bounding boxes and they aren't mirrored even when
|
|
|
|
// the view is mirrored, we need to call the other one if mirrored.
|
|
|
|
if( getView()->IsMirroredX() )
|
|
|
|
{
|
|
|
|
return doAlignRight();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return doAlignLeft();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int ALIGN_DISTRIBUTE_TOOL::doAlignLeft()
|
2014-07-09 12:23:13 +00:00
|
|
|
{
|
2018-12-14 12:22:00 +00:00
|
|
|
ALIGNMENT_RECTS itemsToAlign;
|
|
|
|
ALIGNMENT_RECTS locked_items;
|
2014-07-09 12:23:13 +00:00
|
|
|
|
2018-12-14 12:22:00 +00:00
|
|
|
if( !GetSelections( itemsToAlign, locked_items, []( const ALIGNMENT_RECT left, const ALIGNMENT_RECT right)
|
|
|
|
{ return ( left.second.GetLeft() < right.second.GetLeft() ); } ) )
|
2016-06-21 15:06:28 +00:00
|
|
|
return 0;
|
2014-07-09 12:23:13 +00:00
|
|
|
|
2018-10-10 05:00:07 +00:00
|
|
|
BOARD_COMMIT commit( m_frame );
|
2018-12-14 12:22:00 +00:00
|
|
|
commit.StageItems( m_selectionTool->GetSelection(), CHT_MODIFY );
|
|
|
|
auto targetLeft = selectTarget( itemsToAlign, locked_items, []( const ALIGNMENT_RECT& aVal )
|
|
|
|
{ return aVal.second.GetLeft(); } );
|
2014-07-09 12:23:13 +00:00
|
|
|
|
2016-06-21 15:06:28 +00:00
|
|
|
// Move the selected items
|
2018-02-08 14:47:24 +00:00
|
|
|
for( auto& i : itemsToAlign )
|
2016-06-21 15:06:28 +00:00
|
|
|
{
|
2018-12-14 12:22:00 +00:00
|
|
|
int difference = targetLeft - i.second.GetLeft();
|
2018-02-28 23:13:21 +00:00
|
|
|
BOARD_ITEM* item = i.first;
|
|
|
|
|
|
|
|
// Don't move a pad by itself unless editing the footprint
|
2019-09-05 22:00:47 +00:00
|
|
|
if( item->Type() == PCB_PAD_T && m_frame->IsType( FRAME_PCB_EDITOR ) )
|
2018-02-28 23:13:21 +00:00
|
|
|
item = item->GetParent();
|
|
|
|
|
|
|
|
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 )
|
2017-12-09 19:16:27 +00:00
|
|
|
{
|
|
|
|
// Because this tool uses bounding boxes and they aren't mirrored even when
|
|
|
|
// the view is mirrored, we need to call the other one if mirrored.
|
|
|
|
if( getView()->IsMirroredX() )
|
|
|
|
{
|
|
|
|
return doAlignLeft();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return doAlignRight();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int ALIGN_DISTRIBUTE_TOOL::doAlignRight()
|
2014-07-09 12:23:13 +00:00
|
|
|
{
|
2018-12-14 12:22:00 +00:00
|
|
|
ALIGNMENT_RECTS itemsToAlign;
|
|
|
|
ALIGNMENT_RECTS locked_items;
|
2014-07-09 12:23:13 +00:00
|
|
|
|
2018-12-14 12:22:00 +00:00
|
|
|
if( !GetSelections( itemsToAlign, locked_items, []( const ALIGNMENT_RECT left, const ALIGNMENT_RECT right)
|
|
|
|
{ return ( left.second.GetRight() < right.second.GetRight() ); } ) )
|
2016-06-21 15:06:28 +00:00
|
|
|
return 0;
|
2014-07-09 12:23:13 +00:00
|
|
|
|
2018-10-10 05:00:07 +00:00
|
|
|
BOARD_COMMIT commit( m_frame );
|
2018-12-14 12:22:00 +00:00
|
|
|
commit.StageItems( m_selectionTool->GetSelection(), CHT_MODIFY );
|
|
|
|
auto targetRight = selectTarget( itemsToAlign, locked_items, []( const ALIGNMENT_RECT& aVal )
|
|
|
|
{ return aVal.second.GetRight(); } );
|
2014-07-09 12:23:13 +00:00
|
|
|
|
2016-06-21 15:06:28 +00:00
|
|
|
// Move the selected items
|
2018-02-08 14:47:24 +00:00
|
|
|
for( auto& i : itemsToAlign )
|
2016-06-21 15:06:28 +00:00
|
|
|
{
|
2018-02-08 14:47:24 +00:00
|
|
|
int difference = targetRight - i.second.GetRight();
|
2018-02-28 23:13:21 +00:00
|
|
|
BOARD_ITEM* item = i.first;
|
|
|
|
|
|
|
|
// Don't move a pad by itself unless editing the footprint
|
2019-09-05 22:00:47 +00:00
|
|
|
if( item->Type() == PCB_PAD_T && m_frame->IsType( FRAME_PCB_EDITOR ) )
|
2018-02-28 23:13:21 +00:00
|
|
|
item = item->GetParent();
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-02-08 16:01:36 +00:00
|
|
|
int ALIGN_DISTRIBUTE_TOOL::AlignCenterX( const TOOL_EVENT& aEvent )
|
|
|
|
{
|
2018-12-14 12:22:00 +00:00
|
|
|
ALIGNMENT_RECTS itemsToAlign;
|
|
|
|
ALIGNMENT_RECTS locked_items;
|
2018-02-08 16:01:36 +00:00
|
|
|
|
2018-12-14 12:22:00 +00:00
|
|
|
if( !GetSelections( itemsToAlign, locked_items, []( const ALIGNMENT_RECT left, const ALIGNMENT_RECT right)
|
|
|
|
{ return ( left.second.GetCenter().x < right.second.GetCenter().x ); } ) )
|
2018-02-08 16:01:36 +00:00
|
|
|
return 0;
|
|
|
|
|
2018-10-10 05:00:07 +00:00
|
|
|
BOARD_COMMIT commit( m_frame );
|
2018-12-14 12:22:00 +00:00
|
|
|
commit.StageItems( m_selectionTool->GetSelection(), CHT_MODIFY );
|
|
|
|
auto targetX = selectTarget( itemsToAlign, locked_items, []( const ALIGNMENT_RECT& aVal )
|
|
|
|
{ return aVal.second.GetCenter().x; } );
|
2018-02-08 16:01:36 +00:00
|
|
|
|
|
|
|
// Move the selected items
|
|
|
|
for( auto& i : itemsToAlign )
|
|
|
|
{
|
|
|
|
int difference = targetX - i.second.GetCenter().x;
|
2018-02-28 23:13:21 +00:00
|
|
|
BOARD_ITEM* item = i.first;
|
|
|
|
|
|
|
|
// Don't move a pad by itself unless editing the footprint
|
2019-09-05 22:00:47 +00:00
|
|
|
if( item->Type() == PCB_PAD_T && m_frame->IsType( FRAME_PCB_EDITOR ) )
|
2018-02-28 23:13:21 +00:00
|
|
|
item = item->GetParent();
|
|
|
|
|
|
|
|
item->Move( wxPoint( difference, 0 ) );
|
2018-02-08 16:01:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
commit.Push( _( "Align to middle" ) );
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int ALIGN_DISTRIBUTE_TOOL::AlignCenterY( const TOOL_EVENT& aEvent )
|
|
|
|
{
|
2018-12-14 12:22:00 +00:00
|
|
|
ALIGNMENT_RECTS itemsToAlign;
|
|
|
|
ALIGNMENT_RECTS locked_items;
|
2018-02-08 16:01:36 +00:00
|
|
|
|
2018-12-14 12:22:00 +00:00
|
|
|
if( !GetSelections( itemsToAlign, locked_items, []( const ALIGNMENT_RECT left, const ALIGNMENT_RECT right)
|
|
|
|
{ return ( left.second.GetCenter().y < right.second.GetCenter().y ); } ) )
|
2018-02-08 16:01:36 +00:00
|
|
|
return 0;
|
|
|
|
|
2018-10-10 05:00:07 +00:00
|
|
|
BOARD_COMMIT commit( m_frame );
|
2018-12-14 12:22:00 +00:00
|
|
|
commit.StageItems( m_selectionTool->GetSelection(), CHT_MODIFY );
|
|
|
|
auto targetY = selectTarget( itemsToAlign, locked_items, []( const ALIGNMENT_RECT& aVal )
|
|
|
|
{ return aVal.second.GetCenter().y; } );
|
2018-02-08 16:01:36 +00:00
|
|
|
|
|
|
|
// Move the selected items
|
|
|
|
for( auto& i : itemsToAlign )
|
|
|
|
{
|
|
|
|
int difference = targetY - i.second.GetCenter().y;
|
2018-02-28 23:13:21 +00:00
|
|
|
BOARD_ITEM* item = i.first;
|
|
|
|
|
|
|
|
// Don't move a pad by itself unless editing the footprint
|
2019-09-05 22:00:47 +00:00
|
|
|
if( item->Type() == PCB_PAD_T && m_frame->IsType( FRAME_PCB_EDITOR ) )
|
2018-02-28 23:13:21 +00:00
|
|
|
item = item->GetParent();
|
|
|
|
|
|
|
|
item->Move( wxPoint( 0, difference ) );
|
2018-02-08 16:01:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
commit.Push( _( "Align to center" ) );
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
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
|
|
|
{
|
2019-06-08 21:48:22 +00:00
|
|
|
PCBNEW_SELECTION& selection = m_selectionTool->RequestSelection(
|
2018-09-25 14:23:38 +00:00
|
|
|
[]( const VECTOR2I& aPt, GENERAL_COLLECTOR& aCollector )
|
|
|
|
{ EditToolSelectionFilter( aCollector, EXCLUDE_LOCKED | EXCLUDE_TRANSIENTS ); } );
|
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
|
|
|
|
2018-10-10 05:00:07 +00:00
|
|
|
BOARD_COMMIT commit( m_frame );
|
2016-11-04 21:29:47 +00:00
|
|
|
commit.StageItems( selection, CHT_MODIFY );
|
2014-07-09 12:23:13 +00:00
|
|
|
|
2018-02-08 14:47:24 +00:00
|
|
|
auto itemsToDistribute = GetBoundingBoxes( selection );
|
2016-12-09 11:04:32 +00:00
|
|
|
|
2018-01-31 21:29:16 +00:00
|
|
|
// find the last item by reverse sorting
|
2018-12-14 12:22:00 +00:00
|
|
|
std::sort( itemsToDistribute.begin(), itemsToDistribute.end(),
|
|
|
|
[] ( const ALIGNMENT_RECT left, const ALIGNMENT_RECT right)
|
|
|
|
{ return ( left.second.GetRight() > right.second.GetRight() ); } );
|
2018-02-08 14:47:24 +00:00
|
|
|
const auto lastItem = itemsToDistribute.begin()->first;
|
2014-07-09 12:23:13 +00:00
|
|
|
|
2018-02-26 20:54:33 +00:00
|
|
|
const auto maxRight = itemsToDistribute.begin()->second.GetRight();
|
|
|
|
|
2018-01-31 21:29:16 +00:00
|
|
|
// sort to get starting order
|
2018-12-14 12:22:00 +00:00
|
|
|
std::sort( itemsToDistribute.begin(), itemsToDistribute.end(),
|
|
|
|
[] ( const ALIGNMENT_RECT left, const ALIGNMENT_RECT right)
|
|
|
|
{ return ( left.second.GetX() < right.second.GetX() ); } );
|
2018-02-26 20:54:33 +00:00
|
|
|
const auto minX = itemsToDistribute.begin()->second.GetX();
|
|
|
|
auto totalGap = maxRight - minX;
|
|
|
|
int totalWidth = 0;
|
2014-07-09 12:23:13 +00:00
|
|
|
|
2018-02-08 14:47:24 +00:00
|
|
|
for( auto& i : itemsToDistribute )
|
2018-01-31 21:29:16 +00:00
|
|
|
{
|
2018-02-26 20:54:33 +00:00
|
|
|
totalWidth += i.second.GetWidth();
|
2018-01-31 21:29:16 +00:00
|
|
|
}
|
2014-07-09 12:23:13 +00:00
|
|
|
|
2018-02-26 20:54:33 +00:00
|
|
|
if( totalGap < totalWidth )
|
|
|
|
{
|
|
|
|
// the width of the items exceeds the gap (overlapping items) -> use center point spacing
|
|
|
|
doDistributeCentersHorizontally( itemsToDistribute );
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
totalGap -= totalWidth;
|
|
|
|
doDistributeGapsHorizontally( itemsToDistribute, lastItem, totalGap );
|
|
|
|
}
|
|
|
|
|
|
|
|
commit.Push( _( "Distribute horizontally" ) );
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
2014-07-09 12:23:13 +00:00
|
|
|
|
2018-02-26 20:54:33 +00:00
|
|
|
|
|
|
|
void ALIGN_DISTRIBUTE_TOOL::doDistributeGapsHorizontally( ALIGNMENT_RECTS& itemsToDistribute,
|
|
|
|
const BOARD_ITEM* lastItem, int totalGap ) const
|
|
|
|
{
|
|
|
|
const auto itemGap = totalGap / ( itemsToDistribute.size() - 1 );
|
2018-02-08 14:47:24 +00:00
|
|
|
auto targetX = itemsToDistribute.begin()->second.GetX();
|
2014-07-09 12:23:13 +00:00
|
|
|
|
2018-02-08 14:47:24 +00:00
|
|
|
for( auto& i : itemsToDistribute )
|
2018-01-31 21:29:16 +00:00
|
|
|
{
|
2018-10-10 05:00:07 +00:00
|
|
|
BOARD_ITEM* item = i.first;
|
|
|
|
|
2018-01-31 21:29:16 +00:00
|
|
|
// cover the corner case where the last item is wider than the previous item and gap
|
2018-10-10 05:00:07 +00:00
|
|
|
if( lastItem == item )
|
2018-01-31 21:29:16 +00:00
|
|
|
continue;
|
2014-07-09 12:23:13 +00:00
|
|
|
|
2018-10-10 05:00:07 +00:00
|
|
|
// Don't move a pad by itself unless editing the footprint
|
2019-09-05 22:00:47 +00:00
|
|
|
if( item->Type() == PCB_PAD_T && m_frame->IsType( FRAME_PCB_EDITOR ) )
|
2018-10-10 05:00:07 +00:00
|
|
|
item = item->GetParent();
|
|
|
|
|
2018-01-31 21:29:16 +00:00
|
|
|
int difference = targetX - i.second.GetX();
|
2018-10-10 05:00:07 +00:00
|
|
|
item->Move( wxPoint( difference, 0 ) );
|
2018-01-31 21:29:16 +00:00
|
|
|
targetX += ( i.second.GetWidth() + itemGap );
|
2014-07-09 12:23:13 +00:00
|
|
|
}
|
2018-02-26 20:54:33 +00:00
|
|
|
}
|
2014-07-09 12:23:13 +00:00
|
|
|
|
2016-06-21 15:06:28 +00:00
|
|
|
|
2018-02-26 20:54:33 +00:00
|
|
|
void ALIGN_DISTRIBUTE_TOOL::doDistributeCentersHorizontally( ALIGNMENT_RECTS &itemsToDistribute ) const
|
|
|
|
{
|
2018-12-14 12:22:00 +00:00
|
|
|
std::sort( itemsToDistribute.begin(), itemsToDistribute.end(),
|
|
|
|
[] ( const ALIGNMENT_RECT left, const ALIGNMENT_RECT right)
|
|
|
|
{ return ( left.second.GetCenter().x < right.second.GetCenter().x ); } );
|
2018-02-26 20:54:33 +00:00
|
|
|
const auto totalGap = ( itemsToDistribute.end()-1 )->second.GetCenter().x
|
|
|
|
- itemsToDistribute.begin()->second.GetCenter().x;
|
|
|
|
const auto itemGap = totalGap / ( itemsToDistribute.size() - 1 );
|
|
|
|
auto targetX = itemsToDistribute.begin()->second.GetCenter().x;
|
|
|
|
|
|
|
|
for( auto& i : itemsToDistribute )
|
2018-10-10 05:00:07 +00:00
|
|
|
{
|
|
|
|
BOARD_ITEM* item = i.first;
|
|
|
|
|
|
|
|
// Don't move a pad by itself unless editing the footprint
|
2019-09-05 22:00:47 +00:00
|
|
|
if( item->Type() == PCB_PAD_T && m_frame->IsType( FRAME_PCB_EDITOR ) )
|
2018-10-10 05:00:07 +00:00
|
|
|
item = item->GetParent();
|
|
|
|
|
|
|
|
int difference = targetX - i.second.GetCenter().x;
|
|
|
|
item->Move( wxPoint( difference, 0 ) );
|
|
|
|
targetX += ( itemGap );
|
|
|
|
}
|
2014-07-09 12:23:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
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
|
|
|
{
|
2019-06-08 21:48:22 +00:00
|
|
|
PCBNEW_SELECTION& selection = m_selectionTool->RequestSelection(
|
2018-09-25 14:23:38 +00:00
|
|
|
[]( const VECTOR2I& aPt, GENERAL_COLLECTOR& aCollector )
|
|
|
|
{ EditToolSelectionFilter( aCollector, EXCLUDE_LOCKED | EXCLUDE_TRANSIENTS ); } );
|
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
|
|
|
|
2018-10-10 05:00:07 +00:00
|
|
|
BOARD_COMMIT commit( m_frame );
|
2016-11-04 21:29:47 +00:00
|
|
|
commit.StageItems( selection, CHT_MODIFY );
|
2014-07-09 12:23:13 +00:00
|
|
|
|
2018-02-08 14:47:24 +00:00
|
|
|
auto itemsToDistribute = GetBoundingBoxes( selection );
|
2016-12-09 11:04:32 +00:00
|
|
|
|
2018-01-31 21:29:16 +00:00
|
|
|
// find the last item by reverse sorting
|
2018-12-14 12:22:00 +00:00
|
|
|
std::sort( itemsToDistribute.begin(), itemsToDistribute.end(),
|
|
|
|
[] ( const ALIGNMENT_RECT left, const ALIGNMENT_RECT right)
|
|
|
|
{ return ( left.second.GetBottom() > right.second.GetBottom() ); } );
|
2018-02-08 14:47:24 +00:00
|
|
|
const auto maxBottom = itemsToDistribute.begin()->second.GetBottom();
|
|
|
|
const auto lastItem = itemsToDistribute.begin()->first;
|
2018-01-31 21:29:16 +00:00
|
|
|
|
|
|
|
// sort to get starting order
|
2018-12-14 12:22:00 +00:00
|
|
|
std::sort( itemsToDistribute.begin(), itemsToDistribute.end(),
|
|
|
|
[] ( const ALIGNMENT_RECT left, const ALIGNMENT_RECT right)
|
|
|
|
{ return ( left.second.GetCenter().y < right.second.GetCenter().y ); } );
|
2018-02-26 20:54:33 +00:00
|
|
|
auto minY = itemsToDistribute.begin()->second.GetY();
|
2014-07-09 12:23:13 +00:00
|
|
|
|
2018-02-26 20:54:33 +00:00
|
|
|
auto totalGap = maxBottom - minY;
|
|
|
|
int totalHeight = 0;
|
2014-07-09 12:23:13 +00:00
|
|
|
|
2018-02-08 14:47:24 +00:00
|
|
|
for( auto& i : itemsToDistribute )
|
2018-01-31 21:29:16 +00:00
|
|
|
{
|
2018-02-26 20:54:33 +00:00
|
|
|
totalHeight += i.second.GetHeight();
|
2018-01-31 21:29:16 +00:00
|
|
|
}
|
2014-07-09 12:23:13 +00:00
|
|
|
|
2018-02-26 20:54:33 +00:00
|
|
|
if( totalGap < totalHeight )
|
|
|
|
{
|
|
|
|
// the width of the items exceeds the gap (overlapping items) -> use center point spacing
|
|
|
|
doDistributeCentersVertically( itemsToDistribute );
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
totalGap -= totalHeight;
|
|
|
|
doDistributeGapsVertically( itemsToDistribute, lastItem, totalGap );
|
|
|
|
}
|
|
|
|
|
|
|
|
commit.Push( _( "Distribute vertically" ) );
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-07-09 12:23:13 +00:00
|
|
|
|
2018-02-26 20:54:33 +00:00
|
|
|
void ALIGN_DISTRIBUTE_TOOL::doDistributeGapsVertically( ALIGNMENT_RECTS& itemsToDistribute,
|
|
|
|
const BOARD_ITEM* lastItem, int totalGap ) const
|
|
|
|
{
|
|
|
|
const auto itemGap = totalGap / ( itemsToDistribute.size() - 1 );
|
2018-02-08 14:47:24 +00:00
|
|
|
auto targetY = itemsToDistribute.begin()->second.GetY();
|
2014-07-09 12:23:13 +00:00
|
|
|
|
2018-02-08 14:47:24 +00:00
|
|
|
for( auto& i : itemsToDistribute )
|
2016-06-21 15:06:28 +00:00
|
|
|
{
|
2018-10-10 05:00:07 +00:00
|
|
|
BOARD_ITEM* item = i.first;
|
|
|
|
|
2018-01-31 21:29:16 +00:00
|
|
|
// cover the corner case where the last item is wider than the previous item and gap
|
2018-10-10 05:00:07 +00:00
|
|
|
if( lastItem == item )
|
2018-01-31 21:29:16 +00:00
|
|
|
continue;
|
2014-07-09 12:23:13 +00:00
|
|
|
|
2018-10-10 05:00:07 +00:00
|
|
|
// Don't move a pad by itself unless editing the footprint
|
2019-09-05 22:00:47 +00:00
|
|
|
if( item->Type() == PCB_PAD_T && m_frame->IsType( FRAME_PCB_EDITOR ) )
|
2018-10-10 05:00:07 +00:00
|
|
|
item = item->GetParent();
|
|
|
|
|
2018-01-31 21:29:16 +00:00
|
|
|
int difference = targetY - i.second.GetY();
|
|
|
|
i.first->Move( wxPoint( 0, difference ) );
|
|
|
|
targetY += ( i.second.GetHeight() + itemGap );
|
2014-07-09 12:23:13 +00:00
|
|
|
}
|
2018-02-26 20:54:33 +00:00
|
|
|
}
|
2014-07-09 12:23:13 +00:00
|
|
|
|
2016-06-21 15:06:28 +00:00
|
|
|
|
2018-02-26 20:54:33 +00:00
|
|
|
void ALIGN_DISTRIBUTE_TOOL::doDistributeCentersVertically( ALIGNMENT_RECTS& itemsToDistribute ) const
|
|
|
|
{
|
2018-12-14 12:22:00 +00:00
|
|
|
std::sort( itemsToDistribute.begin(), itemsToDistribute.end(),
|
|
|
|
[] ( const ALIGNMENT_RECT left, const ALIGNMENT_RECT right)
|
|
|
|
{ return ( left.second.GetCenter().y < right.second.GetCenter().y ); } );
|
2018-02-26 20:54:33 +00:00
|
|
|
const auto totalGap = ( itemsToDistribute.end()-1 )->second.GetCenter().y
|
|
|
|
- itemsToDistribute.begin()->second.GetCenter().y;
|
|
|
|
const auto itemGap = totalGap / ( itemsToDistribute.size() - 1 );
|
|
|
|
auto targetY = itemsToDistribute.begin()->second.GetCenter().y;
|
|
|
|
|
|
|
|
for( auto& i : itemsToDistribute )
|
|
|
|
{
|
2018-10-10 05:00:07 +00:00
|
|
|
BOARD_ITEM* item = i.first;
|
|
|
|
|
|
|
|
// Don't move a pad by itself unless editing the footprint
|
2019-09-05 22:00:47 +00:00
|
|
|
if( item->Type() == PCB_PAD_T && m_frame->IsType( FRAME_PCB_EDITOR ) )
|
2018-10-10 05:00:07 +00:00
|
|
|
item = item->GetParent();
|
|
|
|
|
2018-02-26 20:54:33 +00:00
|
|
|
int difference = targetY - i.second.GetCenter().y;
|
2018-10-10 05:00:07 +00:00
|
|
|
item->Move( wxPoint( 0, difference ) );
|
2018-02-26 20:54:33 +00:00
|
|
|
targetY += ( itemGap );
|
|
|
|
}
|
2014-07-09 12:23:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-07-31 12:30:51 +00:00
|
|
|
void ALIGN_DISTRIBUTE_TOOL::setTransitions()
|
2014-07-09 12:23:13 +00:00
|
|
|
{
|
2019-06-09 22:32:08 +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() );
|
|
|
|
Go( &ALIGN_DISTRIBUTE_TOOL::AlignCenterX, PCB_ACTIONS::alignCenterX.MakeEvent() );
|
|
|
|
Go( &ALIGN_DISTRIBUTE_TOOL::AlignCenterY, PCB_ACTIONS::alignCenterY.MakeEvent() );
|
|
|
|
|
|
|
|
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
|
|
|
}
|