KiCad PCB EDA Suite
Loading...
Searching...
No Matches
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-2023 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 <macros.h>
27#include <pad.h>
28#include <pcb_shape.h>
29#include <pcb_track.h>
30#include <zone.h>
31#include <netinfo.h>
32#include <wx/log.h>
33
34
35// Constructor and destructor
37 m_parent( aParent ),
38 m_newNetCode( 0 )
39{
40 // Make sure that the unconnected net has number 0
41 AppendNet( new NETINFO_ITEM( aParent, wxEmptyString, 0 ) );
42}
43
44
46{
47 clear();
48}
49
50
52{
53 NETNAMES_MAP::iterator it, itEnd;
54
55 for( it = m_netNames.begin(), itEnd = m_netNames.end(); it != itEnd; ++it )
56 delete it->second;
57
58 m_netNames.clear();
59 m_netCodes.clear();
60 m_newNetCode = 0;
61}
62
63
65{
66 NETCODES_MAP::const_iterator result = m_netCodes.find( aNetCode );
67
68 if( result != m_netCodes.end() )
69 return (*result).second;
70
71 return nullptr;
72}
73
74
75NETINFO_ITEM* NETINFO_LIST::GetNetItem( const wxString& aNetName ) const
76{
77 NETNAMES_MAP::const_iterator result = m_netNames.find( aNetName );
78
79 if( result != m_netNames.end() )
80 return (*result).second;
81
82 return nullptr;
83}
84
85
87{
88 bool removed = false;
89
90 for( NETCODES_MAP::iterator i = m_netCodes.begin(); i != m_netCodes.end(); ++i )
91 {
92 if ( i->second == aNet )
93 {
94 removed = true;
95 m_netCodes.erase(i);
96 break;
97 }
98 }
99
100 for( NETNAMES_MAP::iterator i = m_netNames.begin(); i != m_netNames.end(); ++i )
101 {
102 if ( i->second == aNet )
103 {
104 wxASSERT_MSG( removed, wxT( "NETINFO_LIST::RemoveNet: target net found in m_netNames "
105 "but not m_netCodes!" ) );
106 m_netNames.erase(i);
107 break;
108 }
109 }
110
111 if( removed )
112 m_newNetCode = std::min( m_newNetCode, aNet->m_netCode - 1 );
113}
114
115
117{
118 NETCODES_MAP existingNets = m_netCodes;
119
120 m_netCodes.clear();
121 m_netNames.clear();
122
123 for( std::pair<const int, NETINFO_ITEM*> item : existingNets )
124 {
125 if( item.second->IsCurrent() )
126 {
127 m_netNames.insert( std::make_pair( item.second->GetNetname(), item.second ) );
128 m_netCodes.insert( std::make_pair( item.first, item.second ) );
129 }
130 }
131}
132
133
135{
136 // if there is a net with such name then just assign the correct number
137 NETINFO_ITEM* sameName = GetNetItem( aNewElement->GetNetname() );
138
139 if( sameName != nullptr )
140 {
141 aNewElement->m_netCode = sameName->GetNetCode();
142
143 return;
144 }
145 else if( aNewElement->m_netCode != (int) m_netCodes.size() || aNewElement->m_netCode < 0 )
146 {
147 // be sure that net codes are consecutive
148 // negative net code means that it has to be auto assigned
149 aNewElement->m_netCode = getFreeNetCode();
150 }
151
152 // net names & codes are supposed to be unique
153 assert( GetNetItem( aNewElement->GetNetname() ) == nullptr );
154 assert( GetNetItem( aNewElement->GetNetCode() ) == nullptr );
155
156 // add an entry for fast look up by a net name using a map
157 m_netNames.insert( std::make_pair( aNewElement->GetNetname(), aNewElement ) );
158 m_netCodes.insert( std::make_pair( aNewElement->GetNetCode(), aNewElement ) );
159}
160
161
163{
164 // Restore the initial state of NETINFO_ITEMs
165 for( NETINFO_ITEM* net : *this )
166 net->Clear();
167
170}
171
172
173#if defined(DEBUG)
174void NETINFO_LIST::Show() const
175{
176 int i = 0;
177 NETNAMES_MAP::const_iterator it, itEnd;
178
179 for( it = m_netNames.begin(), itEnd = m_netNames.end(); it != itEnd; ++it )
180 {
181 wxLogDebug( wxT( "[%d]: netcode:%d netname:<%s>\n" ),
182 i++,
183 it->second->GetNetCode(),
184 TO_UTF8( it->second->GetNetname() ) );
185 }
186}
187#endif
188
189
191{
192 do
193 {
194 if( m_newNetCode < 0 )
195 m_newNetCode = 0;
196 } while( m_netCodes.count( ++m_newNetCode ) != 0 );
197
198 return m_newNetCode;
199}
200
201
202int NETINFO_MAPPING::Translate( int aNetCode ) const
203{
204 std::map<int, int>::const_iterator value = m_netMapping.find( aNetCode );
205
206 if( value != m_netMapping.end() )
207 return value->second;
208
209 // There was no entry for the given net code
210 return aNetCode;
211}
212
213
215{
216 // Collect all the used nets
217 std::set<int> nets;
218
219 // Be sure that the unconnected gets 0 and is mapped as 0
220 nets.insert( 0 );
221
222 // Zones
223 for( ZONE* zone : m_board->Zones() )
224 nets.insert( zone->GetNetCode() );
225
226 // Tracks
227 for( PCB_TRACK* track : m_board->Tracks() )
228 nets.insert( track->GetNetCode() );
229
230 for( BOARD_ITEM* item : m_board->Drawings() )
231 {
232 if( item->Type() != PCB_SHAPE_T )
233 continue;
234
235 PCB_SHAPE* shape = static_cast<PCB_SHAPE*>( item );
236
237 if( shape->GetNetCode() > 0 )
238 nets.insert( shape->GetNetCode() );
239 }
240
241 // footprints/pads
242 for( FOOTPRINT* footprint : m_board->Footprints() )
243 {
244 for( PAD* pad : footprint->Pads() )
245 nets.insert( pad->GetNetCode() );
246 }
247
248 // Prepare the new mapping
249 m_netMapping.clear();
250
251 // Now the nets variable stores all the used net codes (not only for pads) and we are ready to
252 // assign new consecutive net numbers
253 int newNetCode = 0;
254
255 for( auto net : nets )
256 m_netMapping[net] = newNetCode++;
257}
258
259
261{
262 return m_mapping->m_board->FindNet( m_iterator->first );
263}
264
265
267{
268 return m_mapping->m_board->FindNet( m_iterator->first );
269}
270
271
272const int NETINFO_LIST::UNCONNECTED = 0;
273const int NETINFO_LIST::ORPHANED = -1;
274
A base class for any item which can be embedded within the BOARD container class, and therefore insta...
Definition: board_item.h:77
Information pertinent to a Pcbnew printed circuit board.
Definition: board.h:271
ZONES & Zones()
Definition: board.h:319
NETINFO_ITEM * FindNet(int aNetcode) const
Search for a net with the given netcode.
Definition: board.cpp:1562
int SetAreasNetCodesFromNetNames()
Set the .m_NetCode member of all copper areas, according to the area Net Name The SetNetCodesFromNetN...
Definition: board.cpp:1658
void SynchronizeNetsAndNetClasses(bool aResetTrackAndViaSizes)
Copy NETCLASS info to each NET, based on NET membership in a NETCLASS.
Definition: board.cpp:1630
FOOTPRINTS & Footprints()
Definition: board.h:313
TRACKS & Tracks()
Definition: board.h:310
DRAWINGS & Drawings()
Definition: board.h:316
Handle the data for a net.
Definition: netinfo.h:67
const wxString & GetNetname() const
Definition: netinfo.h:125
void Clear()
Set all fields to their default values.
int GetNetCode() const
Definition: netinfo.h:119
int m_netCode
A number equivalent to the net name.
Definition: netinfo.h:188
int getFreeNetCode()
Return the first available net code that is not used by any other net.
static const int UNCONNECTED
Constant that holds the "unconnected net" number (typically 0) all items "connected" to this net are ...
Definition: netinfo.h:387
NETCODES_MAP m_netCodes
map of <int, NETINFO_ITEM*> is NOT owner
Definition: netinfo.h:495
void RemoveUnusedNets()
static const int ORPHANED
Constant that forces initialization of a netinfo item to the NETINFO_ITEM ORPHANED (typically -1) whe...
Definition: netinfo.h:391
int m_newNetCode
possible value for new net code assignment
Definition: netinfo.h:497
void RemoveNet(NETINFO_ITEM *aNet)
Remove a net from the net list.
BOARD * m_parent
Definition: netinfo.h:492
NETINFO_ITEM * GetNetItem(int aNetCode) const
NETINFO_LIST(BOARD *aParent)
void clear()
Delete the list of nets (and free memory).
NETNAMES_MAP m_netNames
map of <wxString, NETINFO_ITEM*>, is NETINFO_ITEM owner
Definition: netinfo.h:494
void AppendNet(NETINFO_ITEM *aNewElement)
Add aNewElement to the end of the net list.
void buildListOfNets()
Rebuild the list of NETINFO_ITEMs.
NETINFO_ITEM * operator->() const
const NETINFO_MAPPING * m_mapping
Definition: netinfo.h:281
std::map< int, int >::const_iterator m_iterator
Definition: netinfo.h:280
NETINFO_ITEM * operator*() const
int Translate(int aNetCode) const
Translate net number according to the map prepared by Update() function.
void Update()
Prepare a mapping for net codes so they can be saved as consecutive numbers.
const BOARD * m_board
Board for which mapping is prepared.
Definition: netinfo.h:315
std::map< int, int > m_netMapping
Map that allows saving net codes with consecutive numbers (for compatibility reasons)
Definition: netinfo.h:316
Definition: pad.h:58
Handle a list of polygons defining a copper zone.
Definition: zone.h:72
This file contains miscellaneous commonly used macros and functions.
#define TO_UTF8(wxstring)
Convert a wxString to a UTF8 encoded C string for all wxWidgets build modes.
Definition: string_utils.h:378
@ PCB_SHAPE_T
class PCB_SHAPE, a segment not on copper layers
Definition: typeinfo.h:88