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