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 std::erase_if( m_connected, []( const CN_ITEM* item ) { return !item->Valid(); } );
136}
137
138
140{
141 if( !pad->IsOnCopperLayer() )
142 return nullptr;
143
144 auto item = new CN_ITEM( pad, false, 1 );
145
146 std::set<VECTOR2I> uniqueAnchors;
147 pad->Padstack().ForEachUniqueLayer(
148 [&]( PCB_LAYER_ID aLayer )
149 {
150 uniqueAnchors.insert( pad->ShapePos( aLayer ) );
151 } );
152
153 for( const VECTOR2I& anchor : uniqueAnchors )
154 item->AddAnchor( anchor );
155
156 item->SetLayers( F_Cu, B_Cu );
157
158 switch( pad->GetAttribute() )
159 {
160 case PAD_ATTRIB::SMD:
161 case PAD_ATTRIB::NPTH:
162 case PAD_ATTRIB::CONN:
164 {
165 LSEQ lmsk = pad->GetLayerSet().CuStack();
166
167 if( !lmsk.empty() )
168 item->SetLayer( lmsk.front() );
169
170 break;
171 }
172
173 default:
174 break;
175 }
176
177 addItemtoTree( item );
178 appendItem( item );
179
180 // Re-mark dirty after tree insertion since BBox() clears the dirty flag
181 item->SetDirty( true );
182 SetDirty();
183 return item;
184}
185
186
188{
189 CN_ITEM* item = new CN_ITEM( track, true );
190 appendItem( item );
191 item->AddAnchor( track->GetStart() );
192 item->AddAnchor( track->GetEnd() );
193 item->SetLayer( track->GetLayer() );
194 addItemtoTree( item );
195
196 // Re-mark dirty after tree insertion since BBox() clears the dirty flag
197 item->SetDirty( true );
198 SetDirty();
199 return item;
200}
201
202
204{
205 CN_ITEM* item = new CN_ITEM( aArc, true );
206 appendItem( item );
207 item->AddAnchor( aArc->GetStart() );
208 item->AddAnchor( aArc->GetEnd() );
209 item->SetLayer( aArc->GetLayer() );
210 addItemtoTree( item );
211
212 // Re-mark dirty after tree insertion since BBox() clears the dirty flag
213 item->SetDirty( true );
214 SetDirty();
215 return item;
216}
217
218
220{
221 CN_ITEM* item = new CN_ITEM( via, !via->GetIsFree(), 1 );
222
223 appendItem( item );
224 item->AddAnchor( via->GetStart() );
225
226 item->SetLayers( via->TopLayer(), via->BottomLayer() );
227 addItemtoTree( item );
228
229 // Re-mark dirty after tree insertion since BBox() clears the dirty flag
230 item->SetDirty( true );
231 SetDirty();
232 return item;
233}
234
235
236const std::vector<CN_ITEM*> CN_LIST::Add( ZONE* zone, PCB_LAYER_ID aLayer )
237{
238 const std::shared_ptr<SHAPE_POLY_SET>& polys = zone->GetFilledPolysList( aLayer );
239
240 std::vector<CN_ITEM*> rv;
241
242 for( int j = 0; j < polys->OutlineCount(); j++ )
243 {
244 CN_ZONE_LAYER* zitem = new CN_ZONE_LAYER( zone, aLayer, j );
245
246 zitem->BuildIndexes();
247
248 for( const VECTOR2I& pt : zone->GetFilledPolysList( aLayer )->COutline( j ).CPoints() )
249 zitem->AddAnchor( pt );
250
251 rv.push_back( Add( zitem ) );
252 }
253
254 return rv;
255}
256
257
259{
260 appendItem( zitem );
261 addItemtoTree( zitem );
262
263 // Re-mark dirty after tree insertion since BBox() clears the dirty flag
264 zitem->SetDirty( true );
265 SetDirty();
266 return zitem;
267}
268
269
271{
272 CN_ITEM* item = new CN_ITEM( shape, true );
273 appendItem( item );
274
275 for( const VECTOR2I& point : shape->GetConnectionPoints() )
276 item->AddAnchor( point );
277
278 item->SetLayer( shape->GetLayer() );
279 addItemtoTree( item );
280
281 // Re-mark dirty after tree insertion since BBox() clears the dirty flag
282 item->SetDirty( true );
283 SetDirty();
284 return item;
285}
286
287
288void CN_LIST::RemoveInvalidItems( std::vector<CN_ITEM*>& aGarbage )
289{
290 if( !m_hasInvalid )
291 return;
292
293 auto lastItem = std::remove_if( m_items.begin(), m_items.end(),
294 [&aGarbage]( CN_ITEM* item )
295 {
296 if( !item->Valid() )
297 {
298 aGarbage.push_back ( item );
299 return true;
300 }
301
302 return false;
303 } );
304
305 m_items.resize( lastItem - m_items.begin() );
306
307 // Compaction shifted everything past the first hole, so restamp before anything reads
308 for( size_t ii = 0; ii < m_items.size(); ++ii )
309 m_items[ii]->SetListIndex( static_cast<int>( ii ) );
310
311 for( CN_ITEM* item : aGarbage )
312 m_index.Remove( item );
313
314 m_hasInvalid = false;
315}
316
317
319{
320 assert( m_item->Valid() );
321 return m_item->Parent();
322}
323
324
326{
327 if( !m_item )
328 return false;
329
330 return m_item->Valid();
331}
332
333
335{
336 return !Valid() || m_item->Dirty();
337}
338
339
341{
342 int accuracy = 0;
343
344 if( !m_cluster )
345 return true;
346
347 // the minimal number of items connected to item_ref
348 // at this anchor point to decide the anchor is *not* dangling
349 size_t minimal_count = 1;
350 size_t connected_count = m_item->ConnectedItems().size();
351
352 // a via can be removed if connected to only one other item.
353 if( Parent()->Type() == PCB_VIA_T )
354 return connected_count < 2;
355
356 if( m_item->AnchorCount() == 1 )
357 return connected_count < minimal_count;
358
359 if( Parent()->Type() == PCB_TRACE_T || Parent()->Type() == PCB_ARC_T )
360 accuracy = KiROUND( static_cast<const PCB_TRACK*>( Parent() )->GetWidth() / 2.0 );
361 else if( Parent()->Type() == PCB_SHAPE_T )
362 accuracy = KiROUND( static_cast<const PCB_SHAPE*>( Parent() )->GetWidth() / 2.0 );
363
364 // Items with multiple anchors have usually items connected to each anchor.
365 // We want only the item count of this anchor point
366 connected_count = 0;
367
368 for( CN_ITEM* item : m_item->ConnectedItems() )
369 {
370 if( item->Parent()->Type() == PCB_ZONE_T )
371 {
372 ZONE* zone = static_cast<ZONE*>( item->Parent() );
373
374 if( zone->HitTestFilledArea( item->GetBoardLayer(), Pos(), accuracy ) )
375 connected_count++;
376 }
377 else if( item->Parent()->HitTest( Pos(), accuracy ) )
378 {
379 connected_count++;
380 }
381 }
382
383 return connected_count < minimal_count;
384}
385
386
388{
389 if( !m_cluster )
390 return 0;
391
392 int connected_count = 0;
393
394 for( CN_ITEM* item : m_item->ConnectedItems() )
395 {
396 if( item->Parent()->Type() == PCB_ZONE_T )
397 {
398 ZONE* zone = static_cast<ZONE*>( item->Parent() );
399
400 if( zone->HitTestFilledArea( item->GetBoardLayer(), Pos() ) )
401 connected_count++;
402 }
403 else if( item->Parent()->HitTest( Pos() ) )
404 {
405 connected_count++;
406 }
407 }
408
409 return connected_count;
410}
411
412
414{
415 m_items.reserve( 64 );
416 m_originPad = nullptr;
417 m_originNet = -1;
418 m_conflicting = false;
419}
420
421
425
426
428{
429 if( !m_originPad || !m_originPad->Valid() )
430 return "<none>";
431 else
432 return m_originPad->Parent()->GetNetname();
433}
434
435
436bool CN_CLUSTER::Contains( const CN_ITEM* aItem )
437{
438 return alg::contains( m_items, aItem );
439}
440
441
443{
444 return std::find_if( m_items.begin(), m_items.end(),
445 [&aItem]( const CN_ITEM* item )
446 {
447 return item->Valid() && item->Parent() == aItem;
448 } ) != m_items.end();
449}
450
451
453{
454 for( CN_ITEM* item : m_items )
455 {
456 wxLogTrace( wxT( "CN" ), wxT( " - item : %p bitem : %p type : %d inet %s\n" ),
457 item,
458 item->Parent(),
459 item->Parent()->Type(),
460 (const char*) item->Parent()->GetNetname().c_str() );
461 wxLogTrace( wxT( "CN" ), wxT( "- item : %p bitem : %p type : %d inet %s\n" ),
462 item,
463 item->Parent(),
464 item->Parent()->Type(),
465 (const char*) item->Parent()->GetNetname().c_str() );
466 item->Dump();
467 }
468}
469
470
472{
473 m_items.push_back( item );
474
475 int netCode = item->Net();
476
477 if( netCode <= 0 )
478 return;
479
480 if( m_originNet <= 0 )
481 {
482 m_originNet = netCode;
484 }
485
486 if( item->Parent()->Type() == PCB_PAD_T && !static_cast<PAD*>( item->Parent() )->IsFreePad() )
487 {
488 int rank;
489 auto it = m_netRanks.find( netCode );
490
491 if( it == m_netRanks.end() )
492 {
493 m_netRanks[netCode] = 1;
494 rank = 1;
495 }
496 else
497 {
498 it->second++;
499 rank = it->second;
500 }
501
502 if( !m_originPad || rank > m_netRanks[m_originNet] )
503 {
504 m_originPad = item;
505 m_originNet = netCode;
506 }
507
508 if( m_originPad && item->Net() != m_originNet )
509 m_conflicting = true;
510 }
511}
constexpr BOX2I KiROUND(const BOX2D &aBoxD)
Definition box2.h:985
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_CONNECTED_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
void appendItem(CN_ITEM *aItem)
The sole place CN_ITEM list numbering is assigned.
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)
Represents a single outline of a zone fill on a particular layer.
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:110
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:614
std::vector< VECTOR2I > GetConnectionPoints() const
PCB_LAYER_ID GetLayer() const override
Return the primary layer this item is on.
Definition pcb_shape.h:70
const VECTOR2I & GetStart() const
Definition pcb_track.h:100
const VECTOR2I & GetEnd() const
Definition pcb_track.h:97
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:701
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:1105
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:104
@ NON_COMPONENT
Pad with no hole that does not connect to a physical component, but rather provides a connection poin...
Definition padstack.h:106
@ SMD
Smd pad, appears on the solder paste layer (default)
Definition padstack.h:100
@ CONN
Like smd, does not appear on the solder paste layer (default) Note: also has a special attribute in G...
Definition padstack.h:101
const int accuracy
@ PCB_SHAPE_T
class PCB_SHAPE, a segment not on copper layers
Definition typeinfo.h:80
@ PCB_VIA_T
class PCB_VIA, a via (like a track segment on a copper layer)
Definition typeinfo.h:89
@ PCB_ZONE_T
class ZONE, a copper pour area
Definition typeinfo.h:100
@ PCB_PAD_T
class PAD, a pad in a footprint
Definition typeinfo.h:79
@ PCB_ARC_T
class PCB_ARC, an arc track segment on a copper layer
Definition typeinfo.h:90
@ PCB_TRACE_T
class PCB_TRACK, a track segment (segment on a copper layer)
Definition typeinfo.h:88
VECTOR2< int32_t > VECTOR2I
Definition vector2d.h:708