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 ()
 
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))
 
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, BOARD_ITEM *aItem)
 
ANCHORnearestAnchor (const VECTOR2I &aPos, int aFlags, LSET aMatchLayers)
 
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, 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_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 37 of file grid_helper.h.

Member Enumeration Documentation

◆ ANCHOR_FLAGS

Enumerator
CORNER 
OUTLINE 
SNAPPABLE 
ORIGIN 

Definition at line 77 of file grid_helper.h.

77  {
78  CORNER = 0x1,
79  OUTLINE = 0x2,
80  SNAPPABLE = 0x4,
81  ORIGIN = 0x8
82  };

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 {
51  m_enableSnap = true;
52  m_enableGrid = true;
53  m_snapSize = 100;
54  KIGFX::VIEW* view = m_frame->GetCanvas()->GetView();
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 }
PCB_DRAW_PANEL_GAL * GetCanvas() const override
Return a pointer to GAL-based canvas of given EDA draw frame.
KIGFX::ORIGIN_VIEWITEM m_viewAxis
Definition: grid_helper.h:138
KIGFX::ORIGIN_VIEWITEM m_viewSnapPoint
Definition: grid_helper.h:137
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:132
virtual KIGFX::PCB_VIEW * GetView() const override
Function GetView() Returns a pointer to the VIEW instance used in the panel.
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
void SetColor(const KIGFX::COLOR4D &aColor)
void SetVisible(VIEW_ITEM *aItem, bool aIsVisible=true)
Sets the item visibility.
Definition: view.cpp:1486
bool m_enableGrid
If true, allow snapping to grid.
Definition: grid_helper.h:133
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, PCB_BASE_FRAME::GetCanvas(), PCB_DRAW_PANEL_GAL::GetView(), 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 71 of file grid_helper.cpp.

72 {
73 }

Member Function Documentation

◆ addAnchor()

void GRID_HELPER::addAnchor ( const VECTOR2I aPos,
int  aFlags,
BOARD_ITEM aItem 
)
inlineprivate

Definition at line 107 of file grid_helper.h.

108  {
109  m_anchors.emplace_back( ANCHOR( aPos, aFlags, aItem ) );
110  }
std::vector< ANCHOR > m_anchors
Definition: grid_helper.h:102

References m_anchors.

Referenced by computeAnchors().

◆ Align()

VECTOR2I GRID_HELPER::Align ( const VECTOR2I aPoint) const

Definition at line 108 of file grid_helper.cpp.

109 {
110  if( !m_enableGrid )
111  return aPoint;
112 
113  const VECTOR2D gridOffset( GetOrigin() );
114  const VECTOR2D grid( GetGrid() );
115 
116  VECTOR2I nearest( KiROUND( ( aPoint.x - gridOffset.x ) / grid.x ) * grid.x + gridOffset.x,
117  KiROUND( ( aPoint.y - gridOffset.y ) / grid.y ) * grid.y + gridOffset.y );
118 
119  if( !m_auxAxis )
120  return nearest;
121 
122  if( std::abs( m_auxAxis->x - aPoint.x ) < std::abs( nearest.x - aPoint.x ) )
123  nearest.x = m_auxAxis->x;
124 
125  if( std::abs( m_auxAxis->y - aPoint.y ) < std::abs( nearest.y - aPoint.y ) )
126  nearest.y = m_auxAxis->y;
127 
128  return nearest;
129 }
OPT< VECTOR2I > m_auxAxis
Definition: grid_helper.h:130
static int KiROUND(double v)
Round a floating point number to an integer using "round halfway cases away from zero".
Definition: common.h:115
VECTOR2I GetOrigin() const
Definition: grid_helper.cpp:86
#define abs(a)
Definition: auxiliary.h:84
VECTOR2I GetGrid() const
Definition: grid_helper.cpp:76
bool m_enableGrid
If true, allow snapping to grid.
Definition: grid_helper.h:133

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

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

◆ AlignToSegment()

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

Definition at line 132 of file grid_helper.cpp.

