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 <boost/optional.hpp>
32 
34 
35 #include <geometry/seg.h>
36 
37 class PCB_BASE_FRAME;
38 
39 class GRID_HELPER {
40 public:
41 
42  GRID_HELPER( PCB_BASE_FRAME* aFrame );
43  ~GRID_HELPER();
44 
45  void SetGrid( int aSize );
46  void SetOrigin( const VECTOR2I& aOrigin );
47 
48  VECTOR2I GetGrid() const;
49  VECTOR2I GetOrigin() const;
50 
51  void SetAuxAxes( bool aEnable, const VECTOR2I& aOrigin = VECTOR2I( 0, 0 ), bool aEnableDiagonal = false );
52 
53  VECTOR2I Align( const VECTOR2I& aPoint ) const;
54 
55  VECTOR2I AlignToSegment ( const VECTOR2I& aPoint, const SEG& aSeg );
56 
57  VECTOR2I BestDragOrigin( const VECTOR2I& aMousePos, BOARD_ITEM* aItem );
58  VECTOR2I BestSnapAnchor( const VECTOR2I& aOrigin, BOARD_ITEM* aDraggedItem );
59 
60 private:
61  enum ANCHOR_FLAGS {
62  CORNER = 0x1,
63  OUTLINE = 0x2,
64  SNAPPABLE = 0x4,
65  ORIGIN = 0x8
66  };
67 
68  struct ANCHOR
69  {
70  ANCHOR( VECTOR2I aPos, int aFlags = CORNER | SNAPPABLE, BOARD_ITEM* aItem = NULL ):
71  pos( aPos ), flags( aFlags ), item( aItem ) {} ;
72 
73  VECTOR2I pos;
74  int flags;
76 
77  double Distance( const VECTOR2I& aP ) const
78  {
79  return ( aP - pos ).EuclideanNorm();
80  }
81 
82  //bool CanSnapItem( const BOARD_ITEM* aItem ) const;
83  };
84 
85  std::vector<ANCHOR> m_anchors;
86 
87  std::set<BOARD_ITEM*> queryVisible( const BOX2I& aArea ) const;
88 
89  void addAnchor( const VECTOR2I& aPos, int aFlags = CORNER | SNAPPABLE, BOARD_ITEM* aItem = NULL )
90  {
91  m_anchors.push_back( ANCHOR( aPos, aFlags, aItem ) );
92  }
93 
94  ANCHOR* nearestAnchor( const VECTOR2I& aPos, int aFlags, LSET aMatchLayers );
95 
96  void computeAnchors( BOARD_ITEM* aItem, const VECTOR2I& aRefPos );
97 
98  void clearAnchors()
99  {
100  m_anchors.clear();
101  }
102 
106 };
107 
108 #endif
VECTOR2I BestDragOrigin(const VECTOR2I &aMousePos, BOARD_ITEM *aItem)
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:56
VECTOR2I Align(const VECTOR2I &aPoint) const
Definition: grid_helper.cpp:95
GRID_HELPER(PCB_BASE_FRAME *aFrame)
Definition: grid_helper.cpp:44
VECTOR2I AlignToSegment(const VECTOR2I &aPoint, const SEG &aSeg)
std::set< BOARD_ITEM * > queryVisible(const BOX2I &aArea) const
bool m_diagonalAuxAxesEnable
Definition: grid_helper.h:105
VECTOR2I BestSnapAnchor(const VECTOR2I &aOrigin, BOARD_ITEM *aDraggedItem)
VECTOR2I GetGrid() const
Definition: grid_helper.cpp:68
void SetOrigin(const VECTOR2I &aOrigin)
Definition: grid_helper.cpp:62
VECTOR2< int > VECTOR2I
Definition: vector2d.h:590
double Distance(const VECTOR2I &aP) const
Definition: grid_helper.h:77
Class LSET is a set of PCB_LAYER_IDs.
ANCHOR * nearestAnchor(const VECTOR2I &aPos, int aFlags, LSET aMatchLayers)
BOARD_ITEM * item
Definition: grid_helper.h:75
boost::optional< VECTOR2I > m_auxAxis
Definition: grid_helper.h:104
VECTOR2I GetOrigin() const
Definition: grid_helper.cpp:78
ANCHOR(VECTOR2I aPos, int aFlags=CORNER|SNAPPABLE, BOARD_ITEM *aItem=NULL)
Definition: grid_helper.h:70
void addAnchor(const VECTOR2I &aPos, int aFlags=CORNER|SNAPPABLE, BOARD_ITEM *aItem=NULL)
Definition: grid_helper.h:89
Definition: seg.h:37
Board layer functions and definitions.
PCB_BASE_FRAME * m_frame
Definition: grid_helper.h:103
void SetAuxAxes(bool aEnable, const VECTOR2I &aOrigin=VECTOR2I(0, 0), bool aEnableDiagonal=false)
Definition: grid_helper.cpp:84
void computeAnchors(BOARD_ITEM *aItem, const VECTOR2I &aRefPos)
std::vector< ANCHOR > m_anchors
Definition: grid_helper.h:85
class PCB_BASE_FRAME basic PCB main window class for Pcbnew, Gerbview, and CvPcb footprint viewer...
void clearAnchors()
Definition: grid_helper.h:98