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, const bool aFrom=false)
 computeAnchors inserts the local anchor points in to the grid helper for the specified board item, given the reference point and the direction of use for the point. More...
 
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

◆ ANCHOR_FLAGS

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::GRID_HELPER ( PCB_BASE_FRAME aFrame)

Definition at line 48 of file grid_helper.cpp.

48  :
49  m_frame( aFrame )
50 {
52  m_enableSnap = true;
53  m_enableGrid = true;
54  m_snapSize = 100;
56 
57  m_viewAxis.SetSize( 20000 );
59  m_viewAxis.SetColor( COLOR4D( 1.0, 1.0, 1.0, 0.4 ) );
60  m_viewAxis.SetDrawAtZero( true );
61  view->Add( &m_viewAxis );
62  view->SetVisible( &m_viewAxis, false );
63 
65  m_viewSnapPoint.SetColor( COLOR4D( 1.0, 1.0, 1.0, 1.0 ) );
67  view->Add( &m_viewSnapPoint );
68  view->SetVisible( &m_viewSnapPoint, false );
69 }
EDA_DRAW_PANEL_GAL * GetGalCanvas() const
Return a pointer to GAL-based canvas of given EDA draw frame.
Definition: draw_frame.h:935
KIGFX::ORIGIN_VIEWITEM m_viewAxis
Definition: grid_helper.h:142
bool m_diagonalAuxAxesEnable
If true, use the aux axis for snapping as well.
Definition: grid_helper.h:135
KIGFX::ORIGIN_VIEWITEM m_viewSnapPoint
Definition: grid_helper.h:141
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:136
KIGFX::VIEW * GetView() const
Function GetView() Returns a pointer to the VIEW instance used in the panel.
PCB_BASE_FRAME * m_frame
Definition: grid_helper.h:132
int m_snapSize
Sets the radius in screen units for snapping to items.
Definition: grid_helper.h:138
void SetColor(const KIGFX::COLOR4D &aColor)
void SetVisible(VIEW_ITEM *aItem, bool aIsVisible=true)
Sets the item visibility.
Definition: view.cpp:1485
bool m_enableGrid
If true, allow snapping to grid.
Definition: grid_helper.h:137
virtual void Add(VIEW_ITEM *aItem, int aDrawPriority=-1)
Function Add() Adds a VIEW_ITEM to the view.
Definition: view.cpp:346
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

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().

◆ ~GRID_HELPER()

GRID_HELPER::~GRID_HELPER ( )

Definition at line 72 of file grid_helper.cpp.

73 {
74 }

Member Function Documentation

◆ addAnchor()

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.

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

References m_anchors.

Referenced by computeAnchors().

◆ Align()

VECTOR2I GRID_HELPER::Align ( const VECTOR2I aPoint) const

Definition at line 125 of file grid_helper.cpp.

126 {
127  if( !m_enableGrid )
128  return aPoint;
129 
130  const VECTOR2D gridOffset( GetOrigin() );
131  const VECTOR2D gridSize( GetGrid() );
132 
133  VECTOR2I nearest( KiROUND( ( aPoint.x - gridOffset.x ) / gridSize.x ) * gridSize.x + gridOffset.x,
134  KiROUND( ( aPoint.y - gridOffset.y ) / gridSize.y ) * gridSize.y + gridOffset.y );
135 
136  if( !m_auxAxis )
137  return nearest;
138 
139  if( std::abs( m_auxAxis->x - aPoint.x ) < std::abs( nearest.x - aPoint.x ) )
140  nearest.x = m_auxAxis->x;
141 
142  if( std::abs( m_auxAxis->y - aPoint.y ) < std::abs( nearest.y - aPoint.y ) )
143  nearest.y = m_auxAxis->y;
144 
145  return nearest;
146 }
OPT< VECTOR2I > m_auxAxis
Definition: grid_helper.h:133
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 GetOrigin() const
Definition: grid_helper.cpp:99
#define abs(a)
Definition: auxiliary.h:84
VECTOR2I GetGrid() const
Definition: grid_helper.cpp:89
bool m_enableGrid
If true, allow snapping to grid.
Definition: grid_helper.h:137

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

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

◆ AlignToSegment()

