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++, it->second->GetNet(),
176  TO_UTF8( it->second->GetNetname() ) );
177  }
178 }
179 #endif
180 
181 
183 {
184  do {
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( int i = 0; i < m_board->GetAreaCount(); ++i )
215  nets.insert( m_board->GetArea( i )->GetNetCode() );
216 
217  // Tracks
218  for( TRACK* track = m_board->m_Track; track; track = track->Next() )
219  nets.insert( track->GetNetCode() );
220 
221  // Modules/pads
222  for( MODULE* module = m_board->m_Modules; module; module = module->Next() )
223  {
224  for( D_PAD* pad = module->PadsList().GetFirst(); pad; pad = pad->Next() )
225  {
226  nets.insert( pad->GetNetCode() );
227  }
228  }
229 
230  // Segzones (should be removed: used only in very old boards)
231  for( SEGZONE* zone = m_board->m_SegZoneDeprecated; zone; zone = zone->Next() )
232  nets.insert( zone->GetNetCode() );
233 
234  // Prepare the new mapping
235  m_netMapping.clear();
236 
237  // Now the nets variable stores all the used net codes (not only for pads) and we are ready to
238  // assign new consecutive net numbers
239  int newNetCode = 0;
240  for( std::set<int>::const_iterator it = nets.begin(), itEnd = nets.end(); it != itEnd; ++it )
241  m_netMapping[*it] = newNetCode++;
242 }
243 
244 
246 {
247  return m_mapping->m_board->FindNet( m_iterator->first );
248 }
249 
250 
252 {
253  return m_mapping->m_board->FindNet( m_iterator->first );
254 }
255 
256 
257 const int NETINFO_LIST::UNCONNECTED = 0;
258 const int NETINFO_LIST::ORPHANED = -1;
259 
Wrapper class, so you can iterate through NETINFO_ITEM*s, not std::pair<int/wxString, NETINFO_ITEM*>
Definition: netinfo.h:486
BOARD * m_Parent
Definition: netinfo.h:578
NETINFO_ITEM * operator->() const
Class BOARD to handle a board.
MODULE * Next() const
Definition: class_module.h:123
NETINFO_ITEM * GetNetItem(int aNetCode) const
Function GetItem.
Classes to handle copper zones.
NETNAMES_MAP m_netNames
map of <wxString, NETINFO_ITEM*>, is NETINFO_ITEM owner
Definition: netinfo.h:580
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.
This file contains miscellaneous commonly used macros and functions.
SEGZONE * Next() const
Definition: class_track.h:363
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:160
void RemoveNet(NETINFO_ITEM *aNet)
Function RemoveNet Removes a new from the net list.
int Translate(int aNetCode) const
Function Translate Translates net number according to the map prepared by Update() function...
int m_newNetCode
possible value for new net code assignment
Definition: netinfo.h:583
D_PAD * Next() const
Definition: class_pad.h:160
int m_NetCode
A number equivalent to the net name.
Definition: netinfo.h:74
int SetAreasNetCodesFromNetNames(void)
Function SetAreasNetCodesFromNetNames Set the .m_NetCode member of all copper areas, according to the area Net Name The SetNetCodesFromNetNames is an equivalent to net name, for fast comparisons.
int GetNet() const
Function GetNet.
Definition: netinfo.h:231
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:473
Class NETINFO_ITEM handles the data for a net.
Definition: netinfo.h:69
iterator begin() const
Definition: netinfo.h:532
TRACK * Next() const
Definition: class_track.h:99
iterator end() const
Definition: netinfo.h:537
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:477
Class BOARD holds information pertinent to a Pcbnew printed circuit board.
Definition: class_board.h:170
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)
Module description (excepted pads)
const wxString & GetNetname() const
Function GetNetname.
Definition: netinfo.h:239
NETINFO_LIST(BOARD *aParent)
static const int UNCONNECTED
Constant that holds the "unconnected net" number (typically 0) all items "connected" to this net are ...
Definition: netinfo.h:469
void buildListOfNets()
Function buildListOfNets builds or rebuilds the list of NETINFO_ITEMs The list is sorted by names...
#define min(a, b)
Definition: auxiliary.h:85
NETCODES_MAP m_netCodes
map of <int, NETINFO_ITEM*> is NOT owner
Definition: netinfo.h:581