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, std::vector< 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  m_snapItem = nullptr;
55  KIGFX::VIEW* view = m_frame->GetCanvas()->GetView();
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 }
ANCHOR * m_snapItem
Pointer to the currently snapped item in m_anchors (NULL if not snapped)
Definition: grid_helper.h:135
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_snapItem, 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,
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 109 of file grid_helper.cpp.

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

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

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

◆ AlignToSegment()

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

Definition at line 133 of file grid_helper.cpp.

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

Definition at line 171 of file grid_helper.cpp.

172 {
173  clearAnchors();
174 
175  for( auto item : aItems )
176  computeAnchors( item, aMousePos, true );
177 
178  double worldScale = m_frame->GetCanvas()->GetGAL()->GetWorldScale();
179  double lineSnapMinCornerDistance = 50.0 / worldScale;
180 
181  ANCHOR* nearestOutline = nearestAnchor( aMousePos, OUTLINE, LSET::AllLayersMask() );
182  ANCHOR* nearestCorner = nearestAnchor( aMousePos, CORNER, LSET::AllLayersMask() );
183  ANCHOR* nearestOrigin = nearestAnchor( aMousePos, ORIGIN, LSET::AllLayersMask() );
184  ANCHOR* best = NULL;
185  double minDist = std::numeric_limits<double>::max();
186 
187  if( nearestOrigin )
188  {
189  minDist = nearestOrigin->Distance( aMousePos );
190  best = nearestOrigin;
191  }
192 
193  if( nearestCorner )
194  {
195  double dist = nearestCorner->Distance( aMousePos );
196 
197  if( dist < minDist )
198  {
199  minDist = dist;
200  best = nearestCorner;
201  }
202  }
203 
204  if( nearestOutline )
205  {
206  double dist = nearestOutline->Distance( aMousePos );
207 
208  if( minDist > lineSnapMinCornerDistance && dist < minDist )
209  best = nearestOutline;
210  }
211 
212  return best ? best->pos : aMousePos;
213 }
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 245 of file grid_helper.cpp.

246 {
247  LSET layers;
248  std::vector<BOARD_ITEM*> item;
249 
250  if( aDraggedItem )
251  {
252  layers = aDraggedItem->GetLayerSet();
253  item.push_back( aDraggedItem );
254  }
255  else
256  layers = LSET::AllLayersMask();
257 
258  return BestSnapAnchor( aOrigin, layers, item );
259 }
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(), POINT_EDITOR::OnSelectionChange(), and DRAWING_TOOL::SetAnchor().

◆ BestSnapAnchor() [2/2]

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

Definition at line 262 of file grid_helper.cpp.

264 {
265  double worldScale = m_frame->GetCanvas()->GetGAL()->GetWorldScale();
266  int snapRange = (int) ( m_snapSize / worldScale );
267 
268  BOX2I bb( VECTOR2I( aOrigin.x - snapRange / 2, aOrigin.y - snapRange / 2 ), VECTOR2I( snapRange, snapRange ) );
269 
270  clearAnchors();
271 
272  for( BOARD_ITEM* item : queryVisible( bb, aSkip ) )
273  computeAnchors( item, aOrigin );
274 
275  ANCHOR* nearest = nearestAnchor( aOrigin, SNAPPABLE, aLayers );
276  VECTOR2I nearestGrid = Align( aOrigin );
277  double gridDist = ( nearestGrid - aOrigin ).EuclideanNorm();
278 
279  if( nearest && m_enableSnap )
280  {
281  double snapDist = nearest->Distance( aOrigin );
282 
283  if( !m_enableGrid || snapDist <= gridDist )
284  {
285  m_viewSnapPoint.SetPosition( nearest->pos );
286 
289  else
291 
292  m_snapItem = nearest;
293  return nearest->pos;
294  }
295  }
296 
297  m_snapItem = nullptr;
299  return nearestGrid;
300 }
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,...
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
std::set< BOARD_ITEM * > queryVisible(const BOX2I &aArea, const std::vector< BOARD_ITEM * > &aSkip) const
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 312 of file grid_helper.cpp.

