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 
36 class PCB_BASE_FRAME;
37 
38 class GRID_HELPER {
39 public:
40 
41  GRID_HELPER( PCB_BASE_FRAME* aFrame );
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  VECTOR2I BestSnapAnchor( const VECTOR2I& aOrigin, BOARD_ITEM* aDraggedItem );
64  VECTOR2I BestSnapAnchor( const VECTOR2I& aOrigin, const LSET& aLayers,
65  const std::vector<BOARD_ITEM*>& aSkip = {} );
66 
67  void SetSkipPoint( const VECTOR2I& aPoint )
68  {
69  m_skipPoint = aPoint;
70  }
71 
76  {
77  m_skipPoint = VECTOR2I( std::numeric_limits<int>::min(), std::numeric_limits<int>::min() );
78  }
79 
80  void SetSnap( bool aSnap )
81  {
82  m_enableSnap = aSnap;
83  }
84 
85  void SetSnapLine( bool aSnap )
86  {
87  m_enableSnapLine = aSnap;
88  }
89 
90  void SetUseGrid( bool aGrid = true )
91  {
92  m_enableGrid = aGrid;
93  }
94 
95 private:
96  enum ANCHOR_FLAGS {
97  CORNER = 0x1,
98  OUTLINE = 0x2,
99  SNAPPABLE = 0x4,
100  ORIGIN = 0x8
101  };
102 
103  struct ANCHOR
104  {
105  ANCHOR( VECTOR2I aPos, int aFlags = CORNER | SNAPPABLE, BOARD_ITEM* aItem = NULL ) :
106  pos( aPos ),
107  flags( aFlags ),
108  item( aItem )
109  { };
110 
111  VECTOR2I pos;
112  int flags;
114 
115  double Distance( const VECTOR2I& aP ) const
116  {
117  return ( aP - pos ).EuclideanNorm();
118  }
119  };
120 
121  std::vector<ANCHOR> m_anchors;
122 
123  std::set<BOARD_ITEM*> queryVisible( const BOX2I& aArea,
124  const std::vector<BOARD_ITEM*>& aSkip ) const;
125 
126  void addAnchor( const VECTOR2I& aPos, int aFlags, BOARD_ITEM* aItem )
127  {
128  m_anchors.emplace_back( ANCHOR( aPos, aFlags, aItem ) );
129  }
130 
131  ANCHOR* nearestAnchor( const VECTOR2I& aPos, int aFlags, LSET aMatchLayers );
132 
141  void computeAnchors( BOARD_ITEM* aItem, const VECTOR2I& aRefPos, bool aFrom = false );
142 
144  {
145  m_anchors.clear();
146  }
147 
150 
157 
161 };
162 
163 #endif
OPT< VECTOR2I > m_auxAxis
Definition: grid_helper.h:149
ANCHOR * m_snapItem
Pointer to the currently snapped item in m_anchors (NULL if not snapped)
Definition: grid_helper.h:155
BOARD_ITEM is a base class for any item which can be embedded within the BOARD container class,...
GRID_HELPER(PCB_BASE_FRAME *aFrame)
Definition: grid_helper.cpp:49
KIGFX::ORIGIN_VIEWITEM m_viewAxis
Definition: grid_helper.h:160
VECTOR2I GetOrigin() const
Definition: grid_helper.cpp:95
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:158
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:115
BOARD_ITEM * item
Definition: grid_helper.h:113
bool m_enableSnap
If true, allow snapping to other items on the layers.
Definition: grid_helper.h:151
ANCHOR(VECTOR2I aPos, int aFlags=CORNER|SNAPPABLE, BOARD_ITEM *aItem=NULL)
Definition: grid_helper.h:105
bool m_enableSnapLine
If true, allow drawing lines from snap points.
Definition: grid_helper.h:153
void SetSnap(bool aSnap)
Definition: grid_helper.h:80
Definition: seg.h:39
VECTOR2I Align(const VECTOR2I &aPoint) const
Board layer functions and definitions.
PCB_BASE_FRAME * m_frame
Definition: grid_helper.h:148
int m_snapSize
Sets the radius in screen units for snapping to items.
Definition: grid_helper.h:154
VECTOR2I m_skipPoint
When drawing a line, we avoid snapping to the source point.
Definition: grid_helper.h:156
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:85
void addAnchor(const VECTOR2I &aPos, int aFlags, BOARD_ITEM *aItem)
Definition: grid_helper.h:126
bool m_enableGrid
If true, allow snapping to grid.
Definition: grid_helper.h:152
void SetUseGrid(bool aGrid=true)
Definition: grid_helper.h:90
void ClearSkipPoint()
We clear the skip point by setting it to an unreachable position, thereby preventing matching.
Definition: grid_helper.h:75
KIGFX::ORIGIN_VIEWITEM m_viewSnapLine
Definition: grid_helper.h:159
std::vector< ANCHOR > m_anchors
Definition: grid_helper.h:121
void SetSkipPoint(const VECTOR2I &aPoint)
Definition: grid_helper.h:67
PCB_BASE_FRAME basic PCB main window class for Pcbnew, Gerbview, and CvPcb footprint viewer.
void clearAnchors()
Definition: grid_helper.h:143