diff --git a/pcbnew/CMakeLists.txt b/pcbnew/CMakeLists.txt index 2884741b8a..bab69a7b37 100644 --- a/pcbnew/CMakeLists.txt +++ b/pcbnew/CMakeLists.txt @@ -265,6 +265,7 @@ set( PCBNEW_CLASS_SRCS tools/pcbnew_control.cpp tools/pcb_editor_control.cpp tools/pcb_tools.cpp + tools/placement_tool.cpp tools/common_actions.cpp ) diff --git a/pcbnew/moduleframe.cpp b/pcbnew/moduleframe.cpp index 53c4e89fc6..603e4e0838 100644 --- a/pcbnew/moduleframe.cpp +++ b/pcbnew/moduleframe.cpp @@ -61,6 +61,7 @@ #include "tools/drawing_tool.h" #include "tools/point_editor.h" #include "tools/pcbnew_control.h" +#include "tools/placement_tool.h" #include "tools/common_actions.h" @@ -278,6 +279,7 @@ FOOTPRINT_EDIT_FRAME::FOOTPRINT_EDIT_FRAME( KIWAY* aKiway, wxWindow* aParent ) : m_toolManager->RegisterTool( new DRAWING_TOOL ); m_toolManager->RegisterTool( new POINT_EDITOR ); m_toolManager->RegisterTool( new PCBNEW_CONTROL ); + m_toolManager->RegisterTool( new PLACEMENT_TOOL ); m_toolManager->GetTool()->EditModules( true ); m_toolManager->GetTool()->EditModules( true ); diff --git a/pcbnew/tools/common_actions.cpp b/pcbnew/tools/common_actions.cpp index fec32b59cf..a1b6966a06 100644 --- a/pcbnew/tools/common_actions.cpp +++ b/pcbnew/tools/common_actions.cpp @@ -285,6 +285,38 @@ TOOL_ACTION COMMON_ACTIONS::pointEditorUpdate( "pcbnew.PointEditor.update", AS_GLOBAL, 0, "", "" ); // No description, it is not supposed to be shown anywhere +// Placement tool +TOOL_ACTION COMMON_ACTIONS::alignTop( "pcbnew.Place.alignTop", + AS_GLOBAL, 0, + "Align items to the top", + "Aligns selected items to the top edge" ); + +TOOL_ACTION COMMON_ACTIONS::alignBottom( "pcbnew.Place.alignBottom", + AS_GLOBAL, 0, + "Align items to the bottom", + "Aligns selected items to the bottom edge" ); + +TOOL_ACTION COMMON_ACTIONS::alignLeft( "pcbnew.Place.alignLeft", + AS_GLOBAL, 0, + "Align items to the left", + "Aligns selected items to the top left" ); + +TOOL_ACTION COMMON_ACTIONS::alignRight( "pcbnew.Place.alignRight", + AS_GLOBAL, 0, + "Align items to the right", + "Aligns selected items to the right edge" ); + +TOOL_ACTION COMMON_ACTIONS::distributeHorizontally( "pcbnew.Place.distributeHorizontally", + AS_GLOBAL, 0, + "Distribute horizontally", + "Distributes selected items along the horizontal axis" ); + +TOOL_ACTION COMMON_ACTIONS::distributeVertically( "pcbnew.Place.distributeVertically", + AS_GLOBAL, 0, + "Distribure vertically", + "Distributes selected items along the vertical axis" ); + + boost::optional COMMON_ACTIONS::TranslateLegacyId( int aId ) { switch( aId ) diff --git a/pcbnew/tools/common_actions.h b/pcbnew/tools/common_actions.h index c61f6e811d..2cd367c05d 100644 --- a/pcbnew/tools/common_actions.h +++ b/pcbnew/tools/common_actions.h @@ -110,6 +110,25 @@ public: /// Update edit points static TOOL_ACTION pointEditorUpdate; + // Placement tool + /// Align items to the top edge of selection bounding box + static TOOL_ACTION alignTop; + + /// Align items to the bottom edge of selection bounding box + static TOOL_ACTION alignBottom; + + /// Align items to the left edge of selection bounding box + static TOOL_ACTION alignLeft; + + /// Align items to the right edge of selection bounding box + static TOOL_ACTION alignRight; + + /// Distributes items evenly along the horizontal axis + static TOOL_ACTION distributeHorizontally; + + /// Distributes items evenly along the vertical axis + static TOOL_ACTION distributeVertically; + // View controls static TOOL_ACTION zoomIn; static TOOL_ACTION zoomOut; diff --git a/pcbnew/tools/pcb_tools.cpp b/pcbnew/tools/pcb_tools.cpp index 6ba1a787e8..9e604a0b04 100644 --- a/pcbnew/tools/pcb_tools.cpp +++ b/pcbnew/tools/pcb_tools.cpp @@ -36,6 +36,7 @@ #include "point_editor.h" #include "pcbnew_control.h" #include "pcb_editor_control.h" +#include "placement_tool.h" #include "common_actions.h" #include @@ -55,6 +56,7 @@ void PCB_EDIT_FRAME::setupTools() m_toolManager->RegisterTool( new POINT_EDITOR ); m_toolManager->RegisterTool( new PCBNEW_CONTROL ); m_toolManager->RegisterTool( new PCB_EDITOR_CONTROL ); + m_toolManager->RegisterTool( new PLACEMENT_TOOL ); m_toolManager->ResetTools( TOOL_BASE::RUN ); // Run the selection tool, it is supposed to be always active diff --git a/pcbnew/tools/placement_tool.cpp b/pcbnew/tools/placement_tool.cpp new file mode 100644 index 0000000000..05ac2522ba --- /dev/null +++ b/pcbnew/tools/placement_tool.cpp @@ -0,0 +1,356 @@ +/* + * This program source code file is part of KiCad, a free EDA CAD application. + * + * Copyright (C) 2014 CERN + * @author Maciej Suminski + * + * 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 +#include +#include + +#include +#include + +PLACEMENT_TOOL::PLACEMENT_TOOL() : + TOOL_INTERACTIVE( "pcbnew.Placement" ) +{ +} + +PLACEMENT_TOOL::~PLACEMENT_TOOL() +{ +} + + +bool PLACEMENT_TOOL::Init() +{ + // Find the selection tool, so they can cooperate + m_selectionTool = static_cast( m_toolMgr->FindTool( "pcbnew.InteractiveSelection" ) ); + + if( !m_selectionTool ) + { + DisplayError( NULL, wxT( "pcbnew.InteractiveSelection tool is not available" ) ); + return false; + } + + // TODO create a context menu and add it to the selection tool + + setTransitions(); + + return true; +} + + +int PLACEMENT_TOOL::AlignTop( TOOL_EVENT& aEvent ) +{ + const SELECTION_TOOL::SELECTION& selection = m_selectionTool->GetSelection(); + + if( selection.Size() > 1 ) + { + PCB_BASE_FRAME* editFrame = getEditFrame(); + RN_DATA* ratsnest = getModel()->GetRatsnest(); + + editFrame->OnModify(); + editFrame->SaveCopyInUndoList( selection.items, UR_CHANGED ); + + // Compute the highest point of selection - it will be the edge of alignment + int top = selection.Item( 0 )->GetBoundingBox().GetY(); + + for( int i = 1; i < selection.Size(); ++i ) + { + int currentTop = selection.Item( i )->GetBoundingBox().GetY(); + + if( top > currentTop ) // Y decreases when going up + top = currentTop; + } + + // Move the selected items + for( int i = 0; i < selection.Size(); ++i ) + { + BOARD_ITEM* item = selection.Item( i ); + int difference = top - item->GetBoundingBox().GetY(); + + item->Move( wxPoint( 0, difference ) ); + item->ViewUpdate(); + ratsnest->Update( item ); + } + + getModel()->GetRatsnest()->Recalculate(); + } + + setTransitions(); + + return 0; +} + + +int PLACEMENT_TOOL::AlignBottom( TOOL_EVENT& aEvent ) +{ + const SELECTION_TOOL::SELECTION& selection = m_selectionTool->GetSelection(); + + if( selection.Size() > 1 ) + { + PCB_BASE_FRAME* editFrame = getEditFrame(); + RN_DATA* ratsnest = getModel()->GetRatsnest(); + + editFrame->OnModify(); + editFrame->SaveCopyInUndoList( selection.items, UR_CHANGED ); + + // Compute the lowest point of selection - it will be the edge of alignment + int bottom = selection.Item( 0 )->GetBoundingBox().GetBottom(); + + for( int i = 1; i < selection.Size(); ++i ) + { + int currentBottom = selection.Item( i )->GetBoundingBox().GetBottom(); + + if( bottom < currentBottom ) // Y increases when going down + bottom = currentBottom; + } + + // Move the selected items + for( int i = 0; i < selection.Size(); ++i ) + { + BOARD_ITEM* item = selection.Item( i ); + int difference = bottom - item->GetBoundingBox().GetBottom(); + + item->Move( wxPoint( 0, difference ) ); + item->ViewUpdate(); + ratsnest->Update( item ); + } + + getModel()->GetRatsnest()->Recalculate(); + } + + setTransitions(); + + return 0; +} + + +int PLACEMENT_TOOL::AlignLeft( TOOL_EVENT& aEvent ) +{ + const SELECTION_TOOL::SELECTION& selection = m_selectionTool->GetSelection(); + + if( selection.Size() > 1 ) + { + PCB_BASE_FRAME* editFrame = getEditFrame(); + RN_DATA* ratsnest = getModel()->GetRatsnest(); + + editFrame->OnModify(); + editFrame->SaveCopyInUndoList( selection.items, UR_CHANGED ); + + // Compute the leftmost point of selection - it will be the edge of alignment + int left = selection.Item( 0 )->GetBoundingBox().GetX(); + + for( int i = 1; i < selection.Size(); ++i ) + { + int currentLeft = selection.Item( i )->GetBoundingBox().GetX(); + + if( left > currentLeft ) // X decreases when going left + left = currentLeft; + } + + // Move the selected items + for( int i = 0; i < selection.Size(); ++i ) + { + BOARD_ITEM* item = selection.Item( i ); + int difference = left - item->GetBoundingBox().GetX(); + + item->Move( wxPoint( difference, 0 ) ); + item->ViewUpdate(); + ratsnest->Update( item ); + } + + getModel()->GetRatsnest()->Recalculate(); + } + + setTransitions(); + + return 0; +} + + +int PLACEMENT_TOOL::AlignRight( TOOL_EVENT& aEvent ) +{ + const SELECTION_TOOL::SELECTION& selection = m_selectionTool->GetSelection(); + + if( selection.Size() > 1 ) + { + PCB_BASE_FRAME* editFrame = getEditFrame(); + RN_DATA* ratsnest = getModel()->GetRatsnest(); + + editFrame->OnModify(); + editFrame->SaveCopyInUndoList( selection.items, UR_CHANGED ); + + // Compute the rightmost point of selection - it will be the edge of alignment + int right = selection.Item( 0 )->GetBoundingBox().GetRight(); + + for( int i = 1; i < selection.Size(); ++i ) + { + int currentRight = selection.Item( i )->GetBoundingBox().GetRight(); + + if( right < currentRight ) // X increases when going right + right = currentRight; + } + + // Move the selected items + for( int i = 0; i < selection.Size(); ++i ) + { + BOARD_ITEM* item = selection.Item( i ); + int difference = right - item->GetBoundingBox().GetRight(); + + item->Move( wxPoint( difference, 0 ) ); + item->ViewUpdate(); + ratsnest->Update( item ); + } + + getModel()->GetRatsnest()->Recalculate(); + } + + setTransitions(); + + 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( TOOL_EVENT& aEvent ) +{ + const SELECTION_TOOL::SELECTION& selection = m_selectionTool->GetSelection(); + + if( selection.Size() > 1 ) + { + PCB_BASE_FRAME* editFrame = getEditFrame(); + RN_DATA* ratsnest = getModel()->GetRatsnest(); + + editFrame->OnModify(); + editFrame->SaveCopyInUndoList( selection.items, UR_CHANGED ); + + // Prepare a list, so the items can be sorted by their X coordinate + std::list itemsList; + for( int i = 0; i < selection.Size(); ++i ) + itemsList.push_back( selection.Item( i ) ); + + // Sort items by X coordinate + itemsList.sort( compareX ); + + // Expected X coordinate for the next item (=minX) + int position = (*itemsList.begin())->GetBoundingBox().Centre().x; + + // X coordinate for the last item + const int maxX = (*itemsList.rbegin())->GetBoundingBox().Centre().x; + + // Distance between items + const int distance = ( maxX - position ) / ( itemsList.size() - 1 ); + + BOOST_FOREACH( BOARD_ITEM* item, itemsList ) + { + int difference = position - item->GetBoundingBox().Centre().x; + + item->Move( wxPoint( difference, 0 ) ); + item->ViewUpdate(); + ratsnest->Update( item ); + + position += distance; + } + + getModel()->GetRatsnest()->Recalculate(); + } + + setTransitions(); + + return 0; +} + + +int PLACEMENT_TOOL::DistributeVertically( TOOL_EVENT& aEvent ) +{ + const SELECTION_TOOL::SELECTION& selection = m_selectionTool->GetSelection(); + + if( selection.Size() > 1 ) + { + PCB_BASE_FRAME* editFrame = getEditFrame(); + RN_DATA* ratsnest = getModel()->GetRatsnest(); + + editFrame->OnModify(); + editFrame->SaveCopyInUndoList( selection.items, UR_CHANGED ); + + // Prepare a list, so the items can be sorted by their Y coordinate + std::list itemsList; + for( int i = 0; i < selection.Size(); ++i ) + itemsList.push_back( selection.Item( i ) ); + + // Sort items by Y coordinate + itemsList.sort( compareY ); + + // Expected Y coordinate for the next item (=minY) + int position = (*itemsList.begin())->GetBoundingBox().Centre().y; + + // Y coordinate for the last item + const int maxY = (*itemsList.rbegin())->GetBoundingBox().Centre().y; + + // Distance between items + const int distance = ( maxY - position ) / ( itemsList.size() - 1 ); + + BOOST_FOREACH( BOARD_ITEM* item, itemsList ) + { + int difference = position - item->GetBoundingBox().Centre().y; + + item->Move( wxPoint( 0, difference ) ); + item->ViewUpdate(); + ratsnest->Update( item ); + + position += distance; + } + + getModel()->GetRatsnest()->Recalculate(); + } + + setTransitions(); + + return 0; +} + + +void PLACEMENT_TOOL::setTransitions() +{ + 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() ); +} diff --git a/pcbnew/tools/placement_tool.h b/pcbnew/tools/placement_tool.h new file mode 100644 index 0000000000..09f6983ac2 --- /dev/null +++ b/pcbnew/tools/placement_tool.h @@ -0,0 +1,73 @@ +/* + * This program source code file is part of KiCad, a free EDA CAD application. + * + * Copyright (C) 2014 CERN + * @author Maciej Suminski + * + * 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 + */ + +#ifndef PLACEMENT_TOOL_H_ +#define PLACEMENT_TOOL_H_ + +#include + +class SELECTION_TOOL; + +/** + * TODO description + */ + +class PLACEMENT_TOOL : public TOOL_INTERACTIVE +{ +public: + PLACEMENT_TOOL(); + virtual ~PLACEMENT_TOOL(); + + /// @copydoc TOOL_INTERACTIVE::Reset() + void Reset( RESET_REASON aReason ) {}; + + /// @copydoc TOOL_INTERACTIVE::Init() + bool Init(); + + /// TODO + int AlignTop( TOOL_EVENT& aEvent ); + + /// TODO + int AlignBottom( TOOL_EVENT& aEvent ); + + /// TODO + int AlignLeft( TOOL_EVENT& aEvent ); + + /// TODO + int AlignRight( TOOL_EVENT& aEvent ); + + /// TODO + int DistributeHorizontally( TOOL_EVENT& aEvent ); + + /// TODO + int DistributeVertically( TOOL_EVENT& aEvent ); + +private: + /// TODO + void setTransitions(); + + SELECTION_TOOL* m_selectionTool; +}; + +#endif /* PLACEMENT_TOOL_H_ */