313 {
314  VECTOR2I origin;
315  auto view = m_frame->GetCanvas()->GetView();
316  auto activeLayers = view->GetPainter()->GetSettings()->GetActiveLayers();
317  bool isHighContrast = view->GetPainter()->GetSettings()->GetHighContrast();
318 
319  switch( aItem->Type() )
320  {
321  case PCB_MODULE_T:
322  {
323  MODULE* mod = static_cast<MODULE*>( aItem );
324 
325  for( auto pad : mod->Pads() )
326  {
327  // Getting pads from the module requires re-checking that the pad is shown
328  if( ( aFrom || m_frame->Settings().m_MagneticPads == CAPTURE_ALWAYS )
329  && pad->GetBoundingBox().Contains( wxPoint( aRefPos.x, aRefPos.y ) )
330  && view->IsVisible( pad )
331  && ( !isHighContrast || activeLayers.count( pad->GetLayer() ) )
332  && pad->ViewGetLOD( pad->GetLayer(), view ) < view->GetScale() )
333  {
334  addAnchor( pad->GetPosition(), CORNER | SNAPPABLE, pad );
335  break;
336  }
337  }
338 
339  // if the cursor is not over a pad, then drag the module by its origin
340  addAnchor( mod->GetPosition(), ORIGIN | SNAPPABLE, mod );
341  break;
342  }
343 
344  case PCB_PAD_T:
345  {
346  if( aFrom || m_frame->Settings().m_MagneticPads == CAPTURE_ALWAYS )
347  {
348  D_PAD* pad = static_cast<D_PAD*>( aItem );
349  addAnchor( pad->GetPosition(), CORNER | SNAPPABLE, pad );
350  }
351 
352  break;
353  }
354 
355  case PCB_MODULE_EDGE_T:
356  case PCB_LINE_T:
357  {
359  break;
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  if( aFrom || m_frame->Settings().m_MagneticTracks == CAPTURE_ALWAYS )
422  {
423  TRACK* track = static_cast<TRACK*>( aItem );
424  VECTOR2I start = track->GetStart();
425  VECTOR2I end = track->GetEnd();
426  origin.x = start.x + ( start.x - end.x ) / 2;
427  origin.y = start.y + ( start.y - end.y ) / 2;
428  addAnchor( start, CORNER | SNAPPABLE, track );
429  addAnchor( end, CORNER | SNAPPABLE, track );
430  addAnchor( origin, ORIGIN, track);
431  }
432 
433  break;
434  }
435 
436  case PCB_MARKER_T:
437  case PCB_TARGET_T:
438  addAnchor( aItem->GetPosition(), ORIGIN | CORNER | SNAPPABLE, aItem );
439  break;
440 
441  case PCB_VIA_T:
442  {
443  if( aFrom || m_frame->Settings().m_MagneticTracks == CAPTURE_ALWAYS )
444  addAnchor( aItem->GetPosition(), ORIGIN | CORNER | SNAPPABLE, aItem );
445 
446  break;
447  }
448 
449  case PCB_ZONE_AREA_T:
450  {
451  const SHAPE_POLY_SET* outline = static_cast<const ZONE_CONTAINER*>( aItem )->Outline();
452 
453  SHAPE_LINE_CHAIN lc;
454  lc.SetClosed( true );
455 
456  for( auto iter = outline->CIterateWithHoles(); iter; iter++ )
457  {
458  addAnchor( *iter, CORNER, aItem );
459  lc.Append( *iter );
460  }
461 
462  addAnchor( lc.NearestPoint( aRefPos ), OUTLINE, aItem );
463 
464  break;
465  }
466 
467  case PCB_DIMENSION_T:
468  {
469  const DIMENSION* dim = static_cast<const DIMENSION*>( aItem );
470  addAnchor( dim->m_crossBarF, CORNER | SNAPPABLE, aItem );
471  addAnchor( dim->m_crossBarO, CORNER | SNAPPABLE, aItem );
472  addAnchor( dim->m_featureLineGO, CORNER | SNAPPABLE, aItem );
473  addAnchor( dim->m_featureLineDO, CORNER | SNAPPABLE, aItem );
474  break;
475  }
476 
477  case PCB_MODULE_TEXT_T:
478  case PCB_TEXT_T:
479  addAnchor( aItem->GetPosition(), ORIGIN, aItem );
480  break;
481 
482  default:
483  break;
484  }
485 }
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
PCB_DRAW_PANEL_GAL * GetCanvas() const override
Return a pointer to GAL-based canvas of given EDA draw frame.
polygon (not yet used for tracks, but could be in microwave apps)
class ZONE_CONTAINER, a zone area
Definition: typeinfo.h:101
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
PAINTER * GetPainter() const
Function GetPainter() Returns the painter object used by the view for drawing VIEW_ITEMS.
Definition: view.h:199
class TRACK, a track segment (segment on a copper layer)
Definition: typeinfo.h:96
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:99
virtual KIGFX::PCB_VIEW * GetView() const override
Function GetView() Returns a pointer to the VIEW instance used in the panel.
class PCB_TARGET, a target (graphic item)
Definition: typeinfo.h:100
virtual const wxPoint GetPosition() const =0
class TEXTE_MODULE, text in a footprint
Definition: typeinfo.h:93
virtual RENDER_SETTINGS * GetSettings()=0
Function GetSettings Returns pointer to current settings that are going to be used when drawing items...
class MARKER_PCB, a marker used to show something
Definition: typeinfo.h:98
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:97
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
const std::set< unsigned int > GetActiveLayers()
Function GetActiveLayers() Returns the set of currently active layers.
Definition: painter.h:89

