KiCad PCB EDA Suite
class_netclass.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) 2009 SoftPLC Corporation, Dick Hollenbeck <dick@softplc.com>
5  * Copyright (C) 2009 Jean-Pierre Charras, jean-pierre.charras@inpg.fr
6  * Copyright (C) 2009 KiCad Developers, see change_log.txt for contributors.
7  *
8  * This program is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU General Public License
10  * as published by the Free Software Foundation; either version 2
11  * of the License, or (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, you may find one here:
20  * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
21  * or you may search the http://www.gnu.org website for the version 2 license,
22  * or you may write to the Free Software Foundation, Inc.,
23  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
24  */
25 
26 #include <fctsys.h>
27 #include <common.h>
28 #include <kicad_string.h>
29 #include <pcbnew.h>
30 #include <richio.h>
31 #include <macros.h>
32 
33 #include <class_board.h>
34 #include <class_netclass.h>
35 
36 
37 // This will get mapped to "kicad_default" in the specctra_export.
38 const char NETCLASS::Default[] = "Default";
39 
40 // Initial values for netclass initialization
41 const int NETCLASS::DEFAULT_CLEARANCE = Millimeter2iu( 0.2 ); // track to track and track to pads clearance
42 const int NETCLASS::DEFAULT_VIA_DIAMETER = Millimeter2iu( 0.8 );
43 const int NETCLASS::DEFAULT_VIA_DRILL = Millimeter2iu( 0.4 );
44 const int NETCLASS::DEFAULT_UVIA_DIAMETER = Millimeter2iu( 0.3 );
45 const int NETCLASS::DEFAULT_UVIA_DRILL = Millimeter2iu( 0.1 );
46 const int NETCLASS::DEFAULT_TRACK_WIDTH = Millimeter2iu( 0.25 );
47 const int NETCLASS::DEFAULT_DIFF_PAIR_WIDTH = Millimeter2iu( 0.2 );
48 const int NETCLASS::DEFAULT_DIFF_PAIR_GAP = Millimeter2iu( 0.25 );
49 
50 
51 NETCLASS::NETCLASS( const wxString& aName ) :
52  m_Name( aName )
53 {
54  // Default settings
58  // These defaults will be overwritten by SetParams,
59  // from the board design parameters, later
65 }
66 
67 
68 void NETCLASS::SetParams( const NETCLASS& aDefaults )
69 {
70  SetClearance( aDefaults.GetClearance() );
71  SetTrackWidth( aDefaults.GetTrackWidth() );
72  SetViaDiameter( aDefaults.GetViaDiameter() );
73  SetViaDrill( aDefaults.GetViaDrill() );
74  SetuViaDiameter( aDefaults.GetuViaDiameter() );
75  SetuViaDrill( aDefaults.GetuViaDrill() );
76  SetDiffPairWidth( aDefaults.GetDiffPairWidth() );
77  SetDiffPairGap( aDefaults.GetDiffPairGap() );
78 
79 }
80 
81 
83 {
84 }
85 
86 
88 {
89  m_default = std::make_shared<NETCLASS>( NETCLASS::Default );
90 }
91 
92 
94 {
95 }
96 
97 
98 bool NETCLASSES::Add( NETCLASSPTR aNetClass )
99 {
100  const wxString& name = aNetClass->GetName();
101 
102  if( name == NETCLASS::Default )
103  {
104  // invoke operator=(), which is currently generated by compiler.
105  GetDefault() = aNetClass;
106 
107  return true;
108  }
109 
110  // Test for an existing netclass:
111  if( !Find( name ) )
112  {
113  // name not found, take ownership
114  m_NetClasses[name] = aNetClass;
115 
116  return true;
117  }
118  else
119  {
120  // name already exists
121  // do not "take ownership" and return false telling caller such.
122  return false;
123  }
124 }
125 
126 
127 NETCLASSPTR NETCLASSES::Remove( const wxString& aNetName )
128 {
129  NETCLASS_MAP::iterator found = m_NetClasses.find( aNetName );
130 
131  if( found != m_NetClasses.end() )
132  {
133  std::shared_ptr<NETCLASS> netclass = found->second;
134  m_NetClasses.erase( found );
135  return netclass;
136  }
137 
138  return NETCLASSPTR();
139 }
140 
141 
142 NETCLASSPTR NETCLASSES::Find( const wxString& aName ) const
143 {
144  if( aName == NETCLASS::Default )
145  return GetDefault();
146 
147  NETCLASS_MAP::const_iterator found = m_NetClasses.find( aName );
148 
149  if( found == m_NetClasses.end() )
150  return NETCLASSPTR();
151  else
152  return found->second;
153 }
154 
155 
157 {
159  NETCLASSPTR defaultNetClass = netClasses.GetDefault();
160 
161  // set all NETs to the default NETCLASS, then later override some
162  // as we go through the NETCLASSes.
163 
164  for( NETINFO_LIST::iterator net( m_NetInfo.begin() ), netEnd( m_NetInfo.end() );
165  net != netEnd; ++net )
166  {
167  net->SetClass( defaultNetClass );
168  }
169 
170  // Add netclass name and pointer to nets. If a net is in more than one netclass,
171  // set the net's name and pointer to only the first netclass. Subsequent
172  // and therefore bogus netclass memberships will be deleted in logic below this loop.
173  for( NETCLASSES::iterator clazz = netClasses.begin(); clazz != netClasses.end(); ++clazz )
174  {
175  NETCLASSPTR netclass = clazz->second;
176 
177  for( NETCLASS::const_iterator member = netclass->begin(); member != netclass->end(); ++member )
178  {
179  const wxString& netname = *member;
180 
181  // although this overall function seems to be adequately fast,
182  // FindNet( wxString ) uses now a fast binary search and is fast
183  // event for large net lists
184  NETINFO_ITEM* net = FindNet( netname );
185 
186  if( net && net->GetClassName() == NETCLASS::Default )
187  {
188  net->SetClass( netclass );
189  }
190  }
191  }
192 
193  // Finally, make sure that every NET is in a NETCLASS, even if that
194  // means the Default NETCLASS. And make sure that all NETCLASSes do not
195  // contain netnames that do not exist, by deleting all netnames from
196  // every netclass and re-adding them.
197 
198  for( NETCLASSES::iterator clazz = netClasses.begin(); clazz != netClasses.end(); ++clazz )
199  {
200  NETCLASSPTR netclass = clazz->second;
201 
202  netclass->Clear();
203  }
204 
205  defaultNetClass->Clear();
206 
207  for( NETINFO_LIST::iterator net( m_NetInfo.begin() ), netEnd( m_NetInfo.end() );
208  net != netEnd; ++net )
209  {
210  const wxString& classname = net->GetClassName();
211 
212  // because of the std:map<> this should be fast, and because of
213  // prior logic, netclass should not be NULL.
214  NETCLASSPTR netclass = netClasses.Find( classname );
215 
216  wxASSERT( netclass );
217 
218  netclass->Add( net->GetNetname() );
219  }
220 
221  // Set initial values for custom track width & via size to match the default netclass settings
223  m_designSettings.SetCustomTrackWidth( defaultNetClass->GetTrackWidth() );
224  m_designSettings.SetCustomViaSize( defaultNetClass->GetViaDiameter() );
225  m_designSettings.SetCustomViaDrill( defaultNetClass->GetViaDrill() );
226 }
227 
228 
229 #if defined(DEBUG)
230 
231 void NETCLASS::Show( int nestLevel, std::ostream& os ) const
232 {
233  // for now, make it look like XML:
234  //NestedSpace( nestLevel, os )
235 
236  os << '<' << GetClass().Lower().mb_str() << ">\n";
237 
238  for( const_iterator i = begin(); i!=end(); ++i )
239  {
240  // NestedSpace( nestLevel+1, os ) << *i;
241  os << TO_UTF8( *i );
242  }
243 
244  // NestedSpace( nestLevel, os )
245  os << "</" << GetClass().Lower().mb_str() << ">\n";
246 }
247 
248 #endif
249 
250 
251 void NETCLASS::Format( OUTPUTFORMATTER* aFormatter, int aNestLevel, int aControlBits ) const
252 {
253  aFormatter->Print( aNestLevel, "(net_class %s %s\n",
254  aFormatter->Quotew( GetName() ).c_str(),
255  aFormatter->Quotew( GetDescription() ).c_str() );
256 
257  aFormatter->Print( aNestLevel+1, "(clearance %s)\n", FMT_IU( GetClearance() ).c_str() );
258  aFormatter->Print( aNestLevel+1, "(trace_width %s)\n", FMT_IU( GetTrackWidth() ).c_str() );
259 
260  aFormatter->Print( aNestLevel+1, "(via_dia %s)\n", FMT_IU( GetViaDiameter() ).c_str() );
261  aFormatter->Print( aNestLevel+1, "(via_drill %s)\n", FMT_IU( GetViaDrill() ).c_str() );
262 
263  aFormatter->Print( aNestLevel+1, "(uvia_dia %s)\n", FMT_IU( GetuViaDiameter() ).c_str() );
264  aFormatter->Print( aNestLevel+1, "(uvia_drill %s)\n", FMT_IU( GetuViaDrill() ).c_str() );
265 
266  // Save the diff_pair_gap and diff_pair_width values only if not the default, to avoid unnecessary
267  // incompatibility with previous Pcbnew versions.
270  {
271  aFormatter->Print( aNestLevel+1, "(diff_pair_gap %s)\n", FMT_IU( GetDiffPairGap() ).c_str() );
272  aFormatter->Print( aNestLevel+1, "(diff_pair_width %s)\n", FMT_IU( GetDiffPairWidth() ).c_str() );
273  }
274 
275 
276  for( NETCLASS::const_iterator it = begin(); it != end(); ++it )
277  aFormatter->Print( aNestLevel+1, "(add_net %s)\n", aFormatter->Quotew( *it ).c_str() );
278 
279  aFormatter->Print( aNestLevel, ")\n\n" );
280 }
const wxString & GetName() const
Wrapper class, so you can iterate through NETINFO_ITEM*s, not std::pair
NETCLASSPTR Find(const wxString &aName) const
Function Find searches this container for a NETCLASS given by aName.
void SetDiffPairWidth(int aSize)
int GetuViaDrill() const
int GetuViaDiameter() const
NETCLASSPTR m_default
the default NETCLASS
int GetClearance() const
static const int DEFAULT_UVIA_DIAMETER
void SetCustomViaDrill(int aDrill)
Function SetCustomViaDrill Sets custom size for via drill (i.e.
Class BOARD to handle a board.
wxString GetClassName() const
Function GetClassName returns the class name.
Class OUTPUTFORMATTER is an important interface (abstract class) used to output 8 bit text in a conve...
Definition: richio.h:327
iterator end()
NETINFO_LIST m_NetInfo
net info list (name, design constraints ..
Definition: class_board.h:199
static const int DEFAULT_DIFF_PAIR_GAP
void SetTrackWidth(int aWidth)
void SetClass(NETCLASSPTR aNetClass)
Function SetClass sets aNetclass into this NET.
int GetTrackWidth() const
void SetCustomViaSize(int aSize)
Function SetCustomViaSize Sets custom size for via diameter (i.e.
void SetViaDrill(int aSize)
iterator begin()
int GetDiffPairGap() const
void Format(OUTPUTFORMATTER *aFormatter, int aNestLevel, int aControlBits) const
Function Format outputs the net class to aFormatter in s-expression form.
This file contains miscellaneous commonly used macros and functions.
void SetDiffPairGap(int aSize)
#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 ...
NETCLASS_MAP::iterator iterator
iterator begin()
Class NETCLASSES is a container for NETCLASS instances.
NETCLASS(const wxString &aName)
Constructor stuffs a NETCLASS instance with aParent, aName, and optionally the initialParameters.
iterator end()
static const char Default[]
the name of the default NETCLASS
bool Add(NETCLASSPTR aNetclass)
Function Add takes aNetclass and puts it into this NETCLASSES container.
wxString GetClass() const
void SetClearance(int aClearance)
Class NETCLASS handles a collection of nets and the parameters used to route or test these nets...
#define FMT_IU
static const int DEFAULT_UVIA_DRILL
void SetParams(const NETCLASS &aDefaults)
Function SetParams will set all the parameters by copying them from defaults.
const wxString & GetDescription() const
std::string Quotew(const wxString &aWrapee)
Definition: richio.cpp:486
void SetuViaDiameter(int aSize)
int GetViaDiameter() const
NETCLASSPTR Remove(const wxString &aNetName)
Function Remove removes a NETCLASS from this container but does not destroy/delete it...
static const int DEFAULT_VIA_DIAMETER
void SetCustomTrackWidth(int aWidth)
Function SetCustomTrackWidth Sets custom width for track (i.e.
static const int DEFAULT_VIA_DRILL
int GetDiffPairWidth() const
int GetViaDrill() const
Class NETINFO_ITEM handles the data for a net.
Definition: class_netinfo.h:69
iterator begin() const
void SetuViaDrill(int aSize)
iterator end() const
NETCLASSPTR GetDefault() const
Function GetDefault.
NETINFO_ITEM * FindNet(int aNetcode) const
Function FindNet searches for a net with the given netcode.
The common library.
static const int DEFAULT_TRACK_WIDTH
const char * name
STRINGSET::const_iterator const_iterator
void SetViaDiameter(int aDia)
BOARD_DESIGN_SETTINGS m_designSettings
Definition: class_board.h:193
int PRINTF_FUNC Print(int nestLevel, const char *fmt,...)
Function Print formats and writes text to the output stream.
Definition: richio.cpp:408
NETCLASS_MAP m_NetClasses
all the NETCLASSes except the default one.
static const int DEFAULT_DIFF_PAIR_WIDTH
static const int DEFAULT_CLEARANCE
void UseCustomTrackViaSize(bool aEnabled)
Function UseCustomTrackViaSize Enables/disables custom track/via size settings.
NETCLASSES m_NetClasses
List of current netclasses. There is always the default netclass.