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
 
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)
 
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
 
void addAnchor (const VECTOR2I &aPos, int aFlags=CORNER|SNAPPABLE, BOARD_ITEM *aItem=NULL)
 
ANCHORnearestAnchor (const VECTOR2I &aPos, int aFlags, LSET aMatchLayers)
 
void computeAnchors (BOARD_ITEM *aItem, const VECTOR2I &aRefPos)
 
void clearAnchors ()
 

Private Attributes

std::vector< ANCHORm_anchors
 
PCB_BASE_FRAMEm_frame
 
OPT< VECTOR2Im_auxAxis
 
bool m_diagonalAuxAxesEnable
 If true, use the aux axis for snapping as well. More...
 
bool m_enableSnap
 If true, allow snapping to other items on the layers. More...
 
bool m_enableGrid
 If true, allow snapping to grid. More...
 
int m_snapSize
 Sets the radius in screen units for snapping to items. More...
 
KIGFX::ORIGIN_VIEWITEM m_viewSnapPoint
 
KIGFX::ORIGIN_VIEWITEM m_viewAxis
 

Detailed Description

Definition at line 40 of file grid_helper.h.

Member Enumeration Documentation

Enumerator
CORNER 
OUTLINE 
SNAPPABLE 
ORIGIN 

Definition at line 73 of file grid_helper.h.

73  {
74  CORNER = 0x1,
75  OUTLINE = 0x2,
76  SNAPPABLE = 0x4,
77  ORIGIN = 0x8
78  };

Constructor & Destructor Documentation

GRID_HELPER::GRID_HELPER ( PCB_BASE_FRAME aFrame)

Definition at line 47 of file grid_helper.cpp.

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

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

Definition at line 71 of file grid_helper.cpp.

72 {
73 }

Member Function Documentation

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

Definition at line 99 of file grid_helper.h.

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

Referenced by computeAnchors().

100  {
101  m_anchors.push_back( ANCHOR( aPos, aFlags, aItem ) );
102  }
std::vector< ANCHOR > m_anchors
Definition: grid_helper.h:95
VECTOR2I GRID_HELPER::Align ( const VECTOR2I aPoint) const

Definition at line 124 of file grid_helper.cpp.

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

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

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

Definition at line 148 of file grid_helper.cpp.

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

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

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

Definition at line 186 of file grid_helper.cpp.

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

Referenced by EDIT_TOOL::Main().

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

Definition at line 256 of file grid_helper.cpp.

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

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

257 {
258  LSET layers;
259 
260  if( aDraggedItem )
261  layers = aDraggedItem->GetLayerSet();
262  else
263  layers = LSET::AllLayersMask();
264 
265  return BestSnapAnchor( aOrigin, layers );
266 }
VECTOR2I BestSnapAnchor(const VECTOR2I &aOrigin, BOARD_ITEM *aDraggedItem)
Class LSET is a set of PCB_LAYER_IDs.
virtual LSET GetLayerSet() const
Function GetLayerSet returns a "layer mask", which is a bitmap of all layers on which the TRACK segme...
static LSET AllLayersMask()
Definition: lset.cpp:710
VECTOR2I GRID_HELPER::BestSnapAnchor ( const VECTOR2I aOrigin,
const LSET aLayers 
)

Definition at line 269 of file grid_helper.cpp.

References Align(), clearAnchors(), computeAnchors(), GRID_HELPER::ANCHOR::Distance(), EuclideanNorm(), KIGFX::GEOMETRY, EDA_DRAW_PANEL_GAL::GetGAL(), EDA_DRAW_FRAME::GetGalCanvas(), EDA_DRAW_PANEL_GAL::GetView(), KIGFX::GAL::GetWorldScale(), KIGFX::VIEW::IsVisible(), m_enableGrid, m_enableSnap, m_frame, m_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.

