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