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  // the minimal number of items connected to item_ref
257  // at this anchor point to decide the anchor is *not* dangling
258  size_t minimal_count = 1;
259  size_t connected_count = m_item->ConnectedItems().size();
260 
261  // a via can be removed if connected to only one other item.
262  if( Parent()->Type() == PCB_VIA_T )
263  return connected_count < 2;
264 
265  if( m_item->AnchorCount() == 1 )
266  return connected_count < minimal_count;
267 
268  // Only items with multiple anchors might have additional items connected that we
269  // should ignore for this calculation.
270  for( auto item : m_item->ConnectedItems() )
271  {
272  if( !item->Parent()->HitTest( wxPoint( Pos().x, Pos().y ) ) )
273  connected_count--;
274  }
275 
276  return connected_count < minimal_count;
277 }
278 
279 
281 {
282  m_items.reserve( 64 );
283  m_originPad = nullptr;
284  m_originNet = -1;
285  m_conflicting = false;
286 }
287 
288 
290 {
291 }
292 
293 
295 {
296  if( !m_originPad || !m_originPad->Valid() )
297  return "<none>";
298  else
299  return m_originPad->Parent()->GetNetname();
300 }
301 
302 
303 bool CN_CLUSTER::Contains( const CN_ITEM* aItem )
304 {
305  return std::find( m_items.begin(), m_items.end(), aItem ) != m_items.end();
306 }
307 
308 
310 {
311  return std::find_if( m_items.begin(), m_items.end(), [ &aItem ] ( const CN_ITEM* item )
312  { return item->Valid() && item->Parent() == aItem; } ) != m_items.end();
313 }
314 
315 
317 {
318  for( auto item : m_items )
319  {
320  wxLogTrace( "CN", " - item : %p bitem : %p type : %d inet %s\n", item, item->Parent(),
321  item->Parent()->Type(), (const char*) item->Parent()->GetNetname().c_str() );
322  printf( "- item : %p bitem : %p type : %d inet %s\n", item, item->Parent(),
323  item->Parent()->Type(), (const char*) item->Parent()->GetNetname().c_str() );
324  item->Dump();
325  }
326 }
327 
328 
330 {
331  m_items.push_back( item );
332 
333  if( m_originNet < 0 )
334  {
335  m_originNet = item->Net();
336  }
337 
338  if( item->Parent()->Type() == PCB_PAD_T )
339  {
340  if( !m_originPad )
341  {
342  m_originPad = item;
343  m_originNet = item->Net();
344  }
345 
346  if( m_originPad && item->Net() != m_originNet )
347  {
348  m_conflicting = true;
349  }
350  }
351 }
const CONNECTED_ITEMS & ConnectedItems() const
bool Contains(const CN_ITEM *aItem)
virtual int AnchorCount() const override
std::vector< CN_ITEM * > m_items
Class ZONE_CONTAINER handles a list of polygons defining a copper zone.
Definition: class_zone.h:59
int GetNetCode() const
Function GetNetCode.
BOARD_CONNECTED_ITEM * m_parent
like PAD_STANDARD, but not plated mechanical use only, no connection allowed
Definition: pad_shapes.h:65
std::shared_ptr< CN_CLUSTER > m_cluster
Cluster to which the anchor belongs.
const wxPoint & GetStart() const
Definition: class_track.h:126
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:61
virtual PCB_LAYER_ID GetLayer() const override
Function GetLayer returns the primary layer this item is on.
Definition: class_zone.cpp:175
class D_PAD, a pad in a footprint
Definition: typeinfo.h:90
BOARD_CONNECTED_ITEM * Parent() const
bool IsDangling() const
has meaning only for tracks and vias.
VECTOR2< int > VECTOR2I
Definition: vector2d.h:587
int PointCount() const
Function PointCount()
Class BOARD_CONNECTED_ITEM is a base class derived from BOARD_ITEM for items that can be connected an...
void SetDirty(bool aDirty=true)
int Net() const
PAD_ATTR_T GetAttribute() const
Definition: class_pad.h:412
class TRACK, a track segment (segment on a copper layer)
Definition: typeinfo.h:95
virtual int AnchorCount() const
const VECTOR2I & CPoint(int aIndex) const
Function CPoint()
std::vector< CN_ITEM * > m_items
Class LSET is a set of PCB_LAYER_IDs.
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:409
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:62
bool Valid() const
virtual const VECTOR2I GetAnchor(int n) const
wxString OriginNetName() const
void AddAnchor(const VECTOR2I &aPos)
void RemoveInvalidItems(std::vector< CN_ITEM *> &aGarbage)
virtual const VECTOR2I GetAnchor(int n) const override
CN_ITEM * m_item
Item owning the anchor.
void SetLayer(int aLayer)
Function SetLayer()
CN_ITEM * m_originPad
const SHAPE_LINE_CHAIN & COutline(int aIndex) const
size_t i
Definition: json11.cpp:597
wxPoint ShapePos() const
Definition: class_pad.cpp:550
BOARD_CONNECTED_ITEM * Parent() const
const wxPoint & GetEnd() const
Definition: class_track.h:123
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:96
virtual PCB_LAYER_ID GetLayer() const
Function GetLayer returns the primary layer this item is on.
Class 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:201
const SHAPE_POLY_SET & GetFilledPolysList() const
Function GetFilledPolysList returns a reference to the list of filled polygons.
Definition: class_zone.h:559