KiCad PCB EDA Suite
pns_tool_base.cpp
Go to the documentation of this file.
1 /*
2  * KiRouter - a push-and-(sometimes-)shove PCB router
3  *
4  * Copyright (C) 2013 CERN
5  * Copyright (C) 2016-2020 KiCad Developers, see AUTHORS.txt for contributors.
6  * Author: Tomasz Wlostowski <tomasz.wlostowski@cern.ch>
7  *
8  * This program is free software: you can redistribute it and/or modify it
9  * under the terms of the GNU General Public License as published by the
10  * Free Software Foundation, either version 3 of the License, or (at your
11  * option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful, but
14  * WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16  * General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License along
19  * with this program. If not, see <http://www.gnu.org/licenses/>.
20  */
21 
22 
23 #include <functional>
24 using namespace std::placeholders;
25 
26 #include <eda_item.h>
27 #include <id.h>
28 #include <view/view.h>
29 #include <view/view_controls.h>
30 #include <pcb_painter.h>
31 #include <pcbnew_settings.h>
32 #include <bitmaps.h>
33 
34 #include <tools/pcb_grid_helper.h>
35 #include <tools/pcb_actions.h>
36 #include <tool/tool_manager.h>
37 
38 #include "pns_arc.h"
39 #include "pns_kicad_iface.h"
40 #include "pns_tool_base.h"
41 #include "pns_segment.h"
42 #include "pns_solid.h"
43 #include "pns_via.h"
44 #include "pns_router.h"
45 #include "pns_meander_placer.h" // fixme: move settings to separate header
46 #include "pns_topology.h"
47 
48 
49 using namespace KIGFX;
50 
51 namespace PNS {
52 
53 
54 TOOL_BASE::TOOL_BASE( const std::string& aToolName ) :
55  PCB_TOOL_BASE( aToolName )
56 {
57  m_gridHelper = nullptr;
58  m_iface = nullptr;
59  m_router = nullptr;
60  m_cancelled = false;
61 
62  m_startItem = nullptr;
63  m_startHighlight = false;
64 
65  m_endItem = nullptr;
66  m_gridHelper = nullptr;
67 
68  m_cancelled = false;
69 }
70 
71 
73 {
74  delete m_gridHelper;
75  delete m_iface;
76  delete m_router;
77 }
78 
79 
81 {
82  delete m_gridHelper;
83  delete m_iface;
84  delete m_router;
85 
87  m_iface->SetBoard( board() );
88  m_iface->SetView( getView() );
89  m_iface->SetHostTool( this );
90  m_iface->SetDisplayOptions( &( frame()->GetDisplayOptions() ) );
91 
92  m_router = new ROUTER;
96 
98 
99  PCBNEW_SETTINGS* settings = frame()->GetPcbNewSettings();
100 
101  if( !settings->m_PnsSettings )
102  settings->m_PnsSettings = std::make_unique<ROUTING_SETTINGS>( settings, "tools.pns" );
103 
104  m_router->LoadSettings( settings->m_PnsSettings.get() );
105 
106  m_gridHelper = new PCB_GRID_HELPER( m_toolMgr, frame()->GetMagneticItemsSettings() );
107 }
108 
109 
110 ITEM* TOOL_BASE::pickSingleItem( const VECTOR2I& aWhere, int aNet, int aLayer, bool aIgnorePads,
111  const std::vector<ITEM*> aAvoidItems )
112 {
113  int tl = aLayer > 0 ? aLayer : getView()->GetTopLayer();
114 
115  static const int candidateCount = 5;
116  ITEM* prioritized[candidateCount];
117  SEG::ecoord dist[candidateCount];
118 
119  for( int i = 0; i < candidateCount; i++ )
120  {
121  prioritized[i] = nullptr;
122  dist[i] = VECTOR2I::ECOORD_MAX;
123  }
124 
125  ITEM_SET candidates = m_router->QueryHoverItems( aWhere );
126 
127  for( ITEM* item : candidates.Items() )
128  {
129  if( !item->IsRoutable() )
130  continue;
131 
132  if( !IsCopperLayer( item->Layers().Start() ) )
133  continue;
134 
135  if( !m_iface->IsAnyLayerVisible( item->Layers() ) )
136  continue;
137 
138  if( alg::contains( aAvoidItems, item ) )
139  continue;
140 
141  // fixme: this causes flicker with live loop removal...
142  //if( item->Parent() && !item->Parent()->ViewIsVisible() )
143  // continue;
144 
145  if( aNet <= 0 || item->Net() == aNet )
146  {
147  if( item->OfKind( ITEM::VIA_T | ITEM::SOLID_T ) )
148  {
149  if( item->OfKind( ITEM::SOLID_T ) && aIgnorePads )
150  continue;
151 
152  SEG::ecoord d = ( item->Shape()->Centre() - aWhere ).SquaredEuclideanNorm();
153 
154  if( d < dist[2] )
155  {
156  prioritized[2] = item;
157  dist[2] = d;
158  }
159 
160  if( item->Layers().Overlaps( tl ) && d < dist[0] )
161  {
162  prioritized[0] = item;
163  dist[0] = d;
164  }
165  }
166  else // ITEM::SEGMENT_T | ITEM::ARC_T
167  {
168  LINKED_ITEM* li = static_cast<LINKED_ITEM*>( item );
169  SEG::ecoord d = std::min( ( li->Anchor( 0 ) - aWhere ).SquaredEuclideanNorm(),
170  ( li->Anchor( 1 ) - aWhere ).SquaredEuclideanNorm() );
171 
172  if( d < dist[3] )
173  {
174  prioritized[3] = item;
175  dist[3] = d;
176  }
177 
178  if( item->Layers().Overlaps( tl ) && d < dist[1] )
179  {
180  prioritized[1] = item;
181  dist[1] = d;
182  }
183  }
184  }
185  // Allow unconnected items as last resort in RM_MarkObstacles mode
186  else if ( item->Net() == 0 && m_router->Settings().Mode() == RM_MarkObstacles )
187  {
188  if( item->OfKind( ITEM::SOLID_T ) && aIgnorePads )
189  continue;
190 
191  if( item->Layers().Overlaps( tl ) )
192  prioritized[4] = item;
193  }
194  }
195 
196  ITEM* rv = NULL;
197 
199 
200  for( int i = 0; i < candidateCount; i++ )
201  {
202  ITEM* item = prioritized[i];
203 
204  if( highContrast && item && !item->Layers().Overlaps( tl ) )
205  item = nullptr;
206 
207  if( item && ( aLayer < 0 || item->Layers().Overlaps( aLayer ) ) )
208  {
209  rv = item;
210  break;
211  }
212  }
213 
214  if( rv )
215  {
216  wxLogTrace( "PNS", "%s, layer : %d, tl: %d", rv->KindStr().c_str(), rv->Layers().Start(), tl );
217  }
218 
219  return rv;
220 }
221 
222 
223 void TOOL_BASE::highlightNet( bool aEnabled, int aNetcode )
224 {
226 
227  if( aNetcode >= 0 && aEnabled )
228  {
229  // If the user has previously set the current net to be highlighted,
230  // we assume they want to keep it highlighted after routing
232  && rs->GetHighlightNetCodes().count( aNetcode ) );
233 
234  rs->SetHighlight( true, aNetcode );
235  }
236  else
237  {
238  if( !m_startHighlight )
239  rs->SetHighlight( false );
240 
241  m_startHighlight = false;
242  }
243 
245 }
246 
248 {
249  // Sync PNS engine settings with the general PCB editor options.
250  auto& pnss = m_router->Settings();
251 
252  pnss.SetSnapToPads(
253  frame()->GetMagneticItemsSettings()->pads == MAGNETIC_OPTIONS::CAPTURE_CURSOR_IN_TRACK_TOOL ||
254  frame()->GetMagneticItemsSettings()->pads == MAGNETIC_OPTIONS::CAPTURE_ALWAYS );
255 
256  pnss.SetSnapToTracks(
257  frame()->GetMagneticItemsSettings()->tracks == MAGNETIC_OPTIONS::CAPTURE_CURSOR_IN_TRACK_TOOL
258  || frame()->GetMagneticItemsSettings()->tracks == MAGNETIC_OPTIONS::CAPTURE_ALWAYS );
259 
260  if( aItem )
261  {
262  if( aItem->OfKind( ITEM::VIA_T | ITEM::SEGMENT_T | ITEM::ARC_T ) )
263  return pnss.GetSnapToTracks();
264  else if( aItem->OfKind( ITEM::SOLID_T ) )
265  return pnss.GetSnapToPads();
266  }
267 
268  return false;
269 }
270 
271 void TOOL_BASE::updateStartItem( const TOOL_EVENT& aEvent, bool aIgnorePads )
272 {
273  int tl = getView()->GetTopLayer();
274  VECTOR2I cp = controls()->GetCursorPosition( !aEvent.Modifier( MD_SHIFT ) );
275  VECTOR2I p;
276  GAL* gal = m_toolMgr->GetView()->GetGAL();
277 
278  controls()->ForceCursorPosition( false );
279  m_gridHelper->SetUseGrid( gal->GetGridSnapping() && !aEvent.Modifier( MD_ALT ) );
280  m_gridHelper->SetSnap( !aEvent.Modifier( MD_SHIFT ) );
281 
282  if( aEvent.IsMotion() || aEvent.IsClick() )
283  p = aEvent.Position();
284  else
285  p = cp;
286 
287  m_startItem = pickSingleItem( p, -1, -1, aIgnorePads );
288 
290  m_startItem = nullptr;
291 
293 
294  if( checkSnap( m_startItem ) )
296 }
297 
298 
300 {
301  int layer;
302  GAL* gal = m_toolMgr->GetView()->GetGAL();
303 
304  m_gridHelper->SetUseGrid( gal->GetGridSnapping() && !aEvent.Modifier( MD_ALT ) );
305  m_gridHelper->SetSnap( !aEvent.Modifier( MD_SHIFT ) );
306 
307  controls()->ForceCursorPosition( false );
308  VECTOR2I mousePos = controls()->GetMousePosition();
309 
310  if( m_router->Settings().Mode() != RM_MarkObstacles &&
311  ( m_router->GetCurrentNets().empty() || m_router->GetCurrentNets().front() < 0 ) )
312  {
313  m_endSnapPoint = snapToItem( nullptr, mousePos );
315  m_endItem = nullptr;
316 
317  return;
318  }
319 
320  if( m_router->IsPlacingVia() )
321  layer = -1;
322  else
323  layer = m_router->GetCurrentLayer();
324 
325  ITEM* endItem = nullptr;
326 
327  std::vector<int> nets = m_router->GetCurrentNets();
328 
329  for( int net : nets )
330  {
331  endItem = pickSingleItem( mousePos, net, layer, false, { m_startItem } );
332 
333  if( endItem )
334  break;
335  }
336 
337  if( checkSnap( endItem ) )
338  {
339  m_endItem = endItem;
340  m_endSnapPoint = snapToItem( endItem, mousePos );
341  }
342  else
343  {
344  m_endItem = nullptr;
345  m_endSnapPoint = m_gridHelper->Align( mousePos );
346  }
347 
349 
350  if( m_endItem )
351  {
352  wxLogTrace( "PNS", "%s, layer : %d",
353  m_endItem->KindStr().c_str(),
354  m_endItem->Layers().Start() );
355  }
356 }
357 
358 
360 {
361  return m_router;
362 }
363 
364 
366 {
367  if( !aItem || !m_iface->IsItemVisible( aItem ) )
368  {
369  return m_gridHelper->Align( aP );
370  }
371 
372  switch( aItem->Kind() )
373  {
374  case ITEM::SOLID_T:
375  return static_cast<SOLID*>( aItem )->Pos();
376 
377  case ITEM::VIA_T:
378  return static_cast<VIA*>( aItem )->Pos();
379 
380  case ITEM::SEGMENT_T:
381  case ITEM::ARC_T:
382  {
383  LINKED_ITEM* li = static_cast<LINKED_ITEM*>( aItem );
384  VECTOR2I A = li->Anchor( 0 );
385  VECTOR2I B = li->Anchor( 1 );
386  SEG::ecoord w_sq = SEG::Square( li->Width() / 2 );
387  SEG::ecoord distA_sq = ( aP - A ).SquaredEuclideanNorm();
388  SEG::ecoord distB_sq = ( aP - B ).SquaredEuclideanNorm();
389 
390  if( distA_sq < w_sq || distB_sq < w_sq )
391  {
392  return ( distA_sq < distB_sq ) ? A : B;
393  }
394  // TODO(snh): Clean this up
395  else if( aItem->Kind() == ITEM::SEGMENT_T )
396  {
397  SEGMENT* seg = static_cast<SEGMENT*>( li );
398  return m_gridHelper->AlignToSegment( aP, seg->Seg() );
399  }
400  else if( aItem->Kind() == ITEM::ARC_T )
401  {
402  ARC* arc = static_cast<ARC*>( li );
403  return m_gridHelper->AlignToArc( aP, *static_cast<const SHAPE_ARC*>( arc->Shape() ) );
404  }
405  }
406  break;
407 
408  default:
409  break;
410  }
411 
412  return m_gridHelper->Align( aP );
413 }
414 
415 }
void SetHostTool(PCB_TOOL_BASE *aTool)
Base class for PNS router board items.
Definition: pns_item.h:55
KIGFX::VIEW * GetView() const
Definition: tool_manager.h:289
void SetView(KIGFX::VIEW *aView)
virtual VECTOR2D GetMousePosition(bool aWorldCoordinates=true) const =0
Return the current mouse pointer position.
BOARD * board() const
The Cairo implementation of the graphics abstraction layer.
Definition: color4d.cpp:175
Container for all the knowledge about how graphical objects are drawn on any output surface/device.
VECTOR2I m_startSnapPoint
Definition: pns_tool_base.h:70
const ITEM_SET QueryHoverItems(const VECTOR2I &aP)
Definition: pns_router.cpp:120
bool IsClick(int aButtonMask=BUT_ANY) const
Definition: tool_event.cpp:181
ENTRIES & Items()
Definition: pns_itemset.h:138
TOOL_MANAGER * m_toolMgr
Definition: tool_base.h:215
VECTOR2I::extended_type ecoord
Definition: seg.h:44
SIZES_SETTINGS m_savedSizes
Definition: pns_tool_base.h:68
void SyncWorld()
Definition: pns_router.cpp:93
bool IsPlacingVia() const
Definition: pns_router.cpp:754
bool IsMotion() const
Definition: tool_event.h:316
bool Overlaps(const LAYER_RANGE &aOther) const
Definition: pns_layerset.h:67
const std::vector< int > GetCurrentNets() const
Definition: pns_router.cpp:726
GAL * GetGAL() const
Return the #GAL this view is using to draw graphical primitives.
Definition: view.h:189
const SEG & Seg() const
Definition: pns_segment.h:84
bool IsHighlightEnabled() const
Return current highlight setting.
static SEG::ecoord Square(int a)
Definition: seg.h:123
bool GetUseGrid() const
Definition: grid_helper.h:68
PAINTER * GetPainter() const
Return the painter object used by the view for drawing #VIEW_ITEMS.
Definition: view.h:207
void ClearWorld()
Definition: pns_router.cpp:102
int Start() const
Definition: pns_layerset.h:82
virtual int Width() const
PCB_BASE_EDIT_FRAME * frame() const
virtual void updateStartItem(const TOOL_EVENT &aEvent, bool aIgnorePads=false)
std::string KindStr() const
Returns the kind of the item, as string.
Definition: pns_item.cpp:126
VECTOR2I AlignToArc(const VECTOR2I &aPoint, const SHAPE_ARC &aSeg)
virtual void updateEndItem(const TOOL_EVENT &aEvent)
bool GetGridSnapping() const
static constexpr extended_type ECOORD_MAX
Definition: vector2d.h:79
virtual void Reset(RESET_REASON aReason) override
Bring the tool to a known, initial state.
bool checkSnap(ITEM *aItem)
PCB_GRID_HELPER * m_gridHelper
Definition: pns_tool_base.h:76
#define NULL
virtual ~TOOL_BASE()
bool IsItemVisible(const PNS::ITEM *aItem) const override
virtual int GetTopLayer() const
Definition: view.cpp:823
Generic, UI-independent tool event.
Definition: tool_event.h:173
Non-active layers are shown normally (no high-contrast mode)
ITEM * m_startItem
Definition: pns_tool_base.h:69
ROUTER * m_router
Definition: pns_tool_base.h:78
PNS_MODE Mode() const
Set the routing mode.
HIGH_CONTRAST_MODE m_ContrastModeDisplay
How inactive layers are displayed.
virtual void ForceCursorPosition(bool aEnabled, const VECTOR2D &aPosition=VECTOR2D(0, 0))
Place the cursor immediately at a given point.
void SetSnapToPads(bool aSnap)
std::unique_ptr< PNS::ROUTING_SETTINGS > m_PnsSettings
bool contains(const _Container &__container, _Value __value)
Returns true if the container contains the given value.
Definition: kicad_algo.h:81
KIGFX::VIEW * getView() const
Returns the instance of #VIEW object used in the application.
Definition: tool_base.cpp:36
void SetInterface(ROUTER_IFACE *aIface)
Definition: pns_router.cpp:796
const PCB_DISPLAY_OPTIONS & displayOptions() const
void SetBoard(BOARD *aBoard)
void SetSnap(bool aSnap)
Definition: grid_helper.h:64
virtual void highlightNet(bool aEnabled, int aNetcode=-1)
int Modifier(int aMask=MD_MODIFIER_MASK) const
Definition: tool_event.h:352
virtual VECTOR2I Anchor(int n) const
Definition: pns_item.h:213
virtual VECTOR2I Align(const VECTOR2I &aPoint) const
Definition: grid_helper.cpp:95
const SHAPE * Shape() const override
Return the geometrical shape of the item.
Definition: pns_arc.h:78
virtual RENDER_SETTINGS * GetSettings()=0
Return a pointer to current settings that are going to be used when drawing items.
const VECTOR2I snapToItem(ITEM *aSnapToItem, VECTOR2I aP)
void UpdateSizes(const SIZES_SETTINGS &aSizes)
Applies stored settings.
Definition: pns_router.cpp:526
void SetUseGrid(bool aSnapToGrid)
Definition: grid_helper.h:67
Ignore collisions, mark obstacles.
KIGFX::VIEW_CONTROLS * controls() const
void SetHighlight(bool aEnabled, int aNetcode=-1, bool aMulti=false)
Turns on/off highlighting.
VECTOR2I m_endSnapPoint
Definition: pns_tool_base.h:74
bool OfKind(int aKindMask) const
Return true if the item's type matches the mask aKindMask.
Definition: pns_item.h:134
PCBNEW_SETTINGS * GetPcbNewSettings() const
virtual ITEM * pickSingleItem(const VECTOR2I &aWhere, int aNet=-1, int aLayer=-1, bool aIgnorePads=false, const std::vector< ITEM * > aAvoidItems={})
RESET_REASON
Determine the reason of reset for a tool.
Definition: tool_base.h:78
bool IsCopperLayer(LAYER_NUM aLayerId)
Tests whether a layer is a copper layer.
PnsKind Kind() const
Return the type (kind) of the item.
Definition: pns_item.h:126
void LoadSettings(ROUTING_SETTINGS *aSettings)
Changes routing settings to ones passed in the parameter.
Definition: pns_router.h:203
const std::set< int > & GetHighlightNetCodes() const
Return the netcode of currently highlighted net.
ROUTER * Router() const
void UpdateAllLayersColor()
Apply the new coloring scheme to all layers.
Definition: view.cpp:770
Push and Shove diff pair dimensions (gap) settings dialog.
void SetDisplayOptions(const PCB_DISPLAY_OPTIONS *aDispOptions)
PNS_KICAD_IFACE * m_iface
Definition: pns_tool_base.h:77
ROUTING_SETTINGS & Settings()
Definition: pns_router.h:189
VECTOR2I AlignToSegment(const VECTOR2I &aPoint, const SEG &aSeg)
const VECTOR2D Position() const
Returns the point where dragging has started.
Definition: tool_event.h:284
const LAYER_RANGE & Layers() const
Definition: pns_item.h:150
VECTOR2D GetCursorPosition() const
Return the current cursor position in world coordinates.
TOOL_BASE(TOOL_TYPE aType, TOOL_ID aId, const std::string &aName=std::string(""))
Definition: tool_base.h:69
Abstract interface for drawing on a 2D-surface.
bool IsAnyLayerVisible(const LAYER_RANGE &aLayer) const override
int GetCurrentLayer() const
Definition: pns_router.cpp:737