KiCad PCB EDA Suite
Loading...
Searching...
No Matches
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 <[email protected]>
5 * Copyright (C) 2009 Jean-Pierre Charras, [email protected]
6 * Copyright The KiCad Developers, see AUTHORS.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, see <https://www.gnu.org/licenses/>.
20 */
21
22#include <algorithm>
23#include <netclass.h>
24#include <macros.h>
25#include <base_units.h>
26#include <api/api_enums.h>
27#include <api/api_utils.h>
28#include <api/common/types/project_settings.pb.h>
29
30// This will get mapped to "kicad_default" in the specctra_export.
31const char NETCLASS::Default[] = "Default";
32
33// Initial values for netclass initialization
34
35// track to track and track to pads clearance.
36const int DEFAULT_CLEARANCE = pcbIUScale.mmToIU( 0.2 );
37const int DEFAULT_VIA_DIAMETER = pcbIUScale.mmToIU( 0.6 );
38const int DEFAULT_VIA_DRILL = pcbIUScale.mmToIU( 0.3 );
39const int DEFAULT_UVIA_DIAMETER = pcbIUScale.mmToIU( 0.3 );
40const int DEFAULT_UVIA_DRILL = pcbIUScale.mmToIU( 0.1 );
41const int DEFAULT_TRACK_WIDTH = pcbIUScale.mmToIU( 0.2 );
42const int DEFAULT_DIFF_PAIR_WIDTH = pcbIUScale.mmToIU( 0.2 );
43const int DEFAULT_DIFF_PAIR_GAP = pcbIUScale.mmToIU( 0.25 );
44const int DEFAULT_DIFF_PAIR_VIAGAP = pcbIUScale.mmToIU( 0.25 );
45
46const int DEFAULT_WIRE_WIDTH = schIUScale.MilsToIU( 6 );
47const int DEFAULT_BUS_WIDTH = schIUScale.MilsToIU( 12 );
48
49const int DEFAULT_LINE_STYLE = 0; // solid
50
51
52NETCLASS::NETCLASS( const wxString& aName, bool aInitWithDefaults ) : m_isDefault( false )
53{
54 m_constituents.push_back( this );
55
56 SetName( aName );
57 SetPriority( -1 );
58 SetTuningProfile( wxEmptyString );
59
60 // Colors are a special optional case - always set, but UNSPECIFIED used in place of optional
63
64 if( aInitWithDefaults )
65 {
75
79 }
80
82}
83
84
86{
87 SetClearanceParent( this );
88 SetTrackWidthParent( this );
90 SetViaDrillParent( this );
92 SetuViaDrillParent( this );
96 SetPcbColorParent( this );
97 SetWireWidthParent( this );
98 SetBusWidthParent( this );
100 SetLineStyleParent( this );
102}
103
104
106{
109 SetClearance( std::optional<int>() );
110 SetViaDrill( std::optional<int>() );
111 SetuViaDrill( std::optional<int>() );
112 SetTrackWidth( std::optional<int>() );
113 SetViaDiameter( std::optional<int>() );
114 SetuViaDiameter( std::optional<int>() );
115 SetDiffPairWidth( std::optional<int>() );
116 SetDiffPairGap( std::optional<int>() );
117 SetDiffPairViaGap( std::optional<int>() );
118 SetWireWidth( std::optional<int>() );
119 SetBusWidth( std::optional<int>() );
120 SetLineStyle( std::optional<int>() );
121
122 ResetParents();
123}
124
125
126bool NETCLASS::operator==( const NETCLASS& other ) const
127{
128 return m_constituents == other.m_constituents;
129}
130
131
133{
134 // m_Description intentionally omitted: the project-file JSON writer does not
135 // serialize description, so a load round-trip drops it. Including it here
136 // would mark every fresh load dirty (or every CopyFrom-produced copy unequal).
137 //
138 // This list is a hand-maintained mirror of the netclass JSON serialization in
139 // NET_SETTINGS (common/project/net_settings.cpp, the saveNetclass/readNetClass
140 // lambdas). Any new persisted field added there must be added here too, or two
141 // netclasses differing only in the new field will silently compare equal.
142 return m_Name == aOther.m_Name
143 && m_Priority == aOther.m_Priority
144 && m_Clearance == aOther.m_Clearance
145 && m_TrackWidth == aOther.m_TrackWidth
146 && m_ViaDia == aOther.m_ViaDia
147 && m_ViaDrill == aOther.m_ViaDrill
148 && m_uViaDia == aOther.m_uViaDia
149 && m_uViaDrill == aOther.m_uViaDrill
151 && m_diffPairGap == aOther.m_diffPairGap
153 && m_wireWidth == aOther.m_wireWidth
154 && m_busWidth == aOther.m_busWidth
156 && m_lineStyle == aOther.m_lineStyle
157 && m_pcbColor == aOther.m_pcbColor
158 && m_tuningProfile == aOther.m_tuningProfile;
159}
160
161
162void NETCLASS::Serialize( google::protobuf::Any &aContainer ) const
163{
164 using namespace kiapi::common;
165 project::NetClass nc;
166
167 nc.set_name( m_Name.ToUTF8() );
168 nc.set_priority( m_Priority );
169
170 nc.set_type( m_constituents.empty() ? project::NCT_EXPLICIT : project::NCT_IMPLICIT );
171
172 for( NETCLASS* member : m_constituents )
173 nc.add_constituents( member->GetName() );
174
175 project::NetClassBoardSettings* board = nc.mutable_board();
176
177 if( m_Clearance )
178 board->mutable_clearance()->set_value_nm( *m_Clearance );
179
180 if( m_TrackWidth )
181 board->mutable_track_width()->set_value_nm( *m_TrackWidth );
182
183 if( m_diffPairWidth )
184 board->mutable_diff_pair_track_width()->set_value_nm( *m_diffPairWidth );
185
186 if( m_diffPairGap )
187 board->mutable_diff_pair_gap()->set_value_nm( *m_diffPairGap );
188
189 if( m_diffPairViaGap )
190 board->mutable_diff_pair_via_gap()->set_value_nm( *m_diffPairViaGap );
191
192 if( m_ViaDia )
193 {
194 kiapi::board::types::PadStackLayer* layer = board->mutable_via_stack()->add_copper_layers();
195 layer->set_shape( kiapi::board::types::PSS_CIRCLE );
196 layer->set_layer( kiapi::board::types::BoardLayer::BL_F_Cu );
197 PackVector2( *layer->mutable_size(), { *m_ViaDia, *m_ViaDia } );
198 }
199
200 if( m_ViaDrill )
201 {
202 PackVector2( *board->mutable_via_stack()->mutable_drill()->mutable_diameter(),
203 { *m_ViaDrill, *m_ViaDrill } );
204 }
205
207 PackColor( *board->mutable_color(), m_pcbColor );
208
209 if( m_tuningProfile != wxEmptyString )
210 board->set_tuning_profile( m_tuningProfile.ToUTF8() );
211
212 project::NetClassSchematicSettings* schematic = nc.mutable_schematic();
213
214 if( m_wireWidth )
215 PackDistance( *schematic->mutable_wire_width(), *m_wireWidth, schIUScale );
216
217 if( m_busWidth )
218 PackDistance( *schematic->mutable_bus_width(), *m_busWidth, schIUScale );
219
221 PackColor( *schematic->mutable_color(), m_schematicColor );
222
223 if( m_lineStyle )
224 {
225 // TODO(JE) resolve issues with moving to kicommon
226 // schematic->set_line_style( ToProtoEnum<LINE_STYLE, types::StrokeLineStyle>(
227 // static_cast<LINE_STYLE>( *m_lineStyle ) ) );
228 }
229
230 aContainer.PackFrom( nc );
231}
232
233
234bool NETCLASS::Deserialize( const google::protobuf::Any &aContainer )
235{
236 using namespace kiapi::common;
237 project::NetClass nc;
238
239 if( !aContainer.UnpackTo( &nc ) )
240 return false;
241
242 m_Name = wxString::FromUTF8( nc.name() );
243 m_Priority = nc.priority();
244
245 // We don't allow creating implicit classes directly
246 if( nc.type() == project::NCT_IMPLICIT )
247 return false;
248
250
251 if( nc.board().has_clearance() )
252 m_Clearance = nc.board().clearance().value_nm();
253
254 if( nc.board().has_track_width() )
255 m_TrackWidth = nc.board().track_width().value_nm();
256
257 if( nc.board().has_diff_pair_track_width() )
258 m_diffPairWidth = nc.board().diff_pair_track_width().value_nm();
259
260 if( nc.board().has_diff_pair_gap() )
261 m_diffPairGap = nc.board().diff_pair_gap().value_nm();
262
263 if( nc.board().has_diff_pair_via_gap() )
264 m_diffPairViaGap = nc.board().diff_pair_via_gap().value_nm();
265
266 if( nc.board().has_via_stack() )
267 {
268 if( nc.board().via_stack().copper_layers_size() > 0 )
269 m_ViaDia = nc.board().via_stack().copper_layers().at( 0 ).size().x_nm();
270
271 if( nc.board().via_stack().has_drill() )
272 m_ViaDrill = nc.board().via_stack().drill().diameter().x_nm();
273 }
274
275 if( nc.board().has_color() )
276 m_pcbColor = UnpackColor( nc.board().color() );
277
278 if( nc.board().has_tuning_profile() )
279 m_tuningProfile = wxString::FromUTF8( nc.board().tuning_profile() );
280
281 if( nc.schematic().has_wire_width() )
282 m_wireWidth = UnpackDistance( nc.schematic().wire_width(), schIUScale );
283
284 if( nc.schematic().has_bus_width() )
285 m_busWidth = UnpackDistance( nc.schematic().bus_width(), schIUScale );
286
287 if( nc.schematic().has_color() )
288 m_schematicColor = UnpackColor( nc.schematic().color() );
289
290 // TODO(JE) resolve issues with moving to kicommon
291 // if( nc.schematic().has_line_style() )
292 // m_lineStyle = static_cast<int>( FromProtoEnum<LINE_STYLE>( nc.schematic().line_style() ) );
293
294 return true;
295}
296
297
298const std::vector<NETCLASS*>& NETCLASS::GetConstituentNetclasses() const
299{
300 return m_constituents;
301}
302
303
304void NETCLASS::SetConstituentNetclasses( std::vector<NETCLASS*>&& constituents )
305{
306 m_constituents = std::move( constituents );
307}
308
309
310bool NETCLASS::ContainsNetclassWithName( const wxString& netclass ) const
311{
312 return std::any_of( m_constituents.begin(), m_constituents.end(),
313 [&netclass]( const NETCLASS* nc )
314 {
315 return nc && nc->GetName().Matches( netclass );
316 } );
317}
318
319
320const wxString NETCLASS::GetHumanReadableName() const
321{
322 if( m_constituents.size() == 1 )
323 return m_Name;
324
325 wxASSERT( m_constituents.size() >= 2 );
326
327 wxString name;
328
329 if( m_constituents.size() == 2 )
330 {
331 name.Printf( _( "%s and %s" ),
333 m_constituents[1]->GetName() );
334 }
335 else if( m_constituents.size() == 3 )
336 {
337 name.Printf( _( "%s, %s and %s" ),
340 m_constituents[2]->GetName() );
341 }
342 else if( m_constituents.size() > 3 )
343 {
344 name.Printf( _( "%s, %s and %d more" ),
347 static_cast<int>( m_constituents.size() - 2 ) );
348 }
349
350 return name;
351}
352
353
354const wxString NETCLASS::GetName() const
355{
356 if( m_constituents.size() == 1 )
357 return m_Name;
358
359 wxASSERT( m_constituents.size() >= 2 );
360
361 size_t strLen = m_constituents.size() - 1; // Count commas
362
363 for( std::size_t i = 0; i < m_constituents.size(); ++i )
364 strLen += m_constituents[i]->m_Name.length();
365
366 wxString name;
367 name.reserve( strLen );
368
369 name += m_constituents[0]->m_Name;
370
371 for( std::size_t i = 1; i < m_constituents.size(); ++i )
372 {
373 name += wxS( ',' );
374 name += m_constituents[i]->m_Name;
375 }
376
377 return name;
378}
const char * name
constexpr EDA_IU_SCALE schIUScale
Definition base_units.h:123
constexpr EDA_IU_SCALE pcbIUScale
Definition base_units.h:121
static const COLOR4D UNSPECIFIED
For legacy support; used as a value to indicate color hasn't been set yet.
Definition color4d.h:398
void SetViaDiameter(int aDia)
Definition netclass.h:141
void SetViaDrill(int aSize)
Definition netclass.h:149
void SetWireWidthParent(NETCLASS *parent)
Definition netclass.h:214
COLOR4D m_pcbColor
Optional PCB color override for this netclass.
Definition netclass.h:283
static const char Default[]
the name of the default NETCLASS
Definition netclass.h:40
void SetuViaDrillParent(NETCLASS *parent)
Definition netclass.h:167
void SetPriority(int aPriority)
Definition netclass.h:247
const wxString GetHumanReadableName() const
Gets the consolidated name of this netclass (which may be an aggregate).
Definition netclass.cpp:320
void SetDiffPairWidthParent(NETCLASS *parent)
Definition netclass.h:175
void SetuViaDiameter(int aSize)
Definition netclass.h:157
void SetDiffPairWidth(int aSize)
Definition netclass.h:173
std::optional< int > m_ViaDrill
via drill hole diameter
Definition netclass.h:269
void SetViaDrillParent(NETCLASS *parent)
Definition netclass.h:151
std::optional< int > m_wireWidth
Definition netclass.h:278
bool m_isDefault
Mark if this instance is the default netclass.
Definition netclass.h:257
std::optional< int > m_busWidth
Definition netclass.h:279
void SetDiffPairGapParent(NETCLASS *parent)
Definition netclass.h:183
void SetTuningProfileParent(NETCLASS *aParent)
Definition netclass.h:253
bool ContainsNetclassWithName(const wxString &netclass) const
Determines if the given netclass name is a constituent of this (maybe aggregate) netclass.
Definition netclass.cpp:310
void ResetParents()
Resets all parent fields to point to this netclass.
Definition netclass.cpp:85
void SetLineStyle(int aStyle)
Definition netclass.h:242
std::optional< int > m_TrackWidth
track width used to route nets
Definition netclass.h:267
int m_Priority
The priority for multiple netclass resolution.
Definition netclass.h:262
NETCLASS(const wxString &aName, bool aInitWithDefaults=true)
Create a NETCLASS instance with aName.
Definition netclass.cpp:52
void SetSchematicColor(COLOR4D aColor)
Definition netclass.h:235
std::optional< int > m_diffPairWidth
Definition netclass.h:274
bool Deserialize(const google::protobuf::Any &aContainer) override
Deserializes the given protobuf message into this object.
Definition netclass.cpp:234
const wxString GetName() const
Gets the name of this (maybe aggregate) netclass in a format for internal usage or for export to exte...
Definition netclass.cpp:354
wxString m_tuningProfile
The tuning profile name being used by this netclass.
Definition netclass.h:285
COLOR4D m_schematicColor
Definition netclass.h:280
std::optional< int > m_diffPairViaGap
Definition netclass.h:276
void SetConstituentNetclasses(std::vector< NETCLASS * > &&constituents)
Sets the netclasses which make up this netclass.
Definition netclass.cpp:304
void SetLineStyleParent(NETCLASS *parent)
Definition netclass.h:244
std::optional< int > m_ViaDia
via diameter
Definition netclass.h:268
void SetDiffPairViaGap(int aSize)
Definition netclass.h:189
void ResetParameters()
Resets all parameters (except Name and Description)
Definition netclass.cpp:105
void SetTrackWidthParent(NETCLASS *parent)
Definition netclass.h:135
std::optional< int > m_lineStyle
Definition netclass.h:281
const std::vector< NETCLASS * > & GetConstituentNetclasses() const
Gets the netclasses which make up this netclass.
Definition netclass.cpp:298
void SetViaDiameterParent(NETCLASS *parent)
Definition netclass.h:143
void SetPcbColor(const COLOR4D &aColor)
Definition netclass.h:205
std::optional< int > m_Clearance
clearance when routing
Definition netclass.h:265
std::optional< int > m_uViaDia
microvia diameter
Definition netclass.h:271
bool EqualsByPersistedFields(const NETCLASS &aOther) const
Compare every persisted field (name, priority, sized fields, colors, line style, tuning profile) to d...
Definition netclass.cpp:132
void SetuViaDrill(int aSize)
Definition netclass.h:165
void SetDiffPairGap(int aSize)
Definition netclass.h:181
void SetBusWidthParent(NETCLASS *parent)
Definition netclass.h:222
void SetClearance(int aClearance)
Definition netclass.h:125
void Serialize(google::protobuf::Any &aContainer) const override
Serializes this object to the given Any message.
Definition netclass.cpp:162
bool operator==(const NETCLASS &other) const
Definition netclass.cpp:126
void SetPcbColorParent(NETCLASS *parent)
Definition netclass.h:206
std::optional< int > m_uViaDrill
microvia drill hole diameter
Definition netclass.h:272
void SetBusWidth(int aWidth)
Definition netclass.h:220
void SetClearanceParent(NETCLASS *parent)
Definition netclass.h:127
void SetWireWidth(int aWidth)
Definition netclass.h:212
void SetDiffPairViaGapParent(NETCLASS *parent)
Definition netclass.h:191
void SetTuningProfile(const wxString &aTuningProfile)
Definition netclass.h:251
void SetuViaDiameterParent(NETCLASS *parent)
Definition netclass.h:159
void SetSchematicColorParent(NETCLASS *parent)
Definition netclass.h:236
void SetName(const wxString &aName)
Set the name of this netclass.
Definition netclass.h:101
void SetTrackWidth(int aWidth)
Definition netclass.h:133
std::vector< NETCLASS * > m_constituents
NETCLASSes contributing to an aggregate.
Definition netclass.h:259
std::optional< int > m_diffPairGap
Definition netclass.h:275
wxString m_Name
Name of the net class.
Definition netclass.h:261
#define _(s)
This file contains miscellaneous commonly used macros and functions.
KICOMMON_API void PackColor(types::Color &aOutput, const KIGFX::COLOR4D &aInput)
KICOMMON_API int UnpackDistance(const types::Distance &aInput, const EDA_IU_SCALE &aScale)
KICOMMON_API KIGFX::COLOR4D UnpackColor(const types::Color &aInput)
KICOMMON_API void PackDistance(types::Distance &aOutput, int aInput, const EDA_IU_SCALE &aScale)
KICOMMON_API void PackVector2(types::Vector2 &aOutput, const VECTOR2I &aInput, const EDA_IU_SCALE &aScale)
const int DEFAULT_UVIA_DIAMETER
Definition netclass.cpp:39
const int DEFAULT_DIFF_PAIR_WIDTH
Definition netclass.cpp:42
const int DEFAULT_DIFF_PAIR_GAP
Definition netclass.cpp:43
const int DEFAULT_DIFF_PAIR_VIAGAP
Definition netclass.cpp:44
const int DEFAULT_LINE_STYLE
Definition netclass.cpp:49
const int DEFAULT_CLEARANCE
Definition netclass.cpp:36
const int DEFAULT_UVIA_DRILL
Definition netclass.cpp:40
const int DEFAULT_TRACK_WIDTH
Definition netclass.cpp:41
const int DEFAULT_WIRE_WIDTH
Definition netclass.cpp:46
const int DEFAULT_VIA_DRILL
Definition netclass.cpp:38
const int DEFAULT_VIA_DIAMETER
Definition netclass.cpp:37
const int DEFAULT_BUS_WIDTH
Definition netclass.cpp:47