KiCad PCB EDA Suite
Loading...
Searching...
No Matches
connectivity_data.cpp
Go to the documentation of this file.
1/*
2 * This program source code file is part of KICAD, a free EDA CAD application.
3 *
4 * Copyright (C) 2017 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
9 * modify it under the terms of the GNU General Public License
10 * as published by the Free Software Foundation; either version 2
11 * of the License, or (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program. If not, see <https://www.gnu.org/licenses/>.
20 */
21
22#ifdef PROFILE
23#include <core/profile.h>
24#endif
25
26#include <algorithm>
27#include <future>
28#include <initializer_list>
29
32#include <properties/property.h>
34#include <board_item.h>
39#include <footprint.h>
40#include <pad.h>
41#include <pcb_track.h>
43#include <progress_reporter.h>
44#include <thread_pool.h>
45#include <trigo.h>
46#include <drc/drc_rtree.h>
48
56
57
58CONNECTIVITY_DATA::CONNECTIVITY_DATA( std::shared_ptr<CONNECTIVITY_DATA> aGlobalConnectivity,
59 const std::vector<BOARD_ITEM*>& aLocalItems,
60 bool aSkipRatsnestUpdate ) :
61 m_skipRatsnestUpdate( aSkipRatsnestUpdate )
62{
63 Build( aGlobalConnectivity, aLocalItems );
64 m_progressReporter = nullptr;
65 m_fromToCache.reset( new FROM_TO_CACHE );
66}
67
68
70{
71 for( RN_NET* net : m_nets )
72 delete net;
73
74 m_nets.clear();
75}
76
77
79{
80 m_connAlgo->Add( aItem );
81 return true;
82}
83
84
86{
87 m_connAlgo->Remove( aItem );
88 return true;
89}
90
91
93{
94 m_connAlgo->Remove( aItem );
95 m_connAlgo->Add( aItem );
96 return true;
97}
98
99
101{
102 aBoard->CacheTriangulation( aReporter );
103
104 std::unique_lock<KISPINLOCK> lock( m_lock, std::try_to_lock );
105
106 if( !lock )
107 return false;
108
109 if( aReporter )
110 {
111 aReporter->Report( _( "Updating nets..." ) );
112 aReporter->KeepRefreshing( false );
113 }
114
115 for( RN_NET* net : m_nets )
116 delete net;
117
118 m_nets.clear();
119
120 m_connAlgo.reset( new CN_CONNECTIVITY_ALGO( this ) );
121 m_connAlgo->Build( aBoard, aReporter );
122
124
125 RefreshNetcodeMap( aBoard );
126
127 if( aReporter )
128 {
129 aReporter->SetCurrentProgress( 0.75 );
130 aReporter->KeepRefreshing( false );
131 }
132
134
135 if( aReporter )
136 {
137 aReporter->SetCurrentProgress( 1.0 );
138 aReporter->KeepRefreshing( false );
139 }
140
141 return true;
142}
143
144
146{
147 m_netcodeMap.clear();
148
149 for( NETINFO_ITEM* net : aBoard->GetNetInfo() )
150 m_netcodeMap[net->GetNetCode()] = net->GetNetname();
151}
152
153
154void CONNECTIVITY_DATA::Build( std::shared_ptr<CONNECTIVITY_DATA>& aGlobalConnectivity,
155 const std::vector<BOARD_ITEM*>& aLocalItems )
156{
157 std::unique_lock<KISPINLOCK> lock( m_lock, std::try_to_lock );
158
159 if( !lock )
160 return;
161
162 m_connAlgo.reset( new CN_CONNECTIVITY_ALGO( this ) );
163 m_connAlgo->LocalBuild( aGlobalConnectivity, aLocalItems );
164
166}
167
168
170{
171 m_connAlgo->ForEachAnchor( [&aDelta]( CN_ANCHOR& anchor )
172 {
173 anchor.Move( aDelta );
174 } );
175}
176
177
179{
180#ifdef PROFILE
181 PROF_TIMER rnUpdate( "update-ratsnest" );
182#endif
183
184 std::vector<RN_NET*> dirty_nets;
185
186 // Start with net 1 as net 0 is reserved for not-connected
187 // Nets without nodes are also ignored
188 std::copy_if( m_nets.begin() + 1, m_nets.end(), std::back_inserter( dirty_nets ),
189 [] ( RN_NET* aNet )
190 {
191 return aNet->IsDirty() && aNet->GetNodeCount() > 0;
192 } );
193
195
196 auto results = tp.submit_loop( 0, dirty_nets.size(),
197 [&]( const int ii )
198 {
199 dirty_nets[ii]->UpdateNet();
200 } );
201 results.wait();
202
203 auto results2 = tp.submit_loop( 0, dirty_nets.size(),
204 [&]( const int ii )
205 {
206 dirty_nets[ii]->OptimizeRNEdges();
207 } );
208 results2.wait();
209
210#ifdef PROFILE
211 rnUpdate.Show();
212#endif
213}
214
215
216void CONNECTIVITY_DATA::addRatsnestCluster( const std::shared_ptr<CN_CLUSTER>& aCluster )
217{
218 RN_NET* rnNet = m_nets[ aCluster->OriginNet() ];
219
220 rnNet->AddCluster( aCluster );
221}
222
223
225{
226
227 // We can take over the lock here if called in the same thread
228 // This is to prevent redraw during a RecalculateRatsnets process
229 std::unique_lock<KISPINLOCK> lock( m_lock );
230
232
233}
234
236{
237 m_connAlgo->PropagateNets( aCommit );
238
239 int lastNet = m_connAlgo->NetCount();
240
241 if( lastNet >= (int) m_nets.size() )
242 {
243 unsigned int prevSize = m_nets.size();
244 m_nets.resize( lastNet + 1 );
245
246 for( unsigned int i = prevSize; i < m_nets.size(); i++ )
247 m_nets[i] = new RN_NET;
248 }
249 else
250 {
251 for( size_t ii = lastNet; ii < m_nets.size(); ++ii )
252 m_nets[ii]->Clear();
253 }
254
255 const std::vector<std::shared_ptr<CN_CLUSTER>>& clusters = m_connAlgo->GetClusters();
256
257 for( int net = 0; net < lastNet; net++ )
258 {
259 if( m_connAlgo->IsNetDirty( net ) )
260 m_nets[net]->Clear();
261 }
262
263 for( const std::shared_ptr<CN_CLUSTER>& c : clusters )
264 {
265 int net = c->OriginNet();
266
267 // Don't add intentionally-kept zone islands to the ratsnest
268 if( c->IsOrphaned() && c->Size() == 1 )
269 {
270 if( dynamic_cast<CN_ZONE_LAYER*>( *c->begin() ) )
271 continue;
272 }
273
274 if( m_connAlgo->IsNetDirty( net ) )
276 }
277
278 m_connAlgo->ClearDirtyFlags();
279
282}
283
284
285void CONNECTIVITY_DATA::BlockRatsnestItems( const std::vector<BOARD_ITEM*>& aItems )
286{
287 std::vector<BOARD_CONNECTED_ITEM*> citems;
288
289 for( BOARD_ITEM* item : aItems )
290 {
291 if( item->Type() == PCB_FOOTPRINT_T )
292 {
293 for( PAD* pad : static_cast<FOOTPRINT*>(item)->Pads() )
294 citems.push_back( pad );
295 }
296 else
297 {
298 if( BOARD_CONNECTED_ITEM* citem = dynamic_cast<BOARD_CONNECTED_ITEM*>( item ) )
299 citems.push_back( citem );
300 }
301 }
302
303 for( const BOARD_CONNECTED_ITEM* item : citems )
304 {
305 if ( m_connAlgo->ItemExists( item ) )
306 {
307 CN_CONNECTIVITY_ALGO::ITEM_MAP_ENTRY& entry = m_connAlgo->ItemEntry( item );
308
309 for( CN_ITEM* cnItem : entry.GetItems() )
310 {
311 for( const std::shared_ptr<CN_ANCHOR>& anchor : cnItem->Anchors() )
312 anchor->SetNoLine( true );
313 }
314 }
315 }
316}
317
318
320{
321 return m_connAlgo->NetCount();
322}
323
324
325void CONNECTIVITY_DATA::FillIsolatedIslandsMap( std::map<ZONE*, std::map<PCB_LAYER_ID, ISOLATED_ISLANDS>>& aMap,
326 bool aConnectivityAlreadyRebuilt )
327{
328 m_connAlgo->FillIsolatedIslandsMap( aMap, aConnectivityAlreadyRebuilt );
329}
330
331
332void CONNECTIVITY_DATA::ComputeLocalRatsnest( const std::vector<BOARD_ITEM*>& aItems,
333 const CONNECTIVITY_DATA* aDynamicData,
334 VECTOR2I aInternalOffset )
335{
336 if( !aDynamicData )
337 return;
338
339 m_dynamicRatsnest.clear();
340 std::mutex dynamic_ratsnest_mutex;
341
342 // This gets connections between the stationary board and the
343 // moving selection
344
345 auto update_lambda = [&]( int nc )
346 {
347 RN_NET* dynamicNet = aDynamicData->m_nets[nc];
348 RN_NET* staticNet = m_nets[nc];
349
353 if( dynamicNet->GetNodeCount() != 0 && dynamicNet->GetNodeCount() != staticNet->GetNodeCount() )
354 {
355 VECTOR2I pos1, pos2;
356
357 if( staticNet->NearestBicoloredPair( dynamicNet, pos1, pos2 ) )
358 {
360 l.a = pos1;
361 l.b = pos2;
362 l.netCode = nc;
363
364 std::lock_guard<std::mutex> lock( dynamic_ratsnest_mutex );
365 m_dynamicRatsnest.push_back( l );
366 }
367 }
368 };
369
371 size_t num_nets = std::min( m_nets.size(), aDynamicData->m_nets.size() );
372
373 auto results = tp.submit_loop( 1, num_nets,
374 [&]( const int ii )
375 {
376 update_lambda( ii );
377 });
378 results.wait();
379
380 // This gets the ratsnest for internal connections in the moving set
381 const std::vector<CN_EDGE>& edges = GetRatsnestForItems( aItems );
382
383 for( const CN_EDGE& edge : edges )
384 {
385 const std::shared_ptr<const CN_ANCHOR>& nodeA = edge.GetSourceNode();
386 const std::shared_ptr<const CN_ANCHOR>& nodeB = edge.GetTargetNode();
387
388 if( !nodeA || nodeA->Dirty() || !nodeB || nodeB->Dirty() )
389 continue;
390
392
393 // Use the parents' positions
394 l.a = nodeA->Parent()->GetPosition() + aInternalOffset;
395 l.b = nodeB->Parent()->GetPosition() + aInternalOffset;
396 l.netCode = 0;
397 m_dynamicRatsnest.push_back( l );
398 }
399}
400
401
403{
404 m_connAlgo->ForEachAnchor( []( CN_ANCHOR& anchor )
405 {
406 anchor.SetNoLine( false );
407 } );
409}
410
411
416
417
419{
420 m_connAlgo->PropagateNets( aCommit );
421}
422
423
425 const std::initializer_list<KICAD_T>& aTypes ) const
426{
427 CN_CONNECTIVITY_ALGO::ITEM_MAP_ENTRY &entry = m_connAlgo->ItemEntry( aItem );
428
429 FOOTPRINT* parentFootprint = aItem->GetParentFootprint();
430
431 auto matchType =
432 [&]( KICAD_T aItemType )
433 {
434 if( aTypes.size() == 0 )
435 return true;
436
437 return alg::contains( aTypes, aItemType);
438 };
439
440 for( CN_ITEM* citem : entry.GetItems() )
441 {
442 for( CN_ITEM* connected : citem->ConnectedItems() )
443 {
444 CN_ZONE_LAYER* zoneLayer = dynamic_cast<CN_ZONE_LAYER*>( connected );
445
446 // StartLayer() and EndLayer() are copper layer ordinals, not PCB_LAYER_IDs
447 int lyIdx = static_cast<int>( CopperLayerToOrdinal( ToLAYER_ID( aLayer ) ) );
448
449 if( connected->Valid()
450 && connected->StartLayer() <= lyIdx && connected->EndLayer() >= lyIdx
451 && matchType( connected->Parent()->Type() )
452 && connected->Net() == aItem->GetNetCode() )
453 {
454 BOARD_ITEM* connectedItem = connected->Parent();
455
456 if( connectedItem == aItem )
457 continue;
458
459 if( parentFootprint && connectedItem
460 && connectedItem->GetParentFootprint() == parentFootprint )
461 {
462 continue;
463 }
464
465 if( aItem->Type() == PCB_PAD_T && connectedItem
466 && connectedItem->Type() == PCB_PAD_T )
467 {
468 const PAD* thisPad = static_cast<const PAD*>( aItem );
469 const PAD* otherPad = static_cast<const PAD*>( connectedItem );
470
471 auto flashesConditionally = []( UNCONNECTED_LAYER_MODE aMode )
472 {
475 };
476
477 if( flashesConditionally( thisPad->Padstack().UnconnectedLayerMode() )
478 && flashesConditionally( otherPad->Padstack().UnconnectedLayerMode() ) )
479 {
480 continue;
481 }
482 }
483
484 if( aItem->Type() == PCB_PAD_T && zoneLayer )
485 {
486 const PAD* pad = static_cast<const PAD*>( aItem );
487 ZONE* zone = static_cast<ZONE*>( zoneLayer->Parent() );
488 int islandIdx = zoneLayer->SubpolyIndex();
489
490 if( zone->IsFilled() )
491 {
492 PCB_LAYER_ID pcbLayer = ToLAYER_ID( aLayer );
493 const SHAPE_POLY_SET* zoneFill = zone->GetFill( pcbLayer );
494 const SHAPE_LINE_CHAIN& padHull = pad->GetEffectivePolygon( pcbLayer,
495 ERROR_INSIDE )->Outline( 0 );
496
497 for( const VECTOR2I& pt : zoneFill->COutline( islandIdx ).CPoints() )
498 {
499 // If the entire island is inside the pad's flashing then the pad
500 // won't actually connect to anything else, so only return true if
501 // part of the island is *outside* the pad's flashing.
502
503 if( !padHull.PointInside( pt ) )
504 return true;
505 }
506 }
507
508 continue;
509 }
510 else if( aItem->Type() == PCB_VIA_T && zoneLayer )
511 {
512 const PCB_VIA* via = static_cast<const PCB_VIA*>( aItem );
513 ZONE* zone = static_cast<ZONE*>( zoneLayer->Parent() );
514 int islandIdx = zoneLayer->SubpolyIndex();
515
516 if( zone->IsFilled() )
517 {
518 PCB_LAYER_ID layer = ToLAYER_ID( aLayer );
519 const SHAPE_POLY_SET* zoneFill = zone->GetFill( layer );
520 SHAPE_CIRCLE viaHull( via->GetCenter(), via->GetWidth( layer ) / 2 );
521
522 for( const VECTOR2I& pt : zoneFill->COutline( islandIdx ).CPoints() )
523 {
524 // If the entire island is inside the via's flashing then the via
525 // won't actually connect to anything else, so only return true if
526 // part of the island is *outside* the via's flashing.
527
528 if( !viaHull.SHAPE::Collide( pt ) )
529 return true;
530 }
531 }
532
533 continue;
534 }
535
536 return true;
537 }
538 }
539 }
540
541 return false;
542}
543
544
545unsigned int CONNECTIVITY_DATA::GetUnconnectedCount( bool aVisibleOnly ) const
546{
547 unsigned int unconnected = 0;
548
549 for( RN_NET* net : m_nets )
550 {
551 if( !net )
552 continue;
553
554 for( const CN_EDGE& edge : net->GetEdges() )
555 {
556 if( edge.IsVisible() || !aVisibleOnly )
557 ++unconnected;
558 }
559 }
560
561 return unconnected;
562}
563
564
566{
567 for( RN_NET* net : m_nets )
568 net->Clear();
569}
570
571
572const std::vector<BOARD_CONNECTED_ITEM*>
574{
577
578 std::vector<BOARD_CONNECTED_ITEM*> rv;
579
580 auto clusters = m_connAlgo->SearchClusters( ( aFlags & IGNORE_NETS ) ? CSM_PROPAGATE : CSM_CONNECTIVITY_CHECK,
581 ( aFlags & EXCLUDE_ZONES ),
582 ( aFlags & IGNORE_NETS ) ? -1 : aItem->GetNetCode() );
583
584 for( const std::shared_ptr<CN_CLUSTER>& cl : clusters )
585 {
586 if( cl->Contains( aItem ) )
587 {
588 for( const CN_ITEM* item : *cl )
589 {
590 if( item->Valid() )
591 rv.push_back( item->Parent() );
592 }
593 }
594 }
595
596 return rv;
597}
598
599
600const std::vector<BOARD_CONNECTED_ITEM*>
601CONNECTIVITY_DATA::GetNetItems( int aNetCode, const std::vector<KICAD_T>& aTypes ) const
602{
603 std::vector<BOARD_CONNECTED_ITEM*> items;
604 items.reserve( 32 );
605
606 std::bitset<MAX_STRUCT_TYPE_ID> type_bits;
607
608 for( KICAD_T scanType : aTypes )
609 {
610 wxASSERT( scanType < MAX_STRUCT_TYPE_ID );
611 type_bits.set( scanType );
612 }
613
614 m_connAlgo->ForEachItem(
615 [&]( CN_ITEM& aItem )
616 {
617 if( aItem.Valid() && ( aItem.Net() == aNetCode ) && type_bits[aItem.Parent()->Type()] )
618 items.push_back( aItem.Parent() );
619 } );
620
621 std::sort( items.begin(), items.end() );
622 items.erase( std::unique( items.begin(), items.end() ), items.end() );
623 return items;
624}
625
626
627const std::vector<PCB_TRACK*>
629{
630 CN_CONNECTIVITY_ALGO::ITEM_MAP_ENTRY& entry = m_connAlgo->ItemEntry( aItem );
631
632 std::set<PCB_TRACK*> tracks;
633 std::vector<PCB_TRACK*> rv;
634
635 for( CN_ITEM* citem : entry.GetItems() )
636 {
637 for( CN_ITEM* connected : citem->ConnectedItems() )
638 {
639 if( connected->Valid() &&
640 ( connected->Parent()->Type() == PCB_TRACE_T ||
641 connected->Parent()->Type() == PCB_VIA_T ||
642 connected->Parent()->Type() == PCB_ARC_T ) )
643 {
644 tracks.insert( static_cast<PCB_TRACK*> ( connected->Parent() ) );
645 }
646 }
647 }
648
649 std::copy( tracks.begin(), tracks.end(), std::back_inserter( rv ) );
650 return rv;
651}
652
653
654void CONNECTIVITY_DATA::GetConnectedPads( const BOARD_CONNECTED_ITEM* aItem, std::set<PAD*>* pads ) const
655{
656 for( CN_ITEM* citem : m_connAlgo->ItemEntry( aItem ).GetItems() )
657 {
658 for( CN_ITEM* connected : citem->ConnectedItems() )
659 {
660 if( connected->Valid() && connected->Parent()->Type() == PCB_PAD_T )
661 pads->insert( static_cast<PAD*> ( connected->Parent() ) );
662 }
663 }
664}
665
666
667const std::vector<PAD*> CONNECTIVITY_DATA::GetConnectedPads( const BOARD_CONNECTED_ITEM* aItem )
668const
669{
670 std::set<PAD*> pads;
671 std::vector<PAD*> rv;
672
673 GetConnectedPads( aItem, &pads );
674
675 std::copy( pads.begin(), pads.end(), std::back_inserter( rv ) );
676 return rv;
677}
678
679
680void CONNECTIVITY_DATA::GetConnectedPadsAndVias( const BOARD_CONNECTED_ITEM* aItem, std::vector<PAD*>* pads,
681 std::vector<PCB_VIA*>* vias )
682{
683 for( CN_ITEM* citem : m_connAlgo->ItemEntry( aItem ).GetItems() )
684 {
685 for( CN_ITEM* connected : citem->ConnectedItems() )
686 {
687 if( connected->Valid() )
688 {
689 BOARD_CONNECTED_ITEM* parent = connected->Parent();
690
691 if( parent->Type() == PCB_PAD_T )
692 pads->push_back( static_cast<PAD*>( parent ) );
693 else if( parent->Type() == PCB_VIA_T )
694 vias->push_back( static_cast<PCB_VIA*>( parent ) );
695 }
696 }
697 }
698}
699
700
702 std::vector<std::set<const BOARD_ITEM*>>* aIslands )
703{
704 aIslands->clear();
705
706 for( CN_ITEM* citem : m_connAlgo->ItemEntry( aZone ).GetItems() )
707 {
708 CN_ZONE_LAYER* island = dynamic_cast<CN_ZONE_LAYER*>( citem );
709
710 if( !island || !island->Valid() || island->GetLayer() != aLayer )
711 continue;
712
713 std::set<const BOARD_ITEM*>& connected = aIslands->emplace_back();
714
715 for( CN_ITEM* other : island->ConnectedItems() )
716 {
717 if( other->Valid() )
718 connected.insert( other->Parent() );
719 }
720 }
721}
722
723
724unsigned int CONNECTIVITY_DATA::GetNodeCount( int aNet ) const
725{
726 int sum = 0;
727
728 if( aNet < 0 ) // Node count for all nets
729 {
730 for( const RN_NET* net : m_nets )
731 sum += net->GetNodeCount();
732 }
733 else if( aNet < (int) m_nets.size() )
734 {
735 sum = m_nets[aNet]->GetNodeCount();
736 }
737
738 return sum;
739}
740
741
742unsigned int CONNECTIVITY_DATA::GetPadCount( int aNet ) const
743{
744 int n = 0;
745
746 for( CN_ITEM* pad : m_connAlgo->ItemList() )
747 {
748 if( !pad->Valid() || pad->Parent()->Type() != PCB_PAD_T)
749 continue;
750
751 PAD* dpad = static_cast<PAD*>( pad->Parent() );
752
753 if( aNet < 0 || aNet == dpad->GetNetCode() )
754 n++;
755 }
756
757 return n;
758}
759
760
761void CONNECTIVITY_DATA::RunOnUnconnectedEdges( std::function<bool( CN_EDGE& )> aFunc )
762{
763 for( RN_NET* rnNet : m_nets )
764 {
765 if( rnNet )
766 {
767 for( CN_EDGE& edge : rnNet->GetEdges() )
768 {
769 if( !aFunc( edge ) )
770 return;
771 }
772 }
773 }
774}
775
776
777static int getMinDist( BOARD_CONNECTED_ITEM* aItem, const VECTOR2I& aPoint )
778{
779 switch( aItem->Type() )
780 {
781 case PCB_TRACE_T:
782 case PCB_ARC_T:
783 {
784 PCB_TRACK* track = static_cast<PCB_TRACK*>( aItem );
785
786 return std::min( track->GetStart().Distance(aPoint ), track->GetEnd().Distance( aPoint ) );
787 }
788
789 default:
790 return aItem->GetPosition().Distance( aPoint );
791 }
792}
793
794
795bool CONNECTIVITY_DATA::TestTrackEndpointDangling( PCB_TRACK* aTrack, bool aIgnoreTracksInPads,
796 VECTOR2I* aPos ) const
797{
798 const std::list<CN_ITEM*>& items = GetConnectivityAlgo()->ItemEntry( aTrack ).GetItems();
799
800 // Not in the connectivity system. This is a bug!
801 if( items.empty() )
802 {
803 wxFAIL_MSG( wxT( "track not in connectivity system" ) );
804 return false;
805 }
806
807 CN_ITEM* citem = items.front();
808
809 if( !citem->Valid() )
810 return false;
811
812 if( aTrack->Type() == PCB_TRACE_T || aTrack->Type() == PCB_ARC_T )
813 {
814 // Test if a segment is connected on each end.
815 //
816 // NB: be wary of short segments which can be connected to the *same* other item on
817 // each end. If that's their only connection then they're still dangling.
818
819 PCB_LAYER_ID layer = aTrack->GetLayer();
820 int accuracy = KiROUND( aTrack->GetWidth() / 2.0 );
821 int start_count = 0;
822 int end_count = 0;
823
824 for( CN_ITEM* connected : citem->ConnectedItems() )
825 {
826 BOARD_CONNECTED_ITEM* item = connected->Parent();
827 ZONE* zone = dynamic_cast<ZONE*>( item );
828 DRC_RTREE* rtree = nullptr;
829 bool hitStart = false;
830 bool hitEnd = false;
831
832 if( item->GetFlags() & IS_DELETED )
833 continue;
834
835 if( zone )
836 rtree = zone->GetBoard()->m_CopperZoneRTreeCache[ zone ].get();
837
838 if( rtree )
839 {
840 SHAPE_CIRCLE start( aTrack->GetStart(), accuracy );
841 SHAPE_CIRCLE end( aTrack->GetEnd(), accuracy );
842
843 hitStart = rtree->QueryColliding( start.BBox(), &start, layer );
844 hitEnd = rtree->QueryColliding( end.BBox(), &end, layer );
845 }
846 else
847 {
848 std::shared_ptr<SHAPE> shape = item->GetEffectiveShape( layer );
849
850 hitStart = shape->Collide( aTrack->GetStart(), accuracy );
851 hitEnd = shape->Collide( aTrack->GetEnd(), accuracy );
852 }
853
854 if( hitStart && hitEnd )
855 {
856 if( zone )
857 {
858 // Both start and end in a zone: track may be redundant, but it's not dangling
859 return false;
860 }
861 else if( item->Type() == PCB_PAD_T || item->Type() == PCB_VIA_T )
862 {
863 // Both start and end are under a pad: see what the caller wants us to do
864 if( aIgnoreTracksInPads )
865 return false;
866 }
867
868 if( getMinDist( item, aTrack->GetStart() ) < getMinDist( item, aTrack->GetEnd() ) )
869 start_count++;
870 else
871 end_count++;
872 }
873 else if( hitStart )
874 {
875 start_count++;
876 }
877 else if( hitEnd )
878 {
879 end_count++;
880 }
881
882 if( start_count > 0 && end_count > 0 )
883 return false;
884 }
885
886 if( aPos )
887 *aPos = (start_count == 0 ) ? aTrack->GetStart() : aTrack->GetEnd();
888
889 return true;
890 }
891 else if( aTrack->Type() == PCB_VIA_T )
892 {
893 // Test if a via is only connected on one layer
894
895 const std::vector<CN_ITEM*>& connected = citem->ConnectedItems();
896
897 if( connected.empty() )
898 {
899 // No connections AND no-net is not an error
900 if( aTrack->GetNetCode() <= 0 )
901 return false;
902
903 if( aPos )
904 *aPos = aTrack->GetPosition();
905
906 return true;
907 }
908
909 // Here, we check if the via is connected only to items on a single layer
910 int first_layer = UNDEFINED_LAYER;
911
912 for( CN_ITEM* item : connected )
913 {
914 if( item->Parent()->GetFlags() & IS_DELETED )
915 continue;
916
917 if( first_layer == UNDEFINED_LAYER )
918 first_layer = item->Layer();
919 else if( item->Layer() != first_layer )
920 return false;
921 }
922
923 if( aPos )
924 *aPos = aTrack->GetPosition();
925
926 return true;
927 }
928 else
929 {
930 wxFAIL_MSG( wxT( "CONNECTIVITY_DATA::TestTrackEndpointDangling: unknown track type" ) );
931 }
932
933 return false;
934}
935
936
937const std::vector<BOARD_CONNECTED_ITEM*>
939 const std::vector<KICAD_T>& aTypes, const int& aMaxError ) const
940{
941 CN_CONNECTIVITY_ALGO::ITEM_MAP_ENTRY& entry = m_connAlgo->ItemEntry( aItem );
942 std::vector<BOARD_CONNECTED_ITEM*> rv;
943 SEG::ecoord maxError_sq = (SEG::ecoord) aMaxError * aMaxError;
944
945 for( CN_ITEM* cnItem : entry.GetItems() )
946 {
947 for( CN_ITEM* connected : cnItem->ConnectedItems() )
948 {
949 for( const std::shared_ptr<CN_ANCHOR>& anchor : connected->Anchors() )
950 {
951 if( ( anchor->Pos() - aAnchor ).SquaredEuclideanNorm() <= maxError_sq )
952 {
953 for( KICAD_T type : aTypes )
954 {
955 if( connected->Valid() && connected->Parent()->Type() == type )
956 {
957 rv.push_back( connected->Parent() );
958 break;
959 }
960 }
961
962 break;
963 }
964 }
965 }
966 }
967
968 return rv;
969}
970
971
973{
974 if ( aNet < 0 || aNet >= (int) m_nets.size() )
975 return nullptr;
976
977 return m_nets[ aNet ];
978}
979
980
982{
983 if ( aItem->Type() == PCB_FOOTPRINT_T)
984 {
985 for( PAD* pad : static_cast<FOOTPRINT*>( aItem )->Pads() )
986 m_connAlgo->MarkNetAsDirty( pad->GetNetCode() );
987 }
988
989 if (aItem->IsConnected() )
990 m_connAlgo->MarkNetAsDirty( static_cast<BOARD_CONNECTED_ITEM*>( aItem )->GetNetCode() );
991}
992
993
995{
996 m_connAlgo->RemoveInvalidRefs();
997
998 for( RN_NET* rnNet : m_nets )
999 rnNet->RemoveInvalidRefs();
1000}
1001
1002
1004{
1005 m_progressReporter = aReporter;
1006 m_connAlgo->SetProgressReporter( m_progressReporter );
1007}
1008
1009
1010const std::vector<CN_EDGE>
1011CONNECTIVITY_DATA::GetRatsnestForItems( const std::vector<BOARD_ITEM*>& aItems )
1012{
1013 std::set<int> nets;
1014 std::vector<CN_EDGE> edges;
1015 std::set<BOARD_CONNECTED_ITEM*> item_set;
1016
1017 for( BOARD_ITEM* item : aItems )
1018 {
1019 if( item->Type() == PCB_FOOTPRINT_T )
1020 {
1021 FOOTPRINT* footprint = static_cast<FOOTPRINT*>( item );
1022
1023 for( PAD* pad : footprint->Pads() )
1024 {
1025 nets.insert( pad->GetNetCode() );
1026 item_set.insert( pad );
1027 }
1028 }
1029 else if( item->IsConnected() )
1030 {
1031 BOARD_CONNECTED_ITEM* conn_item = static_cast<BOARD_CONNECTED_ITEM*>( item );
1032
1033 item_set.insert( conn_item );
1034 nets.insert( conn_item->GetNetCode() );
1035 }
1036 }
1037
1038 for( int netcode : nets )
1039 {
1040 RN_NET* net = GetRatsnestForNet( netcode );
1041
1042 if( !net )
1043 continue;
1044
1045 for( const CN_EDGE& edge : net->GetEdges() )
1046 {
1047 std::shared_ptr<const CN_ANCHOR> srcNode = edge.GetSourceNode();
1048 std::shared_ptr<const CN_ANCHOR> dstNode = edge.GetTargetNode();
1049
1050 if( !srcNode || srcNode->Dirty() || !dstNode || dstNode->Dirty() )
1051 continue;
1052
1053 BOARD_CONNECTED_ITEM* srcParent = srcNode->Parent();
1054 BOARD_CONNECTED_ITEM* dstParent = dstNode->Parent();
1055
1056 bool srcFound = ( item_set.find( srcParent ) != item_set.end() );
1057 bool dstFound = ( item_set.find( dstParent ) != item_set.end() );
1058
1059 if ( srcFound && dstFound )
1060 edges.push_back( edge );
1061 }
1062 }
1063
1064 return edges;
1065}
1066
1067
1068const std::vector<CN_EDGE> CONNECTIVITY_DATA::GetRatsnestForPad( const PAD* aPad )
1069{
1070 std::vector<CN_EDGE> edges;
1071 RN_NET* net = GetRatsnestForNet( aPad->GetNetCode() );
1072
1073 if( !net )
1074 return edges;
1075
1076 for( const CN_EDGE& edge : net->GetEdges() )
1077 {
1078 if( !edge.GetSourceNode() || edge.GetSourceNode()->Dirty() )
1079 continue;
1080
1081 if( !edge.GetTargetNode() || edge.GetTargetNode()->Dirty() )
1082 continue;
1083
1084 if( edge.GetSourceNode()->Parent() == aPad || edge.GetTargetNode()->Parent() == aPad )
1085 edges.push_back( edge );
1086 }
1087
1088 return edges;
1089}
1090
1091
1092const std::vector<CN_EDGE> CONNECTIVITY_DATA::GetRatsnestForComponent( FOOTPRINT* aComponent,
1093 bool aSkipInternalConnections )
1094{
1095 std::set<int> nets;
1096 std::set<const PAD*> pads;
1097 std::vector<CN_EDGE> edges;
1098
1099 for( PAD* pad : aComponent->Pads() )
1100 {
1101 nets.insert( pad->GetNetCode() );
1102 pads.insert( pad );
1103 }
1104
1105 for( int netcode : nets )
1106 {
1107 RN_NET* net = GetRatsnestForNet( netcode );
1108
1109 if( !net )
1110 continue;
1111
1112 for( const CN_EDGE& edge : net->GetEdges() )
1113 {
1114 const std::shared_ptr<const CN_ANCHOR>& srcNode = edge.GetSourceNode();
1115 const std::shared_ptr<const CN_ANCHOR>& dstNode = edge.GetTargetNode();
1116
1117 if( !srcNode || srcNode->Dirty() || !dstNode || dstNode->Dirty() )
1118 continue;
1119
1120 const PAD* srcParent = static_cast<const PAD*>( srcNode->Parent() );
1121 const PAD* dstParent = static_cast<const PAD*>( dstNode->Parent() );
1122
1123 bool srcFound = ( pads.find(srcParent) != pads.end() );
1124 bool dstFound = ( pads.find(dstParent) != pads.end() );
1125
1126 if ( srcFound && dstFound && !aSkipInternalConnections )
1127 edges.push_back( edge );
1128 else if ( srcFound || dstFound )
1129 edges.push_back( edge );
1130 }
1131 }
1132
1133 return edges;
1134}
1135
1136
1138{
1139 if( std::shared_ptr<NET_SETTINGS> netSettings = m_netSettings.lock() )
1140 return netSettings.get();
1141 else
1142 return nullptr;
1143}
@ ERROR_INSIDE
constexpr BOX2I KiROUND(const BOX2D &aBoxD)
Definition box2.h:995
A base class derived from BOARD_ITEM for items that can be connected and have a net,...
PCB_LAYER_ID GetLayer() const override
Return the primary layer this item is on.
std::shared_ptr< NET_SETTINGS > m_NetSettings
A base class for any item which can be embedded within the BOARD container class, and therefore insta...
Definition board_item.h:84
virtual bool IsConnected() const
Returns information if the object is derived from BOARD_CONNECTED_ITEM.
Definition board_item.h:172
virtual const BOARD * GetBoard() const
Return the BOARD in which this BOARD_ITEM resides, or NULL if none.
FOOTPRINT * GetParentFootprint() const
virtual std::shared_ptr< SHAPE > GetEffectiveShape(PCB_LAYER_ID aLayer=UNDEFINED_LAYER, FLASHING aFlash=FLASHING::DEFAULT, DRC_CONSTRAINT_T aUsage=NULL_CONSTRAINT) const
Some pad shapes can be complex (rounded/chamfered rectangle), even without considering custom shapes.
Information pertinent to a Pcbnew printed circuit board.
Definition board.h:409
const NETINFO_LIST & GetNetInfo() const
Definition board.h:1207
std::unordered_map< ZONE *, std::unique_ptr< DRC_RTREE > > m_CopperZoneRTreeCache
Definition board.h:1832
BOARD_DESIGN_SETTINGS & GetDesignSettings() const
Definition board.cpp:1299
void CacheTriangulation(PROGRESS_REPORTER *aReporter=nullptr, const std::vector< ZONE * > &aZones={})
Definition board.cpp:1357
CN_ANCHOR represents a physical location that can be connected: a pad or a track/arc/via endpoint.
const std::list< CN_ITEM * > & GetItems() const
ITEM_MAP_ENTRY & ItemEntry(const BOARD_CONNECTED_ITEM *aItem)
CN_EDGE represents a point-to-point connection, whether realized or unrealized (ie: tracks etc.
CN_ITEM represents a BOARD_CONNETED_ITEM in the connectivity system (ie: a pad, track/arc/via,...
const std::vector< CN_ITEM * > & ConnectedItems() const
int Net() const
bool Valid() const
BOARD_CONNECTED_ITEM * Parent() const
PCB_LAYER_ID GetLayer() const
int SubpolyIndex() const
void FillIsolatedIslandsMap(std::map< ZONE *, std::map< PCB_LAYER_ID, ISOLATED_ISLANDS > > &aMap, bool aConnectivityAlreadyRebuilt=false)
Fill the isolate islands list for each layer of each zone.
void RecalculateRatsnest(BOARD_COMMIT *aCommit=nullptr)
Function RecalculateRatsnest() Updates the ratsnest for the board.
void ClearLocalRatsnest()
Function ClearLocalRatsnest() Erases the temporary, selection-based ratsnest (i.e.
PROGRESS_REPORTER * m_progressReporter
unsigned int GetPadCount(int aNet=-1) const
void MarkItemNetAsDirty(BOARD_ITEM *aItem)
std::weak_ptr< NET_SETTINGS > m_netSettings
Used to get netclass data when drawing ratsnests.
const std::vector< BOARD_CONNECTED_ITEM * > GetConnectedItems(const BOARD_CONNECTED_ITEM *aItem, int aFlags=0) const
void PropagateNets(BOARD_COMMIT *aCommit=nullptr)
Propagates the net codes from the source pads to the tracks/vias.
void RunOnUnconnectedEdges(std::function< bool(CN_EDGE &)> aFunc)
std::vector< RN_DYNAMIC_LINE > m_dynamicRatsnest
bool m_skipRatsnestUpdate
Used to suppress ratsnest calculations on dynamic ratsnests.
const std::vector< CN_EDGE > GetRatsnestForPad(const PAD *aPad)
RN_NET * GetRatsnestForNet(int aNet)
Function GetRatsnestForNet() Returns the ratsnest, expressed as a set of graph edges for a given net.
const std::vector< BOARD_CONNECTED_ITEM * > GetConnectedItemsAtAnchor(const BOARD_CONNECTED_ITEM *aItem, const VECTOR2I &aAnchor, const std::vector< KICAD_T > &aTypes, const int &aMaxError=0) const
Function GetConnectedItemsAtAnchor() Returns a list of items connected to a source item aItem at posi...
void ClearRatsnest()
Function Clear() Erases the connectivity database.
bool Remove(BOARD_ITEM *aItem)
Function Remove() Removes an item from the connectivity data.
void GetConnectedPadsAndVias(const BOARD_CONNECTED_ITEM *aItem, std::vector< PAD * > *pads, std::vector< PCB_VIA * > *vias)
const NET_SETTINGS * GetNetSettings() const
void ComputeLocalRatsnest(const std::vector< BOARD_ITEM * > &aItems, const CONNECTIVITY_DATA *aDynamicData, VECTOR2I aInternalOffset={ 0, 0 })
Function ComputeLocalRatsnest() Calculates the temporary (usually selection-based) ratsnest for the s...
bool TestTrackEndpointDangling(PCB_TRACK *aTrack, bool aIgnoreTracksInPads, VECTOR2I *aPos=nullptr) const
unsigned int GetNodeCount(int aNet=-1) const
void SetProgressReporter(PROGRESS_REPORTER *aReporter)
void BlockRatsnestItems(const std::vector< BOARD_ITEM * > &aItems)
bool IsConnectedOnLayer(const BOARD_CONNECTED_ITEM *aItem, int aLayer, const std::initializer_list< KICAD_T > &aTypes={}) const
const std::vector< PCB_TRACK * > GetConnectedTracks(const BOARD_CONNECTED_ITEM *aItem) const
const std::vector< CN_EDGE > GetRatsnestForComponent(FOOTPRINT *aComponent, bool aSkipInternalConnections=false)
const std::vector< BOARD_CONNECTED_ITEM * > GetNetItems(int aNetCode, const std::vector< KICAD_T > &aTypes) const
Function GetNetItems() Returns the list of items that belong to a certain net.
bool Add(BOARD_ITEM *aItem)
Function Add() Adds an item to the connectivity data.
std::shared_ptr< CN_CONNECTIVITY_ALGO > m_connAlgo
bool Build(BOARD *aBoard, PROGRESS_REPORTER *aReporter=nullptr)
Function Build() Builds the connectivity database for the board aBoard.
std::shared_ptr< FROM_TO_CACHE > m_fromToCache
const std::vector< PAD * > GetConnectedPads(const BOARD_CONNECTED_ITEM *aItem) const
unsigned int GetUnconnectedCount(bool aVisibileOnly) const
std::map< int, wxString > m_netcodeMap
Used to map netcode to net name.
void internalRecalculateRatsnest(BOARD_COMMIT *aCommit=nullptr)
Updates the ratsnest for the board without locking the connectivity mutex.
void RefreshNetcodeMap(BOARD *aBoard)
Refresh the map of netcodes to net names.
void HideLocalRatsnest()
Hides the temporary, selection-based ratsnest lines.
const std::vector< CN_EDGE > GetRatsnestForItems(const std::vector< BOARD_ITEM * > &aItems)
void addRatsnestCluster(const std::shared_ptr< CN_CLUSTER > &aCluster)
std::vector< RN_NET * > m_nets
bool Update(BOARD_ITEM *aItem)
Function Update() Updates the connectivity data for an item.
void Move(const VECTOR2I &aDelta)
Moves the connectivity list anchors.
int GetNetCount() const
Function GetNetCount() Returns the total number of nets in the connectivity database.
void GetZoneIslandConnections(const ZONE *aZone, PCB_LAYER_ID aLayer, std::vector< std::set< const BOARD_ITEM * > > *aIslands)
Return, for each filled island of aZone on aLayer, the items that island touches.
std::shared_ptr< CN_CONNECTIVITY_ALGO > GetConnectivityAlgo() const
Implement an R-tree for fast spatial and layer indexing of connectable items.
Definition drc_rtree.h:45
int QueryColliding(BOARD_ITEM *aRefItem, PCB_LAYER_ID aRefLayer, PCB_LAYER_ID aTargetLayer, std::function< bool(BOARD_ITEM *)> aFilter=nullptr, std::function< bool(BOARD_ITEM *)> aVisitor=nullptr, int aClearance=0) const
This is a fast test which essentially does bounding-box overlap given a worst-case clearance.
Definition drc_rtree.h:277
virtual VECTOR2I GetPosition() const
Definition eda_item.h:348
KICAD_T Type() const
Returns the type of object.
Definition eda_item.h:110
EDA_ITEM_FLAGS GetFlags() const
Definition eda_item.h:167
std::deque< PAD * > & Pads()
Definition footprint.h:404
Handle the data for a net.
Definition netinfo.h:50
NET_SETTINGS stores various net-related settings in a project context.
UNCONNECTED_LAYER_MODE UnconnectedLayerMode() const
Definition padstack.h:378
Definition pad.h:61
const PADSTACK & Padstack() const
Definition pad.h:329
const VECTOR2I & GetStart() const
Definition pcb_track.h:93
VECTOR2I GetPosition() const override
Definition pcb_track.h:83
const VECTOR2I & GetEnd() const
Definition pcb_track.h:90
virtual int GetWidth() const
Definition pcb_track.h:87
A small class to help profiling.
Definition profile.h:46
void Show(std::ostream &aStream=std::cerr)
Print the elapsed time (in a suitable unit) to a stream.
Definition profile.h:103
A progress reporter interface for use in multi-threaded environments.
virtual bool KeepRefreshing(bool aWait=false)=0
Update the UI (if any).
virtual void Report(const wxString &aMessage)=0
Display aMessage in the progress bar dialog.
virtual void SetCurrentProgress(double aProgress)=0
Set the progress value to aProgress (0..1).
Describe ratsnest for a single net.
unsigned int GetNodeCount() const
const std::vector< CN_EDGE > & GetEdges() const
bool NearestBicoloredPair(RN_NET *aOtherNet, VECTOR2I &aPos1, VECTOR2I &aPos2) const
void AddCluster(std::shared_ptr< CN_CLUSTER > aCluster)
VECTOR2I::extended_type ecoord
Definition seg.h:40
const BOX2I BBox(int aClearance=0) const override
Compute a bounding box of the shape, with a margin of aClearance a collision.
Represent a polyline containing arcs as well as line segments: A chain of connected line and/or arc s...
bool PointInside(const VECTOR2I &aPt, int aAccuracy=0, bool aUseBBoxCache=false) const override
Check if point aP lies inside a closed shape.
const std::vector< VECTOR2I > & CPoints() const
Represent a set of closed polygons.
const SHAPE_LINE_CHAIN & COutline(int aIndex) const
double Distance(const VECTOR2< extended_type > &aVector) const
Compute the distance between two vectors.
Definition vector2d.h:549
Handle a list of polygons defining a copper zone.
Definition zone.h:70
bool IsFilled() const
Definition zone.h:306
SHAPE_POLY_SET * GetFill(PCB_LAYER_ID aLayer)
Definition zone.h:699
static int getMinDist(BOARD_CONNECTED_ITEM *aItem, const VECTOR2I &aPoint)
#define EXCLUDE_ZONES
#define IGNORE_NETS
Function GetConnectedItems() Returns a list of items connected to a source item aItem.
#define _(s)
#define IS_DELETED
size_t CopperLayerToOrdinal(PCB_LAYER_ID aLayer)
Converts KiCad copper layer enum to an ordinal between the front and back layers.
Definition layer_ids.h:945
PCB_LAYER_ID
A quick note on layer IDs:
Definition layer_ids.h:56
@ UNDEFINED_LAYER
Definition layer_ids.h:57
PCB_LAYER_ID ToLAYER_ID(int aLayer)
Definition lset.cpp:750
bool contains(const _Container &__container, _Value __value)
Returns true if the container contains the given value.
Definition kicad_algo.h:96
UNCONNECTED_LAYER_MODE
Definition padstack.h:127
Class that computes missing connections on a PCB.
VECTOR2I end
const int accuracy
thread_pool & GetKiCadThreadPool()
Get a reference to the current thread pool.
static thread_pool * tp
BS::priority_thread_pool thread_pool
Definition thread_pool.h:27
KICAD_T
The set of class identification values stored in EDA_ITEM::m_structType.
Definition typeinfo.h:70
@ PCB_VIA_T
class PCB_VIA, a via (like a track segment on a copper layer)
Definition typeinfo.h:89
@ MAX_STRUCT_TYPE_ID
Definition typeinfo.h:243
@ PCB_FOOTPRINT_T
class FOOTPRINT, a footprint
Definition typeinfo.h:78
@ PCB_PAD_T
class PAD, a pad in a footprint
Definition typeinfo.h:79
@ PCB_ARC_T
class PCB_ARC, an arc track segment on a copper layer
Definition typeinfo.h:90
@ PCB_TRACE_T
class PCB_TRACK, a track segment (segment on a copper layer)
Definition typeinfo.h:88
VECTOR2< int32_t > VECTOR2I
Definition vector2d.h:683