133 {
134  OPT_VECTOR2I pts[6];
135 
136  if( !m_enableSnap )
137  return aPoint;
138 
139  const VECTOR2D gridOffset( GetOrigin() );
140  const VECTOR2D gridSize( GetGrid() );
141 
142  VECTOR2I nearest( KiROUND( ( aPoint.x - gridOffset.x ) / gridSize.x ) * gridSize.x + gridOffset.x,
143  KiROUND( ( aPoint.y - gridOffset.y ) / gridSize.y ) * gridSize.y + gridOffset.y );
144 
145  pts[0] = aSeg.A;
146  pts[1] = aSeg.B;
147  pts[2] = aSeg.IntersectLines( SEG( nearest + VECTOR2I( -1, 1 ), nearest + VECTOR2I( 1, -1 ) ) );
148  pts[3] = aSeg.IntersectLines( SEG( nearest + VECTOR2I( -1, -1 ), nearest + VECTOR2I( 1, 1 ) ) );
149 
150  int min_d = std::numeric_limits<int>::max();
151 
152  for( int i = 0; i < 4; i++ )
153  {
154  if( pts[i] && aSeg.Contains( *pts[i] ) )
155  {
156  int d = (*pts[i] - aPoint).EuclideanNorm();
157 
158  if( d < min_d )
159  {
160  min_d = d;
161  nearest = *pts[i];
162  }
163  }
164  }
165 
166  return nearest;
167 }
double EuclideanNorm(const wxPoint &vector)
Euclidean norm of a 2D vector.
Definition: trigo.h:123
static int KiROUND(double v)
Round a floating point number to an integer using "round halfway cases away from zero".
Definition: common.h:115
VECTOR2I GetOrigin() const
Definition: grid_helper.cpp:86
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:76
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:132
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 170 of file grid_helper.cpp.

171 {
172  clearAnchors();
173  computeAnchors( aItem, aMousePos, true );
174 
175  double worldScale = m_frame->GetCanvas()->GetGAL()->GetWorldScale();
176  double lineSnapMinCornerDistance = 50.0 / worldScale;
177 
178  ANCHOR* nearestOutline = nearestAnchor( aMousePos, OUTLINE, LSET::AllLayersMask() );
179  ANCHOR* nearestCorner = nearestAnchor( aMousePos, CORNER, LSET::AllLayersMask() );
180  ANCHOR* nearestOrigin = nearestAnchor( aMousePos, ORIGIN, LSET::AllLayersMask() );
181  ANCHOR* best = NULL;
182  double minDist = std::numeric_limits<double>::max();
183 
184  if( nearestOrigin )
185  {
186  minDist = nearestOrigin->Distance( aMousePos );
187  best = nearestOrigin;
188  }
189 
190  if( nearestCorner )
191  {
192  double dist = nearestCorner->Distance( aMousePos );
193 
194  if( dist < minDist )
195  {
196  minDist = dist;
197  best = nearestCorner;
198  }
199  }
200 
201  if( nearestOutline )
202  {
203  double dist = nearestOutline->Distance( aMousePos );
204 
205  if( minDist > lineSnapMinCornerDistance && dist < minDist )
206  best = nearestOutline;
207  }
208 
209  return best ? best->pos : aMousePos;
210 }
static const int dist[10][10]
Definition: ar_matrix.cpp:320
PCB_DRAW_PANEL_GAL * GetCanvas() const override
Return a pointer to GAL-based canvas of given EDA draw frame.
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,...
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:129
double GetWorldScale() const
Get the world scale.
void clearAnchors()
Definition: grid_helper.h:124

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

Referenced by EDIT_TOOL::Move().

◆ BestSnapAnchor() [1/2]

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

Definition at line 241 of file grid_helper.cpp.

242 {
243  LSET layers;
244  std::vector<BOARD_ITEM*> item;
245 
246  if( aDraggedItem )
247  {
248  layers = aDraggedItem->GetLayerSet();
249  item.push_back( aDraggedItem );
250  }
251  else
252  layers = LSET::AllLayersMask();
253 
254  return BestSnapAnchor( aOrigin, layers, item );
255 }
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(), PCBNEW_PICKER_TOOL::Main(), EDIT_TOOL::MeasureTool(), EDIT_TOOL::Move(), 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 258 of file grid_helper.cpp.

