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 
37 #define FMT_IU BOARD_ITEM::FormatInternalUnits
38 #define FMT_ANGLE BOARD_ITEM::FormatAngle
39 
40 
41 // This will get mapped to "kicad_default" in the specctra_export.
42 const char NETCLASS::Default[] = "Default";
43 
44 // Initial values for netclass initialization
45 const int NETCLASS::DEFAULT_CLEARANCE = Millimeter2iu( 0.2 ); // track to track and track to pads clearance
46 const int NETCLASS::DEFAULT_VIA_DIAMETER = Millimeter2iu( 0.8 );
47 const int NETCLASS::DEFAULT_VIA_DRILL = Millimeter2iu( 0.4 );
48 const int NETCLASS::DEFAULT_UVIA_DIAMETER = Millimeter2iu( 0.3 );
49 const int NETCLASS::DEFAULT_UVIA_DRILL = Millimeter2iu( 0.1 );
50 const int NETCLASS::DEFAULT_TRACK_WIDTH = Millimeter2iu( 0.25 );
51 const int NETCLASS::DEFAULT_DIFF_PAIR_WIDTH = Millimeter2iu( 0.2 );
52 const int NETCLASS::DEFAULT_DIFF_PAIR_GAP = Millimeter2iu( 0.25 );
53 
54 
55 NETCLASS::NETCLASS( const wxString& aName ) :
56  m_Name( aName )
57 {
58  // Default settings
62  // These defaults will be overwritten by SetParams,
63  // from the board design parameters, later
69 }
70 
71 
72 void NETCLASS::SetParams( const NETCLASS& aDefaults )
73 {
74  SetClearance( aDefaults.GetClearance() );
75  SetTrackWidth( aDefaults.GetTrackWidth() );
76  SetViaDiameter( aDefaults.GetViaDiameter() );
77  SetViaDrill( aDefaults.GetViaDrill() );
78  SetuViaDiameter( aDefaults.GetuViaDiameter() );
79  SetuViaDrill( aDefaults.GetuViaDrill() );
80  SetDiffPairWidth( aDefaults.GetDiffPairWidth() );
81  SetDiffPairGap( aDefaults.GetDiffPairGap() );
82 
83 }
84 
85 
87 {
88 }
89 
90 
92 {
93  m_default = std::make_shared<NETCLASS>( NETCLASS::Default );
94 }
95 
96 
98 {
99 }
100 
101 
102 bool NETCLASSES::Add( NETCLASSPTR aNetClass )
103 {
104  const wxString& name = aNetClass->GetName();
105 
106  if( name == NETCLASS::Default )
107  {
108  m_default = aNetClass;
109  return true;
110  }
111 
112  // Test for an existing netclass:
113  if( !Find( name ) )
114  {
115  // name not found, take ownership
116  m_NetClasses[name] = aNetClass;
117 
118  return true;
119  }
120  else
121  {
122  // name already exists
123  // do not "take ownership" and return false telling caller such.
124  return false;
125  }
126 }
127 
128 
129 NETCLASSPTR NETCLASSES::Remove( const wxString& aNetName )
130 {
131  NETCLASS_MAP::iterator found = m_NetClasses.find( aNetName );
132 
133  if( found != m_NetClasses.end() )
134  {
135  std::shared_ptr<NETCLASS> netclass = found->second;
136  m_NetClasses.erase( found );
137  return netclass;
138  }
139 
140  return NETCLASSPTR();
141 }
142 
143 
144 NETCLASSPTR NETCLASSES::Find( const wxString& aName ) const
145 {
146  if( aName == NETCLASS::Default )
147  return GetDefault();
148 
149  NETCLASS_MAP::const_iterator found = m_NetClasses.find( aName );
150 
151  if( found == m_NetClasses.end() )
152  return NETCLASSPTR();
153  else
154  return found->second;
155 }
156 
157 
159 {
161  NETCLASSPTR defaultNetClass = netClasses.GetDefault();
162 
163  // set all NETs to the default NETCLASS, then later override some
164  // as we go through the NETCLASSes.
165 
166  for( NETINFO_LIST::iterator net( m_NetInfo.begin() ), netEnd( m_NetInfo.end() );
167  net != netEnd; ++net )
168  {
169  net->SetClass( defaultNetClass );
170  }
171 
172  // Add netclass name and pointer to nets. If a net is in more than one netclass,
173  // set the net's name and pointer to only the first netclass. Subsequent
174  // and therefore bogus netclass memberships will be deleted in logic below this loop.
175  for( NETCLASSES::iterator clazz = netClasses.begin(); clazz != netClasses.end(); ++clazz )
176  {
177  NETCLASSPTR netclass = clazz->second;
178 
179  for( NETCLASS::const_iterator member = netclass->begin(); member != netclass->end(); ++member )
180  {
181  const wxString& netname = *member;
182 
183  // although this overall function seems to be adequately fast,
184  // FindNet( wxString ) uses now a fast binary search and is fast
185  // event for large net lists
186  NETINFO_ITEM* net = FindNet( netname );
187 
188  if( net && net->GetClassName() == NETCLASS::Default )
189  {
190  net->SetClass( netclass );
191  }
192  }
193  }
194 
195  // Finally, make sure that every NET is in a NETCLASS, even if that
196  // means the Default NETCLASS. And make sure that all NETCLASSes do not
197  // contain netnames that do not exist, by deleting all netnames from
198  // every netclass and re-adding them.
199 
200  for( NETCLASSES::iterator clazz = netClasses.begin(); clazz != netClasses.end(); ++clazz )
201  {
202  NETCLASSPTR netclass = clazz->second;
203 
204  netclass->Clear();
205  }
206 
207  defaultNetClass->Clear();
208 
209  for( NETINFO_LIST::iterator net( m_NetInfo.begin() ), netEnd( m_NetInfo.end() );
210  net != netEnd; ++net )
211  {
212  const wxString& classname = net->GetClassName();
213 
214  // because of the std:map<> this should be fast, and because of
215  // prior logic, netclass should not be NULL.
216  NETCLASSPTR netclass = netClasses.Find( classname );
217 
218  wxASSERT( netclass );
219 
220  netclass->Add( net->GetNetname() );
221  }
222 
223  // Set initial values for custom track width & via size to match the default netclass settings
225  m_designSettings.SetCustomTrackWidth( defaultNetClass->GetTrackWidth() );
226  m_designSettings.SetCustomViaSize( defaultNetClass->GetViaDiameter() );
227  m_designSettings.SetCustomViaDrill( defaultNetClass->GetViaDrill() );
228 }
229 
230 
231 #if defined(DEBUG)
232 
233 void NETCLASS::Show( int nestLevel, std::ostream& os ) const
234 {
235  // for now, make it look like XML:
236  //NestedSpace( nestLevel, os )
237 
238  os << '<' << GetClass().Lower().mb_str() << ">\n";
239 
240  for( const_iterator i = begin(); i!=end(); ++i )
241  {
242  // NestedSpace( nestLevel+1, os ) << *i;
243  os << TO_UTF8( *i );
244  }
245 
246  // NestedSpace( nestLevel, os )
247  os << "</" << GetClass().Lower().mb_str() << ">\n";
248 }
249 
250 #endif
251 
252 
253 void NETCLASS::Format( OUTPUTFORMATTER* aFormatter, int aNestLevel, int aControlBits ) const
254 {
255  aFormatter->Print( aNestLevel, "(net_class %s %s\n",
256  aFormatter->Quotew( GetName() ).c_str(),
257  aFormatter->Quotew( GetDescription() ).c_str() );
258 
259  aFormatter->Print( aNestLevel+1, "(clearance %s)\n", FMT_IU( GetClearance() ).c_str() );
260  aFormatter->Print( aNestLevel+1, "(trace_width %s)\n", FMT_IU( GetTrackWidth() ).c_str() );
261 
262  aFormatter->Print( aNestLevel+1, "(via_dia %s)\n", FMT_IU( GetViaDiameter() ).c_str() );
263  aFormatter->Print( aNestLevel+1, "(via_drill %s)\n", FMT_IU( GetViaDrill() ).c_str() );
264 
265  aFormatter->Print( aNestLevel+1, "(uvia_dia %s)\n", FMT_IU( GetuViaDiameter() ).c_str() );
266  aFormatter->Print( aNestLevel+1, "(uvia_drill %s)\n", FMT_IU( GetuViaDrill() ).c_str() );
267 
268  // Save the diff_pair_gap and diff_pair_width values only if not the default, to avoid unnecessary
269  // incompatibility with previous Pcbnew versions.
272  {
273  aFormatter->Print( aNestLevel+1, "(diff_pair_gap %s)\n", FMT_IU( GetDiffPairGap() ).c_str() );
274  aFormatter->Print( aNestLevel+1, "(diff_pair_width %s)\n", FMT_IU( GetDiffPairWidth() ).c_str() );
275  }
276 
277 
278  for( NETCLASS::const_iterator it = begin(); it != end(); ++it )
279  aFormatter->Print( aNestLevel+1, "(add_net %s)\n", aFormatter->Quotew( *it ).c_str() );
280 
281  aFormatter->Print( aNestLevel, ")\n\n" );
282 }
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.
#define FMT_IU
Abbrevation for fomatting internal units to a string.
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...
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:482
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:404
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.