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 <board_commit.h>
26#include <footprint.h>
27#include <macros.h>
28#include <pad.h>
29#include <pcb_shape.h>
30#include <pcb_track.h>
31#include <zone.h>
32#include <netinfo.h>
33#include <wx/log.h>
34
35
36// Constructor and destructor
38 m_parent( aParent ),
39 m_newNetCode( 0 )
40{
41 // Make sure that the unconnected net has number 0
42 AppendNet( new NETINFO_ITEM( aParent, wxEmptyString, 0 ) );
43}
44
45
47{
48 clear();
49}
50
51
53{
54 NETNAMES_MAP::iterator it, itEnd;
55
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
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 nullptr;
73}
74
75
76NETINFO_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 nullptr;
84}
85
86
88{
89 bool removed = false;
90
91 for( NETCODES_MAP::iterator i = m_netCodes.begin(); i != m_netCodes.end(); ++i )
92 {
93 if ( i->second == aNet )
94 {
95 removed = true;
96 m_netCodes.erase(i);
97 break;
98 }
99 }
100
101 for( NETNAMES_MAP::iterator i = m_netNames.begin(); i != m_netNames.end(); ++i )
102 {
103 if ( i->second == aNet )
104 {
105 wxASSERT_MSG( removed, wxT( "NETINFO_LIST::RemoveNet: target net found in m_netNames "
106 "but not m_netCodes!" ) );
107 m_netNames.erase(i);
108 break;
109 }
110 }
111
112 if( removed )
113 m_newNetCode = std::min( m_newNetCode, aNet->m_netCode - 1 );
114}
115
116
118{
119 NETCODES_MAP existingNets = m_netCodes;
120 std::vector<NETINFO_ITEM*> unusedNets;
121
122 m_netCodes.clear();
123 m_netNames.clear();
124
125 for( const auto& [ netCode, netInfo ] : existingNets )
126 {
127 if( netInfo->IsCurrent() )
128 {
129 m_netNames.insert( std::make_pair( netInfo->GetNetname(), netInfo ) );
130 m_netCodes.insert( std::make_pair( netCode, netInfo ) );
131 }
132 else if( aCommit )
133 {
134 aCommit->Removed( netInfo );
135 }
136 }
137}
138
139
141{
142 // if there is a net with such name then just assign the correct number
143 NETINFO_ITEM* sameName = GetNetItem( aNewElement->GetNetname() );
144
145 if( sameName != nullptr )
146 {
147 aNewElement->m_netCode = sameName->GetNetCode();
148
149 return;
150 }
151 else if( aNewElement->m_netCode != (int) m_netCodes.size() || aNewElement->m_netCode < 0 )
152 {
153 // be sure that net codes are consecutive
154 // negative net code means that it has to be auto assigned
155 aNewElement->m_netCode = getFreeNetCode();
156 }
157
158 // net names & codes are supposed to be unique
159 assert( GetNetItem( aNewElement->GetNetname() ) == nullptr );
160 assert( GetNetItem( aNewElement->GetNetCode() ) == nullptr );
161
162 // add an entry for fast look up by a net name using a map
163 m_netNames.insert( std::make_pair( aNewElement->GetNetname(), aNewElement ) );
164 m_netCodes.insert( std::make_pair( aNewElement->GetNetCode(), aNewElement ) );
165}
166
167
169{
170 // Restore the initial state of NETINFO_ITEMs
171 for( NETINFO_ITEM* net : *this )
172 net->Clear();
173
176}
177
178
179#if defined(DEBUG)
180void NETINFO_LIST::Show() const
181{
182 int i = 0;
183 NETNAMES_MAP::const_iterator it, itEnd;
184
185 for( it = m_netNames.begin(), itEnd = m_netNames.end(); it != itEnd; ++it )
186 {
187 wxLogDebug( wxT( "[%d]: netcode:%d netname:<%s>\n" ),
188 i++,
189 it->second->GetNetCode(),
190 TO_UTF8( it->second->GetNetname() ) );
191 }
192}
193#endif
194
195
197{
198 do
199 {
200 if( m_newNetCode < 0 )
201 m_newNetCode = 0;
202 } while( m_netCodes.count( ++m_newNetCode ) != 0 );
203
204 return m_newNetCode;
205}
206
207
208int NETINFO_MAPPING::Translate( int aNetCode ) const
209{
210 std::map<int, int>::const_iterator value = m_netMapping.find( aNetCode );
211
212 if( value != m_netMapping.end() )
213 return value->second;
214
215 // There was no entry for the given net code
216 return aNetCode;
217}
218
219
221{
222 // Collect all the used nets
223 std::set<int> nets;
224
225 // Be sure that the unconnected gets 0 and is mapped as 0
226 nets.insert( 0 );
227
228 // Zones
229 for( ZONE* zone : m_board->Zones() )
230 nets.insert( zone->GetNetCode() );
231
232 // Tracks
233 for( PCB_TRACK* track : m_board->Tracks() )
234 nets.insert( track->GetNetCode() );
235
236 for( BOARD_ITEM* item : m_board->Drawings() )
237 {
238 if( item->Type() != PCB_SHAPE_T )
239 continue;
240
241 PCB_SHAPE* shape = static_cast<PCB_SHAPE*>( item );
242
243 if( shape->GetNetCode() > 0 )
244 nets.insert( shape->GetNetCode() );
245 }
246
247 // footprints/pads
248 for( FOOTPRINT* footprint : m_board->Footprints() )
249 {
250 for( PAD* pad : footprint->Pads() )
251 nets.insert( pad->GetNetCode() );
252 }
253
254 // Prepare the new mapping
255 m_netMapping.clear();
256
257 // Now the nets variable stores all the used net codes (not only for pads) and we are ready to
258 // assign new consecutive net numbers
259 int newNetCode = 0;
260
261 for( auto net : nets )
262 m_netMapping[net] = newNetCode++;
263}
264
265
267{
268 return m_mapping->m_board->FindNet( m_iterator->first );
269}
270
271
273{
274 return m_mapping->m_board->FindNet( m_iterator->first );
275}
276
277
278const int NETINFO_LIST::UNCONNECTED = 0;
279const int NETINFO_LIST::ORPHANED = -1;
280
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:281
NETINFO_ITEM * FindNet(int aNetcode) const
Search for a net with the given netcode.
Definition: board.cpp:1810
const ZONES & Zones() const
Definition: board.h:326
int SetAreasNetCodesFromNetNames()
Set the .m_NetCode member of all copper areas, according to the area Net Name The SetNetCodesFromNetN...
Definition: board.cpp:1971
void SynchronizeNetsAndNetClasses(bool aResetTrackAndViaSizes)
Copy NETCLASS info to each NET, based on NET membership in a NETCLASS.
Definition: board.cpp:1943
const FOOTPRINTS & Footprints() const
Definition: board.h:322
const TRACKS & Tracks() const
Definition: board.h:320
const DRAWINGS & Drawings() const
Definition: board.h:324
COMMIT & Removed(EDA_ITEM *aItem, BASE_SCREEN *aScreen=nullptr)
Modify a given item in the model.
Definition: commit.h:98
Handle the data for a net.
Definition: netinfo.h:56
const wxString & GetNetname() const
Definition: netinfo.h:114
void Clear()
Set all fields to their default values.
int GetNetCode() const
Definition: netinfo.h:108
int m_netCode
A number equivalent to the net name.
Definition: netinfo.h:192
int getFreeNetCode()
Return the first available net code that is not used by any other net.
void RemoveUnusedNets(BOARD_COMMIT *aCommit)
static const int UNCONNECTED
Constant that holds the "unconnected net" number (typically 0) all items "connected" to this net are ...
Definition: netinfo.h:375
NETCODES_MAP m_netCodes
map of <int, NETINFO_ITEM*> is NOT owner
Definition: netinfo.h:496
static const int ORPHANED
Constant that forces initialization of a netinfo item to the NETINFO_ITEM ORPHANED (typically -1) whe...
Definition: netinfo.h:379
int m_newNetCode
possible value for new net code assignment
Definition: netinfo.h:498
void RemoveNet(NETINFO_ITEM *aNet)
Remove a net from the net list.
BOARD * m_parent
Definition: netinfo.h:493
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:495
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:285
std::map< int, int >::const_iterator m_iterator
Definition: netinfo.h:284
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:319
std::map< int, int > m_netMapping
Map that allows saving net codes with consecutive numbers (for compatibility reasons)
Definition: netinfo.h:320
Definition: pad.h:59
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:391
@ PCB_SHAPE_T
class PCB_SHAPE, a segment not on copper layers
Definition: typeinfo.h:88