KiCad PCB EDA Suite
Loading...
Searching...
No Matches
connectivity_items.cpp
Go to the documentation of this file.
1
2/*
3 * This program source code file is part of KICAD, a free EDA CAD application.
4 *
5 * Copyright (C) 2016-2018 CERN
6 * Copyright The KiCad Developers, see AUTHORS.txt for contributors.
7 *
8 * @author Tomasz Wlostowski <[email protected]>
9 *
10 * This program is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU General Public License
12 * as published by the Free Software Foundation; either version 2
13 * of the License, or (at your option) any later version.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License
21 * along with this program. If not, see <https://www.gnu.org/licenses/>.
22 */
23
24#include <core/kicad_algo.h>
25#include <macros.h>
27#include <trigo.h>
28
29#include <footprint.h>
30#include <pad.h>
31#include <pcb_shape.h>
32#include <pcb_track.h>
33
34#include <wx/log.h>
35
37{
38 if( !m_valid )
39 return 0;
40
41 switch( m_parent->Type() )
42 {
43 case PCB_TRACE_T:
44 case PCB_ARC_T:
45 return 2; // start and end
46
47 case PCB_SHAPE_T:
48 return m_anchors.size();
49
50 default:
51 return 1;
52 }
53}
54
55
56const VECTOR2I CN_ITEM::GetAnchor( int n ) const
57{
58 if( !m_valid )
59 return VECTOR2I();
60
61 switch( m_parent->Type() )
62 {
63 case PCB_PAD_T:
64 return static_cast<PAD*>( m_parent )->GetPosition();
65
66 case PCB_TRACE_T:
67 case PCB_ARC_T:
68 if( n == 0 )
69 return static_cast<const PCB_TRACK*>( m_parent )->GetStart();
70 else
71 return static_cast<const PCB_TRACK*>( m_parent )->GetEnd();
72
73 case PCB_VIA_T:
74 return static_cast<const PCB_VIA*>( m_parent )->GetStart();
75
76 case PCB_SHAPE_T:
77 return ( n < static_cast<int>( m_anchors.size() ) ) ? m_anchors[n]->Pos() : VECTOR2I();
78
79 default:
80 UNIMPLEMENTED_FOR( m_parent->GetClass() );
81 return VECTOR2I();
82 }
83}
84
85
87{
88 wxLogTrace( wxT( "CN" ), wxT( " valid: %d, connected: \n" ), !!Valid() );
89
90 for( CN_ITEM* i : m_connected )
91 {
92 PCB_TRACK* t = static_cast<PCB_TRACK*>( i->Parent() );
93 wxLogTrace( wxT( "CN" ), wxT( " - %p %d\n" ), t, t->Type() );
94 }
95}
96
97
99{
100 if( !Valid() || !HasValidOutline() )
101 return 0;
102
103 return GetOutline().PointCount() ? 1 : 0;
104}
105
106
108{
109 if( !Valid() || !HasValidOutline() )
110 return VECTOR2I();
111
112 return GetOutline().CPoint( 0 );
113}
114
115
117{
118 int count = 0;
119
120 for( CN_ITEM* item : ConnectedItems() )
121 {
122 if( item->Valid() )
123 count++;
124
125 if( count > 1 )
126 break;
127 }
128
129 return count == 1;
130}
131
132
134{
135 for( auto it = m_connected.begin(); it != m_connected.end(); /* increment in loop */ )
136 {
137 if( !(*it)->Valid() )
138 it = m_connected.erase( it );
139 else
140 ++it;
141 }
142}
143
144
146{
147 if( !pad->IsOnCopperLayer() )
148 return nullptr;
149
150 auto item = new CN_ITEM( pad, false, 1 );
151
152 std::set<VECTOR2I> uniqueAnchors;
153 pad->Padstack().ForEachUniqueLayer(
154 [&]( PCB_LAYER_ID aLayer )
155 {
156 uniqueAnchors.insert( pad->ShapePos( aLayer ) );
157 } );
158
159 for( const VECTOR2I& anchor : uniqueAnchors )
160 item->AddAnchor( anchor );
161
162 item->SetLayers( F_Cu, B_Cu );
163
164 switch( pad->GetAttribute() )
165 {
166 case PAD_ATTRIB::SMD:
167 case PAD_ATTRIB::NPTH:
168 case PAD_ATTRIB::CONN:
169 {
170 LSEQ lmsk = pad->GetLayerSet().CuStack();
171
172 if( !lmsk.empty() )
173 item->SetLayer( lmsk.front() );
174
175 break;
176 }
177
178 default:
179 break;
180 }
181
182 addItemtoTree( item );
183 m_items.push_back( item );
184
185 // Re-mark dirty after tree insertion since BBox() clears the dirty flag
186 item->SetDirty( true );
187 SetDirty();
188 return item;
189}
190
191
193{
194 CN_ITEM* item = new CN_ITEM( track, true );
195 m_items.push_back( item );
196 item->AddAnchor( track->GetStart() );
197 item->AddAnchor( track->GetEnd() );
198 item->SetLayer( track->GetLayer() );
199 addItemtoTree( item );
200
201 // Re-mark dirty after tree insertion since BBox() clears the dirty flag
202 item->SetDirty( true );
203 SetDirty();
204 return item;
205}
206
207
209{
210 CN_ITEM* item = new CN_ITEM( aArc, true );
211 m_items.push_back( item );
212 item->AddAnchor( aArc->GetStart() );
213 item->AddAnchor( aArc->GetEnd() );
214 item->SetLayer( aArc->GetLayer() );
215 addItemtoTree( item );
216
217 // Re-mark dirty after tree insertion since BBox() clears the dirty flag
218 item->SetDirty( true );
219 SetDirty();
220 return item;
221}
222
223
225{
226 CN_ITEM* item = new CN_ITEM( via, !via->GetIsFree(), 1 );
227
228 m_items.push_back( item );
229 item->AddAnchor( via->GetStart() );
230
231 item->SetLayers( via->TopLayer(), via->BottomLayer() );
232 addItemtoTree( item );
233
234 // Re-mark dirty after tree insertion since BBox() clears the dirty flag
235 item->SetDirty( true );
236 SetDirty();
237 return item;
238}
239
240
241const std::vector<CN_ITEM*> CN_LIST::Add( ZONE* zone, PCB_LAYER_ID aLayer )
242{
243 const std::shared_ptr<SHAPE_POLY_SET>& polys = zone->GetFilledPolysList( aLayer );
244
245 std::vector<CN_ITEM*> rv;
246
247 for( int j = 0; j < polys->OutlineCount(); j++ )
248 {
249 CN_ZONE_LAYER* zitem = new CN_ZONE_LAYER( zone, aLayer, j );
250
251 zitem->BuildRTree();
252
253 for( const VECTOR2I& pt : zone->GetFilledPolysList( aLayer )->COutline( j ).CPoints() )
254 zitem->AddAnchor( pt );
255
256 rv.push_back( Add( zitem ) );
257 }
258
259 return rv;
260}
261
262
264{
265 m_items.push_back( zitem );
266 addItemtoTree( zitem );
267
268 // Re-mark dirty after tree insertion since BBox() clears the dirty flag
269 zitem->SetDirty( true );
270 SetDirty();
271 return zitem;
272}
273
274
276{
277 CN_ITEM* item = new CN_ITEM( shape, true );
278 m_items.push_back( item );
279
280 for( const VECTOR2I& point : shape->GetConnectionPoints() )
281 item->AddAnchor( point );
282
283 item->SetLayer( shape->GetLayer() );
284 addItemtoTree( item );
285
286 // Re-mark dirty after tree insertion since BBox() clears the dirty flag
287 item->SetDirty( true );
288 SetDirty();
289 return item;
290}
291
292
293void CN_LIST::RemoveInvalidItems( std::vector<CN_ITEM*>& aGarbage )
294{
295 if( !m_hasInvalid )
296 return;
297
298 auto lastItem = std::remove_if( m_items.begin(), m_items.end(),
299 [&aGarbage]( CN_ITEM* item )
300 {
301 if( !item->Valid() )
302 {
303 aGarbage.push_back ( item );
304 return true;
305 }
306
307 return false;
308 } );
309
310 m_items.resize( lastItem - m_items.begin() );
311
312 for( CN_ITEM* item : aGarbage )
313 m_index.Remove( item );
314
315 m_hasInvalid = false;
316}
317
318
320{
321 assert( m_item->Valid() );
322 return m_item->Parent();
323}
324
325
327{
328 if( !m_item )
329 return false;
330
331 return m_item->Valid();
332}
333
334
336{
337 return !Valid() || m_item->Dirty();
338}
339
340
342{
343 int accuracy = 0;
344
345 if( !m_cluster )
346 return true;
347
348 // the minimal number of items connected to item_ref
349 // at this anchor point to decide the anchor is *not* dangling
350 size_t minimal_count = 1;
351 size_t connected_count = m_item->ConnectedItems().size();
352
353 // a via can be removed if connected to only one other item.
354 if( Parent()->Type() == PCB_VIA_T )
355 return connected_count < 2;
356
357 if( m_item->AnchorCount() == 1 )
358 return connected_count < minimal_count;
359
360 if( Parent()->Type() == PCB_TRACE_T || Parent()->Type() == PCB_ARC_T )
361 accuracy = KiROUND( static_cast<const PCB_TRACK*>( Parent() )->GetWidth() / 2.0 );
362 else if( Parent()->Type() == PCB_SHAPE_T )
363 accuracy = KiROUND( static_cast<const PCB_SHAPE*>( Parent() )->GetWidth() / 2.0 );
364
365 // Items with multiple anchors have usually items connected to each anchor.
366 // We want only the item count of this anchor point
367 connected_count = 0;
368
369 for( CN_ITEM* item : m_item->ConnectedItems() )
370 {
371 if( item->Parent()->Type() == PCB_ZONE_T )
372 {
373 ZONE* zone = static_cast<ZONE*>( item->Parent() );
374
375 if( zone->HitTestFilledArea( item->GetBoardLayer(), Pos(), accuracy ) )
376 connected_count++;
377 }
378 else if( item->Parent()->HitTest( Pos(), accuracy ) )
379 {
380 connected_count++;
381 }
382 }
383
384 return connected_count < minimal_count;
385}
386
387
389{
390 if( !m_cluster )
391 return 0;
392
393 int connected_count = 0;
394
395 for( CN_ITEM* item : m_item->ConnectedItems() )
396 {
397 if( item->Parent()->Type() == PCB_ZONE_T )
398 {
399 ZONE* zone = static_cast<ZONE*>( item->Parent() );
400
401 if( zone->HitTestFilledArea( item->GetBoardLayer(), Pos() ) )
402 connected_count++;
403 }
404 else if( item->Parent()->HitTest( Pos() ) )
405 {
406 connected_count++;
407 }
408 }
409
410 return connected_count;
411}
412
413
415{
416 m_items.reserve( 64 );
417 m_originPad = nullptr;
418 m_originNet = -1;
419 m_conflicting = false;
420}
421
422
426
427
429{
430 if( !m_originPad || !m_originPad->Valid() )
431 return "<none>";
432 else
433 return m_originPad->Parent()->GetNetname();
434}
435
436
437bool CN_CLUSTER::Contains( const CN_ITEM* aItem )
438{
439 return alg::contains( m_items, aItem );
440}
441
442
444{
445 return std::find_if( m_items.begin(), m_items.end(),
446 [&aItem]( const CN_ITEM* item )
447 {
448 return item->Valid() && item->Parent() == aItem;
449 } ) != m_items.end();
450}
451
452
454{
455 for( CN_ITEM* item : m_items )
456 {
457 wxLogTrace( wxT( "CN" ), wxT( " - item : %p bitem : %p type : %d inet %s\n" ),
458 item,
459 item->Parent(),
460 item->Parent()->Type(),
461 (const char*) item->Parent()->GetNetname().c_str() );
462 wxLogTrace( wxT( "CN" ), wxT( "- item : %p bitem : %p type : %d inet %s\n" ),
463 item,
464 item->Parent(),
465 item->Parent()->Type(),
466 (const char*) item->Parent()->GetNetname().c_str() );
467 item->Dump();
468 }
469}
470
471
473{
474 m_items.push_back( item );
475
476 int netCode = item->Net();
477
478 if( netCode <= 0 )
479 return;
480
481 if( m_originNet <= 0 )
482 {
483 m_originNet = netCode;
485 }
486
487 if( item->Parent()->Type() == PCB_PAD_T && !static_cast<PAD*>( item->Parent() )->IsFreePad() )
488 {
489 int rank;
490 auto it = m_netRanks.find( netCode );
491
492 if( it == m_netRanks.end() )
493 {
494 m_netRanks[netCode] = 1;
495 rank = 1;
496 }
497 else
498 {
499 it->second++;
500 rank = it->second;
501 }
502
503 if( !m_originPad || rank > m_netRanks[m_originNet] )
504 {
505 m_originPad = item;
506 m_originNet = netCode;
507 }
508
509 if( m_originPad && item->Net() != m_originNet )
510 m_conflicting = true;
511 }
512}
constexpr BOX2I KiROUND(const BOX2D &aBoxD)
Definition box2.h:986
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.
CN_ITEM * m_item
Pad or track/arc/via owning the anchor.
bool Valid() const
int ConnectedItemsCount() const
std::shared_ptr< CN_CLUSTER > m_cluster
Cluster to which the anchor belongs.
const VECTOR2I & Pos() const
bool Dirty() const
BOARD_CONNECTED_ITEM * Parent() const
bool IsDangling() const
The anchor point is dangling if the parent is a track and this anchor point is not connected to anoth...
std::vector< CN_ITEM * > m_items
bool Contains(const CN_ITEM *aItem)
void Add(CN_ITEM *item)
CN_ITEM * m_originPad
wxString OriginNetName() const
std::unordered_map< int, int > m_netRanks
CN_ITEM represents a BOARD_CONNETED_ITEM in the connectivity system (ie: a pad, track/arc/via,...
virtual int AnchorCount() const
void RemoveInvalidRefs()
std::vector< CN_ITEM * > m_connected
list of physically touching items
const std::vector< CN_ITEM * > & ConnectedItems() const
int Net() const
BOARD_CONNECTED_ITEM * m_parent
bool Valid() const
std::shared_ptr< CN_ANCHOR > AddAnchor(const VECTOR2I &aPos)
void SetLayers(int aStartLayer, int aEndLayer)
Set the layers spanned by the item to aStartLayer and aEndLayer.
virtual const VECTOR2I GetAnchor(int n) const
void SetDirty(bool aDirty)
CN_ITEM(BOARD_CONNECTED_ITEM *aParent, bool aCanChangeNet, int aAnchorCount=2)
void SetLayer(int aLayer)
Set the layers spanned by the item to a single layer aLayer.
std::vector< std::shared_ptr< CN_ANCHOR > > m_anchors
bool m_valid
used to identify garbage items (we use lazy removal)
BOARD_CONNECTED_ITEM * Parent() const
CN_ITEM * Add(PAD *pad)
void SetDirty(bool aDirty=true)
std::vector< CN_ITEM * > m_items
void addItemtoTree(CN_ITEM *item)
void RemoveInvalidItems(std::vector< CN_ITEM * > &aGarbage)
virtual int AnchorCount() const override
const SHAPE_LINE_CHAIN & GetOutline() const
virtual const VECTOR2I GetAnchor(int n) const override
bool HasValidOutline() const
KICAD_T Type() const
Returns the type of object.
Definition eda_item.h:108
LSEQ is a sequence (and therefore also a set) of PCB_LAYER_IDs.
Definition lseq.h:47
Definition pad.h:61
bool IsFreePad() const
Definition pad.cpp:573
std::vector< VECTOR2I > GetConnectionPoints() const
PCB_LAYER_ID GetLayer() const override
Return the primary layer this item is on.
Definition pcb_shape.h:68
const VECTOR2I & GetStart() const
Definition pcb_track.h:93
const VECTOR2I & GetEnd() const
Definition pcb_track.h:90
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.
const std::vector< VECTOR2I > & CPoints() const
const SHAPE_LINE_CHAIN & COutline(int aIndex) const
Handle a list of polygons defining a copper zone.
Definition zone.h:70
std::shared_ptr< SHAPE_POLY_SET > GetFilledPolysList(PCB_LAYER_ID aLayer) const
Definition zone.h:697
bool HitTestFilledArea(PCB_LAYER_ID aLayer, const VECTOR2I &aRefPos, int aAccuracy=0) const
Test if the given VECTOR2I is within the bounds of a filled area of this zone.
Definition zone.cpp:979
PCB_LAYER_ID
A quick note on layer IDs:
Definition layer_ids.h:56
@ B_Cu
Definition layer_ids.h:61
@ F_Cu
Definition layer_ids.h:60
This file contains miscellaneous commonly used macros and functions.
#define UNIMPLEMENTED_FOR(type)
Definition macros.h:92
bool contains(const _Container &__container, _Value __value)
Returns true if the container contains the given value.
Definition kicad_algo.h:96
@ NPTH
like PAD_PTH, but not plated mechanical use only, no connection allowed
Definition padstack.h:103
@ SMD
Smd pad, appears on the solder paste layer (default)
Definition padstack.h:99
@ CONN
Like smd, does not appear on the solder paste layer (default) Note: also has a special attribute in G...
Definition padstack.h:100
const int accuracy
@ 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_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_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