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
34  m_parent( aParent )
35 {
36  // Make sure that the unconnected net has number 0
37  AppendNet( new NETINFO_ITEM( aParent, wxEmptyString, 0 ) );
38 
39  m_newNetCode = 0;
40 }
41 
42 
44 {
45  clear();
46 }
47 
48 
50 {
51  NETNAMES_MAP::iterator it, itEnd;
52  for( it = m_netNames.begin(), itEnd = m_netNames.end(); it != itEnd; ++it )
53  delete it->second;
54 
55  m_netNames.clear();
56  m_netCodes.clear();
57  m_newNetCode = 0;
58 }
59 
60 
61 NETINFO_ITEM* NETINFO_LIST::GetNetItem( int aNetCode ) const
62 {
63  NETCODES_MAP::const_iterator result = m_netCodes.find( aNetCode );
64 
65  if( result != m_netCodes.end() )
66  return (*result).second;
67 
68  return NULL;
69 }
70 
71 
72 NETINFO_ITEM* NETINFO_LIST::GetNetItem( const wxString& aNetName ) const
73 {
74  NETNAMES_MAP::const_iterator result = m_netNames.find( aNetName );
75 
76  if( result != m_netNames.end() )
77  return (*result).second;
78 
79  return NULL;
80 }
81 
82 
84 {
85  for( NETCODES_MAP::iterator i = m_netCodes.begin(); i != m_netCodes.end(); ++i )
86  {
87  if ( i->second == aNet )
88  {
89  m_netCodes.erase(i);
90  break;
91  }
92  }
93 
94  for( NETNAMES_MAP::iterator i = m_netNames.begin(); i != m_netNames.end(); ++i )
95  {
96  if ( i->second == aNet )
97  {
98  m_netNames.erase(i);
99  break;
100  }
101  }
102 
103  m_newNetCode = std::min( m_newNetCode, aNet->m_netCode - 1 );
104 }
105 
106 
108 {
109  NETCODES_MAP existingNets = m_netCodes;
110 
111  m_netCodes.clear();
112  m_netNames.clear();
113 
114  for( std::pair<const int, NETINFO_ITEM*> item : existingNets )
115  {
116  if( item.second->IsCurrent() )
117  {
118  m_netNames.insert( std::make_pair( item.second->GetNetname(), item.second ) );
119  m_netCodes.insert( std::make_pair( item.first, item.second ) );
120  }
121  }
122 }
123 
124 
126 {
127  // if there is a net with such name then just assign the correct number
128  NETINFO_ITEM* sameName = GetNetItem( aNewElement->GetNetname() );
129 
130  if( sameName != NULL )
131  {
132  aNewElement->m_netCode = sameName->GetNetCode();
133 
134  return;
135  }
136  // be sure that net codes are consecutive
137  // negative net code means that it has to be auto assigned
138  else if( aNewElement->m_netCode != (int) m_netCodes.size() || aNewElement->m_netCode < 0 )
139  {
140  aNewElement->m_netCode = getFreeNetCode();
141  }
142 
143  // net names & codes are supposed to be unique
144  assert( GetNetItem( aNewElement->GetNetname() ) == NULL );
145  assert( GetNetItem( aNewElement->GetNetCode() ) == NULL );
146 
147  // add an entry for fast look up by a net name using a map
148  m_netNames.insert( std::make_pair( aNewElement->GetNetname(), aNewElement ) );
149  m_netCodes.insert( std::make_pair( aNewElement->GetNetCode(), aNewElement ) );
150 }
151 
152 
154 {
155  // Restore the initial state of NETINFO_ITEMs
156  for( NETINFO_ITEM* net : *this )
157  net->Clear();
158 
161 }
162 
163 
164 #if defined(DEBUG)
165 void NETINFO_LIST::Show() const
166 {
167  int i = 0;
168  NETNAMES_MAP::const_iterator it, itEnd;
169 
170  for( it = m_netNames.begin(), itEnd = m_netNames.end(); it != itEnd; ++it )
171  {
172  wxLogDebug( "[%d]: netcode:%d netname:<%s>\n",
173  i++,
174  it->second->GetNetCode(),
175  TO_UTF8( it->second->GetNetname() ) );
176  }
177 }
178 #endif
179 
180 
182 {
183  do
184  {
185  if( m_newNetCode < 0 )
186  m_newNetCode = 0;
187  } while( m_netCodes.count( ++m_newNetCode ) != 0 );
188 
189  return m_newNetCode;
190 }
191 
192 
193 int NETINFO_MAPPING::Translate( int aNetCode ) const
194 {
195  std::map<int, int>::const_iterator value = m_netMapping.find( aNetCode );
196 
197  if( value != m_netMapping.end() )
198  return value->second;
199 
200  // There was no entry for the given net code
201  return aNetCode;
202 }
203 
204 
206 {
207  // Collect all the used nets
208  std::set<int> nets;
209 
210  // Be sure that the unconnected gets 0 and is mapped as 0
211  nets.insert( 0 );
212 
213  // Zones
214  for( ZONE* zone : m_board->Zones() )
215  nets.insert( zone->GetNetCode() );
216 
217  // Tracks
218  for( TRACK* track : m_board->Tracks() )
219  nets.insert( track->GetNetCode() );
220 
221  // footprints/pads
222  for( FOOTPRINT* footprint : m_board->Footprints() )
223  {
224  for( PAD* pad : footprint->Pads() )
225  nets.insert( pad->GetNetCode() );
226  }
227 
228  // Prepare the new mapping
229  m_netMapping.clear();
230 
231  // Now the nets variable stores all the used net codes (not only for pads) and we are ready to
232  // assign new consecutive net numbers
233  int newNetCode = 0;
234 
235  for( auto net : nets )
236  m_netMapping[net] = newNetCode++;
237 }
238 
239 
241 {
242  return m_mapping->m_board->FindNet( m_iterator->first );
243 }
244 
245 
247 {
248  return m_mapping->m_board->FindNet( m_iterator->first );
249 }
250 
251 
252 const int NETINFO_LIST::UNCONNECTED = 0;
253 const int NETINFO_LIST::ORPHANED = -1;
254 
NETINFO_ITEM * FindNet(int aNetcode) const
Search for a net with the given netcode.
Definition: board.cpp:1268
ZONES & Zones()
Definition: board.h:309
const BOARD * m_board
Board for which mapping is prepared.
Definition: netinfo.h:293
NETNAMES_MAP m_netNames
map of <wxString, NETINFO_ITEM*>, is NETINFO_ITEM owner
Definition: netinfo.h:472
void RemoveUnusedNets()
int getFreeNetCode()
Return 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:294
const NETINFO_MAPPING * m_mapping
Definition: netinfo.h:259
BOARD * m_parent
Definition: netinfo.h:470
void AppendNet(NETINFO_ITEM *aNewElement)
Add aNewElement to the end of the net list.
#define TO_UTF8(wxstring)
Convert a wxString to a UTF8 encoded C string for all wxWidgets build modes.
Definition: macros.h:96
void SynchronizeNetsAndNetClasses()
Copy NETCLASS info to each NET, based on NET membership in a NETCLASS.
Definition: board.cpp:1413
void RemoveNet(NETINFO_ITEM *aNet)
Remove a net from the net list.
#define NULL
NETINFO_ITEM * operator->() const
FOOTPRINTS & Footprints()
Definition: board.h:303
int m_netCode
A number equivalent to the net name.
Definition: netinfo.h:167
int m_newNetCode
possible value for new net code assignment
Definition: netinfo.h:475
const wxString & GetNetname() const
Definition: netinfo.h:119
ZONE handles a list of polygons defining a copper zone.
Definition: zone.h:57
int SetAreasNetCodesFromNetNames()
Set the .m_NetCode member of all copper areas, according to the area Net Name The SetNetCodesFromNetN...
Definition: board.cpp:1446
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
Handle the data for a net.
Definition: netinfo.h:64
void Clear()
Set all fields to their default values.
Definition: netinfo.h:154
std::map< int, int >::const_iterator m_iterator
Definition: netinfo.h:258
Information pertinent to a Pcbnew printed circuit board.
Definition: board.h:190
NETINFO_ITEM * operator *() const
void Update()
Prepare a mapping for net codes so they can be saved as consecutive numbers.
void clear()
Delete the list of nets (and free memory).
NETINFO_LIST(BOARD *aParent)
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
TRACKS & Tracks()
Definition: board.h:300
void buildListOfNets()
Rebuild the list of NETINFO_ITEMs.
Definition: track.h:83
int Translate(int aNetCode) const
Translate 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:473
int GetNetCode() const
Definition: netinfo.h:113