References addAnchor(), SHAPE_LINE_CHAIN::Append(), DRAWSEGMENT::BuildPolyPointsList(), CAPTURE_ALWAYS, SHAPE_POLY_SET::CIterateWithHoles(), CORNER, EuclideanNorm(), KIGFX::RENDER_SETTINGS::GetActiveLayers(), DRAWSEGMENT::GetArcEnd(), DRAWSEGMENT::GetArcStart(), PCB_BASE_FRAME::GetCanvas(), DRAWSEGMENT::GetCenter(), TRACK::GetEnd(), DRAWSEGMENT::GetEnd(), KIGFX::VIEW::GetPainter(), BOARD_ITEM::GetPosition(), D_PAD::GetPosition(), KIGFX::PAINTER::GetSettings(), DRAWSEGMENT::GetShape(), TRACK::GetStart(), DRAWSEGMENT::GetStart(), PCB_DRAW_PANEL_GAL::GetView(), 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 77 of file grid_helper.cpp.

78 {
79  PCB_SCREEN* screen = m_frame->GetScreen();
80 
81  const wxRealPoint& size = screen->GetGridSize();
82 
83  return VECTOR2I( KiROUND( size.x ), KiROUND( size.y ) );
84 }
const wxRealPoint & GetGridSize() const
Return the grid size of the currently selected grid.
Definition: base_screen.h:279
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.
constexpr ret_type KiROUND(fp_type v)
Round a floating point number to an integer using "round halfway cases away from zero".
Definition: common.h:114

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

88 {
89  return VECTOR2I( m_frame->GetGridOrigin() );
90 }
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 303 of file grid_helper.cpp.

304 {
305  if( !m_snapItem )
306  return nullptr;
307 
308  return m_snapItem->item;
309 }
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 488 of file grid_helper.cpp.

489 {
490  double minDist = std::numeric_limits<double>::max();
491  ANCHOR* best = NULL;
492 
493  for( ANCHOR& a : m_anchors )
494  {
495  if( ( aMatchLayers & a.item->GetLayerSet() ) == 0 )
496  continue;
497 
498  if( ( aFlags & a.flags ) != aFlags )
499  continue;
500 
501  double dist = a.Distance( aPos );
502 
503  if( dist < minDist )
504  {
505  minDist = dist;
506  best = &a;
507  }
508  }
509 
510  return best;
511 }
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 216 of file grid_helper.cpp.

218 {
219  std::set<BOARD_ITEM*> items;
220  std::vector<KIGFX::VIEW::LAYER_ITEM_PAIR> selectedItems;
221 
222  auto view = m_frame->GetCanvas()->GetView();
223  auto activeLayers = view->GetPainter()->GetSettings()->GetActiveLayers();
224  bool isHighContrast = view->GetPainter()->GetSettings()->GetHighContrast();
225  view->Query( aArea, selectedItems );
226 
227  for( auto it : selectedItems )
228  {
229  BOARD_ITEM* item = static_cast<BOARD_ITEM*>( it.first );
230 
231  // The item must be visible and on an active layer
232  if( view->IsVisible( item ) && ( !isHighContrast || activeLayers.count( it.second ) )
233  && item->ViewGetLOD( it.second, view ) < view->GetScale() )
234  items.insert ( item );
235  }
236 
237 
238  for( auto ii : aSkip )
239  items.erase( ii );
240 
241  return items;
242 }
Class BOARD_ITEM is a base class for any item which can be embedded within the BOARD container class,...
virtual unsigned int ViewGetLOD(int aLayer, VIEW *aView) const
Function ViewGetLOD() Returns the level of detail (LOD) of the item.
Definition: view_item.h:140
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(), m_frame, and KIGFX::VIEW_ITEM::ViewGetLOD().

Referenced by BestSnapAnchor().

◆ SetAuxAxes()

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

Definition at line 93 of file grid_helper.cpp.

94 {
95  if( aEnable )
96  {
97  m_auxAxis = aOrigin;
98  m_viewAxis.SetPosition( aOrigin );
100  }
101  else
102  {
104  m_frame->GetCanvas()->GetView()->SetVisible( &m_viewAxis, false );
105  }
106 }
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(), GetSnapped(), and GRID_HELPER().

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