KiCad PCB EDA Suite
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 (C) 2018-2023 KiCad Developers, see AUTHORS.txt for contributors.
6 * @author Tomasz Wlostowski <[email protected]>
7 *
8 * This program is free software; you can redistribute it and/or
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, you may find one here:
20 * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
21 * or you may search the http://www.gnu.org website for the version 2 license,
22 * or you may write to the Free Software Foundation, Inc.,
23 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
24 */
25
26#ifdef PROFILE
27#include <profile.h>
28#endif
29
30#include <algorithm>
31#include <future>
32#include <initializer_list>
33
42#include <progress_reporter.h>
43#include <thread_pool.h>
44#include <trigo.h>
45#include <drc/drc_rtree.h>
46
48{
50 m_progressReporter = nullptr;
51 m_fromToCache.reset( new FROM_TO_CACHE );
52}
53
54
55CONNECTIVITY_DATA::CONNECTIVITY_DATA( const std::vector<BOARD_ITEM*>& aItems, bool aSkipRatsnest )
56 : m_skipRatsnest( aSkipRatsnest )
57{
58 Build( aItems );
59 m_progressReporter = nullptr;
60 m_fromToCache.reset( new FROM_TO_CACHE );
61}
62
63
65{
66 for( RN_NET* net : m_nets )
67 delete net;
68
69 m_nets.clear();
70}
71
72
74{
75 m_connAlgo->Add( aItem );
76 return true;
77}
78
79
81{
82 m_connAlgo->Remove( aItem );
83 return true;
84}
85
86
88{
89 m_connAlgo->Remove( aItem );
90 m_connAlgo->Add( aItem );
91 return true;
92}
93
94
96{
97 aBoard->CacheTriangulation( aReporter );
98
99 std::unique_lock<KISPINLOCK> lock( m_lock, std::try_to_lock );
100
101 if( !lock )
102 return false;
103
104 if( aReporter )
105 {
106 aReporter->Report( _( "Updating nets..." ) );
107 aReporter->KeepRefreshing( false );
108 }
109
110 std::shared_ptr<NET_SETTINGS>& netSettings = aBoard->GetDesignSettings().m_NetSettings;
111
112 m_connAlgo.reset( new CN_CONNECTIVITY_ALGO );
113 m_connAlgo->Build( aBoard, aReporter );
114
115 m_netclassMap.clear();
116
117 for( NETINFO_ITEM* net : aBoard->GetNetInfo() )
118 {
119 net->SetNetClass( netSettings->GetEffectiveNetClass( net->GetNetname() ) );
120
121 if( net->GetNetClass()->GetName() != NETCLASS::Default )
122 m_netclassMap[ net->GetNetCode() ] = net->GetNetClass()->GetName();
123 }
124
125 if( aReporter )
126 {
127 aReporter->SetCurrentProgress( 0.75 );
128 aReporter->KeepRefreshing( false );
129 }
130
132
133 if( aReporter )
134 {
135 aReporter->SetCurrentProgress( 1.0 );
136 aReporter->KeepRefreshing( false );
137 }
138
139 return true;
140}
141
142
143void CONNECTIVITY_DATA::Build( const std::vector<BOARD_ITEM*>& aItems )
144{
145 std::unique_lock<KISPINLOCK> lock( m_lock, std::try_to_lock );
146
147 if( !lock )
148 return;
149
150 m_connAlgo.reset( new CN_CONNECTIVITY_ALGO );
151 m_connAlgo->LocalBuild( aItems );
152
154}
155
156
158{
159 m_connAlgo->ForEachAnchor( [&aDelta]( CN_ANCHOR& anchor )
160 {
161 anchor.Move( aDelta );
162 } );
163}
164
165
167{
168#ifdef PROFILE
169 PROF_TIMER rnUpdate( "update-ratsnest" );
170#endif
171
172 std::vector<RN_NET*> dirty_nets;
173
174 // Start with net 1 as net 0 is reserved for not-connected
175 // Nets without nodes are also ignored
176 std::copy_if( m_nets.begin() + 1, m_nets.end(), std::back_inserter( dirty_nets ),
177 [] ( RN_NET* aNet )
178 {
179 return aNet->IsDirty() && aNet->GetNodeCount() > 0;
180 } );
181
183
184 tp.push_loop( dirty_nets.size(),
185 [&]( const int a, const int b)
186 {
187 for( int ii = a; ii < b; ++ii )
188 dirty_nets[ii]->UpdateNet();
189 } );
190 tp.wait_for_tasks();
191
192#ifdef PROFILE
193 rnUpdate.Show();
194#endif
195}
196
197
198void CONNECTIVITY_DATA::addRatsnestCluster( const std::shared_ptr<CN_CLUSTER>& aCluster )
199{
200 RN_NET* rnNet = m_nets[ aCluster->OriginNet() ];
201
202 rnNet->AddCluster( aCluster );
203}
204
205
207{
208
209 // We can take over the lock here if called in the same thread
210 // This is to prevent redraw during a RecalculateRatsnets process
211 std::unique_lock<KISPINLOCK> lock( m_lock );
212
214
215}
216
218{
219 m_connAlgo->PropagateNets( aCommit );
220
221 int lastNet = m_connAlgo->NetCount();
222
223 if( lastNet >= (int) m_nets.size() )
224 {
225 unsigned int prevSize = m_nets.size();
226 m_nets.resize( lastNet + 1 );
227
228 for( unsigned int i = prevSize; i < m_nets.size(); i++ )
229 m_nets[i] = new RN_NET;
230 }
231 else
232 {
233 for( size_t ii = lastNet; ii < m_nets.size(); ++ii )
234 m_nets[ii]->Clear();
235 }
236
237 const std::vector<std::shared_ptr<CN_CLUSTER>>& clusters = m_connAlgo->GetClusters();
238
239 int dirtyNets = 0;
240
241 for( int net = 0; net < lastNet; net++ )
242 {
243 if( m_connAlgo->IsNetDirty( net ) )
244 {
245 m_nets[net]->Clear();
246 dirtyNets++;
247 }
248 }
249
250 for( const std::shared_ptr<CN_CLUSTER>& c : clusters )
251 {
252 int net = c->OriginNet();
253
254 // Don't add intentionally-kept zone islands to the ratsnest
255 if( c->IsOrphaned() && c->Size() == 1 )
256 {
257 if( dynamic_cast<CN_ZONE_LAYER*>( *c->begin() ) )
258 continue;
259 }
260
261 if( m_connAlgo->IsNetDirty( net ) )
263 }
264
265 m_connAlgo->ClearDirtyFlags();
266
267 if( !m_skipRatsnest )
269}
270
271
272void CONNECTIVITY_DATA::BlockRatsnestItems( const std::vector<BOARD_ITEM*>& aItems )
273{
274 std::vector<BOARD_CONNECTED_ITEM*> citems;
275
276 for( BOARD_ITEM* item : aItems )
277 {
278 if( item->Type() == PCB_FOOTPRINT_T )
279 {
280 for( PAD* pad : static_cast<FOOTPRINT*>(item)->Pads() )
281 citems.push_back( pad );
282 }
283 else
284 {
285 if( BOARD_CONNECTED_ITEM* citem = dynamic_cast<BOARD_CONNECTED_ITEM*>( item ) )
286 citems.push_back( citem );
287 }
288 }
289
290 for( const BOARD_CONNECTED_ITEM* item : citems )
291 {
292 if ( m_connAlgo->ItemExists( item ) )
293 {
294 CN_CONNECTIVITY_ALGO::ITEM_MAP_ENTRY& entry = m_connAlgo->ItemEntry( item );
295
296 for( CN_ITEM* cnItem : entry.GetItems() )
297 {
298 for( const std::shared_ptr<CN_ANCHOR>& anchor : cnItem->Anchors() )
299 anchor->SetNoLine( true );
300 }
301 }
302 }
303}
304
305
307{
308 return m_connAlgo->NetCount();
309}
310
311
312void CONNECTIVITY_DATA::FindIsolatedCopperIslands( ZONE* aZone, std::vector<int>& aIslands )
313{
314 // TODO(JE) ZONES
315#if 0
316 m_connAlgo->FindIsolatedCopperIslands( aZone, aIslands );
317#endif
318}
319
320void CONNECTIVITY_DATA::FindIsolatedCopperIslands( std::vector<CN_ZONE_ISOLATED_ISLAND_LIST>& aZones,
321 bool aConnectivityAlreadyRebuilt )
322{
323 m_connAlgo->FindIsolatedCopperIslands( aZones, aConnectivityAlreadyRebuilt );
324}
325
326
327void CONNECTIVITY_DATA::ComputeLocalRatsnest( const std::vector<BOARD_ITEM*>& aItems,
328 const CONNECTIVITY_DATA* aDynamicData,
329 VECTOR2I aInternalOffset )
330{
331 if( !aDynamicData )
332 return;
333
334 m_dynamicRatsnest.clear();
335 std::mutex dynamic_ratsnest_mutex;
336
337 // This gets connections between the stationary board and the
338 // moving selection
339
340 auto update_lambda = [&]( int nc )
341 {
342 RN_NET* dynamicNet = aDynamicData->m_nets[nc];
343 RN_NET* staticNet = m_nets[nc];
344
348 if( dynamicNet->GetNodeCount() != 0
349 && dynamicNet->GetNodeCount() != staticNet->GetNodeCount() )
350 {
351 VECTOR2I pos1, pos2;
352
353 if( staticNet->NearestBicoloredPair( dynamicNet, pos1, pos2 ) )
354 {
356 l.a = pos1;
357 l.b = pos2;
358 l.netCode = nc;
359
360 std::lock_guard<std::mutex> lock( dynamic_ratsnest_mutex );
361 m_dynamicRatsnest.push_back( l );
362 }
363 }
364 };
365
367 size_t num_nets = std::min( m_nets.size(), aDynamicData->m_nets.size() );
368
369 tp.push_loop( 1, num_nets,
370 [&]( const int a, const int b)
371 {
372 for( int ii = a; ii < b; ++ii )
373 update_lambda( ii );
374 });
375 tp.wait_for_tasks();
376
377 // This gets the ratsnest for internal connections in the moving set
378 const std::vector<CN_EDGE>& edges = GetRatsnestForItems( aItems );
379
380 for( const CN_EDGE& edge : edges )
381 {
382 const std::shared_ptr<const CN_ANCHOR>& nodeA = edge.GetSourceNode();
383 const std::shared_ptr<const CN_ANCHOR>& nodeB = edge.GetTargetNode();
385
386 // Use the parents' positions
387 l.a = nodeA->Parent()->GetPosition() + aInternalOffset;
388 l.b = nodeB->Parent()->GetPosition() + aInternalOffset;
389 l.netCode = 0;
390 m_dynamicRatsnest.push_back( l );
391 }
392}
393
394
396{
397 m_connAlgo->ForEachAnchor( []( CN_ANCHOR& anchor )
398 {
399 anchor.SetNoLine( false );
400 } );
402}
403
404
406{
407 m_dynamicRatsnest.clear();
408}
409
410
412{
413 m_connAlgo->PropagateNets( aCommit );
414}
415
416
418 const std::initializer_list<KICAD_T>& aTypes ) const
419{
420 CN_CONNECTIVITY_ALGO::ITEM_MAP_ENTRY &entry = m_connAlgo->ItemEntry( aItem );
421
422 auto matchType =
423 [&]( KICAD_T aItemType )
424 {
425 if( aTypes.size() == 0 )
426 return true;
427
428 return alg::contains( aTypes, aItemType);
429 };
430
431 for( CN_ITEM* citem : entry.GetItems() )
432 {
433 for( CN_ITEM* connected : citem->ConnectedItems() )
434 {
435 CN_ZONE_LAYER* zoneLayer = dynamic_cast<CN_ZONE_LAYER*>( connected );
436
437 if( connected->Valid()
438 && connected->Layers().Overlaps( aLayer )
439 && matchType( connected->Parent()->Type() )
440 && connected->Net() == aItem->GetNetCode() )
441 {
442 if( aItem->Type() == PCB_PAD_T && zoneLayer )
443 {
444 const PAD* pad = static_cast<const PAD*>( aItem );
445 ZONE* zone = static_cast<ZONE*>( zoneLayer->Parent() );
446 int islandIdx = zoneLayer->SubpolyIndex();
447
448 if( zone->IsFilled() )
449 {
450 const SHAPE_POLY_SET* zoneFill = zone->GetFill( ToLAYER_ID( aLayer ) );
451 const SHAPE_LINE_CHAIN& padHull = pad->GetEffectivePolygon()->Outline( 0 );
452
453 for( const VECTOR2I& pt : zoneFill->COutline( islandIdx ).CPoints() )
454 {
455 // If the entire island is inside the pad's flashing then the pad
456 // won't actually connect to anything else, so only return true if
457 // part of the island is *outside* the pad's flashing.
458
459 if( !padHull.PointInside( pt ) )
460 return true;
461 }
462 }
463
464 continue;
465 }
466 else if( aItem->Type() == PCB_VIA_T && zoneLayer )
467 {
468 const PCB_VIA* via = static_cast<const PCB_VIA*>( aItem );
469 ZONE* zone = static_cast<ZONE*>( zoneLayer->Parent() );
470 int islandIdx = zoneLayer->SubpolyIndex();
471
472 if( zone->IsFilled() )
473 {
474 const SHAPE_POLY_SET* zoneFill = zone->GetFill( ToLAYER_ID( aLayer ) );
475 SHAPE_CIRCLE viaHull( via->GetCenter(), via->GetWidth() / 2 );
476
477 for( const VECTOR2I& pt : zoneFill->COutline( islandIdx ).CPoints() )
478 {
479 // If the entire island is inside the via's flashing then the via
480 // won't actually connect to anything else, so only return true if
481 // part of the island is *outside* the via's flashing.
482
483 if( !viaHull.SHAPE::Collide( pt ) )
484 return true;
485 }
486 }
487
488 continue;
489 }
490
491 return true;
492 }
493 }
494 }
495
496 return false;
497}
498
499
500unsigned int CONNECTIVITY_DATA::GetUnconnectedCount( bool aVisibleOnly ) const
501{
502 unsigned int unconnected = 0;
503
504 for( RN_NET* net : m_nets )
505 {
506 if( !net )
507 continue;
508
509 for( const CN_EDGE& edge : net->GetEdges() )
510 {
511 if( edge.IsVisible() || !aVisibleOnly )
512 ++unconnected;
513 }
514 }
515
516 return unconnected;
517}
518
519
521{
522 for( RN_NET* net : m_nets )
523 net->Clear();
524}
525
526
527const std::vector<BOARD_CONNECTED_ITEM*>
529 const std::initializer_list<KICAD_T>& aTypes,
530 bool aIgnoreNetcodes ) const
531{
532 std::vector<BOARD_CONNECTED_ITEM*> rv;
534
535 if( aIgnoreNetcodes )
537 else
539
540 const auto clusters = m_connAlgo->SearchClusters( searchMode, aTypes,
541 aIgnoreNetcodes ? -1 : aItem->GetNetCode() );
542
543 for( const std::shared_ptr<CN_CLUSTER>& cl : clusters )
544 {
545 if( cl->Contains( aItem ) )
546 {
547 for( const CN_ITEM* item : *cl )
548 {
549 if( item->Valid() )
550 rv.push_back( item->Parent() );
551 }
552 }
553 }
554
555 return rv;
556}
557
558
559const std::vector<BOARD_CONNECTED_ITEM*>
560CONNECTIVITY_DATA::GetNetItems( int aNetCode, const std::initializer_list<KICAD_T>& aTypes ) const
561{
562 std::vector<BOARD_CONNECTED_ITEM*> items;
563 items.reserve( 32 );
564
565 std::bitset<MAX_STRUCT_TYPE_ID> type_bits;
566
567 for( KICAD_T scanType : aTypes )
568 {
569 wxASSERT( scanType < MAX_STRUCT_TYPE_ID );
570 type_bits.set( scanType );
571 }
572
573 m_connAlgo->ForEachItem(
574 [&]( CN_ITEM& aItem )
575 {
576 if( aItem.Valid() && ( aItem.Net() == aNetCode ) && type_bits[aItem.Parent()->Type()] )
577 items.push_back( aItem.Parent() );
578 } );
579
580 std::sort( items.begin(), items.end() );
581 items.erase( std::unique( items.begin(), items.end() ), items.end() );
582 return items;
583}
584
585
586const std::vector<PCB_TRACK*>
588{
589 CN_CONNECTIVITY_ALGO::ITEM_MAP_ENTRY& entry = m_connAlgo->ItemEntry( aItem );
590
591 std::set<PCB_TRACK*> tracks;
592 std::vector<PCB_TRACK*> rv;
593
594 for( CN_ITEM* citem : entry.GetItems() )
595 {
596 for( CN_ITEM* connected : citem->ConnectedItems() )
597 {
598 if( connected->Valid() &&
599 ( connected->Parent()->Type() == PCB_TRACE_T ||
600 connected->Parent()->Type() == PCB_VIA_T ||
601 connected->Parent()->Type() == PCB_ARC_T ) )
602 {
603 tracks.insert( static_cast<PCB_TRACK*> ( connected->Parent() ) );
604 }
605 }
606 }
607
608 std::copy( tracks.begin(), tracks.end(), std::back_inserter( rv ) );
609 return rv;
610}
611
612
614 std::set<PAD*>* pads ) const
615{
616 for( CN_ITEM* citem : m_connAlgo->ItemEntry( aItem ).GetItems() )
617 {
618 for( CN_ITEM* connected : citem->ConnectedItems() )
619 {
620 if( connected->Valid() && connected->Parent()->Type() == PCB_PAD_T )
621 pads->insert( static_cast<PAD*> ( connected->Parent() ) );
622 }
623 }
624}
625
626
627const std::vector<PAD*> CONNECTIVITY_DATA::GetConnectedPads( const BOARD_CONNECTED_ITEM* aItem )
628const
629{
630 std::set<PAD*> pads;
631 std::vector<PAD*> rv;
632
633 GetConnectedPads( aItem, &pads );
634
635 std::copy( pads.begin(), pads.end(), std::back_inserter( rv ) );
636 return rv;
637}
638
639
640unsigned int CONNECTIVITY_DATA::GetNodeCount( int aNet ) const
641{
642 int sum = 0;
643
644 if( aNet < 0 ) // Node count for all nets
645 {
646 for( const RN_NET* net : m_nets )
647 sum += net->GetNodeCount();
648 }
649 else if( aNet < (int) m_nets.size() )
650 {
651 sum = m_nets[aNet]->GetNodeCount();
652 }
653
654 return sum;
655}
656
657
658unsigned int CONNECTIVITY_DATA::GetPadCount( int aNet ) const
659{
660 int n = 0;
661
662 for( CN_ITEM* pad : m_connAlgo->ItemList() )
663 {
664 if( !pad->Valid() || pad->Parent()->Type() != PCB_PAD_T)
665 continue;
666
667 PAD* dpad = static_cast<PAD*>( pad->Parent() );
668
669 if( aNet < 0 || aNet == dpad->GetNetCode() )
670 n++;
671 }
672
673 return n;
674}
675
676
677void CONNECTIVITY_DATA::RunOnUnconnectedEdges( std::function<bool( CN_EDGE& )> aFunc )
678{
679 for( RN_NET* rnNet : m_nets )
680 {
681 if( rnNet )
682 {
683 for( CN_EDGE& edge : rnNet->GetEdges() )
684 {
685 if( !aFunc( edge ) )
686 return;
687 }
688 }
689 }
690}
691
692
693static int getMinDist( BOARD_CONNECTED_ITEM* aItem, const VECTOR2I& aPoint )
694{
695 switch( aItem->Type() )
696 {
697 case PCB_TRACE_T:
698 case PCB_ARC_T:
699 {
700 PCB_TRACK* track = static_cast<PCB_TRACK*>( aItem );
701
702 return std::min( GetLineLength( track->GetStart(), aPoint ),
703 GetLineLength( track->GetEnd(), aPoint ) );
704 }
705
706 default:
707 return GetLineLength( aItem->GetPosition(), aPoint );
708 }
709}
710
711
713{
714 const std::list<CN_ITEM*>& items = GetConnectivityAlgo()->ItemEntry( aTrack ).GetItems();
715
716 // Not in the connectivity system. This is a bug!
717 if( items.empty() )
718 {
719 wxFAIL_MSG( wxT( "track not in connectivity system" ) );
720 return false;
721 }
722
723 CN_ITEM* citem = items.front();
724
725 if( !citem->Valid() )
726 return false;
727
728 if( aTrack->Type() == PCB_TRACE_T || aTrack->Type() == PCB_ARC_T )
729 {
730 // Test if a segment is connected on each end.
731 //
732 // NB: be wary of short segments which can be connected to the *same* other item on
733 // each end. If that's their only connection then they're still dangling.
734
735 PCB_LAYER_ID layer = aTrack->GetLayer();
736 int accuracy = KiROUND( aTrack->GetWidth() / 2 );
737 int start_count = 0;
738 int end_count = 0;
739
740 for( CN_ITEM* connected : citem->ConnectedItems() )
741 {
742 BOARD_CONNECTED_ITEM* item = connected->Parent();
743 ZONE* zone = dynamic_cast<ZONE*>( item );
744 DRC_RTREE* rtree = nullptr;
745 bool hitStart = false;
746 bool hitEnd = false;
747
748 if( item->GetFlags() & IS_DELETED )
749 continue;
750
751 if( zone )
752 rtree = zone->GetBoard()->m_CopperZoneRTreeCache[ zone ].get();
753
754 if( rtree )
755 {
756 SHAPE_CIRCLE start( aTrack->GetStart(), accuracy );
757 SHAPE_CIRCLE end( aTrack->GetEnd(), accuracy );
758
759 hitStart = rtree->QueryColliding( start.BBox(), &start, layer );
760 hitEnd = rtree->QueryColliding( end.BBox(), &end, layer );
761 }
762 else
763 {
764 std::shared_ptr<SHAPE> shape = item->GetEffectiveShape( layer );
765
766 hitStart = shape->Collide( aTrack->GetStart(), accuracy );
767 hitEnd = shape->Collide( aTrack->GetEnd(), accuracy );
768 }
769
770 if( hitStart && hitEnd )
771 {
772 if( zone )
773 {
774 // Both start and end in a zone: track may be redundant, but it's not dangling
775 return false;
776 }
777
778 if( getMinDist( item, aTrack->GetStart() ) < getMinDist( item, aTrack->GetEnd() ) )
779 start_count++;
780 else
781 end_count++;
782 }
783 else if( hitStart )
784 {
785 start_count++;
786 }
787 else if( hitEnd )
788 {
789 end_count++;
790 }
791
792 if( start_count > 0 && end_count > 0 )
793 return false;
794 }
795
796 if( aPos )
797 *aPos = (start_count == 0 ) ? aTrack->GetStart() : aTrack->GetEnd();
798
799 return true;
800 }
801 else if( aTrack->Type() == PCB_VIA_T )
802 {
803 // Test if a via is only connected on one layer
804
805 const std::vector<CN_ITEM*>& connected = citem->ConnectedItems();
806
807 if( connected.empty() )
808 {
809 if( aPos )
810 *aPos = aTrack->GetPosition();
811
812 return true;
813 }
814
815 // Here, we check if the via is connected only to items on a single layer
816 int first_layer = UNDEFINED_LAYER;
817
818 for( CN_ITEM* item : connected )
819 {
820 if( item->Parent()->GetFlags() & IS_DELETED )
821 continue;
822
823 if( first_layer == UNDEFINED_LAYER )
824 first_layer = item->Layer();
825 else if( item->Layer() != first_layer )
826 return false;
827 }
828
829 if( aPos )
830 *aPos = aTrack->GetPosition();
831
832 return true;
833 }
834 else
835 {
836 wxFAIL_MSG( wxT( "CONNECTIVITY_DATA::TestTrackEndpointDangling: unknown track type" ) );
837 }
838
839 return false;
840}
841
842
843const std::vector<BOARD_CONNECTED_ITEM*>
845 const VECTOR2I& aAnchor,
846 const std::initializer_list<KICAD_T>& aTypes,
847 const int& aMaxError ) const
848{
849 CN_CONNECTIVITY_ALGO::ITEM_MAP_ENTRY& entry = m_connAlgo->ItemEntry( aItem );
850 std::vector<BOARD_CONNECTED_ITEM*> rv;
851 SEG::ecoord maxError_sq = (SEG::ecoord) aMaxError * aMaxError;
852
853 for( CN_ITEM* cnItem : entry.GetItems() )
854 {
855 for( CN_ITEM* connected : cnItem->ConnectedItems() )
856 {
857 for( const std::shared_ptr<CN_ANCHOR>& anchor : connected->Anchors() )
858 {
859 if( ( anchor->Pos() - aAnchor ).SquaredEuclideanNorm() <= maxError_sq )
860 {
861 for( KICAD_T type : aTypes )
862 {
863 if( connected->Valid() && connected->Parent()->Type() == type )
864 {
865 rv.push_back( connected->Parent() );
866 break;
867 }
868 }
869
870 break;
871 }
872 }
873 }
874 }
875
876 return rv;
877}
878
879
881{
882 if ( aNet < 0 || aNet >= (int) m_nets.size() )
883 {
884 return nullptr;
885 }
886
887 return m_nets[ aNet ];
888}
889
890
892{
893 if ( aItem->Type() == PCB_FOOTPRINT_T)
894 {
895 for( PAD* pad : static_cast<FOOTPRINT*>( aItem )->Pads() )
896 m_connAlgo->MarkNetAsDirty( pad->GetNetCode() );
897 }
898 if (aItem->IsConnected() )
899 {
900 m_connAlgo->MarkNetAsDirty( static_cast<BOARD_CONNECTED_ITEM*>( aItem )->GetNetCode() );
901 }
902}
903
904
906{
907 m_progressReporter = aReporter;
908 m_connAlgo->SetProgressReporter( m_progressReporter );
909}
910
911
912const std::vector<CN_EDGE> CONNECTIVITY_DATA::GetRatsnestForItems( std::vector<BOARD_ITEM*> aItems )
913{
914 std::set<int> nets;
915 std::vector<CN_EDGE> edges;
916 std::set<BOARD_CONNECTED_ITEM*> item_set;
917
918 for( BOARD_ITEM* item : aItems )
919 {
920 if( item->Type() == PCB_FOOTPRINT_T )
921 {
922 FOOTPRINT* footprint = static_cast<FOOTPRINT*>( item );
923
924 for( PAD* pad : footprint->Pads() )
925 {
926 nets.insert( pad->GetNetCode() );
927 item_set.insert( pad );
928 }
929 }
930 else if( auto conn_item = dyn_cast<BOARD_CONNECTED_ITEM*>( item ) )
931 {
932 item_set.insert( conn_item );
933 nets.insert( conn_item->GetNetCode() );
934 }
935 }
936
937 for( int netcode : nets )
938 {
939 RN_NET* net = GetRatsnestForNet( netcode );
940
941 for( const CN_EDGE& edge : net->GetEdges() )
942 {
943 std::shared_ptr<const CN_ANCHOR> srcNode = edge.GetSourceNode();
944 std::shared_ptr<const CN_ANCHOR> dstNode = edge.GetTargetNode();
945
946 BOARD_CONNECTED_ITEM* srcParent = srcNode->Parent();
947 BOARD_CONNECTED_ITEM* dstParent = dstNode->Parent();
948
949 bool srcFound = ( item_set.find( srcParent ) != item_set.end() );
950 bool dstFound = ( item_set.find( dstParent ) != item_set.end() );
951
952 if ( srcFound && dstFound )
953 edges.push_back( edge );
954 }
955 }
956
957 return edges;
958}
959
960
961const std::vector<CN_EDGE> CONNECTIVITY_DATA::GetRatsnestForPad( const PAD* aPad )
962{
963 std::vector<CN_EDGE> edges;
964 RN_NET* net = GetRatsnestForNet( aPad->GetNetCode() );
965
966 for( const CN_EDGE& edge : net->GetEdges() )
967 {
968 if( edge.GetSourceNode()->Parent() == aPad || edge.GetTargetNode()->Parent() == aPad )
969 edges.push_back( edge );
970 }
971
972 return edges;
973}
974
975
976const std::vector<CN_EDGE> CONNECTIVITY_DATA::GetRatsnestForComponent( FOOTPRINT* aComponent, bool aSkipInternalConnections )
977{
978 std::set<int> nets;
979 std::set<const PAD*> pads;
980 std::vector<CN_EDGE> edges;
981
982 for( PAD* pad : aComponent->Pads() )
983 {
984 nets.insert( pad->GetNetCode() );
985 pads.insert( pad );
986 }
987
988 for( const auto& netcode : nets )
989 {
990 RN_NET* net = GetRatsnestForNet( netcode );
991
992 for( const CN_EDGE& edge : net->GetEdges() )
993 {
994 auto srcNode = edge.GetSourceNode();
995 auto dstNode = edge.GetTargetNode();
996
997 const PAD* srcParent = static_cast<const PAD*>( srcNode->Parent() );
998 const PAD* dstParent = static_cast<const PAD*>( dstNode->Parent() );
999
1000 bool srcFound = ( pads.find(srcParent) != pads.end() );
1001 bool dstFound = ( pads.find(dstParent) != pads.end() );
1002
1003 if ( srcFound && dstFound && !aSkipInternalConnections )
1004 {
1005 edges.push_back( edge );
1006 }
1007 else if ( srcFound || dstFound )
1008 {
1009 edges.push_back( edge );
1010 }
1011 }
1012 }
1013
1014 return edges;
1015}
1016
1017
A base class derived from BOARD_ITEM for items that can be connected and have a net,...
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:70
virtual PCB_LAYER_ID GetLayer() const
Return the primary layer this item is on.
Definition: board_item.h:192
virtual bool IsConnected() const
Returns information if the object is derived from BOARD_CONNECTED_ITEM.
Definition: board_item.h:127
virtual std::shared_ptr< SHAPE > GetEffectiveShape(PCB_LAYER_ID aLayer=UNDEFINED_LAYER, FLASHING aFlash=FLASHING::DEFAULT) const
Some pad shapes can be complex (rounded/chamfered rectangle), even without considering custom shapes.
Definition: board_item.cpp:219
virtual const BOARD * GetBoard() const
Return the BOARD in which this BOARD_ITEM resides, or NULL if none.
Definition: board_item.cpp:43
Information pertinent to a Pcbnew printed circuit board.
Definition: board.h:269
const NETINFO_LIST & GetNetInfo() const
Definition: board.h:784
std::unordered_map< ZONE *, std::unique_ptr< DRC_RTREE > > m_CopperZoneRTreeCache
Definition: board.h:1164
BOARD_DESIGN_SETTINGS & GetDesignSettings() const
Definition: board.cpp:704
void CacheTriangulation(PROGRESS_REPORTER *aReporter=nullptr, const std::vector< ZONE * > &aZones={})
Definition: board.cpp:722
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
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
allow parallel connection threads
bool Valid() const
BOARD_CONNECTED_ITEM * Parent() const
int SubpolyIndex() const
bool TestTrackEndpointDangling(PCB_TRACK *aTrack, VECTOR2I *aPos=nullptr) const
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
bool m_skipRatsnest
Used to suppress ratsnest calculations on dynamic ratsnests.
void FindIsolatedCopperIslands(ZONE *aZone, std::vector< int > &aIslands)
Function FindIsolatedCopperIslands() Searches for copper islands in zone aZone that are not connected...
unsigned int GetPadCount(int aNet=-1) const
const std::vector< BOARD_CONNECTED_ITEM * > GetConnectedItemsAtAnchor(const BOARD_CONNECTED_ITEM *aItem, const VECTOR2I &aAnchor, const std::initializer_list< KICAD_T > &aTypes, const int &aMaxError=0) const
Function GetConnectedItemsAtAnchor() Returns a list of items connected to a source item aItem at posi...
void MarkItemNetAsDirty(BOARD_ITEM *aItem)
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
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.
void ClearRatsnest()
Function Clear() Erases the connectivity database.
bool Remove(BOARD_ITEM *aItem)
Function Remove() Removes an item from the connectivity data.
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...
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
std::map< int, wxString > m_netclassMap
Map of netcode -> netclass the net is a member of; used for ratsnest painting.
const std::vector< CN_EDGE > GetRatsnestForComponent(FOOTPRINT *aComponent, bool aSkipInternalConnections=false)
bool Add(BOARD_ITEM *aItem)
Function Add() Adds an item to the connectivity data.
const std::vector< BOARD_CONNECTED_ITEM * > GetConnectedItems(const BOARD_CONNECTED_ITEM *aItem, const std::initializer_list< KICAD_T > &aTypes, bool aIgnoreNetcodes=false) const
Function GetConnectedItems() Returns a list of items connected to a source item aItem.
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
const std::vector< CN_EDGE > GetRatsnestForItems(const std::vector< BOARD_ITEM * > aItems)
const std::vector< BOARD_CONNECTED_ITEM * > GetNetItems(int aNetCode, const std::initializer_list< KICAD_T > &aTypes) const
Function GetNetItems() Returns the list of items that belong to a certain net.
unsigned int GetUnconnectedCount(bool aVisibileOnly) const
void internalRecalculateRatsnest(BOARD_COMMIT *aCommit=nullptr)
Updates the ratsnest for the board without locking the connectivity mutex.
void HideLocalRatsnest()
Hides the temporary, selection-based ratsnest lines.
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.
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:48
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:211
virtual VECTOR2I GetPosition() const
Definition: eda_item.h:249
KICAD_T Type() const
Returns the type of object.
Definition: eda_item.h:97
EDA_ITEM_FLAGS GetFlags() const
Definition: eda_item.h:142
PADS & Pads()
Definition: footprint.h:170
static const char Default[]
the name of the default NETCLASS
Definition: netclass.h:49
Handle the data for a net.
Definition: netinfo.h:67
Definition: pad.h:60
int GetWidth() const
Definition: pcb_track.h:108
const VECTOR2I & GetStart() const
Definition: pcb_track.h:114
VECTOR2I GetPosition() const override
Definition: pcb_track.h:104
const VECTOR2I & GetEnd() const
Definition: pcb_track.h:111
A small class to help profiling.
Definition: profile.h:47
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.
Definition: ratsnest_data.h:63
unsigned int GetNodeCount() const
Definition: ratsnest_data.h:83
const std::vector< CN_EDGE > & GetEdges() const
Definition: ratsnest_data.h:85
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:44
const BOX2I BBox(int aClearance=0) const override
Compute a bounding box of the shape, with a margin of aClearance a collision.
Definition: shape_circle.h:70
bool PointInside(const VECTOR2I &aPt, int aAccuracy=0, bool aUseBBoxCache=false) const
Check if point aP lies inside a polygon (any type) defined by the line chain.
Represent a polyline containing arcs as well as line segments: A chain of connected line and/or arc s...
const std::vector< VECTOR2I > & CPoints() const
Represent a set of closed polygons.
const SHAPE_LINE_CHAIN & COutline(int aIndex) const
Handle a list of polygons defining a copper zone.
Definition: zone.h:57
bool IsFilled() const
Definition: zone.h:242
SHAPE_POLY_SET * GetFill(PCB_LAYER_ID aLayer)
Definition: zone.h:614
static int getMinDist(BOARD_CONNECTED_ITEM *aItem, const VECTOR2I &aPoint)
#define _(s)
#define IS_DELETED
PCB_LAYER_ID
A quick note on layer IDs:
Definition: layer_ids.h:59
@ UNDEFINED_LAYER
Definition: layer_ids.h:60
PCB_LAYER_ID ToLAYER_ID(int aLayer)
Definition: lset.cpp:932
bool contains(const _Container &__container, _Value __value)
Returns true if the container contains the given value.
Definition: kicad_algo.h:99
Class that computes missing connections on a PCB.
thread_pool & GetKiCadThreadPool()
Get a reference to the current thread pool.
Definition: thread_pool.cpp:32
static thread_pool * tp
Definition: thread_pool.cpp:30
BS::thread_pool thread_pool
Definition: thread_pool.h:30
double GetLineLength(const VECTOR2I &aPointA, const VECTOR2I &aPointB)
Return the length of a line segment defined by aPointA and aPointB.
Definition: trigo.h:188
KICAD_T
The set of class identification values stored in EDA_ITEM::m_structType.
Definition: typeinfo.h:78
@ PCB_VIA_T
class PCB_VIA, a via (like a track segment on a copper layer)
Definition: typeinfo.h:102
@ MAX_STRUCT_TYPE_ID
Definition: typeinfo.h:241
@ PCB_FOOTPRINT_T
class FOOTPRINT, a footprint
Definition: typeinfo.h:86
@ PCB_PAD_T
class PAD, a pad in a footprint
Definition: typeinfo.h:87
@ PCB_ARC_T
class PCB_ARC, an arc track segment on a copper layer
Definition: typeinfo.h:103
@ PCB_TRACE_T
class PCB_TRACK, a track segment (segment on a copper layer)
Definition: typeinfo.h:101
constexpr ret_type KiROUND(fp_type v)
Round a floating point number to an integer using "round halfway cases away from zero".
Definition: util.h:85