VECTOR2I GRID_HELPER::AlignToSegment ( const VECTOR2I aPoint,
const SEG aSeg 
)

Definition at line 149 of file grid_helper.cpp.

150 {
151  OPT_VECTOR2I pts[6];
152 
153  if( !m_enableSnap )
154  return aPoint;
155 
156  const VECTOR2D gridOffset( GetOrigin() );
157  const VECTOR2D gridSize( GetGrid() );
158 
159  VECTOR2I nearest( KiROUND( ( aPoint.x - gridOffset.x ) / gridSize.x ) * gridSize.x + gridOffset.x,
160  KiROUND( ( aPoint.y - gridOffset.y ) / gridSize.y ) * gridSize.y + gridOffset.y );
161 
162  pts[0] = aSeg.A;
163  pts[1] = aSeg.B;
164  pts[2] = aSeg.IntersectLines( SEG( nearest + VECTOR2I( -1, 1 ), nearest + VECTOR2I( 1, -1 ) ) );
165  pts[3] = aSeg.IntersectLines( SEG( nearest + VECTOR2I( -1, -1 ), nearest + VECTOR2I( 1, 1 ) ) );
166 
167  int min_d = std::numeric_limits<int>::max();
168 
169  for( int i = 0; i < 4; i++ )
170  {
171  if( pts[i] && aSeg.Contains( *pts[i] ) )
172  {
173  int d = (*pts[i] - aPoint).EuclideanNorm();
174 
175  if( d < min_d )
176  {
177  min_d = d;
178  nearest = *pts[i];
179  }
180  }
181  }
182 
183  return nearest;
184 }
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
VECTOR2I GetOrigin() const
Definition: grid_helper.cpp:99
OPT_VECTOR2I IntersectLines(const SEG &aSeg) const
Function IntersectLines()
Definition: seg.h:183
VECTOR2< int > VECTOR2I
Definition: vector2d.h:587
VECTOR2I GetGrid() const
Definition: grid_helper.cpp:89
OPT< VECTOR2I > OPT_VECTOR2I
Definition: seg.h:34
bool Contains(const VECTOR2I &aP) const
Definition: seg.cpp:188
bool m_enableSnap
If true, allow snapping to other items on the layers.
Definition: grid_helper.h:136
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
VECTOR2I B
Definition: seg.h:45

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().

◆ BestDragOrigin()

VECTOR2I GRID_HELPER::BestDragOrigin ( const VECTOR2I aMousePos,
BOARD_ITEM aItem 
)

Definition at line 187 of file grid_helper.cpp.

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

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().

◆ BestSnapAnchor() [1/2]

VECTOR2I GRID_HELPER::BestSnapAnchor ( const VECTOR2I aOrigin,
BOARD_ITEM aDraggedItem 
)

Definition at line 259 of file grid_helper.cpp.

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

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().

◆ BestSnapAnchor() [2/2]

VECTOR2I GRID_HELPER::BestSnapAnchor ( const VECTOR2I aOrigin,
const LSET aLayers,
const std::vector< BOARD_ITEM * >  aSkip = {} 
)

Definition at line 276 of file grid_helper.cpp.

