KiCad PCB EDA Suite
connectivity_items.cpp
Go to the documentation of this file.
1 /*
2  * This program source code file is part of KICAD, a free EDA CAD application.
3  *
4  * Copyright (C) 2016-2018 CERN
5  * Copyright (C) 2019 KiCad Developers, see AUTHORS.txt for contributors.
6  *
7  * @author Tomasz Wlostowski <tomasz.wlostowski@cern.ch>
8  *
9  * This program is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU General Public License
11  * as published by the Free Software Foundation; either version 2
12  * of the License, or (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, you may find one here:
21  * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
22  * or you may search the http://www.gnu.org website for the version 2 license,
23  * or you may write to the Free Software Foundation, Inc.,
24  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
25  */
26 
28 
30 {
31  if( !m_valid )
32  return 0;
33 
34  switch( m_parent->Type() )
35  {
36  case PCB_PAD_T:
37  return 5; // center, north, south, east and west
38  case PCB_TRACE_T:
39  return 2; // stard and end
40  default:
41  return 1;
42  }
43 }
44 
45 
46 const VECTOR2I CN_ITEM::GetAnchor( int n ) const
47 {
48  VECTOR2I pt0;
49 
50  if( !m_valid )
51  return pt0;
52 
53  switch( m_parent->Type() )
54  {
55  case PCB_PAD_T:
56  {
57  D_PAD* pad = (D_PAD*) m_parent;
58 
59  if( n == 0 )
60  return VECTOR2I( pad->GetPosition() );
61 
62  // ShapePos() is the geometric center (not anchor) for the pad
63  pt0 = pad->ShapePos();
64  VECTOR2I pt1 = pt0;
65 
66  switch( pad->GetShape() )
67  {
68  case PAD_SHAPE_RECT:
69  case PAD_SHAPE_CIRCLE:
70  case PAD_SHAPE_OVAL:
73  switch( n )
74  {
75  case 1: pt1.y -= pad->GetSize().y / 2; break; // North
76  case 2: pt1.y += pad->GetSize().y / 2; break; // South
77  case 3: pt1.x -= pad->GetSize().x / 2; break; // East
78  case 4: pt1.x += pad->GetSize().x / 2; break; // West
79  default: break; // Wicked witch
80  }
81 
82  if( pad->GetOrientation() )
83  RotatePoint( pt1, pad->ShapePos(), pad->GetOrientation() );
84 
85  // Thermal spokes on circular pads form an 'X' instead of a '+'
86  if( pad->GetShape() == PAD_SHAPE_CIRCLE )
87  RotatePoint( pt1, pad->ShapePos(), 450 );
88 
89  return pt1;
90 
92  case PAD_SHAPE_CUSTOM:
93  {
94  switch( n )
95  {
96  case 1: pt1.y = INT_MIN / 2; break; // North
97  case 2: pt1.y = INT_MAX / 2; break; // South
98  case 3: pt1.x = INT_MIN / 2; break; // East
99  case 4: pt1.x = INT_MAX / 2; break; // West
100  default: break; // Wicked witch
101  }
102 
103  if( pad->GetOrientation() )
104  RotatePoint( pt1, pad->ShapePos(), pad->GetOrientation() );
105 
106  SHAPE_POLY_SET padPolySet;
107  pad->BuildPadShapePolygon( padPolySet, wxSize( 0, 0 ), ARC_LOW_DEF );
108  const SHAPE_LINE_CHAIN& padOutline = padPolySet.COutline( 0 );
109  SHAPE_LINE_CHAIN::INTERSECTIONS intersections;
110 
111  padOutline.Intersect( SEG( pt0, pt1 ), intersections );
112 
113  if( intersections.empty() )
114  {
115  // There should always be at least some copper outside the hole and/or
116  // shapePos center
117  assert( false );
118  return pt0;
119  }
120 
121  return intersections[ intersections.size() - 1 ].p;
122  }
123  }
124 
125  break;
126  }
127  case PCB_TRACE_T:
128  if( n == 0 )
129  return static_cast<const TRACK*>( m_parent )->GetStart();
130  else
131  return static_cast<const TRACK*>( m_parent )->GetEnd();
132 
133  case PCB_VIA_T:
134  return static_cast<const VIA*>( m_parent )->GetStart();
135 
136  default:
137  assert( false );
138  break;
139  }
140 
141  return pt0;
142 }
143 
144 
145 int CN_ITEM::Net() const
146 {
147  if( !m_parent || !m_valid )
148  return -1;
149 
150  return m_parent->GetNetCode();
151 }
152 
153 
155 {
156  printf(" valid: %d, connected: \n", !!Valid());
157 
158  for( auto i : m_connected )
159  {
160  TRACK* t = static_cast<TRACK*>( i->Parent() );
161  printf( " - %p %d\n", t, t->Type() );
162  }
163 }
164 
165 
167 {
168  if( !Valid() )
169  return 0;
170 
171  const auto zone = static_cast<const ZONE_CONTAINER*>( Parent() );
172  const auto& outline = zone->GetFilledPolysList().COutline( m_subpolyIndex );
173 
174  return outline.PointCount() ? 1 : 0;
175 }
176 
177 
178 const VECTOR2I CN_ZONE::GetAnchor( int n ) const
179 {
180  if( !Valid() )
181  return VECTOR2I();
182 
183  const auto zone = static_cast<const ZONE_CONTAINER*> ( Parent() );
184  const auto& outline = zone->GetFilledPolysList().COutline( m_subpolyIndex );
185 
186  return outline.CPoint( 0 );
187 }
188 
189 
191 {
192  for( auto it = m_connected.begin(); it != m_connected.end(); )
193  {
194  if( !(*it)->Valid() )
195  it = m_connected.erase( it );
196  else
197  ++it;
198  }
199 }
200 
201 
203  {
204  if( !pad->IsOnCopperLayer() )
205  return nullptr;
206 
207  auto item = new CN_ITEM( pad, false, 1 );
208  item->AddAnchor( pad->ShapePos() );
209  item->SetLayers( LAYER_RANGE( F_Cu, B_Cu ) );
210 
211  switch( pad->GetAttribute() )
212  {
213  case PAD_ATTRIB_SMD:
215  case PAD_ATTRIB_CONN:
216  {
217  LSET lmsk = pad->GetLayerSet();
218 
219  for( int i = 0; i <= MAX_CU_LAYERS; i++ )
220  {
221  if( lmsk[i] )
222  {
223  item->SetLayer( i );
224  break;
225  }
226  }
227  break;
228  }
229  default:
230  break;
231  }
232 
233  addItemtoTree( item );
234  m_items.push_back( item );
235  SetDirty();
236  return item;
237 }
238 
240  {
241  auto item = new CN_ITEM( track, true );
242  m_items.push_back( item );
243  item->AddAnchor( track->GetStart() );
244  item->AddAnchor( track->GetEnd() );
245  item->SetLayer( track->GetLayer() );
246  addItemtoTree( item );
247  SetDirty();
248  return item;
249  }
250 
252  {
253  auto item = new CN_ITEM( via, true, 1 );
254 
255  m_items.push_back( item );
256  item->AddAnchor( via->GetStart() );
257  item->SetLayers( LAYER_RANGE( F_Cu, B_Cu ) );
258  addItemtoTree( item );
259  SetDirty();
260  return item;
261  }
262 
263  const std::vector<CN_ITEM*> CN_LIST::Add( ZONE_CONTAINER* zone )
264  {
265  const auto& polys = zone->GetFilledPolysList();
266 
267  std::vector<CN_ITEM*> rv;
268 
269  for( int j = 0; j < polys.OutlineCount(); j++ )
270  {
271  CN_ZONE* zitem = new CN_ZONE( zone, false, j );
272  const auto& outline = zone->GetFilledPolysList().COutline( j );
273 
274  for( int k = 0; k < outline.PointCount(); k++ )
275  zitem->AddAnchor( outline.CPoint( k ) );
276 
277  m_items.push_back( zitem );
278  zitem->SetLayer( zone->GetLayer() );
279  addItemtoTree( zitem );
280  rv.push_back( zitem );
281  SetDirty();
282  }
283 
284  return rv;
285  }
286 
287 
288 void 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(), [&aGarbage] ( CN_ITEM* item )
294  {
295  if( !item->Valid() )
296  {
297  aGarbage.push_back ( item );
298  return true;
299  }
300 
301  return false;
302  } );
303 
304  m_items.resize( lastItem - m_items.begin() );
305 
306  for( auto item : m_items )
307  item->RemoveInvalidRefs();
308 
309  for( auto item : aGarbage )
310  m_index.Remove( item );
311 
312  m_hasInvalid = false;
313 }
314 
315 
317 {
318  assert( m_item->Valid() );
319  return m_item->Parent();
320 }
321 
322 
323 bool CN_ANCHOR::Valid() const
324 {
325  if( !m_item )
326  return false;
327 
328  return m_item->Valid();
329 }
330 
331 
333 {
334  if( !m_cluster )
335  return true;
336 
337  // the minimal number of items connected to item_ref
338  // at this anchor point to decide the anchor is *not* dangling
339  size_t minimal_count = 1;
340  size_t connected_count = m_item->ConnectedItems().size();
341 
342  // a via can be removed if connected to only one other item.
343  if( Parent()->Type() == PCB_VIA_T )
344  return connected_count < 2;
345 
346  if( m_item->AnchorCount() == 1 )
347  return connected_count < minimal_count;
348 
349  // Items with multiple anchors have usually items connected to each anchor.
350  // We want only the item count of this anchor point
351  connected_count = 0;
352  for( auto item : m_item->ConnectedItems() )
353  {
354  if( item->Parent()->Type() == PCB_ZONE_AREA_T )
355  {
356  ZONE_CONTAINER* zone = static_cast<ZONE_CONTAINER*>( item->Parent() );
357 
358  if( zone->HitTestFilledArea( (wxPoint) Pos() ) )
359  connected_count++;
360  }
361  else if( item->Parent()->HitTest( (wxPoint) Pos() ) )
362  connected_count++;
363  }
364 
365  return connected_count < minimal_count;
366 }
367 
368 
370 {
371  if( !m_cluster )
372  return 0;
373 
374  int connected_count = 0;
375 
376  for( auto item : m_item->ConnectedItems() )
377  {
378  if( item->Parent()->Type() == PCB_ZONE_AREA_T )
379  {
380  ZONE_CONTAINER* zone = static_cast<ZONE_CONTAINER*>( item->Parent() );
381 
382  if( zone->HitTestFilledArea( wxPoint( Pos().x, Pos().y ) ) )
383  connected_count++;
384  }
385  else if( item->Parent()->HitTest( wxPoint( Pos().x, Pos().y ) ) )
386  connected_count++;
387  }
388 
389  return connected_count;
390 }
391 
392 
394 {
395  m_items.reserve( 64 );
396  m_originPad = nullptr;
397  m_originNet = -1;
398  m_conflicting = false;
399 }
400 
401 
403 {
404 }
405 
406 
408 {
409  if( !m_originPad || !m_originPad->Valid() )
410  return "<none>";
411  else
412  return m_originPad->Parent()->GetNetname();
413 }
414 
415 
416 bool CN_CLUSTER::Contains( const CN_ITEM* aItem )
417 {
418  return std::find( m_items.begin(), m_items.end(), aItem ) != m_items.end();
419 }
420 
421 
423 {
424  return std::find_if( m_items.begin(), m_items.end(), [ &aItem ] ( const CN_ITEM* item )
425  { return item->Valid() && item->Parent() == aItem; } ) != m_items.end();
426 }
427 
428 
430 {
431  for( auto item : m_items )
432  {
433  wxLogTrace( "CN", " - item : %p bitem : %p type : %d inet %s\n", item, item->Parent(),
434  item->Parent()->Type(), (const char*) item->Parent()->GetNetname().c_str() );
435  printf( "- item : %p bitem : %p type : %d inet %s\n", item, item->Parent(),
436  item->Parent()->Type(), (const char*) item->Parent()->GetNetname().c_str() );
437  item->Dump();
438  }
439 }
440 
441 
443 {
444  m_items.push_back( item );
445 
446  if( item->Net() <= 0 )
447  return;
448 
449  if( m_originNet <= 0 )
450  {
451  m_originNet = item->Net();
452  }
453 
454  if( item->Parent()->Type() == PCB_PAD_T )
455  {
456  if( !m_originPad )
457  {
458  m_originPad = item;
459  m_originNet = item->Net();
460  }
461 
462  if( m_originPad && item->Net() != m_originNet )
463  {
464  m_conflicting = true;
465  }
466  }
467 }
void RemoveInvalidItems(std::vector< CN_ITEM * > &aGarbage)
const CONNECTED_ITEMS & ConnectedItems() const
bool Contains(const CN_ITEM *aItem)
virtual int AnchorCount() const override
std::vector< CN_ITEM * > m_items
ZONE_CONTAINER handles a list of polygons defining a copper zone.
Definition: class_zone.h:60
int Intersect(const SEG &aSeg, INTERSECTIONS &aIp) const
Function Intersect()
int GetNetCode() const
Function GetNetCode.
std::vector< INTERSECTION > INTERSECTIONS
BOARD_CONNECTED_ITEM * m_parent
like PAD_STANDARD, but not plated mechanical use only, no connection allowed
Definition: pad_shapes.h:66
std::shared_ptr< CN_CLUSTER > m_cluster
Cluster to which the anchor belongs.
const wxPoint & GetStart() const
Definition: class_track.h:109
void addItemtoTree(CN_ITEM *item)
bool m_valid
valid flag, used to identify garbage items (we use lazy removal)
CN_ITEM * Add(D_PAD *pad)
Smd pad, appears on the solder paste layer (default)
Definition: pad_shapes.h:62
class ZONE_CONTAINER, a zone area
Definition: typeinfo.h:101
virtual PCB_LAYER_ID GetLayer() const override
Function GetLayer returns the primary layer this item is on.
Definition: class_zone.cpp:204
void BuildPadShapePolygon(SHAPE_POLY_SET &aCornerBuffer, wxSize aInflateValue, int aError=ARC_HIGH_DEF) const
Function BuildPadShapePolygon Build the Corner list of the polygonal shape, depending on shape,...
bool HitTestFilledArea(const wxPoint &aRefPos) const
Function HitTestFilledArea tests if the given wxPoint is within the bounds of a filled area of this z...
Definition: class_zone.cpp:675
class D_PAD, a pad in a footprint
Definition: typeinfo.h:90
BOARD_CONNECTED_ITEM * Parent() const
void RotatePoint(int *pX, int *pY, double angle)
Definition: trigo.cpp:208
bool IsDangling() const
has meaning only for tracks and vias.
VECTOR2< int > VECTOR2I
Definition: vector2d.h:594
BOARD_CONNECTED_ITEM is a base class derived from BOARD_ITEM for items that can be connected and have...
void SetDirty(bool aDirty=true)
int ConnectedItemsCount() const
has meaning only for tracks and vias.
int Net() const
PAD_ATTR_T GetAttribute() const
Definition: class_pad.h:445
class TRACK, a track segment (segment on a copper layer)
Definition: typeinfo.h:96
virtual int AnchorCount() const
std::vector< CN_ITEM * > m_items
LSET is a set of PCB_LAYER_IDs.
SHAPE_POLY_SET.
bool IsOnCopperLayer() const override
Definition: class_pad.h:285
LSET GetLayerSet() const override
Function GetLayerSet returns a "layer mask", which is a bitmap of all layers on which the TRACK segme...
Definition: class_pad.h:442
void Add(CN_ITEM *item)
const wxString & GetNetname() const
Function GetNetname.
Like smd, does not appear on the solder paste layer (default) note also has a special attribute in Ge...
Definition: pad_shapes.h:63
bool Valid() const
virtual const VECTOR2I GetAnchor(int n) const
Definition: seg.h:39
wxString OriginNetName() const
void AddAnchor(const VECTOR2I &aPos)
virtual const VECTOR2I GetAnchor(int n) const override
CN_ITEM * m_item
Item owning the anchor.
SHAPE_LINE_CHAIN.
void SetLayer(int aLayer)
Function SetLayer()
CN_ITEM * m_originPad
double GetOrientation() const
Function GetOrientation returns the rotation angle of the pad in tenths of degrees,...
Definition: class_pad.h:422
const SHAPE_LINE_CHAIN & COutline(int aIndex) const
wxPoint ShapePos() const
Definition: class_pad.cpp:570
BOARD_CONNECTED_ITEM * Parent() const
const wxPoint & GetEnd() const
Definition: class_track.h:106
void Remove(T aItem)
Function Remove() Removes an item from the tree.
const VECTOR2I & Pos() const
CONNECTED_ITEMS m_connected
list of items physically connected (touching)
class VIA, a via (like a track segment on a copper layer)
Definition: typeinfo.h:97
PAD_SHAPE_T GetShape() const
Function GetShape.
Definition: class_pad.h:237
const wxSize & GetSize() const
Definition: class_pad.h:300
const wxPoint GetPosition() const override
Definition: class_pad.h:241
virtual PCB_LAYER_ID GetLayer() const
Function GetLayer returns the primary layer this item is on.
LAYER_RANGE.
Definition: pns_layerset.h:32
void RemoveInvalidRefs()
bool Valid() const
CN_RTREE< CN_ITEM * > m_index
KICAD_T Type() const
Function Type()
Definition: base_struct.h:207
const SHAPE_POLY_SET & GetFilledPolysList() const
Function GetFilledPolysList returns a reference to the list of filled polygons.
Definition: class_zone.h:553