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 
28 #include <fctsys.h>
29 #include <gr_basic.h>
30 #include <common.h>
31 #include <class_drawpanel.h>
32 #include <macros.h>
33 #include <pcbnew.h>
34 
35 #include <class_board.h>
36 #include <class_module.h>
37 #include <class_pad.h>
38 #include <class_track.h>
39 #include <class_zone.h>
40 #include <netinfo.h>
41 
42 
43 // Constructor and destructor
44 NETINFO_LIST::NETINFO_LIST( BOARD* aParent ) : m_Parent( aParent )
45 {
46  // Make sure that the unconnected net has number 0
47  AppendNet( new NETINFO_ITEM( aParent, wxEmptyString, 0 ) );
48 
49  m_newNetCode = 0;
50 }
51 
52 
54 {
55  clear();
56 }
57 
58 
60 {
61  NETNAMES_MAP::iterator it, itEnd;
62  for( it = m_netNames.begin(), itEnd = m_netNames.end(); it != itEnd; ++it )
63  delete it->second;
64 
65  m_netNames.clear();
66  m_netCodes.clear();
67  m_newNetCode = 0;
68 }
69 
70 
71 NETINFO_ITEM* NETINFO_LIST::GetNetItem( int aNetCode ) const
72 {
73  NETCODES_MAP::const_iterator result = m_netCodes.find( aNetCode );
74 
75  if( result != m_netCodes.end() )
76  return (*result).second;
77 
78  return NULL;
79 }
80 
81 
82 NETINFO_ITEM* NETINFO_LIST::GetNetItem( const wxString& aNetName ) const
83 {
84  NETNAMES_MAP::const_iterator result = m_netNames.find( aNetName );
85 
86  if( result != m_netNames.end() )
87  return (*result).second;
88 
89  return NULL;
90 }
91 
92 
94 {
95  for( NETCODES_MAP::iterator i = m_netCodes.begin(); i != m_netCodes.end(); ++i )
96  {
97  if ( i->second == aNet )
98  {
99  m_netCodes.erase(i);
100  break;
101  }
102  }
103 
104  for( NETNAMES_MAP::iterator i = m_netNames.begin(); i != m_netNames.end(); ++i )
105  {
106  if ( i->second == aNet )
107  {
108  m_netNames.erase(i);
109  break;
110  }
111  }
112 
114 }
115 
116 
118 {
119  // if there is a net with such name then just assign the correct number
120  NETINFO_ITEM* sameName = GetNetItem( aNewElement->GetNetname() );
121 
122  if( sameName != NULL )
123  {
124  aNewElement->m_NetCode = sameName->GetNet();
125 
126  return;
127  }
128  // be sure that net codes are consecutive
129  // negative net code means that it has to be auto assigned
130  else if( ( aNewElement->m_NetCode != (int) m_netCodes.size() ) || ( aNewElement->m_NetCode < 0 ) )
131  {
132  aNewElement->m_NetCode = getFreeNetCode();
133  }
134 
135  // net names & codes are supposed to be unique
136  assert( GetNetItem( aNewElement->GetNetname() ) == NULL );
137  assert( GetNetItem( aNewElement->GetNet() ) == NULL );
138 
139  // add an entry for fast look up by a net name using a map
140  m_netNames.insert( std::make_pair( aNewElement->GetNetname(), aNewElement ) );
141  m_netCodes.insert( std::make_pair( aNewElement->GetNet(), aNewElement ) );
142 }
143 
144 
158 {
159  // Restore the initial state of NETINFO_ITEMs
160  for( NETINFO_LIST::iterator net( begin() ), netEnd( end() ); net != netEnd; ++net )
161  net->Clear();
162 
165 }
166 
167 #if defined(DEBUG)
168 void NETINFO_LIST::Show() const
169 {
170  int i = 0;
171  NETNAMES_MAP::const_iterator it, itEnd;
172  for( it = m_netNames.begin(), itEnd = m_netNames.end(); it != itEnd; ++it )
173  {
174  printf( "[%d]: netcode:%d netname:<%s>\n",
175  i++,
176  it->second->GetNet(),
177  TO_UTF8( it->second->GetNetname() ) );
178  }
179 }
180 #endif
181 
182 
184 {
185  do {
186  if( m_newNetCode < 0 )
187  m_newNetCode = 0;
188  } while( m_netCodes.count( ++m_newNetCode ) != 0 );
189 
190  return m_newNetCode;
191 }
192 
193 
194 int NETINFO_MAPPING::Translate( int aNetCode ) const
195 {
196  std::map<int, int>::const_iterator value = m_netMapping.find( aNetCode );
197 
198  if( value != m_netMapping.end() )
199  return value->second;
200 
201  // There was no entry for the given net code
202  return aNetCode;
203 }
204 
205 
207 {
208  // Collect all the used nets
209  std::set<int> nets;
210 
211  // Be sure that the unconnected gets 0 and is mapped as 0
212  nets.insert( 0 );
213 
214  // Zones
215  for( int i = 0; i < m_board->GetAreaCount(); ++i )
216  nets.insert( m_board->GetArea( i )->GetNetCode() );
217 
218  // Tracks
219  for( TRACK* track = m_board->m_Track; track; track = track->Next() )
220  nets.insert( track->GetNetCode() );
221 
222  // Modules/pads
223  for( MODULE* module = m_board->m_Modules; module; module = module->Next() )
224  {
225  for( D_PAD* pad = module->PadsList().GetFirst(); pad; pad = pad->Next() )
226  {
227  nets.insert( pad->GetNetCode() );
228  }
229  }
230 
231  // Prepare the new mapping
232  m_netMapping.clear();
233 
234  // Now the nets variable stores all the used net codes (not only for pads) and we are ready to
235  // assign new consecutive net numbers
236  int newNetCode = 0;
237  for( std::set<int>::const_iterator it = nets.begin(), itEnd = nets.end(); it != itEnd; ++it )
238  m_netMapping[*it] = newNetCode++;
239 }
240 
241 
243 {
244  return m_mapping->m_board->FindNet( m_iterator->first );
245 }
246 
247 
249 {
250  return m_mapping->m_board->FindNet( m_iterator->first );
251 }
252 
253 
254 const int NETINFO_LIST::UNCONNECTED = 0;
255 const int NETINFO_LIST::ORPHANED = -1;
256 
D_PAD * Next() const
Definition: class_pad.h:162
iterator end() const
Definition: netinfo.h:533
Wrapper class, so you can iterate through NETINFO_ITEM*s, not std::pair<int/wxString,...
Definition: netinfo.h:482
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:574
const BOARD * m_board
Board for which mapping is prepared
Definition: netinfo.h:386
Class BOARD to handle a board.
Classes to handle copper zones.
NETNAMES_MAP m_netNames
map of <wxString, NETINFO_ITEM*>, is NETINFO_ITEM owner
Definition: netinfo.h:576
int getFreeNetCode()
Function getFreeNetCode returns the first available net code that is not used by any other net.
Functions relatives to tracks, vias and segments used to fill zones.
std::map< int, int > m_netMapping
Map that allows saving net codes with consecutive numbers (for compatibility reasons)
Definition: netinfo.h:389
const NETINFO_MAPPING * m_mapping
Definition: netinfo.h:350
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:47
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:981
NETINFO_ITEM * operator->() const
iterator begin() const
Definition: netinfo.h:528
int m_newNetCode
possible value for new net code assignment
Definition: netinfo.h:579
const wxString & GetNetname() const
Function GetNetname.
Definition: netinfo.h:235
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:469
Class NETINFO_ITEM handles the data for a net.
Definition: netinfo.h:65
std::map< int, int >::const_iterator m_iterator
Definition: netinfo.h:349
static NETINFO_ITEM ORPHANED_ITEM
NETINFO_ITEM meaning that there was no net assigned for an item, as there was no board storing net li...
Definition: netinfo.h:473
int GetNet() const
Function GetNet.
Definition: netinfo.h:227
Class BOARD holds information pertinent to a Pcbnew printed circuit board.
Definition: class_board.h:170
DLIST< MODULE > m_Modules
Definition: class_board.h:248
size_t i
Definition: json11.cpp:597
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)
DLIST< TRACK > m_Track
Definition: class_board.h:249
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:952
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:465
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.
#define min(a, b)
Definition: auxiliary.h:85
NETCODES_MAP m_netCodes
map of <int, NETINFO_ITEM*> is NOT owner
Definition: netinfo.h:577