270 {
271  double worldScale = m_frame->GetGalCanvas()->GetGAL()->GetWorldScale();
272  int snapRange = (int) ( m_snapSize / worldScale );
273 
274  BOX2I bb( VECTOR2I( aOrigin.x - snapRange / 2, aOrigin.y - snapRange / 2 ), VECTOR2I( snapRange, snapRange ) );
275 
276  clearAnchors();
277 
278  for( BOARD_ITEM* item : queryVisible( bb ) )
279  {
280  computeAnchors( item, aOrigin );
281  }
282 
283  ANCHOR* nearest = nearestAnchor( aOrigin, SNAPPABLE, aLayers );
284  VECTOR2I nearestGrid = Align( aOrigin );
285  double gridDist = ( nearestGrid - aOrigin ).EuclideanNorm();
286 
287  if( nearest && m_enableSnap )
288  {
289  double snapDist = nearest->Distance( aOrigin );
290 
291  if( !m_enableGrid || snapDist <= gridDist )
292  {
293  m_viewSnapPoint.SetPosition( nearest->pos );
294 
297  else
299 
300  return nearest->pos;
301  }
302  }
303 
305  return nearestGrid;
306 }
double EuclideanNorm(const wxPoint &vector)
Euclidean norm of a 2D vector.
Definition: trigo.h:112
Class BOARD_ITEM is a base class for any item which can be embedded within the BOARD container class...
double GetWorldScale() const
Get the world scale.
VECTOR2I Align(const VECTOR2I &aPoint) const
Class VECTOR2 defines a general 2D-vector/point.
Definition: vector2d.h:61
std::set< BOARD_ITEM * > queryVisible(const BOX2I &aArea) const
KIGFX::VIEW * GetView() const
Function GetView() Returns a pointer to the VIEW instance used in the panel.
VECTOR2< int > VECTOR2I
Definition: vector2d.h:587
KIGFX::ORIGIN_VIEWITEM m_viewSnapPoint
Definition: grid_helper.h:121
KIGFX::GAL * GetGAL() const
Function GetGAL() Returns a pointer to the GAL instance used in the panel.
ANCHOR * nearestAnchor(const VECTOR2I &aPos, int aFlags, LSET aMatchLayers)
void SetPosition(const VECTOR2D &aPosition)
bool m_enableSnap
If true, allow snapping to other items on the layers.
Definition: grid_helper.h:117
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:1521
PCB_BASE_FRAME * m_frame
Definition: grid_helper.h:113
int m_snapSize
Sets the radius in screen units for snapping to items.
Definition: grid_helper.h:119
void computeAnchors(BOARD_ITEM *aItem, const VECTOR2I &aRefPos)
void SetVisible(VIEW_ITEM *aItem, bool aIsVisible=true)
Sets the item visibility.
Definition: view.cpp:1467
EDA_DRAW_PANEL_GAL * GetGalCanvas() const
Return a pointer to GAL-based canvas of given EDA draw frame.
Definition: draw_frame.h:908
bool m_enableGrid
If true, allow snapping to grid.
Definition: grid_helper.h:118
bool IsVisible(const VIEW_ITEM *aItem) const
Returns information if the item is visible (or not).
Definition: view.cpp:1507
void clearAnchors()
Definition: grid_helper.h:108
Color has changed.
Definition: view_item.h:57
void GRID_HELPER::clearAnchors ( )
inlineprivate

Definition at line 108 of file grid_helper.h.

Referenced by BestDragOrigin(), and BestSnapAnchor().

109  {
110  m_anchors.clear();
111  }
std::vector< ANCHOR > m_anchors
Definition: grid_helper.h:95
void GRID_HELPER::computeAnchors ( BOARD_ITEM aItem,
const VECTOR2I aRefPos 
)
private

Definition at line 309 of file grid_helper.cpp.

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

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

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

Definition at line 88 of file grid_helper.cpp.

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

Referenced by Align(), and AlignToSegment().

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

Definition at line 98 of file grid_helper.cpp.

References PCB_BASE_FRAME::GetGridOrigin(), and m_frame.

Referenced by Align(), and AlignToSegment().

99 {
100  return VECTOR2I( m_frame->GetGridOrigin() );
101 }
const wxPoint & GetGridOrigin() const override
Return the absolute coordinates of the origin of the snap grid.
VECTOR2< int > VECTOR2I
Definition: vector2d.h:587
PCB_BASE_FRAME * m_frame
Definition: grid_helper.h:113
GRID_HELPER::ANCHOR * GRID_HELPER::nearestAnchor ( const VECTOR2I aPos,
int  aFlags,
LSET  aMatchLayers 
)
private

Definition at line 459 of file grid_helper.cpp.

References dist, m_anchors, and max.

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

460 {
461  double minDist = std::numeric_limits<double>::max();
462  ANCHOR* best = NULL;
463 
464  for( ANCHOR& a : m_anchors )
465  {
466  if( ( aMatchLayers & a.item->GetLayerSet() ) == 0 )
467  continue;
468 
469  if( ( aFlags & a.flags ) != aFlags )
470  continue;
471 
472  double dist = a.Distance( aPos );
473 
474  if( dist < minDist )
475  {
476  minDist = dist;
477  best = &a;
478  }
479  }
480 
481  return best;
482 }
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:95
std::set< BOARD_ITEM * > GRID_HELPER::queryVisible ( const BOX2I aArea) const
private

Definition at line 229 of file grid_helper.cpp.

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

Referenced by BestSnapAnchor().

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

Definition at line 104 of file grid_helper.cpp.

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

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

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

Definition at line 76 of file grid_helper.cpp.

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

Definition at line 82 of file grid_helper.cpp.

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

Member Data Documentation

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

Definition at line 95 of file grid_helper.h.

Referenced by nearestAnchor().

OPT<VECTOR2I> GRID_HELPER::m_auxAxis
private

Definition at line 114 of file grid_helper.h.

Referenced by Align(), and SetAuxAxes().

bool GRID_HELPER::m_diagonalAuxAxesEnable
private

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

Definition at line 116 of file grid_helper.h.

Referenced by GRID_HELPER(), and SetAuxAxes().

bool GRID_HELPER::m_enableGrid
private

If true, allow snapping to grid.

Definition at line 118 of file grid_helper.h.

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

bool GRID_HELPER::m_enableSnap
private

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

Definition at line 117 of file grid_helper.h.

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

PCB_BASE_FRAME* GRID_HELPER::m_frame
private
int GRID_HELPER::m_snapSize
private

Sets the radius in screen units for snapping to items.

Definition at line 119 of file grid_helper.h.

Referenced by BestSnapAnchor(), and GRID_HELPER().

KIGFX::ORIGIN_VIEWITEM GRID_HELPER::m_viewAxis
private

Definition at line 122 of file grid_helper.h.

Referenced by GRID_HELPER(), and SetAuxAxes().

KIGFX::ORIGIN_VIEWITEM GRID_HELPER::m_viewSnapPoint
private

Definition at line 121 of file grid_helper.h.

Referenced by BestSnapAnchor(), and GRID_HELPER().


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