From b5ef511063313f6659418a64b0ecfb776247406f Mon Sep 17 00:00:00 2001 From: Maciej Suminski Date: Tue, 30 Jun 2015 14:08:03 +0200 Subject: [PATCH] Fixed alignment to grid when there is a grid offset (GAL). --- pcbnew/dialogs/dialog_set_grid.cpp | 7 +++++++ pcbnew/tools/edit_tool.cpp | 2 +- pcbnew/tools/grid_helper.cpp | 29 +++++++++++++++-------------- pcbnew/tools/grid_helper.h | 24 ++++++++++++------------ pcbnew/tools/pcbnew_control.cpp | 22 ++++++++++++++++------ 5 files changed, 51 insertions(+), 33 deletions(-) diff --git a/pcbnew/dialogs/dialog_set_grid.cpp b/pcbnew/dialogs/dialog_set_grid.cpp index 3f2dfbaca6..78e68522e0 100644 --- a/pcbnew/dialogs/dialog_set_grid.cpp +++ b/pcbnew/dialogs/dialog_set_grid.cpp @@ -39,6 +39,7 @@ #include #include #include +#include // Max values for grid size #define MAX_GRID_SIZE ( 50.0 * IU_PER_MM ) @@ -282,9 +283,15 @@ bool PCB_BASE_FRAME::InvokeDialogGrid() TOOL_MANAGER* mgr = GetToolManager(); if( mgr && IsGalCanvasActive() ) + { mgr->RunAction( "common.Control.gridPreset", true, screen->GetGridCmdId() - ID_POPUP_GRID_LEVEL_1000 ); + TOOL_EVENT gridOriginUpdate = COMMON_ACTIONS::gridSetOrigin.MakeEvent(); + gridOriginUpdate.SetParameter( new VECTOR2D( grid_origin ) ); + mgr->ProcessEvent( gridOriginUpdate ); + } + m_canvas->Refresh(); return true; diff --git a/pcbnew/tools/edit_tool.cpp b/pcbnew/tools/edit_tool.cpp index 321d9bfd4e..5ed0fd3d17 100644 --- a/pcbnew/tools/edit_tool.cpp +++ b/pcbnew/tools/edit_tool.cpp @@ -228,7 +228,7 @@ int EDIT_TOOL::Main( const TOOL_EVENT& aEvent ) if( m_dragging ) { m_cursor = grid.BestSnapAnchor( evt->Position(), selection.Item( 0 ) ); - getViewControls()->ForceCursorPosition ( true, m_cursor ); + getViewControls()->ForceCursorPosition( true, m_cursor ); wxPoint movement = wxPoint( m_cursor.x, m_cursor.y ) - selection.Item( 0 )->GetPosition(); diff --git a/pcbnew/tools/grid_helper.cpp b/pcbnew/tools/grid_helper.cpp index d432122200..389ff27bdf 100644 --- a/pcbnew/tools/grid_helper.cpp +++ b/pcbnew/tools/grid_helper.cpp @@ -60,26 +60,27 @@ void GRID_HELPER::SetGrid( int aSize ) void GRID_HELPER::SetOrigin( const VECTOR2I& aOrigin ) { + assert( false ); } -VECTOR2I GRID_HELPER::GetGrid() +VECTOR2I GRID_HELPER::GetGrid() const { PCB_SCREEN* screen = m_frame->GetScreen(); const wxRealPoint& size = screen->GetGridSize(); - return VECTOR2I ( KiROUND( size.x ), KiROUND( size.y ) ); + return VECTOR2I( KiROUND( size.x ), KiROUND( size.y ) ); } -VECTOR2I GRID_HELPER::GetOrigin() +VECTOR2I GRID_HELPER::GetOrigin() const { - return VECTOR2I( 0, 0 ); + return VECTOR2I( m_frame->GetGridOrigin() ); } -void GRID_HELPER::SetAuxAxes( bool aEnable, const VECTOR2I aOrigin, bool aEnableDiagonal ) +void GRID_HELPER::SetAuxAxes( bool aEnable, const VECTOR2I& aOrigin, bool aEnableDiagonal ) { if( aEnable ) m_auxAxis = aOrigin; @@ -90,13 +91,13 @@ void GRID_HELPER::SetAuxAxes( bool aEnable, const VECTOR2I aOrigin, bool aEnable } -VECTOR2I GRID_HELPER::Align( const VECTOR2I& aPoint ) +VECTOR2I GRID_HELPER::Align( const VECTOR2I& aPoint ) const { - const VECTOR2D gridOffset( GetOrigin () ); + const VECTOR2D gridOffset( GetOrigin() ); const VECTOR2D gridSize( GetGrid() ); VECTOR2I nearest( round( ( aPoint.x - gridOffset.x ) / gridSize.x ) * gridSize.x + gridOffset.x, - round( ( aPoint.y - gridOffset.y ) / gridSize.y ) * gridSize.y + gridOffset.y ); + round( ( aPoint.y - gridOffset.y ) / gridSize.y ) * gridSize.y + gridOffset.y ); if( !m_auxAxis ) return nearest; @@ -152,7 +153,7 @@ VECTOR2I GRID_HELPER::BestDragOrigin( const VECTOR2I &aMousePos, BOARD_ITEM* aIt } -std::set GRID_HELPER::queryVisible( const BOX2I& aArea ) +std::set GRID_HELPER::queryVisible( const BOX2I& aArea ) const { std::set items; @@ -172,12 +173,12 @@ std::set GRID_HELPER::queryVisible( const BOX2I& aArea ) } -VECTOR2I GRID_HELPER::BestSnapAnchor( const VECTOR2I &aOrigin, BOARD_ITEM* aDraggedItem ) +VECTOR2I GRID_HELPER::BestSnapAnchor( const VECTOR2I& aOrigin, BOARD_ITEM* aDraggedItem ) { double worldScale = m_frame->GetGalCanvas()->GetGAL()->GetWorldScale(); int snapRange = (int) ( 100.0 / worldScale ); - BOX2I bb( VECTOR2I( aOrigin.x - snapRange / 2, aOrigin.y - snapRange/2 ), VECTOR2I( snapRange, snapRange ) ); + BOX2I bb( VECTOR2I( aOrigin.x - snapRange / 2, aOrigin.y - snapRange / 2 ), VECTOR2I( snapRange, snapRange ) ); clearAnchors(); @@ -197,9 +198,9 @@ VECTOR2I GRID_HELPER::BestSnapAnchor( const VECTOR2I &aOrigin, BOARD_ITEM* aDrag if( nearest && snapDist < gridDist ) return nearest->pos; - } + } - return nearestGrid; + return nearestGrid; } @@ -323,7 +324,7 @@ void GRID_HELPER::computeAnchors( BOARD_ITEM* aItem, const VECTOR2I& aRefPos ) } -GRID_HELPER::ANCHOR* GRID_HELPER::nearestAnchor( VECTOR2I aPos, int aFlags, LSET aMatchLayers ) +GRID_HELPER::ANCHOR* GRID_HELPER::nearestAnchor( const VECTOR2I& aPos, int aFlags, LSET aMatchLayers ) { double minDist = std::numeric_limits::max(); ANCHOR* best = NULL; diff --git a/pcbnew/tools/grid_helper.h b/pcbnew/tools/grid_helper.h index 59435b5d1a..f239b1149b 100644 --- a/pcbnew/tools/grid_helper.h +++ b/pcbnew/tools/grid_helper.h @@ -43,15 +43,15 @@ public: void SetGrid( int aSize ); void SetOrigin( const VECTOR2I& aOrigin ); - VECTOR2I GetGrid(); - VECTOR2I GetOrigin(); + VECTOR2I GetGrid() const; + VECTOR2I GetOrigin() const; - void SetAuxAxes( bool aEnable, const VECTOR2I aOrigin = VECTOR2I( 0, 0 ), bool aEnableDiagonal = false ); + void SetAuxAxes( bool aEnable, const VECTOR2I& aOrigin = VECTOR2I( 0, 0 ), bool aEnableDiagonal = false ); - VECTOR2I Align( const VECTOR2I& aPoint ); + VECTOR2I Align( const VECTOR2I& aPoint ) const; - VECTOR2I BestDragOrigin ( const VECTOR2I &aMousePos, BOARD_ITEM* aItem ); - VECTOR2I BestSnapAnchor ( const VECTOR2I &aOrigin, BOARD_ITEM* aDraggedItem ); + VECTOR2I BestDragOrigin( const VECTOR2I& aMousePos, BOARD_ITEM* aItem ); + VECTOR2I BestSnapAnchor( const VECTOR2I& aOrigin, BOARD_ITEM* aDraggedItem ); private: enum ANCHOR_FLAGS { @@ -70,28 +70,28 @@ private: int flags; BOARD_ITEM* item; - double Distance( const VECTOR2I& aP ) + double Distance( const VECTOR2I& aP ) const { return ( aP - pos ).EuclideanNorm(); } - bool CanSnapItem( const BOARD_ITEM* aItem ); + //bool CanSnapItem( const BOARD_ITEM* aItem ) const; }; std::vector m_anchors; - std::set queryVisible( const BOX2I& aArea ); + std::set queryVisible( const BOX2I& aArea ) const; - void addAnchor( VECTOR2I aPos, int aFlags = CORNER | SNAPPABLE, BOARD_ITEM* aItem = NULL ) + void addAnchor( const VECTOR2I& aPos, int aFlags = CORNER | SNAPPABLE, BOARD_ITEM* aItem = NULL ) { m_anchors.push_back( ANCHOR( aPos, aFlags, aItem ) ); } - ANCHOR* nearestAnchor( VECTOR2I aPos, int aFlags, LSET aMatchLayers ); + ANCHOR* nearestAnchor( const VECTOR2I& aPos, int aFlags, LSET aMatchLayers ); void computeAnchors( BOARD_ITEM* aItem, const VECTOR2I& aRefPos ); - void clearAnchors () + void clearAnchors() { m_anchors.clear(); } diff --git a/pcbnew/tools/pcbnew_control.cpp b/pcbnew/tools/pcbnew_control.cpp index a30a8642f0..d1249c1cca 100644 --- a/pcbnew/tools/pcbnew_control.cpp +++ b/pcbnew/tools/pcbnew_control.cpp @@ -472,13 +472,23 @@ static bool setOrigin( KIGFX::VIEW* aView, PCB_BASE_FRAME* aFrame, int PCBNEW_CONTROL::GridSetOrigin( const TOOL_EVENT& aEvent ) { - PICKER_TOOL* picker = m_toolMgr->GetTool(); - assert( picker ); + VECTOR2D* origin = aEvent.Parameter(); - // TODO it will not check the toolbar button in module editor, as it uses a different ID.. - m_frame->SetToolID( ID_PCB_PLACE_GRID_COORD_BUTT, wxCURSOR_PENCIL, _( "Adjust grid origin" ) ); - picker->SetClickHandler( boost::bind( setOrigin, getView(), m_frame, m_gridOrigin, _1 ) ); - picker->Activate(); + if( origin ) + { + setOrigin( getView(), m_frame, m_gridOrigin, *origin ); + delete origin; + } + else + { + PICKER_TOOL* picker = m_toolMgr->GetTool(); + assert( picker ); + + // TODO it will not check the toolbar button in module editor, as it uses a different ID.. + m_frame->SetToolID( ID_PCB_PLACE_GRID_COORD_BUTT, wxCURSOR_PENCIL, _( "Adjust grid origin" ) ); + picker->SetClickHandler( boost::bind( setOrigin, getView(), m_frame, m_gridOrigin, _1 ) ); + picker->Activate(); + } return 0; }