278 {
279  double worldScale = m_frame->GetGalCanvas()->GetGAL()->GetWorldScale();
280  int snapRange = (int) ( m_snapSize / worldScale );
281 
282  BOX2I bb( VECTOR2I( aOrigin.x - snapRange / 2, aOrigin.y - snapRange / 2 ), VECTOR2I( snapRange, snapRange ) );
283 
284  clearAnchors();
285 
286  for( BOARD_ITEM* item : queryVisible( bb, aSkip ) )
287  {
288  computeAnchors( item, aOrigin );
289  }
290 
291  ANCHOR* nearest = nearestAnchor( aOrigin, SNAPPABLE, aLayers );
292  VECTOR2I nearestGrid = Align( aOrigin );
293  double gridDist = ( nearestGrid - aOrigin ).EuclideanNorm();
294 
295  if( nearest && m_enableSnap )
296  {
297  double snapDist = nearest->Distance( aOrigin );
298 
299  if( !m_enableGrid || snapDist <= gridDist )
300  {
301  m_viewSnapPoint.SetPosition( nearest->pos );
302 
305  else
307 
308  m_snapItem = nearest;
309  return nearest->pos;
310  }
311  }
312 
313  m_snapItem = nullptr;
315  return nearestGrid;
316 }
double EuclideanNorm(const wxPoint &vector)
Euclidean norm of a 2D vector.
Definition: trigo.h:112
void computeAnchors(BOARD_ITEM *aItem, const VECTOR2I &aRefPos, const bool aFrom=false)
computeAnchors inserts the local anchor points in to the grid helper for the specified board item,...
ANCHOR * m_snapItem
Pointer to the currently snapped item in m_anchors (NULL if not snapped)
Definition: grid_helper.h:139
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
EDA_DRAW_PANEL_GAL * GetGalCanvas() const
Return a pointer to GAL-based canvas of given EDA draw frame.
Definition: draw_frame.h:935
Class VECTOR2 defines a general 2D-vector/point.
Definition: vector2d.h:61
VECTOR2< int > VECTOR2I
Definition: vector2d.h:587
KIGFX::ORIGIN_VIEWITEM m_viewSnapPoint
Definition: grid_helper.h:141
ANCHOR * nearestAnchor(const VECTOR2I &aPos, int aFlags, LSET aMatchLayers)
void SetPosition(const VECTOR2D &aPosition)
bool IsVisible(const VIEW_ITEM *aItem) const
Returns information if the item is visible (or not).
Definition: view.cpp:1525
KIGFX::GAL * GetGAL() const
Function GetGAL() Returns a pointer to the GAL instance used in the panel.
bool m_enableSnap
If true, allow snapping to other items on the layers.
Definition: grid_helper.h:136
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:1539
VECTOR2I Align(const VECTOR2I &aPoint) const
KIGFX::VIEW * GetView() const
Function GetView() Returns a pointer to the VIEW instance used in the panel.
PCB_BASE_FRAME * m_frame
Definition: grid_helper.h:132
int m_snapSize
Sets the radius in screen units for snapping to items.
Definition: grid_helper.h:138
void SetVisible(VIEW_ITEM *aItem, bool aIsVisible=true)
Sets the item visibility.
Definition: view.cpp:1485
bool m_enableGrid
If true, allow snapping to grid.
Definition: grid_helper.h:137
double GetWorldScale() const
Get the world scale.
void clearAnchors()
Definition: grid_helper.h:127
Color has changed.
Definition: view_item.h:57

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.

◆ clearAnchors()

void GRID_HELPER::clearAnchors ( )
inlineprivate

Definition at line 127 of file grid_helper.h.

128  {
129  m_anchors.clear();
130  }
std::vector< ANCHOR > m_anchors
Definition: grid_helper.h:105

References m_anchors.

Referenced by BestDragOrigin(), and BestSnapAnchor().

◆ computeAnchors()

void GRID_HELPER::computeAnchors ( BOARD_ITEM aItem,
const VECTOR2I aRefPos,
const bool  aFrom = false 
)
private

computeAnchors inserts the local anchor points in to the grid helper for the specified board item, given the reference point and the direction of use for the point.

Parameters
aItemThe board item for which to compute the anchors
aRefPosThe point for which to compute the anchors (if used by the component)
aFromIs this for an anchor that is designating a source point (aFrom=true) or not

Definition at line 328 of file grid_helper.cpp.

