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 #include <math/vector2d.h>
30 #include <core/optional.h>
31 #include <origin_viewitem.h>
33 #include <geometry/seg.h>
34 
35 class PCB_BASE_FRAME;
36 
37 class GRID_HELPER {
38 public:
39 
40  GRID_HELPER( PCB_BASE_FRAME* aFrame );
41  ~GRID_HELPER();
42 
43  VECTOR2I GetGrid() const;
44  VECTOR2I GetOrigin() const;
45 
53  BOARD_ITEM* GetSnapped() const;
54 
55  void SetAuxAxes( bool aEnable, const VECTOR2I& aOrigin = VECTOR2I( 0, 0 ) );
56 
57  VECTOR2I Align( const VECTOR2I& aPoint ) const;
58 
59  VECTOR2I AlignToSegment ( const VECTOR2I& aPoint, const SEG& aSeg );
60 
61  VECTOR2I BestDragOrigin( const VECTOR2I& aMousePos, BOARD_ITEM* aItem );
62  VECTOR2I BestSnapAnchor( const VECTOR2I& aOrigin, BOARD_ITEM* aDraggedItem );
63  VECTOR2I BestSnapAnchor( const VECTOR2I& aOrigin, const LSET& aLayers,
64  const std::vector<BOARD_ITEM*> aSkip = {} );
65 
66  void SetSnap( bool aSnap )
67  {
68  m_enableSnap = aSnap;
69  }
70 
71  void SetUseGrid( bool aGrid = true )
72  {
73  m_enableGrid = aGrid;
74  }
75 
76 private:
77  enum ANCHOR_FLAGS {
78  CORNER = 0x1,
79  OUTLINE = 0x2,
80  SNAPPABLE = 0x4,
81  ORIGIN = 0x8
82  };
83 
84  struct ANCHOR
85  {
86  ANCHOR( VECTOR2I aPos, int aFlags = CORNER | SNAPPABLE, BOARD_ITEM* aItem = NULL ) :
87  pos( aPos ),
88  flags( aFlags ),
89  item( aItem )
90  { };
91 
92  VECTOR2I pos;
93  int flags;
95 
96  double Distance( const VECTOR2I& aP ) const
97  {
98  return ( aP - pos ).EuclideanNorm();
99  }
100  };
101 
102  std::vector<ANCHOR> m_anchors;
103 
104  std::set<BOARD_ITEM*> queryVisible( const BOX2I& aArea,
105  const std::vector<BOARD_ITEM*> aSkip ) const;
106 
107  void addAnchor( const VECTOR2I& aPos, int aFlags, BOARD_ITEM* aItem )
108  {
109  m_anchors.emplace_back( ANCHOR( aPos, aFlags, aItem ) );
110  }
111 
112  ANCHOR* nearestAnchor( const VECTOR2I& aPos, int aFlags, LSET aMatchLayers );
113 
122  void computeAnchors( BOARD_ITEM* aItem, const VECTOR2I& aRefPos, bool aFrom = false );
123 
125  {
126  m_anchors.clear();
127  }
128 
131 
136 
139 };
140 
141 #endif
VECTOR2I BestDragOrigin(const VECTOR2I &aMousePos, BOARD_ITEM *aItem)
OPT< VECTOR2I > m_auxAxis
Definition: grid_helper.h:130
ANCHOR * m_snapItem
Pointer to the currently snapped item in m_anchors (NULL if not snapped)
Definition: grid_helper.h:135
Class BOARD_ITEM is a base class for any item which can be embedded within the BOARD container class,...
std::set< BOARD_ITEM * > queryVisible(const BOX2I &aArea, const std::vector< BOARD_ITEM * > aSkip) const
GRID_HELPER(PCB_BASE_FRAME *aFrame)
Definition: grid_helper.cpp:48
KIGFX::ORIGIN_VIEWITEM m_viewAxis
Definition: grid_helper.h:138
VECTOR2I GetOrigin() const
Definition: grid_helper.cpp:86
VECTOR2I AlignToSegment(const VECTOR2I &aPoint, const SEG &aSeg)
VECTOR2I BestSnapAnchor(const VECTOR2I &aOrigin, BOARD_ITEM *aDraggedItem)
VECTOR2< int > VECTOR2I
Definition: vector2d.h:587
VECTOR2I GetGrid() const
Definition: grid_helper.cpp:76
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:137
void SetAuxAxes(bool aEnable, const VECTOR2I &aOrigin=VECTOR2I(0, 0))
Definition: grid_helper.cpp:92
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,...
Class LSET is a set of PCB_LAYER_IDs.
ANCHOR * nearestAnchor(const VECTOR2I &aPos, int aFlags, LSET aMatchLayers)
double Distance(const VECTOR2I &aP) const
Definition: grid_helper.h:96
BOARD_ITEM * item
Definition: grid_helper.h:94
bool m_enableSnap
If true, allow snapping to other items on the layers.
Definition: grid_helper.h:132
ANCHOR(VECTOR2I aPos, int aFlags=CORNER|SNAPPABLE, BOARD_ITEM *aItem=NULL)
Definition: grid_helper.h:86
void SetSnap(bool aSnap)
Definition: grid_helper.h:66
Definition: seg.h:36
VECTOR2I Align(const VECTOR2I &aPoint) const
Board layer functions and definitions.
PCB_BASE_FRAME * m_frame
Definition: grid_helper.h:129
int m_snapSize
Sets the radius in screen units for snapping to items.
Definition: grid_helper.h:134
boost::optional< T > OPT
Definition: optional.h:7
void addAnchor(const VECTOR2I &aPos, int aFlags, BOARD_ITEM *aItem)
Definition: grid_helper.h:107
bool m_enableGrid
If true, allow snapping to grid.
Definition: grid_helper.h:133
void SetUseGrid(bool aGrid=true)
Definition: grid_helper.h:71
std::vector< ANCHOR > m_anchors
Definition: grid_helper.h:102
class PCB_BASE_FRAME basic PCB main window class for Pcbnew, Gerbview, and CvPcb footprint viewer.
void clearAnchors()
Definition: grid_helper.h:124