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) 2018 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  return m_parent->Type() == PCB_TRACE_T ? 2 : 1;
35 }
36 
37 
38 const VECTOR2I CN_ITEM::GetAnchor( int n ) const
39 {
40  if( !m_valid )
41  return VECTOR2I();
42 
43  switch( m_parent->Type() )
44  {
45  case PCB_PAD_T:
46  return static_cast<const D_PAD*>( m_parent )->ShapePos();
47  break;
48 
49  case PCB_TRACE_T:
50  {
51  auto tr = static_cast<const TRACK*>( m_parent );
52  return ( n == 0 ? tr->GetStart() : tr->GetEnd() );
53 
54  break;
55  }
56 
57  case PCB_VIA_T:
58  return static_cast<const VIA*>( m_parent )->GetStart();
59 
60  default:
61  assert( false );
62  return VECTOR2I();
63  }
64 }
65 
66 
67 int CN_ITEM::Net() const
68 {
69  if( !m_parent || !m_valid )
70  return -1;
71 
72  return m_parent->GetNetCode();
73 }
74 
75 
77 {
78  printf(" valid: %d, connected: \n", !!Valid());
79 
80  for( auto i : m_connected )
81  {
82  TRACK* t = static_cast<TRACK*>( i->Parent() );
83  printf( " - %p %d\n", t, t->Type() );
84  }
85 }
86 
87 
89 {
90  if( !Valid() )
91  return 0;
92 
93  const auto zone = static_cast<const ZONE_CONTAINER*>( Parent() );
94  const auto& outline = zone->GetFilledPolysList().COutline( m_subpolyIndex );
95 
96  return outline.PointCount() ? 1 : 0;
97 }
98 
99 
100 const VECTOR2I CN_ZONE::GetAnchor( int n ) const
101 {
102  if( !Valid() )
103  return VECTOR2I();
104 
105  const auto zone = static_cast<const ZONE_CONTAINER*> ( Parent() );
106  const auto& outline = zone->GetFilledPolysList().COutline( m_subpolyIndex );
107 
108  return outline.CPoint( 0 );
109 }
110 
111 
113 {
114  for( auto it = m_connected.begin(); it != m_connected.end(); )
115  {
116  if( !(*it)->Valid() )
117  it = m_connected.erase( it );
118  else
119  ++it;
120  }
121 }
122 
123 
125  {
126  auto item = new CN_ITEM( pad, false, 1 );
127  item->AddAnchor( pad->ShapePos() );
128  item->SetLayers( LAYER_RANGE( F_Cu, B_Cu ) );
129 
130  switch( pad->GetAttribute() )
131  {
132  case PAD_ATTRIB_SMD:
134  case PAD_ATTRIB_CONN:
135  {
136  LSET lmsk = pad->GetLayerSet();
137 
138  for( int i = 0; i <= MAX_CU_LAYERS; i++ )
139  {
140  if( lmsk[i] )
141  {
142  item->SetLayer( i );
143  break;
144  }
145  }
146  break;
147  }
148  default:
149  break;
150  }
151 
152  addItemtoTree( item );
153  m_items.push_back( item );
154  SetDirty();
155  return item;
156  }
157 
159  {
160  auto item = new CN_ITEM( track, true );
161  m_items.push_back( item );
162  item->AddAnchor( track->GetStart() );
163  item->AddAnchor( track->GetEnd() );
164  item->SetLayer( track->GetLayer() );
165  addItemtoTree( item );
166  SetDirty();
167  return item;
168  }
169 
171  {
172  auto item = new CN_ITEM( via, true, 1 );
173 
174  m_items.push_back( item );
175  item->AddAnchor( via->GetStart() );
176  item->SetLayers( LAYER_RANGE( F_Cu, B_Cu ) );
177  addItemtoTree( item );
178  SetDirty();
179  return item;
180  }
181 
182  const std::vector<CN_ITEM*> CN_LIST::Add( ZONE_CONTAINER* zone )
183  {
184  const auto& polys = zone->GetFilledPolysList();
185 
186  std::vector<CN_ITEM*> rv;
187 
188  for( int j = 0; j < polys.OutlineCount(); j++ )
189  {
190  CN_ZONE* zitem = new CN_ZONE( zone, false, j );
191  const auto& outline = zone->GetFilledPolysList().COutline( j );
192 
193  for( int k = 0; k < outline.PointCount(); k++ )
194  zitem->AddAnchor( outline.CPoint( k ) );
195 
196  m_items.push_back( zitem );
197  zitem->SetLayer( zone->GetLayer() );
198  addItemtoTree( zitem );
199  rv.push_back( zitem );
200  SetDirty();
201  }
202 
203  return rv;
204  }
205 
206 
207 void CN_LIST::RemoveInvalidItems( std::vector<CN_ITEM*>& aGarbage )
208 {
209  if( !m_hasInvalid )
210  return;
211 
212  auto lastItem = std::remove_if(m_items.begin(), m_items.end(), [&aGarbage] ( CN_ITEM* item )
213  {
214  if( !item->Valid() )
215  {
216  aGarbage.push_back ( item );
217  return true;
218  }
219 
220  return false;
221  } );
222 
223  m_items.resize( lastItem - m_items.begin() );
224 
225  for( auto item : m_items )
226  item->RemoveInvalidRefs();
227 
228  for( auto item : aGarbage )
229  m_index.Remove( item );
230 
231  m_hasInvalid = false;
232 }
233 
234 
236 {
237  assert( m_item->Valid() );
238  return m_item->Parent();
239 }
240 
241 
242 bool CN_ANCHOR::Valid() const
243 {
244  if( !m_item )
245  return false;
246 
247  return m_item->Valid();
248 }
249 
250 
252 {
253  if( !m_cluster )
254  return true;
255 
256  // Calculate the item count connected to this anchor.
257  // m_cluster groups all items connected, but they are not necessary connected
258  // at this coordinate point (they are only candidates)
259  BOARD_CONNECTED_ITEM* item_ref = Parent();
260  LSET layers = item_ref->GetLayerSet() & LSET::AllCuMask();
261 
262  // the number of items connected to item_ref at ths anchor point
263  int connected_items_count = 0;
264 
265  // the minimal number of items connected to item_ref
266  // at this anchor point to decide the anchor is *not* dangling
267  int minimal_count = 1;
268 
269  // a via can be removed if connected to only one other item.
270  // the minimal_count is therefore 2
271  if( item_ref->Type() == PCB_VIA_T )
272  minimal_count = 2;
273 
274  for( CN_ITEM* item : *m_cluster )
275  {
276  if( !item->Valid() )
277  continue;
278 
279  BOARD_CONNECTED_ITEM* brd_item = item->Parent();
280 
281  if( brd_item == item_ref )
282  continue;
283 
284  // count only items on the same layer at this coordinate (especially for zones)
285  if( !( brd_item->GetLayerSet() & layers ).any() )
286  continue;
287 
288  if( brd_item->Type() == PCB_ZONE_AREA_T )
289  {
290  ZONE_CONTAINER* zone = static_cast<ZONE_CONTAINER*>( brd_item );
291 
292  if( zone->HitTestInsideZone( wxPoint( Pos() ) ) )
293  connected_items_count++;
294  }
295  else if( brd_item->HitTest( wxPoint( Pos() ) ) )
296  connected_items_count++;
297  }
298 
299  return connected_items_count < minimal_count;
300 }
301 
302 
304 {
305  m_items.reserve( 64 );
306  m_originPad = nullptr;
307  m_originNet = -1;
308  m_conflicting = false;
309 }
310 
311 
313 {
314 }
315 
316 
318 {
319  if( !m_originPad || !m_originPad->Valid() )
320  return "<none>";
321  else
322  return m_originPad->Parent()->GetNetname();
323 }
324 
325 
326 bool CN_CLUSTER::Contains( const CN_ITEM* aItem )
327 {
328  return std::find( m_items.begin(), m_items.end(), aItem ) != m_items.end();
329 }
330 
331 
333 {
334  return std::find_if( m_items.begin(), m_items.end(), [ &aItem ] ( const CN_ITEM* item )
335  { return item->Valid() && item->Parent() == aItem; } ) != m_items.end();
336 }
337 
338 
340 {
341  for( auto item : m_items )
342  {
343  wxLogTrace( "CN", " - item : %p bitem : %p type : %d inet %s\n", item, item->Parent(),
344  item->Parent()->Type(), (const char*) item->Parent()->GetNetname().c_str() );
345  printf( "- item : %p bitem : %p type : %d inet %s\n", item, item->Parent(),
346  item->Parent()->Type(), (const char*) item->Parent()->GetNetname().c_str() );
347  item->Dump();
348  }
349 }
350 
351 
353 {
354  m_items.push_back( item );
355 
356  if( m_originNet < 0 )
357  {
358  m_originNet = item->Net();
359  }
360 
361  if( item->Parent()->Type() == PCB_PAD_T )
362  {
363  if( !m_originPad )
364  {
365  m_originPad = item;
366  m_originNet = item->Net();
367  }
368 
369  if( m_originPad && item->Net() != m_originNet )
370  {
371  m_conflicting = true;
372  }
373  }
374 }
void RemoveInvalidItems(std::vector< CN_ITEM * > &aGarbage)
static LSET AllCuMask(int aCuLayerCount=MAX_CU_LAYERS)
Function AllCuMask returns a mask holding the requested number of Cu PCB_LAYER_IDs.
Definition: lset.cpp:673
KICAD_T Type() const
Function Type()
Definition: base_struct.h:201
bool Contains(const CN_ITEM *aItem)
virtual int AnchorCount() const override
Class ZONE_CONTAINER handles a list of polygons defining a copper zone.
Definition: class_zone.h:60
BOARD_CONNECTED_ITEM * m_parent
PAD_ATTR_T GetAttribute() const
Definition: class_pad.h:405
bool Valid() const
like PAD_STANDARD, but not plated mechanical use only, no connection allowed
Definition: pad_shapes.h:65
virtual PCB_LAYER_ID GetLayer() const
Function GetLayer returns the primary layer this item is on.
int PointCount() const
Function PointCount()
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:61
class ZONE_CONTAINER, a zone area
Definition: typeinfo.h:102
virtual PCB_LAYER_ID GetLayer() const override
Function GetLayer returns the primary layer this item is on.
Definition: class_zone.cpp:175
BOARD_CONNECTED_ITEM * Parent() const
class D_PAD, a pad in a footprint
Definition: typeinfo.h:90
VECTOR2< int > VECTOR2I
Definition: vector2d.h:587
Class BOARD_CONNECTED_ITEM is a base class derived from BOARD_ITEM for items that can be connected an...
const wxPoint & GetEnd() const
Definition: class_track.h:119
class TRACK, a track segment (segment on a copper layer)
Definition: typeinfo.h:95
wxString OriginNetName() const
bool IsDangling() const
has meaning only for tracks and vias.
Class LSET is a set of PCB_LAYER_IDs.
bool Valid() const
bool HitTestInsideZone(const wxPoint &aPosition) const
Function HitTest tests if a point is inside the zone area, i.e.
Definition: class_zone.h:262
const wxPoint & GetStart() const
Definition: class_track.h:122
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:402
virtual LSET GetLayerSet() const
Function GetLayerSet returns a "layer mask", which is a bitmap of all layers on which the TRACK segme...
const SHAPE_LINE_CHAIN & COutline(int aIndex) const
void Add(CN_ITEM *item)
int Net() const
Like smd, does not appear on the solder paste layer (default) note also has a special attribute in Ge...
Definition: pad_shapes.h:62
const SHAPE_POLY_SET & GetFilledPolysList() const
Function GetFilledPolysList returns a reference to the list of filled polygons.
Definition: class_zone.h:540
virtual const VECTOR2I GetAnchor(int n) const
void SetDirty(bool aDirty)
int GetNetCode() const
Function GetNetCode.
virtual bool HitTest(const wxPoint &aPosition) const override
Function HitTest tests if aPosition is contained within or on the bounding area of an item...
void AddAnchor(const VECTOR2I &aPos)
BOARD_CONNECTED_ITEM * Parent() const
virtual const VECTOR2I GetAnchor(int n) const override
void SetLayer(int aLayer)
Function SetLayer()
size_t i
Definition: json11.cpp:597
CN_ITEM(BOARD_CONNECTED_ITEM *aParent, bool aCanChangeNet, int aAnchorCount=2)
wxPoint ShapePos() const
Definition: class_pad.cpp:517
virtual int AnchorCount() 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:96
const VECTOR2I & CPoint(int aIndex) const
Function CPoint()
Class LAYER_RANGE.
Definition: pns_layerset.h:32
void RemoveInvalidRefs()