KiCad PCB EDA Suite
GRID_HELPER Class Reference

#include <grid_helper.h>

Classes

struct  ANCHOR
 

Public Member Functions

 GRID_HELPER (PCB_BASE_FRAME *aFrame)
 
 ~GRID_HELPER ()
 
void SetGrid (int aSize)
 
void SetOrigin (const VECTOR2I &aOrigin)
 
VECTOR2I GetGrid () const
 
VECTOR2I GetOrigin () const
 
BOARD_ITEMGetSnapped () 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. More...
 
void SetAuxAxes (bool aEnable, const VECTOR2I &aOrigin=VECTOR2I(0, 0), bool aEnableDiagonal=false)
 
VECTOR2I Align (const VECTOR2I &aPoint) const
 
VECTOR2I AlignToSegment (const VECTOR2I &aPoint, const SEG &aSeg)
 
VECTOR2I BestDragOrigin (const VECTOR2I &aMousePos, BOARD_ITEM *aItem)
 
VECTOR2I BestSnapAnchor (const VECTOR2I &aOrigin, BOARD_ITEM *aDraggedItem)
 
VECTOR2I BestSnapAnchor (const VECTOR2I &aOrigin, const LSET &aLayers, const std::vector< BOARD_ITEM * > aSkip={})
 
void SetSnap (bool aSnap)
 
void SetUseGrid (bool aGrid=true)
 

Private Types

enum  ANCHOR_FLAGS { CORNER = 0x1, OUTLINE = 0x2, SNAPPABLE = 0x4, ORIGIN = 0x8 }
 

Private Member Functions

std::set< BOARD_ITEM * > queryVisible (const BOX2I &aArea, const std::vector< BOARD_ITEM * > aSkip) const
 
void addAnchor (const VECTOR2I &aPos, int aFlags=CORNER|SNAPPABLE, BOARD_ITEM *aItem=NULL)
 
ANCHORnearestAnchor (const VECTOR2I &aPos, int aFlags, LSET aMatchLayers)
 
void computeAnchors (BOARD_ITEM *aItem, const VECTOR2I &aRefPos)
 
void clearAnchors ()
 

Private Attributes

std::vector< ANCHORm_anchors
 
PCB_BASE_FRAMEm_frame
 
OPT< VECTOR2Im_auxAxis
 
bool m_diagonalAuxAxesEnable
 If true, use the aux axis for snapping as well. More...
 
bool m_enableSnap
 If true, allow snapping to other items on the layers. More...
 
bool m_enableGrid
 If true, allow snapping to grid. More...
 
int m_snapSize
 Sets the radius in screen units for snapping to items. More...
 
ANCHORm_snapItem
 Pointer to the currently snapped item in m_anchors (NULL if not snapped) More...
 
KIGFX::ORIGIN_VIEWITEM m_viewSnapPoint
 
KIGFX::ORIGIN_VIEWITEM m_viewAxis
 

Detailed Description

Definition at line 40 of file grid_helper.h.

Member Enumeration Documentation

Enumerator
CORNER 
OUTLINE 
SNAPPABLE 
ORIGIN 

Definition at line 83 of file grid_helper.h.

83  {
84  CORNER = 0x1,
85  OUTLINE = 0x2,
86  SNAPPABLE = 0x4,
87  ORIGIN = 0x8
88  };

Constructor & Destructor Documentation

GRID_HELPER::GRID_HELPER ( PCB_BASE_FRAME aFrame)

Definition at line 47 of file grid_helper.cpp.

References KIGFX::VIEW::Add(), KIGFX::ORIGIN_VIEWITEM::CIRCLE_CROSS, KIGFX::ORIGIN_VIEWITEM::CROSS, EDA_DRAW_FRAME::GetGalCanvas(), EDA_DRAW_PANEL_GAL::GetView(), m_diagonalAuxAxesEnable, m_enableGrid, m_enableSnap, m_frame, m_snapSize, m_viewAxis, m_viewSnapPoint, KIGFX::ORIGIN_VIEWITEM::SetColor(), KIGFX::ORIGIN_VIEWITEM::SetDrawAtZero(), KIGFX::ORIGIN_VIEWITEM::SetSize(), KIGFX::ORIGIN_VIEWITEM::SetStyle(), and KIGFX::VIEW::SetVisible().

