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#include <ranges>
32
34#include <progress_reporter.h>
36#include <board_commit.h>
37#include <thread_pool.h>
38#include <pcb_shape.h>
39
40#include <wx/log.h>
41
42#ifdef PROFILE
43#include <core/profile.h>
44#endif
45
46
48{
49 markItemNetAsDirty( aItem );
50
51 switch( aItem->Type() )
52 {
53 case PCB_FOOTPRINT_T:
54 for( PAD* pad : static_cast<FOOTPRINT*>( aItem )->Pads() )
55 {
56 m_itemMap[pad].MarkItemsAsInvalid();
57 m_itemMap.erase( pad );
58 }
59
60 m_itemList.SetDirty( true );
61 break;
62
63 case PCB_PAD_T:
64 case PCB_TRACE_T:
65 case PCB_ARC_T:
66 case PCB_VIA_T:
67 case PCB_ZONE_T:
68 case PCB_SHAPE_T:
69 m_itemMap[aItem].MarkItemsAsInvalid();
70 m_itemMap.erase ( aItem );
71 m_itemList.SetDirty( true );
72 break;
73
74 default:
75 return false;
76 }
77
78 // Once we delete an item, it may connect between lists, so mark both as potentially invalid
79 m_itemList.SetHasInvalid( true );
80
81 return true;
82}
83
84
86{
87 if( aItem->IsConnected() )
88 {
89 const BOARD_CONNECTED_ITEM* citem = static_cast<const BOARD_CONNECTED_ITEM*>( aItem );
90 MarkNetAsDirty( citem->GetNetCode() );
91 }
92 else
93 {
94 if( aItem->Type() == PCB_FOOTPRINT_T )
95 {
96 const FOOTPRINT* footprint = static_cast<const FOOTPRINT*>( aItem );
97
98 for( PAD* pad : footprint->Pads() )
99 MarkNetAsDirty( pad->GetNetCode() );
100 }
101 }
102}
103
104
106{
107 if( !aItem->IsOnCopperLayer() )
108 return false;
109
110 auto alreadyAdded =
111 [this]( BOARD_ITEM* item )
112 {
113 auto it = m_itemMap.find( item );
114
115 if( it == m_itemMap.end() )
116 return false;
117
118 // Don't be fooled by an empty ITEM_MAP_ENTRY auto-created by operator[].
119 return !it->second.GetItems().empty();
120 };
121
122 switch( aItem->Type() )
123 {
124 case PCB_NETINFO_T:
125 MarkNetAsDirty( static_cast<NETINFO_ITEM*>( aItem )->GetNetCode() );
126 break;
127
128 case PCB_FOOTPRINT_T:
129 {
130 if( static_cast<FOOTPRINT*>( aItem )->GetAttributes() & FP_JUST_ADDED )
131 return false;
132
133 for( PAD* pad : static_cast<FOOTPRINT*>( aItem )->Pads() )
134 {
135 if( alreadyAdded( pad ) )
136 return false;
137
138 add( m_itemList, pad );
139 }
140
141 break;
142 }
143
144 case PCB_PAD_T:
145 {
146 if( FOOTPRINT* fp = aItem->GetParentFootprint() )
147 {
148 if( fp->GetAttributes() & FP_JUST_ADDED )
149 return false;
150 }
151
152 if( alreadyAdded( aItem ) )
153 return false;
154
155 add( m_itemList, static_cast<PAD*>( aItem ) );
156 break;
157 }
158
159 case PCB_TRACE_T:
160 if( alreadyAdded( aItem ) )
161 return false;
162
163 add( m_itemList, static_cast<PCB_TRACK*>( aItem ) );
164 break;
165
166 case PCB_ARC_T:
167 if( alreadyAdded( aItem ) )
168 return false;
169
170 add( m_itemList, static_cast<PCB_ARC*>( aItem ) );
171 break;
172
173 case PCB_VIA_T:
174 if( alreadyAdded( aItem ) )
175 return false;
176
177 add( m_itemList, static_cast<PCB_VIA*>( aItem ) );
178 break;
179
180 case PCB_SHAPE_T:
181 if( alreadyAdded( aItem ) )
182 return false;
183
184 if( !IsCopperLayer( aItem->GetLayer() ) )
185 return false;
186
187 add( m_itemList, static_cast<PCB_SHAPE*>( aItem ) );
188 break;
189
190 case PCB_ZONE_T:
191 {
192 ZONE* zone = static_cast<ZONE*>( aItem );
193
194 if( alreadyAdded( aItem ) )
195 return false;
196
197 m_itemMap[zone] = ITEM_MAP_ENTRY();
198
199 // Don't check for connections on layers that only exist in the zone but
200 // were disabled in the board
201 BOARD* board = zone->GetBoard();
202 LSET layerset = board->GetEnabledLayers() & zone->GetLayerSet();
203
204 layerset.RunOnLayers(
205 [&]( PCB_LAYER_ID layer )
206 {
207 for( CN_ITEM* zitem : m_itemList.Add( zone, layer ) )
208 m_itemMap[zone].Link( zitem );
209 } );
210
211 break;
212 }
213
214 default:
215 return false;
216 }
217
218 markItemNetAsDirty( aItem );
219
220 return true;
221}
222
223
225{
226 for( CN_ITEM* item : m_itemList )
227 item->RemoveInvalidRefs();
228}
229
230
232{
233 std::lock_guard lock( m_mutex );
234#ifdef PROFILE
235 PROF_TIMER garbage_collection( "garbage-collection" );
236#endif
237 std::vector<CN_ITEM*> garbage;
238 garbage.reserve( 1024 );
239
240 m_parentConnectivityData->RemoveInvalidRefs();
241
242 if( m_isLocal )
243 m_globalConnectivityData->RemoveInvalidRefs();
244
245 m_itemList.RemoveInvalidItems( garbage );
246
247 for( CN_ITEM* item : garbage )
248 delete item;
249
250#ifdef PROFILE
251 garbage_collection.Show();
252 PROF_TIMER search_basic( "search-basic" );
253#endif
254
256 std::vector<CN_ITEM*> dirtyItems;
257 std::copy_if( m_itemList.begin(), m_itemList.end(), std::back_inserter( dirtyItems ),
258 [] ( CN_ITEM* aItem )
259 {
260 return aItem->Dirty();
261 } );
262
264 {
265 m_progressReporter->SetMaxProgress( dirtyItems.size() );
266
267 if( !m_progressReporter->KeepRefreshing() )
268 return;
269 }
270
271 if( m_itemList.IsDirty() )
272 {
273 std::vector<std::future<size_t>> returns( dirtyItems.size() );
274
275 // Collect deferred net code changes to avoid data races in parallel search.
276 // Free vias connected to zones have their net codes updated after all parallel
277 // work completes.
278 std::vector<std::pair<BOARD_CONNECTED_ITEM*, int>> deferredNetCodes;
279 std::mutex deferredNetCodesMutex;
280
281 for( size_t ii = 0; ii < dirtyItems.size(); ++ii )
282 {
283 returns[ii] = tp.submit_task(
284 [&dirtyItems, ii, this, &deferredNetCodes, &deferredNetCodesMutex] () ->size_t
285 {
286 if( m_progressReporter && m_progressReporter->IsCancelled() )
287 return 0;
288
289 CN_VISITOR visitor( dirtyItems[ii], &deferredNetCodes, &deferredNetCodesMutex );
290 m_itemList.FindNearby( dirtyItems[ii], visitor );
291
293 m_progressReporter->AdvanceProgress();
294
295 return 1;
296 } );
297 }
298
299 for( const std::future<size_t>& ret : returns )
300 {
301 // Here we balance returns with a 250ms timeout to allow UI updating
302 std::future_status status = ret.wait_for( std::chrono::milliseconds( 250 ) );
303
304 while( status != std::future_status::ready )
305 {
307 m_progressReporter->KeepRefreshing();
308
309 status = ret.wait_for( std::chrono::milliseconds( 250 ) );
310 }
311 }
312
313 // Apply deferred net code changes now that parallel search is complete
314 for( const auto& [item, netCode] : deferredNetCodes )
315 item->SetNetCode( netCode );
316
318 m_progressReporter->KeepRefreshing();
319 }
320
321#ifdef PROFILE
322 search_basic.Show();
323#endif
324
325 m_itemList.ClearDirtyFlags();
326}
327
328
333
334
336CN_CONNECTIVITY_ALGO::SearchClusters( CLUSTER_SEARCH_MODE aMode, bool aExcludeZones, int aSingleNet )
337{
338 bool withinAnyNet = ( aMode != CSM_PROPAGATE );
339
340 std::deque<CN_ITEM*> Q;
341 std::set<CN_ITEM*> item_set;
342
343 CLUSTERS clusters;
344
345 if( m_itemList.IsDirty() )
347
348 std::set<CN_ITEM*> visited;
349
350 auto addToSearchList =
351 [&item_set, withinAnyNet, aSingleNet, &aExcludeZones]( CN_ITEM *aItem )
352 {
353 if( withinAnyNet && aItem->Net() <= 0 )
354 return;
355
356 if( !aItem->Valid() )
357 return;
358
359 if( aSingleNet >=0 && aItem->Net() != aSingleNet )
360 return;
361
362 if( aExcludeZones && aItem->Parent()->Type() == PCB_ZONE_T )
363 return;
364
365 item_set.insert( aItem );
366 };
367
368 std::for_each( m_itemList.begin(), m_itemList.end(), addToSearchList );
369
370 if( m_progressReporter && m_progressReporter->IsCancelled() )
371 return CLUSTERS();
372
373 while( !item_set.empty() )
374 {
375 std::shared_ptr<CN_CLUSTER> cluster = std::make_shared<CN_CLUSTER>();
376 CN_ITEM* root;
377 auto it = item_set.begin();
378
379 while( it != item_set.end() && visited.contains( *it ) )
380 it = item_set.erase( item_set.begin() );
381
382 if( it == item_set.end() )
383 break;
384
385 root = *it;
386 visited.insert( root );
387
388 Q.clear();
389 Q.push_back( root );
390
391 while( Q.size() )
392 {
393 CN_ITEM* current = Q.front();
394
395 Q.pop_front();
396 cluster->Add( current );
397
398 for( CN_ITEM* n : current->ConnectedItems() )
399 {
400 if( withinAnyNet && n->Net() != root->Net() )
401 continue;
402
403 if( aExcludeZones && n->Parent()->Type() == PCB_ZONE_T )
404 continue;
405
406 if( !visited.contains( n ) && n->Valid() )
407 {
408 visited.insert( n );
409 Q.push_back( n );
410 }
411 }
412 }
413
414 clusters.push_back( std::move( cluster ) );
415 }
416
417 if( m_progressReporter && m_progressReporter->IsCancelled() )
418 return CLUSTERS();
419
420 std::sort( clusters.begin(), clusters.end(),
421 []( const std::shared_ptr<CN_CLUSTER>& a, const std::shared_ptr<CN_CLUSTER>& b )
422 {
423 return a->OriginNet() < b->OriginNet();
424 } );
425
426 return clusters;
427}
428
429
431{
432 // Generate CN_ZONE_LAYERs for each island on each layer of each zone
433 //
434 std::vector<CN_ZONE_LAYER*> zitems;
435
436 for( ZONE* zone : aBoard->Zones() )
437 {
438 if( zone->IsOnCopperLayer() )
439 {
440 m_itemMap[zone] = ITEM_MAP_ENTRY();
441 markItemNetAsDirty( zone );
442
443 // Don't check for connections on layers that only exist in the zone but
444 // were disabled in the board
445 BOARD* board = zone->GetBoard();
446 LSET layerset = board->GetEnabledLayers() & zone->GetLayerSet() & LSET::AllCuMask();
447
448 layerset.RunOnLayers(
449 [&]( PCB_LAYER_ID layer )
450 {
451 for( int j = 0; j < zone->GetFilledPolysList( layer )->OutlineCount(); j++ )
452 zitems.push_back( new CN_ZONE_LAYER( zone, layer, j ) );
453 } );
454 }
455 }
456
457 // Setup progress metrics
458 //
459 int progressDelta = 50;
460 double size = 0.0;
461
462 size += zitems.size(); // Once for building RTrees
463 size += zitems.size(); // Once for adding to connectivity
464 size += aBoard->Tracks().size();
465 size += aBoard->Drawings().size();
466
467 for( FOOTPRINT* footprint : aBoard->Footprints() )
468 size += footprint->Pads().size();
469
470 size *= 1.5; // Our caller gets the other third of the progress bar
471
472 progressDelta = std::max( progressDelta, (int) size / 4 );
473
474 auto report =
475 [&]( int progress )
476 {
477 if( aReporter && ( progress % progressDelta ) == 0 )
478 {
479 aReporter->SetCurrentProgress( progress / size );
480 aReporter->KeepRefreshing( false );
481 }
482 };
483
484 // Generate RTrees for CN_ZONE_LAYER items (in parallel)
485 //
487 std::vector<std::future<size_t>> returns( zitems.size() );
488
489 auto cache_zones =
490 [aReporter]( CN_ZONE_LAYER* aZoneLayer ) -> size_t
491 {
492 if( aReporter && aReporter->IsCancelled() )
493 return 0;
494
495 aZoneLayer->BuildRTree();
496
497 if( aReporter )
498 aReporter->AdvanceProgress();
499
500 return 1;
501 };
502
503 for( size_t ii = 0; ii < zitems.size(); ++ii )
504 {
505 CN_ZONE_LAYER* ptr = zitems[ii];
506 returns[ii] = tp.submit_task(
507 [cache_zones, ptr] { return cache_zones( ptr ); } );
508 }
509
510 for( const std::future<size_t>& ret : returns )
511 {
512 std::future_status status = ret.wait_for( std::chrono::milliseconds( 250 ) );
513
514 while( status != std::future_status::ready )
515 {
516 if( aReporter )
517 aReporter->KeepRefreshing();
518
519 status = ret.wait_for( std::chrono::milliseconds( 250 ) );
520 }
521
522 }
523
524 // Add CN_ZONE_LAYERS, tracks, and pads to connectivity
525 //
526 int ii = zitems.size();
527
528 for( CN_ZONE_LAYER* zitem : zitems )
529 {
530 m_itemList.Add( zitem );
531 m_itemMap[ zitem->Parent() ].Link( zitem );
532 report( ++ii );
533 }
534
535 for( PCB_TRACK* tv : aBoard->Tracks() )
536 {
537 Add( tv );
538 report( ++ii );
539 }
540
541 for( FOOTPRINT* footprint : aBoard->Footprints() )
542 {
543 for( PAD* pad : footprint->Pads() )
544 {
545 Add( pad );
546 report( ++ii );
547 }
548 }
549
550 for( BOARD_ITEM* drawing : aBoard->Drawings() )
551 {
552 if( PCB_SHAPE* shape = dynamic_cast<PCB_SHAPE*>( drawing ) )
553 {
554 if( shape->IsOnCopperLayer() )
555 Add( shape );
556 }
557
558 report( ++ii );
559 }
560
561 if( aReporter )
562 {
563 aReporter->SetCurrentProgress( (double) ii / (double) size );
564 aReporter->KeepRefreshing( false );
565 }
566}
567
568
569void CN_CONNECTIVITY_ALGO::LocalBuild( const std::shared_ptr<CONNECTIVITY_DATA>& aGlobalConnectivity,
570 const std::vector<BOARD_ITEM*>& aLocalItems )
571{
572 m_isLocal = true;
573 m_globalConnectivityData = aGlobalConnectivity;
574
575 for( BOARD_ITEM* item : aLocalItems )
576 {
577 switch( item->Type() )
578 {
579 case PCB_TRACE_T:
580 case PCB_ARC_T:
581 case PCB_VIA_T:
582 case PCB_PAD_T:
583 case PCB_FOOTPRINT_T:
584 case PCB_SHAPE_T:
585 Add( item );
586 break;
587
588 default:
589 break;
590 }
591 }
592}
593
594
596{
597 for( const std::shared_ptr<CN_CLUSTER>& cluster : m_connClusters )
598 {
599 if( cluster->IsConflicting() )
600 {
601 // Conflicting pads in cluster: we don't know the user's intent so best to do
602 // nothing.
603 wxLogTrace( wxT( "CN" ), wxT( "Conflicting pads in cluster %p; skipping propagation" ),
604 cluster.get() );
605 }
606 else if( cluster->HasValidNet() )
607 {
608 // Propagate from the origin (will be a pad if there are any, or another item if
609 // there are no pads).
610 int n_changed = 0;
611
612 for( CN_ITEM* item : *cluster )
613 {
614 if( item->Valid() && item->CanChangeNet()
615 && item->Parent()->GetNetCode() != cluster->OriginNet() )
616 {
617 MarkNetAsDirty( item->Parent()->GetNetCode() );
618 MarkNetAsDirty( cluster->OriginNet() );
619
620 if( aCommit )
621 aCommit->Modify( item->Parent() );
622
623 item->Parent()->SetNetCode( cluster->OriginNet() );
624 n_changed++;
625 }
626 }
627
628 if( n_changed )
629 {
630 wxLogTrace( wxT( "CN" ), wxT( "Cluster %p: net: %d %s" ),
631 cluster.get(),
632 cluster->OriginNet(),
633 (const char*) cluster->OriginNetName().c_str() );
634 }
635 else
636 {
637 wxLogTrace( wxT( "CN" ), wxT( "Cluster %p: no changeable items to propagate to" ),
638 cluster.get() );
639 }
640 }
641 else
642 {
643 wxLogTrace( wxT( "CN" ), wxT( "Cluster %p: connected to unused net" ),
644 cluster.get() );
645 }
646 }
647}
648
649
656
657
658void CN_CONNECTIVITY_ALGO::FillIsolatedIslandsMap( std::map<ZONE*, std::map<PCB_LAYER_ID, ISOLATED_ISLANDS>>& aMap,
659 bool aConnectivityAlreadyRebuilt )
660{
661 int progressDelta = 50;
662 int ii = 0;
663
664 progressDelta = std::max( progressDelta, (int) aMap.size() / 4 );
665
666 if( !aConnectivityAlreadyRebuilt )
667 {
668 for( const auto& [ zone, islands ] : aMap )
669 {
670 Remove( zone );
671 Add( zone );
672 ii++;
673
674 if( m_progressReporter && ( ii % progressDelta ) == 0 )
675 {
676 m_progressReporter->SetCurrentProgress( (double) ii / (double) aMap.size() );
677 m_progressReporter->KeepRefreshing( false );
678 }
679
680 if( m_progressReporter && m_progressReporter->IsCancelled() )
681 return;
682 }
683 }
684
686
687 for( auto& [ zone, zoneIslands ] : aMap )
688 {
689 for( auto& [ layer, layerIslands ] : zoneIslands )
690 {
691 if( zone->GetFilledPolysList( layer )->IsEmpty() )
692 continue;
693
694 bool notInConnectivity = true;
695
696 for( const std::shared_ptr<CN_CLUSTER>& cluster : m_connClusters )
697 {
698 for( CN_ITEM* item : *cluster )
699 {
700 if( item->Parent() == zone && item->GetBoardLayer() == layer )
701 {
702 CN_ZONE_LAYER* z = static_cast<CN_ZONE_LAYER*>( item );
703 notInConnectivity = false;
704
705 if( cluster->IsOrphaned() )
706 layerIslands.m_IsolatedOutlines.push_back( z->SubpolyIndex() );
707 else if( z->HasSingleConnection() )
708 layerIslands.m_SingleConnectionOutlines.push_back( z->SubpolyIndex() );
709 }
710 }
711 }
712
713 if( notInConnectivity )
714 layerIslands.m_IsolatedOutlines.push_back( 0 );
715 }
716 }
717}
718
719
725
726
728{
729 if( aNet < 0 )
730 return;
731
732 if( (int) m_dirtyNets.size() <= aNet )
733 {
734 int lastNet = m_dirtyNets.size() - 1;
735
736 if( lastNet < 0 )
737 lastNet = 0;
738
739 m_dirtyNets.resize( aNet + 1 );
740
741 for( int i = lastNet; i < aNet + 1; i++ )
742 m_dirtyNets[i] = true;
743 }
744
745 m_dirtyNets[aNet] = true;
746}
747
748
750{
751 PCB_LAYER_ID layer = aZoneLayer->GetLayer();
752 BOARD_CONNECTED_ITEM* item = aItem->Parent();
753
754 if( !item->IsOnLayer( layer ) )
755 return;
756
757 auto connect =
758 [&]()
759 {
760 // We don't propagate nets from zones, so any free-via net changes need to happen now.
761 // Defer the SetNetCode call to avoid data races during parallel connectivity search.
762 if( aItem->Parent()->Type() == PCB_VIA_T && aItem->CanChangeNet() )
763 {
764 std::lock_guard<std::mutex> lock( *m_deferredNetCodesMutex );
765 m_deferredNetCodes->emplace_back( aItem->Parent(), aZoneLayer->Net() );
766 }
767
768 aZoneLayer->Connect( aItem );
769 aItem->Connect( aZoneLayer );
770 };
771
772 // Try quick checks first...
773 if( item->Type() == PCB_PAD_T )
774 {
775 PAD* pad = static_cast<PAD*>( item );
776
777 if( pad->ConditionallyFlashed( layer )
778 && pad->GetZoneLayerOverride( layer ) == ZLO_FORCE_NO_ZONE_CONNECTION )
779 {
780 return;
781 }
782
783 // Don't connect zones to pads on backdrilled or post-machined layers
784 if( pad->IsBackdrilledOrPostMachined( layer ) )
785 return;
786 }
787 else if( item->Type() == PCB_VIA_T )
788 {
789 PCB_VIA* via = static_cast<PCB_VIA*>( item );
790
791 if( via->ConditionallyFlashed( layer )
792 && via->GetZoneLayerOverride( layer ) == ZLO_FORCE_NO_ZONE_CONNECTION )
793 {
794 return;
795 }
796
797 // Don't connect zones to vias on backdrilled or post-machined layers
798 if( via->IsBackdrilledOrPostMachined( layer ) )
799 return;
800 }
801
802 for( int i = 0; i < aItem->AnchorCount(); ++i )
803 {
804 if( aZoneLayer->ContainsPoint( aItem->GetAnchor( i ) ) )
805 {
806 connect();
807 return;
808 }
809 }
810
811 if( item->Type() == PCB_VIA_T || item->Type() == PCB_PAD_T )
812 {
813 // As long as the pad/via crosses the zone layer, check for the full effective shape
814 // We check for the overlapping layers above
815 if( aZoneLayer->Collide( item->GetEffectiveShape( layer, FLASHING::ALWAYS_FLASHED ).get() ) )
816 connect();
817
818 return;
819 }
820
821 if( aZoneLayer->Collide( item->GetEffectiveShape( layer ).get() ) )
822 connect();
823}
824
826{
827 // CN_ZONE_LAYER now caches its own copy of the outline, so we just check if it's non-empty.
828 if( !aZoneLayerA->HasValidOutline() || !aZoneLayerB->HasValidOutline() )
829 return;
830
831 const BOX2I& boxA = aZoneLayerA->BBox();
832 const BOX2I& boxB = aZoneLayerB->BBox();
833
834 PCB_LAYER_ID layer = aZoneLayerA->GetLayer();
835
836 if( aZoneLayerB->GetLayer() != layer )
837 return;
838
839 if( !boxA.Intersects( boxB ) )
840 return;
841
842 const SHAPE_LINE_CHAIN& outlineA = aZoneLayerA->GetOutline();
843
844 for( int i = 0; i < outlineA.PointCount(); i++ )
845 {
846 const VECTOR2I& pt = outlineA.CPoint( i );
847
848 if( !boxB.Contains( pt ) )
849 continue;
850
851 if( aZoneLayerB->ContainsPoint( pt ) )
852 {
853 aZoneLayerA->Connect( aZoneLayerB );
854 aZoneLayerB->Connect( aZoneLayerA );
855 return;
856 }
857 }
858
859 const SHAPE_LINE_CHAIN& outlineB = aZoneLayerB->GetOutline();
860
861 for( int i = 0; i < outlineB.PointCount(); i++ )
862 {
863 const VECTOR2I& pt = outlineB.CPoint( i );
864
865 if( !boxA.Contains( pt ) )
866 continue;
867
868 if( aZoneLayerA->ContainsPoint( pt ) )
869 {
870 aZoneLayerA->Connect( aZoneLayerB );
871 aZoneLayerB->Connect( aZoneLayerA );
872 return;
873 }
874 }
875}
876
877
879{
880 const BOARD_CONNECTED_ITEM* parentA = aCandidate->Parent();
881 const BOARD_CONNECTED_ITEM* parentB = m_item->Parent();
882
883 if( !aCandidate->Valid() || !m_item->Valid() )
884 return true;
885
886 if( parentA == parentB )
887 return true;
888
889 // Don't connect items in different nets that can't be changed
890 if( !aCandidate->CanChangeNet() && !m_item->CanChangeNet() && aCandidate->Net() != m_item->Net() )
891 return true;
892
893 // If both m_item and aCandidate are marked dirty, they will both be searched
894 // Since we are reciprocal in our connection, we arbitrarily pick one of the connections
895 // to conduct the expensive search
896 if( aCandidate->Dirty() && aCandidate < m_item )
897 return true;
898
899 // We should handle zone-zone connection separately
900 if ( parentA->Type() == PCB_ZONE_T && parentB->Type() == PCB_ZONE_T )
901 {
903 static_cast<CN_ZONE_LAYER*>( aCandidate ) );
904 return true;
905 }
906
907 if( parentA->Type() == PCB_ZONE_T )
908 {
909 checkZoneItemConnection( static_cast<CN_ZONE_LAYER*>( aCandidate ), m_item );
910 return true;
911 }
912
913 if( parentB->Type() == PCB_ZONE_T )
914 {
915 checkZoneItemConnection( static_cast<CN_ZONE_LAYER*>( m_item ), aCandidate );
916 return true;
917 }
918
919 LSET commonLayers = parentA->GetLayerSet() & parentB->GetLayerSet();
920
921 if( const BOARD* board = parentA->GetBoard() )
922 commonLayers &= board->GetEnabledLayers();
923
924 for( PCB_LAYER_ID layer : commonLayers )
925 {
928
929 if( parentA->Type() == PCB_PAD_T )
930 {
931 if( !static_cast<const PAD*>( parentA )->ConditionallyFlashed( layer ) )
932 flashingA = FLASHING::ALWAYS_FLASHED;
933 }
934 else if( parentA->Type() == PCB_VIA_T )
935 {
936 if( !static_cast<const PCB_VIA*>( parentA )->ConditionallyFlashed( layer ) )
937 flashingA = FLASHING::ALWAYS_FLASHED;
938 }
939
940 if( parentB->Type() == PCB_PAD_T )
941 {
942 if( !static_cast<const PAD*>( parentB )->ConditionallyFlashed( layer ) )
943 flashingB = FLASHING::ALWAYS_FLASHED;
944 }
945 else if( parentB->Type() == PCB_VIA_T )
946 {
947 if( !static_cast<const PCB_VIA*>( parentB )->ConditionallyFlashed( layer ) )
948 flashingB = FLASHING::ALWAYS_FLASHED;
949 }
950
951 if( parentA->GetEffectiveShape( layer, flashingA )->Collide(
952 parentB->GetEffectiveShape( layer, flashingB ).get() ) )
953 {
954 m_item->Connect( aCandidate );
955 aCandidate->Connect( m_item );
956 return true;
957 }
958 }
959
960 return true;
961};
962
963
965{
966 m_ratsnestClusters.clear();
967 m_connClusters.clear();
968 m_itemMap.clear();
969 m_itemList.Clear();
970
971}
972
977
978
980{
981 // Map of footprint -> map of pad number -> list of CN_ITEMs for pads with that number
982 std::map<FOOTPRINT*, std::map<wxString, std::vector<CN_ITEM*>>> padsByFootprint;
983
984 for( CN_ITEM* item : m_itemList )
985 {
986 if( !item->Valid() || item->Parent()->Type() != PCB_PAD_T )
987 continue;
988
989 auto pad = static_cast<const PAD*>( item->Parent() );
990
991 FOOTPRINT* fp = pad->GetParentFootprint();
992
993 padsByFootprint[fp][ pad->GetNumber() ].emplace_back( item );
994 }
995
996 for( auto& [footprint, padsMap] : padsByFootprint )
997 {
998 if( footprint->GetDuplicatePadNumbersAreJumpers() )
999 {
1000 for( const std::vector<CN_ITEM*>& padsList : padsMap | std::views::values )
1001 {
1002 for( size_t i = 0; i < padsList.size(); ++i )
1003 {
1004 for( size_t j = 1; j < padsList.size(); ++j )
1005 {
1006 padsList[i]->Connect( padsList[j] );
1007 padsList[j]->Connect( padsList[i] );
1008 }
1009 }
1010 }
1011 }
1012
1013 for( const std::set<wxString>& group : footprint->JumperPadGroups() )
1014 {
1015 std::vector<CN_ITEM*> toConnect;
1016
1017 for( const wxString& padNumber : group )
1018 std::ranges::copy( padsMap[padNumber], std::back_inserter( toConnect ) );
1019
1020 for( size_t i = 0; i < toConnect.size(); ++i )
1021 {
1022 for( size_t j = 1; j < toConnect.size(); ++j )
1023 {
1024 toConnect[i]->Connect( toConnect[j] );
1025 toConnect[j]->Connect( toConnect[i] );
1026 }
1027 }
1028 }
1029 }
1030}
@ ZLO_FORCE_NO_ZONE_CONNECTION
Definition board_item.h:75
BOX2< VECTOR2I > BOX2I
Definition box2.h:922
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:84
virtual PCB_LAYER_ID GetLayer() const
Return the primary layer this item is on.
Definition board_item.h:237
virtual bool IsConnected() const
Returns information if the object is derived from BOARD_CONNECTED_ITEM.
Definition board_item.h:139
virtual bool IsOnLayer(PCB_LAYER_ID aLayer) const
Test to see if this object is on the given layer.
Definition board_item.h:319
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:257
virtual bool IsOnCopperLayer() const
Definition board_item.h:156
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:967
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
std::vector< std::pair< BOARD_CONNECTED_ITEM *, int > > * m_deferredNetCodes
Deferred net code changes collected during parallel connectivity search.
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)
std::mutex * m_deferredNetCodesMutex
bool operator()(CN_ITEM *aCandidate)
const SHAPE_LINE_CHAIN & GetOutline() const
PCB_LAYER_ID GetLayer() const
bool Collide(SHAPE *aRefShape) const
int SubpolyIndex() const
bool ContainsPoint(const VECTOR2I &p) const
bool HasValidOutline() 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:111
std::deque< PAD * > & Pads()
Definition footprint.h:306
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:263
static LSET AllCuMask()
return AllCuMask( MAX_CU_LAYERS );
Definition lset.cpp:608
Handle the data for a net.
Definition netinfo.h:54
Definition pad.h:55
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.
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:73
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:89
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::priority_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: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
VECTOR2< int32_t > VECTOR2I
Definition vector2d.h:695