KiCad PCB EDA Suite
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 <netclass.h>
35 
36 // This will get mapped to "kicad_default" in the specctra_export.
37 const char NETCLASS::Default[] = "Default";
38 
39 // Initial values for netclass initialization
40 const int DEFAULT_CLEARANCE = Millimeter2iu( 0.2 ); // track to track and track to pads clearance
41 const int DEFAULT_VIA_DIAMETER = Millimeter2iu( 0.8 );
42 const int DEFAULT_VIA_DRILL = Millimeter2iu( 0.4 );
43 const int DEFAULT_UVIA_DIAMETER = Millimeter2iu( 0.3 );
44 const int DEFAULT_UVIA_DRILL = Millimeter2iu( 0.1 );
45 const int DEFAULT_TRACK_WIDTH = Millimeter2iu( 0.25 );
46 const int DEFAULT_DIFF_PAIR_WIDTH = Millimeter2iu( 0.2 );
47 const int DEFAULT_DIFF_PAIR_GAP = Millimeter2iu( 0.25 );
48 const int DEFAULT_DIFF_PAIR_VIAGAP = 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
66 }
67 
68 
69 void NETCLASS::SetParams( const NETCLASS& aDefaults )
70 {
71  SetClearance( aDefaults.GetClearance() );
72  SetTrackWidth( aDefaults.GetTrackWidth() );
73  SetViaDiameter( aDefaults.GetViaDiameter() );
74  SetViaDrill( aDefaults.GetViaDrill() );
75  SetuViaDiameter( aDefaults.GetuViaDiameter() );
76  SetuViaDrill( aDefaults.GetuViaDrill() );
77  SetDiffPairWidth( aDefaults.GetDiffPairWidth() );
78  SetDiffPairGap( aDefaults.GetDiffPairGap() );
79  SetDiffPairViaGap( aDefaults.GetDiffPairViaGap() );
80 }
81 
82 
84 {
85 }
86 
87 
89 {
90  m_default = std::make_shared<NETCLASS>( NETCLASS::Default );
91 }
92 
93 
95 {
96 }
97 
98 
99 bool NETCLASSES::Add( const NETCLASSPTR& aNetClass )
100 {
101  const wxString& name = aNetClass->GetName();
102 
103  if( name == NETCLASS::Default )
104  {
105  m_default = aNetClass;
106  return true;
107  }
108 
109  // Test for an existing netclass:
110  if( !Find( name ) )
111  {
112  // name not found, take ownership
113  m_NetClasses[name] = aNetClass;
114 
115  return true;
116  }
117  else
118  {
119  // name already exists
120  // do not "take ownership" and return false telling caller such.
121  return false;
122  }
123 }
124 
125 
126 NETCLASSPTR NETCLASSES::Remove( const wxString& aNetName )
127 {
128  NETCLASS_MAP::iterator found = m_NetClasses.find( aNetName );
129 
130  if( found != m_NetClasses.end() )
131  {
132  std::shared_ptr<NETCLASS> netclass = found->second;
133  m_NetClasses.erase( found );
134  return netclass;
135  }
136 
137  return NETCLASSPTR();
138 }
139 
140 
141 NETCLASSPTR NETCLASSES::Find( const wxString& aName ) const
142 {
143  if( aName == NETCLASS::Default )
144  return GetDefault();
145 
146  NETCLASS_MAP::const_iterator found = m_NetClasses.find( aName );
147 
148  if( found == m_NetClasses.end() )
149  return NETCLASSPTR();
150  else
151  return found->second;
152 }
153 
154 
156 {
157  NETCLASSES& netClasses = m_designSettings.m_NetClasses;
158  NETCLASSPTR defaultNetClass = netClasses.GetDefault();
159 
160  // set all NETs to the default NETCLASS, then later override some
161  // as we go through the NETCLASSes.
162 
163  for( NETINFO_LIST::iterator net( m_NetInfo.begin() ), netEnd( m_NetInfo.end() );
164  net != netEnd; ++net )
165  {
166  net->SetClass( defaultNetClass );
167  }
168 
169  // Add netclass name and pointer to nets. If a net is in more than one netclass,
170  // set the net's name and pointer to only the first netclass. Subsequent
171  // and therefore bogus netclass memberships will be deleted in logic below this loop.
172  for( NETCLASSES::iterator clazz = netClasses.begin(); clazz != netClasses.end(); ++clazz )
173  {
174  NETCLASSPTR netclass = clazz->second;
175 
176  for( NETCLASS::const_iterator member = netclass->begin(); member != netclass->end(); ++member )
177  {
178  const wxString& netname = *member;
179 
180  // although this overall function seems to be adequately fast,
181  // FindNet( wxString ) uses now a fast binary search and is fast
182  // event for large net lists
183  NETINFO_ITEM* net = FindNet( netname );
184 
185  if( net && net->GetClassName() == NETCLASS::Default )
186  {
187  net->SetClass( netclass );
188  }
189  }
190  }
191 
192  // Finally, make sure that every NET is in a NETCLASS, even if that
193  // means the Default NETCLASS. And make sure that all NETCLASSes do not
194  // contain netnames that do not exist, by deleting all netnames from
195  // every netclass and re-adding them.
196 
197  for( NETCLASSES::iterator clazz = netClasses.begin(); clazz != netClasses.end(); ++clazz )
198  {
199  NETCLASSPTR netclass = clazz->second;
200 
201  netclass->Clear();
202  }
203 
204  defaultNetClass->Clear();
205 
206  for( NETINFO_LIST::iterator net( m_NetInfo.begin() ), netEnd( m_NetInfo.end() );
207  net != netEnd; ++net )
208  {
209  const wxString& classname = net->GetClassName();
210 
211  // because of the std:map<> this should be fast, and because of
212  // prior logic, netclass should not be NULL.
213  NETCLASSPTR netclass = netClasses.Find( classname );
214 
215  wxASSERT( netclass );
216 
217  netclass->Add( net->GetNetname() );
218  }
219 
220  // Set initial values for custom track width & via size to match the default netclass settings
221  m_designSettings.UseCustomTrackViaSize( false );
222  m_designSettings.SetCustomTrackWidth( defaultNetClass->GetTrackWidth() );
223  m_designSettings.SetCustomViaSize( defaultNetClass->GetViaDiameter() );
224  m_designSettings.SetCustomViaDrill( defaultNetClass->GetViaDrill() );
225  m_designSettings.SetCustomDiffPairWidth( defaultNetClass->GetDiffPairWidth() );
226  m_designSettings.SetCustomDiffPairGap( defaultNetClass->GetDiffPairGap() );
227  m_designSettings.SetCustomDiffPairViaGap( defaultNetClass->GetDiffPairViaGap() );
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", FormatInternalUnits( GetClearance() ).c_str() );
260  aFormatter->Print( aNestLevel+1, "(trace_width %s)\n", FormatInternalUnits( GetTrackWidth() ).c_str() );
261 
262  aFormatter->Print( aNestLevel+1, "(via_dia %s)\n", FormatInternalUnits( GetViaDiameter() ).c_str() );
263  aFormatter->Print( aNestLevel+1, "(via_drill %s)\n", FormatInternalUnits( GetViaDrill() ).c_str() );
264 
265  aFormatter->Print( aNestLevel+1, "(uvia_dia %s)\n", FormatInternalUnits( GetuViaDiameter() ).c_str() );
266  aFormatter->Print( aNestLevel+1, "(uvia_drill %s)\n", FormatInternalUnits( 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_width %s)\n",
274  FormatInternalUnits( GetDiffPairWidth() ).c_str() );
275  aFormatter->Print( aNestLevel+1, "(diff_pair_gap %s)\n",
276  FormatInternalUnits( GetDiffPairGap() ).c_str() );
277 
278  // 6.0 TODO: figure out what to do with DiffPairViaGap...
279  }
280 
281  for( NETCLASS::const_iterator it = begin(); it != end(); ++it )
282  aFormatter->Print( aNestLevel+1, "(add_net %s)\n", aFormatter->Quotew( *it ).c_str() );
283 
284  aFormatter->Print( aNestLevel, ")\n\n" );
285 }
const wxString & GetName() const
Definition: netclass.h:96
Wrapper class, so you can iterate through NETINFO_ITEM*s, not std::pair<int/wxString, NETINFO_ITEM*>
Definition: netinfo.h:486
NETCLASSPTR Find(const wxString &aName) const
Function Find searches this container for a NETCLASS given by aName.
Definition: netclass.cpp:141
void SetDiffPairWidth(int aSize)
Definition: netclass.h:181
int GetuViaDrill() const
Definition: netclass.h:177
int GetuViaDiameter() const
Definition: netclass.h:174
int GetClearance() const
Definition: netclass.h:162
Class BOARD to handle a board.
wxString GetClassName() const
Function GetClassName returns the class name.
Definition: netinfo.h:135
int GetDiffPairViaGap() const
Definition: netclass.h:186
const int DEFAULT_VIA_DIAMETER
Definition: netclass.cpp:41
Class OUTPUTFORMATTER is an important interface (abstract class) used to output 8 bit text in a conve...
Definition: richio.h:327
iterator end()
Definition: netclass.h:249
void SetTrackWidth(int aWidth)
Definition: netclass.h:166
int GetTrackWidth() const
Definition: netclass.h:165
void SetViaDrill(int aSize)
Definition: netclass.h:172
iterator begin()
Definition: netclass.h:132
int GetDiffPairGap() const
Definition: netclass.h:183
void Format(OUTPUTFORMATTER *aFormatter, int aNestLevel, int aControlBits) const
Function Format outputs the net class to aFormatter in s-expression form.
Definition: netclass.cpp:253
This file contains miscellaneous commonly used macros and functions.
void SetDiffPairGap(int aSize)
Definition: netclass.h:184
#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 ...
Definition: netclass.cpp:155
NETCLASS_MAP::iterator iterator
Definition: netclass.h:247
iterator begin()
Definition: netclass.h:248
Class NETCLASSES is a container for NETCLASS instances.
Definition: netclass.h:224
NETCLASS(const wxString &aName)
Constructor stuffs a NETCLASS instance with aParent, aName, and optionally the initialParameters.
Definition: netclass.cpp:51
iterator end()
Definition: netclass.h:133
static const char Default[]
the name of the default NETCLASS
Definition: netclass.h:80
wxString GetClass() const
Definition: netclass.h:91
void SetClearance(int aClearance)
Definition: netclass.h:163
~NETCLASS()
Definition: netclass.cpp:83
const int DEFAULT_UVIA_DIAMETER
Definition: netclass.cpp:43
const int DEFAULT_CLEARANCE
Definition: netclass.cpp:40
Class NETCLASS handles a collection of nets and the parameters used to route or test these nets...
Definition: netclass.h:55
const int DEFAULT_DIFF_PAIR_VIAGAP
Definition: netclass.cpp:48
void SetParams(const NETCLASS &aDefaults)
Function SetParams will set all the parameters by copying them from defaults.
Definition: netclass.cpp:69
const wxString & GetDescription() const
Definition: netclass.h:159
std::string Quotew(const wxString &aWrapee)
Definition: richio.cpp:482
const int DEFAULT_DIFF_PAIR_GAP
Definition: netclass.cpp:47
void SetuViaDiameter(int aSize)
Definition: netclass.h:175
int GetViaDiameter() const
Definition: netclass.h:168
NETCLASSPTR Remove(const wxString &aNetName)
Function Remove removes a NETCLASS from this container but does not destroy/delete it...
Definition: netclass.cpp:126
void SetDiffPairViaGap(int aSize)
Definition: netclass.h:187
int GetDiffPairWidth() const
Definition: netclass.h:180
int GetViaDrill() const
Definition: netclass.h:171
const int DEFAULT_VIA_DRILL
Definition: netclass.cpp:42
bool Add(const NETCLASSPTR &aNetclass)
Function Add takes aNetclass and puts it into this NETCLASSES container.
Definition: netclass.cpp:99
Class NETINFO_ITEM handles the data for a net.
Definition: netinfo.h:69
void SetuViaDrill(int aSize)
Definition: netclass.h:178
const int DEFAULT_DIFF_PAIR_WIDTH
Definition: netclass.cpp:46
const int DEFAULT_TRACK_WIDTH
Definition: netclass.cpp:45
const char * name
Definition: DXF_plotter.cpp:61
NETCLASSPTR GetDefault() const
Function GetDefault.
Definition: netclass.h:268
size_t i
Definition: json11.cpp:597
void SetClass(const NETCLASSPTR &aNetClass)
Function SetClass sets aNetclass into this NET.
The common library.
STRINGSET::const_iterator const_iterator
Definition: netclass.h:135
void SetViaDiameter(int aDia)
Definition: netclass.h:169
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.
Definition: netclass.h:229
const int DEFAULT_UVIA_DRILL
Definition: netclass.cpp:44
std::string FormatInternalUnits(int aValue)
Function FormatInternalUnits converts aValue from internal units to a string appropriate for writing ...
Definition: base_units.cpp:457