329 {
330  VECTOR2I origin;
331 
332  switch( aItem->Type() )
333  {
334  case PCB_MODULE_T:
335  {
336  MODULE* mod = static_cast<MODULE*>( aItem );
337 
338  for( auto pad : mod->Pads() )
339  {
340  if( ( aFrom || m_frame->Settings().m_magneticPads == CAPTURE_ALWAYS ) &&
341  pad->GetBoundingBox().Contains( wxPoint( aRefPos.x, aRefPos.y ) ) )
342  {
343  addAnchor( pad->GetPosition(), CORNER | SNAPPABLE, pad );
344  break;
345  }
346  }
347 
348  // if the cursor is not over a pad, then drag the module by its origin
349  addAnchor( mod->GetPosition(), ORIGIN | SNAPPABLE, mod );
350  break;
351  }
352 
353  case PCB_PAD_T:
354  {
355  if( aFrom || m_frame->Settings().m_magneticPads == CAPTURE_ALWAYS )
356  {
357  D_PAD* pad = static_cast<D_PAD*>( aItem );
358  addAnchor( pad->GetPosition(), CORNER | SNAPPABLE, pad );
359  }
360 
361  break;
362  }
363 
364  case PCB_MODULE_EDGE_T:
365  case PCB_LINE_T:
366  {
368  break;
369 
370  DRAWSEGMENT* dseg = static_cast<DRAWSEGMENT*>( aItem );
371  VECTOR2I start = dseg->GetStart();
372  VECTOR2I end = dseg->GetEnd();
373 
374  switch( dseg->GetShape() )
375  {
376  case S_CIRCLE:
377  {
378  int r = ( start - end ).EuclideanNorm();
379 
380  addAnchor( start, ORIGIN | SNAPPABLE, dseg );
381  addAnchor( start + VECTOR2I( -r, 0 ), OUTLINE | SNAPPABLE, dseg );
382  addAnchor( start + VECTOR2I( r, 0 ), OUTLINE | SNAPPABLE, dseg );
383  addAnchor( start + VECTOR2I( 0, -r ), OUTLINE | SNAPPABLE, dseg );
384  addAnchor( start + VECTOR2I( 0, r ), OUTLINE | SNAPPABLE, dseg );
385  break;
386  }
387 
388  case S_ARC:
389  origin = dseg->GetCenter();
390  addAnchor( dseg->GetArcStart(), CORNER | SNAPPABLE, dseg );
391  addAnchor( dseg->GetArcEnd(), CORNER | SNAPPABLE, dseg );
392  addAnchor( origin, ORIGIN | SNAPPABLE, dseg );
393  break;
394 
395  case S_RECT:
396  addAnchor( start, CORNER | SNAPPABLE, dseg );
397  addAnchor( VECTOR2I( end.x, start.y ), CORNER | SNAPPABLE, dseg );
398  addAnchor( VECTOR2I( start.x, end.y ), CORNER | SNAPPABLE, dseg );
399  addAnchor( end, CORNER | SNAPPABLE, dseg );
400  break;
401 
402  case S_SEGMENT:
403  origin.x = start.x + ( start.x - end.x ) / 2;
404  origin.y = start.y + ( start.y - end.y ) / 2;
405  addAnchor( start, CORNER | SNAPPABLE, dseg );
406  addAnchor( end, CORNER | SNAPPABLE, dseg );
407  addAnchor( origin, ORIGIN, dseg );
408  break;
409 
410  case S_POLYGON:
411  for( const auto& p : dseg->BuildPolyPointsList() )
412  addAnchor( p, CORNER | SNAPPABLE, dseg );
413 
414  break;
415 
416  case S_CURVE:
417  addAnchor( start, CORNER | SNAPPABLE, dseg );
418  addAnchor( end, CORNER | SNAPPABLE, dseg );
419  //Fallthrough
420  default:
421  origin = dseg->GetStart();
422  addAnchor( origin, ORIGIN | SNAPPABLE, dseg );
423  break;
424  }
425  break;
426  }
427 
428  case PCB_TRACE_T:
429  {
430  if( aFrom || m_frame->Settings().m_magneticTracks == CAPTURE_ALWAYS )
431  {
432  TRACK* track = static_cast<TRACK*>( aItem );
433  VECTOR2I start = track->GetStart();
434  VECTOR2I end = track->GetEnd();
435  origin.x = start.x + ( start.x - end.x ) / 2;
436  origin.y = start.y + ( start.y - end.y ) / 2;
437  addAnchor( start, CORNER | SNAPPABLE, track );
438  addAnchor( end, CORNER | SNAPPABLE, track );
439  addAnchor( origin, ORIGIN, track);
440  }
441 
442  break;
443  }
444 
445  case PCB_MARKER_T:
446  case PCB_TARGET_T:
447  addAnchor( aItem->GetPosition(), ORIGIN | CORNER | SNAPPABLE, aItem );
448  break;
449 
450  case PCB_VIA_T:
451  {
452  if( aFrom || m_frame->Settings().m_magneticTracks == CAPTURE_ALWAYS )
453  addAnchor( aItem->GetPosition(), ORIGIN | CORNER | SNAPPABLE, aItem );
454 
455  break;
456  }
457 
458  case PCB_ZONE_AREA_T:
459  {
460  const SHAPE_POLY_SET* outline = static_cast<const ZONE_CONTAINER*>( aItem )->Outline();
461 
462  SHAPE_LINE_CHAIN lc;
463  lc.SetClosed( true );
464 
465  for( auto iter = outline->CIterateWithHoles(); iter; iter++ )
466  {
467  addAnchor( *iter, CORNER, aItem );
468  lc.Append( *iter );
469  }
470 
471  addAnchor( lc.NearestPoint( aRefPos ), OUTLINE, aItem );
472 
473  break;
474  }
475 
476  case PCB_DIMENSION_T:
477  {
478  const DIMENSION* dim = static_cast<const DIMENSION*>( aItem );
479  addAnchor( dim->m_crossBarF, CORNER | SNAPPABLE, aItem );
480  addAnchor( dim->m_crossBarO, CORNER | SNAPPABLE, aItem );
481  addAnchor( dim->m_featureLineGO, CORNER | SNAPPABLE, aItem );
482  addAnchor( dim->m_featureLineDO, CORNER | SNAPPABLE, aItem );
483  break;
484  }
485 
486  case PCB_MODULE_TEXT_T:
487  case PCB_TEXT_T:
488  addAnchor( aItem->GetPosition(), ORIGIN, aItem );
489  break;
490 
491  default:
492  break;
493  }
494 }
double EuclideanNorm(const wxPoint &vector)
Euclidean norm of a 2D vector.
Definition: trigo.h:112
const VECTOR2I NearestPoint(const VECTOR2I &aP) const
Function NearestPoint()
wxPoint m_crossBarF
const wxPoint GetCenter() const override
Function GetCenter()
const wxPoint & GetStart() const
Definition: class_track.h:126
STROKE_T GetShape() const
const wxPoint GetArcEnd() const
polygon (not yet used for tracks, but could be in microwave apps)
class ZONE_CONTAINER, a zone area
Definition: typeinfo.h:102
Class VECTOR2 defines a general 2D-vector/point.
Definition: vector2d.h:61
MAGNETIC_PAD_OPTION_VALUES m_magneticPads
CONST_ITERATOR CIterateWithHoles(int aOutline) const
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
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...
VECTOR2< int > VECTOR2I
Definition: vector2d.h:587
const wxPoint & GetEnd() const
Function GetEnd returns the ending point of the graphic.
class EDGE_MODULE, a footprint edge
Definition: typeinfo.h:94
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()
wxPoint m_featureLineGO
MAGNETIC_PAD_OPTION_VALUES m_magneticTracks
segment with non rounded ends
class MODULE, a footprint
Definition: typeinfo.h:89
void SetClosed(bool aClosed)
Function SetClosed()
Class SHAPE_POLY_SET.
SHAPE_LINE_CHAIN & Outline(int aIndex)
Returns the reference to aIndex-th outline in the set
PCB_GENERAL_SETTINGS & Settings()
Arcs (with rounded ends)
const wxPoint & GetArcStart() const
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.
PCB_BASE_FRAME * m_frame
Definition: grid_helper.h:132
const wxPoint & GetEnd() const
Definition: class_track.h:123
wxPoint m_crossBarO
class VIA, a via (like a track segment on a copper layer)
Definition: typeinfo.h:96
const wxPoint & GetStart() const
Function GetStart returns the starting point of the graphic.
const wxPoint GetPosition() const override
Definition: class_pad.h:220
class DRAWSEGMENT, a segment not on copper layers
Definition: typeinfo.h:91
wxPoint m_featureLineDO
#define mod(a, n)
Definition: greymap.cpp:24
Class DIMENSION.
KICAD_T Type() const
Function Type()
Definition: base_struct.h:201

