KiCad PCB EDA Suite
Loading...
Searching...
No Matches
connectivity_data.cpp
Go to the documentation of this file.
1/*
2 * This program source code file is part of KICAD, a free EDA CAD application.
3 *
4 * Copyright (C) 2017 CERN
5 * Copyright (C) 2018-2024 KiCad Developers, see AUTHORS.txt for contributors.
6 * @author Tomasz Wlostowski <[email protected]>
7 *
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License
10 * as published by the Free Software Foundation; either version 2
11 * of the License, or (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, you may find one here:
20 * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
21 * or you may search the http://www.gnu.org website for the version 2 license,
22 * or you may write to the Free Software Foundation, Inc.,
23 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
24 */
25
26#ifdef PROFILE
27#include <core/profile.h>
28#endif
29
30#include <algorithm>
31#include <future>
32#include <initializer_list>
33
42#include <progress_reporter.h>
43#include <core/thread_pool.h>
44#include <trigo.h>
45#include <drc/drc_rtree.h>
46
48 m_skipRatsnestUpdate( false )
49{
50 m_connAlgo.reset( new CN_CONNECTIVITY_ALGO( this ) );
51 m_progressReporter = nullptr;
52 m_fromToCache.reset( new FROM_TO_CACHE );
53}
54
55
56CONNECTIVITY_DATA::CONNECTIVITY_DATA( std::shared_ptr<CONNECTIVITY_DATA> aGlobalConnectivity,
57 const std::vector<BOARD_ITEM*>& aLocalItems,
58 bool aSkipRatsnestUpdate ) :
59 m_skipRatsnestUpdate( aSkipRatsnestUpdate )
60{
61 Build( aGlobalConnectivity, aLocalItems );
62 m_progressReporter = nullptr;
63 m_fromToCache.reset( new FROM_TO_CACHE );
64}
65
66
68{
69 for( RN_NET* net : m_nets )
70 delete net;
71
72 m_nets.clear();
73}
74
75
77{
78 m_connAlgo->Add( aItem );
79 return true;
80}
81
82
84{
85 m_connAlgo->Remove( aItem );
86 return true;
87}
88
89
91{
92 m_connAlgo->Remove( aItem );
93 m_connAlgo->Add( aItem );
94 return true;
95}
96
97
99{
100 aBoard->CacheTriangulation( aReporter );
101
102 std::unique_lock<KISPINLOCK> lock( m_lock, std::try_to_lock );
103
104 if( !lock )
105 return false;
106
107 if( aReporter )
108 {
109 aReporter->Report( _( "Updating nets..." ) );
110 aReporter->KeepRefreshing( false );
111 }
112
113 for( auto net : m_nets )
114 if ( net )
115 delete net;
116
117 m_nets.clear();
118
119 m_connAlgo.reset( new CN_CONNECTIVITY_ALGO( this ) );
120 m_connAlgo->Build( aBoard, aReporter );
121
123
124 RefreshNetcodeMap( aBoard );
125
126 if( aReporter )
127 {
128 aReporter->SetCurrentProgress( 0.75 );
129 aReporter->KeepRefreshing( false );
130 }
131
133
134 if( aReporter )
135 {
136 aReporter->SetCurrentProgress( 1.0 );
137 aReporter->KeepRefreshing( false );
138 }
139
140 return true;
141}
142
143
145{
146 m_netcodeMap.clear();
147
148 for( NETINFO_ITEM* net : aBoard->GetNetInfo() )
149 m_netcodeMap[net->GetNetCode()] = net->GetNetname();
150}
151
152
153void CONNECTIVITY_DATA::Build( std::shared_ptr<CONNECTIVITY_DATA>& aGlobalConnectivity,
154 const std::vector<BOARD_ITEM*>& aLocalItems )
155{
156 std::unique_lock<KISPINLOCK> lock( m_lock, std::try_to_lock );
157
158 if( !lock )
159 return;
160
161 m_connAlgo.reset( new CN_CONNECTIVITY_ALGO( this ) );
162 m_connAlgo->LocalBuild( aGlobalConnectivity, aLocalItems );
163
165}
166
167
169{
170 m_connAlgo->ForEachAnchor( [&aDelta]( CN_ANCHOR& anchor )
171 {
172 anchor.Move( aDelta );
173 } );
174}
175
176
178{
179#ifdef PROFILE
180 PROF_TIMER rnUpdate( "update-ratsnest" );
181#endif
182
183 std::vector<RN_NET*> dirty_nets;
184
185 // Start with net 1 as net 0 is reserved for not-connected
186 // Nets without nodes are also ignored
187 std::copy_if( m_nets.begin() + 1, m_nets.end(), std::back_inserter( dirty_nets ),
188 [] ( RN_NET* aNet )
189 {
190 return aNet->IsDirty() && aNet->GetNodeCount() > 0;
191 } );
192
194
195 tp.push_loop( dirty_nets.size(),
196 [&]( const int a, const int b )
197 {
198 for( int ii = a; ii < b; ++ii )
199 dirty_nets[ii]->UpdateNet();
200 } );
201 tp.wait_for_tasks();
202
203 tp.push_loop( dirty_nets.size(),
204 [&]( const int a, const int b )
205 {
206 for( int ii = a; ii < b; ++ii )
207 dirty_nets[ii]->OptimizeRNEdges();
208 } );
209 tp.wait_for_tasks();
210
211#ifdef PROFILE
212 rnUpdate.Show();
213#endif
214}
215
216
217void CONNECTIVITY_DATA::addRatsnestCluster( const std::shared_ptr<CN_CLUSTER>& aCluster )
218{
219 RN_NET* rnNet = m_nets[ aCluster->OriginNet() ];
220
221 rnNet->AddCluster( aCluster );
222}
223
224
226{
227
228 // We can take over the lock here if called in the same thread
229 // This is to prevent redraw during a RecalculateRatsnets process
230 std::unique_lock<KISPINLOCK> lock( m_lock );
231
233
234}
235
237{
238 m_connAlgo->PropagateNets( aCommit );
239
240 int lastNet = m_connAlgo->NetCount();
241
242 if( lastNet >= (int) m_nets.size() )
243 {
244 unsigned int prevSize = m_nets.size();
245 m_nets.resize( lastNet + 1 );
246
247 for( unsigned int i = prevSize; i < m_nets.size(); i++ )
248 m_nets[i] = new RN_NET;
249 }
250 else
251 {
252 for( size_t ii = lastNet; ii < m_nets.size(); ++ii )
253 m_nets[ii]->Clear();
254 }
255
256 const std::vector<std::shared_ptr<CN_CLUSTER>>& clusters = m_connAlgo->GetClusters();
257
258 for( int net = 0; net < lastNet; net++ )
259 {
260 if( m_connAlgo->IsNetDirty( net ) )
261 {
262 m_nets[net]->Clear();
263 }
264 }
265
266 for( const std::shared_ptr<CN_CLUSTER>& c : clusters )
267 {
268 int net = c->OriginNet();
269
270 // Don't add intentionally-kept zone islands to the ratsnest
271 if( c->IsOrphaned() && c->Size() == 1 )
272 {
273 if( dynamic_cast<CN_ZONE_LAYER*>( *c->begin() ) )
274 continue;
275 }
276
277 if( m_connAlgo->IsNetDirty( net ) )
279 }
280
281 m_connAlgo->ClearDirtyFlags();
282
285}
286
287
288void CONNECTIVITY_DATA::BlockRatsnestItems( const std::vector<BOARD_ITEM*>& aItems )
289{
290 std::vector<BOARD_CONNECTED_ITEM*> citems;
291
292 for( BOARD_ITEM* item : aItems )
293 {
294 if( item->Type() == PCB_FOOTPRINT_T )
295 {
296 for( PAD* pad : static_cast<FOOTPRINT*>(item)->Pads() )
297 citems.push_back( pad );
298 }
299 else
300 {
301 if( BOARD_CONNECTED_ITEM* citem = dynamic_cast<BOARD_CONNECTED_ITEM*>( item ) )
302 citems.push_back( citem );
303 }
304 }
305
306 for( const BOARD_CONNECTED_ITEM* item : citems )
307 {
308 if ( m_connAlgo->ItemExists( item ) )
309 {
310 CN_CONNECTIVITY_ALGO::ITEM_MAP_ENTRY& entry = m_connAlgo->ItemEntry( item );
311
312 for( CN_ITEM* cnItem : entry.GetItems() )
313 {
314 for( const std::shared_ptr<CN_ANCHOR>& anchor : cnItem->Anchors() )
315 anchor->SetNoLine( true );
316 }
317 }
318 }
319}
320
321
323{
324 return m_connAlgo->NetCount();
325}
326
327
328void CONNECTIVITY_DATA::FillIsolatedIslandsMap( std::map<ZONE*, std::map<PCB_LAYER_ID, ISOLATED_ISLANDS>>& aMap,
329 bool aConnectivityAlreadyRebuilt )
330{
331 m_connAlgo->FillIsolatedIslandsMap( aMap, aConnectivityAlreadyRebuilt );
332}
333
334
335void CONNECTIVITY_DATA::ComputeLocalRatsnest( const std::vector<BOARD_ITEM*>& aItems,
336 const CONNECTIVITY_DATA* aDynamicData,
337 VECTOR2I aInternalOffset )
338{
339 if( !aDynamicData )
340 return;
341
342 m_dynamicRatsnest.clear();
343 std::mutex dynamic_ratsnest_mutex;
344
345 // This gets connections between the stationary board and the
346 // moving selection
347
348 auto update_lambda = [&]( int nc )
349 {
350 RN_NET* dynamicNet = aDynamicData->m_nets[nc];
351 RN_NET* staticNet = m_nets[nc];
352
356 if( dynamicNet->GetNodeCount() != 0
357 && dynamicNet->GetNodeCount() != staticNet->GetNodeCount() )
358 {
359 VECTOR2I pos1, pos2;
360
361 if( staticNet->NearestBicoloredPair( dynamicNet, pos1, pos2 ) )
362 {
364 l.a = pos1;
365 l.b = pos2;
366 l.netCode = nc;
367
368 std::lock_guard<std::mutex> lock( dynamic_ratsnest_mutex );
369 m_dynamicRatsnest.push_back( l );
370 }
371 }
372 };
373
375 size_t num_nets = std::min( m_nets.size(), aDynamicData->m_nets.size() );
376
377 tp.push_loop( 1, num_nets,
378 [&]( const int a, const int b)
379 {
380 for( int ii = a; ii < b; ++ii )
381 update_lambda( ii );
382 });
383 tp.wait_for_tasks();
384
385 // This gets the ratsnest for internal connections in the moving set
386 const std::vector<CN_EDGE>& edges = GetRatsnestForItems( aItems );
387
388 for( const CN_EDGE& edge : edges )
389 {
390 const std::shared_ptr<const CN_ANCHOR>& nodeA = edge.GetSourceNode();
391 const std::shared_ptr<const CN_ANCHOR>& nodeB = edge.GetTargetNode();
392
393 if( !nodeA || nodeA->Dirty() || !nodeB || nodeB->Dirty() )
394 continue;
395
397
398 // Use the parents' positions
399 l.a = nodeA->Parent()->GetPosition() + aInternalOffset;
400 l.b = nodeB->Parent()->GetPosition() + aInternalOffset;
401 l.netCode = 0;
402 m_dynamicRatsnest.push_back( l );
403 }
404}
405
406
408{
409 m_connAlgo->ForEachAnchor( []( CN_ANCHOR& anchor )
410 {
411 anchor.SetNoLine( false );
412 } );
414}
415
416
418{
419 m_dynamicRatsnest.clear();
420}
421
422
424{
425 m_connAlgo->PropagateNets( aCommit );
426}
427
428
430 const std::initializer_list<KICAD_T>& aTypes ) const
431{
432 CN_CONNECTIVITY_ALGO::ITEM_MAP_ENTRY &entry = m_connAlgo->ItemEntry( aItem );
433
434 auto matchType =
435 [&]( KICAD_T aItemType )
436 {
437 if( aTypes.size() == 0 )
438 return true;
439
440 return alg::contains( aTypes, aItemType);
441 };
442
443 for( CN_ITEM* citem : entry.GetItems() )
444 {
445 for( CN_ITEM* connected : citem->ConnectedItems() )
446 {
447 CN_ZONE_LAYER* zoneLayer = dynamic_cast<CN_ZONE_LAYER*>( connected );
448
449 // lyIdx is compatible with StartLayer() and EndLayer() notation in CN_ITEM
450 // items, where B_Cu is set to INT_MAX (std::numeric_limits<int>::max())
451 int lyIdx = aLayer;
452
453 if( aLayer == B_Cu )
454 lyIdx = std::numeric_limits<int>::max();
455
456 if( connected->Valid()
457 && connected->StartLayer() <= lyIdx && connected->EndLayer() >= lyIdx
458 && matchType( connected->Parent()->Type() )
459 && connected->Net() == aItem->GetNetCode() )
460 {
461 if( aItem->Type() == PCB_PAD_T && zoneLayer )
462 {
463 const PAD* pad = static_cast<const PAD*>( aItem );
464 ZONE* zone = static_cast<ZONE*>( zoneLayer->Parent() );
465 int islandIdx = zoneLayer->SubpolyIndex();
466
467 if( zone->IsFilled() )
468 {
469 PCB_LAYER_ID pcbLayer = ToLAYER_ID( aLayer );
470 const SHAPE_POLY_SET* zoneFill = zone->GetFill( pcbLayer );
471 const SHAPE_LINE_CHAIN& padHull =
472 pad->GetEffectivePolygon( pcbLayer, ERROR_INSIDE )->Outline( 0 );
473
474 for( const VECTOR2I& pt : zoneFill->COutline( islandIdx ).CPoints() )
475 {
476 // If the entire island is inside the pad's flashing then the pad
477 // won't actually connect to anything else, so only return true if
478 // part of the island is *outside* the pad's flashing.
479
480 if( !padHull.PointInside( pt ) )
481 return true;
482 }
483 }
484
485 continue;
486 }
487 else if( aItem->Type() == PCB_VIA_T && zoneLayer )
488 {
489 const PCB_VIA* via = static_cast<const PCB_VIA*>( aItem );
490 ZONE* zone = static_cast<ZONE*>( zoneLayer->Parent() );
491 int islandIdx = zoneLayer->SubpolyIndex();
492
493 if( zone->IsFilled() )
494 {
495 PCB_LAYER_ID lyr = ToLAYER_ID( aLayer );
496 const SHAPE_POLY_SET* zoneFill = zone->GetFill( lyr );
497 SHAPE_CIRCLE viaHull( via->GetCenter(), via->GetWidth( lyr ) / 2 );
498
499 for( const VECTOR2I& pt : zoneFill->COutline( islandIdx ).CPoints() )
500 {
501 // If the entire island is inside the via's flashing then the via
502 // won't actually connect to anything else, so only return true if
503 // part of the island is *outside* the via's flashing.
504
505 if( !viaHull.SHAPE::Collide( pt ) )
506 return true;
507 }
508 }
509
510 continue;
511 }
512
513 return true;
514 }
515 }
516 }
517
518 return false;
519}
520
521
522unsigned int CONNECTIVITY_DATA::GetUnconnectedCount( bool aVisibleOnly ) const
523{
524 unsigned int unconnected = 0;
525
526 for( RN_NET* net : m_nets )
527 {
528 if( !net )
529 continue;
530
531 for( const CN_EDGE& edge : net->GetEdges() )
532 {
533 if( edge.IsVisible() || !aVisibleOnly )
534 ++unconnected;
535 }
536 }
537
538 return unconnected;
539}
540
541
543{
544 for( RN_NET* net : m_nets )
545 net->Clear();
546}
547
548
549const std::vector<BOARD_CONNECTED_ITEM*>
551 const std::vector<KICAD_T>& aTypes,
552 bool aIgnoreNetcodes ) const
553{
554 std::vector<BOARD_CONNECTED_ITEM*> rv;
556
557 if( aIgnoreNetcodes )
559 else
561
562 const auto clusters = m_connAlgo->SearchClusters( searchMode, aTypes,
563 aIgnoreNetcodes ? -1 : aItem->GetNetCode() );
564
565 for( const std::shared_ptr<CN_CLUSTER>& cl : clusters )
566 {
567 if( cl->Contains( aItem ) )
568 {
569 for( const CN_ITEM* item : *cl )
570 {
571 if( item->Valid() )
572 rv.push_back( item->Parent() );
573 }
574 }
575 }
576
577 return rv;
578}
579
580
581const std::vector<BOARD_CONNECTED_ITEM*>
582CONNECTIVITY_DATA::GetNetItems( int aNetCode, const std::vector<KICAD_T>& aTypes ) const
583{
584 std::vector<BOARD_CONNECTED_ITEM*> items;
585 items.reserve( 32 );
586
587 std::bitset<MAX_STRUCT_TYPE_ID> type_bits;
588
589 for( KICAD_T scanType : aTypes )
590 {
591 wxASSERT( scanType < MAX_STRUCT_TYPE_ID );
592 type_bits.set( scanType );
593 }
594
595 m_connAlgo->ForEachItem(
596 [&]( CN_ITEM& aItem )
597 {
598 if( aItem.Valid() && ( aItem.Net() == aNetCode ) && type_bits[aItem.Parent()->Type()] )
599 items.push_back( aItem.Parent() );
600 } );
601
602 std::sort( items.begin(), items.end() );
603 items.erase( std::unique( items.begin(), items.end() ), items.end() );
604 return items;
605}
606
607
608const std::vector<PCB_TRACK*>
610{
611 CN_CONNECTIVITY_ALGO::ITEM_MAP_ENTRY& entry = m_connAlgo->ItemEntry( aItem );
612
613 std::set<PCB_TRACK*> tracks;
614 std::vector<PCB_TRACK*> rv;
615
616 for( CN_ITEM* citem : entry.GetItems() )
617 {
618 for( CN_ITEM* connected : citem->ConnectedItems() )
619 {
620 if( connected->Valid() &&
621 ( connected->Parent()->Type() == PCB_TRACE_T ||
622 connected->Parent()->Type() == PCB_VIA_T ||
623 connected->Parent()->Type() == PCB_ARC_T ) )
624 {
625 tracks.insert( static_cast<PCB_TRACK*> ( connected->Parent() ) );
626 }
627 }
628 }
629
630 std::copy( tracks.begin(), tracks.end(), std::back_inserter( rv ) );
631 return rv;
632}
633
634
636 std::set<PAD*>* pads ) const
637{
638 for( CN_ITEM* citem : m_connAlgo->ItemEntry( aItem ).GetItems() )
639 {
640 for( CN_ITEM* connected : citem->ConnectedItems() )
641 {
642 if( connected->Valid() && connected->Parent()->Type() == PCB_PAD_T )
643 pads->insert( static_cast<PAD*> ( connected->Parent() ) );
644 }
645 }
646}
647
648
649const std::vector<PAD*> CONNECTIVITY_DATA::GetConnectedPads( const BOARD_CONNECTED_ITEM* aItem )
650const
651{
652 std::set<PAD*> pads;
653 std::vector<PAD*> rv;
654
655 GetConnectedPads( aItem, &pads );
656
657 std::copy( pads.begin(), pads.end(), std::back_inserter( rv ) );
658 return rv;
659}
660
661
663 std::vector<PAD*>* pads,
664 std::vector<PCB_VIA*>* vias )
665{
666 for( CN_ITEM* citem : m_connAlgo->ItemEntry( aItem ).GetItems() )
667 {
668 for( CN_ITEM* connected : citem->ConnectedItems() )
669 {
670 if( connected->Valid() )
671 {
672 BOARD_CONNECTED_ITEM* parent = connected->Parent();
673
674 if( parent->Type() == PCB_PAD_T )
675 pads->push_back( static_cast<PAD*>( parent ) );
676 else if( parent->Type() == PCB_VIA_T )
677 vias->push_back( static_cast<PCB_VIA*>( parent ) );
678 }
679 }
680 }
681}
682
683
684unsigned int CONNECTIVITY_DATA::GetNodeCount( int aNet ) const
685{
686 int sum = 0;
687
688 if( aNet < 0 ) // Node count for all nets
689 {
690 for( const RN_NET* net : m_nets )
691 sum += net->GetNodeCount();
692 }
693 else if( aNet < (int) m_nets.size() )
694 {
695 sum = m_nets[aNet]->GetNodeCount();
696 }
697
698 return sum;
699}
700
701
702unsigned int CONNECTIVITY_DATA::GetPadCount( int aNet ) const
703{
704 int n = 0;
705
706 for( CN_ITEM* pad : m_connAlgo->ItemList() )
707 {
708 if( !pad->Valid() || pad->Parent()->Type() != PCB_PAD_T)
709 continue;
710
711 PAD* dpad = static_cast<PAD*>( pad->Parent() );
712
713 if( aNet < 0 || aNet == dpad->GetNetCode() )
714 n++;
715 }
716
717 return n;
718}
719
720
721void CONNECTIVITY_DATA::RunOnUnconnectedEdges( std::function<bool( CN_EDGE& )> aFunc )
722{
723 for( RN_NET* rnNet : m_nets )
724 {
725 if( rnNet )
726 {
727 for( CN_EDGE& edge : rnNet->GetEdges() )
728 {
729 if( !aFunc( edge ) )
730 return;
731 }
732 }
733 }
734}
735
736
737static int getMinDist( BOARD_CONNECTED_ITEM* aItem, const VECTOR2I& aPoint )
738{
739 switch( aItem->Type() )
740 {
741 case PCB_TRACE_T:
742 case PCB_ARC_T:
743 {
744 PCB_TRACK* track = static_cast<PCB_TRACK*>( aItem );
745
746 return std::min( track->GetStart().Distance(aPoint ),
747 track->GetEnd().Distance( aPoint ) );
748 }
749
750 default:
751 return aItem->GetPosition().Distance( aPoint );
752 }
753}
754
755
756bool CONNECTIVITY_DATA::TestTrackEndpointDangling( PCB_TRACK* aTrack, bool aIgnoreTracksInPads,
757 VECTOR2I* aPos ) const
758{
759 const std::list<CN_ITEM*>& items = GetConnectivityAlgo()->ItemEntry( aTrack ).GetItems();
760
761 // Not in the connectivity system. This is a bug!
762 if( items.empty() )
763 {
764 wxFAIL_MSG( wxT( "track not in connectivity system" ) );
765 return false;
766 }
767
768 CN_ITEM* citem = items.front();
769
770 if( !citem->Valid() )
771 return false;
772
773 if( aTrack->Type() == PCB_TRACE_T || aTrack->Type() == PCB_ARC_T )
774 {
775 // Test if a segment is connected on each end.
776 //
777 // NB: be wary of short segments which can be connected to the *same* other item on
778 // each end. If that's their only connection then they're still dangling.
779
780 PCB_LAYER_ID layer = aTrack->GetLayer();
781 int accuracy = KiROUND( aTrack->GetWidth() / 2 );
782 int start_count = 0;
783 int end_count = 0;
784
785 for( CN_ITEM* connected : citem->ConnectedItems() )
786 {
787 BOARD_CONNECTED_ITEM* item = connected->Parent();
788 ZONE* zone = dynamic_cast<ZONE*>( item );
789 DRC_RTREE* rtree = nullptr;
790 bool hitStart = false;
791 bool hitEnd = false;
792
793 if( item->GetFlags() & IS_DELETED )
794 continue;
795
796 if( zone )
797 rtree = zone->GetBoard()->m_CopperZoneRTreeCache[ zone ].get();
798
799 if( rtree )
800 {
801 SHAPE_CIRCLE start( aTrack->GetStart(), accuracy );
802 SHAPE_CIRCLE end( aTrack->GetEnd(), accuracy );
803
804 hitStart = rtree->QueryColliding( start.BBox(), &start, layer );
805 hitEnd = rtree->QueryColliding( end.BBox(), &end, layer );
806 }
807 else
808 {
809 std::shared_ptr<SHAPE> shape = item->GetEffectiveShape( layer );
810
811 hitStart = shape->Collide( aTrack->GetStart(), accuracy );
812 hitEnd = shape->Collide( aTrack->GetEnd(), accuracy );
813 }
814
815 if( hitStart && hitEnd )
816 {
817 if( zone )
818 {
819 // Both start and end in a zone: track may be redundant, but it's not dangling
820 return false;
821 }
822 else if( item->Type() == PCB_PAD_T || item->Type() == PCB_VIA_T )
823 {
824 // Both start and end are under a pad: see what the caller wants us to do
825 if( aIgnoreTracksInPads )
826 return false;
827 }
828
829 if( getMinDist( item, aTrack->GetStart() ) < getMinDist( item, aTrack->GetEnd() ) )
830 start_count++;
831 else
832 end_count++;
833 }
834 else if( hitStart )
835 {
836 start_count++;
837 }
838 else if( hitEnd )
839 {
840 end_count++;
841 }
842
843 if( start_count > 0 && end_count > 0 )
844 return false;
845 }
846
847 if( aPos )
848 *aPos = (start_count == 0 ) ? aTrack->GetStart() : aTrack->GetEnd();
849
850 return true;
851 }
852 else if( aTrack->Type() == PCB_VIA_T )
853 {
854 // Test if a via is only connected on one layer
855
856 const std::vector<CN_ITEM*>& connected = citem->ConnectedItems();
857
858 if( connected.empty() )
859 {
860 if( aPos )
861 *aPos = aTrack->GetPosition();
862
863 return true;
864 }
865
866 // Here, we check if the via is connected only to items on a single layer
867 int first_layer = UNDEFINED_LAYER;
868
869 for( CN_ITEM* item : connected )
870 {
871 if( item->Parent()->GetFlags() & IS_DELETED )
872 continue;
873
874 if( first_layer == UNDEFINED_LAYER )
875 first_layer = item->Layer();
876 else if( item->Layer() != first_layer )
877 return false;
878 }
879
880 if( aPos )
881 *aPos = aTrack->GetPosition();
882
883 return true;
884 }
885 else
886 {
887 wxFAIL_MSG( wxT( "CONNECTIVITY_DATA::TestTrackEndpointDangling: unknown track type" ) );
888 }
889
890 return false;
891}
892
893
894const std::vector<BOARD_CONNECTED_ITEM*>
896 const VECTOR2I& aAnchor,
897 const std::vector<KICAD_T>& aTypes,
898 const int& aMaxError ) const
899{
900 CN_CONNECTIVITY_ALGO::ITEM_MAP_ENTRY& entry = m_connAlgo->ItemEntry( aItem );
901 std::vector<BOARD_CONNECTED_ITEM*> rv;
902 SEG::ecoord maxError_sq = (SEG::ecoord) aMaxError * aMaxError;
903
904 for( CN_ITEM* cnItem : entry.GetItems() )
905 {
906 for( CN_ITEM* connected : cnItem->ConnectedItems() )
907 {
908 for( const std::shared_ptr<CN_ANCHOR>& anchor : connected->Anchors() )
909 {
910 if( ( anchor->Pos() - aAnchor ).SquaredEuclideanNorm() <= maxError_sq )
911 {
912 for( KICAD_T type : aTypes )
913 {
914 if( connected->Valid() && connected->Parent()->Type() == type )
915 {
916 rv.push_back( connected->Parent() );
917 break;
918 }
919 }
920
921 break;
922 }
923 }
924 }
925 }
926
927 return rv;
928}
929
930
932{
933 if ( aNet < 0 || aNet >= (int) m_nets.size() )
934 return nullptr;
935
936 return m_nets[ aNet ];
937}
938
939
941{
942 if ( aItem->Type() == PCB_FOOTPRINT_T)
943 {
944 for( PAD* pad : static_cast<FOOTPRINT*>( aItem )->Pads() )
945 m_connAlgo->MarkNetAsDirty( pad->GetNetCode() );
946 }
947
948 if (aItem->IsConnected() )
949 m_connAlgo->MarkNetAsDirty( static_cast<BOARD_CONNECTED_ITEM*>( aItem )->GetNetCode() );
950}
951
952
954{
955 m_connAlgo->RemoveInvalidRefs();
956
957 for( RN_NET* rnNet : m_nets )
958 rnNet->RemoveInvalidRefs();
959}
960
961
963{
964 m_progressReporter = aReporter;
965 m_connAlgo->SetProgressReporter( m_progressReporter );
966}
967
968
969const std::vector<CN_EDGE>
970CONNECTIVITY_DATA::GetRatsnestForItems( const std::vector<BOARD_ITEM*>& aItems )
971{
972 std::set<int> nets;
973 std::vector<CN_EDGE> edges;
974 std::set<BOARD_CONNECTED_ITEM*> item_set;
975
976 for( BOARD_ITEM* item : aItems )
977 {
978 if( item->Type() == PCB_FOOTPRINT_T )
979 {
980 FOOTPRINT* footprint = static_cast<FOOTPRINT*>( item );
981
982 for( PAD* pad : footprint->Pads() )
983 {
984 nets.insert( pad->GetNetCode() );
985 item_set.insert( pad );
986 }
987 }
988 else if( item->IsConnected() )
989 {
990 BOARD_CONNECTED_ITEM* conn_item = static_cast<BOARD_CONNECTED_ITEM*>( item );
991
992 item_set.insert( conn_item );
993 nets.insert( conn_item->GetNetCode() );
994 }
995 }
996
997 for( int netcode : nets )
998 {
999 RN_NET* net = GetRatsnestForNet( netcode );
1000
1001 if( !net )
1002 continue;
1003
1004 for( const CN_EDGE& edge : net->GetEdges() )
1005 {
1006 std::shared_ptr<const CN_ANCHOR> srcNode = edge.GetSourceNode();
1007 std::shared_ptr<const CN_ANCHOR> dstNode = edge.GetTargetNode();
1008
1009 if( !srcNode || srcNode->Dirty() || !dstNode || dstNode->Dirty() )
1010 continue;
1011
1012 BOARD_CONNECTED_ITEM* srcParent = srcNode->Parent();
1013 BOARD_CONNECTED_ITEM* dstParent = dstNode->Parent();
1014
1015 bool srcFound = ( item_set.find( srcParent ) != item_set.end() );
1016 bool dstFound = ( item_set.find( dstParent ) != item_set.end() );
1017
1018 if ( srcFound && dstFound )
1019 edges.push_back( edge );
1020 }
1021 }
1022
1023 return edges;
1024}
1025
1026
1027const std::vector<CN_EDGE> CONNECTIVITY_DATA::GetRatsnestForPad( const PAD* aPad )
1028{
1029 std::vector<CN_EDGE> edges;
1030 RN_NET* net = GetRatsnestForNet( aPad->GetNetCode() );
1031
1032 if( !net )
1033 return edges;
1034
1035 for( const CN_EDGE& edge : net->GetEdges() )
1036 {
1037 if( !edge.GetSourceNode() || edge.GetSourceNode()->Dirty() )
1038 continue;
1039
1040 if( !edge.GetTargetNode() || edge.GetTargetNode()->Dirty() )
1041 continue;
1042
1043 if( edge.GetSourceNode()->Parent() == aPad || edge.GetTargetNode()->Parent() == aPad )
1044 edges.push_back( edge );
1045 }
1046
1047 return edges;
1048}
1049
1050
1051const std::vector<CN_EDGE> CONNECTIVITY_DATA::GetRatsnestForComponent( FOOTPRINT* aComponent,
1052 bool aSkipInternalConnections )
1053{
1054 std::set<int> nets;
1055 std::set<const PAD*> pads;
1056 std::vector<CN_EDGE> edges;
1057
1058 for( PAD* pad : aComponent->Pads() )
1059 {
1060 nets.insert( pad->GetNetCode() );
1061 pads.insert( pad );
1062 }
1063
1064 for( int netcode : nets )
1065 {
1066 RN_NET* net = GetRatsnestForNet( netcode );
1067
1068 if( !net )
1069 continue;
1070
1071 for( const CN_EDGE& edge : net->GetEdges() )
1072 {
1073 const std::shared_ptr<const CN_ANCHOR>& srcNode = edge.GetSourceNode();
1074 const std::shared_ptr<const CN_ANCHOR>& dstNode = edge.GetTargetNode();
1075
1076 if( !srcNode || srcNode->Dirty() || !dstNode || dstNode->Dirty() )
1077 continue;
1078
1079 const PAD* srcParent = static_cast<const PAD*>( srcNode->Parent() );
1080 const PAD* dstParent = static_cast<const PAD*>( dstNode->Parent() );
1081
1082 bool srcFound = ( pads.find(srcParent) != pads.end() );
1083 bool dstFound = ( pads.find(dstParent) != pads.end() );
1084
1085 if ( srcFound && dstFound && !aSkipInternalConnections )
1086 edges.push_back( edge );
1087 else if ( srcFound || dstFound )
1088 edges.push_back( edge );
1089 }
1090 }
1091
1092 return edges;
1093}
1094
1095
1097{
1098 if( std::shared_ptr<NET_SETTINGS> netSettings = m_netSettings.lock() )
1099 return netSettings.get();
1100 else
1101 return nullptr;
1102}
@ ERROR_INSIDE
Definition: approximation.h:34
constexpr BOX2I KiROUND(const BOX2D &aBoxD)
Definition: box2.h:990
A base class derived from BOARD_ITEM for items that can be connected and have a net,...
std::shared_ptr< NET_SETTINGS > m_NetSettings
A base class for any item which can be embedded within the BOARD container class, and therefore insta...
Definition: board_item.h:79
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:133
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:47
Information pertinent to a Pcbnew printed circuit board.
Definition: board.h:290
const NETINFO_LIST & GetNetInfo() const
Definition: board.h:871
std::unordered_map< ZONE *, std::unique_ptr< DRC_RTREE > > m_CopperZoneRTreeCache
Definition: board.h:1298
BOARD_DESIGN_SETTINGS & GetDesignSettings() const
Definition: board.cpp:892
void CacheTriangulation(PROGRESS_REPORTER *aReporter=nullptr, const std::vector< ZONE * > &aZones={})
Definition: board.cpp:930
CN_ANCHOR represents a physical location that can be connected: a pad or a track/arc/via endpoint.
const std::list< CN_ITEM * > & GetItems() const
CN_EDGE represents a point-to-point connection, whether realized or unrealized (ie: tracks etc.
CN_ITEM represents a BOARD_CONNETED_ITEM in the connectivity system (ie: a pad, track/arc/via,...
const std::vector< CN_ITEM * > & ConnectedItems() const
int Net() const
bool Valid() const
BOARD_CONNECTED_ITEM * Parent() const
int SubpolyIndex() const
void FillIsolatedIslandsMap(std::map< ZONE *, std::map< PCB_LAYER_ID, ISOLATED_ISLANDS > > &aMap, bool aConnectivityAlreadyRebuilt=false)
Fill the isolate islands list for each layer of each zone.
void RecalculateRatsnest(BOARD_COMMIT *aCommit=nullptr)
Function RecalculateRatsnest() Updates the ratsnest for the board.
void ClearLocalRatsnest()
Function ClearLocalRatsnest() Erases the temporary, selection-based ratsnest (i.e.
PROGRESS_REPORTER * m_progressReporter
unsigned int GetPadCount(int aNet=-1) const
void MarkItemNetAsDirty(BOARD_ITEM *aItem)
std::weak_ptr< NET_SETTINGS > m_netSettings
Used to get netclass data when drawing ratsnests.
void PropagateNets(BOARD_COMMIT *aCommit=nullptr)
Propagates the net codes from the source pads to the tracks/vias.
void RunOnUnconnectedEdges(std::function< bool(CN_EDGE &)> aFunc)
std::vector< RN_DYNAMIC_LINE > m_dynamicRatsnest
bool m_skipRatsnestUpdate
Used to suppress ratsnest calculations on dynamic ratsnests.
const std::vector< CN_EDGE > GetRatsnestForPad(const PAD *aPad)
RN_NET * GetRatsnestForNet(int aNet)
Function GetRatsnestForNet() Returns the ratsnest, expressed as a set of graph edges for a given net.
const std::vector< BOARD_CONNECTED_ITEM * > GetConnectedItemsAtAnchor(const BOARD_CONNECTED_ITEM *aItem, const VECTOR2I &aAnchor, const std::vector< KICAD_T > &aTypes, const int &aMaxError=0) const
Function GetConnectedItemsAtAnchor() Returns a list of items connected to a source item aItem at posi...
void ClearRatsnest()
Function Clear() Erases the connectivity database.
bool Remove(BOARD_ITEM *aItem)
Function Remove() Removes an item from the connectivity data.
void GetConnectedPadsAndVias(const BOARD_CONNECTED_ITEM *aItem, std::vector< PAD * > *pads, std::vector< PCB_VIA * > *vias)
const NET_SETTINGS * GetNetSettings() const
void ComputeLocalRatsnest(const std::vector< BOARD_ITEM * > &aItems, const CONNECTIVITY_DATA *aDynamicData, VECTOR2I aInternalOffset={ 0, 0 })
Function ComputeLocalRatsnest() Calculates the temporary (usually selection-based) ratsnest for the s...
bool TestTrackEndpointDangling(PCB_TRACK *aTrack, bool aIgnoreTracksInPads, VECTOR2I *aPos=nullptr) const
unsigned int GetNodeCount(int aNet=-1) const
void SetProgressReporter(PROGRESS_REPORTER *aReporter)
void BlockRatsnestItems(const std::vector< BOARD_ITEM * > &aItems)
bool IsConnectedOnLayer(const BOARD_CONNECTED_ITEM *aItem, int aLayer, const std::initializer_list< KICAD_T > &aTypes={}) const
const std::vector< PCB_TRACK * > GetConnectedTracks(const BOARD_CONNECTED_ITEM *aItem) const
const std::vector< CN_EDGE > GetRatsnestForComponent(FOOTPRINT *aComponent, bool aSkipInternalConnections=false)
const std::vector< BOARD_CONNECTED_ITEM * > GetNetItems(int aNetCode, const std::vector< KICAD_T > &aTypes) const
Function GetNetItems() Returns the list of items that belong to a certain net.
bool Add(BOARD_ITEM *aItem)
Function Add() Adds an item to the connectivity data.
std::shared_ptr< CN_CONNECTIVITY_ALGO > m_connAlgo
bool Build(BOARD *aBoard, PROGRESS_REPORTER *aReporter=nullptr)
Function Build() Builds the connectivity database for the board aBoard.
const std::vector< BOARD_CONNECTED_ITEM * > GetConnectedItems(const BOARD_CONNECTED_ITEM *aItem, const std::vector< KICAD_T > &aTypes, bool aIgnoreNetcodes=false) const
Function GetConnectedItems() Returns a list of items connected to a source item aItem.
std::shared_ptr< FROM_TO_CACHE > m_fromToCache
const std::vector< PAD * > GetConnectedPads(const BOARD_CONNECTED_ITEM *aItem) const
unsigned int GetUnconnectedCount(bool aVisibileOnly) const
std::map< int, wxString > m_netcodeMap
Used to map netcode to net name.
void internalRecalculateRatsnest(BOARD_COMMIT *aCommit=nullptr)
Updates the ratsnest for the board without locking the connectivity mutex.
void RefreshNetcodeMap(BOARD *aBoard)
Refresh the map of netcodes to net names.
void HideLocalRatsnest()
Hides the temporary, selection-based ratsnest lines.
const std::vector< CN_EDGE > GetRatsnestForItems(const std::vector< BOARD_ITEM * > &aItems)
void addRatsnestCluster(const std::shared_ptr< CN_CLUSTER > &aCluster)
std::vector< RN_NET * > m_nets
bool Update(BOARD_ITEM *aItem)
Function Update() Updates the connectivity data for an item.
void Move(const VECTOR2I &aDelta)
Moves the connectivity list anchors.
int GetNetCount() const
Function GetNetCount() Returns the total number of nets in the connectivity database.
std::shared_ptr< CN_CONNECTIVITY_ALGO > GetConnectivityAlgo() const
Implement an R-tree for fast spatial and layer indexing of connectable items.
Definition: drc_rtree.h:48
int QueryColliding(BOARD_ITEM *aRefItem, PCB_LAYER_ID aRefLayer, PCB_LAYER_ID aTargetLayer, std::function< bool(BOARD_ITEM *)> aFilter=nullptr, std::function< bool(BOARD_ITEM *)> aVisitor=nullptr, int aClearance=0) const
This is a fast test which essentially does bounding-box overlap given a worst-case clearance.
Definition: drc_rtree.h:215
virtual VECTOR2I GetPosition() const
Definition: eda_item.h:243
KICAD_T Type() const
Returns the type of object.
Definition: eda_item.h:101
EDA_ITEM_FLAGS GetFlags() const
Definition: eda_item.h:130
std::deque< PAD * > & Pads()
Definition: footprint.h:206
Handle the data for a net.
Definition: netinfo.h:56
NET_SETTINGS stores various net-related settings in a project context.
Definition: net_settings.h:39
Definition: pad.h:54
const VECTOR2I & GetStart() const
Definition: pcb_track.h:122
VECTOR2I GetPosition() const override
Definition: pcb_track.h:112
const VECTOR2I & GetEnd() const
Definition: pcb_track.h:119
virtual int GetWidth() const
Definition: pcb_track.h:116
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 KeepRefreshing(bool aWait=false)=0
Update the UI (if any).
virtual void Report(const wxString &aMessage)=0
Display aMessage in the progress bar dialog.
virtual void SetCurrentProgress(double aProgress)=0
Set the progress value to aProgress (0..1).
Describe ratsnest for a single net.
Definition: ratsnest_data.h:63
unsigned int GetNodeCount() const
Definition: ratsnest_data.h:94
const std::vector< CN_EDGE > & GetEdges() const
Definition: ratsnest_data.h:96
bool NearestBicoloredPair(RN_NET *aOtherNet, VECTOR2I &aPos1, VECTOR2I &aPos2) const
void AddCluster(std::shared_ptr< CN_CLUSTER > aCluster)
VECTOR2I::extended_type ecoord
Definition: seg.h:44
const BOX2I BBox(int aClearance=0) const override
Compute a bounding box of the shape, with a margin of aClearance a collision.
Definition: shape_circle.h:70
bool PointInside(const VECTOR2I &aPt, int aAccuracy=0, bool aUseBBoxCache=false) const override
Check if point aP lies inside a closed shape.
Represent a polyline containing arcs as well as line segments: A chain of connected line and/or arc s...
const std::vector< VECTOR2I > & CPoints() const
Represent a set of closed polygons.
const SHAPE_LINE_CHAIN & COutline(int aIndex) const
double Distance(const VECTOR2< extended_type > &aVector) const
Compute the distance between two vectors.
Definition: vector2d.h:557
Handle a list of polygons defining a copper zone.
Definition: zone.h:73
bool IsFilled() const
Definition: zone.h:261
SHAPE_POLY_SET * GetFill(PCB_LAYER_ID aLayer)
Definition: zone.h:623
static int getMinDist(BOARD_CONNECTED_ITEM *aItem, const VECTOR2I &aPoint)
#define _(s)
#define IS_DELETED
PCB_LAYER_ID
A quick note on layer IDs:
Definition: layer_ids.h:60
@ B_Cu
Definition: layer_ids.h:65
@ UNDEFINED_LAYER
Definition: layer_ids.h:61
PCB_LAYER_ID ToLAYER_ID(int aLayer)
Definition: lset.cpp:810
bool contains(const _Container &__container, _Value __value)
Returns true if the container contains the given value.
Definition: kicad_algo.h:100
Class that computes missing connections on a PCB.
static thread_pool * tp
Definition: thread_pool.cpp:30
BS::thread_pool thread_pool
Definition: thread_pool.h:30
thread_pool & GetKiCadThreadPool()
Get a reference to the current thread pool.
Definition: thread_pool.cpp:32
KICAD_T
The set of class identification values stored in EDA_ITEM::m_structType.
Definition: typeinfo.h:78
@ PCB_VIA_T
class PCB_VIA, a via (like a track segment on a copper layer)
Definition: typeinfo.h:97
@ MAX_STRUCT_TYPE_ID
Definition: typeinfo.h:237
@ 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_TRACE_T
class PCB_TRACK, a track segment (segment on a copper layer)
Definition: typeinfo.h:96