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 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 #include <wx/numdlg.h>
23 
24 #include <functional>
25 using namespace std::placeholders;
26 
27 #include "class_draw_panel_gal.h"
28 #include "class_board.h"
29 
30 #include <pcb_edit_frame.h>
31 #include <id.h>
32 #include <macros.h>
33 #include <view/view_controls.h>
34 #include <pcb_painter.h>
39 #include <base_units.h>
40 #include <bitmaps.h>
41 
42 #include <tool/action_menu.h>
43 #include <tools/pcb_actions.h>
44 #include <tools/grid_helper.h>
45 
46 #include "pns_kicad_iface.h"
47 #include "pns_tool_base.h"
48 #include "pns_segment.h"
49 #include "pns_solid.h"
50 #include "pns_via.h"
51 #include "pns_router.h"
52 #include "pns_meander_placer.h" // fixme: move settings to separate header
53 #include "pns_tune_status_popup.h"
54 #include "pns_topology.h"
55 
56 #include <view/view.h>
57 
58 using namespace KIGFX;
59 
60 namespace PNS {
61 
62 
63 TOOL_BASE::TOOL_BASE( const std::string& aToolName ) :
64  PCB_TOOL_BASE( aToolName )
65 {
66  m_gridHelper = nullptr;
67  m_iface = nullptr;
68  m_router = nullptr;
69 
70  m_startItem = nullptr;
71  m_startLayer = 0;
72  m_startHighlight = false;
73 
74  m_endItem = nullptr;
75  m_gridHelper = nullptr;
76 
77  m_cancelled = false;
78 }
79 
80 
82 {
83  delete m_gridHelper;
84  delete m_iface;
85  delete m_router;
86 }
87 
88 
89 
91 {
92  delete m_gridHelper;
93  delete m_iface;
94  delete m_router;
95 
97  m_iface->SetBoard( board() );
98  m_iface->SetView( getView() );
99  m_iface->SetHostTool( this );
100  m_iface->SetDisplayOptions( &( frame()->GetDisplayOptions() ) );
101 
102  m_router = new ROUTER;
104  m_router->ClearWorld();
105  m_router->SyncWorld();
108 
109  m_gridHelper = new GRID_HELPER( frame() );
110 }
111 
112 
113 ITEM* TOOL_BASE::pickSingleItem( const VECTOR2I& aWhere, int aNet, int aLayer, bool aIgnorePads,
114  const std::vector<ITEM*> aAvoidItems)
115 {
116  int tl = getView()->GetTopLayer();
117 
118  if( aLayer > 0 )
119  tl = aLayer;
120 
121  static const int candidateCount = 5;
122  ITEM* prioritized[candidateCount];
123  int dist[candidateCount];
124 
125  for( int i = 0; i < candidateCount; i++ )
126  {
127  prioritized[i] = 0;
128  dist[i] = std::numeric_limits<int>::max();
129  }
130 
131  ITEM_SET candidates = m_router->QueryHoverItems( aWhere );
132 
133  for( ITEM* item : candidates.Items() )
134  {
135  if( !item->IsRoutable() )
136  continue;
137 
138  if( !IsCopperLayer( item->Layers().Start() ) )
139  continue;
140 
141  if( !m_iface->IsAnyLayerVisible( item->Layers() ) )
142  continue;
143 
144  if( std::find( aAvoidItems.begin(), aAvoidItems.end(), item ) != aAvoidItems.end() )
145  continue;
146 
147  // fixme: this causes flicker with live loop removal...
148  //if( item->Parent() && !item->Parent()->ViewIsVisible() )
149  // continue;
150 
151  if( aNet <= 0 || item->Net() == aNet )
152  {
153  if( item->OfKind( ITEM::VIA_T | ITEM::SOLID_T ) )
154  {
155  if( item->OfKind( ITEM::SOLID_T ) && aIgnorePads )
156  continue;
157 
158  int itemDist = ( item->Shape()->Centre() - aWhere ).SquaredEuclideanNorm();
159 
160  if( !prioritized[2] || itemDist < dist[2] )
161  {
162  prioritized[2] = item;
163  dist[2] = itemDist;
164  }
165  if( item->Layers().Overlaps( tl ) && itemDist < dist[0] )
166  {
167  prioritized[0] = item;
168  dist[0] = itemDist;
169  }
170  }
171  else
172  {
173  if( !prioritized[3] )
174  prioritized[3] = item;
175  if( item->Layers().Overlaps( tl ) )
176  prioritized[1] = item;
177  }
178  }
179  // Allow unconnected items as last resort in RM_MarkObstacles mode
180  else if ( item->Net() == 0 && m_router->Settings().Mode() == RM_MarkObstacles )
181  {
182  if( item->OfKind( ITEM::SOLID_T ) && aIgnorePads )
183  continue;
184 
185  if( item->Layers().Overlaps( tl ) )
186  prioritized[4] = item;
187  }
188  }
189 
190  ITEM* rv = NULL;
191 
192  for( int i = 0; i < candidateCount; i++ )
193  {
194  ITEM* item = prioritized[i];
195 
197  if( item && !item->Layers().Overlaps( tl ) )
198  item = NULL;
199 
200  if( item && ( aLayer < 0 || item->Layers().Overlaps( aLayer ) ) )
201  {
202  rv = item;
203  break;
204  }
205  }
206 
207  if( rv )
208  {
209  wxLogTrace( "PNS", "%s, layer : %d, tl: %d", rv->KindStr().c_str(), rv->Layers().Start(), tl );
210  }
211 
212  return rv;
213 }
214 
215 
216 void TOOL_BASE::highlightNet( bool aEnabled, int aNetcode )
217 {
219 
220  if( aNetcode >= 0 && aEnabled )
221  {
222  // If the user has previously set the current net to be highlighted,
223  // we assume they want to keep it highlighted after routing
224  m_startHighlight = ( rs->IsHighlightEnabled() && rs->GetHighlightNetCode() == aNetcode );
225 
226  rs->SetHighlight( true, aNetcode );
227  }
228  else
229  {
230  if( !m_startHighlight )
231  rs->SetHighlight( false );
232 
233  m_startHighlight = false;
234  }
235 
237 }
238 
240 {
241  // Sync PNS engine settings with the general PCB editor options.
242  auto& pnss = m_router->Settings();
243 
244  pnss.SetSnapToPads( frame()->Settings().m_MagneticPads == CAPTURE_CURSOR_IN_TRACK_TOOL
245  || frame()->Settings().m_MagneticPads == CAPTURE_ALWAYS );
246 
247  pnss.SetSnapToTracks( frame()->Settings().m_MagneticTracks == CAPTURE_CURSOR_IN_TRACK_TOOL
248  || frame()->Settings().m_MagneticTracks == CAPTURE_ALWAYS );
249 
250  if( aItem )
251  {
252  if( aItem->OfKind( ITEM::VIA_T ) || aItem->OfKind( ITEM::SEGMENT_T ) )
253  return pnss.GetSnapToTracks();
254  else if( aItem->OfKind( ITEM::SOLID_T ) )
255  return pnss.GetSnapToPads();
256  }
257 
258  return false;
259 }
260 
261 void TOOL_BASE::updateStartItem( const TOOL_EVENT& aEvent, bool aIgnorePads )
262 {
263  int tl = getView()->GetTopLayer();
264  VECTOR2I cp = controls()->GetCursorPosition( !aEvent.Modifier( MD_SHIFT ) );
265  VECTOR2I p;
266 
267  controls()->ForceCursorPosition( false );
268  m_gridHelper->SetUseGrid( !aEvent.Modifier( MD_ALT ) );
269  m_gridHelper->SetSnap( !aEvent.Modifier( MD_SHIFT ) );
270 
271  bool snapEnabled = true;
272 
273  if( aEvent.IsMotion() || aEvent.IsClick() )
274  {
275  snapEnabled = !aEvent.Modifier( MD_SHIFT );
276  p = aEvent.Position();
277  }
278  else
279  {
280  p = cp;
281  }
282 
283  m_startItem = pickSingleItem( p, -1, -1, aIgnorePads );
284 
285  if( !snapEnabled && m_startItem && !m_startItem->Layers().Overlaps( tl ) )
286  m_startItem = nullptr;
287 
288  m_startSnapPoint = snapToItem( snapEnabled, m_startItem, p );
289 
290  if( checkSnap( m_startItem ) )
291  {
293  }
294 }
295 
296 
298 {
299  int layer;
300  bool snapEnabled = !aEvent.Modifier( MD_SHIFT );
301  m_gridHelper->SetUseGrid( !aEvent.Modifier( MD_ALT ) );
302  m_gridHelper->SetSnap( snapEnabled );
303 
304  controls()->ForceCursorPosition( false );
305  VECTOR2I mousePos = controls()->GetMousePosition();
306 
307  if( m_router->Settings().Mode() != RM_MarkObstacles &&
308  ( m_router->GetCurrentNets().empty() || m_router->GetCurrentNets().front() < 0 ) )
309  {
310  m_endSnapPoint = snapToItem( snapEnabled, nullptr, mousePos );
312  m_endItem = nullptr;
313 
314  return;
315  }
316 
317  if( m_router->IsPlacingVia() )
318  layer = -1;
319  else
320  layer = m_router->GetCurrentLayer();
321 
322  ITEM* endItem = nullptr;
323 
324  std::vector<int> nets = m_router->GetCurrentNets();
325 
326  for( int net : nets )
327  {
328  endItem = pickSingleItem( mousePos, net, layer, false, { m_startItem } );
329 
330  if( endItem )
331  break;
332  }
333 
334  if( checkSnap( endItem ) )
335  {
336  m_endItem = endItem;
337  m_endSnapPoint = snapToItem( snapEnabled, endItem, mousePos );
338  } else {
339  m_endItem = nullptr;
340  m_endSnapPoint = m_gridHelper->Align( mousePos );
341  }
342 
344 
345  if( m_endItem )
346  {
347  wxLogTrace( "PNS", "%s, layer : %d", m_endItem->KindStr().c_str(), m_endItem->Layers().Start() );
348  }
349 }
350 
351 
352 void TOOL_BASE::deleteTraces( ITEM* aStartItem, bool aWholeTrack )
353 {
354  NODE *node = m_router->GetWorld()->Branch();
355 
356  if( !aStartItem )
357  return;
358 
359  if( !aWholeTrack )
360  {
361  node->Remove( aStartItem );
362  }
363  else
364  {
365  TOPOLOGY topo( node );
366  ITEM_SET path = topo.AssembleTrivialPath( aStartItem );
367 
368  for( const auto& ent : path.Items() )
369  node->Remove( ent.item );
370  }
371 
372  m_router->CommitRouting( node );
373 }
374 
375 
377 {
378  return m_router;
379 }
380 
381 
382 const VECTOR2I TOOL_BASE::snapToItem( bool aEnabled, ITEM* aItem, VECTOR2I aP)
383 {
384  VECTOR2I anchor;
385 
386  if( !aItem || !aEnabled || !m_iface->IsItemVisible( aItem ) )
387  {
388  return m_gridHelper->Align( aP );
389  }
390 
391  switch( aItem->Kind() )
392  {
393  case ITEM::SOLID_T:
394  anchor = static_cast<SOLID*>( aItem )->Pos();
395  break;
396 
397  case ITEM::VIA_T:
398  anchor = static_cast<VIA*>( aItem )->Pos();
399  break;
400 
401  case ITEM::SEGMENT_T:
402  {
403  SEGMENT* seg = static_cast<SEGMENT*>( aItem );
404  const SEG& s = seg->Seg();
405  int w = seg->Width();
406 
407 
408  if( ( aP - s.A ).EuclideanNorm() < w / 2 )
409  anchor = s.A;
410  else if( ( aP - s.B ).EuclideanNorm() < w / 2 )
411  anchor = s.B;
412  else
413  anchor = m_gridHelper->AlignToSegment( aP, s );
414 
415  break;
416  }
417 
418  default:
419  break;
420  }
421 
422  return anchor;
423 }
424 
425 }
void SetHostTool(PCB_TOOL_BASE *aTool)
ITEM.
Definition: pns_item.h:53
void SetView(KIGFX::VIEW *aView)
NODE.
Definition: pns_node.h:140
GRID_HELPER * m_gridHelper
Definition: pns_tool_base.h:76
static const int dist[10][10]
Definition: ar_matrix.cpp:326
virtual VECTOR2D GetMousePosition(bool aWorldCoordinates=true) const =0
Function GetMousePosition() Returns the current mouse pointer position.
BOARD * board() const
Class CAIRO_GAL is the cairo implementation of the graphics abstraction layer.
Definition: color4d.cpp:131
void LoadSettings(const ROUTING_SETTINGS &aSettings)
Changes routing settings to ones passed in the parameter.
Definition: pns_router.h:204
RENDER_SETTINGS Contains all the knowledge about how graphical objects are drawn on any output surfac...
Definition: painter.h:56
Implementation of conversion functions that require both schematic and board internal units.
VIEW_CONTROLS class definition.
VECTOR2I m_startSnapPoint
Definition: pns_tool_base.h:70
const ITEM_SET QueryHoverItems(const VECTOR2I &aP)
Definition: pns_router.cpp:117
bool IsClick(int aButtonMask=BUT_ANY) const
Definition: tool_event.cpp:178
ENTRIES & Items()
Definition: pns_itemset.h:140
bool m_startHighlight
Keeps track of whether the net was highlighted before routing.
Definition: pns_tool_base.h:71
SIZES_SETTINGS m_savedSizes
Stores sizes settings between router invocations.
Definition: pns_tool_base.h:67
void SyncWorld()
Definition: pns_router.cpp:90
VECTOR2I AlignToSegment(const VECTOR2I &aPoint, const SEG &aSeg)
bool IsPlacingVia() const
Definition: pns_router.cpp:498
bool IsMotion() const
Definition: tool_event.h:306
bool Overlaps(const LAYER_RANGE &aOther) const
Definition: pns_layerset.h:68
const std::vector< int > GetCurrentNets() const
Definition: pns_router.cpp:458
bool IsAnyLayerVisible(const LAYER_RANGE &aLayer) override
const SEG & Seg() const
Definition: pns_segment.h:93
bool IsHighlightEnabled() const
Function IsHighlightEnabled Returns current highlight setting.
Definition: painter.h:118
NODE * Branch()
Function Branch()
Definition: pns_node.cpp:104
const VECTOR2I snapToItem(bool aEnabled, ITEM *aItem, VECTOR2I aP)
PAINTER * GetPainter() const
Function GetPainter() Returns the painter object used by the view for drawing VIEW_ITEMS.
Definition: view.h:199
This file contains miscellaneous commonly used macros and functions.
void ClearWorld()
Definition: pns_router.cpp:99
int Start() const
Definition: pns_layerset.h:83
PCB_BASE_EDIT_FRAME * frame() const
virtual void updateStartItem(const TOOL_EVENT &aEvent, bool aIgnorePads=false)
std::string KindStr() const
Function KindStr()
Definition: pns_item.cpp:71
virtual void updateEndItem(const TOOL_EVENT &aEvent)
virtual void Reset(RESET_REASON aReason) override
Function Reset() Brings the tool to a known, initial state.
bool checkSnap(ITEM *aItem)
int Width() const
Definition: pns_segment.h:88
const ITEM_SET AssembleTrivialPath(ITEM *aStart)
#define NULL
void SetHighlight(bool aEnabled, int aNetcode=-1, bool aHighlightItems=false)
Function SetHighlight Turns on/off highlighting - it may be done for the active layer,...
Definition: painter.h:141
virtual ~TOOL_BASE()
virtual int GetTopLayer() const
Definition: view.cpp:851
TOOL_EVENT.
Definition: tool_event.h:171
ITEM * m_startItem
Definition: pns_tool_base.h:68
ROUTER * m_router
Definition: pns_tool_base.h:78
PNS_MODE Mode() const
Returns the routing mode.
virtual void ForceCursorPosition(bool aEnabled, const VECTOR2D &aPosition=VECTOR2D(0, 0))
Function ForceCursorPosition() Places the cursor immediately at a given point.
void SetSnapToPads(bool aSnap)
ROUTING_SETTINGS m_savedSettings
Stores routing settings between router invocations.
Definition: pns_tool_base.h:66
KIGFX::VIEW * getView() const
Function getView()
Definition: tool_base.cpp:36
void SetInterface(ROUTER_IFACE *aIface)
Definition: pns_router.cpp:522
void Remove(SOLID *aSolid)
Function Remove()
Definition: pns_node.cpp:715
const PCB_DISPLAY_OPTIONS & displayOptions() const
void SetSnap(bool aSnap)
Definition: grid_helper.h:80
virtual void highlightNet(bool aEnabled, int aNetcode=-1)
int Modifier(int aMask=MD_MODIFIER_MASK) const
Returns information about key modifiers state (Ctrl, Alt, etc.)
Definition: tool_event.h:342
Definition: seg.h:39
bool IsItemVisible(const PNS::ITEM *aItem) override
VECTOR2I Align(const VECTOR2I &aPoint) const
virtual RENDER_SETTINGS * GetSettings()=0
Function GetSettings Returns pointer to current settings that are going to be used when drawing items...
void UpdateSizes(const SIZES_SETTINGS &aSizes)
Applies stored settings.
Definition: pns_router.cpp:317
KIGFX::VIEW_CONTROLS * controls() const
void CommitRouting(NODE *aNode)
Definition: pns_router.cpp:356
VECTOR2I m_endSnapPoint
Definition: pns_tool_base.h:74
void deleteTraces(ITEM *aStartItem, bool aWholeTrack)
VECTOR2I A
Definition: seg.h:47
bool OfKind(int aKindMask) const
Function OfKind()
Definition: pns_item.h:132
virtual ITEM * pickSingleItem(const VECTOR2I &aWhere, int aNet=-1, int aLayer=-1, bool aIgnorePads=false, const std::vector< ITEM * > aAvoidItems={})
RESET_REASON
Determines the reason of reset for a tool
Definition: tool_base.h:79
bool IsCopperLayer(LAYER_NUM aLayerId)
Function IsCopperLayer tests whether a layer is a copper layer.
PnsKind Kind() const
Function Kind()
Definition: pns_item.h:122
int GetHighlightNetCode() const
Function GetHighlightNetCode Returns netcode of currently highlighted net.
Definition: painter.h:128
void SetUseGrid(bool aGrid=true)
Definition: grid_helper.h:90
ROUTER * Router() const
void UpdateAllLayersColor()
Function UpdateAllLayersColor() Applies the new coloring scheme to all layers.
Definition: view.cpp:798
Push and Shove diff pair dimensions (gap) settings dialog.
NODE * GetWorld() const
Definition: pns_router.h:146
void SetBoard(BOARD *aBoard)
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:190
const VECTOR2D Position() const
Returns mouse cursor position in world coordinates.
Definition: tool_event.h:274
const LAYER_RANGE & Layers() const
Definition: pns_item.h:150
VECTOR2D GetCursorPosition() const
Returns 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:70
int GetCurrentLayer() const
Definition: pns_router.cpp:467
VECTOR2I B
Definition: seg.h:48