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-2021 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
26#include <pcb_painter.h>
27#include <pcbnew_settings.h>
28
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#include "pns_dragger.h"
37
38
39using namespace KIGFX;
40
41namespace PNS {
42
43
44TOOL_BASE::TOOL_BASE( const std::string& aToolName ) :
45 PCB_TOOL_BASE( aToolName )
46{
47 m_gridHelper = nullptr;
48 m_iface = nullptr;
49 m_router = nullptr;
50 m_cancelled = false;
51
52 m_startItem = nullptr;
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
78 m_iface->SetHostTool( this );
79
80 m_router = new ROUTER;
84
86
88
89 if( !settings->m_PnsSettings )
90 settings->m_PnsSettings = std::make_unique<ROUTING_SETTINGS>( settings, "tools.pns" );
91
92 m_router->LoadSettings( settings->m_PnsSettings.get() );
93
94 m_gridHelper = new PCB_GRID_HELPER( m_toolMgr, frame()->GetMagneticItemsSettings() );
95}
96
97
98ITEM* TOOL_BASE::pickSingleItem( const VECTOR2I& aWhere, int aNet, int aLayer, bool aIgnorePads,
99 const std::vector<ITEM*> aAvoidItems )
100{
101 int tl = aLayer > 0 ? aLayer : getView()->GetTopLayer();
102
103 static const int candidateCount = 5;
104 ITEM* prioritized[candidateCount];
105 SEG::ecoord dist[candidateCount];
106
107 for( int i = 0; i < candidateCount; i++ )
108 {
109 prioritized[i] = nullptr;
110 dist[i] = VECTOR2I::ECOORD_MAX;
111 }
112
113 auto haveCandidates =
114 [&]()
115 {
116 for( ITEM* item : prioritized )
117 {
118 if( item )
119 return true;
120 }
121
122 return false;
123 };
124
125 for( bool useClearance : { false, true } )
126 {
127 ITEM_SET candidates = m_router->QueryHoverItems( aWhere, useClearance );
128
129 for( ITEM* item : candidates.Items() )
130 {
131 if( !item->IsRoutable() )
132 continue;
133
134 if( !IsCopperLayer( item->Layers().Start() ) )
135 continue;
136
137 if( !m_iface->IsAnyLayerVisible( item->Layers() ) )
138 continue;
139
140 if( alg::contains( aAvoidItems, item ) )
141 continue;
142
143 // fixme: this causes flicker with live loop removal...
144 //if( item->Parent() && !item->Parent()->ViewIsVisible() )
145 // continue;
146
147 if( item->OfKind( ITEM::SOLID_T ) && aIgnorePads )
148 {
149 continue;
150 }
151 else if( aNet <= 0 || item->Net() == aNet )
152 {
153 if( item->OfKind( ITEM::VIA_T | ITEM::SOLID_T ) )
154 {
155 SEG::ecoord d = ( item->Shape()->Centre() - aWhere ).SquaredEuclideanNorm();
156
157 if( d < dist[2] )
158 {
159 prioritized[2] = item;
160 dist[2] = d;
161 }
162
163 if( item->Layers().Overlaps( tl ) && d < dist[0] )
164 {
165 prioritized[0] = item;
166 dist[0] = d;
167 }
168 }
169 else // ITEM::SEGMENT_T | ITEM::ARC_T
170 {
171 LINKED_ITEM* li = static_cast<LINKED_ITEM*>( item );
172 SEG::ecoord d = std::min( ( li->Anchor( 0 ) - aWhere ).SquaredEuclideanNorm(),
173 ( li->Anchor( 1 ) - aWhere ).SquaredEuclideanNorm() );
174
175 if( d < dist[3] )
176 {
177 prioritized[3] = item;
178 dist[3] = d;
179 }
180
181 if( item->Layers().Overlaps( tl ) && d < dist[1] )
182 {
183 prioritized[1] = item;
184 dist[1] = d;
185 }
186 }
187 }
188 else if( item->OfKind( ITEM::SOLID_T ) && item->IsFreePad() )
189 {
190 // Allow free pads only when already inside pad
191 if( item->Shape()->Collide( aWhere ) )
192 {
193 prioritized[0] = item;
194 dist[0] = 0;
195 }
196 }
197 else if ( item->Net() == 0 && m_router->Settings().Mode() == RM_MarkObstacles )
198 {
199 // Allow unconnected items as last resort in RM_MarkObstacles mode
200 if( item->Layers().Overlaps( tl ) )
201 prioritized[4] = item;
202 }
203 }
204
205 if( haveCandidates() )
206 break;
207 }
208
209 ITEM* rv = nullptr;
210
211 bool highContrast = ( frame()->GetDisplayOptions().m_ContrastModeDisplay != HIGH_CONTRAST_MODE::NORMAL );
212
213 for( ITEM* item : prioritized )
214 {
215 if( highContrast && item && !item->Layers().Overlaps( tl ) )
216 item = nullptr;
217
218 if( item && ( aLayer < 0 || item->Layers().Overlaps( aLayer ) ) )
219 {
220 rv = item;
221 break;
222 }
223 }
224
225 if( rv )
226 {
227 wxLogTrace( wxT( "PNS" ), wxT( "%s, layer : %d, tl: %d" ),
228 rv->KindStr().c_str(),
229 rv->Layers().Start(),
230 tl );
231 }
232
233 return rv;
234}
235
236
237void TOOL_BASE::highlightNets( bool aEnabled, std::set<int> aNetcodes )
238{
240
241 if( aNetcodes.size() > 0 && aEnabled )
242 {
243 // If the user has previously set some of the routed nets to be highlighted,
244 // we assume they want to keep them highlighted after routing
245
246 const std::set<int>& currentNetCodes = rs->GetHighlightNetCodes();
247 bool keep = false;
248
249 for( const int& netcode : aNetcodes )
250 {
251 if( currentNetCodes.find( netcode ) != currentNetCodes.end() )
252 {
253 keep = true;
254 break;
255 }
256 }
257
258 if( rs->IsHighlightEnabled() && keep )
259 m_startHighlightNetcodes = currentNetCodes;
260 else
262
263 rs->SetHighlight( aNetcodes, true );
264 }
265 else
266 {
268 }
269
270 // Do not remove this call. This is required to update the layers when we highlight a net.
271 // In this case, highlighting a net dims all other elements, so the colors need to update
273}
274
275
277{
278 // Sync PNS engine settings with the general PCB editor options.
279 auto& pnss = m_router->Settings();
280
281 // If we're dragging a track segment, don't try to snap to items that are part of the original line.
283 && m_router->GetDragger() )
284 {
285 DRAGGER* dragger = dynamic_cast<DRAGGER*>( m_router->GetDragger() );
286 LINKED_ITEM* liItem = dynamic_cast<LINKED_ITEM*>( aItem );
287
288 if( dragger && liItem && dragger->GetOriginalLine().ContainsLink( liItem ) )
289 {
290 return false;
291 }
292 }
293
294 pnss.SetSnapToPads(
295 frame()->GetMagneticItemsSettings()->pads == MAGNETIC_OPTIONS::CAPTURE_CURSOR_IN_TRACK_TOOL ||
296 frame()->GetMagneticItemsSettings()->pads == MAGNETIC_OPTIONS::CAPTURE_ALWAYS );
297
298 pnss.SetSnapToTracks(
299 frame()->GetMagneticItemsSettings()->tracks == MAGNETIC_OPTIONS::CAPTURE_CURSOR_IN_TRACK_TOOL
300 || frame()->GetMagneticItemsSettings()->tracks == MAGNETIC_OPTIONS::CAPTURE_ALWAYS );
301
302 if( aItem )
303 {
305 return pnss.GetSnapToTracks();
306 else if( aItem->OfKind( ITEM::SOLID_T ) )
307 return pnss.GetSnapToPads();
308 }
309
310 return false;
311}
312
313
314void TOOL_BASE::updateStartItem( const TOOL_EVENT& aEvent, bool aIgnorePads )
315{
316 int tl = getView()->GetTopLayer();
317 VECTOR2I cp = aEvent.IsPrime() ? aEvent.Position()
318 : controls()->GetCursorPosition( !aEvent.Modifier( MD_SHIFT ) );
319 VECTOR2I p;
320 GAL* gal = m_toolMgr->GetView()->GetGAL();
321
322 controls()->ForceCursorPosition( false );
324 m_gridHelper->SetSnap( !aEvent.Modifier( MD_SHIFT ) );
325
326 if( aEvent.IsMotion() || aEvent.IsClick() )
327 p = aEvent.Position();
328 else
329 p = cp;
330
331 m_startItem = pickSingleItem( aEvent.IsClick() ? cp : p, -1, -1, aIgnorePads );
332
334 m_startItem = nullptr;
335
338}
339
340
342{
343 int layer;
344 GAL* gal = m_toolMgr->GetView()->GetGAL();
345
347 m_gridHelper->SetSnap( !aEvent.Modifier( MD_SHIFT ) );
348
349 controls()->ForceCursorPosition( false );
350
351 VECTOR2I mousePos = controls()->GetMousePosition();
352
353 if( m_router->GetState() == ROUTER::ROUTE_TRACK && aEvent.IsDrag() )
354 {
355 // If the user is moving the mouse quickly while routing then clicks will come in as
356 // short drags. In this case we want to use the drag origin rather than the current
357 // mouse position.
358 mousePos = aEvent.DragOrigin();
359 }
360
362 ( m_router->GetCurrentNets().empty() || m_router->GetCurrentNets().front() < 0 ) )
363 {
364 m_endSnapPoint = snapToItem( nullptr, mousePos );
366 m_endItem = nullptr;
367
368 return;
369 }
370
371 if( m_router->IsPlacingVia() )
372 layer = -1;
373 else
374 layer = m_router->GetCurrentLayer();
375
376 ITEM* endItem = nullptr;
377
378 std::vector<int> nets = m_router->GetCurrentNets();
379
380 for( int net : nets )
381 {
382 endItem = pickSingleItem( mousePos, net, layer, false, { m_startItem } );
383
384 if( endItem )
385 break;
386 }
387
388 if( m_gridHelper->GetSnap() && checkSnap( endItem ) )
389 {
390 m_endItem = endItem;
391 m_endSnapPoint = snapToItem( endItem, mousePos );
392 }
393 else
394 {
395 m_endItem = nullptr;
396 m_endSnapPoint = m_gridHelper->Align( mousePos );
397 }
398
400
401 if( m_endItem )
402 {
403 wxLogTrace( wxT( "PNS" ), wxT( "%s, layer : %d" ),
404 m_endItem->KindStr().c_str(),
405 m_endItem->Layers().Start() );
406 }
407}
408
409
411{
412 return m_router;
413}
414
415
416const VECTOR2I TOOL_BASE::snapToItem( ITEM* aItem, const VECTOR2I& aP )
417{
418 if( !aItem || !m_iface->IsItemVisible( aItem ) )
419 {
420 return m_gridHelper->Align( aP );
421 }
422
423 switch( aItem->Kind() )
424 {
425 case ITEM::SOLID_T:
426 return static_cast<SOLID*>( aItem )->Pos();
427
428 case ITEM::VIA_T:
429 return static_cast<VIA*>( aItem )->Pos();
430
431 case ITEM::SEGMENT_T:
432 case ITEM::ARC_T:
433 {
434 LINKED_ITEM* li = static_cast<LINKED_ITEM*>( aItem );
435 VECTOR2I A = li->Anchor( 0 );
436 VECTOR2I B = li->Anchor( 1 );
437 SEG::ecoord w_sq = SEG::Square( li->Width() / 2 );
438 SEG::ecoord distA_sq = ( aP - A ).SquaredEuclideanNorm();
439 SEG::ecoord distB_sq = ( aP - B ).SquaredEuclideanNorm();
440
441 if( distA_sq < w_sq || distB_sq < w_sq )
442 {
443 return ( distA_sq < distB_sq ) ? A : B;
444 }
445 else if( aItem->Kind() == ITEM::SEGMENT_T )
446 {
447 // TODO(snh): Clean this up
448 SEGMENT* seg = static_cast<SEGMENT*>( li );
449 return m_gridHelper->AlignToSegment( aP, seg->Seg() );
450 }
451 else if( aItem->Kind() == ITEM::ARC_T )
452 {
453 ARC* arc = static_cast<ARC*>( li );
454 return m_gridHelper->AlignToArc( aP, *static_cast<const SHAPE_ARC*>( arc->Shape() ) );
455 }
456
457 break;
458 }
459
460 default:
461 break;
462 }
463
464 return m_gridHelper->Align( aP );
465}
466
467}
bool GetSnap() const
Definition: grid_helper.h:66
void SetSnap(bool aSnap)
Definition: grid_helper.h:65
bool GetUseGrid() const
Definition: grid_helper.h:69
virtual VECTOR2I Align(const VECTOR2I &aPoint) const
void SetUseGrid(bool aSnapToGrid)
Definition: grid_helper.h:68
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.
VECTOR2D GetCursorPosition() const
Return the current cursor position in world coordinates.
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:758
virtual int GetTopLayer() const
Definition: view.cpp:813
GAL * GetGAL() const
Return the #GAL this view is using to draw graphical primitives.
Definition: view.h:195
PAINTER * GetPainter() const
Return the painter object used by the view for drawing #VIEW_ITEMS.
Definition: view.h:213
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)
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:77
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:91
PnsKind Kind() const
Return the type (kind) of the item.
Definition: pns_item.h:162
@ SOLID_T
Definition: pns_item.h:98
@ SEGMENT_T
Definition: pns_item.h:101
const LAYER_RANGE & Layers() const
Definition: pns_item.h:191
bool OfKind(int aKindMask) const
Definition: pns_item.h:170
virtual VECTOR2I Anchor(int n) const
Definition: pns_item.h:232
std::string KindStr() const
Definition: pns_item.cpp:246
virtual int Width() const
void ClearWorld()
Definition: pns_router.cpp:102
void UpdateSizes(const SIZES_SETTINGS &aSizes)
Applies stored settings.
Definition: pns_router.cpp:734
void LoadSettings(ROUTING_SETTINGS *aSettings)
Changes routing settings to ones passed in the parameter.
Definition: pns_router.h:203
const std::vector< int > GetCurrentNets() const
Definition: pns_router.cpp:980
void SetInterface(ROUTER_IFACE *aIface)
void SyncWorld()
Definition: pns_router.cpp:92
bool IsPlacingVia() const
const ITEM_SET QueryHoverItems(const VECTOR2I &aP, bool aUseClearance=false)
Definition: pns_router.cpp:121
ROUTING_SETTINGS & Settings()
Definition: pns_router.h:189
DRAG_ALGO * GetDragger()
Definition: pns_router.h:137
RouterState GetState() const
Definition: pns_router.h:135
int GetCurrentLayer() const
Definition: pns_router.cpp:991
PNS_MODE Mode() const
Set the routing mode.
const SEG & Seg() const
Definition: pns_segment.h:84
bool checkSnap(ITEM *aItem)
virtual void updateStartItem(const TOOL_EVENT &aEvent, bool aIgnorePads=false)
virtual void highlightNets(bool aEnabled, std::set< int > aNetcodes={})
virtual ITEM * pickSingleItem(const VECTOR2I &aWhere, int aNet=-1, int aLayer=-1, bool aIgnorePads=false, const std::vector< ITEM * > aAvoidItems={})
ROUTER * Router() const
std::set< int > m_startHighlightNetcodes
Definition: pns_tool_base.h:72
const VECTOR2I snapToItem(ITEM *aSnapToItem, const VECTOR2I &aP)
virtual void Reset(RESET_REASON aReason) override
Bring the tool to a known, initial state.
virtual ~TOOL_BASE()
SIZES_SETTINGS m_savedSizes
Definition: pns_tool_base.h:69
PNS_KICAD_IFACE * m_iface
Definition: pns_tool_base.h:78
ITEM * m_startItem
Definition: pns_tool_base.h:70
virtual void updateEndItem(const TOOL_EVENT &aEvent)
TOOL_BASE(const std::string &aToolName)
ROUTER * m_router
Definition: pns_tool_base.h:79
VECTOR2I m_endSnapPoint
Definition: pns_tool_base.h:75
PCB_GRID_HELPER * m_gridHelper
Definition: pns_tool_base.h:77
VECTOR2I m_startSnapPoint
Definition: pns_tool_base.h:71
void SetBoard(BOARD *aBoard)
void SetView(KIGFX::VIEW *aView)
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:156
bool DisableGridSnapping() const
Definition: tool_event.h:344
bool IsPrime() const
Returns information about key modifiers state (Ctrl, Alt, etc.)
Definition: tool_event.h:333
const VECTOR2D Position() const
Returns the point where dragging has started.
Definition: tool_event.h:266
bool IsClick(int aButtonMask=BUT_ANY) const
Definition: tool_event.cpp:189
bool IsDrag(int aButtonMask=BUT_ANY) const
Definition: tool_event.h:288
int Modifier(int aMask=MD_MODIFIER_MASK) const
Definition: tool_event.h:339
const VECTOR2D DragOrigin() const
Returns information about mouse buttons state.
Definition: tool_event.h:272
bool IsMotion() const
Definition: tool_event.h:303
KIGFX::VIEW * GetView() const
Definition: tool_manager.h:285
static constexpr extended_type ECOORD_MAX
Definition: vector2d.h:75
bool IsCopperLayer(int aLayerId)
Tests whether a layer is a copper layer.
Definition: layer_ids.h:831
The Cairo implementation of the graphics abstraction layer.
Definition: color4d.cpp:246
Push and Shove diff pair dimensions (gap) settings dialog.
@ RM_MarkObstacles
Ignore collisions, mark obstacles.
bool contains(const _Container &__container, _Value __value)
Returns true if the container contains the given value.
Definition: kicad_algo.h:99
MAGNETIC_OPTIONS tracks
MAGNETIC_OPTIONS pads
@ MD_SHIFT
Definition: tool_event.h:138