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