47  :
48  m_frame( aFrame )
49 {
51  m_enableSnap = true;
52  m_enableGrid = true;
53  m_snapSize = 100;
55 
56  m_viewAxis.SetSize( 20000 );
58  m_viewAxis.SetColor( COLOR4D( 1.0, 1.0, 1.0, 0.4 ) );
59  m_viewAxis.SetDrawAtZero( true );
60  view->Add( &m_viewAxis );
61  view->SetVisible( &m_viewAxis, false );
62 
64  m_viewSnapPoint.SetColor( COLOR4D( 1.0, 1.0, 1.0, 1.0 ) );
66  view->Add( &m_viewSnapPoint );
67  view->SetVisible( &m_viewSnapPoint, false );
68 }
KIGFX::ORIGIN_VIEWITEM m_viewAxis
Definition: grid_helper.h:134
bool m_diagonalAuxAxesEnable
If true, use the aux axis for snapping as well.
Definition: grid_helper.h:127
KIGFX::VIEW * GetView() const
Function GetView() Returns a pointer to the VIEW instance used in the panel.
KIGFX::ORIGIN_VIEWITEM m_viewSnapPoint
Definition: grid_helper.h:133
void SetSize(int aSize)
void SetDrawAtZero(bool aDrawFlag)
Function SetDrawAtZero() Set the draw at zero flag.
bool m_enableSnap
If true, allow snapping to other items on the layers.
Definition: grid_helper.h:128
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 SetColor(const KIGFX::COLOR4D &aColor)
void SetVisible(VIEW_ITEM *aItem, bool aIsVisible=true)
Sets the item visibility.
Definition: view.cpp:1484
EDA_DRAW_PANEL_GAL * GetGalCanvas() const
Return a pointer to GAL-based canvas of given EDA draw frame.
Definition: draw_frame.h:925
bool m_enableGrid
If true, allow snapping to grid.
Definition: grid_helper.h:129
virtual void Add(VIEW_ITEM *aItem, int aDrawPriority=-1)
Function Add() Adds a VIEW_ITEM to the view.
Definition: view.cpp:345
Class VIEW.
Definition: view.h:61
void SetStyle(MARKER_STYLE aStyle)
Class COLOR4D is the color representation with 4 components: red, green, blue, alpha.
Definition: color4d.h:39
GRID_HELPER::~GRID_HELPER ( )

Definition at line 71 of file grid_helper.cpp.

72 {
73 }

Member Function Documentation

void GRID_HELPER::addAnchor ( const VECTOR2I aPos,
int  aFlags = CORNER | SNAPPABLE,
BOARD_ITEM aItem = NULL 
)
inlineprivate

Definition at line 110 of file grid_helper.h.

References GRID_HELPER::ANCHOR::ANCHOR(), computeAnchors(), and nearestAnchor().

Referenced by computeAnchors().

111  {
112  m_anchors.push_back( ANCHOR( aPos, aFlags, aItem ) );
113  }
std::vector< ANCHOR > m_anchors
Definition: grid_helper.h:105
VECTOR2I GRID_HELPER::Align ( const VECTOR2I aPoint) const

Definition at line 124 of file grid_helper.cpp.

References abs, GetGrid(), GetOrigin(), KiROUND(), m_auxAxis, m_enableGrid, VECTOR2< T >::x, and VECTOR2< T >::y.

Referenced by BestSnapAnchor(), EDIT_TOOL::Main(), and PNS::TOOL_BASE::snapToItem().

125 {
126  if( !m_enableGrid )
127  return aPoint;
128 
129  const VECTOR2D gridOffset( GetOrigin() );
130  const VECTOR2D gridSize( GetGrid() );
131 
132  VECTOR2I nearest( KiROUND( ( aPoint.x - gridOffset.x ) / gridSize.x ) * gridSize.x + gridOffset.x,
133  KiROUND( ( aPoint.y - gridOffset.y ) / gridSize.y ) * gridSize.y + gridOffset.y );
134 
135  if( !m_auxAxis )
136  return nearest;
137 
138  if( std::abs( m_auxAxis->x - aPoint.x ) < std::abs( nearest.x - aPoint.x ) )
139  nearest.x = m_auxAxis->x;
140 
141  if( std::abs( m_auxAxis->y - aPoint.y ) < std::abs( nearest.y - aPoint.y ) )
142  nearest.y = m_auxAxis->y;
143 
144  return nearest;
145 }
OPT< VECTOR2I > m_auxAxis
Definition: grid_helper.h:125
static int KiROUND(double v)
Round a floating point number to an integer using "round halfway cases away from zero".
Definition: common.h:121
VECTOR2I GetGrid() const
Definition: grid_helper.cpp:88
#define abs(a)
Definition: auxiliary.h:84
VECTOR2I GetOrigin() const
Definition: grid_helper.cpp:98
bool m_enableGrid
If true, allow snapping to grid.
Definition: grid_helper.h:129
VECTOR2I GRID_HELPER::AlignToSegment ( const VECTOR2I aPoint,
const SEG aSeg 
)

Definition at line 148 of file grid_helper.cpp.

References SEG::A, SEG::B, SEG::Contains(), EuclideanNorm(), GetGrid(), GetOrigin(), i, SEG::IntersectLines(), KiROUND(), m_enableSnap, max, VECTOR2< T >::x, and VECTOR2< T >::y.

Referenced by DRAWING_TOOL::DrawVia(), and PNS::TOOL_BASE::snapToItem().