References addAnchor(), SHAPE_LINE_CHAIN::Append(), DRAWSEGMENT::BuildPolyPointsList(), CAPTURE_ALWAYS, SHAPE_POLY_SET::CIterateWithHoles(), CORNER, EuclideanNorm(), DRAWSEGMENT::GetArcEnd(), DRAWSEGMENT::GetArcStart(), DRAWSEGMENT::GetCenter(), DRAWSEGMENT::GetEnd(), TRACK::GetEnd(), BOARD_ITEM::GetPosition(), D_PAD::GetPosition(), DRAWSEGMENT::GetShape(), DRAWSEGMENT::GetStart(), TRACK::GetStart(), DIMENSION::m_crossBarF, DIMENSION::m_crossBarO, DIMENSION::m_featureLineDO, DIMENSION::m_featureLineGO, m_frame, PCB_GENERAL_SETTINGS::m_magneticGraphics, PCB_GENERAL_SETTINGS::m_magneticPads, PCB_GENERAL_SETTINGS::m_magneticTracks, mod, SHAPE_LINE_CHAIN::NearestPoint(), ORIGIN, OUTLINE, SHAPE_POLY_SET::Outline(), 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(), PCB_BASE_FRAME::Settings(), SNAPPABLE, EDA_ITEM::Type(), VECTOR2< T >::x, and VECTOR2< T >::y.

