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 BOARD;
45 class BOARD_ITEM;
46 class MSG_PANEL_ITEM;
47 class PCB_BASE_FRAME;
48 
49 
50 /*****************************/
51 /* flags for a RATSNEST_ITEM */
52 /*****************************/
53 #define CH_VISIBLE 1 /* Visible */
54 #define CH_UNROUTABLE 2 /* Don't use autorouter. */
55 #define CH_ROUTE_REQ 4 /* Must be routed by the autorouter. */
56 #define CH_ACTIF 8 /* Not routed. */
57 #define LOCAL_RATSNEST_ITEM 0x8000 /* Line between two pads of a single footprint. */
58 
59 DECL_VEC_FOR_SWIG( PADS_VEC, PAD* )
60 
61 
64 class NETINFO_ITEM : public BOARD_ITEM
65 {
66 public:
67 
68  NETINFO_ITEM( BOARD* aParent, const wxString& aNetName = wxEmptyString, int aNetCode = -1 );
69  ~NETINFO_ITEM();
70 
71  static inline bool ClassOf( const EDA_ITEM* aItem )
72  {
73  return aItem && PCB_NETINFO_T == aItem->Type();
74  }
75 
76  wxString GetClass() const override
77  {
78  return wxT( "NETINFO_ITEM" );
79  }
80 
81 #if defined(DEBUG)
82  void Show( int nestLevel, std::ostream& os ) const override
83  {
84  }
85 #endif
86 
87  wxPoint GetPosition() const override
88  {
89  static wxPoint dummy(0, 0);
90  return dummy;
91  }
92 
93  void SetPosition( const wxPoint& aPos ) override
94  {
95  }
96 
97  void SetNetClass( const NETCLASSPTR& aNetClass );
98 
104  {
105  return m_netClass.get();
106  }
107 
108  wxString GetNetClassName() const
109  {
110  return m_netClass ? m_netClass->GetName() : NETCLASS::Default;
111  }
112 
113  int GetNetCode() const { return m_netCode; }
114  void SetNetCode( int aNetCode ) { m_netCode = aNetCode; }
115 
119  const wxString& GetNetname() const { return m_netname; }
120 
124  const wxString& GetShortNetname() const { return m_shortNetname; }
125 
130  void SetNetname( const wxString& aNewName )
131  {
132  m_netname = aNewName;
133 
134  if( aNewName.Contains( "/" ) )
135  m_shortNetname = aNewName.AfterLast( '/' );
136  else
137  m_shortNetname = aNewName;
138  }
139 
140  bool IsCurrent() const { return m_isCurrent; }
141  void SetIsCurrent( bool isCurrent ) { m_isCurrent = isCurrent; }
142 
149  void GetMsgPanelInfo( EDA_DRAW_FRAME* aFrame, std::vector<MSG_PANEL_ITEM>& aList ) override;
150 
154  void Clear()
155  {
156  SetNetClass( NETCLASSPTR());
157  }
158 
159  BOARD* GetParent() const
160  {
161  return m_parent;
162  }
163 
164 private:
165  friend class NETINFO_LIST;
166 
167  int m_netCode;
168  wxString m_netname;
169  wxString m_shortNetname;
170 
171  NETCLASSPTR m_netClass;
172 
173  bool m_isCurrent;
174 
178 };
179 
180 
182 {
183 public:
185  {
186  m_board = NULL;
187  }
188 
189 
193  void SetBoard( const BOARD* aBoard )
194  {
195  m_board = aBoard;
196  Update();
197  }
198 
204  void Update();
205 
214  int Translate( int aNetCode ) const;
215 
218  class iterator
219  {
220  public:
221  iterator( std::map<int, int>::const_iterator aIter, const NETINFO_MAPPING* aMapping ) :
222  m_iterator( aIter ), m_mapping( aMapping )
223  {
224  }
225 
228  {
229  ++m_iterator;
230 
231  return *this;
232  }
233 
236  {
237  iterator ret = *this;
238  ++m_iterator;
239 
240  return ret;
241  }
242 
243  NETINFO_ITEM* operator*() const;
244 
245  NETINFO_ITEM* operator->() const;
246 
247  bool operator!=( const iterator& aOther ) const
248  {
249  return m_iterator != aOther.m_iterator;
250  }
251 
252  bool operator==( const iterator& aOther ) const
253  {
254  return m_iterator == aOther.m_iterator;
255  }
256 
257  private:
258  std::map<int, int>::const_iterator m_iterator;
260  };
261 
268  iterator begin() const
269  {
270  return iterator( m_netMapping.begin(), this );
271  }
272 
279  iterator end() const
280  {
281  return iterator( m_netMapping.end(), this );
282  }
283 
287  int GetSize() const
288  {
289  return m_netMapping.size();
290  }
291 
292 private:
293  const BOARD* m_board;
294  std::map<int, int> m_netMapping;
295 };
297 
298 
299 #if 0
300 // waiting for swig to support std::unordered_map, see
301 // http://www.swig.org/Doc3.0/CPlusPlus11.html
302 // section 7.3.3
303 #include <hashtables.h>
304 DECL_HASH_FOR_SWIG( NETNAMES_MAP, wxString, NETINFO_ITEM* )
305 DECL_HASH_FOR_SWIG( NETCODES_MAP, int, NETINFO_ITEM* )
306 #else
307 // use std::map for now
308 DECL_MAP_FOR_SWIG( NETNAMES_MAP, wxString, NETINFO_ITEM* )
309 DECL_MAP_FOR_SWIG( NETCODES_MAP, int, NETINFO_ITEM* )
310 #endif
311 
316 {
317  friend class BOARD;
318 
319 public:
320  NETINFO_LIST( BOARD* aParent );
321  ~NETINFO_LIST();
322 
327  NETINFO_ITEM* GetNetItem( int aNetCode ) const;
328 
333  NETINFO_ITEM* GetNetItem( const wxString& aNetName ) const;
334 
339  unsigned GetNetCount() const { return m_netNames.size(); }
340 
345  void AppendNet( NETINFO_ITEM* aNewElement );
346 
350  void RemoveNet( NETINFO_ITEM* aNet );
351  void RemoveUnusedNets();
352 
357  const NETNAMES_MAP& NetsByName() const { return m_netNames; }
359 
361  const NETCODES_MAP& NetsByNetcode() const { return m_netCodes; }
362 
365  static const int UNCONNECTED;
366 
369  static const int ORPHANED;
370 
374  {
375  static NETINFO_ITEM* g_orphanedItem;
376 
377  if( !g_orphanedItem )
378  g_orphanedItem = new NETINFO_ITEM( nullptr, wxEmptyString, NETINFO_LIST::UNCONNECTED );
379 
380  return g_orphanedItem;
381  }
382 
383 #if defined(DEBUG)
384  void Show() const;
385 #endif
386 
387 #ifndef SWIG
388  class iterator
391  {
392  public:
393  iterator( NETNAMES_MAP::const_iterator aIter ) : m_iterator( aIter )
394  {
395  }
396 
399  {
400  ++m_iterator;
401  return *this;
402  }
403 
406  {
407  iterator ret = *this;
408  ++m_iterator;
409  return ret;
410  }
411 
413  {
414  return m_iterator->second;
415  }
416 
418  {
419  return m_iterator->second;
420  }
421 
422  bool operator!=( const iterator& aOther ) const
423  {
424  return m_iterator != aOther.m_iterator;
425  }
426 
427  bool operator==( const iterator& aOther ) const
428  {
429  return m_iterator == aOther.m_iterator;
430  }
431 
432  private:
433  NETNAMES_MAP::const_iterator m_iterator;
434  };
435 
436  iterator begin() const
437  {
438  return iterator( m_netNames.begin() );
439  }
440 
441  iterator end() const
442  {
443  return iterator( m_netNames.end() );
444  }
445 #endif
446 
447  BOARD* GetParent() const
448  {
449  return m_parent;
450  }
451 
452 private:
456  void clear();
457 
463  void buildListOfNets();
464 
468  int getFreeNetCode();
469 
471 
472  NETNAMES_MAP m_netNames;
473  NETCODES_MAP m_netCodes;
474 
476 };
477 
478 #endif // CLASS_NETINFO_
NETCLASS * GetNetClass()
Definition: netinfo.h:103
iterator end() const
Definition: netinfo.h:441
NETNAMES_MAP::const_iterator m_iterator
Definition: netinfo.h:433
BOARD * GetParent() const
Definition: netinfo.h:447
NETINFO_ITEM * operator *() const
Definition: netinfo.h:412
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:422
const iterator & operator++()
pre-increment operator
Definition: netinfo.h:398
BOARD * GetParent() const
Definition: netinfo.h:159
static NETINFO_ITEM * OrphanedItem()
Wrapper class, so you can iterate through NETINFO_ITEM*s, not std::pair<int/wxString,...
Definition: netinfo.h:373
A base class for any item which can be embedded within the BOARD container class, and therefore insta...
Definition: board_item.h:82
wxString GetNetClassName() const
Definition: netinfo.h:108
const NETCODES_MAP & NetsByNetcode() const
Return the netcode map, at least for python.
Definition: netinfo.h:361
const iterator & operator++()
pre-increment operator
Definition: netinfo.h:227
void SetIsCurrent(bool isCurrent)
Definition: netinfo.h:141
const BOARD * m_board
Board for which mapping is prepared.
Definition: netinfo.h:293
bool m_isCurrent
Indicates the net is currently in use.
Definition: netinfo.h:173
static LIB_PART * dummy()
Used to draw a dummy shape when a LIB_PART is not found in library.
Definition: sch_symbol.cpp:69
iterator operator++(int)
post-increment operator
Definition: netinfo.h:235
NETNAMES_MAP m_netNames
map of <wxString, NETINFO_ITEM*>, is NETINFO_ITEM owner
Definition: netinfo.h:472
BOARD * m_parent
The parent board the net belongs to.
Definition: netinfo.h:177
void RemoveUnusedNets()
The base class for create windows for drawing purpose.
bool IsCurrent() const
Definition: netinfo.h:140
int getFreeNetCode()
Return the first available net code that is not used by any other net.
const wxString & GetShortNetname() const
Definition: netinfo.h:124
std::map< int, int > m_netMapping
Map that allows saving net codes with consecutive numbers (for compatibility reasons)
Definition: netinfo.h:294
const NETINFO_MAPPING * m_mapping
Definition: netinfo.h:259
BOARD * m_parent
Definition: netinfo.h:470
const NETNAMES_MAP & NetsByName() const
Return the name map, at least for python.
Definition: netinfo.h:358
void SetBoard(const BOARD *aBoard)
Set a BOARD object that is used to prepare the net code map.
Definition: netinfo.h:193
NETINFO_ITEM * operator->() const
Definition: netinfo.h:417
void AppendNet(NETINFO_ITEM *aNewElement)
Add aNewElement to the end of the net list.
NETCLASSPTR m_netClass
Definition: netinfo.h:171
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:221
int GetSize() const
Definition: netinfo.h:287
iterator end() const
Return iterator to the last entry in the mapping.
Definition: netinfo.h:279
#define NULL
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:315
NETINFO_ITEM * operator->() const
#define DECL_HASH_FOR_SWIG(TypeName, KeyType, ValueType)
Declare a std::unordered_map but no swig template.
Definition: hashtables.h:116
iterator begin() const
Definition: netinfo.h:436
int m_newNetCode
possible value for new net code assignment
Definition: netinfo.h:475
iterator operator++(int)
post-increment operator
Definition: netinfo.h:405
int m_netCode
A number equivalent to the net name.
Definition: netinfo.h:167
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:114
#define DECL_MAP_FOR_SWIG(TypeName, KeyType, ValueType)
Definition: macros_swig.h:52
const wxString & GetNetname() const
Definition: netinfo.h:119
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:130
wxString GetClass() const override
Return the class name.
Definition: netinfo.h:76
wxString m_shortNetname
short net name, like vout from /sheet/subsheet/vout.
Definition: netinfo.h:169
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:369
wxString m_netname
Full net name like /sheet/subsheet/vout used by Eeschema.
Definition: netinfo.h:168
bool operator==(const iterator &aOther) const
Definition: netinfo.h:252
Handle the data for a net.
Definition: netinfo.h:64
iterator(NETNAMES_MAP::const_iterator aIter)
Definition: netinfo.h:393
void Clear()
Set all fields to their default values.
Definition: netinfo.h:154
bool operator==(const iterator &aOther) const
Definition: netinfo.h:427
std::map< int, int >::const_iterator m_iterator
Definition: netinfo.h:258
Information pertinent to a Pcbnew printed circuit board.
Definition: board.h:190
wxPoint GetPosition() const override
Definition: netinfo.h:87
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:149
void Update()
Prepare a mapping for net codes so they can be saved as consecutive numbers.
unsigned GetNetCount() const
Definition: netinfo.h:339
iterator begin() const
Return iterator to the first entry in the mapping.
Definition: netinfo.h:268
This file contains macros just for swig binding.
void clear()
Delete the list of nets (and free memory).
NETINFO_LIST(BOARD *aParent)
EDA_MSG_ITEM is used EDA_MSG_PANEL as the item type for displaying messages.
Definition: msgpanel.h:54
#define DECL_VEC_FOR_SWIG(TypeName, MemberType)
Declare a std::vector but no swig template.
Definition: macros_swig.h:50
Definition: pad.h:60
NETINFO_ITEM * GetNetItem(int aNetCode) const
static const int UNCONNECTED
Constant that forces initialization of a netinfo item to the NETINFO_ITEM ORPHANED (typically -1) whe...
Definition: netinfo.h:365
static bool ClassOf(const EDA_ITEM *aItem)
Definition: netinfo.h:71
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:247
KICAD_T Type() const
Returns the type of object.
Definition: eda_item.h:162
void SetPosition(const wxPoint &aPos) override
Definition: netinfo.h:93
NETCODES_MAP m_netCodes
map of <int, NETINFO_ITEM*> is NOT owner
Definition: netinfo.h:473
int GetNetCode() const
Definition: netinfo.h:113