149 {
150  OPT_VECTOR2I pts[6];
151 
152  if( !m_enableSnap )
153  return aPoint;
154 
155  const VECTOR2D gridOffset( GetOrigin() );
156  const VECTOR2D gridSize( GetGrid() );
157 
158  VECTOR2I nearest( KiROUND( ( aPoint.x - gridOffset.x ) / gridSize.x ) * gridSize.x + gridOffset.x,
159  KiROUND( ( aPoint.y - gridOffset.y ) / gridSize.y ) * gridSize.y + gridOffset.y );
160 
161  pts[0] = aSeg.A;
162  pts[1] = aSeg.B;
163  pts[2] = aSeg.IntersectLines( SEG( nearest, nearest + VECTOR2I( 1, 0 ) ) );
164  pts[3] = aSeg.IntersectLines( SEG( nearest, nearest + VECTOR2I( 0, 1 ) ) );
165 
166  int min_d = std::numeric_limits<int>::max();
167 
168  for( int i = 0; i < 4; i++ )
169  {
170  if( pts[i] && aSeg.Contains( *pts[i] ) )
171  {
172  int d = (*pts[i] - aPoint).EuclideanNorm();
173 
174  if( d < min_d )
175  {
176  min_d = d;
177  nearest = *pts[i];
178  }
179  }
180  }
181 
182  return nearest;
183 }
double EuclideanNorm(const wxPoint &vector)
Euclidean norm of a 2D vector.
Definition: trigo.h:112
static int KiROUND(double v)
Round a floating point number to an integer using "round halfway cases away from zero".
Definition: common.h:121
OPT_VECTOR2I IntersectLines(const SEG &aSeg) const
Function IntersectLines()
Definition: seg.h:183
VECTOR2I GetGrid() const
Definition: grid_helper.cpp:88
VECTOR2< int > VECTOR2I
Definition: vector2d.h:587
OPT< VECTOR2I > OPT_VECTOR2I
Definition: seg.h:34
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
Definition: seg.h:36
#define max(a, b)
Definition: auxiliary.h:86
size_t i
Definition: json11.cpp:597
VECTOR2I A
Definition: seg.h:44
bool Contains(const VECTOR2I &aP) const
Definition: seg.cpp:188
VECTOR2I B
Definition: seg.h:45
VECTOR2I GRID_HELPER::BestDragOrigin ( const VECTOR2I aMousePos,
BOARD_ITEM aItem 
)

Definition at line 186 of file grid_helper.cpp.

References LSET::AllLayersMask(), clearAnchors(), computeAnchors(), CORNER, dist, GRID_HELPER::ANCHOR::Distance(), EDA_DRAW_PANEL_GAL::GetGAL(), EDA_DRAW_FRAME::GetGalCanvas(), KIGFX::GAL::GetWorldScale(), m_frame, max, nearestAnchor(), ORIGIN, OUTLINE, and GRID_HELPER::ANCHOR::pos.

Referenced by EDIT_TOOL::Main().

187 {
188  clearAnchors();
189  computeAnchors( aItem, aMousePos );
190 
191  double worldScale = m_frame->GetGalCanvas()->GetGAL()->GetWorldScale();
192  double lineSnapMinCornerDistance = 50.0 / worldScale;
193 
194  ANCHOR* nearestOutline = nearestAnchor( aMousePos, OUTLINE, LSET::AllLayersMask() );
195  ANCHOR* nearestCorner = nearestAnchor( aMousePos, CORNER, LSET::AllLayersMask() );
196  ANCHOR* nearestOrigin = nearestAnchor( aMousePos, ORIGIN, LSET::AllLayersMask() );
197  ANCHOR* best = NULL;
198  double minDist = std::numeric_limits<double>::max();
199 
200  if( nearestOrigin )
201  {
202  minDist = nearestOrigin->Distance( aMousePos );
203  best = nearestOrigin;
204  }
205 
206  if( nearestCorner )
207  {
208  double dist = nearestCorner->Distance( aMousePos );
209 
210  if( dist < minDist )
211  {
212  minDist = dist;
213  best = nearestCorner;
214  }
215  }
216 
217  if( nearestOutline )
218  {
219  double dist = nearestOutline->Distance( aMousePos );
220 
221  if( minDist > lineSnapMinCornerDistance && dist < minDist )
222  best = nearestOutline;
223  }
224 
225  return best ? best->pos : aMousePos;
226 }
static const int dist[10][10]
Definition: ar_matrix.cpp:320
double GetWorldScale() const
Get the world scale.
KIGFX::GAL * GetGAL() const
Function GetGAL() Returns a pointer to the GAL instance used in the panel.
ANCHOR * nearestAnchor(const VECTOR2I &aPos, int aFlags, LSET aMatchLayers)
static LSET AllLayersMask()
Definition: lset.cpp:713
#define max(a, b)
Definition: auxiliary.h:86
PCB_BASE_FRAME * m_frame
Definition: grid_helper.h:124
void computeAnchors(BOARD_ITEM *aItem, const VECTOR2I &aRefPos)
EDA_DRAW_PANEL_GAL * GetGalCanvas() const
Return a pointer to GAL-based canvas of given EDA draw frame.
Definition: draw_frame.h:925
void clearAnchors()
Definition: grid_helper.h:119
VECTOR2I GRID_HELPER::BestSnapAnchor ( const VECTOR2I aOrigin,
BOARD_ITEM aDraggedItem 
)

Definition at line 258 of file grid_helper.cpp.

References LSET::AllLayersMask(), and BOARD_ITEM::GetLayerSet().

Referenced by DRAWING_TOOL::drawArc(), DRAWING_TOOL::DrawDimension(), DRAWING_TOOL::drawSegment(), DRAWING_TOOL::drawZone(), PICKER_TOOL::Main(), EDIT_TOOL::Main(), EDIT_TOOL::MeasureTool(), and POINT_EDITOR::OnSelectionChange().

