KiCad PCB EDA Suite
grid_helper.h
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) 2014 CERN
5  * Copyright (C) 2020 KiCad Developers, see AUTHORS.txt for contributors.
6  * @author Tomasz Wlostowski <tomasz.wlostowski@cern.ch>
7  *
8  * This program is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU General Public License
10  * as published by the Free Software Foundation; either version 2
11  * of the License, or (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, you may find one here:
20  * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
21  * or you may search the http://www.gnu.org website for the version 2 license,
22  * or you may write to the Free Software Foundation, Inc.,
23  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
24  */
25 
26 #ifndef __GRID_HELPER_H
27 #define __GRID_HELPER_H
28 
29 #include <vector>
30 #include <math/vector2d.h>
31 #include <core/optional.h>
32 #include <origin_viewitem.h>
34 #include <geometry/seg.h>
35 #include <geometry/shape_arc.h>
36 
37 
38 class GRID_HELPER {
39 public:
40 
41  GRID_HELPER( TOOL_MANAGER* aToolMgr, MAGNETIC_SETTINGS* aMagneticSettings );
42  ~GRID_HELPER();
43 
44  VECTOR2I GetGrid() const;
45  VECTOR2I GetOrigin() const;
46 
54  BOARD_ITEM* GetSnapped() const;
55 
56  void SetAuxAxes( bool aEnable, const VECTOR2I& aOrigin = VECTOR2I( 0, 0 ) );
57 
58  VECTOR2I Align( const VECTOR2I& aPoint ) const;
59 
60  VECTOR2I AlignToSegment ( const VECTOR2I& aPoint, const SEG& aSeg );
61 
62  VECTOR2I BestDragOrigin( const VECTOR2I& aMousePos, std::vector<BOARD_ITEM*>& aItem );
63 
64  VECTOR2I AlignToArc ( const VECTOR2I& aPoint, const SHAPE_ARC& aSeg );
65 
66  VECTOR2I BestSnapAnchor( const VECTOR2I& aOrigin, BOARD_ITEM* aDraggedItem );
67  VECTOR2I BestSnapAnchor( const VECTOR2I& aOrigin, const LSET& aLayers,
68  const std::vector<BOARD_ITEM*>& aSkip = {} );
69 
70  void SetSkipPoint( const VECTOR2I& aPoint )
71  {
72  m_skipPoint = aPoint;
73  }
74 
79  {
80  m_skipPoint = VECTOR2I( std::numeric_limits<int>::min(), std::numeric_limits<int>::min() );
81  }
82 
83  void SetSnap( bool aSnap )
84  {
85  m_enableSnap = aSnap;
86  }
87 
88  void SetSnapLine( bool aSnap )
89  {
90  m_enableSnapLine = aSnap;
91  }
92 
93  void SetUseGrid( bool aGrid = true )
94  {
95  m_enableGrid = aGrid;
96  }
97 
98 private:
99  enum ANCHOR_FLAGS {
100  CORNER = 0x1,
101  OUTLINE = 0x2,
102  SNAPPABLE = 0x4,
103  ORIGIN = 0x8
104  };
105 
106  struct ANCHOR
107  {
108  ANCHOR( VECTOR2I aPos, int aFlags = CORNER | SNAPPABLE, BOARD_ITEM* aItem = NULL ) :
109  pos( aPos ),
110  flags( aFlags ),
111  item( aItem )
112  { };
113 
114  VECTOR2I pos;
115  int flags;
117 
118  double Distance( const VECTOR2I& aP ) const
119  {
120  return ( aP - pos ).EuclideanNorm();
121  }
122  };
123 
124  std::vector<ANCHOR> m_anchors;
125 
126  std::set<BOARD_ITEM*> queryVisible( const BOX2I& aArea,
127  const std::vector<BOARD_ITEM*>& aSkip ) const;
128 
129  void addAnchor( const VECTOR2I& aPos, int aFlags, BOARD_ITEM* aItem )
130  {
131  m_anchors.emplace_back( ANCHOR( aPos, aFlags, aItem ) );
132  }
133 
134  ANCHOR* nearestAnchor( const VECTOR2I& aPos, int aFlags, LSET aMatchLayers );
135 
144  void computeAnchors( BOARD_ITEM* aItem, const VECTOR2I& aRefPos, bool aFrom = false );
145 
147  {
148  m_anchors.clear();
149  }
150 
153 
154  bool m_enableSnap; // If true, allow snapping to other items on the layers
155  bool m_enableGrid; // If true, allow snapping to grid
156  bool m_enableSnapLine; // If true, allow drawing lines from snap points
157  int m_snapSize; // Sets the radius in screen units for snapping to items
158  ANCHOR* m_snapItem; // Pointer to the currently snapped item in m_anchors
159  // (NULL if not snapped)
160  VECTOR2I m_skipPoint; // When drawing a line, we avoid snapping to the source point
161 
163 
167 };
168 
169 #endif
OPT< VECTOR2I > m_auxAxis
Definition: grid_helper.h:152
ANCHOR * m_snapItem
Definition: grid_helper.h:158
BOARD_ITEM is a base class for any item which can be embedded within the BOARD container class,...
TOOL_MANAGER * m_toolMgr
Definition: grid_helper.h:151
KIGFX::ORIGIN_VIEWITEM m_viewAxis
Definition: grid_helper.h:166
VECTOR2I GetOrigin() const
Definition: grid_helper.cpp:93
VECTOR2I AlignToSegment(const VECTOR2I &aPoint, const SEG &aSeg)
VECTOR2I BestSnapAnchor(const VECTOR2I &aOrigin, BOARD_ITEM *aDraggedItem)
VECTOR2< int > VECTOR2I
Definition: vector2d.h:594
VECTOR2I GetGrid() const
Definition: grid_helper.cpp:85
BOARD_ITEM * GetSnapped() const
Function GetSnapped If the GRID_HELPER has highlighted a snap point (target shown),...
KIGFX::ORIGIN_VIEWITEM m_viewSnapPoint
Definition: grid_helper.h:164
TOOL_MANAGER.
Definition: tool_manager.h:51
void SetAuxAxes(bool aEnable, const VECTOR2I &aOrigin=VECTOR2I(0, 0))
void computeAnchors(BOARD_ITEM *aItem, const VECTOR2I &aRefPos, bool aFrom=false)
computeAnchors inserts the local anchor points in to the grid helper for the specified board item,...
VECTOR2I BestDragOrigin(const VECTOR2I &aMousePos, std::vector< BOARD_ITEM * > &aItem)
LSET is a set of PCB_LAYER_IDs.
ANCHOR * nearestAnchor(const VECTOR2I &aPos, int aFlags, LSET aMatchLayers)
#define NULL
double Distance(const VECTOR2I &aP) const
Definition: grid_helper.h:118
GRID_HELPER(TOOL_MANAGER *aToolMgr, MAGNETIC_SETTINGS *aMagneticSettings)
Definition: grid_helper.cpp:48
BOARD_ITEM * item
Definition: grid_helper.h:116
bool m_enableSnap
Definition: grid_helper.h:154
ANCHOR(VECTOR2I aPos, int aFlags=CORNER|SNAPPABLE, BOARD_ITEM *aItem=NULL)
Definition: grid_helper.h:108
VECTOR2I AlignToArc(const VECTOR2I &aPoint, const SHAPE_ARC &aSeg)
bool m_enableSnapLine
Definition: grid_helper.h:156
void SetSnap(bool aSnap)
Definition: grid_helper.h:83
Definition: seg.h:39
VECTOR2I Align(const VECTOR2I &aPoint) const
Board layer functions and definitions.
VECTOR2I m_skipPoint
Definition: grid_helper.h:160
std::set< BOARD_ITEM * > queryVisible(const BOX2I &aArea, const std::vector< BOARD_ITEM * > &aSkip) const
boost::optional< T > OPT
Definition: optional.h:7
void SetSnapLine(bool aSnap)
Definition: grid_helper.h:88
void addAnchor(const VECTOR2I &aPos, int aFlags, BOARD_ITEM *aItem)
Definition: grid_helper.h:129
MAGNETIC_SETTINGS * m_magneticSettings
Definition: grid_helper.h:162
bool m_enableGrid
Definition: grid_helper.h:155
void SetUseGrid(bool aGrid=true)
Definition: grid_helper.h:93
void ClearSkipPoint()
We clear the skip point by setting it to an unreachable position, thereby preventing matching.
Definition: grid_helper.h:78
KIGFX::ORIGIN_VIEWITEM m_viewSnapLine
Definition: grid_helper.h:165
std::vector< ANCHOR > m_anchors
Definition: grid_helper.h:124
void SetSkipPoint(const VECTOR2I &aPoint)
Definition: grid_helper.h:70
void clearAnchors()
Definition: grid_helper.h:146