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-2020 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 <board.h>
25 #include <footprint.h>
26 #include <pad.h>
27 #include <track.h>
28 #include <zone.h>
29 #include <netinfo.h>
30 
31 
32 // Constructor and destructor
33 NETINFO_LIST::NETINFO_LIST( BOARD* aParent ) : m_Parent( aParent )
34 {
35  // Make sure that the unconnected net has number 0
36  AppendNet( new NETINFO_ITEM( aParent, wxEmptyString, 0 ) );
37 
38  m_newNetCode = 0;
39 }
40 
41 
43 {
44  clear();
45 }
46 
47 
49 {
50  NETNAMES_MAP::iterator it, itEnd;
51  for( it = m_netNames.begin(), itEnd = m_netNames.end(); it != itEnd; ++it )
52  delete it->second;
53 
54  m_netNames.clear();
55  m_netCodes.clear();
56  m_newNetCode = 0;
57 }
58 
59 
60 NETINFO_ITEM* NETINFO_LIST::GetNetItem( int aNetCode ) const
61 {
62  NETCODES_MAP::const_iterator result = m_netCodes.find( aNetCode );
63 
64  if( result != m_netCodes.end() )
65  return (*result).second;
66 
67  return NULL;
68 }
69 
70 
71 NETINFO_ITEM* NETINFO_LIST::GetNetItem( const wxString& aNetName ) const
72 {
73  NETNAMES_MAP::const_iterator result = m_netNames.find( aNetName );
74 
75  if( result != m_netNames.end() )
76  return (*result).second;
77 
78  return NULL;
79 }
80 
81 
83 {
84  for( NETCODES_MAP::iterator i = m_netCodes.begin(); i != m_netCodes.end(); ++i )
85  {
86  if ( i->second == aNet )
87  {
88  m_netCodes.erase(i);
89  break;
90  }
91  }
92 
93  for( NETNAMES_MAP::iterator i = m_netNames.begin(); i != m_netNames.end(); ++i )
94  {
95  if ( i->second == aNet )
96  {
97  m_netNames.erase(i);
98  break;
99  }
100  }
101 
102  m_newNetCode = std::min( m_newNetCode, aNet->m_NetCode - 1 );
103 }
104 
105 
107 {
108  NETCODES_MAP existingNets = m_netCodes;
109 
110  m_netCodes.clear();
111  m_netNames.clear();
112 
113  for( std::pair<const int, NETINFO_ITEM*> item : existingNets )
114  {
115  if( item.second->IsCurrent() )
116  {
117  m_netNames.insert( std::make_pair( item.second->GetNetname(), item.second ) );
118  m_netCodes.insert( std::make_pair( item.first, item.second ) );
119  }
120  }
121 }
122 
123 
125 {
126  // if there is a net with such name then just assign the correct number
127  NETINFO_ITEM* sameName = GetNetItem( aNewElement->GetNetname() );
128 
129  if( sameName != NULL )
130  {
131  aNewElement->m_NetCode = sameName->GetNet();
132 
133  return;
134  }
135  // be sure that net codes are consecutive
136  // negative net code means that it has to be auto assigned
137  else if( ( aNewElement->m_NetCode != (int) m_netCodes.size() ) || ( aNewElement->m_NetCode < 0 ) )
138  {
139  aNewElement->m_NetCode = getFreeNetCode();
140  }
141 
142  // net names & codes are supposed to be unique
143  assert( GetNetItem( aNewElement->GetNetname() ) == NULL );
144  assert( GetNetItem( aNewElement->GetNet() ) == NULL );
145 
146  // add an entry for fast look up by a net name using a map
147  m_netNames.insert( std::make_pair( aNewElement->GetNetname(), aNewElement ) );
148  m_netCodes.insert( std::make_pair( aNewElement->GetNet(), aNewElement ) );
149 }
150 
151 
153 {
154  // Restore the initial state of NETINFO_ITEMs
155  for( NETINFO_ITEM* net : *this )
156  net->Clear();
157 
160 }
161 
162 
163 #if defined(DEBUG)
164 void NETINFO_LIST::Show() const
165 {
166  int i = 0;
167  NETNAMES_MAP::const_iterator it, itEnd;
168 
169  for( it = m_netNames.begin(), itEnd = m_netNames.end(); it != itEnd; ++it )
170  {
171  wxLogDebug( "[%d]: netcode:%d netname:<%s>\n",
172  i++,
173  it->second->GetNet(),
174  TO_UTF8( it->second->GetNetname() ) );
175  }
176 }
177 #endif
178 
179 
181 {
182  do
183  {
184  if( m_newNetCode < 0 )
185  m_newNetCode = 0;
186  } while( m_netCodes.count( ++m_newNetCode ) != 0 );
187 
188  return m_newNetCode;
189 }
190 
191 
192 int NETINFO_MAPPING::Translate( int aNetCode ) const
193 {
194  std::map<int, int>::const_iterator value = m_netMapping.find( aNetCode );
195 
196  if( value != m_netMapping.end() )
197  return value->second;
198 
199  // There was no entry for the given net code
200  return aNetCode;
201 }
202 
203 
205 {
206  // Collect all the used nets
207  std::set<int> nets;
208 
209  // Be sure that the unconnected gets 0 and is mapped as 0
210  nets.insert( 0 );
211 
212  // Zones
213  for( ZONE* zone : m_board->Zones() )
214  nets.insert( zone->GetNetCode() );
215 
216  // Tracks
217  for( TRACK* track : m_board->Tracks() )
218  nets.insert( track->GetNetCode() );
219 
220  // footprints/pads
221  for( FOOTPRINT* footprint : m_board->Footprints() )
222  {
223  for( PAD* pad : footprint->Pads() )
224  nets.insert( pad->GetNetCode() );
225  }
226 
227  // Prepare the new mapping
228  m_netMapping.clear();
229 
230  // Now the nets variable stores all the used net codes (not only for pads) and we are ready to
231  // assign new consecutive net numbers
232  int newNetCode = 0;
233 
234  for( auto net : nets )
235  m_netMapping[net] = newNetCode++;
236 }
237 
238 
240 {
241  return m_mapping->m_board->FindNet( m_iterator->first );
242 }
243 
244 
246 {
247  return m_mapping->m_board->FindNet( m_iterator->first );
248 }
249 
250 
251 const int NETINFO_LIST::UNCONNECTED = 0;
252 const int NETINFO_LIST::ORPHANED = -1;
253 
NETINFO_ITEM * FindNet(int aNetcode) const
Search for a net with the given netcode.
Definition: board.cpp:1234
BOARD * m_Parent
Definition: netinfo.h:585
ZONES & Zones()
Definition: board.h:289
const BOARD * m_board
Board for which mapping is prepared
Definition: netinfo.h:397
NETNAMES_MAP m_netNames
map of <wxString, NETINFO_ITEM*>, is NETINFO_ITEM owner
Definition: netinfo.h:587
void RemoveUnusedNets()
int getFreeNetCode()
Function getFreeNetCode returns the first available net code that is not used by any other net.
std::map< int, int > m_netMapping
Map that allows saving net codes with consecutive numbers (for compatibility reasons)
Definition: netinfo.h:400
const NETINFO_MAPPING * m_mapping
Definition: netinfo.h:361
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:95
void SynchronizeNetsAndNetClasses()
Copy NETCLASS info to each NET, based on NET membership in a NETCLASS.
Definition: board.cpp:1379
void RemoveNet(NETINFO_ITEM *aNet)
Function RemoveNet Removes a new from the net list.
#define NULL
NETINFO_ITEM * operator->() const
FOOTPRINTS & Footprints()
Definition: board.h:283
int m_newNetCode
possible value for new net code assignment
Definition: netinfo.h:590
const wxString & GetNetname() const
Function GetNetname.
Definition: netinfo.h:231
ZONE handles a list of polygons defining a copper zone.
Definition: zone.h:57
int m_NetCode
A number equivalent to the net name.
Definition: netinfo.h:70
int SetAreasNetCodesFromNetNames()
Set the .m_NetCode member of all copper areas, according to the area Net Name The SetNetCodesFromNetN...
Definition: board.cpp:1412
static const int ORPHANED
Constant that forces initialization of a netinfo item to the NETINFO_ITEM ORPHANED (typically -1) whe...
Definition: netinfo.h:482
NETINFO_ITEM handles the data for a net.
Definition: netinfo.h:65
void Clear()
Function Clear sets all fields to their defaults values.
Definition: netinfo.h:271
std::map< int, int >::const_iterator m_iterator
Definition: netinfo.h:360
int GetNet() const
Function GetNet.
Definition: netinfo.h:223
Information pertinent to a Pcbnew printed circuit board.
Definition: board.h:186
NETINFO_ITEM * operator *() const
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)
NETINFO_LIST(BOARD *aParent)
Definition: pad.h:59
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:478
TRACKS & Tracks()
Definition: board.h:280
void buildListOfNets()
Function buildListOfNets builds or rebuilds the list of NETINFO_ITEMs The list is sorted by names.
Definition: track.h:83
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:588