259 {
260  LSET layers;
261  std::vector<BOARD_ITEM*> item;
262 
263  if( aDraggedItem )
264  {
265  layers = aDraggedItem->GetLayerSet();
266  item.push_back( aDraggedItem );
267  }
268  else
269  layers = LSET::AllLayersMask();
270 
271  return BestSnapAnchor( aOrigin, layers, item );
272 }
VECTOR2I BestSnapAnchor(const VECTOR2I &aOrigin, BOARD_ITEM *aDraggedItem)
Class LSET is a set of PCB_LAYER_IDs.
virtual LSET GetLayerSet() const
Function GetLayerSet returns a "layer mask", which is a bitmap of all layers on which the TRACK segme...
static LSET AllLayersMask()
Definition: lset.cpp:713
VECTOR2I GRID_HELPER::BestSnapAnchor ( const VECTOR2I aOrigin,
const LSET aLayers,
const std::vector< BOARD_ITEM * >  aSkip = {} 
)

Definition at line 275 of file grid_helper.cpp.

References Align(), clearAnchors(), computeAnchors(), GRID_HELPER::ANCHOR::Distance(), EuclideanNorm(), KIGFX::GEOMETRY, EDA_DRAW_PANEL_GAL::GetGAL(), EDA_DRAW_FRAME::GetGalCanvas(), EDA_DRAW_PANEL_GAL::GetView(), KIGFX::GAL::GetWorldScale(), KIGFX::VIEW::IsVisible(), m_enableGrid, m_enableSnap, m_frame, m_snapItem, m_snapSize, m_viewSnapPoint, nearestAnchor(), GRID_HELPER::ANCHOR::pos, queryVisible(), KIGFX::ORIGIN_VIEWITEM::SetPosition(), KIGFX::VIEW::SetVisible(), SNAPPABLE, KIGFX::VIEW::Update(), VECTOR2< T >::x, and VECTOR2< T >::y.

277 {
278  double worldScale = m_frame->GetGalCanvas()->GetGAL()->GetWorldScale();
279  int snapRange = (int) ( m_snapSize / worldScale );
280 
281  BOX2I bb( VECTOR2I( aOrigin.x - snapRange / 2, aOrigin.y - snapRange / 2 ), VECTOR2I( snapRange, snapRange ) );
282 
283  clearAnchors();
284 
285  for( BOARD_ITEM* item : queryVisible( bb, aSkip ) )
286  {
287  computeAnchors( item, aOrigin );
288  }
289 
290  ANCHOR* nearest = nearestAnchor( aOrigin, SNAPPABLE, aLayers );
291  VECTOR2I nearestGrid = Align( aOrigin );
292  double gridDist = ( nearestGrid - aOrigin ).EuclideanNorm();
293 
294  if( nearest && m_enableSnap )
295  {
296  double snapDist = nearest->Distance( aOrigin );
297 
298  if( !m_enableGrid || snapDist <= gridDist )
299  {
300  m_viewSnapPoint.SetPosition( nearest->pos );
301 
304  else
306 
307  m_snapItem = nearest;
308  return nearest->pos;
309  }
310  }
311 
312  m_snapItem = nullptr;
314  return nearestGrid;
315 }
double EuclideanNorm(const wxPoint &vector)
Euclidean norm of a 2D vector.
Definition: trigo.h:112
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...
double GetWorldScale() const
Get the world scale.
VECTOR2I Align(const VECTOR2I &aPoint) const
Class VECTOR2 defines a general 2D-vector/point.
Definition: vector2d.h:61
KIGFX::VIEW * GetView() const
Function GetView() Returns a pointer to the VIEW instance used in the panel.
VECTOR2< int > VECTOR2I
Definition: vector2d.h:587
KIGFX::ORIGIN_VIEWITEM m_viewSnapPoint
Definition: grid_helper.h:133
KIGFX::GAL * GetGAL() const
Function GetGAL() Returns a pointer to the GAL instance used in the panel.
ANCHOR * nearestAnchor(const VECTOR2I &aPos, int aFlags, LSET aMatchLayers)
std::set< BOARD_ITEM * > queryVisible(const BOX2I &aArea, const std::vector< BOARD_ITEM * > aSkip) const
void SetPosition(const VECTOR2D &aPosition)
bool m_enableSnap
If true, allow snapping to other items on the layers.
Definition: grid_helper.h:128
virtual void Update(VIEW_ITEM *aItem, int aUpdateFlags)
For dynamic VIEWs, informs the associated VIEW that the graphical representation of this item has cha...
Definition: view.cpp:1538
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 computeAnchors(BOARD_ITEM *aItem, const VECTOR2I &aRefPos)
void SetVisible(VIEW_ITEM *aItem, bool aIsVisible=true)
Sets the item visibility.
Definition: view.cpp:1484
EDA_DRAW_PANEL_GAL * GetGalCanvas() const
Return a pointer to GAL-based canvas of given EDA draw frame.
Definition: draw_frame.h:925
bool m_enableGrid
If true, allow snapping to grid.
Definition: grid_helper.h:129
bool IsVisible(const VIEW_ITEM *aItem) const
Returns information if the item is visible (or not).
Definition: view.cpp:1524
void clearAnchors()
Definition: grid_helper.h:119
Color has changed.
Definition: view_item.h:57
void GRID_HELPER::clearAnchors ( )
inlineprivate

Definition at line 119 of file grid_helper.h.

Referenced by BestDragOrigin(), and BestSnapAnchor().

120  {
121  m_anchors.clear();
122  }
std::vector< ANCHOR > m_anchors
Definition: grid_helper.h:105
void GRID_HELPER::computeAnchors ( BOARD_ITEM aItem,
const VECTOR2I aRefPos 
)
private

Definition at line 327 of file grid_helper.cpp.

