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  NETCODES_MAP existingNets = m_netCodes;
114 
115  m_netCodes.clear();
116  m_netNames.clear();
117 
118  for( std::pair<const int, NETINFO_ITEM*> item : existingNets )
119  {
120  if( item.second->IsCurrent() )
121  {
122  m_netNames.insert( std::make_pair( item.second->GetNetname(), item.second ) );
123  m_netCodes.insert( std::make_pair( item.first, item.second ) );
124  }
125  }
126 }
127 
128 
130 {
131  // if there is a net with such name then just assign the correct number
132  NETINFO_ITEM* sameName = GetNetItem( aNewElement->GetNetname() );
133 
134  if( sameName != NULL )
135  {
136  aNewElement->m_NetCode = sameName->GetNet();
137 
138  return;
139  }
140  // be sure that net codes are consecutive
141  // negative net code means that it has to be auto assigned
142  else if( ( aNewElement->m_NetCode != (int) m_netCodes.size() ) || ( aNewElement->m_NetCode < 0 ) )
143  {
144  aNewElement->m_NetCode = getFreeNetCode();
145  }
146 
147  // net names & codes are supposed to be unique
148  assert( GetNetItem( aNewElement->GetNetname() ) == NULL );
149  assert( GetNetItem( aNewElement->GetNet() ) == NULL );
150 
151  // add an entry for fast look up by a net name using a map
152  m_netNames.insert( std::make_pair( aNewElement->GetNetname(), aNewElement ) );
153  m_netCodes.insert( std::make_pair( aNewElement->GetNet(), aNewElement ) );
154 }
155 
156 
158 {
159  // Restore the initial state of NETINFO_ITEMs
160  for( NETINFO_ITEM* net : *this )
161  net->Clear();
162 
165 }
166 
167 
168 #if defined(DEBUG)
169 void NETINFO_LIST::Show() const
170 {
171  int i = 0;
172  NETNAMES_MAP::const_iterator it, itEnd;
173  for( it = m_netNames.begin(), itEnd = m_netNames.end(); it != itEnd; ++it )
174  {
175  printf( "[%d]: netcode:%d netname:<%s>\n",
176  i++,
177  it->second->GetNet(),
178  TO_UTF8( it->second->GetNetname() ) );
179  }
180 }
181 #endif
182 
183 
185 {
186  do {
187  if( m_newNetCode < 0 )
188  m_newNetCode = 0;
189  } while( m_netCodes.count( ++m_newNetCode ) != 0 );
190 
191  return m_newNetCode;
192 }
193 
194 
195 int NETINFO_MAPPING::Translate( int aNetCode ) const
196 {
197  std::map<int, int>::const_iterator value = m_netMapping.find( aNetCode );
198 
199  if( value != m_netMapping.end() )
200  return value->second;
201 
202  // There was no entry for the given net code
203  return aNetCode;
204 }
205 
206 
208 {
209  // Collect all the used nets
210  std::set<int> nets;
211 
212  // Be sure that the unconnected gets 0 and is mapped as 0
213  nets.insert( 0 );
214 
215  // Zones
216  for( int i = 0; i < m_board->GetAreaCount(); ++i )
217  nets.insert( m_board->GetArea( i )->GetNetCode() );
218 
219  // Tracks
220  for( auto track : m_board->Tracks() )
221  nets.insert( track->GetNetCode() );
222 
223  // Modules/pads
224  for( auto module : m_board->Modules() )
225  {
226  for( auto pad : module->Pads() )
227  {
228  nets.insert( pad->GetNetCode() );
229  }
230  }
231 
232  // Prepare the new mapping
233  m_netMapping.clear();
234 
235  // Now the nets variable stores all the used net codes (not only for pads) and we are ready to
236  // assign new consecutive net numbers
237  int newNetCode = 0;
238  for( std::set<int>::const_iterator it = nets.begin(), itEnd = nets.end(); it != itEnd; ++it )
239  m_netMapping[*it] = newNetCode++;
240 }
241 
242 
244 {
245  return m_mapping->m_board->FindNet( m_iterator->first );
246 }
247 
248 
250 {
251  return m_mapping->m_board->FindNet( m_iterator->first );
252 }
253 
254 
255 const int NETINFO_LIST::UNCONNECTED = 0;
256 const int NETINFO_LIST::ORPHANED = -1;
257 
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:585
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.
A single base class (TRACK) represents both tracks and vias, with subclasses for curved tracks (ARC) ...
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
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.
void SynchronizeNetsAndNetClasses()
Function SynchronizeNetsAndNetClasses copies NETCLASS info to each NET, based on NET membership in a ...
void RemoveNet(NETINFO_ITEM *aNet)
Function RemoveNet Removes a new from the net list.
int GetAreaCount() const
Function GetAreaCount.
Definition: class_board.h:934
#define NULL
MODULES & Modules()
Definition: class_board.h:266
NETINFO_ITEM * operator->() const
int m_newNetCode
possible value for new net code assignment
Definition: netinfo.h:590
const wxString & GetNetname() const
Function GetNetname.
Definition: netinfo.h:231
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: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
BOARD holds information pertinent to a Pcbnew printed circuit board.
Definition: class_board.h:180
NETINFO_ITEM * operator *() const
#define TO_UTF8(wxstring)
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)
ZONE_CONTAINER * GetArea(int index) const
Function GetArea returns the Area (Zone Container) at a given index.
Definition: class_board.h:899
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:478
TRACKS & Tracks()
Definition: class_board.h:257
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:588