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