References addAnchor(), SHAPE_LINE_CHAIN::Append(), DRAWSEGMENT::BuildPolyPointsList(), SHAPE_POLY_SET::CIterateWithHoles(), CORNER, EuclideanNorm(), DRAWSEGMENT::GetArcEnd(), DRAWSEGMENT::GetArcStart(), DRAWSEGMENT::GetCenter(), DRAWSEGMENT::GetEnd(), TRACK::GetEnd(), BOARD_ITEM::GetPosition(), MODULE::GetPosition(), D_PAD::GetPosition(), DRAWSEGMENT::GetShape(), DRAWSEGMENT::GetStart(), TRACK::GetStart(), DIMENSION::m_crossBarF, DIMENSION::m_crossBarO, DIMENSION::m_featureLineDO, DIMENSION::m_featureLineGO, mod, SHAPE_LINE_CHAIN::NearestPoint(), ORIGIN, OUTLINE, MODULE::Pads(), PCB_DIMENSION_T, PCB_LINE_T, PCB_MARKER_T, PCB_MODULE_EDGE_T, PCB_MODULE_T, PCB_MODULE_TEXT_T, PCB_PAD_T, PCB_TARGET_T, PCB_TEXT_T, PCB_TRACE_T, PCB_VIA_T, PCB_ZONE_AREA_T, S_ARC, S_CIRCLE, S_CURVE, S_POLYGON, S_RECT, S_SEGMENT, SHAPE_LINE_CHAIN::SetClosed(), SNAPPABLE, EDA_ITEM::Type(), VECTOR2< T >::x, and VECTOR2< T >::y.

Referenced by addAnchor(), BestDragOrigin(), and BestSnapAnchor().