Referenced by BestDragOrigin(), and BestSnapAnchor().

◆ GetGrid()

VECTOR2I GRID_HELPER::GetGrid ( ) const

Definition at line 89 of file grid_helper.cpp.

90 {
91  PCB_SCREEN* screen = m_frame->GetScreen();
92 
93  const wxRealPoint& size = screen->GetGridSize();
94 
95  return VECTOR2I( KiROUND( size.x ), KiROUND( size.y ) );
96 }
const wxRealPoint & GetGridSize() const
Return the grid size of the currently selected grid.
Definition: base_screen.h:410
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:132
PCB_SCREEN * GetScreen() const override
Return a pointer to a BASE_SCREEN or one of its derivatives.

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

Referenced by Align(), and AlignToSegment().

◆ GetOrigin()

VECTOR2I GRID_HELPER::GetOrigin ( ) const

Definition at line 99 of file grid_helper.cpp.

100 {
101  return VECTOR2I( m_frame->GetGridOrigin() );
102 }
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:132

References PCB_BASE_FRAME::GetGridOrigin(), and m_frame.

Referenced by Align(), and AlignToSegment().

◆ GetSnapped()

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 319 of file grid_helper.cpp.

320 {
321  if( !m_snapItem )
322  return nullptr;
323 
324  return m_snapItem->item;
325 }
ANCHOR * m_snapItem
Pointer to the currently snapped item in m_anchors (NULL if not snapped)
Definition: grid_helper.h:139
BOARD_ITEM * item
Definition: grid_helper.h:97

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

Referenced by DRAWING_TOOL::drawSegment().

◆ nearestAnchor()

GRID_HELPER::ANCHOR * GRID_HELPER::nearestAnchor ( const VECTOR2I aPos,
int  aFlags,
LSET  aMatchLayers 
)
private

Definition at line 497 of file grid_helper.cpp.

498 {
499  double minDist = std::numeric_limits<double>::max();
500  ANCHOR* best = NULL;
501 
502  for( ANCHOR& a : m_anchors )
503  {
504  if( ( aMatchLayers & a.item->GetLayerSet() ) == 0 )
505  continue;
506 
507  if( ( aFlags & a.flags ) != aFlags )
508  continue;
509 
510  double dist = a.Distance( aPos );
511 
512  if( dist < minDist )
513  {
514  minDist = dist;
515  best = &a;
516  }
517  }
518 
519  return best;
520 }
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

References dist, m_anchors, and max.

Referenced by BestDragOrigin(), and BestSnapAnchor().

◆ queryVisible()

std::set< BOARD_ITEM * > GRID_HELPER::queryVisible ( const BOX2I aArea,
const std::vector< BOARD_ITEM * >  aSkip 
) const
private

Definition at line 230 of file grid_helper.cpp.

