KiCad PCB EDA Suite
Loading...
Searching...
No Matches
connectivity_algo.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) 2016-2018 CERN
5 * Copyright The KiCad Developers, see AUTHORS.txt for contributors.
6 *
7 * @author Tomasz Wlostowski <[email protected]>
8 *
9 * This program is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU General Public License
11 * as published by the Free Software Foundation; either version 2
12 * of the License, or (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, you may find one here:
21 * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
22 * or you may search the http://www.gnu.org website for the version 2 license,
23 * or you may write to the Free Software Foundation, Inc.,
24 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
25 */
26
27
28#include <algorithm>
29#include <future>
30#include <mutex>
31
33#include <progress_reporter.h>
35#include <board_commit.h>
36#include <thread_pool.h>
37#include <pcb_shape.h>
38
39#include <wx/log.h>
40
41#ifdef PROFILE
42#include <core/profile.h>
43#endif
44
45
47{
48 markItemNetAsDirty( aItem );
49
50 switch( aItem->Type() )
51 {
52 case PCB_FOOTPRINT_T:
53 for( PAD* pad : static_cast<FOOTPRINT*>( aItem )->Pads() )
54 {
55 m_itemMap[pad].MarkItemsAsInvalid();
56 m_itemMap.erase( pad );
57 }
58
59 m_itemList.SetDirty( true );
60 break;
61
62 case PCB_PAD_T:
63 case PCB_TRACE_T:
64 case PCB_ARC_T:
65 case PCB_VIA_T:
66 case PCB_ZONE_T:
67 case PCB_SHAPE_T:
68 m_itemMap[aItem].MarkItemsAsInvalid();
69 m_itemMap.erase ( aItem );
70 m_itemList.SetDirty( true );
71 break;
72
73 default:
74 return false;
75 }
76
77 // Once we delete an item, it may connect between lists, so mark both as potentially invalid
78 m_itemList.SetHasInvalid( true );
79
80 return true;
81}
82
83
85{
86 if( aItem->IsConnected() )
87 {
88 const BOARD_CONNECTED_ITEM* citem = static_cast<const BOARD_CONNECTED_ITEM*>( aItem );
89 MarkNetAsDirty( citem->GetNetCode() );
90 }
91 else
92 {
93 if( aItem->Type() == PCB_FOOTPRINT_T )
94 {
95 const FOOTPRINT* footprint = static_cast<const FOOTPRINT*>( aItem );
96
97 for( PAD* pad : footprint->Pads() )
98 MarkNetAsDirty( pad->GetNetCode() );
99 }
100 }
101}
102
103
105{
106 if( !aItem->IsOnCopperLayer() )
107 return false;
108
109 auto alreadyAdded =
110 [this]( BOARD_ITEM* item )
111 {
112 auto it = m_itemMap.find( item );
113
114 if( it == m_itemMap.end() )
115 return false;
116
117 // Don't be fooled by an empty ITEM_MAP_ENTRY auto-created by operator[].
118 return !it->second.GetItems().empty();
119 };
120
121 switch( aItem->Type() )
122 {
123 case PCB_NETINFO_T:
124 MarkNetAsDirty( static_cast<NETINFO_ITEM*>( aItem )->GetNetCode() );
125 break;
126
127 case PCB_FOOTPRINT_T:
128 {
129 if( static_cast<FOOTPRINT*>( aItem )->GetAttributes() & FP_JUST_ADDED )
130 return false;
131
132 for( PAD* pad : static_cast<FOOTPRINT*>( aItem )->Pads() )
133 {
134 if( alreadyAdded( pad ) )
135 return false;
136
137 add( m_itemList, pad );
138 }
139
140 break;
141 }
142
143 case PCB_PAD_T:
144 {
145 if( FOOTPRINT* fp = aItem->GetParentFootprint() )
146 {
147 if( fp->GetAttributes() & FP_JUST_ADDED )
148 return false;
149 }
150
151 if( alreadyAdded( aItem ) )
152 return false;
153
154 add( m_itemList, static_cast<PAD*>( aItem ) );
155 break;
156 }
157
158 case PCB_TRACE_T:
159 if( alreadyAdded( aItem ) )
160 return false;
161
162 add( m_itemList, static_cast<PCB_TRACK*>( aItem ) );
163 break;
164
165 case PCB_ARC_T:
166 if( alreadyAdded( aItem ) )
167 return false;
168
169 add( m_itemList, static_cast<PCB_ARC*>( aItem ) );
170 break;
171
172 case PCB_VIA_T:
173 if( alreadyAdded( aItem ) )
174 return false;
175
176 add( m_itemList, static_cast<PCB_VIA*>( aItem ) );
177 break;
178
179 case PCB_SHAPE_T:
180 if( alreadyAdded( aItem ) )
181 return false;
182
183 if( !IsCopperLayer( aItem->GetLayer() ) )
184 return false;
185
186 add( m_itemList, static_cast<PCB_SHAPE*>( aItem ) );
187 break;
188
189 case PCB_ZONE_T:
190 {
191 ZONE* zone = static_cast<ZONE*>( aItem );
192
193 if( alreadyAdded( aItem ) )
194 return false;
195
196 m_itemMap[zone] = ITEM_MAP_ENTRY();
197
198 // Don't check for connections on layers that only exist in the zone but
199 // were disabled in the board
200 BOARD* board = zone->GetBoard();
201 LSET layerset = board->GetEnabledLayers() & zone->GetLayerSet();
202
203 layerset.RunOnLayers(
204 [&]( PCB_LAYER_ID layer )
205 {
206 for( CN_ITEM* zitem : m_itemList.Add( zone, layer ) )
207 m_itemMap[zone].Link( zitem );
208 } );
209 }
210 break;
211
212 default:
213 return false;
214 }
215
216 markItemNetAsDirty( aItem );
217
218 return true;
219}
220
221
223{
224 for( CN_ITEM* item : m_itemList )
225 item->RemoveInvalidRefs();
226}
227
228
230{
231 std::lock_guard lock( m_mutex );
232#ifdef PROFILE
233 PROF_TIMER garbage_collection( "garbage-collection" );
234#endif
235 std::vector<CN_ITEM*> garbage;
236 garbage.reserve( 1024 );
237
238 m_parentConnectivityData->RemoveInvalidRefs();
239
240 if( m_isLocal )
241 m_globalConnectivityData->RemoveInvalidRefs();
242
243 m_itemList.RemoveInvalidItems( garbage );
244
245 for( CN_ITEM* item : garbage )
246 delete item;
247
248#ifdef PROFILE
249 garbage_collection.Show();
250 PROF_TIMER search_basic( "search-basic" );
251#endif
252
254 std::vector<CN_ITEM*> dirtyItems;
255 std::copy_if( m_itemList.begin(), m_itemList.end(), std::back_inserter( dirtyItems ),
256 [] ( CN_ITEM* aItem )
257 {
258 return aItem->Dirty();
259 } );
260
262 {
263 m_progressReporter->SetMaxProgress( dirtyItems.size() );
264
265 if( !m_progressReporter->KeepRefreshing() )
266 return;
267 }
268
269 if( m_itemList.IsDirty() )
270 {
271 std::vector<std::future<size_t>> returns( dirtyItems.size() );
272
273 for( size_t ii = 0; ii < dirtyItems.size(); ++ii )
274 {
275 returns[ii] = tp.submit_task(
276 [&dirtyItems, ii, this] () ->size_t
277 {
278 if( m_progressReporter && m_progressReporter->IsCancelled() )
279 return 0;
280
281 CN_VISITOR visitor( dirtyItems[ii] );
282 m_itemList.FindNearby( dirtyItems[ii], visitor );
283
285 m_progressReporter->AdvanceProgress();
286
287 return 1;
288 } );
289 }
290
291 for( const std::future<size_t>& ret : returns )
292 {
293 // Here we balance returns with a 250ms timeout to allow UI updating
294 std::future_status status = ret.wait_for( std::chrono::milliseconds( 250 ) );
295
296 while( status != std::future_status::ready )
297 {
299 m_progressReporter->KeepRefreshing();
300
301 status = ret.wait_for( std::chrono::milliseconds( 250 ) );
302 }
303 }
304
306 m_progressReporter->KeepRefreshing();
307 }
308
309#ifdef PROFILE
310 search_basic.Show();
311#endif
312
313 m_itemList.ClearDirtyFlags();
314}
315
316
321
322
324CN_CONNECTIVITY_ALGO::SearchClusters( CLUSTER_SEARCH_MODE aMode, bool aExcludeZones, int aSingleNet )
325{
326 bool withinAnyNet = ( aMode != CSM_PROPAGATE );
327
328 std::deque<CN_ITEM*> Q;
329 std::set<CN_ITEM*> item_set;
330
331 CLUSTERS clusters;
332
333 if( m_itemList.IsDirty() )
335
336 std::set<CN_ITEM*> visited;
337
338 auto addToSearchList =
339 [&item_set, withinAnyNet, aSingleNet, &aExcludeZones]( CN_ITEM *aItem )
340 {
341 if( withinAnyNet && aItem->Net() <= 0 )
342 return;
343
344 if( !aItem->Valid() )
345 return;
346
347 if( aSingleNet >=0 && aItem->Net() != aSingleNet )
348 return;
349
350 if( aExcludeZones && aItem->Parent()->Type() == PCB_ZONE_T )
351 return;
352
353 item_set.insert( aItem );
354 };
355
356 std::for_each( m_itemList.begin(), m_itemList.end(), addToSearchList );
357
358 if( m_progressReporter && m_progressReporter->IsCancelled() )
359 return CLUSTERS();
360
361 while( !item_set.empty() )
362 {
363 std::shared_ptr<CN_CLUSTER> cluster = std::make_shared<CN_CLUSTER>();
364 CN_ITEM* root;
365 auto it = item_set.begin();
366
367 while( it != item_set.end() && visited.contains( *it ) )
368 it = item_set.erase( item_set.begin() );
369
370 if( it == item_set.end() )
371 break;
372
373 root = *it;
374 visited.insert( root );
375
376 Q.clear();
377 Q.push_back( root );
378
379 while( Q.size() )
380 {
381 CN_ITEM* current = Q.front();
382
383 Q.pop_front();
384 cluster->Add( current );
385
386 for( CN_ITEM* n : current->ConnectedItems() )
387 {
388 if( withinAnyNet && n->Net() != root->Net() )
389 continue;
390
391 if( aExcludeZones && n->Parent()->Type() == PCB_ZONE_T )
392 continue;
393
394 if( !visited.contains( n ) && n->Valid() )
395 {
396 visited.insert( n );
397 Q.push_back( n );
398 }
399 }
400 }
401
402 clusters.push_back( std::move( cluster ) );
403 }
404
405 if( m_progressReporter && m_progressReporter->IsCancelled() )
406 return CLUSTERS();
407
408 std::sort( clusters.begin(), clusters.end(),
409 []( const std::shared_ptr<CN_CLUSTER>& a, const std::shared_ptr<CN_CLUSTER>& b )
410 {
411 return a->OriginNet() < b->OriginNet();
412 } );
413
414 return clusters;
415}
416
417
419{
420 // Generate CN_ZONE_LAYERs for each island on each layer of each zone
421 //
422 std::vector<CN_ZONE_LAYER*> zitems;
423
424 for( ZONE* zone : aBoard->Zones() )
425 {
426 if( zone->IsOnCopperLayer() )
427 {
428 m_itemMap[zone] = ITEM_MAP_ENTRY();
429 markItemNetAsDirty( zone );
430
431 // Don't check for connections on layers that only exist in the zone but
432 // were disabled in the board
433 BOARD* board = zone->GetBoard();
434 LSET layerset = board->GetEnabledLayers() & zone->GetLayerSet() & LSET::AllCuMask();
435
436 layerset.RunOnLayers(
437 [&]( PCB_LAYER_ID layer )
438 {
439 for( int j = 0; j < zone->GetFilledPolysList( layer )->OutlineCount(); j++ )
440 zitems.push_back( new CN_ZONE_LAYER( zone, layer, j ) );
441 } );
442 }
443 }
444
445 // Setup progress metrics
446 //
447 int progressDelta = 50;
448 double size = 0.0;
449
450 size += zitems.size(); // Once for building RTrees
451 size += zitems.size(); // Once for adding to connectivity
452 size += aBoard->Tracks().size();
453 size += aBoard->Drawings().size();
454
455 for( FOOTPRINT* footprint : aBoard->Footprints() )
456 size += footprint->Pads().size();
457
458 size *= 1.5; // Our caller gets the other third of the progress bar
459
460 progressDelta = std::max( progressDelta, (int) size / 4 );
461
462 auto report =
463 [&]( int progress )
464 {
465 if( aReporter && ( progress % progressDelta ) == 0 )
466 {
467 aReporter->SetCurrentProgress( progress / size );
468 aReporter->KeepRefreshing( false );
469 }
470 };
471
472 // Generate RTrees for CN_ZONE_LAYER items (in parallel)
473 //
475 std::vector<std::future<size_t>> returns( zitems.size() );
476
477 auto cache_zones =
478 [aReporter]( CN_ZONE_LAYER* aZoneLayer ) -> size_t
479 {
480 if( aReporter && aReporter->IsCancelled() )
481 return 0;
482
483 aZoneLayer->BuildRTree();
484
485 if( aReporter )
486 aReporter->AdvanceProgress();
487
488 return 1;
489 };
490
491 for( size_t ii = 0; ii < zitems.size(); ++ii )
492 {
493 CN_ZONE_LAYER* ptr = zitems[ii];
494 returns[ii] = tp.submit_task(
495 [cache_zones, ptr] { return cache_zones( ptr ); } );
496 }
497
498 for( const std::future<size_t>& ret : returns )
499 {
500 std::future_status status = ret.wait_for( std::chrono::milliseconds( 250 ) );
501
502 while( status != std::future_status::ready )
503 {
504 if( aReporter )
505 aReporter->KeepRefreshing();
506
507 status = ret.wait_for( std::chrono::milliseconds( 250 ) );
508 }
509
510 }
511
512 // Add CN_ZONE_LAYERS, tracks, and pads to connectivity
513 //
514 int ii = zitems.size();
515
516 for( CN_ZONE_LAYER* zitem : zitems )
517 {
518 m_itemList.Add( zitem );
519 m_itemMap[ zitem->Parent() ].Link( zitem );
520 report( ++ii );
521 }
522
523 for( PCB_TRACK* tv : aBoard->Tracks() )
524 {
525 Add( tv );
526 report( ++ii );
527 }
528
529 for( FOOTPRINT* footprint : aBoard->Footprints() )
530 {
531 for( PAD* pad : footprint->Pads() )
532 {
533 Add( pad );
534 report( ++ii );
535 }
536 }
537
538 for( BOARD_ITEM* drawing : aBoard->Drawings() )
539 {
540 if( PCB_SHAPE* shape = dynamic_cast<PCB_SHAPE*>( drawing ) )
541 {
542 if( shape->IsOnCopperLayer() )
543 Add( shape );
544 }
545
546 report( ++ii );
547 }
548
549 if( aReporter )
550 {
551 aReporter->SetCurrentProgress( (double) ii / (double) size );
552 aReporter->KeepRefreshing( false );
553 }
554}
555
556
557void CN_CONNECTIVITY_ALGO::LocalBuild( const std::shared_ptr<CONNECTIVITY_DATA>& aGlobalConnectivity,
558 const std::vector<BOARD_ITEM*>& aLocalItems )
559{
560 m_isLocal = true;
561 m_globalConnectivityData = aGlobalConnectivity;
562
563 for( BOARD_ITEM* item : aLocalItems )
564 {
565 switch( item->Type() )
566 {
567 case PCB_TRACE_T:
568 case PCB_ARC_T:
569 case PCB_VIA_T:
570 case PCB_PAD_T:
571 case PCB_FOOTPRINT_T:
572 case PCB_SHAPE_T:
573 Add( item );
574 break;
575
576 default:
577 break;
578 }
579 }
580}
581
582
584{
585 for( const std::shared_ptr<CN_CLUSTER>& cluster : m_connClusters )
586 {
587 if( cluster->IsConflicting() )
588 {
589 // Conflicting pads in cluster: we don't know the user's intent so best to do
590 // nothing.
591 wxLogTrace( wxT( "CN" ), wxT( "Conflicting pads in cluster %p; skipping propagation" ),
592 cluster.get() );
593 }
594 else if( cluster->HasValidNet() )
595 {
596 // Propagate from the origin (will be a pad if there are any, or another item if
597 // there are no pads).
598 int n_changed = 0;
599
600 for( CN_ITEM* item : *cluster )
601 {
602 if( item->Valid() && item->CanChangeNet()
603 && item->Parent()->GetNetCode() != cluster->OriginNet() )
604 {
605 MarkNetAsDirty( item->Parent()->GetNetCode() );
606 MarkNetAsDirty( cluster->OriginNet() );
607
608 if( aCommit )
609 aCommit->Modify( item->Parent() );
610
611 item->Parent()->SetNetCode( cluster->OriginNet() );
612 n_changed++;
613 }
614 }
615
616 if( n_changed )
617 {
618 wxLogTrace( wxT( "CN" ), wxT( "Cluster %p: net: %d %s" ),
619 cluster.get(),
620 cluster->OriginNet(),
621 (const char*) cluster->OriginNetName().c_str() );
622 }
623 else
624 {
625 wxLogTrace( wxT( "CN" ), wxT( "Cluster %p: no changeable items to propagate to" ),
626 cluster.get() );
627 }
628 }
629 else
630 {
631 wxLogTrace( wxT( "CN" ), wxT( "Cluster %p: connected to unused net" ),
632 cluster.get() );
633 }
634 }
635}
636
637
644
645
646void CN_CONNECTIVITY_ALGO::FillIsolatedIslandsMap( std::map<ZONE*, std::map<PCB_LAYER_ID, ISOLATED_ISLANDS>>& aMap,
647 bool aConnectivityAlreadyRebuilt )
648{
649 int progressDelta = 50;
650 int ii = 0;
651
652 progressDelta = std::max( progressDelta, (int) aMap.size() / 4 );
653
654 if( !aConnectivityAlreadyRebuilt )
655 {
656 for( const auto& [ zone, islands ] : aMap )
657 {
658 Remove( zone );
659 Add( zone );
660 ii++;
661
662 if( m_progressReporter && ( ii % progressDelta ) == 0 )
663 {
664 m_progressReporter->SetCurrentProgress( (double) ii / (double) aMap.size() );
665 m_progressReporter->KeepRefreshing( false );
666 }
667
668 if( m_progressReporter && m_progressReporter->IsCancelled() )
669 return;
670 }
671 }
672
674
675 for( auto& [ zone, zoneIslands ] : aMap )
676 {
677 for( auto& [ layer, layerIslands ] : zoneIslands )
678 {
679 if( zone->GetFilledPolysList( layer )->IsEmpty() )
680 continue;
681
682 for( const std::shared_ptr<CN_CLUSTER>& cluster : m_connClusters )
683 {
684 for( CN_ITEM* item : *cluster )
685 {
686 if( item->Parent() == zone && item->GetBoardLayer() == layer )
687 {
688 CN_ZONE_LAYER* z = static_cast<CN_ZONE_LAYER*>( item );
689
690 if( cluster->IsOrphaned() )
691 layerIslands.m_IsolatedOutlines.push_back( z->SubpolyIndex() );
692 else if( z->HasSingleConnection() )
693 layerIslands.m_SingleConnectionOutlines.push_back( z->SubpolyIndex() );
694 }
695 }
696 }
697 }
698 }
699}
700
701
707
708
710{
711 if( aNet < 0 )
712 return;
713
714 if( (int) m_dirtyNets.size() <= aNet )
715 {
716 int lastNet = m_dirtyNets.size() - 1;
717
718 if( lastNet < 0 )
719 lastNet = 0;
720
721 m_dirtyNets.resize( aNet + 1 );
722
723 for( int i = lastNet; i < aNet + 1; i++ )
724 m_dirtyNets[i] = true;
725 }
726
727 m_dirtyNets[aNet] = true;
728}
729
730
732{
733 PCB_LAYER_ID layer = aZoneLayer->GetLayer();
734 BOARD_CONNECTED_ITEM* item = aItem->Parent();
735
736 if( !item->IsOnLayer( layer ) )
737 return;
738
739 auto connect =
740 [&]()
741 {
742 // We don't propagate nets from zones, so any free-via net changes need to happen now.
743 if( aItem->Parent()->Type() == PCB_VIA_T && aItem->CanChangeNet() )
744 aItem->Parent()->SetNetCode( aZoneLayer->Net() );
745
746 aZoneLayer->Connect( aItem );
747 aItem->Connect( aZoneLayer );
748 };
749
750 // Try quick checks first...
751 if( item->Type() == PCB_PAD_T )
752 {
753 PAD* pad = static_cast<PAD*>( item );
754
755 if( pad->ConditionallyFlashed( layer )
756 && pad->GetZoneLayerOverride( layer ) == ZLO_FORCE_NO_ZONE_CONNECTION )
757 {
758 return;
759 }
760 }
761 else if( item->Type() == PCB_VIA_T )
762 {
763 PCB_VIA* via = static_cast<PCB_VIA*>( item );
764
765 if( via->ConditionallyFlashed( layer )
766 && via->GetZoneLayerOverride( layer ) == ZLO_FORCE_NO_ZONE_CONNECTION )
767 {
768 return;
769 }
770 }
771
772 for( int i = 0; i < aItem->AnchorCount(); ++i )
773 {
774 if( aZoneLayer->ContainsPoint( aItem->GetAnchor( i ) ) )
775 {
776 connect();
777 return;
778 }
779 }
780
781 if( item->Type() == PCB_VIA_T || item->Type() == PCB_PAD_T )
782 {
783 // As long as the pad/via crosses the zone layer, check for the full effective shape
784 // We check for the overlapping layers above
785 if( aZoneLayer->Collide( item->GetEffectiveShape( layer, FLASHING::ALWAYS_FLASHED ).get() ) )
786 connect();
787
788 return;
789 }
790
791 if( aZoneLayer->Collide( item->GetEffectiveShape( layer ).get() ) )
792 connect();
793}
794
796{
797 const ZONE* zoneA = static_cast<const ZONE*>( aZoneLayerA->Parent() );
798 const ZONE* zoneB = static_cast<const ZONE*>( aZoneLayerB->Parent() );
799
800 const BOX2I& boxA = aZoneLayerA->BBox();
801 const BOX2I& boxB = aZoneLayerB->BBox();
802
803 PCB_LAYER_ID layer = aZoneLayerA->GetLayer();
804
805 if( aZoneLayerB->GetLayer() != layer )
806 return;
807
808 if( !boxA.Intersects( boxB ) )
809 return;
810
811 const SHAPE_LINE_CHAIN& outline = zoneA->GetFilledPolysList( layer )->COutline( aZoneLayerA->SubpolyIndex() );
812
813 for( int i = 0; i < outline.PointCount(); i++ )
814 {
815 if( !boxB.Contains( outline.CPoint( i ) ) )
816 continue;
817
818 if( aZoneLayerB->ContainsPoint( outline.CPoint( i ) ) )
819 {
820 aZoneLayerA->Connect( aZoneLayerB );
821 aZoneLayerB->Connect( aZoneLayerA );
822 return;
823 }
824 }
825
826 const SHAPE_LINE_CHAIN& outline2 = zoneB->GetFilledPolysList( layer )->COutline( aZoneLayerB->SubpolyIndex() );
827
828 for( int i = 0; i < outline2.PointCount(); i++ )
829 {
830 if( !boxA.Contains( outline2.CPoint( i ) ) )
831 continue;
832
833 if( aZoneLayerA->ContainsPoint( outline2.CPoint( i ) ) )
834 {
835 aZoneLayerA->Connect( aZoneLayerB );
836 aZoneLayerB->Connect( aZoneLayerA );
837 return;
838 }
839 }
840}
841
842
844{
845 const BOARD_CONNECTED_ITEM* parentA = aCandidate->Parent();
846 const BOARD_CONNECTED_ITEM* parentB = m_item->Parent();
847
848 if( !aCandidate->Valid() || !m_item->Valid() )
849 return true;
850
851 if( parentA == parentB )
852 return true;
853
854 // Don't connect items in different nets that can't be changed
855 if( !aCandidate->CanChangeNet() && !m_item->CanChangeNet() && aCandidate->Net() != m_item->Net() )
856 return true;
857
858 // If both m_item and aCandidate are marked dirty, they will both be searched
859 // Since we are reciprocal in our connection, we arbitrarily pick one of the connections
860 // to conduct the expensive search
861 if( aCandidate->Dirty() && aCandidate < m_item )
862 return true;
863
864 // We should handle zone-zone connection separately
865 if ( parentA->Type() == PCB_ZONE_T && parentB->Type() == PCB_ZONE_T )
866 {
868 static_cast<CN_ZONE_LAYER*>( aCandidate ) );
869 return true;
870 }
871
872 if( parentA->Type() == PCB_ZONE_T )
873 {
874 checkZoneItemConnection( static_cast<CN_ZONE_LAYER*>( aCandidate ), m_item );
875 return true;
876 }
877
878 if( parentB->Type() == PCB_ZONE_T )
879 {
880 checkZoneItemConnection( static_cast<CN_ZONE_LAYER*>( m_item ), aCandidate );
881 return true;
882 }
883
884 LSET commonLayers = parentA->GetLayerSet() & parentB->GetLayerSet();
885
886 if( const BOARD* board = parentA->GetBoard() )
887 commonLayers &= board->GetEnabledLayers();
888
889 for( PCB_LAYER_ID layer : commonLayers )
890 {
893
894 if( parentA->Type() == PCB_PAD_T )
895 {
896 if( !static_cast<const PAD*>( parentA )->ConditionallyFlashed( layer ) )
897 flashingA = FLASHING::ALWAYS_FLASHED;
898 }
899 else if( parentA->Type() == PCB_VIA_T )
900 {
901 if( !static_cast<const PCB_VIA*>( parentA )->ConditionallyFlashed( layer ) )
902 flashingA = FLASHING::ALWAYS_FLASHED;
903 }
904
905 if( parentB->Type() == PCB_PAD_T )
906 {
907 if( !static_cast<const PAD*>( parentB )->ConditionallyFlashed( layer ) )
908 flashingB = FLASHING::ALWAYS_FLASHED;
909 }
910 else if( parentB->Type() == PCB_VIA_T )
911 {
912 if( !static_cast<const PCB_VIA*>( parentB )->ConditionallyFlashed( layer ) )
913 flashingB = FLASHING::ALWAYS_FLASHED;
914 }
915
916 if( parentA->GetEffectiveShape( layer, flashingA )->Collide(
917 parentB->GetEffectiveShape( layer, flashingB ).get() ) )
918 {
919 m_item->Connect( aCandidate );
920 aCandidate->Connect( m_item );
921 return true;
922 }
923 }
924
925 return true;
926};
927
928
930{
931 m_ratsnestClusters.clear();
932 m_connClusters.clear();
933 m_itemMap.clear();
934 m_itemList.Clear();
935
936}
937
942
943
945{
946 // Map of footprint -> map of pad number -> list of CN_ITEMs for pads with that number
947 std::map<FOOTPRINT*, std::map<wxString, std::vector<CN_ITEM*>>> padsByFootprint;
948
949 for( CN_ITEM* item : m_itemList )
950 {
951 if( !item->Valid() || item->Parent()->Type() != PCB_PAD_T )
952 continue;
953
954 auto pad = static_cast<const PAD*>( item->Parent() );
955
956 FOOTPRINT* fp = pad->GetParentFootprint();
957
958 padsByFootprint[fp][ pad->GetNumber() ].emplace_back( item );
959 }
960
961 for( auto& [footprint, padsMap] : padsByFootprint )
962 {
963 if( footprint->GetDuplicatePadNumbersAreJumpers() )
964 {
965 for( const std::vector<CN_ITEM*>& padsList : padsMap | std::views::values )
966 {
967 for( size_t i = 0; i < padsList.size(); ++i )
968 {
969 for( size_t j = 1; j < padsList.size(); ++j )
970 {
971 padsList[i]->Connect( padsList[j] );
972 padsList[j]->Connect( padsList[i] );
973 }
974 }
975 }
976 }
977
978 for( const std::set<wxString>& group : footprint->JumperPadGroups() )
979 {
980 std::vector<CN_ITEM*> toConnect;
981
982 for( const wxString& padNumber : group )
983 std::ranges::copy( padsMap[padNumber], std::back_inserter( toConnect ) );
984
985 for( size_t i = 0; i < toConnect.size(); ++i )
986 {
987 for( size_t j = 1; j < toConnect.size(); ++j )
988 {
989 toConnect[i]->Connect( toConnect[j] );
990 toConnect[j]->Connect( toConnect[i] );
991 }
992 }
993 }
994 }
995}
@ ZLO_FORCE_NO_ZONE_CONNECTION
Definition board_item.h:70
BOX2< VECTOR2I > BOX2I
Definition box2.h:922
A base class derived from BOARD_ITEM for items that can be connected and have a net,...
bool SetNetCode(int aNetCode, bool aNoAssert)
Set net using a net code.
A base class for any item which can be embedded within the BOARD container class, and therefore insta...
Definition board_item.h:79
virtual PCB_LAYER_ID GetLayer() const
Return the primary layer this item is on.
Definition board_item.h:232
virtual bool IsConnected() const
Returns information if the object is derived from BOARD_CONNECTED_ITEM.
Definition board_item.h:134
virtual bool IsOnLayer(PCB_LAYER_ID aLayer) const
Test to see if this object is on the given layer.
Definition board_item.h:314
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.
virtual const BOARD * GetBoard() const
Return the BOARD in which this BOARD_ITEM resides, or NULL if none.
FOOTPRINT * GetParentFootprint() const
virtual LSET GetLayerSet() const
Return a std::bitset of all layers on which the item physically resides.
Definition board_item.h:252
virtual bool IsOnCopperLayer() const
Definition board_item.h:151
Information pertinent to a Pcbnew printed circuit board.
Definition board.h:322
const ZONES & Zones() const
Definition board.h:367
const FOOTPRINTS & Footprints() const
Definition board.h:363
const TRACKS & Tracks() const
Definition board.h:361
const LSET & GetEnabledLayers() const
A proxy function that calls the corresponding function in m_BoardSettings.
Definition board.cpp:927
const DRAWINGS & Drawings() const
Definition board.h:365
constexpr bool Contains(const Vec &aPoint) const
Definition box2.h:168
constexpr bool Intersects(const BOX2< Vec > &aRect) const
Definition box2.h:311
void FillIsolatedIslandsMap(std::map< ZONE *, std::map< PCB_LAYER_ID, ISOLATED_ISLANDS > > &aMap, bool aConnectivityAlreadyRebuilt)
Fill in the isolated islands map with copper islands that are not connected to a net.
bool Remove(BOARD_ITEM *aItem)
CONNECTIVITY_DATA * m_parentConnectivityData
void add(Container &c, BItem brditem)
PROGRESS_REPORTER * m_progressReporter
std::vector< std::shared_ptr< CN_CLUSTER > > m_connClusters
void propagateConnections(BOARD_COMMIT *aCommit=nullptr)
const CLUSTERS & GetClusters()
void LocalBuild(const std::shared_ptr< CONNECTIVITY_DATA > &aGlobalConnectivity, const std::vector< BOARD_ITEM * > &aLocalItems)
const CLUSTERS SearchClusters(CLUSTER_SEARCH_MODE aMode, bool aExcludeZones, int aSingleNet)
void markItemNetAsDirty(const BOARD_ITEM *aItem)
std::vector< std::shared_ptr< CN_CLUSTER > > m_ratsnestClusters
void PropagateNets(BOARD_COMMIT *aCommit=nullptr)
Propagate nets from pads to other items in clusters.
std::shared_ptr< CONNECTIVITY_DATA > m_globalConnectivityData
std::vector< bool > m_dirtyNets
std::unordered_map< const BOARD_ITEM *, ITEM_MAP_ENTRY > m_itemMap
void SetProgressReporter(PROGRESS_REPORTER *aReporter)
std::vector< std::shared_ptr< CN_CLUSTER > > CLUSTERS
void Build(BOARD *aBoard, PROGRESS_REPORTER *aReporter=nullptr)
bool Add(BOARD_ITEM *aItem)
CN_ITEM represents a BOARD_CONNETED_ITEM in the connectivity system (ie: a pad, track/arc/via,...
void Connect(CN_ITEM *b)
const BOX2I & BBox()
virtual int AnchorCount() const
const std::vector< CN_ITEM * > & ConnectedItems() const
int Net() const
bool Valid() const
virtual const VECTOR2I GetAnchor(int n) const
bool CanChangeNet() const
bool Dirty() const
BOARD_CONNECTED_ITEM * Parent() const
void checkZoneItemConnection(CN_ZONE_LAYER *aZoneLayer, CN_ITEM *aItem)
CN_ITEM * m_item
The item we are looking for connections to.
void checkZoneZoneConnection(CN_ZONE_LAYER *aZoneLayerA, CN_ZONE_LAYER *aZoneLayerB)
bool operator()(CN_ITEM *aCandidate)
PCB_LAYER_ID GetLayer() const
bool Collide(SHAPE *aRefShape) const
int SubpolyIndex() const
bool ContainsPoint(const VECTOR2I &p) const
COMMIT & Modify(EDA_ITEM *aItem, BASE_SCREEN *aScreen=nullptr, RECURSE_MODE aRecurse=RECURSE_MODE::NO_RECURSE)
Modify a given item in the model.
Definition commit.h:106
KICAD_T Type() const
Returns the type of object.
Definition eda_item.h:110
std::deque< PAD * > & Pads()
Definition footprint.h:224
LSET is a set of PCB_LAYER_IDs.
Definition lset.h:37
void RunOnLayers(const std::function< void(PCB_LAYER_ID)> &aFunction) const
Execute a function on each layer of the LSET.
Definition lset.h:252
static LSET AllCuMask()
return AllCuMask( MAX_CU_LAYERS );
Definition lset.cpp:591
Handle the data for a net.
Definition netinfo.h:54
Definition pad.h:54
A small class to help profiling.
Definition profile.h:49
void Show(std::ostream &aStream=std::cerr)
Print the elapsed time (in a suitable unit) to a stream.
Definition profile.h:105
A progress reporter interface for use in multi-threaded environments.
virtual bool IsCancelled() const =0
virtual bool KeepRefreshing(bool aWait=false)=0
Update the UI (if any).
virtual void AdvanceProgress()=0
Increment the progress bar length (inside the current virtual zone).
virtual void SetCurrentProgress(double aProgress)=0
Set the progress value to aProgress (0..1).
Represent a polyline containing arcs as well as line segments: A chain of connected line and/or arc s...
int PointCount() const
Return the number of points (vertices) in this line chain.
const VECTOR2I & CPoint(int aIndex) const
Return a reference to a given point in the line chain.
const SHAPE_LINE_CHAIN & COutline(int aIndex) const
virtual bool Collide(const VECTOR2I &aP, int aClearance=0, int *aActual=nullptr, VECTOR2I *aLocation=nullptr) const
Check if the boundary of shape (this) lies closer to the point aP than aClearance,...
Definition shape.h:181
Handle a list of polygons defining a copper zone.
Definition zone.h:74
const std::shared_ptr< SHAPE_POLY_SET > & GetFilledPolysList(PCB_LAYER_ID aLayer) const
Definition zone.h:600
virtual LSET GetLayerSet() const override
Return a std::bitset of all layers on which the item physically resides.
Definition zone.h:136
@ FP_JUST_ADDED
Definition footprint.h:86
a few functions useful in geometry calculations.
FLASHING
Enum used during connectivity building to ensure we do not query connectivity while building the data...
Definition layer_ids.h:184
@ NEVER_FLASHED
Never flashed for connectivity.
Definition layer_ids.h:187
@ ALWAYS_FLASHED
Always flashed for connectivity.
Definition layer_ids.h:186
bool IsCopperLayer(int aLayerId)
Test whether a layer is a copper layer.
Definition layer_ids.h:677
PCB_LAYER_ID
A quick note on layer IDs:
Definition layer_ids.h:60
thread_pool & GetKiCadThreadPool()
Get a reference to the current thread pool.
static thread_pool * tp
BS::thread_pool< 0 > thread_pool
Definition thread_pool.h:31
@ PCB_SHAPE_T
class PCB_SHAPE, a segment not on copper layers
Definition typeinfo.h:88
@ PCB_VIA_T
class PCB_VIA, a via (like a track segment on a copper layer)
Definition typeinfo.h:97
@ PCB_ZONE_T
class ZONE, a copper pour area
Definition typeinfo.h:108
@ 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:98
@ PCB_NETINFO_T
class NETINFO_ITEM, a description of a net
Definition typeinfo.h:110
@ PCB_TRACE_T
class PCB_TRACK, a track segment (segment on a copper layer)
Definition typeinfo.h:96