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 The 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
36#include <properties/property.h>
38#include <board_item.h>
43#include <footprint.h>
44#include <pad.h>
45#include <pcb_track.h>
47#include <progress_reporter.h>
48#include <thread_pool.h>
49#include <trigo.h>
50#include <drc/drc_rtree.h>
52
60
61
62CONNECTIVITY_DATA::CONNECTIVITY_DATA( std::shared_ptr<CONNECTIVITY_DATA> aGlobalConnectivity,
63 const std::vector<BOARD_ITEM*>& aLocalItems,
64 bool aSkipRatsnestUpdate ) :
65 m_skipRatsnestUpdate( aSkipRatsnestUpdate )
66{
67 Build( aGlobalConnectivity, aLocalItems );
68 m_progressReporter = nullptr;
69 m_fromToCache.reset( new FROM_TO_CACHE );
70}
71
72
74{
75 for( RN_NET* net : m_nets )
76 delete net;
77
78 m_nets.clear();
79}
80
81
83{
84 m_connAlgo->Add( aItem );
85 return true;
86}
87
88
90{
91 m_connAlgo->Remove( aItem );
92 return true;
93}
94
95
97{
98 m_connAlgo->Remove( aItem );
99 m_connAlgo->Add( aItem );
100 return true;
101}
102
103
105{
106 aBoard->CacheTriangulation( aReporter );
107
108 std::unique_lock<KISPINLOCK> lock( m_lock, std::try_to_lock );
109
110 if( !lock )
111 return false;
112
113 if( aReporter )
114 {
115 aReporter->Report( _( "Updating nets..." ) );
116 aReporter->KeepRefreshing( false );
117 }
118
119 for( RN_NET* net : m_nets )
120 delete net;
121
122 m_nets.clear();
123
124 m_connAlgo.reset( new CN_CONNECTIVITY_ALGO( this ) );
125 m_connAlgo->Build( aBoard, aReporter );
126
128
129 RefreshNetcodeMap( aBoard );
130
131 if( aReporter )
132 {
133 aReporter->SetCurrentProgress( 0.75 );
134 aReporter->KeepRefreshing( false );
135 }
136
138
139 if( aReporter )
140 {
141 aReporter->SetCurrentProgress( 1.0 );
142 aReporter->KeepRefreshing( false );
143 }
144
145 return true;
146}
147
148
150{
151 m_netcodeMap.clear();
152
153 for( NETINFO_ITEM* net : aBoard->GetNetInfo() )
154 m_netcodeMap[net->GetNetCode()] = net->GetNetname();
155}
156
157
158void CONNECTIVITY_DATA::Build( std::shared_ptr<CONNECTIVITY_DATA>& aGlobalConnectivity,
159 const std::vector<BOARD_ITEM*>& aLocalItems )
160{
161 std::unique_lock<KISPINLOCK> lock( m_lock, std::try_to_lock );
162
163 if( !lock )
164 return;
165
166 m_connAlgo.reset( new CN_CONNECTIVITY_ALGO( this ) );
167 m_connAlgo->LocalBuild( aGlobalConnectivity, aLocalItems );
168
170}
171
172
174{
175 m_connAlgo->ForEachAnchor( [&aDelta]( CN_ANCHOR& anchor )
176 {
177 anchor.Move( aDelta );
178 } );
179}
180
181
183{
184#ifdef PROFILE
185 PROF_TIMER rnUpdate( "update-ratsnest" );
186#endif
187
188 std::vector<RN_NET*> dirty_nets;
189
190 // Start with net 1 as net 0 is reserved for not-connected
191 // Nets without nodes are also ignored
192 std::copy_if( m_nets.begin() + 1, m_nets.end(), std::back_inserter( dirty_nets ),
193 [] ( RN_NET* aNet )
194 {
195 return aNet->IsDirty() && aNet->GetNodeCount() > 0;
196 } );
197
199
200 auto results = tp.submit_loop( 0, dirty_nets.size(),
201 [&]( const int ii )
202 {
203 dirty_nets[ii]->UpdateNet();
204 } );
205 results.wait();
206
207 auto results2 = tp.submit_loop( 0, dirty_nets.size(),
208 [&]( const int ii )
209 {
210 dirty_nets[ii]->OptimizeRNEdges();
211 } );
212 results2.wait();
213
214#ifdef PROFILE
215 rnUpdate.Show();
216#endif
217}
218
219
220void CONNECTIVITY_DATA::addRatsnestCluster( const std::shared_ptr<CN_CLUSTER>& aCluster )
221{
222 RN_NET* rnNet = m_nets[ aCluster->OriginNet() ];
223
224 rnNet->AddCluster( aCluster );
225}
226
227
229{
230
231 // We can take over the lock here if called in the same thread
232 // This is to prevent redraw during a RecalculateRatsnets process
233 std::unique_lock<KISPINLOCK> lock( m_lock );
234
236
237}
238
240{
241 m_connAlgo->PropagateNets( aCommit );
242
243 int lastNet = m_connAlgo->NetCount();
244
245 if( lastNet >= (int) m_nets.size() )
246 {
247 unsigned int prevSize = m_nets.size();
248 m_nets.resize( lastNet + 1 );
249
250 for( unsigned int i = prevSize; i < m_nets.size(); i++ )
251 m_nets[i] = new RN_NET;
252 }
253 else
254 {
255 for( size_t ii = lastNet; ii < m_nets.size(); ++ii )
256 m_nets[ii]->Clear();
257 }
258
259 const std::vector<std::shared_ptr<CN_CLUSTER>>& clusters = m_connAlgo->GetClusters();
260
261 for( int net = 0; net < lastNet; net++ )
262 {
263 if( m_connAlgo->IsNetDirty( net ) )
264 m_nets[net]->Clear();
265 }
266
267 for( const std::shared_ptr<CN_CLUSTER>& c : clusters )
268 {
269 int net = c->OriginNet();
270
271 // Don't add intentionally-kept zone islands to the ratsnest
272 if( c->IsOrphaned() && c->Size() == 1 )
273 {
274 if( dynamic_cast<CN_ZONE_LAYER*>( *c->begin() ) )
275 continue;
276 }
277
278 if( m_connAlgo->IsNetDirty( net ) )
280 }
281
282 m_connAlgo->ClearDirtyFlags();
283
286}
287
288
289void CONNECTIVITY_DATA::BlockRatsnestItems( const std::vector<BOARD_ITEM*>& aItems )
290{
291 std::vector<BOARD_CONNECTED_ITEM*> citems;
292
293 for( BOARD_ITEM* item : aItems )
294 {
295 if( item->Type() == PCB_FOOTPRINT_T )
296 {
297 for( PAD* pad : static_cast<FOOTPRINT*>(item)->Pads() )
298 citems.push_back( pad );
299 }
300 else
301 {
302 if( BOARD_CONNECTED_ITEM* citem = dynamic_cast<BOARD_CONNECTED_ITEM*>( item ) )
303 citems.push_back( citem );
304 }
305 }
306
307 for( const BOARD_CONNECTED_ITEM* item : citems )
308 {
309 if ( m_connAlgo->ItemExists( item ) )
310 {
311 CN_CONNECTIVITY_ALGO::ITEM_MAP_ENTRY& entry = m_connAlgo->ItemEntry( item );
312
313 for( CN_ITEM* cnItem : entry.GetItems() )
314 {
315 for( const std::shared_ptr<CN_ANCHOR>& anchor : cnItem->Anchors() )
316 anchor->SetNoLine( true );
317 }
318 }
319 }
320}
321
322
324{
325 return m_connAlgo->NetCount();
326}
327
328
329void CONNECTIVITY_DATA::FillIsolatedIslandsMap( std::map<ZONE*, std::map<PCB_LAYER_ID, ISOLATED_ISLANDS>>& aMap,
330 bool aConnectivityAlreadyRebuilt )
331{
332 m_connAlgo->FillIsolatedIslandsMap( aMap, aConnectivityAlreadyRebuilt );
333}
334
335
336void CONNECTIVITY_DATA::ComputeLocalRatsnest( const std::vector<BOARD_ITEM*>& aItems,
337 const CONNECTIVITY_DATA* aDynamicData,
338 VECTOR2I aInternalOffset )
339{
340 if( !aDynamicData )
341 return;
342
343 m_dynamicRatsnest.clear();
344 std::mutex dynamic_ratsnest_mutex;
345
346 // This gets connections between the stationary board and the
347 // moving selection
348
349 auto update_lambda = [&]( int nc )
350 {
351 RN_NET* dynamicNet = aDynamicData->m_nets[nc];
352 RN_NET* staticNet = m_nets[nc];
353
357 if( dynamicNet->GetNodeCount() != 0 && 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 auto results = tp.submit_loop( 1, num_nets,
378 [&]( const int ii )
379 {
380 update_lambda( ii );
381 });
382 results.wait();
383
384 // This gets the ratsnest for internal connections in the moving set
385 const std::vector<CN_EDGE>& edges = GetRatsnestForItems( aItems );
386
387 for( const CN_EDGE& edge : edges )
388 {
389 const std::shared_ptr<const CN_ANCHOR>& nodeA = edge.GetSourceNode();
390 const std::shared_ptr<const CN_ANCHOR>& nodeB = edge.GetTargetNode();
391
392 if( !nodeA || nodeA->Dirty() || !nodeB || nodeB->Dirty() )
393 continue;
394
396
397 // Use the parents' positions
398 l.a = nodeA->Parent()->GetPosition() + aInternalOffset;
399 l.b = nodeB->Parent()->GetPosition() + aInternalOffset;
400 l.netCode = 0;
401 m_dynamicRatsnest.push_back( l );
402 }
403}
404
405
407{
408 m_connAlgo->ForEachAnchor( []( CN_ANCHOR& anchor )
409 {
410 anchor.SetNoLine( false );
411 } );
413}
414
415
420
421
423{
424 m_connAlgo->PropagateNets( aCommit );
425}
426
427
429 const std::initializer_list<KICAD_T>& aTypes ) const
430{
431 CN_CONNECTIVITY_ALGO::ITEM_MAP_ENTRY &entry = m_connAlgo->ItemEntry( aItem );
432
433 FOOTPRINT* parentFootprint = aItem->GetParentFootprint();
434
435 auto matchType =
436 [&]( KICAD_T aItemType )
437 {
438 if( aTypes.size() == 0 )
439 return true;
440
441 return alg::contains( aTypes, aItemType);
442 };
443
444 for( CN_ITEM* citem : entry.GetItems() )
445 {
446 for( CN_ITEM* connected : citem->ConnectedItems() )
447 {
448 CN_ZONE_LAYER* zoneLayer = dynamic_cast<CN_ZONE_LAYER*>( connected );
449
450 // lyIdx is compatible with StartLayer() and EndLayer() notation in CN_ITEM
451 // items, where B_Cu is set to INT_MAX (std::numeric_limits<int>::max())
452 int lyIdx = aLayer;
453
454 if( aLayer == B_Cu )
455 lyIdx = std::numeric_limits<int>::max();
456
457 if( connected->Valid()
458 && connected->StartLayer() <= lyIdx && connected->EndLayer() >= lyIdx
459 && matchType( connected->Parent()->Type() )
460 && connected->Net() == aItem->GetNetCode() )
461 {
462 BOARD_ITEM* connectedItem = connected->Parent();
463
464 if( connectedItem == aItem )
465 continue;
466
467 if( parentFootprint && connectedItem
468 && connectedItem->GetParentFootprint() == parentFootprint )
469 {
470 continue;
471 }
472
473 if( aItem->Type() == PCB_PAD_T && connectedItem
474 && connectedItem->Type() == PCB_PAD_T )
475 {
476 const PAD* thisPad = static_cast<const PAD*>( aItem );
477 const PAD* otherPad = static_cast<const PAD*>( connectedItem );
478
479 auto flashesConditionally = []( UNCONNECTED_LAYER_MODE aMode )
480 {
483 };
484
485 if( flashesConditionally( thisPad->Padstack().UnconnectedLayerMode() )
486 && flashesConditionally( otherPad->Padstack().UnconnectedLayerMode() ) )
487 {
488 continue;
489 }
490 }
491
492 if( aItem->Type() == PCB_PAD_T && zoneLayer )
493 {
494 const PAD* pad = static_cast<const PAD*>( aItem );
495 ZONE* zone = static_cast<ZONE*>( zoneLayer->Parent() );
496 int islandIdx = zoneLayer->SubpolyIndex();
497
498 if( zone->IsFilled() )
499 {
500 PCB_LAYER_ID pcbLayer = ToLAYER_ID( aLayer );
501 const SHAPE_POLY_SET* zoneFill = zone->GetFill( pcbLayer );
502 const SHAPE_LINE_CHAIN& padHull = pad->GetEffectivePolygon( pcbLayer,
503 ERROR_INSIDE )->Outline( 0 );
504
505 for( const VECTOR2I& pt : zoneFill->COutline( islandIdx ).CPoints() )
506 {
507 // If the entire island is inside the pad's flashing then the pad
508 // won't actually connect to anything else, so only return true if
509 // part of the island is *outside* the pad's flashing.
510
511 if( !padHull.PointInside( pt ) )
512 return true;
513 }
514 }
515
516 continue;
517 }
518 else if( aItem->Type() == PCB_VIA_T && zoneLayer )
519 {
520 const PCB_VIA* via = static_cast<const PCB_VIA*>( aItem );
521 ZONE* zone = static_cast<ZONE*>( zoneLayer->Parent() );
522 int islandIdx = zoneLayer->SubpolyIndex();
523
524 if( zone->IsFilled() )
525 {
526 PCB_LAYER_ID layer = ToLAYER_ID( aLayer );
527 const SHAPE_POLY_SET* zoneFill = zone->GetFill( layer );
528 SHAPE_CIRCLE viaHull( via->GetCenter(), via->GetWidth( layer ) / 2 );
529
530 for( const VECTOR2I& pt : zoneFill->COutline( islandIdx ).CPoints() )
531 {
532 // If the entire island is inside the via's flashing then the via
533 // won't actually connect to anything else, so only return true if
534 // part of the island is *outside* the via's flashing.
535
536 if( !viaHull.SHAPE::Collide( pt ) )
537 return true;
538 }
539 }
540
541 continue;
542 }
543
544 return true;
545 }
546 }
547 }
548
549 return false;
550}
551
552
553unsigned int CONNECTIVITY_DATA::GetUnconnectedCount( bool aVisibleOnly ) const
554{
555 unsigned int unconnected = 0;
556
557 for( RN_NET* net : m_nets )
558 {
559 if( !net )
560 continue;
561
562 for( const CN_EDGE& edge : net->GetEdges() )
563 {
564 if( edge.IsVisible() || !aVisibleOnly )
565 ++unconnected;
566 }
567 }
568
569 return unconnected;
570}
571
572
574{
575 for( RN_NET* net : m_nets )
576 net->Clear();
577}
578
579
580const std::vector<BOARD_CONNECTED_ITEM*>
582{
585
586 std::vector<BOARD_CONNECTED_ITEM*> rv;
587
588 auto clusters = m_connAlgo->SearchClusters( ( aFlags & IGNORE_NETS ) ? CSM_PROPAGATE : CSM_CONNECTIVITY_CHECK,
589 ( aFlags & EXCLUDE_ZONES ),
590 ( aFlags & IGNORE_NETS ) ? -1 : aItem->GetNetCode() );
591
592 for( const std::shared_ptr<CN_CLUSTER>& cl : clusters )
593 {
594 if( cl->Contains( aItem ) )
595 {
596 for( const CN_ITEM* item : *cl )
597 {
598 if( item->Valid() )
599 rv.push_back( item->Parent() );
600 }
601 }
602 }
603
604 return rv;
605}
606
607
608const std::vector<BOARD_CONNECTED_ITEM*>
609CONNECTIVITY_DATA::GetNetItems( int aNetCode, const std::vector<KICAD_T>& aTypes ) const
610{
611 std::vector<BOARD_CONNECTED_ITEM*> items;
612 items.reserve( 32 );
613
614 std::bitset<MAX_STRUCT_TYPE_ID> type_bits;
615
616 for( KICAD_T scanType : aTypes )
617 {
618 wxASSERT( scanType < MAX_STRUCT_TYPE_ID );
619 type_bits.set( scanType );
620 }
621
622 m_connAlgo->ForEachItem(
623 [&]( CN_ITEM& aItem )
624 {
625 if( aItem.Valid() && ( aItem.Net() == aNetCode ) && type_bits[aItem.Parent()->Type()] )
626 items.push_back( aItem.Parent() );
627 } );
628
629 std::sort( items.begin(), items.end() );
630 items.erase( std::unique( items.begin(), items.end() ), items.end() );
631 return items;
632}
633
634
635const std::vector<PCB_TRACK*>
637{
638 CN_CONNECTIVITY_ALGO::ITEM_MAP_ENTRY& entry = m_connAlgo->ItemEntry( aItem );
639
640 std::set<PCB_TRACK*> tracks;
641 std::vector<PCB_TRACK*> rv;
642
643 for( CN_ITEM* citem : entry.GetItems() )
644 {
645 for( CN_ITEM* connected : citem->ConnectedItems() )
646 {
647 if( connected->Valid() &&
648 ( connected->Parent()->Type() == PCB_TRACE_T ||
649 connected->Parent()->Type() == PCB_VIA_T ||
650 connected->Parent()->Type() == PCB_ARC_T ) )
651 {
652 tracks.insert( static_cast<PCB_TRACK*> ( connected->Parent() ) );
653 }
654 }
655 }
656
657 std::copy( tracks.begin(), tracks.end(), std::back_inserter( rv ) );
658 return rv;
659}
660
661
662void CONNECTIVITY_DATA::GetConnectedPads( const BOARD_CONNECTED_ITEM* aItem, std::set<PAD*>* pads ) const
663{
664 for( CN_ITEM* citem : m_connAlgo->ItemEntry( aItem ).GetItems() )
665 {
666 for( CN_ITEM* connected : citem->ConnectedItems() )
667 {
668 if( connected->Valid() && connected->Parent()->Type() == PCB_PAD_T )
669 pads->insert( static_cast<PAD*> ( connected->Parent() ) );
670 }
671 }
672}
673
674
675const std::vector<PAD*> CONNECTIVITY_DATA::GetConnectedPads( const BOARD_CONNECTED_ITEM* aItem )
676const
677{
678 std::set<PAD*> pads;
679 std::vector<PAD*> rv;
680
681 GetConnectedPads( aItem, &pads );
682
683 std::copy( pads.begin(), pads.end(), std::back_inserter( rv ) );
684 return rv;
685}
686
687
688void CONNECTIVITY_DATA::GetConnectedPadsAndVias( const BOARD_CONNECTED_ITEM* aItem, std::vector<PAD*>* pads,
689 std::vector<PCB_VIA*>* vias )
690{
691 for( CN_ITEM* citem : m_connAlgo->ItemEntry( aItem ).GetItems() )
692 {
693 for( CN_ITEM* connected : citem->ConnectedItems() )
694 {
695 if( connected->Valid() )
696 {
697 BOARD_CONNECTED_ITEM* parent = connected->Parent();
698
699 if( parent->Type() == PCB_PAD_T )
700 pads->push_back( static_cast<PAD*>( parent ) );
701 else if( parent->Type() == PCB_VIA_T )
702 vias->push_back( static_cast<PCB_VIA*>( parent ) );
703 }
704 }
705 }
706}
707
708
709unsigned int CONNECTIVITY_DATA::GetNodeCount( int aNet ) const
710{
711 int sum = 0;
712
713 if( aNet < 0 ) // Node count for all nets
714 {
715 for( const RN_NET* net : m_nets )
716 sum += net->GetNodeCount();
717 }
718 else if( aNet < (int) m_nets.size() )
719 {
720 sum = m_nets[aNet]->GetNodeCount();
721 }
722
723 return sum;
724}
725
726
727unsigned int CONNECTIVITY_DATA::GetPadCount( int aNet ) const
728{
729 int n = 0;
730
731 for( CN_ITEM* pad : m_connAlgo->ItemList() )
732 {
733 if( !pad->Valid() || pad->Parent()->Type() != PCB_PAD_T)
734 continue;
735
736 PAD* dpad = static_cast<PAD*>( pad->Parent() );
737
738 if( aNet < 0 || aNet == dpad->GetNetCode() )
739 n++;
740 }
741
742 return n;
743}
744
745
746void CONNECTIVITY_DATA::RunOnUnconnectedEdges( std::function<bool( CN_EDGE& )> aFunc )
747{
748 for( RN_NET* rnNet : m_nets )
749 {
750 if( rnNet )
751 {
752 for( CN_EDGE& edge : rnNet->GetEdges() )
753 {
754 if( !aFunc( edge ) )
755 return;
756 }
757 }
758 }
759}
760
761
762static int getMinDist( BOARD_CONNECTED_ITEM* aItem, const VECTOR2I& aPoint )
763{
764 switch( aItem->Type() )
765 {
766 case PCB_TRACE_T:
767 case PCB_ARC_T:
768 {
769 PCB_TRACK* track = static_cast<PCB_TRACK*>( aItem );
770
771 return std::min( track->GetStart().Distance(aPoint ), track->GetEnd().Distance( aPoint ) );
772 }
773
774 default:
775 return aItem->GetPosition().Distance( aPoint );
776 }
777}
778
779
780bool CONNECTIVITY_DATA::TestTrackEndpointDangling( PCB_TRACK* aTrack, bool aIgnoreTracksInPads,
781 VECTOR2I* aPos ) const
782{
783 const std::list<CN_ITEM*>& items = GetConnectivityAlgo()->ItemEntry( aTrack ).GetItems();
784
785 // Not in the connectivity system. This is a bug!
786 if( items.empty() )
787 {
788 wxFAIL_MSG( wxT( "track not in connectivity system" ) );
789 return false;
790 }
791
792 CN_ITEM* citem = items.front();
793
794 if( !citem->Valid() )
795 return false;
796
797 if( aTrack->Type() == PCB_TRACE_T || aTrack->Type() == PCB_ARC_T )
798 {
799 // Test if a segment is connected on each end.
800 //
801 // NB: be wary of short segments which can be connected to the *same* other item on
802 // each end. If that's their only connection then they're still dangling.
803
804 PCB_LAYER_ID layer = aTrack->GetLayer();
805 int accuracy = KiROUND( aTrack->GetWidth() / 2.0 );
806 int start_count = 0;
807 int end_count = 0;
808
809 for( CN_ITEM* connected : citem->ConnectedItems() )
810 {
811 BOARD_CONNECTED_ITEM* item = connected->Parent();
812 ZONE* zone = dynamic_cast<ZONE*>( item );
813 DRC_RTREE* rtree = nullptr;
814 bool hitStart = false;
815 bool hitEnd = false;
816
817 if( item->GetFlags() & IS_DELETED )
818 continue;
819
820 if( zone )
821 rtree = zone->GetBoard()->m_CopperZoneRTreeCache[ zone ].get();
822
823 if( rtree )
824 {
825 SHAPE_CIRCLE start( aTrack->GetStart(), accuracy );
826 SHAPE_CIRCLE end( aTrack->GetEnd(), accuracy );
827
828 hitStart = rtree->QueryColliding( start.BBox(), &start, layer );
829 hitEnd = rtree->QueryColliding( end.BBox(), &end, layer );
830 }
831 else
832 {
833 std::shared_ptr<SHAPE> shape = item->GetEffectiveShape( layer );
834
835 hitStart = shape->Collide( aTrack->GetStart(), accuracy );
836 hitEnd = shape->Collide( aTrack->GetEnd(), accuracy );
837 }
838
839 if( hitStart && hitEnd )
840 {
841 if( zone )
842 {
843 // Both start and end in a zone: track may be redundant, but it's not dangling
844 return false;
845 }
846 else if( item->Type() == PCB_PAD_T || item->Type() == PCB_VIA_T )
847 {
848 // Both start and end are under a pad: see what the caller wants us to do
849 if( aIgnoreTracksInPads )
850 return false;
851 }
852
853 if( getMinDist( item, aTrack->GetStart() ) < getMinDist( item, aTrack->GetEnd() ) )
854 start_count++;
855 else
856 end_count++;
857 }
858 else if( hitStart )
859 {
860 start_count++;
861 }
862 else if( hitEnd )
863 {
864 end_count++;
865 }
866
867 if( start_count > 0 && end_count > 0 )
868 return false;
869 }
870
871 if( aPos )
872 *aPos = (start_count == 0 ) ? aTrack->GetStart() : aTrack->GetEnd();
873
874 return true;
875 }
876 else if( aTrack->Type() == PCB_VIA_T )
877 {
878 // Test if a via is only connected on one layer
879
880 const std::vector<CN_ITEM*>& connected = citem->ConnectedItems();
881
882 if( connected.empty() )
883 {
884 // No connections AND no-net is not an error
885 if( aTrack->GetNetCode() <= 0 )
886 return false;
887
888 if( aPos )
889 *aPos = aTrack->GetPosition();
890
891 return true;
892 }
893
894 // Here, we check if the via is connected only to items on a single layer
895 int first_layer = UNDEFINED_LAYER;
896
897 for( CN_ITEM* item : connected )
898 {
899 if( item->Parent()->GetFlags() & IS_DELETED )
900 continue;
901
902 if( first_layer == UNDEFINED_LAYER )
903 first_layer = item->Layer();
904 else if( item->Layer() != first_layer )
905 return false;
906 }
907
908 if( aPos )
909 *aPos = aTrack->GetPosition();
910
911 return true;
912 }
913 else
914 {
915 wxFAIL_MSG( wxT( "CONNECTIVITY_DATA::TestTrackEndpointDangling: unknown track type" ) );
916 }
917
918 return false;
919}
920
921
922const std::vector<BOARD_CONNECTED_ITEM*>
924 const std::vector<KICAD_T>& aTypes, const int& aMaxError ) const
925{
926 CN_CONNECTIVITY_ALGO::ITEM_MAP_ENTRY& entry = m_connAlgo->ItemEntry( aItem );
927 std::vector<BOARD_CONNECTED_ITEM*> rv;
928 SEG::ecoord maxError_sq = (SEG::ecoord) aMaxError * aMaxError;
929
930 for( CN_ITEM* cnItem : entry.GetItems() )
931 {
932 for( CN_ITEM* connected : cnItem->ConnectedItems() )
933 {
934 for( const std::shared_ptr<CN_ANCHOR>& anchor : connected->Anchors() )
935 {
936 if( ( anchor->Pos() - aAnchor ).SquaredEuclideanNorm() <= maxError_sq )
937 {
938 for( KICAD_T type : aTypes )
939 {
940 if( connected->Valid() && connected->Parent()->Type() == type )
941 {
942 rv.push_back( connected->Parent() );
943 break;
944 }
945 }
946
947 break;
948 }
949 }
950 }
951 }
952
953 return rv;
954}
955
956
958{
959 if ( aNet < 0 || aNet >= (int) m_nets.size() )
960 return nullptr;
961
962 return m_nets[ aNet ];
963}
964
965
967{
968 if ( aItem->Type() == PCB_FOOTPRINT_T)
969 {
970 for( PAD* pad : static_cast<FOOTPRINT*>( aItem )->Pads() )
971 m_connAlgo->MarkNetAsDirty( pad->GetNetCode() );
972 }
973
974 if (aItem->IsConnected() )
975 m_connAlgo->MarkNetAsDirty( static_cast<BOARD_CONNECTED_ITEM*>( aItem )->GetNetCode() );
976}
977
978
980{
981 m_connAlgo->RemoveInvalidRefs();
982
983 for( RN_NET* rnNet : m_nets )
984 rnNet->RemoveInvalidRefs();
985}
986
987
989{
990 m_progressReporter = aReporter;
991 m_connAlgo->SetProgressReporter( m_progressReporter );
992}
993
994
995const std::vector<CN_EDGE>
996CONNECTIVITY_DATA::GetRatsnestForItems( const std::vector<BOARD_ITEM*>& aItems )
997{
998 std::set<int> nets;
999 std::vector<CN_EDGE> edges;
1000 std::set<BOARD_CONNECTED_ITEM*> item_set;
1001
1002 for( BOARD_ITEM* item : aItems )
1003 {
1004 if( item->Type() == PCB_FOOTPRINT_T )
1005 {
1006 FOOTPRINT* footprint = static_cast<FOOTPRINT*>( item );
1007
1008 for( PAD* pad : footprint->Pads() )
1009 {
1010 nets.insert( pad->GetNetCode() );
1011 item_set.insert( pad );
1012 }
1013 }
1014 else if( item->IsConnected() )
1015 {
1016 BOARD_CONNECTED_ITEM* conn_item = static_cast<BOARD_CONNECTED_ITEM*>( item );
1017
1018 item_set.insert( conn_item );
1019 nets.insert( conn_item->GetNetCode() );
1020 }
1021 }
1022
1023 for( int netcode : nets )
1024 {
1025 RN_NET* net = GetRatsnestForNet( netcode );
1026
1027 if( !net )
1028 continue;
1029
1030 for( const CN_EDGE& edge : net->GetEdges() )
1031 {
1032 std::shared_ptr<const CN_ANCHOR> srcNode = edge.GetSourceNode();
1033 std::shared_ptr<const CN_ANCHOR> dstNode = edge.GetTargetNode();
1034
1035 if( !srcNode || srcNode->Dirty() || !dstNode || dstNode->Dirty() )
1036 continue;
1037
1038 BOARD_CONNECTED_ITEM* srcParent = srcNode->Parent();
1039 BOARD_CONNECTED_ITEM* dstParent = dstNode->Parent();
1040
1041 bool srcFound = ( item_set.find( srcParent ) != item_set.end() );
1042 bool dstFound = ( item_set.find( dstParent ) != item_set.end() );
1043
1044 if ( srcFound && dstFound )
1045 edges.push_back( edge );
1046 }
1047 }
1048
1049 return edges;
1050}
1051
1052
1053const std::vector<CN_EDGE> CONNECTIVITY_DATA::GetRatsnestForPad( const PAD* aPad )
1054{
1055 std::vector<CN_EDGE> edges;
1056 RN_NET* net = GetRatsnestForNet( aPad->GetNetCode() );
1057
1058 if( !net )
1059 return edges;
1060
1061 for( const CN_EDGE& edge : net->GetEdges() )
1062 {
1063 if( !edge.GetSourceNode() || edge.GetSourceNode()->Dirty() )
1064 continue;
1065
1066 if( !edge.GetTargetNode() || edge.GetTargetNode()->Dirty() )
1067 continue;
1068
1069 if( edge.GetSourceNode()->Parent() == aPad || edge.GetTargetNode()->Parent() == aPad )
1070 edges.push_back( edge );
1071 }
1072
1073 return edges;
1074}
1075
1076
1077const std::vector<CN_EDGE> CONNECTIVITY_DATA::GetRatsnestForComponent( FOOTPRINT* aComponent,
1078 bool aSkipInternalConnections )
1079{
1080 std::set<int> nets;
1081 std::set<const PAD*> pads;
1082 std::vector<CN_EDGE> edges;
1083
1084 for( PAD* pad : aComponent->Pads() )
1085 {
1086 nets.insert( pad->GetNetCode() );
1087 pads.insert( pad );
1088 }
1089
1090 for( int netcode : nets )
1091 {
1092 RN_NET* net = GetRatsnestForNet( netcode );
1093
1094 if( !net )
1095 continue;
1096
1097 for( const CN_EDGE& edge : net->GetEdges() )
1098 {
1099 const std::shared_ptr<const CN_ANCHOR>& srcNode = edge.GetSourceNode();
1100 const std::shared_ptr<const CN_ANCHOR>& dstNode = edge.GetTargetNode();
1101
1102 if( !srcNode || srcNode->Dirty() || !dstNode || dstNode->Dirty() )
1103 continue;
1104
1105 const PAD* srcParent = static_cast<const PAD*>( srcNode->Parent() );
1106 const PAD* dstParent = static_cast<const PAD*>( dstNode->Parent() );
1107
1108 bool srcFound = ( pads.find(srcParent) != pads.end() );
1109 bool dstFound = ( pads.find(dstParent) != pads.end() );
1110
1111 if ( srcFound && dstFound && !aSkipInternalConnections )
1112 edges.push_back( edge );
1113 else if ( srcFound || dstFound )
1114 edges.push_back( edge );
1115 }
1116 }
1117
1118 return edges;
1119}
1120
1121
1123{
1124 if( std::shared_ptr<NET_SETTINGS> netSettings = m_netSettings.lock() )
1125 return netSettings.get();
1126 else
1127 return nullptr;
1128}
@ ERROR_INSIDE
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,...
PCB_LAYER_ID GetLayer() const override
Return the primary layer this item is on.
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:84
virtual bool IsConnected() const
Returns information if the object is derived from BOARD_CONNECTED_ITEM.
Definition board_item.h:139
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
Information pertinent to a Pcbnew printed circuit board.
Definition board.h:322
const NETINFO_LIST & GetNetInfo() const
Definition board.h:996
std::unordered_map< ZONE *, std::unique_ptr< DRC_RTREE > > m_CopperZoneRTreeCache
Definition board.h:1472
BOARD_DESIGN_SETTINGS & GetDesignSettings() const
Definition board.cpp:1083
void CacheTriangulation(PROGRESS_REPORTER *aReporter=nullptr, const std::vector< ZONE * > &aZones={})
Definition board.cpp:1141
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
ITEM_MAP_ENTRY & ItemEntry(const BOARD_CONNECTED_ITEM *aItem)
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.
const std::vector< BOARD_CONNECTED_ITEM * > GetConnectedItems(const BOARD_CONNECTED_ITEM *aItem, int aFlags=0) const
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.
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:49
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:221
virtual VECTOR2I GetPosition() const
Definition eda_item.h:278
KICAD_T Type() const
Returns the type of object.
Definition eda_item.h:111
EDA_ITEM_FLAGS GetFlags() const
Definition eda_item.h:151
std::deque< PAD * > & Pads()
Definition footprint.h:306
Handle the data for a net.
Definition netinfo.h:54
NET_SETTINGS stores various net-related settings in a project context.
UNCONNECTED_LAYER_MODE UnconnectedLayerMode() const
Definition padstack.h:363
Definition pad.h:55
const PADSTACK & Padstack() const
Definition pad.h:333
const VECTOR2I & GetStart() const
Definition pcb_track.h:97
VECTOR2I GetPosition() const override
Definition pcb_track.h:87
const VECTOR2I & GetEnd() const
Definition pcb_track.h:94
virtual int GetWidth() const
Definition pcb_track.h:91
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.
unsigned int GetNodeCount() const
const std::vector< CN_EDGE > & GetEdges() const
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.
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:561
Handle a list of polygons defining a copper zone.
Definition zone.h:73
bool IsFilled() const
Definition zone.h:297
SHAPE_POLY_SET * GetFill(PCB_LAYER_ID aLayer)
Definition zone.h:613
static int getMinDist(BOARD_CONNECTED_ITEM *aItem, const VECTOR2I &aPoint)
#define EXCLUDE_ZONES
#define IGNORE_NETS
Function GetConnectedItems() Returns a list of items connected to a source item aItem.
#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:754
bool contains(const _Container &__container, _Value __value)
Returns true if the container contains the given value.
Definition kicad_algo.h:100
UNCONNECTED_LAYER_MODE
Definition padstack.h:128
Class that computes missing connections on a PCB.
VECTOR2I end
const int accuracy
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
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:242
@ 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
VECTOR2< int32_t > VECTOR2I
Definition vector2d.h:695