260 {
261  double worldScale = m_frame->GetCanvas()->GetGAL()->GetWorldScale();
262  int snapRange = (int) ( m_snapSize / worldScale );
263 
264  BOX2I bb( VECTOR2I( aOrigin.x - snapRange / 2, aOrigin.y - snapRange / 2 ), VECTOR2I( snapRange, snapRange ) );
265 
266  clearAnchors();
267 
268  for( BOARD_ITEM* item : queryVisible( bb, aSkip ) )
269  computeAnchors( item, aOrigin );
270 
271  ANCHOR* nearest = nearestAnchor( aOrigin, SNAPPABLE, aLayers );
272  VECTOR2I nearestGrid = Align( aOrigin );
273  double gridDist = ( nearestGrid - aOrigin ).EuclideanNorm();
274 
275  if( nearest && m_enableSnap )
276  {
277  double snapDist = nearest->Distance( aOrigin );
278 
279  if( !m_enableGrid || snapDist <= gridDist )
280  {
281  m_viewSnapPoint.SetPosition( nearest->pos );
282 
285  else
287 
288  m_snapItem = nearest;
289  return nearest->pos;
290  }
291  }
292 
293  m_snapItem = nullptr;
295  return nearestGrid;
296 }
double EuclideanNorm(const wxPoint &vector)
Euclidean norm of a 2D vector.
Definition: trigo.h:123
ANCHOR * m_snapItem
Pointer to the currently snapped item in m_anchors (NULL if not snapped)
Definition: grid_helper.h:135
virtual void Update(VIEW_ITEM *aItem, int aUpdateFlags) override
For dynamic VIEWs, informs the associated VIEW that the graphical representation of this item has cha...
Definition: pcb_view.cpp:91
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
PCB_DRAW_PANEL_GAL * GetCanvas() const override
Return a pointer to GAL-based canvas of given EDA draw frame.
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:137
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,...
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:1526
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:132
virtual KIGFX::PCB_VIEW * GetView() const override
Function GetView() Returns a pointer to the VIEW instance used in the panel.
VECTOR2I Align(const VECTOR2I &aPoint) const
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
void SetVisible(VIEW_ITEM *aItem, bool aIsVisible=true)
Sets the item visibility.
Definition: view.cpp:1486
bool m_enableGrid
If true, allow snapping to grid.
Definition: grid_helper.h:133
double GetWorldScale() const
Get the world scale.
void clearAnchors()
Definition: grid_helper.h:124
Color has changed.
Definition: view_item.h:57

References Align(), clearAnchors(), computeAnchors(), GRID_HELPER::ANCHOR::Distance(), EuclideanNorm(), KIGFX::GEOMETRY, PCB_BASE_FRAME::GetCanvas(), EDA_DRAW_PANEL_GAL::GetGAL(), PCB_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::PCB_VIEW::Update(), VECTOR2< T >::x, and VECTOR2< T >::y.

◆ clearAnchors()

void GRID_HELPER::clearAnchors ( )
inlineprivate

Definition at line 124 of file grid_helper.h.

125  {
126  m_anchors.clear();
127  }
std::vector< ANCHOR > m_anchors
Definition: grid_helper.h:102

References m_anchors.

Referenced by BestDragOrigin(), and BestSnapAnchor().

◆ computeAnchors()

