KiCad PCB EDA Suite
netinfo_list.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) 1992-2014 KiCad Developers, see AUTHORS.txt for contributors.
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2
9  * of the License, or (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, you may find one here:
18  * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
19  * or you may search the http://www.gnu.org website for the version 2 license,
20  * or you may write to the Free Software Foundation, Inc.,
21  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
22  */
23 
24 #include <fctsys.h>
25 #include <gr_basic.h>
26 #include <common.h>
27 #include <macros.h>
28 #include <pcbnew.h>
29 #include <class_board.h>
30 #include <class_module.h>
31 #include <class_pad.h>
32 #include <class_track.h>
33 #include <class_zone.h>
34 #include <netinfo.h>
35 
36 
37 // Constructor and destructor
38 NETINFO_LIST::NETINFO_LIST( BOARD* aParent ) : m_Parent( aParent )
39 {
40  // Make sure that the unconnected net has number 0
41  AppendNet( new NETINFO_ITEM( aParent, wxEmptyString, 0 ) );
42 
43  m_newNetCode = 0;
44 }
45 
46 
48 {
49  clear();
50 }
51 
52 
54 {
55  NETNAMES_MAP::iterator it, itEnd;
56  for( it = m_netNames.begin(), itEnd = m_netNames.end(); it != itEnd; ++it )
57  delete it->second;
58 
59  m_netNames.clear();
60  m_netCodes.clear();
61  m_newNetCode = 0;
62 }
63 
64 
65 NETINFO_ITEM* NETINFO_LIST::GetNetItem( int aNetCode ) const
66 {
67  NETCODES_MAP::const_iterator result = m_netCodes.find( aNetCode );
68 
69  if( result != m_netCodes.end() )
70  return (*result).second;
71 
72  return NULL;
73 }
74 
75 
76 NETINFO_ITEM* NETINFO_LIST::GetNetItem( const wxString& aNetName ) const
77 {
78  NETNAMES_MAP::const_iterator result = m_netNames.find( aNetName );
79 
80  if( result != m_netNames.end() )
81  return (*result).second;
82 
83  return NULL;
84 }
85 
86 
88 {
89  for( NETCODES_MAP::iterator i = m_netCodes.begin(); i != m_netCodes.end(); ++i )
90  {
91  if ( i->second == aNet )
92  {
93  m_netCodes.erase(i);
94  break;
95  }
96  }
97 
98  for( NETNAMES_MAP::iterator i = m_netNames.begin(); i != m_netNames.end(); ++i )
99  {
100  if ( i->second == aNet )
101  {
102  m_netNames.erase(i);
103  break;
104  }
105  }
106 
107  m_newNetCode = std::min( m_newNetCode, aNet->m_NetCode - 1 );
108 }
109 
110 
112 {
113  // if there is a net with such name then just assign the correct number
114  NETINFO_ITEM* sameName = GetNetItem( aNewElement->GetNetname() );
115 
116  if( sameName != NULL )
117  {
118  aNewElement->m_NetCode = sameName->GetNet();
119 
120  return;
121  }
122  // be sure that net codes are consecutive
123  // negative net code means that it has to be auto assigned
124  else if( ( aNewElement->m_NetCode != (int) m_netCodes.size() ) || ( aNewElement->m_NetCode < 0 ) )
125  {
126  aNewElement->m_NetCode = getFreeNetCode();
127  }
128 
129  // net names & codes are supposed to be unique
130  assert( GetNetItem( aNewElement->GetNetname() ) == NULL );
131  assert( GetNetItem( aNewElement->GetNet() ) == NULL );
132 
133  // add an entry for fast look up by a net name using a map
134  m_netNames.insert( std::make_pair( aNewElement->GetNetname(), aNewElement ) );
135  m_netCodes.insert( std::make_pair( aNewElement->GetNet(), aNewElement ) );
136 }
137 
138 
150 {
151  // Restore the initial state of NETINFO_ITEMs
152  for( NETINFO_LIST::iterator net( begin() ), netEnd( end() ); net != netEnd; ++net )
153  net->Clear();
154 
157 }
158 
159 #if defined(DEBUG)
160 void NETINFO_LIST::Show() const
161 {
162  int i = 0;
163  NETNAMES_MAP::const_iterator it, itEnd;
164  for( it = m_netNames.begin(), itEnd = m_netNames.end(); it != itEnd; ++it )
165  {
166  printf( "[%d]: netcode:%d netname:<%s>\n",
167  i++,
168  it->second->GetNet(),
169  TO_UTF8( it->second->GetNetname() ) );
170  }
171 }
172 #endif
173 
174 
176 {
177  do {
178  if( m_newNetCode < 0 )
179  m_newNetCode = 0;
180  } while( m_netCodes.count( ++m_newNetCode ) != 0 );
181 
182  return m_newNetCode;
183 }
184 
185 
186 int NETINFO_MAPPING::Translate( int aNetCode ) const
187 {
188  std::map<int, int>::const_iterator value = m_netMapping.find( aNetCode );
189 
190  if( value != m_netMapping.end() )
191  return value->second;
192 
193  // There was no entry for the given net code
194  return aNetCode;
195 }
196 
197 
199 {
200  // Collect all the used nets
201  std::set<int> nets;
202 
203  // Be sure that the unconnected gets 0 and is mapped as 0
204  nets.insert( 0 );
205 
206  // Zones
207  for( int i = 0; i < m_board->GetAreaCount(); ++i )
208  nets.insert( m_board->GetArea( i )->GetNetCode() );
209 
210  // Tracks
211  for( auto track : m_board->Tracks() )
212  nets.insert( track->GetNetCode() );
213 
214  // Modules/pads
215  for( auto module : m_board->Modules() )
216  {
217  for( auto pad : module->Pads() )
218  {
219  nets.insert( pad->GetNetCode() );
220  }
221  }
222 
223  // Prepare the new mapping
224  m_netMapping.clear();
225 
226  // Now the nets variable stores all the used net codes (not only for pads) and we are ready to
227  // assign new consecutive net numbers
228  int newNetCode = 0;
229  for( std::set<int>::const_iterator it = nets.begin(), itEnd = nets.end(); it != itEnd; ++it )
230  m_netMapping[*it] = newNetCode++;
231 }
232 
233 
235 {
236  return m_mapping->m_board->FindNet( m_iterator->first );
237 }
238 
239 
241 {
242  return m_mapping->m_board->FindNet( m_iterator->first );
243 }
244 
245 
246 const int NETINFO_LIST::UNCONNECTED = 0;
247 const int NETINFO_LIST::ORPHANED = -1;
248 
iterator end() const
Definition: netinfo.h:538
Wrapper class, so you can iterate through NETINFO_ITEM*s, not std::pair<int/wxString,...
Definition: netinfo.h:487
NETINFO_ITEM * FindNet(int aNetcode) const
Function FindNet searches for a net with the given netcode.
int GetNetCode() const
Function GetNetCode.
BOARD * m_Parent
Definition: netinfo.h:569
const BOARD * m_board
Board for which mapping is prepared
Definition: netinfo.h:383
NETNAMES_MAP m_netNames
map of <wxString, NETINFO_ITEM*>, is NETINFO_ITEM owner
Definition: netinfo.h:571
int getFreeNetCode()
Function getFreeNetCode returns the first available net code that is not used by any other net.
Definitions for tracks, vias and zones.
std::map< int, int > m_netMapping
Map that allows saving net codes with consecutive numbers (for compatibility reasons)
Definition: netinfo.h:386
const NETINFO_MAPPING * m_mapping
Definition: netinfo.h:347
This file contains miscellaneous commonly used macros and functions.
void AppendNet(NETINFO_ITEM *aNewElement)
Function AppendNet adds aNewElement to the end of the net list.
#define TO_UTF8(wxstring)
Macro TO_UTF8 converts a wxString to a UTF8 encoded C string for all wxWidgets build modes.
Definition: macros.h:48
void SynchronizeNetsAndNetClasses()
Function SynchronizeNetsAndNetClasses copies NETCLASS info to each NET, based on NET membership in a ...
Definition: netclass.cpp:155
void RemoveNet(NETINFO_ITEM *aNet)
Function RemoveNet Removes a new from the net list.
int GetAreaCount() const
Function GetAreaCount.
Definition: class_board.h:919
#define NULL
MODULES & Modules()
Definition: class_board.h:229
NETINFO_ITEM * operator->() const
iterator begin() const
Definition: netinfo.h:533
int m_newNetCode
possible value for new net code assignment
Definition: netinfo.h:574
const wxString & GetNetname() const
Function GetNetname.
Definition: netinfo.h:232
int m_NetCode
A number equivalent to the net name.
Definition: netinfo.h:70
int SetAreasNetCodesFromNetNames(void)
Function SetAreasNetCodesFromNetNames Set the .m_NetCode member of all copper areas,...
Pad object description.
static const int ORPHANED
Constant that forces initialization of a netinfo item to the NETINFO_ITEM ORPHANED (typically -1) whe...
Definition: netinfo.h:466
NETINFO_ITEM handles the data for a net.
Definition: netinfo.h:65
std::map< int, int >::const_iterator m_iterator
Definition: netinfo.h:346
int GetNet() const
Function GetNet.
Definition: netinfo.h:224
BOARD holds information pertinent to a Pcbnew printed circuit board.
Definition: class_board.h:163
NETINFO_ITEM * operator *() const
The common library.
void Update()
Function Update Prepares a mapping for net codes so they can be saved as consecutive numbers.
void clear()
Function clear deletes the list of nets (and free memory)
Module description (excepted pads)
ZONE_CONTAINER * GetArea(int index) const
Function GetArea returns the Area (Zone Container) at a given index.
Definition: class_board.h:884
NETINFO_LIST(BOARD *aParent)
NETINFO_ITEM * GetNetItem(int aNetCode) const
Function GetItem.
static const int UNCONNECTED
Constant that holds the "unconnected net" number (typically 0) all items "connected" to this net are ...
Definition: netinfo.h:462
TRACKS & Tracks()
Definition: class_board.h:220
void buildListOfNets()
Function buildListOfNets builds or rebuilds the list of NETINFO_ITEMs The list is sorted by names.
int Translate(int aNetCode) const
Function Translate Translates net number according to the map prepared by Update() function.
NETCODES_MAP m_netCodes
map of <int, NETINFO_ITEM*> is NOT owner
Definition: netinfo.h:572