KiCad PCB EDA Suite
Loading...
Searching...
No Matches
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-2023 KiCad Developers, see AUTHORS.txt for contributors.
6 * Author: Tomasz Wlostowski <[email protected]>
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>
24using namespace std::placeholders;
25
27#include <pcb_painter.h>
28#include <pcbnew_settings.h>
29#include <view/view_controls.h>
30
32#include <wx/log.h>
33
34#include "pns_kicad_iface.h"
35#include "pns_tool_base.h"
36#include "pns_arc.h"
37#include "pns_solid.h"
38#include "pns_dragger.h"
39
40const unsigned int PNS::TOOL_BASE::COORDS_PADDING = pcbIUScale.mmToIU( 20 );
41
42using namespace KIGFX;
43
44namespace PNS {
45
46
47TOOL_BASE::TOOL_BASE( const std::string& aToolName ) :
48 PCB_TOOL_BASE( aToolName )
49{
50 m_gridHelper = nullptr;
51 m_iface = nullptr;
52 m_router = nullptr;
53 m_cancelled = false;
54
55 m_startItem = nullptr;
56
57 m_endItem = nullptr;
58 m_gridHelper = nullptr;
59
60 m_cancelled = false;
61}
62
63
65{
66 delete m_gridHelper;
67 delete m_router;
68 delete m_iface; // Delete after m_router because PNS::NODE dtor needs m_ruleResolver
69}
70
71
73{
74 delete m_gridHelper;
75 delete m_router;
76 delete m_iface; // Delete after m_router because PNS::NODE dtor needs m_ruleResolver
77
78 if( aReason == RESET_REASON::SHUTDOWN )
79 {
80 m_gridHelper = nullptr;
81 m_router = nullptr;
82 m_iface = nullptr;
83 return;
84 }
85
89 m_iface->SetHostTool( this );
90
91 m_router = new ROUTER;
95
97
99
100 if( !settings->m_PnsSettings )
101 settings->m_PnsSettings = std::make_unique<ROUTING_SETTINGS>( settings, "tools.pns" );
102
103 m_router->LoadSettings( settings->m_PnsSettings.get() );
104
105 m_gridHelper = new PCB_GRID_HELPER( m_toolMgr, frame()->GetMagneticItemsSettings() );
106}
107
108
109ITEM* TOOL_BASE::pickSingleItem( const VECTOR2I& aWhere, NET_HANDLE aNet, int aLayer,
110 bool aIgnorePads, const std::vector<ITEM*> aAvoidItems )
111{
112 int tl = aLayer > 0 ? aLayer : getView()->GetTopLayer();
113 int maxSlopRadius = std::max( m_gridHelper->GetGrid().x, m_gridHelper->GetGrid().y );
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 auto haveCandidates =
126 [&]()
127 {
128 for( ITEM* item : prioritized )
129 {
130 if( item )
131 return true;
132 }
133
134 return false;
135 };
136
137 for( int slopRadius : { 0, maxSlopRadius } )
138 {
139 ITEM_SET candidates = m_router->QueryHoverItems( aWhere, slopRadius );
140
141 for( ITEM* item : candidates.Items() )
142 {
143 if( !item->IsRoutable() )
144 continue;
145
146 if( !IsCopperLayer( item->Layers().Start() ) )
147 continue;
148
149 if( !m_iface->IsAnyLayerVisible( item->Layers() ) )
150 continue;
151
152 if( alg::contains( aAvoidItems, item ) )
153 continue;
154
155 // fixme: this causes flicker with live loop removal...
156 //if( item->Parent() && !item->Parent()->ViewIsVisible() )
157 // continue;
158
159 if( item->OfKind( ITEM::SOLID_T ) && aIgnorePads )
160 {
161 continue;
162 }
163 else if( m_router->GetInterface()->GetNetCode( aNet) <= 0 || item->Net() == aNet )
164 {
165 if( item->OfKind( ITEM::VIA_T | ITEM::SOLID_T ) )
166 {
167 SEG::ecoord d = ( item->Shape()->Centre() - aWhere ).SquaredEuclideanNorm();
168
169 if( d < dist[2] )
170 {
171 prioritized[2] = item;
172 dist[2] = d;
173 }
174
175 if( item->Layers().Overlaps( tl ) && d < dist[0] )
176 {
177 prioritized[0] = item;
178 dist[0] = d;
179 }
180 }
181 else // ITEM::SEGMENT_T | ITEM::ARC_T
182 {
183 LINKED_ITEM* li = static_cast<LINKED_ITEM*>( item );
184 SEG::ecoord d = std::min( ( li->Anchor( 0 ) - aWhere ).SquaredEuclideanNorm(),
185 ( li->Anchor( 1 ) - aWhere ).SquaredEuclideanNorm() );
186
187 if( d < dist[3] )
188 {
189 prioritized[3] = item;
190 dist[3] = d;
191 }
192
193 if( item->Layers().Overlaps( tl ) && d < dist[1] )
194 {
195 prioritized[1] = item;
196 dist[1] = d;
197 }
198 }
199 }
200 else if( item->OfKind( ITEM::SOLID_T ) && item->IsFreePad() )
201 {
202 // Allow free pads only when already inside pad
203 if( item->Shape()->Collide( aWhere ) )
204 {
205 prioritized[0] = item;
206 dist[0] = 0;
207 }
208 }
209 else if ( item->Net() == 0 && m_router->Settings().Mode() == RM_MarkObstacles )
210 {
211 // Allow unconnected items as last resort in RM_MarkObstacles mode
212 if( item->Layers().Overlaps( tl ) )
213 prioritized[4] = item;
214 }
215 }
216
217 if( haveCandidates() )
218 break;
219 }
220
221 ITEM* rv = nullptr;
222
223 bool highContrast = ( frame()->GetDisplayOptions().m_ContrastModeDisplay != HIGH_CONTRAST_MODE::NORMAL );
224
225 for( ITEM* item : prioritized )
226 {
227 if( highContrast && item && !item->Layers().Overlaps( tl ) )
228 item = nullptr;
229
230 if( item && ( aLayer < 0 || item->Layers().Overlaps( aLayer ) ) )
231 {
232 rv = item;
233 break;
234 }
235 }
236
237 if( rv )
238 {
239 wxLogTrace( wxT( "PNS" ), wxT( "%s, layer : %d, tl: %d" ),
240 rv->KindStr().c_str(),
241 rv->Layers().Start(),
242 tl );
243 }
244
245 return rv;
246}
247
248
249void TOOL_BASE::highlightNets( bool aEnabled, std::set<NET_HANDLE> aNets )
250{
252 std::set<int> netcodes;
253
254 for( const NET_HANDLE& net : aNets )
255 netcodes.insert( m_router->GetInterface()->GetNetCode( net ) );
256
257 if( netcodes.size() > 0 && aEnabled )
258 {
259 // If the user has previously set some of the routed nets to be highlighted,
260 // we assume they want to keep them highlighted after routing
261
262 const std::set<int>& currentNetCodes = rs->GetHighlightNetCodes();
263 bool keep = false;
264
265 for( const int& netcode : netcodes )
266 {
267 if( currentNetCodes.find( netcode ) != currentNetCodes.end() )
268 {
269 keep = true;
270 break;
271 }
272 }
273
274 if( rs->IsHighlightEnabled() && keep )
275 m_startHighlightNetcodes = currentNetCodes;
276 else
278
279 rs->SetHighlight( netcodes, true );
280 }
281 else
282 {
284 }
285
286 // Do not remove this call. This is required to update the layers when we highlight a net.
287 // In this case, highlighting a net dims all other elements, so the colors need to update
289}
290
291
293{
294 // Sync PNS engine settings with the general PCB editor options.
296
297 // If we're dragging a track segment, don't try to snap to items that are part of the original line.
299 && m_router->GetDragger() )
300 {
301 DRAGGER* dragger = dynamic_cast<DRAGGER*>( m_router->GetDragger() );
302 LINKED_ITEM* linkedItem = dynamic_cast<LINKED_ITEM*>( aItem );
303
304 if( dragger && linkedItem && dragger->GetOriginalLine().ContainsLink( linkedItem ) )
305 return false;
306 }
307
309
310 pnss.SetSnapToPads( magSettings->pads == MAGNETIC_OPTIONS::CAPTURE_CURSOR_IN_TRACK_TOOL
311 || magSettings->pads == MAGNETIC_OPTIONS::CAPTURE_ALWAYS );
312
313 pnss.SetSnapToTracks( magSettings->tracks == MAGNETIC_OPTIONS::CAPTURE_CURSOR_IN_TRACK_TOOL
314 || magSettings->tracks == MAGNETIC_OPTIONS::CAPTURE_ALWAYS );
315
316 if( aItem )
317 {
319 return pnss.GetSnapToTracks();
320 else if( aItem->OfKind( ITEM::SOLID_T ) )
321 return pnss.GetSnapToPads();
322 }
323
324 return false;
325}
326
327
328void TOOL_BASE::updateStartItem( const TOOL_EVENT& aEvent, bool aIgnorePads )
329{
330 int tl = getView()->GetTopLayer();
331 GAL* gal = m_toolMgr->GetView()->GetGAL();
332 VECTOR2I pos = aEvent.HasPosition() ? (VECTOR2I) aEvent.Position() : m_startSnapPoint;
333
334 pos = GetClampedCoords( pos, COORDS_PADDING );
335
336 if( aEvent.Modifier( MD_CTRL ) && aEvent.Modifier( MD_SHIFT ) )
337 {
338 m_startItem = nullptr;
341 return;
342 }
343
344 controls()->ForceCursorPosition( false );
346 m_gridHelper->SetSnap( !aEvent.Modifier( MD_SHIFT ) );
347
348 m_startItem = pickSingleItem( pos, nullptr, -1, aIgnorePads );
349
351 m_startItem = nullptr;
352
355}
356
357
359{
360 int layer;
361 GAL* gal = m_toolMgr->GetView()->GetGAL();
362
364 m_gridHelper->SetSnap( !aEvent.Modifier( MD_SHIFT ) );
365
366 controls()->ForceCursorPosition( false );
367
368 VECTOR2I mousePos = GetClampedCoords( controls()->GetMousePosition(), COORDS_PADDING );
369
370 if( m_router->GetState() == ROUTER::ROUTE_TRACK && aEvent.IsDrag() )
371 {
372 // If the user is moving the mouse quickly while routing then clicks will come in as
373 // short drags. In this case we want to use the drag origin rather than the current
374 // mouse position.
375 mousePos = aEvent.DragOrigin();
376 }
377
379 ( m_router->GetCurrentNets().empty() || m_router->GetCurrentNets().front() == nullptr ) )
380 {
381 m_endSnapPoint = snapToItem( nullptr, mousePos );
383 m_endItem = nullptr;
384
385 return;
386 }
387
388 if( m_router->IsPlacingVia() )
389 layer = -1;
390 else
391 layer = m_router->GetCurrentLayer();
392
393 ITEM* endItem = nullptr;
394
395 std::vector<NET_HANDLE> nets = m_router->GetCurrentNets();
396
397 for( NET_HANDLE net : nets )
398 {
399 endItem = pickSingleItem( mousePos, net, layer, false, { m_startItem } );
400
401 if( endItem )
402 break;
403 }
404
405 if( m_gridHelper->GetSnap() && checkSnap( endItem ) )
406 {
407 m_endItem = endItem;
408 m_endSnapPoint = snapToItem( endItem, mousePos );
409 }
410 else
411 {
412 m_endItem = nullptr;
414 : GRID_WIRES );
415 }
416
418
419 if( m_endItem )
420 {
421 wxLogTrace( wxT( "PNS" ), wxT( "%s, layer : %d" ),
422 m_endItem->KindStr().c_str(),
423 m_endItem->Layers().Start() );
424 }
425}
426
427
429{
430 return m_router;
431}
432
433
435{
436 return m_iface;
437}
438
439
440const VECTOR2I TOOL_BASE::snapToItem( ITEM* aItem, const VECTOR2I& aP )
441{
442 if( !aItem || !m_iface->IsItemVisible( aItem ) )
443 {
445 }
446
447 switch( aItem->Kind() )
448 {
449 case ITEM::SOLID_T:
450 {
451 SOLID* solid = static_cast<SOLID*>( aItem );
452
453 if( solid->AnchorPoints().empty() )
454 return solid->Anchor( 0 );
455
457 SEG::ecoord minDist = std::numeric_limits<SEG::ecoord>::max();
458
459 for( VECTOR2I anchorCandidate : solid->AnchorPoints() )
460 {
461 SEG::ecoord distSq = ( aP - anchorCandidate ).SquaredEuclideanNorm();
462
463 if( distSq < minDist )
464 {
465 minDist = distSq;
466 anchor = anchorCandidate;
467 }
468 }
469
470 return anchor;
471 }
472
473 case ITEM::VIA_T:
474 return static_cast<VIA*>( aItem )->Pos();
475
476 case ITEM::SEGMENT_T:
477 case ITEM::ARC_T:
478 {
479 LINKED_ITEM* li = static_cast<LINKED_ITEM*>( aItem );
480 VECTOR2I A = li->Anchor( 0 );
481 VECTOR2I B = li->Anchor( 1 );
482 SEG::ecoord w_sq = SEG::Square( li->Width() / 2 );
483 SEG::ecoord distA_sq = ( aP - A ).SquaredEuclideanNorm();
484 SEG::ecoord distB_sq = ( aP - B ).SquaredEuclideanNorm();
485
486 if( distA_sq < w_sq || distB_sq < w_sq )
487 {
488 return ( distA_sq < distB_sq ) ? A : B;
489 }
490 else if( aItem->Kind() == ITEM::SEGMENT_T )
491 {
492 // TODO(snh): Clean this up
493 SEGMENT* seg = static_cast<SEGMENT*>( li );
494 return m_gridHelper->AlignToSegment( aP, seg->Seg() );
495 }
496 else if( aItem->Kind() == ITEM::ARC_T )
497 {
498 ARC* arc = static_cast<ARC*>( li );
499 return m_gridHelper->AlignToArc( aP, *static_cast<const SHAPE_ARC*>( arc->Shape() ) );
500 }
501
502 break;
503 }
504
505 default:
506 break;
507 }
508
510}
511
512}
constexpr EDA_IU_SCALE pcbIUScale
Definition: base_units.h:108
bool GetSnap() const
Definition: grid_helper.h:107
void SetSnap(bool aSnap)
Definition: grid_helper.h:106
bool GetUseGrid() const
Definition: grid_helper.h:110
void SetUseGrid(bool aSnapToGrid)
Definition: grid_helper.h:109
VECTOR2I GetGrid() const
Definition: grid_helper.cpp:53
Abstract interface for drawing on a 2D-surface.
bool GetGridSnapping() const
virtual RENDER_SETTINGS * GetSettings()=0
Return a pointer to current settings that are going to be used when drawing items.
Container for all the knowledge about how graphical objects are drawn on any output surface/device.
const std::set< int > & GetHighlightNetCodes() const
Return the netcode of currently highlighted net.
bool IsHighlightEnabled() const
Return current highlight setting.
void SetHighlight(bool aEnabled, int aNetcode=-1, bool aMulti=false)
Turns on/off highlighting.
virtual void ForceCursorPosition(bool aEnabled, const VECTOR2D &aPosition=VECTOR2D(0, 0))
Place the cursor immediately at a given point.
virtual VECTOR2D GetMousePosition(bool aWorldCoordinates=true) const =0
Return the current mouse pointer position.
void UpdateAllLayersColor()
Apply the new coloring scheme to all layers.
Definition: view.cpp:804
virtual int GetTopLayer() const
Definition: view.cpp:862
GAL * GetGAL() const
Return the #GAL this view is using to draw graphical primitives.
Definition: view.h:203
PAINTER * GetPainter() const
Return the painter object used by the view for drawing #VIEW_ITEMS.
Definition: view.h:221
int Start() const
Definition: pns_layerset.h:82
bool Overlaps(const LAYER_RANGE &aOther) const
Definition: pns_layerset.h:67
std::unique_ptr< PNS::ROUTING_SETTINGS > m_PnsSettings
const PCB_DISPLAY_OPTIONS & GetDisplayOptions() const
Display options control the way tracks, vias, outlines and other things are shown (for instance solid...
PCBNEW_SETTINGS * GetPcbNewSettings() const
virtual MAGNETIC_SETTINGS * GetMagneticItemsSettings()
HIGH_CONTRAST_MODE m_ContrastModeDisplay
How inactive layers are displayed.
VECTOR2I AlignToArc(const VECTOR2I &aPoint, const SHAPE_ARC &aSeg)
VECTOR2I AlignToSegment(const VECTOR2I &aPoint, const SEG &aSeg)
virtual VECTOR2I Align(const VECTOR2I &aPoint, GRID_HELPER_GRIDS aGrid) const
Definition: grid_helper.h:60
PCB_BASE_EDIT_FRAME * frame() const
KIGFX::VIEW_CONTROLS * controls() const
BOARD * board() const
const SHAPE * Shape() const override
Return the geometrical shape of the item.
Definition: pns_arc.h:78
DRAGGER.
Definition: pns_dragger.h:48
const LINE & GetOriginalLine()
Definition: pns_dragger.h:103
std::vector< ITEM * > & Items()
Definition: pns_itemset.h:87
Base class for PNS router board items.
Definition: pns_item.h:97
PnsKind Kind() const
Return the type (kind) of the item.
Definition: pns_item.h:167
@ SEGMENT_T
Definition: pns_item.h:106
const LAYER_RANGE & Layers() const
Definition: pns_item.h:196
bool OfKind(int aKindMask) const
Definition: pns_item.h:175
virtual VECTOR2I Anchor(int n) const
Definition: pns_item.h:237
std::string KindStr() const
Definition: pns_item.cpp:282
virtual int Width() const
virtual int GetNetCode(NET_HANDLE aNet) const =0
void ClearWorld()
Definition: pns_router.cpp:104
ROUTER_IFACE * GetInterface() const
Definition: pns_router.h:218
void UpdateSizes(const SIZES_SETTINGS &aSizes)
Applies stored settings.
Definition: pns_router.cpp:722
void LoadSettings(ROUTING_SETTINGS *aSettings)
Changes routing settings to ones passed in the parameter.
Definition: pns_router.h:206
const ITEM_SET QueryHoverItems(const VECTOR2I &aP, int aSlopRadius=0)
Definition: pns_router.cpp:123
void SetInterface(ROUTER_IFACE *aIface)
void SyncWorld()
Definition: pns_router.cpp:94
bool IsPlacingVia() const
Definition: pns_router.cpp:998
ROUTING_SETTINGS & Settings()
Definition: pns_router.h:192
DRAG_ALGO * GetDragger()
Definition: pns_router.h:140
RouterState GetState() const
Definition: pns_router.h:138
int GetCurrentLayer() const
Definition: pns_router.cpp:981
const std::vector< NET_HANDLE > GetCurrentNets() const
Definition: pns_router.cpp:970
Contain all persistent settings of the router, such as the mode, optimization effort,...
void SetSnapToTracks(bool aSnap)
void SetSnapToPads(bool aSnap)
PNS_MODE Mode() const
Set the routing mode.
const SEG & Seg() const
Definition: pns_segment.h:84
const std::vector< VECTOR2I > & AnchorPoints() const
Definition: pns_solid.h:114
virtual VECTOR2I Anchor(int aN) const override
Definition: pns_solid.cpp:95
bool checkSnap(ITEM *aItem)
virtual void updateStartItem(const TOOL_EVENT &aEvent, bool aIgnorePads=false)
ROUTER * Router() const
PNS_KICAD_IFACE * GetInterface() const
std::set< int > m_startHighlightNetcodes
Definition: pns_tool_base.h:73
const VECTOR2I snapToItem(ITEM *aSnapToItem, const VECTOR2I &aP)
virtual void Reset(RESET_REASON aReason) override
Bring the tool to a known, initial state.
virtual void highlightNets(bool aEnabled, std::set< NET_HANDLE > aNetcodes={})
virtual ~TOOL_BASE()
SIZES_SETTINGS m_savedSizes
Definition: pns_tool_base.h:70
PNS_KICAD_IFACE * m_iface
Definition: pns_tool_base.h:79
virtual ITEM * pickSingleItem(const VECTOR2I &aWhere, NET_HANDLE aNet=nullptr, int aLayer=-1, bool aIgnorePads=false, const std::vector< ITEM * > aAvoidItems={})
ITEM * m_startItem
Definition: pns_tool_base.h:71
virtual void updateEndItem(const TOOL_EVENT &aEvent)
TOOL_BASE(const std::string &aToolName)
static const unsigned int COORDS_PADDING
Definition: pns_tool_base.h:84
ROUTER * m_router
Definition: pns_tool_base.h:80
VECTOR2I m_endSnapPoint
Definition: pns_tool_base.h:76
PCB_GRID_HELPER * m_gridHelper
Definition: pns_tool_base.h:78
VECTOR2I m_startSnapPoint
Definition: pns_tool_base.h:72
void SetBoard(BOARD *aBoard)
void SetView(KIGFX::VIEW *aView)
virtual void SetHostTool(PCB_TOOL_BASE *aTool)
bool IsItemVisible(const PNS::ITEM *aItem) const override
bool IsAnyLayerVisible(const LAYER_RANGE &aLayer) const override
VECTOR2I::extended_type ecoord
Definition: seg.h:44
static SEG::ecoord Square(int a)
Definition: seg.h:123
TOOL_MANAGER * m_toolMgr
Definition: tool_base.h:218
KIGFX::VIEW * getView() const
Returns the instance of #VIEW object used in the application.
Definition: tool_base.cpp:36
RESET_REASON
Determine the reason of reset for a tool.
Definition: tool_base.h:78
@ SHUTDOWN
Tool is being shut down.
Definition: tool_base.h:84
Generic, UI-independent tool event.
Definition: tool_event.h:167
bool HasPosition() const
Definition: tool_event.h:256
bool DisableGridSnapping() const
Definition: tool_event.h:363
const VECTOR2D Position() const
Returns the point where dragging has started.
Definition: tool_event.h:285
bool IsDrag(int aButtonMask=BUT_ANY) const
Definition: tool_event.h:307
int Modifier(int aMask=MD_MODIFIER_MASK) const
Definition: tool_event.h:358
const VECTOR2D DragOrigin() const
Returns information about mouse buttons state.
Definition: tool_event.h:291
KIGFX::VIEW * GetView() const
Definition: tool_manager.h:391
static constexpr extended_type ECOORD_MAX
Definition: vector2d.h:75
VECTOR2< ret_type > GetClampedCoords(const VECTOR2< in_type > &aCoords, pad_type aPadding=1u)
Clamps a vector to values that can be negated, respecting numeric limits of coordinates data type wit...
@ GRID_VIAS
Definition: grid_helper.h:43
@ GRID_WIRES
Definition: grid_helper.h:42
bool IsCopperLayer(int aLayerId)
Tests whether a layer is a copper layer.
Definition: layer_ids.h:531
The Cairo implementation of the graphics abstraction layer.
Definition: color4d.cpp:247
Push and Shove diff pair dimensions (gap) settings dialog.
@ RM_MarkObstacles
Ignore collisions, mark obstacles.
void * NET_HANDLE
Definition: pns_item.h:54
bool contains(const _Container &__container, _Value __value)
Returns true if the container contains the given value.
Definition: kicad_algo.h:100
constexpr int mmToIU(double mm) const
Definition: base_units.h:88
MAGNETIC_OPTIONS tracks
MAGNETIC_OPTIONS pads
@ MD_CTRL
Definition: tool_event.h:143
@ MD_SHIFT
Definition: tool_event.h:142
VECTOR2< int32_t > VECTOR2I
Definition: vector2d.h:673