KiCad PCB EDA Suite
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages Concepts
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 <tomasz.wlostowski@cern.ch>
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
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
239
240 if( m_isLocal )
241 m_globalConnectivityData->RemoveInvalidRefs();
242
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
266 return;
267 }
268
269 if( m_itemList.IsDirty() )
270 {
271 std::vector<std::future<size_t>> returns( dirtyItems.size() );
272
273 auto conn_lambda =
274 [&dirtyItems]( size_t aItem, CN_LIST* aItemList,
275 PROGRESS_REPORTER* aReporter) -> size_t
276 {
277 if( aReporter && aReporter->IsCancelled() )
278 return 0;
279
280 CN_VISITOR visitor( dirtyItems[aItem] );
281 aItemList->FindNearby( dirtyItems[aItem], visitor );
282
283 if( aReporter )
284 aReporter->AdvanceProgress();
285
286 return 1;
287 };
288
289 for( size_t ii = 0; ii < dirtyItems.size(); ++ii )
290 returns[ii] = tp.submit( conn_lambda, ii, &m_itemList, m_progressReporter );
291
292 for( const std::future<size_t>& ret : returns )
293 {
294 // Here we balance returns with a 250ms timeout to allow UI updating
295 std::future_status status = ret.wait_for( std::chrono::milliseconds( 250 ) );
296
297 while( status != std::future_status::ready )
298 {
301
302 status = ret.wait_for( std::chrono::milliseconds( 250 ) );
303 }
304 }
305
308 }
309
310#ifdef PROFILE
311 search_basic.Show();
312#endif
313
315}
316
317
319{
320 return SearchClusters( aMode, ( aMode == CSM_PROPAGATE ), -1 );
321}
322
323
325CN_CONNECTIVITY_ALGO::SearchClusters( CLUSTER_SEARCH_MODE aMode, bool aExcludeZones, int aSingleNet )
326{
327 bool withinAnyNet = ( aMode != CSM_PROPAGATE );
328
329 std::deque<CN_ITEM*> Q;
330 std::set<CN_ITEM*> item_set;
331
332 CLUSTERS clusters;
333
334 if( m_itemList.IsDirty() )
336
337 std::set<CN_ITEM*> visited;
338
339 auto addToSearchList =
340 [&item_set, withinAnyNet, aSingleNet, &aExcludeZones]( CN_ITEM *aItem )
341 {
342 if( withinAnyNet && aItem->Net() <= 0 )
343 return;
344
345 if( !aItem->Valid() )
346 return;
347
348 if( aSingleNet >=0 && aItem->Net() != aSingleNet )
349 return;
350
351 if( aExcludeZones && aItem->Parent()->Type() == PCB_ZONE_T )
352 return;
353
354 item_set.insert( aItem );
355 };
356
357 std::for_each( m_itemList.begin(), m_itemList.end(), addToSearchList );
358
360 return CLUSTERS();
361
362 while( !item_set.empty() )
363 {
364 std::shared_ptr<CN_CLUSTER> cluster = std::make_shared<CN_CLUSTER>();
365 CN_ITEM* root;
366 auto it = item_set.begin();
367
368 while( it != item_set.end() && visited.contains( *it ) )
369 it = item_set.erase( item_set.begin() );
370
371 if( it == item_set.end() )
372 break;
373
374 root = *it;
375 visited.insert( root );
376
377 Q.clear();
378 Q.push_back( root );
379
380 while( Q.size() )
381 {
382 CN_ITEM* current = Q.front();
383
384 Q.pop_front();
385 cluster->Add( current );
386
387 for( CN_ITEM* n : current->ConnectedItems() )
388 {
389 if( withinAnyNet && n->Net() != root->Net() )
390 continue;
391
392 if( aExcludeZones && n->Parent()->Type() == PCB_ZONE_T )
393 continue;
394
395 if( !visited.contains( n ) && n->Valid() )
396 {
397 visited.insert( n );
398 Q.push_back( n );
399 }
400 }
401 }
402
403 clusters.push_back( cluster );
404 }
405
407 return CLUSTERS();
408
409 std::sort( clusters.begin(), clusters.end(),
410 []( const std::shared_ptr<CN_CLUSTER>& a, const std::shared_ptr<CN_CLUSTER>& b )
411 {
412 return a->OriginNet() < b->OriginNet();
413 } );
414
415 return clusters;
416}
417
418
420{
421 // Generate CN_ZONE_LAYERs for each island on each layer of each zone
422 //
423 std::vector<CN_ZONE_LAYER*> zitems;
424
425 for( ZONE* zone : aBoard->Zones() )
426 {
427 if( zone->IsOnCopperLayer() )
428 {
429 m_itemMap[zone] = ITEM_MAP_ENTRY();
430 markItemNetAsDirty( zone );
431
432 // Don't check for connections on layers that only exist in the zone but
433 // were disabled in the board
434 BOARD* board = zone->GetBoard();
435 LSET layerset = board->GetEnabledLayers() & zone->GetLayerSet() & LSET::AllCuMask();
436
437 layerset.RunOnLayers(
438 [&]( PCB_LAYER_ID layer )
439 {
440 for( int j = 0; j < zone->GetFilledPolysList( layer )->OutlineCount(); j++ )
441 zitems.push_back( new CN_ZONE_LAYER( zone, layer, j ) );
442 } );
443 }
444 }
445
446 // Setup progress metrics
447 //
448 int progressDelta = 50;
449 double size = 0.0;
450
451 size += zitems.size(); // Once for building RTrees
452 size += zitems.size(); // Once for adding to connectivity
453 size += aBoard->Tracks().size();
454 size += aBoard->Drawings().size();
455
456 for( FOOTPRINT* footprint : aBoard->Footprints() )
457 size += footprint->Pads().size();
458
459 size *= 1.5; // Our caller gets the other third of the progress bar
460
461 progressDelta = std::max( progressDelta, (int) size / 4 );
462
463 auto report =
464 [&]( int progress )
465 {
466 if( aReporter && ( progress % progressDelta ) == 0 )
467 {
468 aReporter->SetCurrentProgress( progress / size );
469 aReporter->KeepRefreshing( false );
470 }
471 };
472
473 // Generate RTrees for CN_ZONE_LAYER items (in parallel)
474 //
476 std::vector<std::future<size_t>> returns( zitems.size() );
477
478 auto cache_zones =
479 [aReporter]( CN_ZONE_LAYER* aZoneLayer ) -> size_t
480 {
481 if( aReporter && aReporter->IsCancelled() )
482 return 0;
483
484 aZoneLayer->BuildRTree();
485
486 if( aReporter )
487 aReporter->AdvanceProgress();
488
489 return 1;
490 };
491
492 for( size_t ii = 0; ii < zitems.size(); ++ii )
493 returns[ii] = tp.submit( cache_zones, zitems[ii] );
494
495 for( const std::future<size_t>& ret : returns )
496 {
497 std::future_status status = ret.wait_for( std::chrono::milliseconds( 250 ) );
498
499 while( status != std::future_status::ready )
500 {
501 if( aReporter )
502 aReporter->KeepRefreshing();
503
504 status = ret.wait_for( std::chrono::milliseconds( 250 ) );
505 }
506
507 }
508
509 // Add CN_ZONE_LAYERS, tracks, and pads to connectivity
510 //
511 int ii = zitems.size();
512
513 for( CN_ZONE_LAYER* zitem : zitems )
514 {
515 m_itemList.Add( zitem );
516 m_itemMap[ zitem->Parent() ].Link( zitem );
517 report( ++ii );
518 }
519
520 for( PCB_TRACK* tv : aBoard->Tracks() )
521 {
522 Add( tv );
523 report( ++ii );
524 }
525
526 for( FOOTPRINT* footprint : aBoard->Footprints() )
527 {
528 for( PAD* pad : footprint->Pads() )
529 {
530 Add( pad );
531 report( ++ii );
532 }
533 }
534
535 for( BOARD_ITEM* drawing : aBoard->Drawings() )
536 {
537 if( PCB_SHAPE* shape = dynamic_cast<PCB_SHAPE*>( drawing ) )
538 {
539 if( shape->IsOnCopperLayer() )
540 Add( shape );
541 }
542
543 report( ++ii );
544 }
545
546 if( aReporter )
547 {
548 aReporter->SetCurrentProgress( (double) ii / (double) size );
549 aReporter->KeepRefreshing( false );
550 }
551}
552
553
554void CN_CONNECTIVITY_ALGO::LocalBuild( std::shared_ptr<CONNECTIVITY_DATA> aGlobalConnectivity,
555 const std::vector<BOARD_ITEM*>& aLocalItems )
556{
557 m_isLocal = true;
558 m_globalConnectivityData = aGlobalConnectivity;
559
560 for( BOARD_ITEM* item : aLocalItems )
561 {
562 switch( item->Type() )
563 {
564 case PCB_TRACE_T:
565 case PCB_ARC_T:
566 case PCB_VIA_T:
567 case PCB_PAD_T:
568 case PCB_FOOTPRINT_T:
569 case PCB_SHAPE_T:
570 Add( item );
571 break;
572
573 default:
574 break;
575 }
576 }
577}
578
579
581{
582 for( const std::shared_ptr<CN_CLUSTER>& cluster : m_connClusters )
583 {
584 if( cluster->IsConflicting() )
585 {
586 // Conflicting pads in cluster: we don't know the user's intent so best to do
587 // nothing.
588 wxLogTrace( wxT( "CN" ), wxT( "Conflicting pads in cluster %p; skipping propagation" ),
589 cluster.get() );
590 }
591 else if( cluster->HasValidNet() )
592 {
593 // Propagate from the origin (will be a pad if there are any, or another item if
594 // there are no pads).
595 int n_changed = 0;
596
597 for( CN_ITEM* item : *cluster )
598 {
599 if( item->Valid() && item->CanChangeNet()
600 && item->Parent()->GetNetCode() != cluster->OriginNet() )
601 {
602 MarkNetAsDirty( item->Parent()->GetNetCode() );
603 MarkNetAsDirty( cluster->OriginNet() );
604
605 if( aCommit )
606 aCommit->Modify( item->Parent() );
607
608 item->Parent()->SetNetCode( cluster->OriginNet() );
609 n_changed++;
610 }
611 }
612
613 if( n_changed )
614 {
615 wxLogTrace( wxT( "CN" ), wxT( "Cluster %p: net: %d %s" ),
616 cluster.get(),
617 cluster->OriginNet(),
618 (const char*) cluster->OriginNetName().c_str() );
619 }
620 else
621 {
622 wxLogTrace( wxT( "CN" ), wxT( "Cluster %p: no changeable items to propagate to" ),
623 cluster.get() );
624 }
625 }
626 else
627 {
628 wxLogTrace( wxT( "CN" ), wxT( "Cluster %p: connected to unused net" ),
629 cluster.get() );
630 }
631 }
632}
633
634
636{
639 propagateConnections( aCommit );
640}
641
642
644 std::map<ZONE*, std::map<PCB_LAYER_ID, ISOLATED_ISLANDS>>& aMap,
645 bool aConnectivityAlreadyRebuilt )
646{
647 int progressDelta = 50;
648 int ii = 0;
649
650 progressDelta = std::max( progressDelta, (int) aMap.size() / 4 );
651
652 if( !aConnectivityAlreadyRebuilt )
653 {
654 for( const auto& [ zone, islands ] : aMap )
655 {
656 Remove( zone );
657 Add( zone );
658 ii++;
659
660 if( m_progressReporter && ( ii % progressDelta ) == 0 )
661 {
662 m_progressReporter->SetCurrentProgress( (double) ii / (double) aMap.size() );
664 }
665
667 return;
668 }
669 }
670
672
673 for( auto& [ zone, zoneIslands ] : aMap )
674 {
675 for( auto& [ layer, layerIslands ] : zoneIslands )
676 {
677 if( zone->GetFilledPolysList( layer )->IsEmpty() )
678 continue;
679
680 for( const std::shared_ptr<CN_CLUSTER>& cluster : m_connClusters )
681 {
682 for( CN_ITEM* item : *cluster )
683 {
684 if( item->Parent() == zone && item->GetBoardLayer() == layer )
685 {
686 CN_ZONE_LAYER* z = static_cast<CN_ZONE_LAYER*>( item );
687
688 if( cluster->IsOrphaned() )
689 layerIslands.m_IsolatedOutlines.push_back( z->SubpolyIndex() );
690 else if( z->HasSingleConnection() )
691 layerIslands.m_SingleConnectionOutlines.push_back( z->SubpolyIndex() );
692 }
693 }
694 }
695 }
696 }
697}
698
699
701{
703 return m_ratsnestClusters;
704}
705
706
708{
709 if( aNet < 0 )
710 return;
711
712 if( (int) m_dirtyNets.size() <= aNet )
713 {
714 int lastNet = m_dirtyNets.size() - 1;
715
716 if( lastNet < 0 )
717 lastNet = 0;
718
719 m_dirtyNets.resize( aNet + 1 );
720
721 for( int i = lastNet; i < aNet + 1; i++ )
722 m_dirtyNets[i] = true;
723 }
724
725 m_dirtyNets[aNet] = true;
726}
727
728
730{
731 PCB_LAYER_ID layer = aZoneLayer->GetLayer();
732 BOARD_CONNECTED_ITEM* item = aItem->Parent();
733
734 if( !item->IsOnLayer( layer ) )
735 return;
736
737 auto connect =
738 [&]()
739 {
740 aZoneLayer->Connect( aItem );
741 aItem->Connect( aZoneLayer );
742 };
743
744 // Try quick checks first...
745 if( item->Type() == PCB_PAD_T )
746 {
747 PAD* pad = static_cast<PAD*>( item );
748
749 if( pad->ConditionallyFlashed( layer )
750 && pad->GetZoneLayerOverride( layer ) == ZLO_FORCE_NO_ZONE_CONNECTION )
751 {
752 return;
753 }
754 }
755 else if( item->Type() == PCB_VIA_T )
756 {
757 PCB_VIA* via = static_cast<PCB_VIA*>( item );
758
759 if( via->ConditionallyFlashed( layer )
760 && via->GetZoneLayerOverride( layer ) == ZLO_FORCE_NO_ZONE_CONNECTION )
761 {
762 return;
763 }
764 }
765
766 for( int i = 0; i < aItem->AnchorCount(); ++i )
767 {
768 if( aZoneLayer->ContainsPoint( aItem->GetAnchor( i ) ) )
769 {
770 connect();
771 return;
772 }
773 }
774
775 if( item->Type() == PCB_VIA_T || item->Type() == PCB_PAD_T )
776 {
777 // As long as the pad/via crosses the zone layer, check for the full effective shape
778 // We check for the overlapping layers above
779 if( aZoneLayer->Collide( item->GetEffectiveShape( layer, FLASHING::ALWAYS_FLASHED ).get() ) )
780 connect();
781
782 return;
783 }
784
785 if( aZoneLayer->Collide( item->GetEffectiveShape( layer ).get() ) )
786 connect();
787}
788
790{
791 const ZONE* zoneA = static_cast<const ZONE*>( aZoneLayerA->Parent() );
792 const ZONE* zoneB = static_cast<const ZONE*>( aZoneLayerB->Parent() );
793
794 const BOX2I& boxA = aZoneLayerA->BBox();
795 const BOX2I& boxB = aZoneLayerB->BBox();
796
797 PCB_LAYER_ID layer = aZoneLayerA->GetLayer();
798
799 if( aZoneLayerB->GetLayer() != layer )
800 return;
801
802 if( !boxA.Intersects( boxB ) )
803 return;
804
805 const SHAPE_LINE_CHAIN& outline =
806 zoneA->GetFilledPolysList( layer )->COutline( aZoneLayerA->SubpolyIndex() );
807
808 for( int i = 0; i < outline.PointCount(); i++ )
809 {
810 if( !boxB.Contains( outline.CPoint( i ) ) )
811 continue;
812
813 if( aZoneLayerB->ContainsPoint( outline.CPoint( i ) ) )
814 {
815 aZoneLayerA->Connect( aZoneLayerB );
816 aZoneLayerB->Connect( aZoneLayerA );
817 return;
818 }
819 }
820
821 const SHAPE_LINE_CHAIN& outline2 =
822 zoneB->GetFilledPolysList( layer )->COutline( aZoneLayerB->SubpolyIndex() );
823
824 for( int i = 0; i < outline2.PointCount(); i++ )
825 {
826 if( !boxA.Contains( outline2.CPoint( i ) ) )
827 continue;
828
829 if( aZoneLayerA->ContainsPoint( outline2.CPoint( i ) ) )
830 {
831 aZoneLayerA->Connect( aZoneLayerB );
832 aZoneLayerB->Connect( aZoneLayerA );
833 return;
834 }
835 }
836}
837
838
840{
841 const BOARD_CONNECTED_ITEM* parentA = aCandidate->Parent();
842 const BOARD_CONNECTED_ITEM* parentB = m_item->Parent();
843
844 if( !aCandidate->Valid() || !m_item->Valid() )
845 return true;
846
847 if( parentA == parentB )
848 return true;
849
850 // Don't connect items in different nets that can't be changed
851 if( !aCandidate->CanChangeNet() && !m_item->CanChangeNet() && aCandidate->Net() != m_item->Net() )
852 return true;
853
854 // If both m_item and aCandidate are marked dirty, they will both be searched
855 // Since we are reciprocal in our connection, we arbitrarily pick one of the connections
856 // to conduct the expensive search
857 if( aCandidate->Dirty() && aCandidate < m_item )
858 return true;
859
860 // We should handle zone-zone connection separately
861 if ( parentA->Type() == PCB_ZONE_T && parentB->Type() == PCB_ZONE_T )
862 {
864 static_cast<CN_ZONE_LAYER*>( aCandidate ) );
865 return true;
866 }
867
868 if( parentA->Type() == PCB_ZONE_T )
869 {
870 checkZoneItemConnection( static_cast<CN_ZONE_LAYER*>( aCandidate ), m_item );
871 return true;
872 }
873
874 if( parentB->Type() == PCB_ZONE_T )
875 {
876 checkZoneItemConnection( static_cast<CN_ZONE_LAYER*>( m_item ), aCandidate );
877 return true;
878 }
879
880 LSET commonLayers = parentA->GetLayerSet() & parentB->GetLayerSet();
881
882 for( size_t ii = 0; ii < commonLayers.size(); ++ii )
883 {
884 if( commonLayers.test( ii ) )
885 {
886 PCB_LAYER_ID layer = PCB_LAYER_ID( ii );
887 FLASHING flashingA = FLASHING::NEVER_FLASHED;
888 FLASHING flashingB = FLASHING::NEVER_FLASHED;
889
890 if( parentA->Type() == PCB_PAD_T )
891 {
892 if( !static_cast<const PAD*>( parentA )->ConditionallyFlashed( layer ) )
893 flashingA = FLASHING::ALWAYS_FLASHED;
894 }
895 else if( parentA->Type() == PCB_VIA_T )
896 {
897 if( !static_cast<const PCB_VIA*>( parentA )->ConditionallyFlashed( layer ) )
898 flashingA = FLASHING::ALWAYS_FLASHED;
899 }
900
901 if( parentB->Type() == PCB_PAD_T )
902 {
903 if( !static_cast<const PAD*>( parentB )->ConditionallyFlashed( layer ) )
904 flashingB = FLASHING::ALWAYS_FLASHED;
905 }
906 else if( parentB->Type() == PCB_VIA_T )
907 {
908 if( !static_cast<const PCB_VIA*>( parentB )->ConditionallyFlashed( layer ) )
909 flashingB = FLASHING::ALWAYS_FLASHED;
910 }
911
912 if( parentA->GetEffectiveShape( layer, flashingA )->Collide(
913 parentB->GetEffectiveShape( layer, flashingB ).get() ) )
914 {
915 m_item->Connect( aCandidate );
916 aCandidate->Connect( m_item );
917 return true;
918 }
919 }
920 }
921
922 return true;
923};
924
925
927{
928 m_ratsnestClusters.clear();
929 m_connClusters.clear();
930 m_itemMap.clear();
932
933}
934
936{
937 m_progressReporter = aReporter;
938}
939
940
942{
943 // Map of footprint -> map of pad number -> list of CN_ITEMs for pads with that number
944 std::map<FOOTPRINT*, std::map<wxString, std::vector<CN_ITEM*>>> padsByFootprint;
945
946 for( CN_ITEM* item : m_itemList )
947 {
948 if( item->Parent()->Type() != PCB_PAD_T )
949 continue;
950
951 auto pad = static_cast<const PAD*>( item->Parent() );
952
953 FOOTPRINT* fp = pad->GetParentFootprint();
954
955 padsByFootprint[fp][ pad->GetNumber() ].emplace_back( item );
956 }
957
958 for( auto& [footprint, padsMap] : padsByFootprint )
959 {
960 if( footprint->GetDuplicatePadNumbersAreJumpers() )
961 {
962 for( const std::vector<CN_ITEM*>& padsList : padsMap | std::views::values )
963 {
964 for( size_t i = 0; i < padsList.size(); ++i )
965 {
966 for( size_t j = 1; j < padsList.size(); ++j )
967 {
968 padsList[i]->Connect( padsList[j] );
969 padsList[j]->Connect( padsList[i] );
970 }
971 }
972 }
973 }
974
975 for( const std::set<wxString>& group : footprint->JumperPadGroups() )
976 {
977 std::vector<CN_ITEM*> toConnect;
978
979 for( const wxString& padNumber : group )
980 std::ranges::copy( padsMap[padNumber], std::back_inserter( toConnect ) );
981
982 for( size_t i = 0; i < toConnect.size(); ++i )
983 {
984 for( size_t j = 1; j < toConnect.size(); ++j )
985 {
986 toConnect[i]->Connect( toConnect[j] );
987 toConnect[j]->Connect( toConnect[i] );
988 }
989 }
990 }
991 }
992}
@ ZLO_FORCE_NO_ZONE_CONNECTION
Definition: board_item.h:69
A base class derived from BOARD_ITEM for items that can be connected and have a net,...
A base class for any item which can be embedded within the BOARD container class, and therefore insta...
Definition: board_item.h:78
virtual PCB_LAYER_ID GetLayer() const
Return the primary layer this item is on.
Definition: board_item.h:229
virtual bool IsConnected() const
Returns information if the object is derived from BOARD_CONNECTED_ITEM.
Definition: board_item.h:131
virtual bool IsOnLayer(PCB_LAYER_ID aLayer) const
Test to see if this object is on the given layer.
Definition: board_item.h:311
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:279
virtual const BOARD * GetBoard() const
Return the BOARD in which this BOARD_ITEM resides, or NULL if none.
Definition: board_item.cpp:48
FOOTPRINT * GetParentFootprint() const
Definition: board_item.cpp:299
virtual LSET GetLayerSet() const
Return a std::bitset of all layers on which the item physically resides.
Definition: board_item.h:249
virtual bool IsOnCopperLayer() const
Definition: board_item.h:148
Information pertinent to a Pcbnew printed circuit board.
Definition: board.h:297
const ZONES & Zones() const
Definition: board.h:342
const FOOTPRINTS & Footprints() const
Definition: board.h:338
const TRACKS & Tracks() const
Definition: board.h:336
const LSET & GetEnabledLayers() const
A proxy function that calls the corresponding function in m_BoardSettings.
Definition: board.cpp:829
const DRAWINGS & Drawings() const
Definition: board.h:340
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 LocalBuild(std::shared_ptr< CONNECTIVITY_DATA > aGlobalConnectivity, const std::vector< BOARD_ITEM * > &aLocalItems)
void propagateConnections(BOARD_COMMIT *aCommit=nullptr)
const CLUSTERS & GetClusters()
void MarkNetAsDirty(int aNet)
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::vector< std::shared_ptr< CN_CLUSTER > > CLUSTERS
std::unordered_map< const BOARD_ITEM *, ITEM_MAP_ENTRY > m_itemMap
void SetProgressReporter(PROGRESS_REPORTER *aReporter)
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
CN_ITEM * Add(PAD *pad)
std::vector< CN_ITEM * >::iterator begin()
void SetDirty(bool aDirty=true)
bool IsDirty() const
std::vector< CN_ITEM * >::iterator end()
void ClearDirtyFlags()
void SetHasInvalid(bool aInvalid=true)
void RemoveInvalidItems(std::vector< CN_ITEM * > &aGarbage)
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)
Modify a given item in the model.
Definition: commit.h:108
KICAD_T Type() const
Returns the type of object.
Definition: eda_item.h:108
std::deque< PAD * > & Pads()
Definition: footprint.h:211
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:255
static LSET AllCuMask(int aCuLayerCount=MAX_CU_LAYERS)
Return a mask holding the requested number of Cu PCB_LAYER_IDs.
Definition: lset.cpp:572
Handle the data for a net.
Definition: netinfo.h:56
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).
virtual void SetMaxProgress(int aMaxProgress)=0
Fix the value that gives the 100 percent progress bar length (inside the current virtual zone).
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.
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:648
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:85
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
bool IsCopperLayer(int aLayerId)
Test whether a layer is a copper layer.
Definition: layer_ids.h:663
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.
Definition: thread_pool.cpp:30
static thread_pool * tp
Definition: thread_pool.cpp:28
BS::thread_pool 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:107
@ 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:109
@ PCB_TRACE_T
class PCB_TRACK, a track segment (segment on a copper layer)
Definition: typeinfo.h:96