328 {
329  VECTOR2I origin;
330 
331  switch( aItem->Type() )
332  {
333  case PCB_MODULE_T:
334  {
335  MODULE* mod = static_cast<MODULE*>( aItem );
336 
337  for( auto pad : mod->Pads() )
338  {
339  if( pad->GetBoundingBox().Contains( wxPoint( aRefPos.x, aRefPos.y ) ) )
340  {
341  addAnchor( pad->GetPosition(), CORNER | SNAPPABLE, pad );
342  break;
343  }
344  }
345 
346  // if the cursor is not over a pad, then drag the module by its origin
347  addAnchor( mod->GetPosition(), ORIGIN | SNAPPABLE, mod );
348  break;
349  }
350 
351  case PCB_PAD_T:
352  {
353  D_PAD* pad = static_cast<D_PAD*>( aItem );
354  addAnchor( pad->GetPosition(), CORNER | SNAPPABLE, pad );
355  break;
356  }
357 
358  case PCB_MODULE_EDGE_T:
359  case PCB_LINE_T:
360  {
361  DRAWSEGMENT* dseg = static_cast<DRAWSEGMENT*>( aItem );
362  VECTOR2I start = dseg->GetStart();
363  VECTOR2I end = dseg->GetEnd();
364 
365  switch( dseg->GetShape() )
366  {
367  case S_CIRCLE:
368  {
369  int r = ( start - end ).EuclideanNorm();
370 
371  addAnchor( start, ORIGIN | SNAPPABLE, dseg );
372  addAnchor( start + VECTOR2I( -r, 0 ), OUTLINE | SNAPPABLE, dseg );
373  addAnchor( start + VECTOR2I( r, 0 ), OUTLINE | SNAPPABLE, dseg );
374  addAnchor( start + VECTOR2I( 0, -r ), OUTLINE | SNAPPABLE, dseg );
375  addAnchor( start + VECTOR2I( 0, r ), OUTLINE | SNAPPABLE, dseg );
376  break;
377  }
378 
379  case S_ARC:
380  origin = dseg->GetCenter();
381  addAnchor( dseg->GetArcStart(), CORNER | SNAPPABLE, dseg );
382  addAnchor( dseg->GetArcEnd(), CORNER | SNAPPABLE, dseg );
383  addAnchor( origin, ORIGIN | SNAPPABLE, dseg );
384  break;
385 
386  case S_RECT:
387  addAnchor( start, CORNER | SNAPPABLE, dseg );
388  addAnchor( VECTOR2I( end.x, start.y ), CORNER | SNAPPABLE, dseg );
389  addAnchor( VECTOR2I( start.x, end.y ), CORNER | SNAPPABLE, dseg );
390  addAnchor( end, CORNER | SNAPPABLE, dseg );
391  break;
392 
393  case S_SEGMENT:
394  origin.x = start.x + ( start.x - end.x ) / 2;
395  origin.y = start.y + ( start.y - end.y ) / 2;
396  addAnchor( start, CORNER | SNAPPABLE, dseg );
397  addAnchor( end, CORNER | SNAPPABLE, dseg );
398  addAnchor( origin, ORIGIN, dseg );
399  break;
400 
401  case S_POLYGON:
402  for( const auto& p : dseg->BuildPolyPointsList() )
403  addAnchor( p, CORNER | SNAPPABLE, dseg );
404 
405  break;
406 
407  case S_CURVE:
408  addAnchor( start, CORNER | SNAPPABLE, dseg );
409  addAnchor( end, CORNER | SNAPPABLE, dseg );
410  //Fallthrough
411  default:
412  origin = dseg->GetStart();
413  addAnchor( origin, ORIGIN | SNAPPABLE, dseg );
414  break;
415  }
416  break;
417  }
418 
419  case PCB_TRACE_T:
420  {
421  TRACK* track = static_cast<TRACK*>( aItem );
422  VECTOR2I start = track->GetStart();
423  VECTOR2I end = track->GetEnd();
424  origin.x = start.x + ( start.x - end.x ) / 2;
425  origin.y = start.y + ( start.y - end.y ) / 2;
426  addAnchor( start, CORNER | SNAPPABLE, track );
427  addAnchor( end, CORNER | SNAPPABLE, track );
428  addAnchor( origin, ORIGIN, track);
429  break;
430  }
431 
432  case PCB_MARKER_T:
433  case PCB_TARGET_T:
434  case PCB_VIA_T:
435  addAnchor( aItem->GetPosition(), ORIGIN | CORNER | SNAPPABLE, aItem );
436  break;
437 
438  case PCB_ZONE_AREA_T:
439  {
440  const SHAPE_POLY_SET* outline = static_cast<const ZONE_CONTAINER*>( aItem )->Outline();
441 
442  SHAPE_LINE_CHAIN lc;
443  lc.SetClosed( true );
444 
445  for( auto iter = outline->CIterateWithHoles(); iter; iter++ )
446  {
447  addAnchor( *iter, CORNER, aItem );
448  lc.Append( *iter );
449  }
450 
451  addAnchor( lc.NearestPoint( aRefPos ), OUTLINE, aItem );
452 
453  break;
454  }
455 
456  case PCB_DIMENSION_T:
457  {
458  const DIMENSION* dim = static_cast<const DIMENSION*>( aItem );
459  addAnchor( dim->m_crossBarF, CORNER | SNAPPABLE, aItem );
460  addAnchor( dim->m_crossBarO, CORNER | SNAPPABLE, aItem );
461  addAnchor( dim->m_featureLineGO, CORNER | SNAPPABLE, aItem );
462  addAnchor( dim->m_featureLineDO, CORNER | SNAPPABLE, aItem );
463  break;
464  }
465 
466  case PCB_MODULE_TEXT_T:
467  case PCB_TEXT_T:
468  addAnchor( aItem->GetPosition(), ORIGIN, aItem );
469  break;
470 
471  default:
472  break;
473  }
474 }
double EuclideanNorm(const wxPoint &vector)
Euclidean norm of a 2D vector.
Definition: trigo.h:112
KICAD_T Type() const
Function Type()
Definition: base_struct.h:201
Class ZONE_CONTAINER handles a list of polygons defining a copper zone.
Definition: class_zone.h:59
const std::vector< wxPoint > BuildPolyPointsList() const
Build and return the list of corners in a std::vector<wxPoint> It must be used only to convert the SH...
wxPoint m_crossBarF
const wxPoint GetCenter() const override
Function GetCenter()
polygon (not yet used for tracks, but could be in microwave apps)
CONST_ITERATOR CIterateWithHoles(int aOutline) const
class ZONE_CONTAINER, a zone area
Definition: typeinfo.h:102
Class VECTOR2 defines a general 2D-vector/point.
Definition: vector2d.h:61
DLIST_ITERATOR_WRAPPER< D_PAD > Pads()
Definition: class_module.h:168
const VECTOR2I NearestPoint(const VECTOR2I &aP) const
Function NearestPoint()
class TEXTE_PCB, text on a layer
Definition: typeinfo.h:92
usual segment : line with rounded ends
class D_PAD, a pad in a footprint
Definition: typeinfo.h:90
VECTOR2< int > VECTOR2I
Definition: vector2d.h:587
class EDGE_MODULE, a footprint edge
Definition: typeinfo.h:94
const wxPoint & GetEnd() const
Definition: class_track.h:123
class TRACK, a track segment (segment on a copper layer)
Definition: typeinfo.h:95
void Append(int aX, int aY, bool aAllowDuplication=false)
Function Append()
const wxPoint & GetArcStart() const
wxPoint m_featureLineGO
segment with non rounded ends
class MODULE, a footprint
Definition: typeinfo.h:89
void SetClosed(bool aClosed)
Function SetClosed()
STROKE_T GetShape() const
const wxPoint & GetEnd() const
Function GetEnd returns the ending point of the graphic.
Class SHAPE_POLY_SET.
const wxPoint & GetStart() const
Definition: class_track.h:126
Arcs (with rounded ends)
Bezier Curve.
class DIMENSION, a dimension (graphic item)
Definition: typeinfo.h:100
class PCB_TARGET, a target (graphic item)
Definition: typeinfo.h:101
virtual const wxPoint GetPosition() const =0
void addAnchor(const VECTOR2I &aPos, int aFlags=CORNER|SNAPPABLE, BOARD_ITEM *aItem=NULL)
Definition: grid_helper.h:110
class TEXTE_MODULE, text in a footprint
Definition: typeinfo.h:93
class MARKER_PCB, a marker used to show something
Definition: typeinfo.h:99
Class SHAPE_LINE_CHAIN.
const wxPoint GetArcEnd() const
wxPoint m_crossBarO
class VIA, a via (like a track segment on a copper layer)
Definition: typeinfo.h:96
const wxPoint GetPosition() const override
Definition: class_pad.h:220
class DRAWSEGMENT, a segment not on copper layers
Definition: typeinfo.h:91
const wxPoint & GetStart() const
Function GetStart returns the starting point of the graphic.
wxPoint m_featureLineDO
const wxPoint GetPosition() const override
Definition: class_module.h:183
#define mod(a, n)
Definition: greymap.cpp:24
Class DIMENSION.
VECTOR2I GRID_HELPER::GetGrid ( ) const

