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
31#include <wx/log.h>
32
33#include "pns_kicad_iface.h"
34#include "pns_tool_base.h"
35#include "pns_arc.h"
36#include "pns_solid.h"
37#include "pns_dragger.h"
38
39
40using namespace KIGFX;
41
42namespace PNS {
43
44
45TOOL_BASE::TOOL_BASE( const std::string& aToolName ) :
46 PCB_TOOL_BASE( aToolName )
47{
48 m_gridHelper = nullptr;
49 m_iface = nullptr;
50 m_router = nullptr;
51 m_cancelled = false;
52
53 m_startItem = nullptr;
54
55 m_endItem = nullptr;
56 m_gridHelper = nullptr;
57
58 m_cancelled = false;
59}
60
61
63{
64 delete m_gridHelper;
65 delete m_router;
66 delete m_iface; // Delete after m_router because PNS::NODE dtor needs m_ruleResolver
67}
68
69
71{
72 delete m_gridHelper;
73 delete m_router;
74 delete m_iface; // Delete after m_router because PNS::NODE dtor needs m_ruleResolver
75
79 m_iface->SetHostTool( this );
80
81 m_router = new ROUTER;
85
87
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
99ITEM* TOOL_BASE::pickSingleItem( const VECTOR2I& aWhere, NET_HANDLE aNet, int aLayer,
100 bool aIgnorePads, const std::vector<ITEM*> aAvoidItems )
101{
102 int tl = aLayer > 0 ? aLayer : getView()->GetTopLayer();
103 int maxSlopRadius = std::max( m_gridHelper->GetGrid().x, m_gridHelper->GetGrid().y );
104
105 static const int candidateCount = 5;
106 ITEM* prioritized[candidateCount];
107 SEG::ecoord dist[candidateCount];
108
109 for( int i = 0; i < candidateCount; i++ )
110 {
111 prioritized[i] = nullptr;
112 dist[i] = VECTOR2I::ECOORD_MAX;
113 }
114
115 auto haveCandidates =
116 [&]()
117 {
118 for( ITEM* item : prioritized )
119 {
120 if( item )
121 return true;
122 }
123
124 return false;
125 };
126
127 for( int slopRadius : { 0, maxSlopRadius } )
128 {
129 ITEM_SET candidates = m_router->QueryHoverItems( aWhere, slopRadius );
130
131 for( ITEM* item : candidates.Items() )
132 {
133 if( !item->IsRoutable() )
134 continue;
135
136 if( !IsCopperLayer( item->Layers().Start() ) )
137 continue;
138
139 if( !m_iface->IsAnyLayerVisible( item->Layers() ) )
140 continue;
141
142 if( alg::contains( aAvoidItems, item ) )
143 continue;
144
145 // fixme: this causes flicker with live loop removal...
146 //if( item->Parent() && !item->Parent()->ViewIsVisible() )
147 // continue;
148
149 if( item->OfKind( ITEM::SOLID_T ) && aIgnorePads )
150 {
151 continue;
152 }
153 else if( m_router->GetInterface()->GetNetCode( aNet) <= 0 || item->Net() == aNet )
154 {
155 if( item->OfKind( ITEM::VIA_T | ITEM::SOLID_T ) )
156 {
157 SEG::ecoord d = ( item->Shape()->Centre() - aWhere ).SquaredEuclideanNorm();
158
159 if( d < dist[2] )
160 {
161 prioritized[2] = item;
162 dist[2] = d;
163 }
164
165 if( item->Layers().Overlaps( tl ) && d < dist[0] )
166 {
167 prioritized[0] = item;
168 dist[0] = d;
169 }
170 }
171 else // ITEM::SEGMENT_T | ITEM::ARC_T
172 {
173 LINKED_ITEM* li = static_cast<LINKED_ITEM*>( item );
174 SEG::ecoord d = std::min( ( li->Anchor( 0 ) - aWhere ).SquaredEuclideanNorm(),
175 ( li->Anchor( 1 ) - aWhere ).SquaredEuclideanNorm() );
176
177 if( d < dist[3] )
178 {
179 prioritized[3] = item;
180 dist[3] = d;
181 }
182
183 if( item->Layers().Overlaps( tl ) && d < dist[1] )
184 {
185 prioritized[1] = item;
186 dist[1] = d;
187 }
188 }
189 }
190 else if( item->OfKind( ITEM::SOLID_T ) && item->IsFreePad() )
191 {
192 // Allow free pads only when already inside pad
193 if( item->Shape()->Collide( aWhere ) )
194 {
195 prioritized[0] = item;
196 dist[0] = 0;
197 }
198 }
199 else if ( item->Net() == 0 && m_router->Settings().Mode() == RM_MarkObstacles )
200 {
201 // Allow unconnected items as last resort in RM_MarkObstacles mode
202 if( item->Layers().Overlaps( tl ) )
203 prioritized[4] = item;
204 }
205 }
206
207 if( haveCandidates() )
208 break;
209 }
210
211 ITEM* rv = nullptr;
212
213 bool highContrast = ( frame()->GetDisplayOptions().m_ContrastModeDisplay != HIGH_CONTRAST_MODE::NORMAL );
214
215 for( ITEM* item : prioritized )
216 {
217 if( highContrast && item && !item->Layers().Overlaps( tl ) )
218 item = nullptr;
219
220 if( item && ( aLayer < 0 || item->Layers().Overlaps( aLayer ) ) )
221 {
222 rv = item;
223 break;
224 }
225 }
226
227 if( rv )
228 {
229 wxLogTrace( wxT( "PNS" ), wxT( "%s, layer : %d, tl: %d" ),
230 rv->KindStr().c_str(),
231 rv->Layers().Start(),
232 tl );
233 }
234
235 return rv;
236}
237
238
239void TOOL_BASE::highlightNets( bool aEnabled, std::set<NET_HANDLE> aNets )
240{
242 std::set<int> netcodes;
243
244 for( const NET_HANDLE& net : aNets )
245 netcodes.insert( m_router->GetInterface()->GetNetCode( net ) );
246
247 if( netcodes.size() > 0 && aEnabled )
248 {
249 // If the user has previously set some of the routed nets to be highlighted,
250 // we assume they want to keep them highlighted after routing
251
252 const std::set<int>& currentNetCodes = rs->GetHighlightNetCodes();
253 bool keep = false;
254
255 for( const int& netcode : netcodes )
256 {
257 if( currentNetCodes.find( netcode ) != currentNetCodes.end() )
258 {
259 keep = true;
260 break;
261 }
262 }
263
264 if( rs->IsHighlightEnabled() && keep )
265 m_startHighlightNetcodes = currentNetCodes;
266 else
268
269 rs->SetHighlight( netcodes, true );
270 }
271 else
272 {
274 }
275
276 // Do not remove this call. This is required to update the layers when we highlight a net.
277 // In this case, highlighting a net dims all other elements, so the colors need to update
279}
280
281
283{
284 // Sync PNS engine settings with the general PCB editor options.
286
287 // If we're dragging a track segment, don't try to snap to items that are part of the original line.
289 && m_router->GetDragger() )
290 {
291 DRAGGER* dragger = dynamic_cast<DRAGGER*>( m_router->GetDragger() );
292 LINKED_ITEM* linkedItem = dynamic_cast<LINKED_ITEM*>( aItem );
293
294 if( dragger && linkedItem && dragger->GetOriginalLine().ContainsLink( linkedItem ) )
295 return false;
296 }
297
299
300 pnss.SetSnapToPads( magSettings->pads == MAGNETIC_OPTIONS::CAPTURE_CURSOR_IN_TRACK_TOOL
301 || magSettings->pads == MAGNETIC_OPTIONS::CAPTURE_ALWAYS );
302
303 pnss.SetSnapToTracks( magSettings->tracks == MAGNETIC_OPTIONS::CAPTURE_CURSOR_IN_TRACK_TOOL
304 || magSettings->tracks == MAGNETIC_OPTIONS::CAPTURE_ALWAYS );
305
306 if( aItem )
307 {
309 return pnss.GetSnapToTracks();
310 else if( aItem->OfKind( ITEM::SOLID_T ) )
311 return pnss.GetSnapToPads();
312 }
313
314 return false;
315}
316
317
318void TOOL_BASE::updateStartItem( const TOOL_EVENT& aEvent, bool aIgnorePads )
319{
320 int tl = getView()->GetTopLayer();
321 GAL* gal = m_toolMgr->GetView()->GetGAL();
322 VECTOR2I pos = aEvent.HasPosition() ? (VECTOR2I) aEvent.Position() : m_startSnapPoint;
323
324 controls()->ForceCursorPosition( false );
326 m_gridHelper->SetSnap( !aEvent.Modifier( MD_SHIFT ) );
327
328 m_startItem = pickSingleItem( pos, nullptr, -1, aIgnorePads );
329
331 m_startItem = nullptr;
332
335}
336
337
339{
340 int layer;
341 GAL* gal = m_toolMgr->GetView()->GetGAL();
342
344 m_gridHelper->SetSnap( !aEvent.Modifier( MD_SHIFT ) );
345
346 controls()->ForceCursorPosition( false );
347
348 VECTOR2I mousePos = controls()->GetMousePosition();
349
350 if( m_router->GetState() == ROUTER::ROUTE_TRACK && aEvent.IsDrag() )
351 {
352 // If the user is moving the mouse quickly while routing then clicks will come in as
353 // short drags. In this case we want to use the drag origin rather than the current
354 // mouse position.
355 mousePos = aEvent.DragOrigin();
356 }
357
359 ( m_router->GetCurrentNets().empty() || m_router->GetCurrentNets().front() == nullptr ) )
360 {
361 m_endSnapPoint = snapToItem( nullptr, mousePos );
363 m_endItem = nullptr;
364
365 return;
366 }
367
368 if( m_router->IsPlacingVia() )
369 layer = -1;
370 else
371 layer = m_router->GetCurrentLayer();
372
373 ITEM* endItem = nullptr;
374
375 std::vector<NET_HANDLE> nets = m_router->GetCurrentNets();
376
377 for( NET_HANDLE net : nets )
378 {
379 endItem = pickSingleItem( mousePos, net, layer, false, { m_startItem } );
380
381 if( endItem )
382 break;
383 }
384
385 if( m_gridHelper->GetSnap() && checkSnap( endItem ) )
386 {
387 m_endItem = endItem;
388 m_endSnapPoint = snapToItem( endItem, mousePos );
389 }
390 else
391 {
392 m_endItem = nullptr;
394 : GRID_WIRES );
395 }
396
398
399 if( m_endItem )
400 {
401 wxLogTrace( wxT( "PNS" ), wxT( "%s, layer : %d" ),
402 m_endItem->KindStr().c_str(),
403 m_endItem->Layers().Start() );
404 }
405}
406
407
409{
410 return m_router;
411}
412
413
415{
416 return m_iface;
417}
418
419
420const VECTOR2I TOOL_BASE::snapToItem( ITEM* aItem, const VECTOR2I& aP )
421{
422 if( !aItem || !m_iface->IsItemVisible( aItem ) )
423 {
425 }
426
427 switch( aItem->Kind() )
428 {
429 case ITEM::SOLID_T:
430 {
431 SOLID* solid = static_cast<SOLID*>( aItem );
432
433 if( solid->AnchorPoints().empty() )
434 return solid->Anchor( 0 );
435
437 SEG::ecoord minDist = std::numeric_limits<SEG::ecoord>::max();
438
439 for( VECTOR2I anchorCandidate : solid->AnchorPoints() )
440 {
441 SEG::ecoord distSq = ( aP - anchorCandidate ).SquaredEuclideanNorm();
442
443 if( distSq < minDist )
444 {
445 minDist = distSq;
446 anchor = anchorCandidate;
447 }
448 }
449
450 return anchor;
451 }
452
453 case ITEM::VIA_T:
454 return static_cast<VIA*>( aItem )->Pos();
455
456 case ITEM::SEGMENT_T:
457 case ITEM::ARC_T:
458 {
459 LINKED_ITEM* li = static_cast<LINKED_ITEM*>( aItem );
460 VECTOR2I A = li->Anchor( 0 );
461 VECTOR2I B = li->Anchor( 1 );
462 SEG::ecoord w_sq = SEG::Square( li->Width() / 2 );
463 SEG::ecoord distA_sq = ( aP - A ).SquaredEuclideanNorm();
464 SEG::ecoord distB_sq = ( aP - B ).SquaredEuclideanNorm();
465
466 if( distA_sq < w_sq || distB_sq < w_sq )
467 {
468 return ( distA_sq < distB_sq ) ? A : B;
469 }
470 else if( aItem->Kind() == ITEM::SEGMENT_T )
471 {
472 // TODO(snh): Clean this up
473 SEGMENT* seg = static_cast<SEGMENT*>( li );
474 return m_gridHelper->AlignToSegment( aP, seg->Seg() );
475 }
476 else if( aItem->Kind() == ITEM::ARC_T )
477 {
478 ARC* arc = static_cast<ARC*>( li );
479 return m_gridHelper->AlignToArc( aP, *static_cast<const SHAPE_ARC*>( arc->Shape() ) );
480 }
481
482 break;
483 }
484
485 default:
486 break;
487 }
488
490}
491
492}
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:763
virtual int GetTopLayer() const
Definition: view.cpp:818
GAL * GetGAL() const
Return the #GAL this view is using to draw graphical primitives.
Definition: view.h:197
PAINTER * GetPainter() const
Return the painter object used by the view for drawing #VIEW_ITEMS.
Definition: view.h:215
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:166
@ SEGMENT_T
Definition: pns_item.h:105
const LAYER_RANGE & Layers() const
Definition: pns_item.h:195
bool OfKind(int aKindMask) const
Definition: pns_item.h:174
virtual VECTOR2I Anchor(int n) const
Definition: pns_item.h:236
std::string KindStr() const
Definition: pns_item.cpp:278
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)
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:216
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
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:378
static constexpr extended_type ECOORD_MAX
Definition: vector2d.h:75
@ 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:879
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
MAGNETIC_OPTIONS tracks
MAGNETIC_OPTIONS pads
@ MD_SHIFT
Definition: tool_event.h:142
VECTOR2< int > VECTOR2I
Definition: vector2d.h:588