232 {
233  std::set<BOARD_ITEM*> items;
234  std::vector<KIGFX::VIEW::LAYER_ITEM_PAIR> selectedItems;
235 
236  auto view = m_frame->GetGalCanvas()->GetView();
237  auto activeLayers = view->GetPainter()->GetSettings()->GetActiveLayers();
238  bool isHighContrast = view->GetPainter()->GetSettings()->GetHighContrast();
239  view->Query( aArea, selectedItems );
240 
241  for( auto it : selectedItems )
242  {
243  BOARD_ITEM* item = static_cast<BOARD_ITEM*>( it.first );
244 
245  // The item must be visible and on an active layer
246  if( view->IsVisible( item )
247  && ( !isHighContrast || activeLayers.count( it.second ) ) )
248  items.insert ( item );
249  }
250 
251 
252  for( auto ii : aSkip )
253  items.erase( ii );
254 
255  return items;
256 }
Class BOARD_ITEM is a base class for any item which can be embedded within the BOARD container class,...
EDA_DRAW_PANEL_GAL * GetGalCanvas() const
Return a pointer to GAL-based canvas of given EDA draw frame.
Definition: draw_frame.h:935
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...
KIGFX::VIEW * GetView() const
Function GetView() Returns a pointer to the VIEW instance used in the panel.
PCB_BASE_FRAME * m_frame
Definition: grid_helper.h:132
const std::set< unsigned int > GetActiveLayers()
Function GetActiveLayers() Returns the set of currently active layers.
Definition: painter.h:89

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().

◆ SetAuxAxes()

void GRID_HELPER::SetAuxAxes ( bool  aEnable,
const VECTOR2I aOrigin = VECTOR2I( 0, 0 ),
bool  aEnableDiagonal = false 
)

Definition at line 105 of file grid_helper.cpp.

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

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 ROUTER_TOOL::InlineDrag(), EDIT_TOOL::Main(), POINT_EDITOR::OnSelectionChange(), and ROUTER_TOOL::performDragging().

◆ SetGrid()

void GRID_HELPER::SetGrid ( int  aSize)

Definition at line 77 of file grid_helper.cpp.

78 {
79  assert( false );
80 }

◆ SetOrigin()

void GRID_HELPER::SetOrigin ( const VECTOR2I aOrigin)

Definition at line 83 of file grid_helper.cpp.

84 {
85  assert( false );
86 }

◆ SetSnap()

void GRID_HELPER::SetSnap ( bool  aSnap)
inline

◆ SetUseGrid()

void GRID_HELPER::SetUseGrid ( bool  aGrid = true)
inline

Member Data Documentation

◆ m_anchors

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

Definition at line 105 of file grid_helper.h.

Referenced by addAnchor(), clearAnchors(), and nearestAnchor().

◆ m_auxAxis

OPT<VECTOR2I> GRID_HELPER::m_auxAxis
private

Definition at line 133 of file grid_helper.h.

Referenced by Align(), and SetAuxAxes().

◆ m_diagonalAuxAxesEnable

bool GRID_HELPER::m_diagonalAuxAxesEnable
private

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

Definition at line 135 of file grid_helper.h.

Referenced by GRID_HELPER(), and SetAuxAxes().

◆ m_enableGrid

bool GRID_HELPER::m_enableGrid
private

If true, allow snapping to grid.

Definition at line 137 of file grid_helper.h.

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

◆ m_enableSnap

bool GRID_HELPER::m_enableSnap
private

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

Definition at line 136 of file grid_helper.h.

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

◆ m_frame

PCB_BASE_FRAME* GRID_HELPER::m_frame
private

◆ m_snapItem

ANCHOR* GRID_HELPER::m_snapItem
private

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

Definition at line 139 of file grid_helper.h.

Referenced by BestSnapAnchor(), and GetSnapped().

◆ m_snapSize

int GRID_HELPER::m_snapSize
private

Sets the radius in screen units for snapping to items.

Definition at line 138 of file grid_helper.h.

Referenced by BestSnapAnchor(), and GRID_HELPER().

◆ m_viewAxis

KIGFX::ORIGIN_VIEWITEM GRID_HELPER::m_viewAxis
private

Definition at line 142 of file grid_helper.h.

Referenced by GRID_HELPER(), and SetAuxAxes().

◆ m_viewSnapPoint

KIGFX::ORIGIN_VIEWITEM GRID_HELPER::m_viewSnapPoint
private

Definition at line 141 of file grid_helper.h.

Referenced by BestSnapAnchor(), and GRID_HELPER().


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