void GRID_HELPER::computeAnchors ( BOARD_ITEM aItem,
const VECTOR2I aRefPos,
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 308 of file grid_helper.cpp.

309 {
310  VECTOR2I origin;
311 
312  switch( aItem->Type() )
313  {
314  case PCB_MODULE_T:
315  {
316  MODULE* mod = static_cast<MODULE*>( aItem );
317 
318  for( auto pad : mod->Pads() )
319  {
320  if( ( aFrom || m_frame->Settings().m_MagneticPads == CAPTURE_ALWAYS )
321  && pad->GetBoundingBox().Contains( wxPoint( aRefPos.x, aRefPos.y ) ) )
322  {
323  addAnchor( pad->GetPosition(), CORNER | SNAPPABLE, pad );
324  break;
325  }
326  }
327 
328  // if the cursor is not over a pad, then drag the module by its origin
329  addAnchor( mod->GetPosition(), ORIGIN | SNAPPABLE, mod );
330  break;
331  }
332 
333  case PCB_PAD_T:
334  {
335  if( aFrom || m_frame->Settings().m_MagneticPads == CAPTURE_ALWAYS )
336  {
337  D_PAD* pad = static_cast<D_PAD*>( aItem );
338  addAnchor( pad->GetPosition(), CORNER | SNAPPABLE, pad );
339  }
340 
341  break;
342  }
343 
344  case PCB_MODULE_EDGE_T:
345  case PCB_LINE_T:
346  {
348  break;
349 
350  DRAWSEGMENT* dseg = static_cast<DRAWSEGMENT*>( aItem );
351  VECTOR2I start = dseg->GetStart();
352  VECTOR2I end = dseg->GetEnd();
353 
354  switch( dseg->GetShape() )
355  {
356  case S_CIRCLE:
357  {
358  int r = ( start - end ).EuclideanNorm();
359 
360  addAnchor( start, ORIGIN | SNAPPABLE, dseg );
361  addAnchor( start + VECTOR2I( -r, 0 ), OUTLINE | SNAPPABLE, dseg );
362  addAnchor( start + VECTOR2I( r, 0 ), OUTLINE | SNAPPABLE, dseg );
363  addAnchor( start + VECTOR2I( 0, -r ), OUTLINE | SNAPPABLE, dseg );
364  addAnchor( start + VECTOR2I( 0, r ), OUTLINE | SNAPPABLE, dseg );
365  break;
366  }
367 
368  case S_ARC:
369  origin = dseg->GetCenter();
370  addAnchor( dseg->GetArcStart(), CORNER | SNAPPABLE, dseg );
371  addAnchor( dseg->GetArcEnd(), CORNER | SNAPPABLE, dseg );
372  addAnchor( origin, ORIGIN | SNAPPABLE, dseg );
373  break;
374 
375  case S_RECT:
376  addAnchor( start, CORNER | SNAPPABLE, dseg );
377  addAnchor( VECTOR2I( end.x, start.y ), CORNER | SNAPPABLE, dseg );
378  addAnchor( VECTOR2I( start.x, end.y ), CORNER | SNAPPABLE, dseg );
379  addAnchor( end, CORNER | SNAPPABLE, dseg );
380  break;
381 
382  case S_SEGMENT:
383  origin.x = start.x + ( start.x - end.x ) / 2;
384  origin.y = start.y + ( start.y - end.y ) / 2;
385  addAnchor( start, CORNER | SNAPPABLE, dseg );
386  addAnchor( end, CORNER | SNAPPABLE, dseg );
387  addAnchor( origin, ORIGIN, dseg );
388  break;
389 
390  case S_POLYGON:
391  for( const auto& p : dseg->BuildPolyPointsList() )
392  addAnchor( p, CORNER | SNAPPABLE, dseg );
393 
394  break;
395 
396  case S_CURVE:
397  addAnchor( start, CORNER | SNAPPABLE, dseg );
398  addAnchor( end, CORNER | SNAPPABLE, dseg );
399  //Fallthrough
400  default:
401  origin = dseg->GetStart();
402  addAnchor( origin, ORIGIN | SNAPPABLE, dseg );
403  break;
404  }
405  break;
406  }
407 
408  case PCB_TRACE_T:
409  {
410  if( aFrom || m_frame->Settings().m_MagneticTracks == CAPTURE_ALWAYS )
411  {
412  TRACK* track = static_cast<TRACK*>( aItem );
413  VECTOR2I start = track->GetStart();
414  VECTOR2I end = track->GetEnd();
415  origin.x = start.x + ( start.x - end.x ) / 2;
416  origin.y = start.y + ( start.y - end.y ) / 2;
417  addAnchor( start, CORNER | SNAPPABLE, track );
418  addAnchor( end, CORNER | SNAPPABLE, track );
419  addAnchor( origin, ORIGIN, track);
420  }
421 
422  break;
423  }
424 
425  case PCB_MARKER_T:
426  case PCB_TARGET_T:
427  addAnchor( aItem->GetPosition(), ORIGIN | CORNER | SNAPPABLE, aItem );
428  break;
429 
430  case PCB_VIA_T:
431  {
432  if( aFrom || m_frame->Settings().m_MagneticTracks == CAPTURE_ALWAYS )
433  addAnchor( aItem->GetPosition(), ORIGIN | CORNER | SNAPPABLE, aItem );
434 
435  break;
436  }
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:123
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:109
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:100
Class VECTOR2 defines a general 2D-vector/point.
Definition: vector2d.h:61
MAGNETIC_OPTIONS m_MagneticTracks
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
segment with non rounded ends
class MODULE, a footprint
Definition: typeinfo.h:89
void SetClosed(bool aClosed)
Function SetClosed()
MAGNETIC_OPTIONS m_MagneticPads
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:98
class PCB_TARGET, a target (graphic item)
Definition: typeinfo.h:99
virtual const wxPoint GetPosition() const =0
class TEXTE_MODULE, text in a footprint
Definition: typeinfo.h:93
class MARKER_PCB, a marker used to show something
Definition: typeinfo.h:97
Class SHAPE_LINE_CHAIN.
PCB_BASE_FRAME * m_frame
Definition: grid_helper.h:129
const wxPoint & GetEnd() const
Definition: class_track.h:106
void addAnchor(const VECTOR2I &aPos, int aFlags, BOARD_ITEM *aItem)
Definition: grid_helper.h:107
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:225
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:210

References addAnchor(), SHAPE_LINE_CHAIN::Append(), DRAWSEGMENT::BuildPolyPointsList(), CAPTURE_ALWAYS, SHAPE_POLY_SET::CIterateWithHoles(), CORNER, EuclideanNorm(), DRAWSEGMENT::GetArcEnd(), DRAWSEGMENT::GetArcStart(), DRAWSEGMENT::GetCenter(), TRACK::GetEnd(), DRAWSEGMENT::GetEnd(), BOARD_ITEM::GetPosition(), D_PAD::GetPosition(), DRAWSEGMENT::GetShape(), TRACK::GetStart(), DRAWSEGMENT::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 76 of file grid_helper.cpp.

77 {
78  PCB_SCREEN* screen = m_frame->GetScreen();
79 
80  const wxRealPoint& size = screen->GetGridSize();
81 
82  return VECTOR2I( KiROUND( size.x ), KiROUND( size.y ) );
83 }
const wxRealPoint & GetGridSize() const
Return the grid size of the currently selected grid.
Definition: base_screen.h:279
static int KiROUND(double v)
Round a floating point number to an integer using "round halfway cases away from zero".
Definition: common.h:115
VECTOR2< int > VECTOR2I
Definition: vector2d.h:587
PCB_BASE_FRAME * m_frame
Definition: grid_helper.h:129
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 86 of file grid_helper.cpp.

87 {
88  return VECTOR2I( m_frame->GetGridOrigin() );
89 }
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:129

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

300 {
301  if( !m_snapItem )
302  return nullptr;
303 
304  return m_snapItem->item;
305 }
ANCHOR * m_snapItem
Pointer to the currently snapped item in m_anchors (NULL if not snapped)
Definition: grid_helper.h:135
BOARD_ITEM * item
Definition: grid_helper.h:94

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

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:102

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

215 {
216  std::set<BOARD_ITEM*> items;
217  std::vector<KIGFX::VIEW::LAYER_ITEM_PAIR> selectedItems;
218 
219  auto view = m_frame->GetCanvas()->GetView();
220  auto activeLayers = view->GetPainter()->GetSettings()->GetActiveLayers();
221  bool isHighContrast = view->GetPainter()->GetSettings()->GetHighContrast();
222  view->Query( aArea, selectedItems );
223 
224  for( auto it : selectedItems )
225  {
226  BOARD_ITEM* item = static_cast<BOARD_ITEM*>( it.first );
227 
228  // The item must be visible and on an active layer
229  if( view->IsVisible( item ) && ( !isHighContrast || activeLayers.count( it.second ) ) )
230  items.insert ( item );
231  }
232 
233 
234  for( auto ii : aSkip )
235  items.erase( ii );
236 
237  return items;
238 }
Class BOARD_ITEM is a base class for any item which can be embedded within the BOARD container class,...
PCB_DRAW_PANEL_GAL * GetCanvas() const override
Return a pointer to GAL-based canvas of given EDA draw frame.
PAINTER * GetPainter() const
Function GetPainter() Returns the painter object used by the view for drawing VIEW_ITEMS.
Definition: view.h:199
virtual KIGFX::PCB_VIEW * GetView() const override
Function GetView() Returns a pointer to the VIEW instance used in the panel.
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:129
const std::set< unsigned int > GetActiveLayers()
Function GetActiveLayers() Returns the set of currently active layers.
Definition: painter.h:89

References KIGFX::RENDER_SETTINGS::GetActiveLayers(), PCB_BASE_FRAME::GetCanvas(), KIGFX::VIEW::GetPainter(), KIGFX::PAINTER::GetSettings(), PCB_DRAW_PANEL_GAL::GetView(), and m_frame.

Referenced by BestSnapAnchor().

◆ SetAuxAxes()

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

Definition at line 92 of file grid_helper.cpp.

93 {
94  if( aEnable )
95  {
96  m_auxAxis = aOrigin;
97  m_viewAxis.SetPosition( aOrigin );
99  }
100  else
101  {
103  m_frame->GetCanvas()->GetView()->SetVisible( &m_viewAxis, false );
104  }
105 }
OPT< VECTOR2I > m_auxAxis
Definition: grid_helper.h:130
PCB_DRAW_PANEL_GAL * GetCanvas() const override
Return a pointer to GAL-based canvas of given EDA draw frame.
KIGFX::ORIGIN_VIEWITEM m_viewAxis
Definition: grid_helper.h:138
void SetPosition(const VECTOR2D &aPosition)
virtual KIGFX::PCB_VIEW * GetView() const override
Function GetView() Returns a pointer to the VIEW instance used in the panel.
PCB_BASE_FRAME * m_frame
Definition: grid_helper.h:129
boost::optional< T > OPT
Definition: optional.h:7
void SetVisible(VIEW_ITEM *aItem, bool aIsVisible=true)
Sets the item visibility.
Definition: view.cpp:1486

References PCB_BASE_FRAME::GetCanvas(), PCB_DRAW_PANEL_GAL::GetView(), m_auxAxis, m_frame, m_viewAxis, KIGFX::ORIGIN_VIEWITEM::SetPosition(), and KIGFX::VIEW::SetVisible().

Referenced by ROUTER_TOOL::InlineDrag(), EDIT_TOOL::Move(), POINT_EDITOR::OnSelectionChange(), and ROUTER_TOOL::performDragging().

◆ SetSnap()

◆ SetUseGrid()

Member Data Documentation

◆ m_anchors

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

Definition at line 102 of file grid_helper.h.

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

◆ m_auxAxis

OPT<VECTOR2I> GRID_HELPER::m_auxAxis
private

Definition at line 130 of file grid_helper.h.

Referenced by Align(), and SetAuxAxes().

◆ m_enableGrid

bool GRID_HELPER::m_enableGrid
private

If true, allow snapping to grid.

Definition at line 133 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 132 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 135 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 134 of file grid_helper.h.

Referenced by BestSnapAnchor(), and GRID_HELPER().

◆ m_viewAxis

KIGFX::ORIGIN_VIEWITEM GRID_HELPER::m_viewAxis
private

Definition at line 138 of file grid_helper.h.

Referenced by GRID_HELPER(), and SetAuxAxes().

◆ m_viewSnapPoint

KIGFX::ORIGIN_VIEWITEM GRID_HELPER::m_viewSnapPoint
private

Definition at line 137 of file grid_helper.h.

Referenced by BestSnapAnchor(), and GRID_HELPER().


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