Definition at line 88 of file grid_helper.cpp.

References BASE_SCREEN::GetGridSize(), PCB_BASE_FRAME::GetScreen(), KiROUND(), and m_frame.

Referenced by Align(), and AlignToSegment().

89 {
90  PCB_SCREEN* screen = m_frame->GetScreen();
91 
92  const wxRealPoint& size = screen->GetGridSize();
93 
94  return VECTOR2I( KiROUND( size.x ), KiROUND( size.y ) );
95 }
static int KiROUND(double v)
Round a floating point number to an integer using "round halfway cases away from zero".
Definition: common.h:121
VECTOR2< int > VECTOR2I
Definition: vector2d.h:587
PCB_BASE_FRAME * m_frame
Definition: grid_helper.h:124
const wxRealPoint & GetGridSize() const
Return the grid size of the currently selected grid.
Definition: base_screen.h:410
PCB_SCREEN * GetScreen() const override
Return a pointer to a BASE_SCREEN or one of its derivatives.
VECTOR2I GRID_HELPER::GetOrigin ( ) const

Definition at line 98 of file grid_helper.cpp.

References PCB_BASE_FRAME::GetGridOrigin(), and m_frame.

Referenced by Align(), and AlignToSegment().

99 {
100  return VECTOR2I( m_frame->GetGridOrigin() );
101 }
const wxPoint & GetGridOrigin() const override
Return the absolute coordinates of the origin of the snap grid.
VECTOR2< int > VECTOR2I
Definition: vector2d.h:587
PCB_BASE_FRAME * m_frame
Definition: grid_helper.h:124
BOARD_ITEM * GRID_HELPER::GetSnapped ( void  ) 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.

Returns
NULL if not snapped. Pointer to snapped item otherwise

Definition at line 318 of file grid_helper.cpp.

References GRID_HELPER::ANCHOR::item, and m_snapItem.

Referenced by DRAWING_TOOL::drawSegment().

319 {
320  if( !m_snapItem )
321  return nullptr;
322 
323  return m_snapItem->item;
324 }
ANCHOR * m_snapItem
Pointer to the currently snapped item in m_anchors (NULL if not snapped)
Definition: grid_helper.h:131
BOARD_ITEM * item
Definition: grid_helper.h:97
GRID_HELPER::ANCHOR * GRID_HELPER::nearestAnchor ( const VECTOR2I aPos,
int  aFlags,
LSET  aMatchLayers 
)
private

Definition at line 477 of file grid_helper.cpp.

References dist, m_anchors, and max.

Referenced by addAnchor(), BestDragOrigin(), and BestSnapAnchor().

478 {
479  double minDist = std::numeric_limits<double>::max();
480  ANCHOR* best = NULL;
481 
482  for( ANCHOR& a : m_anchors )
483  {
484  if( ( aMatchLayers & a.item->GetLayerSet() ) == 0 )
485  continue;
486 
487  if( ( aFlags & a.flags ) != aFlags )
488  continue;
489 
490  double dist = a.Distance( aPos );
491 
492  if( dist < minDist )
493  {
494  minDist = dist;
495  best = &a;
496  }
497  }
498 
499  return best;
500 }
static const int dist[10][10]
Definition: ar_matrix.cpp:320
#define max(a, b)
Definition: auxiliary.h:86
std::vector< ANCHOR > m_anchors
Definition: grid_helper.h:105
std::set< BOARD_ITEM * > GRID_HELPER::queryVisible ( const BOX2I aArea,
const std::vector< BOARD_ITEM * >  aSkip 
) const
private

Definition at line 229 of file grid_helper.cpp.

References KIGFX::RENDER_SETTINGS::GetActiveLayers(), EDA_DRAW_FRAME::GetGalCanvas(), KIGFX::VIEW::GetPainter(), KIGFX::PAINTER::GetSettings(), EDA_DRAW_PANEL_GAL::GetView(), and m_frame.

Referenced by BestSnapAnchor().

231 {
232  std::set<BOARD_ITEM*> items;
233  std::vector<KIGFX::VIEW::LAYER_ITEM_PAIR> selectedItems;
234 
235  auto view = m_frame->GetGalCanvas()->GetView();
236  auto activeLayers = view->GetPainter()->GetSettings()->GetActiveLayers();
237  bool isHighContrast = view->GetPainter()->GetSettings()->GetHighContrast();
238  view->Query( aArea, selectedItems );
239 
240  for( auto it : selectedItems )
241  {
242  BOARD_ITEM* item = static_cast<BOARD_ITEM*>( it.first );
243 
244  // The item must be visible and on an active layer
245  if( view->IsVisible( item )
246  && ( !isHighContrast || activeLayers.count( it.second ) ) )
247  items.insert ( item );
248  }
249 
250 
251  for( auto ii : aSkip )
252  items.erase( ii );
253 
254  return items;
255 }
Class BOARD_ITEM is a base class for any item which can be embedded within the BOARD container class...
KIGFX::VIEW * GetView() const
Function GetView() Returns a pointer to the VIEW instance used in the panel.
PAINTER * GetPainter() const
Function GetPainter() Returns the painter object used by the view for drawing VIEW_ITEMS.
Definition: view.h:199
virtual RENDER_SETTINGS * GetSettings()=0
Function GetSettings Returns pointer to current settings that are going to be used when drawing items...
PCB_BASE_FRAME * m_frame
Definition: grid_helper.h:124
EDA_DRAW_PANEL_GAL * GetGalCanvas() const
Return a pointer to GAL-based canvas of given EDA draw frame.
Definition: draw_frame.h:925
const std::set< unsigned int > GetActiveLayers()
Function GetActiveLayers() Returns the set of currently active layers.
Definition: painter.h:89
void GRID_HELPER::SetAuxAxes ( bool  aEnable,
const VECTOR2I aOrigin = VECTOR2I( 0, 0 ),
bool  aEnableDiagonal = false 
)

