KiCad PCB EDA Suite
grid_helper.cpp
Go to the documentation of this file.
1/*
2 * This program source code file is part of KiCad, a free EDA CAD application.
3 *
4 * Copyright (C) 2018-2021 KiCad Developers, see AUTHORS.txt for contributors.
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version 2
9 * of the License, or (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, you may find one here:
18 * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
19 * or you may search the http://www.gnu.org website for the version 2 license,
20 * or you may write to the Free Software Foundation, Inc.,
21 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
22 */
23
24
25#include <functional>
26using namespace std::placeholders;
27
28#include <math/util.h> // for KiROUND
29#include <math/vector2d.h>
30#include <tool/tool_manager.h>
31#include <view/view.h>
32#include <tool/grid_helper.h>
33
34
36 m_toolMgr( aToolMgr )
37{
39 m_enableSnap = true;
40 m_enableSnapLine = true;
41 m_enableGrid = true;
42 m_snapItem = nullptr;
43}
44
45
47{
48}
49
50
52{
54
55 return VECTOR2I( KiROUND( size.x ), KiROUND( size.y ) );
56}
57
58
60{
62
63 return VECTOR2I( origin );
64}
65
66
67void GRID_HELPER::SetAuxAxes( bool aEnable, const VECTOR2I& aOrigin )
68{
69 if( aEnable )
70 {
71 m_auxAxis = aOrigin;
72 m_viewAxis.SetPosition( wxPoint( aOrigin ) );
74 }
75 else
76 {
77 m_auxAxis = std::optional<VECTOR2I>();
79 }
80}
81
82
84{
85 const VECTOR2D gridOffset( GetOrigin() );
86 const VECTOR2D grid( GetGrid() );
87
88 VECTOR2I nearest( KiROUND( ( aPoint.x - gridOffset.x ) / grid.x ) * grid.x + gridOffset.x,
89 KiROUND( ( aPoint.y - gridOffset.y ) / grid.y ) * grid.y + gridOffset.y );
90
91 return nearest;
92}
93
94
95VECTOR2I GRID_HELPER::Align( const VECTOR2I& aPoint ) const
96{
97 if( !canUseGrid() )
98 return aPoint;
99
100 VECTOR2I nearest = AlignGrid( aPoint );
101
102 if( !m_auxAxis )
103 return nearest;
104
105 if( std::abs( m_auxAxis->x - aPoint.x ) < std::abs( nearest.x - aPoint.x ) )
106 nearest.x = m_auxAxis->x;
107
108 if( std::abs( m_auxAxis->y - aPoint.y ) < std::abs( nearest.y - aPoint.y ) )
109 nearest.y = m_auxAxis->y;
110
111 return nearest;
112}
113
114
std::optional< VECTOR2I > m_auxAxis
Definition: grid_helper.h:130
GRID_HELPER(TOOL_MANAGER *aToolMgr)
Definition: grid_helper.cpp:35
bool m_enableGrid
Definition: grid_helper.h:135
virtual VECTOR2I Align(const VECTOR2I &aPoint) const
Definition: grid_helper.cpp:95
virtual ~GRID_HELPER()
Definition: grid_helper.cpp:46
TOOL_MANAGER * m_toolMgr
Definition: grid_helper.h:129
void SetAuxAxes(bool aEnable, const VECTOR2I &aOrigin=VECTOR2I(0, 0))
Definition: grid_helper.cpp:67
VECTOR2I GetGrid() const
Definition: grid_helper.cpp:51
ANCHOR * m_snapItem
Definition: grid_helper.h:137
bool m_enableSnapLine
Definition: grid_helper.h:136
bool m_enableSnap
Definition: grid_helper.h:134
VECTOR2I AlignGrid(const VECTOR2I &aPoint) const
Definition: grid_helper.cpp:83
VECTOR2I GetOrigin() const
Definition: grid_helper.cpp:59
bool canUseGrid() const
Check whether it is possible to use the grid – this depends both on local grid helper settings and gl...
Definition: grid_helper.h:121
KIGFX::ORIGIN_VIEWITEM m_viewAxis
Definition: grid_helper.h:143
const VECTOR2D & GetGridOrigin() const
const VECTOR2D & GetGridSize() const
Return the grid size.
void SetPosition(const VECTOR2I &aPosition) override
GAL * GetGAL() const
Return the #GAL this view is using to draw graphical primitives.
Definition: view.h:195
void SetVisible(VIEW_ITEM *aItem, bool aIsVisible=true)
Set the item visibility.
Definition: view.cpp:1512
Master controller class:
Definition: tool_manager.h:55
KIGFX::VIEW * GetView() const
Definition: tool_manager.h:285
EDA_ANGLE abs(const EDA_ANGLE &aAngle)
Definition: eda_angle.h:401
constexpr ret_type KiROUND(fp_type v)
Round a floating point number to an integer using "round halfway cases away from zero".
Definition: util.h:85
VECTOR2< int > VECTOR2I
Definition: vector2d.h:618