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  * @author Tomasz Wlostowski <tomasz.wlostowski@cern.ch>
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * as published by the Free Software Foundation; either version 2
10  * of the License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, you may find one here:
19  * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
20  * or you may search the http://www.gnu.org website for the version 2 license,
21  * or you may write to the Free Software Foundation, Inc.,
22  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
23  */
24 
25 #ifndef __GRID_HELPER_H
26 #define __GRID_HELPER_H
27 
28 #include <vector>
29 
30 #include <math/vector2d.h>
31 #include <core/optional.h>
32 #include <origin_viewitem.h>
33 
35 
36 #include <geometry/seg.h>
37 
38 class PCB_BASE_FRAME;
39 
40 class GRID_HELPER {
41 public:
42 
43  GRID_HELPER( PCB_BASE_FRAME* aFrame );
44  ~GRID_HELPER();
45 
46  void SetGrid( int aSize );
47  void SetOrigin( const VECTOR2I& aOrigin );
48 
49  VECTOR2I GetGrid() const;
50  VECTOR2I GetOrigin() const;
51 
59  BOARD_ITEM* GetSnapped() const;
60 
61  void SetAuxAxes( bool aEnable, const VECTOR2I& aOrigin = VECTOR2I( 0, 0 ), bool aEnableDiagonal = false );
62 
63  VECTOR2I Align( const VECTOR2I& aPoint ) const;
64 
65  VECTOR2I AlignToSegment ( const VECTOR2I& aPoint, const SEG& aSeg );
66 
67  VECTOR2I BestDragOrigin( const VECTOR2I& aMousePos, BOARD_ITEM* aItem );
68  VECTOR2I BestSnapAnchor( const VECTOR2I& aOrigin, BOARD_ITEM* aDraggedItem );
69  VECTOR2I BestSnapAnchor( const VECTOR2I& aOrigin, const LSET& aLayers,
70  const std::vector<BOARD_ITEM*> aSkip = {} );
71 
72  void SetSnap( bool aSnap )
73  {
74  m_enableSnap = aSnap;
75  }
76 
77  void SetUseGrid( bool aGrid = true )
78  {
79  m_enableGrid = aGrid;
80  }
81 
82 private:
83  enum ANCHOR_FLAGS {
84  CORNER = 0x1,
85  OUTLINE = 0x2,
86  SNAPPABLE = 0x4,
87  ORIGIN = 0x8
88  };
89 
90  struct ANCHOR
91  {
92  ANCHOR( VECTOR2I aPos, int aFlags = CORNER | SNAPPABLE, BOARD_ITEM* aItem = NULL ):
93  pos( aPos ), flags( aFlags ), item( aItem ) {} ;
94 
95  VECTOR2I pos;
96  int flags;
98 
99  double Distance( const VECTOR2I& aP ) const
100  {
101  return ( aP - pos ).EuclideanNorm();
102  }
103  };
104 
105  std::vector<ANCHOR> m_anchors;
106 
107  std::set<BOARD_ITEM*> queryVisible( const BOX2I& aArea,
108  const std::vector<BOARD_ITEM*> aSkip ) const;
109 
110  void addAnchor( const VECTOR2I& aPos, int aFlags = CORNER | SNAPPABLE, BOARD_ITEM* aItem = NULL )
111  {
112  m_anchors.push_back( ANCHOR( aPos, aFlags, aItem ) );
113  }
114 
115  ANCHOR* nearestAnchor( const VECTOR2I& aPos, int aFlags, LSET aMatchLayers );
116 
117  void computeAnchors( BOARD_ITEM* aItem, const VECTOR2I& aRefPos );
118 
120  {
121  m_anchors.clear();
122  }
123 
126 
132 
135 };
136 
137 #endif
VECTOR2I BestDragOrigin(const VECTOR2I &aMousePos, BOARD_ITEM *aItem)
OPT< VECTOR2I > m_auxAxis
Definition: grid_helper.h:125
ANCHOR * m_snapItem
Pointer to the currently snapped item in m_anchors (NULL if not snapped)
Definition: grid_helper.h:131
Class BOARD_ITEM is a base class for any item which can be embedded within the BOARD container class...
void SetGrid(int aSize)
Definition: grid_helper.cpp:76
VECTOR2I Align(const VECTOR2I &aPoint) const
GRID_HELPER(PCB_BASE_FRAME *aFrame)
Definition: grid_helper.cpp:47
KIGFX::ORIGIN_VIEWITEM m_viewAxis
Definition: grid_helper.h:134
VECTOR2I AlignToSegment(const VECTOR2I &aPoint, const SEG &aSeg)
bool m_diagonalAuxAxesEnable
If true, use the aux axis for snapping as well.
Definition: grid_helper.h:127
VECTOR2I BestSnapAnchor(const VECTOR2I &aOrigin, BOARD_ITEM *aDraggedItem)
VECTOR2I GetGrid() const
Definition: grid_helper.cpp:88
void SetOrigin(const VECTOR2I &aOrigin)
Definition: grid_helper.cpp:82
VECTOR2< int > VECTOR2I
Definition: vector2d.h:587
double Distance(const VECTOR2I &aP) const
Definition: grid_helper.h:99
KIGFX::ORIGIN_VIEWITEM m_viewSnapPoint
Definition: grid_helper.h:133
Class LSET is a set of PCB_LAYER_IDs.
ANCHOR * nearestAnchor(const VECTOR2I &aPos, int aFlags, LSET aMatchLayers)
std::set< BOARD_ITEM * > queryVisible(const BOX2I &aArea, const std::vector< BOARD_ITEM * > aSkip) const
BOARD_ITEM * item
Definition: grid_helper.h:97
VECTOR2I GetOrigin() const
Definition: grid_helper.cpp:98
bool m_enableSnap
If true, allow snapping to other items on the layers.
Definition: grid_helper.h:128
ANCHOR(VECTOR2I aPos, int aFlags=CORNER|SNAPPABLE, BOARD_ITEM *aItem=NULL)
Definition: grid_helper.h:92
void SetSnap(bool aSnap)
Definition: grid_helper.h:72
void addAnchor(const VECTOR2I &aPos, int aFlags=CORNER|SNAPPABLE, BOARD_ITEM *aItem=NULL)
Definition: grid_helper.h:110
Definition: seg.h:36
Board layer functions and definitions.
PCB_BASE_FRAME * m_frame
Definition: grid_helper.h:124
int m_snapSize
Sets the radius in screen units for snapping to items.
Definition: grid_helper.h:130
void SetAuxAxes(bool aEnable, const VECTOR2I &aOrigin=VECTOR2I(0, 0), bool aEnableDiagonal=false)
void computeAnchors(BOARD_ITEM *aItem, const VECTOR2I &aRefPos)
boost::optional< T > OPT
Definition: optional.h:7
bool m_enableGrid
If true, allow snapping to grid.
Definition: grid_helper.h:129
void SetUseGrid(bool aGrid=true)
Definition: grid_helper.h:77
std::vector< ANCHOR > m_anchors
Definition: grid_helper.h:105
BOARD_ITEM * GetSnapped() const
Function GetSnapped If the GRID_HELPER has highlighted a snap point (target shown), this function will return a pointer to the item to which it snapped.
class PCB_BASE_FRAME basic PCB main window class for Pcbnew, Gerbview, and CvPcb footprint viewer...
void clearAnchors()
Definition: grid_helper.h:119