Definition at line 104 of file grid_helper.cpp.

References EDA_DRAW_FRAME::GetGalCanvas(), EDA_DRAW_PANEL_GAL::GetView(), m_auxAxis, m_diagonalAuxAxesEnable, m_frame, m_viewAxis, KIGFX::ORIGIN_VIEWITEM::SetPosition(), and KIGFX::VIEW::SetVisible().

Referenced by EDIT_TOOL::Main(), and POINT_EDITOR::OnSelectionChange().

105 {
106  KIGFX::VIEW* view = m_frame->GetGalCanvas()->GetView();
107 
108  if( aEnable )
109  {
110  m_auxAxis = aOrigin;
111  m_viewAxis.SetPosition( aOrigin );
112  view->SetVisible( &m_viewAxis, true );
113  }
114  else
115  {
117  view->SetVisible( &m_viewAxis, false );
118  }
119 
120  m_diagonalAuxAxesEnable = aEnable;
121 }
OPT< VECTOR2I > m_auxAxis
Definition: grid_helper.h:125
KIGFX::ORIGIN_VIEWITEM m_viewAxis
Definition: grid_helper.h:134
bool m_diagonalAuxAxesEnable
If true, use the aux axis for snapping as well.
Definition: grid_helper.h:127
KIGFX::VIEW * GetView() const
Function GetView() Returns a pointer to the VIEW instance used in the panel.
void SetPosition(const VECTOR2D &aPosition)
PCB_BASE_FRAME * m_frame
Definition: grid_helper.h:124
boost::optional< T > OPT
Definition: optional.h:7
void SetVisible(VIEW_ITEM *aItem, bool aIsVisible=true)
Sets the item visibility.
Definition: view.cpp:1484
EDA_DRAW_PANEL_GAL * GetGalCanvas() const
Return a pointer to GAL-based canvas of given EDA draw frame.
Definition: draw_frame.h:925
Class VIEW.
Definition: view.h:61
void GRID_HELPER::SetGrid ( int  aSize)

Definition at line 76 of file grid_helper.cpp.

77 {
78  assert( false );
79 }
void GRID_HELPER::SetOrigin ( const VECTOR2I aOrigin)

Definition at line 82 of file grid_helper.cpp.

83 {
84  assert( false );
85 }
void GRID_HELPER::SetSnap ( bool  aSnap)
inline
void GRID_HELPER::SetUseGrid ( bool  aGrid = true)
inline

Member Data Documentation

std::vector<ANCHOR> GRID_HELPER::m_anchors
private

Definition at line 105 of file grid_helper.h.

Referenced by nearestAnchor().

OPT<VECTOR2I> GRID_HELPER::m_auxAxis
private

Definition at line 125 of file grid_helper.h.

Referenced by Align(), and SetAuxAxes().

bool GRID_HELPER::m_diagonalAuxAxesEnable
private

If true, use the aux axis for snapping as well.

Definition at line 127 of file grid_helper.h.

Referenced by GRID_HELPER(), and SetAuxAxes().

bool GRID_HELPER::m_enableGrid
private

If true, allow snapping to grid.

Definition at line 129 of file grid_helper.h.

Referenced by Align(), BestSnapAnchor(), GRID_HELPER(), and SetUseGrid().

bool GRID_HELPER::m_enableSnap
private

If true, allow snapping to other items on the layers.

Definition at line 128 of file grid_helper.h.

Referenced by AlignToSegment(), BestSnapAnchor(), GRID_HELPER(), and SetSnap().

PCB_BASE_FRAME* GRID_HELPER::m_frame
private
ANCHOR* GRID_HELPER::m_snapItem
private

Pointer to the currently snapped item in m_anchors (NULL if not snapped)

Definition at line 131 of file grid_helper.h.

Referenced by BestSnapAnchor(), and GetSnapped().

int GRID_HELPER::m_snapSize
private

Sets the radius in screen units for snapping to items.

Definition at line 130 of file grid_helper.h.

Referenced by BestSnapAnchor(), and GRID_HELPER().

KIGFX::ORIGIN_VIEWITEM GRID_HELPER::m_viewAxis
private

Definition at line 134 of file grid_helper.h.

Referenced by GRID_HELPER(), and SetAuxAxes().

KIGFX::ORIGIN_VIEWITEM GRID_HELPER::m_viewSnapPoint
private

Definition at line 133 of file grid_helper.h.

Referenced by BestSnapAnchor(), and GRID_HELPER().


The documentation for this class was generated from the following files: