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 {
114 m_newNetCode = std::min( m_newNetCode, aNet->m_netCode - 1 );
116 }
117}
118
119
121{
122 NETCODES_MAP existingNets = m_netCodes;
123 std::vector<NETINFO_ITEM*> unusedNets;
124
125 m_netCodes.clear();
126 m_netNames.clear();
127
128 for( const auto& [ netCode, netInfo ] : existingNets )
129 {
130 if( netInfo->IsCurrent() )
131 {
132 m_netNames.insert( std::make_pair( netInfo->GetNetname(), netInfo ) );
133 m_netCodes.insert( std::make_pair( netCode, netInfo ) );
134 }
135 else
136 {
138
139 if( aCommit )
140 aCommit->Removed( netInfo );
141 }
142 }
143}
144
145
147{
148 // if there is a net with such name then just assign the correct number
149 NETINFO_ITEM* sameName = GetNetItem( aNewElement->GetNetname() );
150
151 if( sameName != nullptr )
152 {
153 aNewElement->m_netCode = sameName->GetNetCode();
154
155 return;
156 }
157 else if( aNewElement->m_netCode != (int) m_netCodes.size() || aNewElement->m_netCode < 0 )
158 {
159 // be sure that net codes are consecutive
160 // negative net code means that it has to be auto assigned
161 aNewElement->m_netCode = getFreeNetCode();
162 }
163
164 // net names & codes are supposed to be unique
165 assert( GetNetItem( aNewElement->GetNetname() ) == nullptr );
166 assert( GetNetItem( aNewElement->GetNetCode() ) == nullptr );
167
168 // add an entry for fast look up by a net name using a map
169 m_netNames.insert( std::make_pair( aNewElement->GetNetname(), aNewElement ) );
170 m_netCodes.insert( std::make_pair( aNewElement->GetNetCode(), aNewElement ) );
171
173}
174
175
177{
178 // Restore the initial state of NETINFO_ITEMs
179 for( NETINFO_ITEM* net : *this )
180 net->Clear();
181
184}
185
186
188{
189 std::map<wxString, int> shortNames;
190
191 for( NETINFO_ITEM* net : *this )
192 shortNames[net->m_shortNetname]++;
193
194 for( NETINFO_ITEM* net : *this )
195 {
196 if( shortNames[net->m_shortNetname] == 1 )
198 else
200 }
201
203}
204
205
206#if defined(DEBUG)
207void NETINFO_LIST::Show() const
208{
209 int i = 0;
210 NETNAMES_MAP::const_iterator it, itEnd;
211
212 for( it = m_netNames.begin(), itEnd = m_netNames.end(); it != itEnd; ++it )
213 {
214 wxLogDebug( wxT( "[%d]: netcode:%d netname:<%s>\n" ),
215 i++,
216 it->second->GetNetCode(),
217 TO_UTF8( it->second->GetNetname() ) );
218 }
219}
220#endif
221
222
224{
225 do
226 {
227 if( m_newNetCode < 0 )
228 m_newNetCode = 0;
229 } while( m_netCodes.count( ++m_newNetCode ) != 0 );
230
231 return m_newNetCode;
232}
233
234
235int NETINFO_MAPPING::Translate( int aNetCode ) const
236{
237 std::map<int, int>::const_iterator value = m_netMapping.find( aNetCode );
238
239 if( value != m_netMapping.end() )
240 return value->second;
241
242 // There was no entry for the given net code
243 return aNetCode;
244}
245
246
248{
249 // Collect all the used nets
250 std::set<int> nets;
251
252 // Be sure that the unconnected gets 0 and is mapped as 0
253 nets.insert( 0 );
254
255 // Zones
256 for( ZONE* zone : m_board->Zones() )
257 nets.insert( zone->GetNetCode() );
258
259 // Tracks
260 for( PCB_TRACK* track : m_board->Tracks() )
261 nets.insert( track->GetNetCode() );
262
263 for( BOARD_ITEM* item : m_board->Drawings() )
264 {
265 if( item->Type() != PCB_SHAPE_T )
266 continue;
267
268 PCB_SHAPE* shape = static_cast<PCB_SHAPE*>( item );
269
270 if( shape->GetNetCode() > 0 )
271 nets.insert( shape->GetNetCode() );
272 }
273
274 // footprints/pads
275 for( FOOTPRINT* footprint : m_board->Footprints() )
276 {
277 for( PAD* pad : footprint->Pads() )
278 nets.insert( pad->GetNetCode() );
279 }
280
281 // Prepare the new mapping
282 m_netMapping.clear();
283
284 // Now the nets variable stores all the used net codes (not only for pads) and we are ready to
285 // assign new consecutive net numbers
286 int newNetCode = 0;
287
288 for( auto net : nets )
289 m_netMapping[net] = newNetCode++;
290}
291
292
294{
295 return m_mapping->m_board->FindNet( m_iterator->first );
296}
297
298
300{
301 return m_mapping->m_board->FindNet( m_iterator->first );
302}
303
304
305const int NETINFO_LIST::UNCONNECTED = 0;
306const int NETINFO_LIST::ORPHANED = -1;
307
A base class for any item which can be embedded within the BOARD container class, and therefore insta...
Definition: board_item.h:79
Information pertinent to a Pcbnew printed circuit board.
Definition: board.h:289
NETINFO_ITEM * FindNet(int aNetcode) const
Search for a net with the given netcode.
Definition: board.cpp:1900
const ZONES & Zones() const
Definition: board.h:334
int SetAreasNetCodesFromNetNames()
Set the .m_NetCode member of all copper areas, according to the area Net Name The SetNetCodesFromNetN...
Definition: board.cpp:2061
void SynchronizeNetsAndNetClasses(bool aResetTrackAndViaSizes)
Copy NETCLASS info to each NET, based on NET membership in a NETCLASS.
Definition: board.cpp:2033
const FOOTPRINTS & Footprints() const
Definition: board.h:330
const TRACKS & Tracks() const
Definition: board.h:328
const DRAWINGS & Drawings() const
Definition: board.h:332
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
wxString m_shortNetname
Short net name, like vout from /sheet/subsheet/vout.
Definition: netinfo.h:194
wxString m_displayNetname
Unescaped netname for display.
Definition: netinfo.h:196
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
wxString m_netname
Full net name like /sheet/subsheet/vout used by Eeschema.
Definition: netinfo.h:193
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:381
NETCODES_MAP m_netCodes
map of <int, NETINFO_ITEM*> is NOT owner
Definition: netinfo.h:506
static const int ORPHANED
Constant that forces initialization of a netinfo item to the NETINFO_ITEM ORPHANED (typically -1) whe...
Definition: netinfo.h:385
int m_newNetCode
possible value for new net code assignment
Definition: netinfo.h:508
void RemoveNet(NETINFO_ITEM *aNet)
Remove a net from the net list.
BOARD * m_parent
Definition: netinfo.h:503
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:505
void AppendNet(NETINFO_ITEM *aNewElement)
Add aNewElement to the end of the net list.
bool m_DisplayNetnamesDirty
Definition: netinfo.h:500
void RebuildDisplayNetnames() const
void buildListOfNets()
Rebuild the list of NETINFO_ITEMs.
NETINFO_ITEM * operator->() const
const NETINFO_MAPPING * m_mapping
Definition: netinfo.h:289
std::map< int, int >::const_iterator m_iterator
Definition: netinfo.h:288
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:323
std::map< int, int > m_netMapping
Map that allows saving net codes with consecutive numbers (for compatibility reasons)
Definition: netinfo.h:324
Definition: pad.h:54
Handle a list of polygons defining a copper zone.
Definition: zone.h:73
This file contains miscellaneous commonly used macros and functions.
wxString UnescapeString(const wxString &aSource)
#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