KiCad PCB EDA Suite
Loading...
Searching...
No Matches
pns_router.cpp
Go to the documentation of this file.
1/*
2 * KiRouter - a push-and-(sometimes-)shove PCB router
3 *
4 * Copyright (C) 2013-2014 CERN
5 * Copyright The 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#include <cstdio>
23#include <memory>
24#include <vector>
25
27
28#include <advanced_config.h>
30
31#include <pcb_painter.h>
32#include <pad.h>
33#include <zone.h>
34
35#include <geometry/shape.h>
36
37#include "pns_node.h"
38#include "pns_line_placer.h"
39#include "pns_line.h"
40#include "pns_solid.h"
41#include "pns_utils.h"
42#include "pns_router.h"
43#include "pns_shove.h"
44#include "pns_dragger.h"
45#include "pns_multi_dragger.h"
47#include "pns_topology.h"
49#include "pns_meander_placer.h"
52#include "router_preview_item.h"
53
54namespace PNS {
55
56// an ugly singleton for drawing debug items within the router context.
57// To be fixed sometime in the future.
59
61{
62 theRouter = this;
63
64 m_state = IDLE;
66
67 m_logger = nullptr;
68
69 if( ADVANCED_CFG::GetCfg().m_EnableRouterDump )
70 m_logger = new LOGGER;
71
72 // Initialize all other variables:
73 m_lastNode = nullptr;
74 m_iterLimit = 0;
75 m_settings = nullptr;
76 m_iface = nullptr;
77 m_visibleViewArea.SetMaximum();
78}
79
80
82{
83 return theRouter;
84}
85
86
88{
89 ClearWorld();
90 theRouter = nullptr;
91 delete m_logger;
92}
93
94
96{
97 ClearWorld();
98
99 m_world = std::make_unique<NODE>( );
100 m_iface->SyncWorld( m_world.get() );
101 m_world->FixupVirtualVias();
102}
103
104
106{
107 if( m_world )
108 {
109 m_world->SetRuleResolver( nullptr );
110 m_world->KillChildren();
111 m_world.reset();
112 }
113
114 m_placer.reset();
115}
116
117
119{
120 return m_state != IDLE;
121}
122
123
124const ITEM_SET ROUTER::QueryHoverItems( const VECTOR2I& aP, int aSlopRadius )
125{
126 NODE* node = m_placer ? m_placer->CurrentNode() : m_world.get();
127 PNS::ITEM_SET ret;
128
129 wxCHECK( node, ret );
130
131 if( aSlopRadius > 0 )
132 {
133 NODE::OBSTACLES obs;
134 SEGMENT test( SEG( aP, aP ), nullptr );
136
137 test.SetWidth( 1 );
138 test.SetLayers( PNS_LAYER_RANGE::All() );
139
140 opts.m_differentNetsOnly = false;
141 opts.m_overrideClearance = aSlopRadius;
142
143 node->QueryColliding( &test, obs, opts );
144
145 for( const OBSTACLE& obstacle : obs )
146 ret.Add( obstacle.m_item, false );
147
148 return ret;
149 }
150 else
151 {
152 return node->HitTest( aP );
153 }
154}
155
156
157bool ROUTER::StartDragging( const VECTOR2I& aP, ITEM* aItem, int aDragMode )
158{
159 m_leaderSegments.clear();
160 return StartDragging( aP, ITEM_SET( aItem ), aDragMode );
161}
162
163
164bool ROUTER::StartDragging( const VECTOR2I& aP, ITEM_SET aStartItems, int aDragMode )
165{
166 m_leaderSegments.clear();
167
168 if( aStartItems.Empty() )
169 return false;
170
172
173 if( aStartItems.Count( ITEM::SOLID_T ) == aStartItems.Size() )
174 {
175 m_dragger = std::make_unique<COMPONENT_DRAGGER>( this );
177 }
178 // more than 1 track segment or arc to drag? launch the multisegment dragger
179 else if( aStartItems.Count( ITEM::SEGMENT_T | ITEM::ARC_T ) > 1 )
180 {
181 m_dragger = std::make_unique<MULTI_DRAGGER>( this );
183 }
184 else
185 {
186 m_dragger = std::make_unique<DRAGGER>( this );
188 }
189
190 m_dragger->SetMode( static_cast<PNS::DRAG_MODE>( aDragMode ) );
191 m_dragger->SetWorld( m_world.get() );
192 m_dragger->SetLogger( m_logger );
193 m_dragger->SetDebugDecorator( m_iface->GetDebugDecorator() );
194
195 if( m_logger )
196 m_logger->Clear();
197
198 if( m_logger )
199 {
200 if( aStartItems.Size() == 1 )
201 m_logger->Log( LOGGER::EVT_START_DRAG, aP, aStartItems[0] );
202 else if( aStartItems.Size() > 1 )
203 m_logger->LogM( LOGGER::EVT_START_MULTIDRAG, aP, aStartItems.Items() ); // fixme default args
204 }
205
206 if( m_dragger->Start( aP, aStartItems ) )
207 {
208 return true;
209 }
210 else
211 {
212 m_dragger.reset();
213 m_state = IDLE;
214 return false;
215 }
216}
217
218
219bool ROUTER::isStartingPointRoutable( const VECTOR2I& aWhere, ITEM* aStartItem, int aLayer )
220{
221 if( Settings().AllowDRCViolations() )
222 return true;
223
225 {
226 if( m_sizes.DiffPairGap() < m_sizes.MinClearance() )
227 {
228 SetFailureReason( _( "Diff pair gap is less than board minimum clearance." ) );
229 return false;
230 }
231 }
232
233 ITEM_SET candidates = QueryHoverItems( aWhere );
234 wxString failureReason;
235
236 for( ITEM* item : candidates.Items() )
237 {
238 // Edge cuts are put on all layers, but they're not *really* on all layers
239 if( item->BoardItem() && item->BoardItem()->GetLayer() == Edge_Cuts )
240 continue;
241
242 if( !item->Layers().Overlaps( aLayer ) )
243 continue;
244
245 if( item->IsRoutable() )
246 {
247 failureReason = wxEmptyString;
248 break;
249 }
250 else
251 {
252 BOARD_ITEM* parent = item->BoardItem();
253
254 switch( parent->Type() )
255 {
256 case PCB_PAD_T:
257 {
258 PAD* pad = static_cast<PAD*>( parent );
259
260 if( pad->GetAttribute() == PAD_ATTRIB::NPTH )
261 failureReason = _( "Cannot start routing from a non-plated hole." );
262 }
263 break;
264
265 case PCB_ZONE_T:
266 {
267 ZONE* zone = static_cast<ZONE*>( parent );
268
269 if( !zone->HasKeepoutParametersSet() )
270 break;
271
272 if( !zone->GetZoneName().IsEmpty() )
273 {
274 failureReason = wxString::Format( _( "Rule area '%s' disallows tracks." ),
275 zone->GetZoneName() );
276 }
277 else
278 {
279 failureReason = _( "Rule area disallows tracks." );
280 }
281 }
282 break;
283
284 case PCB_FIELD_T:
285 case PCB_TEXT_T:
286 case PCB_TEXTBOX_T:
287 failureReason = _( "Cannot start routing from a text item." );
288 break;
289
290 default:
291 break;
292 }
293 }
294 }
295
296 if( !failureReason.IsEmpty() )
297 {
298 SetFailureReason( failureReason );
299 return false;
300 }
301
302 VECTOR2I startPoint = aWhere;
303
305 {
306 SHAPE_LINE_CHAIN dummyStartSeg;
307 LINE dummyStartLine;
308
309 dummyStartSeg.Append( startPoint );
310 dummyStartSeg.Append( startPoint, true );
311
312 dummyStartLine.SetShape( dummyStartSeg );
313 dummyStartLine.SetLayer( aLayer );
314 dummyStartLine.SetNet( aStartItem ? aStartItem->Net() : 0 );
315 dummyStartLine.SetWidth( m_sizes.TrackWidth() );
316
317 if( m_world->CheckColliding( &dummyStartLine, ITEM::ANY_T ) )
318 {
319 // If the only reason we collide is track width; it's better to allow the user to start
320 // anyway and just highlight the resulting collisions, so they can change width later.
321 dummyStartLine.SetWidth( m_sizes.BoardMinTrackWidth() );
322
323 if( m_world->CheckColliding( &dummyStartLine, ITEM::ANY_T ) )
324 {
325 ITEM_SET dummyStartSet( &dummyStartLine );
326 NODE::ITEM_VECTOR highlightedItems;
327
328 markViolations( m_world.get(), dummyStartSet, highlightedItems );
329
330 for( ITEM* item : highlightedItems )
331 m_iface->HideItem( item );
332
333 SetFailureReason( _( "The routing start point violates DRC." ) );
334 return false;
335 }
336 }
337 }
338 else if( m_mode == PNS_MODE_ROUTE_DIFF_PAIR )
339 {
340 if( !aStartItem )
341 {
342 SetFailureReason( _( "Cannot start a differential pair in the middle of nowhere." ) );
343 return false;
344 }
345
346 DP_PRIMITIVE_PAIR dpPair;
347 wxString errorMsg;
348
349 if( !DIFF_PAIR_PLACER::FindDpPrimitivePair( m_world.get(), startPoint, aStartItem, dpPair,
350 &errorMsg ) )
351 {
352 SetFailureReason( errorMsg );
353 return false;
354 }
355
356 // Check if the gap at the start point is compatible with the configured diff pair settings.
357 // This only applies when starting from track segments, where the gap between existing
358 // tracks should match the configured diff pair gap. When starting from pads or vias,
359 // the anchor-to-anchor distance is determined by pad/via placement, not routing rules.
360 if( aStartItem->OfKind( ITEM::SEGMENT_T | ITEM::ARC_T ) )
361 {
362 int actualGap = ( dpPair.AnchorP() - dpPair.AnchorN() ).EuclideanNorm();
363 int configuredGap = m_sizes.DiffPairGap() + m_sizes.DiffPairWidth();
364
365 // Allow some tolerance (10%) for minor differences, but warn about significant mismatches
366 int tolerance = configuredGap / 10;
367
368 if( std::abs( actualGap - configuredGap ) > tolerance )
369 {
371 _( "The differential pair gap at the start point does not match "
372 "the configured gap. This can occur in neckdown areas where tracks "
373 "have narrower width and spacing. Adjust the differential pair "
374 "settings or start from a location with the correct gap." ) );
375 return false;
376 }
377 }
378
379 SHAPE_LINE_CHAIN dummyStartSegA;
380 SHAPE_LINE_CHAIN dummyStartSegB;
381 LINE dummyStartLineA;
382 LINE dummyStartLineB;
383
384 dummyStartSegA.Append( dpPair.AnchorN() );
385 dummyStartSegA.Append( dpPair.AnchorN(), true );
386
387 dummyStartSegB.Append( dpPair.AnchorP() );
388 dummyStartSegB.Append( dpPair.AnchorP(), true );
389
390 dummyStartLineA.SetShape( dummyStartSegA );
391 dummyStartLineA.SetLayer( aLayer );
392 dummyStartLineA.SetNet( dpPair.PrimN()->Net() );
393 dummyStartLineA.SetWidth( m_sizes.DiffPairWidth() );
394
395 dummyStartLineB.SetShape( dummyStartSegB );
396 dummyStartLineB.SetLayer( aLayer );
397 dummyStartLineB.SetNet( dpPair.PrimP()->Net() );
398 dummyStartLineB.SetWidth( m_sizes.DiffPairWidth() );
399
400 if( m_world->CheckColliding( &dummyStartLineA, ITEM::ANY_T )
401 || m_world->CheckColliding( &dummyStartLineB, ITEM::ANY_T ) )
402 {
403 // If the only reason we collide is track width; it's better to allow the user to start
404 // anyway and just highlight the resulting collisions, so they can change width later.
405 dummyStartLineA.SetWidth( m_sizes.BoardMinTrackWidth() );
406 dummyStartLineB.SetWidth( m_sizes.BoardMinTrackWidth() );
407
408 if( m_world->CheckColliding( &dummyStartLineA, ITEM::ANY_T )
409 || m_world->CheckColliding( &dummyStartLineB, ITEM::ANY_T ) )
410 {
411 ITEM_SET dummyStartSet;
412 NODE::ITEM_VECTOR highlightedItems;
413
414 dummyStartSet.Add( dummyStartLineA );
415 dummyStartSet.Add( dummyStartLineB );
416 markViolations( m_world.get(), dummyStartSet, highlightedItems );
417
418 for( ITEM* item : highlightedItems )
419 m_iface->HideItem( item );
420
421 SetFailureReason( _( "The routing start point violates DRC." ) );
422 return false;
423 }
424 }
425 }
426
427 return true;
428}
429
430
431bool ROUTER::StartRouting( const VECTOR2I& aP, ITEM* aStartItem, int aLayer )
432{
434
435 if( !isStartingPointRoutable( aP, aStartItem, aLayer ) )
436 return false;
437
438 switch( m_mode )
439 {
441 m_placer = std::make_unique<LINE_PLACER>( this );
442 break;
443
445 m_placer = std::make_unique<DIFF_PAIR_PLACER>( this );
446 break;
447
449 m_placer = std::make_unique<MEANDER_PLACER>( this );
450 break;
451
453 m_placer = std::make_unique<DP_MEANDER_PLACER>( this );
454 break;
455
457 m_placer = std::make_unique<MEANDER_SKEW_PLACER>( this );
458 break;
459
460 default:
461 return false;
462 }
463
464 m_placer->UpdateSizes( m_sizes );
465 m_placer->SetLayer( aLayer );
466 m_placer->SetDebugDecorator( m_iface->GetDebugDecorator() );
467 m_placer->SetLogger( m_logger );
468
469 if( m_placer->Start( aP, aStartItem ) )
470 {
472
473 if( m_logger )
474 {
475 m_logger->Clear();
476 m_logger->Log( LOGGER::EVT_START_ROUTE, aP, aStartItem, &m_sizes, m_placer->CurrentLayer() );
477 }
478
479 return true;
480 }
481 else
482 {
483 m_state = IDLE;
484 m_placer.reset();
485
486 return false;
487 }
488}
489
490
491bool ROUTER::Move( const VECTOR2I& aP, ITEM* endItem )
492{
493 if( m_logger )
494 m_logger->Log( LOGGER::EVT_MOVE, aP, endItem );
495
496 switch( m_state )
497 {
498 case ROUTE_TRACK:
499 return movePlacing( aP, endItem );
500
501 case DRAG_SEGMENT:
502 case DRAG_COMPONENT:
503 return moveDragging( aP, endItem );
504
505 default:
506 break;
507 }
508
510
511 return false;
512}
513
514
516 ITEM*& aOtherEndItem )
517{
518 // Can't finish something with no connections
519 if( GetCurrentNets().empty() )
520 return false;
521
522 PLACEMENT_ALGO* placer = Placer();
523
524 if( placer == nullptr || placer->Traces().Size() == 0 )
525 return false;
526
527 LINE* trace = dynamic_cast<LINE*>( placer->Traces()[0] );
528
529 if( trace == nullptr )
530 return false;
531
532 PNS::NODE* lastNode = placer->CurrentNode( true );
533 PNS::TOPOLOGY topo( lastNode );
534
535 // If the user has drawn a line, get the anchor nearest to the line end
536 if( trace->SegmentCount() > 0 )
537 {
538 return topo.NearestUnconnectedAnchorPoint( trace, aOtherEnd, aOtherEndLayers,
539 aOtherEndItem );
540 }
541
542 // Otherwise, find the closest anchor to our start point
543
544 // Get joint from placer start item
545 const JOINT* jt = lastNode->FindJoint( placer->CurrentStart(), placer->CurrentLayer(),
546 placer->CurrentNets()[0] );
547
548 if( !jt )
549 return false;
550
551 // Get unconnected item from joint
552 int anchor;
553 PNS::ITEM* it = topo.NearestUnconnectedItem( jt, &anchor );
554
555 if( !it )
556 return false;
557
558 aOtherEnd = it->Anchor( anchor );
559 aOtherEndLayers = it->Layers();
560 aOtherEndItem = it;
561
562 return true;
563}
564
565
567{
568 if( m_state != ROUTE_TRACK )
569 return false;
570
571 PLACEMENT_ALGO* placer = Placer();
572
573 if( placer == nullptr || placer->Traces().Size() == 0 )
574 return false;
575
576 LINE* current = dynamic_cast<LINE*>( placer->Traces()[0] );
577
578 if( current == nullptr )
579 return false;
580
581 // Get our current line and position and nearest ratsnest to them if it exists
582 VECTOR2I otherEnd;
583 PNS_LAYER_RANGE otherEndLayers;
584 ITEM* otherEndItem = nullptr;
585
586 // Get the anchor nearest to the end of the trace the user is routing
587 if( !GetNearestRatnestAnchor( otherEnd, otherEndLayers, otherEndItem ) )
588 return false;
589
590 // Keep moving until we don't change position or hit the limit
591 int triesLeft = 5;
592 VECTOR2I moveResultPoint;
593
594 do
595 {
596 moveResultPoint = placer->CurrentEnd();
597 Move( otherEnd, otherEndItem );
598 triesLeft--;
599 } while( placer->CurrentEnd() != moveResultPoint && triesLeft );
600
601 // If we've made it, fix the route and we're done
602 if( moveResultPoint == otherEnd && otherEndLayers.Overlaps( GetCurrentLayer() ) )
603 {
604 bool forceFinish = false;
605 bool allowViolations = false;
606
607 return FixRoute( otherEnd, otherEndItem, forceFinish, allowViolations );
608 }
609
610 return false;
611}
612
613
614bool ROUTER::ContinueFromEnd( ITEM** aNewStartItem )
615{
616 PLACEMENT_ALGO* placer = Placer();
617
618 if( placer == nullptr || placer->Traces().Size() == 0 )
619 return false;
620
621 LINE* current = dynamic_cast<LINE*>( placer->Traces()[0] );
622
623 if( current == nullptr )
624 return false;
625
626 int currentLayer = GetCurrentLayer();
627 VECTOR2I currentEnd = placer->CurrentEnd();
628 VECTOR2I otherEnd;
629 PNS_LAYER_RANGE otherEndLayers;
630 ITEM* otherEndItem = nullptr;
631
632 // Get the anchor nearest to the end of the trace the user is routing
633 if( !GetNearestRatnestAnchor( otherEnd, otherEndLayers, otherEndItem ) )
634 return false;
635
637
638 // Commit whatever we've fixed and restart routing from the other end
639 int nextLayer = otherEndLayers.Overlaps( currentLayer ) ? currentLayer : otherEndLayers.Start();
640
641 if( !StartRouting( otherEnd, otherEndItem, nextLayer ) )
642 return false;
643
644 // Attempt to route to our current position
645 Move( currentEnd, nullptr );
646
647 *aNewStartItem = otherEndItem;
648
649 return true;
650}
651
652
653bool ROUTER::moveDragging( const VECTOR2I& aP, ITEM* aEndItem )
654{
655 m_iface->EraseView();
656
657 bool ret = m_dragger->Drag( aP );
658 ITEM_SET dragged = m_dragger->Traces();
659
660 m_leaderSegments = m_dragger->GetLastCommittedLeaderSegments();
661
662 updateView( m_dragger->CurrentNode(), dragged, true );
663 return ret;
664}
665
666
667void ROUTER::markViolations( NODE* aNode, ITEM_SET& aCurrent, NODE::ITEM_VECTOR& aRemoved )
668{
669 auto updateItem =
670 [&]( ITEM* currentItem, ITEM* itemToMark )
671 {
672 std::unique_ptr<ITEM> tmp( itemToMark->Clone() );
673
674 int clearance;
675 bool removeOriginal = true;
676
677 clearance = aNode->GetClearance( currentItem, itemToMark );
678
679 if( itemToMark->Layers().IsMultilayer() && !currentItem->Layers().IsMultilayer() )
680 tmp->SetLayer( currentItem->Layer() );
681
682 if( itemToMark->IsCompoundShapePrimitive() )
683 {
684 // We're only highlighting one (or more) of several primitives so we don't
685 // want all the other parts of the object to disappear
686 removeOriginal = false;
687 }
688
689 m_iface->DisplayItem( tmp.get(), clearance );
690
691 if( removeOriginal )
692 aRemoved.push_back( itemToMark );
693 };
694
695 for( ITEM* item : aCurrent.Items() )
696 {
697 NODE::OBSTACLES obstacles;
698
699 aNode->QueryColliding( item, obstacles );
700
701 if( item->OfKind( ITEM::LINE_T ) )
702 {
703 LINE* l = static_cast<LINE*>( item );
704
705 if( l->EndsWithVia() )
706 {
707 VIA v( l->Via() );
708 aNode->QueryColliding( &v, obstacles );
709 }
710 }
711
712 ITEM_SET draggedItems;
713
714 if( GetDragger() )
715 draggedItems = GetDragger()->Traces();
716
717 for( const OBSTACLE& obs : obstacles )
718 {
719 // Don't mark items being dragged; only board items they collide with
720 if( draggedItems.Contains( obs.m_item ) )
721 continue;
722
723 obs.m_item->Mark( obs.m_item->Marker() | MK_VIOLATION );
724 updateItem( item, obs.m_item );
725 }
726
727 if( item->Kind() == ITEM::LINE_T )
728 {
729 LINE* line = static_cast<LINE*>( item );
730
731 // Show clearance on any blocking obstacles
732 if( line->GetBlockingObstacle() )
733 updateItem( item, line->GetBlockingObstacle() );
734 }
735 }
736}
737
738
739void ROUTER::updateView( NODE* aNode, ITEM_SET& aCurrent, bool aDragging )
740{
741 NODE::ITEM_VECTOR removed, added;
742 NODE::OBSTACLES obstacles;
743
744 if( !aNode )
745 return;
746
747 markViolations( aNode, aCurrent, removed );
748
749 aNode->GetUpdatedItems( removed, added );
750
751 std::vector<const PNS::ITEM*> cacheCheckItems( added.begin(), added.end() );
752 GetRuleResolver()->ClearCacheForItems( cacheCheckItems );
753
754 for( ITEM* item : added )
755 {
756 int clearance = GetRuleResolver()->Clearance( item, nullptr );
757 m_iface->DisplayItem( item, clearance, aDragging );
758 }
759
760 for( ITEM* item : removed )
761 m_iface->HideItem( item );
762}
763
764
766{
767 m_sizes = aSizes;
768
769 // Change track/via size settings
770 if( m_state == ROUTE_TRACK )
771 m_placer->UpdateSizes( m_sizes );
772}
773
774
775bool ROUTER::movePlacing( const VECTOR2I& aP, ITEM* aEndItem )
776{
777 m_iface->EraseView();
778
779 bool ret = m_placer->Move( aP, aEndItem );
780 ITEM_SET current = m_placer->Traces();
781
782 for( const ITEM* item : current.CItems() )
783 {
784 if( !item->OfKind( ITEM::LINE_T ) )
785 continue;
786
787 const LINE* l = static_cast<const LINE*>( item );
788 int clearance = GetRuleResolver()->Clearance( item, nullptr );
789
790 m_iface->DisplayItem( l, clearance, false, PNS_HEAD_TRACE );
791
792 if( l->EndsWithVia() )
793 {
794 const VIA& via = l->Via();
795 clearance = GetRuleResolver()->Clearance( &via, nullptr );
796
797 if( via.HasHole() )
798 {
799 int holeClearance = GetRuleResolver()->Clearance( via.Hole(), nullptr );
800 int annularWidth = std::max( 0, via.Diameter( l->Layer() ) - via.Drill() ) / 2;
801 int excessHoleClearance = holeClearance - annularWidth;
802
803 if( excessHoleClearance > clearance )
804 clearance = excessHoleClearance;
805 }
806
807 m_iface->DisplayItem( &l->Via(), clearance, false, PNS_HEAD_TRACE );
808 }
809 }
810
811 //ITEM_SET tmp( &current );
812
813 updateView( m_placer->CurrentNode( true ), current );
814
815 return ret;
816}
817
818
819void ROUTER::GetUpdatedItems( std::vector<PNS::ITEM*>& aRemoved, std::vector<PNS::ITEM*>& aAdded,
820 std::vector<PNS::ITEM*>& aHeads )
821{
822 NODE *node = nullptr;
823 ITEM_SET current;
824
825 if( m_state == ROUTE_TRACK )
826 {
827 node = m_placer->CurrentNode( true );
828 current = m_placer->Traces();
829 }
830 else if ( m_state == DRAG_SEGMENT )
831 {
832 node = m_dragger->CurrentNode();
833 current = m_dragger->Traces();
834 }
835
836 // There probably should be a debugging assertion and possibly a PNS_LOGGER call here but
837 // I'm not sure how to be proceed WLS.
838 if( !node )
839 return;
840
841 node->GetUpdatedItems( aRemoved, aAdded );
842
843 for( const ITEM* item : current.CItems() )
844 aHeads.push_back( item->Clone() );
845}
846
847
849{
850 if( m_state == ROUTE_TRACK && !m_placer->HasPlacedAnything() )
851 return;
852
853 NODE::ITEM_VECTOR removed;
854 NODE::ITEM_VECTOR added;
855 NODE::ITEM_VECTOR changed;
856
857 aNode->GetUpdatedItems( removed, added );
858
859 for( ITEM* item : removed )
860 {
861 bool is_changed = false;
862
863 // Items in remove/add that share the same parent are just updated versions
864 // We move them to the updated vector to preserve attributes such as UUID and pad data
865 if( item->Parent() )
866 {
867 for( NODE::ITEM_VECTOR::iterator added_it = added.begin();
868 added_it != added.end(); ++added_it )
869 {
870 if( ( *added_it )->Parent() && ( *added_it )->Parent() == item->Parent() )
871 {
872 changed.push_back( *added_it );
873 added.erase( added_it );
874 is_changed = true;
875 break;
876 }
877 }
878 }
879
880 if( !is_changed && !item->IsVirtual() )
881 m_iface->RemoveItem( item );
882 }
883
884 for( ITEM* item : added )
885 {
886 if( !item->IsVirtual() )
887 m_iface->AddItem( item );
888 }
889
890 for( ITEM* item : changed )
891 {
892 if( !item->IsVirtual() )
893 m_iface->UpdateItem( item );
894 }
895
896 m_iface->Commit();
897 m_world->Commit( aNode );
898}
899
900
901bool ROUTER::FixRoute( const VECTOR2I& aP, ITEM* aEndItem, bool aForceFinish, bool aForceCommit )
902{
903 bool rv = false;
904
905 if( m_logger )
906 m_logger->Log( LOGGER::EVT_FIX, aP, aEndItem );
907
908 switch( m_state )
909 {
910 case ROUTE_TRACK:
911 rv = m_placer->FixRoute( aP, aEndItem, aForceFinish );
912 break;
913
914 case DRAG_SEGMENT:
915 case DRAG_COMPONENT:
916 rv = m_dragger->FixRoute( aForceCommit );
917 break;
918
919 default:
920 break;
921 }
922
923 return rv;
924}
925
927{
928 return m_leaderSegments;
929};
930
931
932std::optional<VECTOR2I> ROUTER::UndoLastSegment()
933{
934 if( !RoutingInProgress() )
935 return std::nullopt;
936
937 if( m_logger )
939
940 return m_placer->UnfixRoute();
941}
942
943
945{
946 if( m_state == ROUTE_TRACK )
947 m_placer->CommitPlacement();
948
949 StopRouting();
950}
951
952
954{
955 // Update the ratsnest with new changes
956
957 if( m_placer )
958 {
959 std::vector<NET_HANDLE> nets;
960 m_placer->GetModifiedNets( nets );
961
962 // Update the ratsnest with new changes
963 for( NET_HANDLE n : nets )
964 m_iface->UpdateNet( n );
965 }
966
967 if( !RoutingInProgress() )
968 return;
969
970 m_placer.reset();
971 m_dragger.reset();
972
973 m_iface->EraseView();
974
975 m_state = IDLE;
976 m_world->KillChildren();
977 m_world->ClearRanks();
978}
979
980
982{
983 m_iface->EraseView();
984}
985
986
988{
989 if( m_state == ROUTE_TRACK )
990 {
991 m_placer->FlipPosture();
992 }
993}
994
995
996bool ROUTER::SwitchLayer( int aLayer )
997{
998 if( m_state == ROUTE_TRACK )
999 return m_placer->SetLayer( aLayer );
1000
1001 return false;
1002}
1003
1004
1006{
1007 if( m_state == ROUTE_TRACK )
1008 {
1009 bool toggle = !m_placer->IsPlacingVia();
1010 m_placer->ToggleVia( toggle );
1011
1012 if( m_logger )
1013 m_logger->Log( LOGGER::EVT_TOGGLE_VIA, VECTOR2I(), nullptr, &m_sizes );
1014 }
1015}
1016
1017
1018const std::vector<NET_HANDLE> ROUTER::GetCurrentNets() const
1019{
1020 if( m_placer )
1021 return m_placer->CurrentNets();
1022 else if( m_dragger )
1023 return m_dragger->CurrentNets();
1024
1025 return std::vector<NET_HANDLE>();
1026}
1027
1028
1030{
1031 if( m_placer )
1032 return m_placer->CurrentLayer();
1033 else if( m_dragger )
1034 return m_dragger->CurrentLayer();
1035
1036 return -1;
1037}
1038
1039
1041{
1042 return m_logger;
1043}
1044
1045
1047{
1048 if( !m_placer )
1049 return false;
1050
1051 return m_placer->IsPlacingVia();
1052}
1053
1054
1069
1070
1071void ROUTER::SetOrthoMode( bool aEnable )
1072{
1073 if( !m_placer )
1074 return;
1075
1076 m_placer->SetOrthoMode( aEnable );
1077}
1078
1079
1081{
1082 m_mode = aMode;
1083}
1084
1085
1087{
1088 m_iface = aIface;
1089}
1090
1091
1092void ROUTER::BreakSegmentOrArc( ITEM *aItem, const VECTOR2I& aP )
1093{
1094 NODE *node = m_world->Branch();
1095
1096 LINE_PLACER placer( this );
1097
1098 bool ret = false;
1099
1100 if( aItem->OfKind( ITEM::SEGMENT_T ) )
1101 ret = placer.SplitAdjacentSegments( node, aItem, aP );
1102 else if( aItem->OfKind( ITEM::ARC_T ) )
1103 ret = placer.SplitAdjacentArcs( node, aItem, aP );
1104
1105 if( ret )
1106 {
1107 CommitRouting( node );
1108 }
1109 else
1110 {
1111 delete node;
1112 }
1113}
1114
1115}
static const ADVANCED_CFG & GetCfg()
Get the singleton instance's config, which is shared by all consumers.
A base class for any item which can be embedded within the BOARD container class, and therefore insta...
Definition board_item.h:83
CORNER_MODE
Corner modes.
Definition direction45.h:67
@ ROUNDED_90
H/V with filleted corners.
Definition direction45.h:71
@ MITERED_90
H/V only (90-degree corners)
Definition direction45.h:70
@ ROUNDED_45
H/V/45 with filleted corners.
Definition direction45.h:69
@ MITERED_45
H/V/45 with mitered corners (default)
Definition direction45.h:68
KICAD_T Type() const
Returns the type of object.
Definition eda_item.h:110
Definition pad.h:55
static bool FindDpPrimitivePair(NODE *aWorld, const VECTOR2I &aP, ITEM *aItem, DP_PRIMITIVE_PAIR &aPair, wxString *aErrorMsg=nullptr)
Store starting/ending primitives (pads, vias or segments) for a differential pair.
const VECTOR2I & AnchorN() const
const VECTOR2I & AnchorP() const
virtual const ITEM_SET Traces()=0
Function Traces()
bool Empty() const
Definition pns_itemset.h:82
int Size() const
int Count(int aKindMask=-1) const
Definition pns_itemset.h:66
void Add(const LINE &aLine)
bool Contains(ITEM *aItem) const
std::vector< ITEM * > & Items()
Definition pns_itemset.h:87
const std::vector< ITEM * > & CItems() const
Definition pns_itemset.h:88
Base class for PNS router board items.
Definition pns_item.h:98
const PNS_LAYER_RANGE & Layers() const
Definition pns_item.h:212
virtual NET_HANDLE Net() const
Definition pns_item.h:210
void SetNet(NET_HANDLE aNet)
Definition pns_item.h:209
virtual int Layer() const
Definition pns_item.h:216
void SetLayer(int aLayer)
Definition pns_item.h:215
bool OfKind(int aKindMask) const
Definition pns_item.h:181
virtual VECTOR2I Anchor(int n) const
Definition pns_item.h:268
A 2D point on a given set of layers and belonging to a certain net, that links together a number of b...
Definition pns_joint.h:43
Single track placement algorithm.
bool SplitAdjacentArcs(NODE *aNode, ITEM *aArc, const VECTOR2I &aP)
Snaps the point aP to arc aArc.
bool SplitAdjacentSegments(NODE *aNode, ITEM *aSeg, const VECTOR2I &aP)
Snaps the point aP to segment aSeg.
Represents a track on a PCB, connecting two non-trivial joints (that is, vias, pads,...
Definition pns_line.h:62
ITEM * GetBlockingObstacle() const
Definition pns_line.h:235
void SetShape(const SHAPE_LINE_CHAIN &aLine)
Return the shape of the line.
Definition pns_line.h:131
VIA & Via()
Definition pns_line.h:203
int SegmentCount() const
Definition pns_line.h:144
void SetWidth(int aWidth)
Return line width.
Definition pns_line.h:155
bool EndsWithVia() const
Definition pns_line.h:195
@ EVT_START_MULTIDRAG
Definition pns_logger.h:54
Keep the router "world" - i.e.
Definition pns_node.h:240
std::vector< ITEM * > ITEM_VECTOR
Definition pns_node.h:251
int GetClearance(const ITEM *aA, const ITEM *aB, bool aUseClearanceEpsilon=true) const
Return the pre-set worst case clearance between any pair of items.
Definition pns_node.cpp:141
void GetUpdatedItems(ITEM_VECTOR &aRemoved, ITEM_VECTOR &aAdded)
Return the list of items removed and added in this branch with respect to the root branch.
const JOINT * FindJoint(const VECTOR2I &aPos, int aLayer, NET_HANDLE aNet) const
Search for a joint at a given position, layer and belonging to given net.
std::set< OBSTACLE > OBSTACLES
Definition pns_node.h:252
int QueryColliding(const ITEM *aItem, OBSTACLES &aObstacles, const COLLISION_SEARCH_OPTIONS &aOpts=COLLISION_SEARCH_OPTIONS()) const
Find items colliding (closer than clearance) with the item aItem.
Definition pns_node.cpp:267
const ITEM_SET HitTest(const VECTOR2I &aPoint) const
Find all items that contain the point aPoint.
Definition pns_node.cpp:520
virtual NODE * CurrentNode(bool aLoopsRemoved=false) const =0
Function CurrentNode()
virtual const VECTOR2I & CurrentEnd() const =0
Function CurrentEnd()
virtual const ITEM_SET Traces()=0
Function Traces()
virtual const VECTOR2I & CurrentStart() const =0
Function CurrentStart()
virtual int CurrentLayer() const =0
Function CurrentLayer()
virtual const std::vector< NET_HANDLE > CurrentNets() const =0
Function CurrentNets()
void updateView(NODE *aNode, ITEM_SET &aCurrent, bool aDragging=false)
void SetMode(ROUTER_MODE aMode)
bool moveDragging(const VECTOR2I &aP, ITEM *aItem)
bool SwitchLayer(int layer)
void StopRouting()
void ClearViewDecorations()
std::vector< PNS::ITEM * > m_leaderSegments
Definition pns_router.h:263
void ToggleCornerMode()
PLACEMENT_ALGO * Placer()
Definition pns_router.h:230
std::vector< PNS::ITEM * > GetLastCommittedLeaderSegments()
NODE * m_lastNode
Definition pns_router.h:258
void ClearWorld()
void BreakSegmentOrArc(ITEM *aItem, const VECTOR2I &aP)
bool ContinueFromEnd(ITEM **aNewStartItem)
void UpdateSizes(const SIZES_SETTINGS &aSizes)
Applies stored settings.
void SetFailureReason(const wxString &aReason)
Definition pns_router.h:227
LOGGER * Logger()
RouterState m_state
Definition pns_router.h:255
void CommitRouting()
std::unique_ptr< DRAG_ALGO > m_dragger
Definition pns_router.h:261
const ITEM_SET QueryHoverItems(const VECTOR2I &aP, int aSlopRadius=0)
void SetInterface(ROUTER_IFACE *aIface)
void markViolations(NODE *aNode, ITEM_SET &aCurrent, NODE::ITEM_VECTOR &aRemoved)
void SyncWorld()
std::unique_ptr< PLACEMENT_ALGO > m_placer
Definition pns_router.h:260
bool isStartingPointRoutable(const VECTOR2I &aWhere, ITEM *aItem, int aLayer)
ROUTER_IFACE * m_iface
Definition pns_router.h:265
bool IsPlacingVia() const
void FlipPosture()
RULE_RESOLVER * GetRuleResolver() const
Definition pns_router.h:194
SIZES_SETTINGS m_sizes
Definition pns_router.h:270
ROUTING_SETTINGS & Settings()
Definition pns_router.h:206
DRAG_ALGO * GetDragger()
Definition pns_router.h:154
bool movePlacing(const VECTOR2I &aP, ITEM *aItem)
bool RoutingInProgress() const
BOX2I m_visibleViewArea
Definition pns_router.h:254
static ROUTER * GetInstance()
std::optional< VECTOR2I > UndoLastSegment()
LOGGER * m_logger
Definition pns_router.h:272
void SetOrthoMode(bool aEnable)
bool StartDragging(const VECTOR2I &aP, ITEM *aItem, int aDragMode=DM_ANY)
bool StartRouting(const VECTOR2I &aP, ITEM *aItem, int aLayer)
int GetCurrentLayer() const
void GetUpdatedItems(std::vector< PNS::ITEM * > &aRemoved, std::vector< PNS::ITEM * > &aAdded, std::vector< PNS::ITEM * > &aHeads)
bool FixRoute(const VECTOR2I &aP, ITEM *aItem, bool aForceFinish, bool aForceCommit)
std::unique_ptr< NODE > m_world
Definition pns_router.h:257
void ToggleViaPlacement()
ROUTING_SETTINGS * m_settings
Definition pns_router.h:269
const std::vector< NET_HANDLE > GetCurrentNets() const
ROUTER_MODE m_mode
Definition pns_router.h:271
bool GetNearestRatnestAnchor(VECTOR2I &aOtherEnd, PNS_LAYER_RANGE &aOtherEndLayers, ITEM *&aOtherEndItem)
bool Move(const VECTOR2I &aP, ITEM *aItem)
virtual void ClearCacheForItems(std::vector< const ITEM * > &aItems)
Definition pns_node.h:168
virtual void ClearTemporaryCaches()
Definition pns_node.h:170
virtual int Clearance(const ITEM *aA, const ITEM *aB, bool aUseClearanceEpsilon=true)=0
virtual void ClearCaches()
Definition pns_node.h:169
ITEM * NearestUnconnectedItem(const JOINT *aStart, int *aAnchor=nullptr, int aKindMask=ITEM::ANY_T)
bool NearestUnconnectedAnchorPoint(const LINE *aTrack, VECTOR2I &aPoint, PNS_LAYER_RANGE &aLayers, ITEM *&aItem)
Represent a contiguous set of PCB layers.
int Start() const
bool Overlaps(const PNS_LAYER_RANGE &aOther) const
static PNS_LAYER_RANGE All()
bool IsMultilayer() const
Definition seg.h:42
Represent a polyline containing arcs as well as line segments: A chain of connected line and/or arc s...
void Append(int aX, int aY, bool aAllowDuplication=false)
Append a new point at the end of the line chain.
Handle a list of polygons defining a copper zone.
Definition zone.h:74
const wxString & GetZoneName() const
Definition zone.h:164
bool HasKeepoutParametersSet() const
Accessor to determine if any keepout parameters are set.
Definition zone.h:706
static bool empty(const wxTextEntryBase *aCtrl)
#define _(s)
@ Edge_Cuts
Definition layer_ids.h:112
Push and Shove diff pair dimensions (gap) settings dialog.
static ROUTER * theRouter
void * NET_HANDLE
Definition pns_item.h:55
ROUTER_MODE
Definition pns_router.h:62
@ PNS_MODE_ROUTE_SINGLE
Definition pns_router.h:63
@ PNS_MODE_ROUTE_DIFF_PAIR
Definition pns_router.h:64
@ PNS_MODE_TUNE_DIFF_PAIR
Definition pns_router.h:66
@ PNS_MODE_TUNE_SINGLE
Definition pns_router.h:65
@ PNS_MODE_TUNE_DIFF_PAIR_SKEW
Definition pns_router.h:67
DRAG_MODE
Definition pns_router.h:71
@ MK_VIOLATION
Definition pns_item.h:44
EDA_ANGLE abs(const EDA_ANGLE &aAngle)
Definition eda_angle.h:400
@ NPTH
like PAD_PTH, but not plated mechanical use only, no connection allowed
Definition padstack.h:103
#define PNS_HEAD_TRACE
Hold an object colliding with another object, along with some useful data about the collision.
Definition pns_node.h:88
int clearance
@ PCB_TEXTBOX_T
class PCB_TEXTBOX, wrapped text on a layer
Definition typeinfo.h:93
@ PCB_ZONE_T
class ZONE, a copper pour area
Definition typeinfo.h:108
@ PCB_TEXT_T
class PCB_TEXT, text on a layer
Definition typeinfo.h:92
@ PCB_FIELD_T
class PCB_FIELD, text associated with a footprint property
Definition typeinfo.h:90
@ PCB_PAD_T
class PAD, a pad in a footprint
Definition typeinfo.h:87
VECTOR2< int32_t > VECTOR2I
Definition vector2d.h:695