KiCad PCB EDA Suite
netinfo.h
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) 2009 Jean-Pierre Charras, jp.charras at wanadoo.fr
5  * Copyright (C) 1992-2021 KiCad Developers, see AUTHORS.txt for contributors.
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * as published by the Free Software Foundation; either version 2
10  * of the License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, you may find one here:
19  * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
20  * or you may search the http://www.gnu.org website for the version 2 license,
21  * or you may write to the Free Software Foundation, Inc.,
22  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
23  */
24 
25 /*
26  * Classes to handle info on nets
27  */
28 
29 #ifndef CLASS_NETINFO_
30 #define CLASS_NETINFO_
31 
32 #include <macros_swig.h>
33 #include <gr_basic.h>
34 #include <netclass.h>
35 #include <board_item.h>
36 
37 
38 
39 class wxDC;
40 class wxPoint;
41 class LINE_READER;
42 class EDA_DRAW_FRAME;
43 class PAD;
44 class PCB_TRACK;
45 class BOARD;
46 class BOARD_ITEM;
47 class MSG_PANEL_ITEM;
48 class PCB_BASE_FRAME;
49 
50 
51 /*****************************/
52 /* flags for a RATSNEST_ITEM */
53 /*****************************/
54 #define CH_VISIBLE 1 /* Visible */
55 #define CH_UNROUTABLE 2 /* Don't use autorouter. */
56 #define CH_ROUTE_REQ 4 /* Must be routed by the autorouter. */
57 #define CH_ACTIF 8 /* Not routed. */
58 #define LOCAL_RATSNEST_ITEM 0x8000 /* Line between two pads of a single footprint. */
59 
60 DECL_VEC_FOR_SWIG( PADS_VEC, PAD* )
61 DECL_VEC_FOR_SWIG( TRACKS_VEC, PCB_TRACK* )
62 
63 
66 class NETINFO_ITEM : public BOARD_ITEM
67 {
68 public:
69 
70  NETINFO_ITEM( BOARD* aParent, const wxString& aNetName = wxEmptyString, int aNetCode = -1 );
71  ~NETINFO_ITEM();
72 
73  static inline bool ClassOf( const EDA_ITEM* aItem )
74  {
75  return aItem && PCB_NETINFO_T == aItem->Type();
76  }
77 
78  wxString GetClass() const override
79  {
80  return wxT( "NETINFO_ITEM" );
81  }
82 
83 #if defined(DEBUG)
84  void Show( int nestLevel, std::ostream& os ) const override
85  {
86  }
87 #endif
88 
89  wxPoint GetPosition() const override
90  {
91  static wxPoint dummy(0, 0);
92  return dummy;
93  }
94 
95  void SetPosition( const wxPoint& aPos ) override
96  {
97  }
98 
99  EDA_ITEM* Clone() const override
100  {
101  return new NETINFO_ITEM( *this );
102  }
103 
104  void SetNetClass( const NETCLASSPTR& aNetClass );
105 
111  {
112  return m_netClass.get();
113  }
114 
115  wxString GetNetClassName() const
116  {
117  return m_netClass ? m_netClass->GetName() : NETCLASS::Default;
118  }
119 
120  int GetNetCode() const { return m_netCode; }
121  void SetNetCode( int aNetCode ) { m_netCode = aNetCode; }
122 
126  const wxString& GetNetname() const { return m_netname; }
127 
131  const wxString& GetShortNetname() const { return m_shortNetname; }
132 
137  void SetNetname( const wxString& aNewName )
138  {
139  m_netname = aNewName;
140 
141  if( aNewName.Contains( wxT( "/" ) ) )
142  m_shortNetname = aNewName.AfterLast( '/' );
143  else
144  m_shortNetname = aNewName;
145  }
146 
147  bool IsCurrent() const { return m_isCurrent; }
148  void SetIsCurrent( bool isCurrent ) { m_isCurrent = isCurrent; }
149 
156  void GetMsgPanelInfo( EDA_DRAW_FRAME* aFrame, std::vector<MSG_PANEL_ITEM>& aList ) override;
157 
161  void Clear()
162  {
163  SetNetClass( NETCLASSPTR());
164  }
165 
166  BOARD* GetParent() const
167  {
168  return m_parent;
169  }
170 
171 private:
172  friend class NETINFO_LIST;
173 
174  int m_netCode;
175  wxString m_netname;
176  wxString m_shortNetname;
177 
178  NETCLASSPTR m_netClass;
179 
180  bool m_isCurrent;
181 
185 };
186 
187 
189 {
190 public:
192  {
193  m_board = nullptr;
194  }
195 
196 
200  void SetBoard( const BOARD* aBoard )
201  {
202  m_board = aBoard;
203  Update();
204  }
205 
211  void Update();
212 
221  int Translate( int aNetCode ) const;
222 
225  class iterator
226  {
227  public:
228  iterator( std::map<int, int>::const_iterator aIter, const NETINFO_MAPPING* aMapping ) :
229  m_iterator( aIter ), m_mapping( aMapping )
230  {
231  }
232 
235  {
236  ++m_iterator;
237 
238  return *this;
239  }
240 
243  {
244  iterator ret = *this;
245  ++m_iterator;
246 
247  return ret;
248  }
249 
250  NETINFO_ITEM* operator*() const;
251 
252  NETINFO_ITEM* operator->() const;
253 
254  bool operator!=( const iterator& aOther ) const
255  {
256  return m_iterator != aOther.m_iterator;
257  }
258 
259  bool operator==( const iterator& aOther ) const
260  {
261  return m_iterator == aOther.m_iterator;
262  }
263 
264  private:
265  std::map<int, int>::const_iterator m_iterator;
267  };
268 
275  iterator begin() const
276  {
277  return iterator( m_netMapping.begin(), this );
278  }
279 
286  iterator end() const
287  {
288  return iterator( m_netMapping.end(), this );
289  }
290 
294  int GetSize() const
295  {
296  return m_netMapping.size();
297  }
298 
299 private:
300  const BOARD* m_board;
301  std::map<int, int> m_netMapping;
302 };
304 
305 
306 #if 0
307 // waiting for swig to support std::unordered_map, see
308 // http://www.swig.org/Doc3.0/CPlusPlus11.html
309 // section 7.3.3
310 #include <hashtables.h>
311 DECL_HASH_FOR_SWIG( NETNAMES_MAP, wxString, NETINFO_ITEM* )
312 DECL_HASH_FOR_SWIG( NETCODES_MAP, int, NETINFO_ITEM* )
313 #else
314 // use std::map for now
315 DECL_MAP_FOR_SWIG( NETNAMES_MAP, wxString, NETINFO_ITEM* )
316 DECL_MAP_FOR_SWIG( NETCODES_MAP, int, NETINFO_ITEM* )
317 #endif
318 
323 {
324  friend class BOARD;
325 
326 public:
327  NETINFO_LIST( BOARD* aParent );
328  ~NETINFO_LIST();
329 
334  NETINFO_ITEM* GetNetItem( int aNetCode ) const;
335 
340  NETINFO_ITEM* GetNetItem( const wxString& aNetName ) const;
341 
346  unsigned GetNetCount() const { return m_netNames.size(); }
347 
352  void AppendNet( NETINFO_ITEM* aNewElement );
353 
357  void RemoveNet( NETINFO_ITEM* aNet );
358  void RemoveUnusedNets();
359 
364  const NETNAMES_MAP& NetsByName() const { return m_netNames; }
366 
368  const NETCODES_MAP& NetsByNetcode() const { return m_netCodes; }
369 
372  static const int UNCONNECTED;
373 
376  static const int ORPHANED;
377 
381  {
382  static NETINFO_ITEM* g_orphanedItem;
383 
384  if( !g_orphanedItem )
385  g_orphanedItem = new NETINFO_ITEM( nullptr, wxEmptyString, NETINFO_LIST::UNCONNECTED );
386 
387  return g_orphanedItem;
388  }
389 
390 #if defined(DEBUG)
391  void Show() const;
392 #endif
393 
394 #ifndef SWIG
395  class iterator
398  {
399  public:
400  iterator( NETNAMES_MAP::const_iterator aIter ) : m_iterator( aIter )
401  {
402  }
403 
406  {
407  ++m_iterator;
408  return *this;
409  }
410 
413  {
414  iterator ret = *this;
415  ++m_iterator;
416  return ret;
417  }
418 
420  {
421  return m_iterator->second;
422  }
423 
425  {
426  return m_iterator->second;
427  }
428 
429  bool operator!=( const iterator& aOther ) const
430  {
431  return m_iterator != aOther.m_iterator;
432  }
433 
434  bool operator==( const iterator& aOther ) const
435  {
436  return m_iterator == aOther.m_iterator;
437  }
438 
439  private:
440  NETNAMES_MAP::const_iterator m_iterator;
441  };
442 
443  iterator begin() const
444  {
445  return iterator( m_netNames.begin() );
446  }
447 
448  iterator end() const
449  {
450  return iterator( m_netNames.end() );
451  }
452 #endif
453 
454  BOARD* GetParent() const
455  {
456  return m_parent;
457  }
458 
459 private:
463  void clear();
464 
470  void buildListOfNets();
471 
475  int getFreeNetCode();
476 
478 
479  NETNAMES_MAP m_netNames;
480  NETCODES_MAP m_netCodes;
481 
483 };
484 
485 #endif // CLASS_NETINFO_
NETCLASS * GetNetClass()
Definition: netinfo.h:110
iterator end() const
Definition: netinfo.h:448
NETNAMES_MAP::const_iterator m_iterator
Definition: netinfo.h:440
BOARD * GetParent() const
Definition: netinfo.h:454
NETINFO_ITEM * operator *() const
Definition: netinfo.h:419
An abstract class from which implementation specific LINE_READERs may be derived to read single lines...
Definition: richio.h:80
bool operator!=(const iterator &aOther) const
Definition: netinfo.h:429
const iterator & operator++()
pre-increment operator
Definition: netinfo.h:405
BOARD * GetParent() const
Definition: netinfo.h:166
static NETINFO_ITEM * OrphanedItem()
Wrapper class, so you can iterate through NETINFO_ITEM*s, not std::pair<int/wxString,...
Definition: netinfo.h:380
A base class for any item which can be embedded within the BOARD container class, and therefore insta...
Definition: board_item.h:49
wxString GetNetClassName() const
Definition: netinfo.h:115
const NETCODES_MAP & NetsByNetcode() const
Return the netcode map, at least for python.
Definition: netinfo.h:368
const iterator & operator++()
pre-increment operator
Definition: netinfo.h:234
void SetIsCurrent(bool isCurrent)
Definition: netinfo.h:148
const BOARD * m_board
Board for which mapping is prepared.
Definition: netinfo.h:300
bool m_isCurrent
Indicates the net is currently in use.
Definition: netinfo.h:180
iterator operator++(int)
post-increment operator
Definition: netinfo.h:242
NETNAMES_MAP m_netNames
map of <wxString, NETINFO_ITEM*>, is NETINFO_ITEM owner
Definition: netinfo.h:479
BOARD * m_parent
The parent board the net belongs to.
Definition: netinfo.h:184
void RemoveUnusedNets()
The base class for create windows for drawing purpose.
bool IsCurrent() const
Definition: netinfo.h:147
int getFreeNetCode()
Return the first available net code that is not used by any other net.
const wxString & GetShortNetname() const
Definition: netinfo.h:131
std::map< int, int > m_netMapping
Map that allows saving net codes with consecutive numbers (for compatibility reasons)
Definition: netinfo.h:301
const NETINFO_MAPPING * m_mapping
Definition: netinfo.h:266
BOARD * m_parent
Definition: netinfo.h:477
const NETNAMES_MAP & NetsByName() const
Return the name map, at least for python.
Definition: netinfo.h:365
void SetBoard(const BOARD *aBoard)
Set a BOARD object that is used to prepare the net code map.
Definition: netinfo.h:200
NETINFO_ITEM * operator->() const
Definition: netinfo.h:424
void AppendNet(NETINFO_ITEM *aNewElement)
Add aNewElement to the end of the net list.
static LIB_SYMBOL * dummy()
Used to draw a dummy shape when a LIB_SYMBOL is not found in library.
Definition: sch_symbol.cpp:72
NETCLASSPTR m_netClass
Definition: netinfo.h:178
void RemoveNet(NETINFO_ITEM *aNet)
Remove a net from the net list.
iterator(std::map< int, int >::const_iterator aIter, const NETINFO_MAPPING *aMapping)
Definition: netinfo.h:228
int GetSize() const
Definition: netinfo.h:294
iterator end() const
Return iterator to the last entry in the mapping.
Definition: netinfo.h:286
static const char Default[]
the name of the default NETCLASS
Definition: netclass.h:49
Container for NETINFO_ITEM elements, which are the nets.
Definition: netinfo.h:322
NETINFO_ITEM * operator->() const
#define DECL_HASH_FOR_SWIG(TypeName, KeyType, ValueType)
Declare a std::unordered_map but no swig template.
Definition: hashtables.h:107
iterator begin() const
Definition: netinfo.h:443
int m_newNetCode
possible value for new net code assignment
Definition: netinfo.h:482
iterator operator++(int)
post-increment operator
Definition: netinfo.h:412
int m_netCode
A number equivalent to the net name.
Definition: netinfo.h:174
A collection of nets and the parameters used to route or test these nets.
Definition: netclass.h:46
void SetNetCode(int aNetCode)
Definition: netinfo.h:121
#define DECL_MAP_FOR_SWIG(TypeName, KeyType, ValueType)
Definition: macros_swig.h:52
const wxString & GetNetname() const
Definition: netinfo.h:126
void SetNetname(const wxString &aNewName)
Set the long netname to aNetName, and the short netname to the last token in the long netname's path.
Definition: netinfo.h:137
wxString GetClass() const override
Return the class name.
Definition: netinfo.h:78
wxString m_shortNetname
short net name, like vout from /sheet/subsheet/vout.
Definition: netinfo.h:176
static const int ORPHANED
NETINFO_ITEM meaning that there was no net assigned for an item, as there was no board storing net li...
Definition: netinfo.h:376
wxString m_netname
Full net name like /sheet/subsheet/vout used by Eeschema.
Definition: netinfo.h:175
bool operator==(const iterator &aOther) const
Definition: netinfo.h:259
Handle the data for a net.
Definition: netinfo.h:66
iterator(NETNAMES_MAP::const_iterator aIter)
Definition: netinfo.h:400
void Clear()
Set all fields to their default values.
Definition: netinfo.h:161
bool operator==(const iterator &aOther) const
Definition: netinfo.h:434
std::map< int, int >::const_iterator m_iterator
Definition: netinfo.h:265
Information pertinent to a Pcbnew printed circuit board.
Definition: board.h:191
wxPoint GetPosition() const override
Definition: netinfo.h:89
class NETINFO_ITEM, a description of a net
Definition: typeinfo.h:107
NETINFO_ITEM * operator *() const
A base class for most all the KiCad significant classes used in schematics and boards.
Definition: eda_item.h:99
void Update()
Prepare a mapping for net codes so they can be saved as consecutive numbers.
unsigned GetNetCount() const
Definition: netinfo.h:346
iterator begin() const
Return iterator to the first entry in the mapping.
Definition: netinfo.h:275
This file contains macros just for swig binding.
void clear()
Delete the list of nets (and free memory).
NETINFO_LIST(BOARD *aParent)
EDA_MSG_PANEL items for displaying messages.
Definition: msgpanel.h:53
#define DECL_VEC_FOR_SWIG(TypeName, MemberType)
Declare a std::vector but no swig template.
Definition: macros_swig.h:50
Definition: pad.h:57
NETINFO_ITEM * GetNetItem(int aNetCode) const
EDA_ITEM * Clone() const override
Create a duplicate of this item with linked list members set to NULL.
Definition: netinfo.h:99
static const int UNCONNECTED
Constant that forces initialization of a netinfo item to the NETINFO_ITEM ORPHANED (typically -1) whe...
Definition: netinfo.h:372
static bool ClassOf(const EDA_ITEM *aItem)
Definition: netinfo.h:73
Base PCB main window class for Pcbnew, Gerbview, and CvPcb footprint viewer.
void buildListOfNets()
Rebuild the list of NETINFO_ITEMs.
int Translate(int aNetCode) const
Translate net number according to the map prepared by Update() function.
bool operator!=(const iterator &aOther) const
Definition: netinfo.h:254
KICAD_T Type() const
Returns the type of object.
Definition: eda_item.h:112
void SetPosition(const wxPoint &aPos) override
Definition: netinfo.h:95
NETCODES_MAP m_netCodes
map of <int, NETINFO_ITEM*> is NOT owner
Definition: netinfo.h:480
int GetNetCode() const
Definition: netinfo.h:120