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-2021 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 <pcb_painter.h>
27 #include <pcbnew_settings.h>
28 
29 #include <tools/pcb_grid_helper.h>
30 #include <wx/log.h>
31 
32 #include "pns_kicad_iface.h"
33 #include "pns_tool_base.h"
34 #include "pns_arc.h"
35 #include "pns_solid.h"
36 
37 
38 using namespace KIGFX;
39 
40 namespace PNS {
41 
42 
43 TOOL_BASE::TOOL_BASE( const std::string& aToolName ) :
44  PCB_TOOL_BASE( aToolName )
45 {
46  m_gridHelper = nullptr;
47  m_iface = nullptr;
48  m_router = nullptr;
49  m_cancelled = false;
50 
51  m_startItem = nullptr;
52  m_startHighlight = false;
53 
54  m_endItem = nullptr;
55  m_gridHelper = nullptr;
56 
57  m_cancelled = false;
58 }
59 
60 
62 {
63  delete m_gridHelper;
64  delete m_iface;
65  delete m_router;
66 }
67 
68 
70 {
71  delete m_gridHelper;
72  delete m_iface;
73  delete m_router;
74 
76  m_iface->SetBoard( board() );
77  m_iface->SetView( getView() );
78  m_iface->SetHostTool( this );
79  m_iface->SetDisplayOptions( &( frame()->GetDisplayOptions() ) );
80 
81  m_router = new ROUTER;
85 
87 
88  PCBNEW_SETTINGS* settings = frame()->GetPcbNewSettings();
89 
90  if( !settings->m_PnsSettings )
91  settings->m_PnsSettings = std::make_unique<ROUTING_SETTINGS>( settings, "tools.pns" );
92 
93  m_router->LoadSettings( settings->m_PnsSettings.get() );
94 
95  m_gridHelper = new PCB_GRID_HELPER( m_toolMgr, frame()->GetMagneticItemsSettings() );
96 }
97 
98 
99 ITEM* TOOL_BASE::pickSingleItem( const VECTOR2I& aWhere, int aNet, int aLayer, bool aIgnorePads,
100  const std::vector<ITEM*> aAvoidItems )
101 {
102  int tl = aLayer > 0 ? aLayer : getView()->GetTopLayer();
103 
104  static const int candidateCount = 5;
105  ITEM* prioritized[candidateCount];
106  SEG::ecoord dist[candidateCount];
107 
108  for( int i = 0; i < candidateCount; i++ )
109  {
110  prioritized[i] = nullptr;
111  dist[i] = VECTOR2I::ECOORD_MAX;
112  }
113 
114  ITEM_SET candidates = m_router->QueryHoverItems( aWhere );
115 
116  if( candidates.Empty() )
117  candidates = m_router->QueryHoverItems( aWhere, true );
118 
119  for( ITEM* item : candidates.Items() )
120  {
121  if( !item->IsRoutable() )
122  continue;
123 
124  if( !IsCopperLayer( item->Layers().Start() ) )
125  continue;
126 
127  if( !m_iface->IsAnyLayerVisible( item->Layers() ) )
128  continue;
129 
130  if( alg::contains( aAvoidItems, item ) )
131  continue;
132 
133  // fixme: this causes flicker with live loop removal...
134  //if( item->Parent() && !item->Parent()->ViewIsVisible() )
135  // continue;
136 
137  if( aNet <= 0 || item->Net() == aNet )
138  {
139  if( item->OfKind( ITEM::VIA_T | ITEM::SOLID_T ) )
140  {
141  if( item->OfKind( ITEM::SOLID_T ) && aIgnorePads )
142  continue;
143 
144  SEG::ecoord d = ( item->Shape()->Centre() - aWhere ).SquaredEuclideanNorm();
145 
146  if( d < dist[2] )
147  {
148  prioritized[2] = item;
149  dist[2] = d;
150  }
151 
152  if( item->Layers().Overlaps( tl ) && d < dist[0] )
153  {
154  prioritized[0] = item;
155  dist[0] = d;
156  }
157  }
158  else // ITEM::SEGMENT_T | ITEM::ARC_T
159  {
160  LINKED_ITEM* li = static_cast<LINKED_ITEM*>( item );
161  SEG::ecoord d = std::min( ( li->Anchor( 0 ) - aWhere ).SquaredEuclideanNorm(),
162  ( li->Anchor( 1 ) - aWhere ).SquaredEuclideanNorm() );
163 
164  if( d < dist[3] )
165  {
166  prioritized[3] = item;
167  dist[3] = d;
168  }
169 
170  if( item->Layers().Overlaps( tl ) && d < dist[1] )
171  {
172  prioritized[1] = item;
173  dist[1] = d;
174  }
175  }
176  }
177  else if ( item->Net() == 0 && m_router->Settings().Mode() == RM_MarkObstacles )
178  {
179  // Allow unconnected items as last resort in RM_MarkObstacles mode
180  if( item->OfKind( ITEM::SOLID_T ) && aIgnorePads )
181  continue;
182 
183  if( item->Layers().Overlaps( tl ) )
184  prioritized[4] = item;
185  }
186  }
187 
188  ITEM* rv = nullptr;
189 
191 
192  for( int i = 0; i < candidateCount; i++ )
193  {
194  ITEM* item = prioritized[i];
195 
196  if( highContrast && item && !item->Layers().Overlaps( tl ) )
197  item = nullptr;
198 
199  if( item && ( aLayer < 0 || item->Layers().Overlaps( aLayer ) ) )
200  {
201  rv = item;
202  break;
203  }
204  }
205 
206  if( rv )
207  {
208  wxLogTrace( "PNS", "%s, layer : %d, tl: %d", rv->KindStr().c_str(), rv->Layers().Start(),
209  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
225  && rs->GetHighlightNetCodes().count( aNetcode ) );
226 
227  rs->SetHighlight( true, aNetcode );
228  }
229  else
230  {
231  if( !m_startHighlight )
232  rs->SetHighlight( false );
233 
234  m_startHighlight = false;
235  }
236 
238 }
239 
240 
242 {
243  // Sync PNS engine settings with the general PCB editor options.
244  auto& pnss = m_router->Settings();
245 
246  pnss.SetSnapToPads(
247  frame()->GetMagneticItemsSettings()->pads == MAGNETIC_OPTIONS::CAPTURE_CURSOR_IN_TRACK_TOOL ||
248  frame()->GetMagneticItemsSettings()->pads == MAGNETIC_OPTIONS::CAPTURE_ALWAYS );
249 
250  pnss.SetSnapToTracks(
251  frame()->GetMagneticItemsSettings()->tracks == MAGNETIC_OPTIONS::CAPTURE_CURSOR_IN_TRACK_TOOL
252  || frame()->GetMagneticItemsSettings()->tracks == MAGNETIC_OPTIONS::CAPTURE_ALWAYS );
253 
254  if( aItem )
255  {
256  if( aItem->OfKind( ITEM::VIA_T | ITEM::SEGMENT_T | ITEM::ARC_T ) )
257  return pnss.GetSnapToTracks();
258  else if( aItem->OfKind( ITEM::SOLID_T ) )
259  return pnss.GetSnapToPads();
260  }
261 
262  return false;
263 }
264 
265 
266 void TOOL_BASE::updateStartItem( const TOOL_EVENT& aEvent, bool aIgnorePads )
267 {
268  int tl = getView()->GetTopLayer();
269  VECTOR2I cp = controls()->GetCursorPosition( !aEvent.Modifier( MD_SHIFT ) );
270  VECTOR2I p;
271  GAL* gal = m_toolMgr->GetView()->GetGAL();
272 
273  controls()->ForceCursorPosition( false );
275  m_gridHelper->SetSnap( !aEvent.Modifier( MD_SHIFT ) );
276 
277  if( aEvent.IsMotion() || aEvent.IsClick() )
278  p = aEvent.Position();
279  else
280  p = cp;
281 
282  m_startItem = pickSingleItem( p, -1, -1, aIgnorePads );
283 
285  m_startItem = nullptr;
286 
289 }
290 
291 
293 {
294  int layer;
295  GAL* gal = m_toolMgr->GetView()->GetGAL();
296 
298  m_gridHelper->SetSnap( !aEvent.Modifier( MD_SHIFT ) );
299 
300  controls()->ForceCursorPosition( false );
301  VECTOR2I mousePos = controls()->GetMousePosition();
302 
303  if( m_router->Settings().Mode() != RM_MarkObstacles &&
304  ( m_router->GetCurrentNets().empty() || m_router->GetCurrentNets().front() < 0 ) )
305  {
306  m_endSnapPoint = snapToItem( nullptr, mousePos );
308  m_endItem = nullptr;
309 
310  return;
311  }
312 
313  if( m_router->IsPlacingVia() )
314  layer = -1;
315  else
316  layer = m_router->GetCurrentLayer();
317 
318  ITEM* endItem = nullptr;
319 
320  std::vector<int> nets = m_router->GetCurrentNets();
321 
322  for( int net : nets )
323  {
324  endItem = pickSingleItem( mousePos, net, layer, false, { m_startItem } );
325 
326  if( endItem )
327  break;
328  }
329 
330  if( m_gridHelper->GetSnap() && checkSnap( endItem ) )
331  {
332  m_endItem = endItem;
333  m_endSnapPoint = snapToItem( endItem, mousePos );
334  }
335  else
336  {
337  m_endItem = nullptr;
338  m_endSnapPoint = m_gridHelper->Align( mousePos );
339  }
340 
342 
343  if( m_endItem )
344  {
345  wxLogTrace( "PNS", "%s, layer : %d",
346  m_endItem->KindStr().c_str(),
347  m_endItem->Layers().Start() );
348  }
349 }
350 
351 
353 {
354  return m_router;
355 }
356 
357 
358 const VECTOR2I TOOL_BASE::snapToItem( ITEM* aItem, const VECTOR2I& aP )
359 {
360  if( !aItem || !m_iface->IsItemVisible( aItem ) )
361  {
362  return m_gridHelper->Align( aP );
363  }
364 
365  switch( aItem->Kind() )
366  {
367  case ITEM::SOLID_T:
368  return static_cast<SOLID*>( aItem )->Pos();
369 
370  case ITEM::VIA_T:
371  return static_cast<VIA*>( aItem )->Pos();
372 
373  case ITEM::SEGMENT_T:
374  case ITEM::ARC_T:
375  {
376  LINKED_ITEM* li = static_cast<LINKED_ITEM*>( aItem );
377  VECTOR2I A = li->Anchor( 0 );
378  VECTOR2I B = li->Anchor( 1 );
379  SEG::ecoord w_sq = SEG::Square( li->Width() / 2 );
380  SEG::ecoord distA_sq = ( aP - A ).SquaredEuclideanNorm();
381  SEG::ecoord distB_sq = ( aP - B ).SquaredEuclideanNorm();
382 
383  if( distA_sq < w_sq || distB_sq < w_sq )
384  {
385  return ( distA_sq < distB_sq ) ? A : B;
386  }
387  else if( aItem->Kind() == ITEM::SEGMENT_T )
388  {
389  // TODO(snh): Clean this up
390  SEGMENT* seg = static_cast<SEGMENT*>( li );
391  return m_gridHelper->AlignToSegment( aP, seg->Seg() );
392  }
393  else if( aItem->Kind() == ITEM::ARC_T )
394  {
395  ARC* arc = static_cast<ARC*>( li );
396  return m_gridHelper->AlignToArc( aP, *static_cast<const SHAPE_ARC*>( arc->Shape() ) );
397  }
398 
399  break;
400  }
401 
402  default:
403  break;
404  }
405 
406  return m_gridHelper->Align( aP );
407 }
408 
409 }
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:283
bool GetSnap() const
Definition: grid_helper.h:65
void SetView(KIGFX::VIEW *aView)
bool Empty() const
Definition: pns_itemset.h:130
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:236
Container for all the knowledge about how graphical objects are drawn on any output surface/device.
const ITEM_SET QueryHoverItems(const VECTOR2I &aP, bool aUseClearance=false)
Definition: pns_router.cpp:120
VECTOR2I m_startSnapPoint
Definition: pns_tool_base.h:70
bool IsClick(int aButtonMask=BUT_ANY) const
Definition: tool_event.cpp:199
ENTRIES & Items()
Definition: pns_itemset.h:135
TOOL_MANAGER * m_toolMgr
Definition: tool_base.h:214
VECTOR2I::extended_type ecoord
Definition: seg.h:43
SIZES_SETTINGS m_savedSizes
Definition: pns_tool_base.h:68
void SyncWorld()
Definition: pns_router.cpp:92
bool IsPlacingVia() const
Definition: pns_router.cpp:798
bool IsMotion() const
Definition: tool_event.h:300
bool Overlaps(const LAYER_RANGE &aOther) const
Definition: pns_layerset.h:67
const std::vector< int > GetCurrentNets() const
Definition: pns_router.cpp:770
GAL * GetGAL() const
Return the #GAL this view is using to draw graphical primitives.
Definition: view.h:190
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:122
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:208
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.
const VECTOR2I snapToItem(ITEM *aSnapToItem, const VECTOR2I &aP)
bool checkSnap(ITEM *aItem)
PCB_GRID_HELPER * m_gridHelper
Definition: pns_tool_base.h:76
virtual ~TOOL_BASE()
bool IsItemVisible(const PNS::ITEM *aItem) const override
virtual int GetTopLayer() const
Definition: view.cpp:821
Generic, UI-independent tool event.
Definition: tool_event.h:152
Inactive 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)
bool DisableGridSnapping() const
Definition: tool_event.h:341
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:99
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:840
bool IsCopperLayer(LAYER_NUM aLayerId)
Tests whether a layer is a copper layer.
Definition: layer_ids.h: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:336
virtual VECTOR2I Anchor(int n) const
Definition: pns_item.h:215
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:77
virtual RENDER_SETTINGS * GetSettings()=0
Return a 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:560
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:136
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:77
PnsKind Kind() const
Return the type (kind) of the item.
Definition: pns_item.h:128
void LoadSettings(ROUTING_SETTINGS *aSettings)
Changes routing settings to ones passed in the parameter.
Definition: pns_router.h:195
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:766
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:181
VECTOR2I AlignToSegment(const VECTOR2I &aPoint, const SEG &aSeg)
const VECTOR2D Position() const
Returns the point where dragging has started.
Definition: tool_event.h:263
const LAYER_RANGE & Layers() const
Definition: pns_item.h:152
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:68
Abstract interface for drawing on a 2D-surface.
bool IsAnyLayerVisible(const LAYER_RANGE &aLayer) const override
int GetCurrentLayer() const
Definition: pns_router.cpp:781