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, 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 <algorithm>
27#include <netclass.h>
28#include <macros.h>
29#include <base_units.h>
30#include <api/api_enums.h>
31#include <api/api_utils.h>
32#include <api/common/types/project_settings.pb.h>
33
34// This will get mapped to "kicad_default" in the specctra_export.
35const char NETCLASS::Default[] = "Default";
36
37// Initial values for netclass initialization
38const int DEFAULT_CLEARANCE = pcbIUScale.mmToIU( 0.2 ); // track to track and track to pads clearance
47
50
51const int DEFAULT_LINE_STYLE = 0; // solid
52
53
54NETCLASS::NETCLASS( const wxString& aName, bool aInitWithDefaults ) : m_isDefault( false )
55{
56 m_constituents.push_back( this );
57
58 SetName( aName );
59 SetPriority( -1 );
60
61 // Colors are a special optional case - always set, but UNSPECIFIED used in place of optional
62 SetPcbColor( COLOR4D::UNSPECIFIED );
63 SetSchematicColor( COLOR4D::UNSPECIFIED );
64
65 if( aInitWithDefaults )
66 {
76
80 }
81
83}
84
85
87{
88 SetClearanceParent( this );
89 SetTrackWidthParent( this );
91 SetViaDrillParent( this );
93 SetuViaDrillParent( this );
97 SetPcbColorParent( this );
98 SetWireWidthParent( this );
99 SetBusWidthParent( this );
101 SetLineStyleParent( this );
102}
103
104
106{
107 SetPcbColor( COLOR4D::UNSPECIFIED );
108 SetSchematicColor( COLOR4D::UNSPECIFIED );
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
132void NETCLASS::Serialize( google::protobuf::Any &aContainer ) const
133{
134 using namespace kiapi::common;
135 project::NetClass nc;
136
137 nc.set_name( m_Name.ToUTF8() );
138 nc.set_priority( m_Priority );
139
140 nc.set_type( m_constituents.empty() ? project::NCT_EXPLICIT : project::NCT_IMPLICIT );
141
142 for( NETCLASS* member : m_constituents )
143 nc.add_constituents( member->GetName() );
144
145 project::NetClassBoardSettings* board = nc.mutable_board();
146
147 if( m_Clearance )
148 board->mutable_clearance()->set_value_nm( *m_Clearance );
149
150 if( m_TrackWidth )
151 board->mutable_track_width()->set_value_nm( *m_TrackWidth );
152
153 if( m_diffPairWidth )
154 board->mutable_diff_pair_track_width()->set_value_nm( *m_diffPairWidth );
155
156 if( m_diffPairGap )
157 board->mutable_diff_pair_gap()->set_value_nm( *m_diffPairGap );
158
159 if( m_diffPairViaGap )
160 board->mutable_diff_pair_via_gap()->set_value_nm( *m_diffPairViaGap );
161
162 if( m_ViaDia )
163 {
164 kiapi::board::types::PadStackLayer* layer = board->mutable_via_stack()->add_copper_layers();
165 layer->set_shape( kiapi::board::types::PSS_CIRCLE );
166 layer->set_layer( kiapi::board::types::BoardLayer::BL_F_Cu );
167 PackVector2( *layer->mutable_size(), { *m_ViaDia, *m_ViaDia } );
168 }
169
170 if( m_ViaDrill )
171 {
172 PackVector2( *board->mutable_via_stack()->mutable_drill()->mutable_diameter(),
173 { *m_ViaDrill, *m_ViaDrill } );
174 }
175
176 if( m_pcbColor != COLOR4D::UNSPECIFIED )
177 PackColor( *board->mutable_color(), m_pcbColor );
178
179 project::NetClassSchematicSettings* schematic = nc.mutable_schematic();
180
181 if( m_wireWidth )
182 schematic->mutable_wire_width()->set_value_nm( *m_wireWidth );
183
184 if( m_busWidth )
185 schematic->mutable_bus_width()->set_value_nm( *m_busWidth );
186
187 if( m_schematicColor != COLOR4D::UNSPECIFIED )
188 PackColor( *schematic->mutable_color(), m_schematicColor );
189
190 if( m_lineStyle )
191 {
192 // TODO(JE) resolve issues with moving to kicommon
193 // schematic->set_line_style( ToProtoEnum<LINE_STYLE, types::StrokeLineStyle>(
194 // static_cast<LINE_STYLE>( *m_lineStyle ) ) );
195 }
196
197 aContainer.PackFrom( nc );
198}
199
200
201bool NETCLASS::Deserialize( const google::protobuf::Any &aContainer )
202{
203 using namespace kiapi::common;
204 project::NetClass nc;
205
206 if( !aContainer.UnpackTo( &nc ) )
207 return false;
208
209 m_Name = wxString::FromUTF8( nc.name() );
210 m_Priority = nc.priority();
211
212 // We don't allow creating implicit classes directly
213 if( nc.type() == project::NCT_IMPLICIT )
214 return false;
215
217
218 if( nc.board().has_clearance() )
219 m_Clearance = nc.board().clearance().value_nm();
220
221 if( nc.board().has_track_width() )
222 m_TrackWidth = nc.board().track_width().value_nm();
223
224 if( nc.board().has_diff_pair_track_width() )
225 m_diffPairWidth = nc.board().diff_pair_track_width().value_nm();
226
227 if( nc.board().has_diff_pair_gap() )
228 m_diffPairGap = nc.board().diff_pair_gap().value_nm();
229
230 if( nc.board().has_diff_pair_via_gap() )
231 m_diffPairViaGap = nc.board().diff_pair_via_gap().value_nm();
232
233 if( nc.board().has_via_stack() )
234 {
235 if( nc.board().via_stack().copper_layers_size() > 0 )
236 m_ViaDia = nc.board().via_stack().copper_layers().at( 0 ).size().x_nm();
237
238 if( nc.board().via_stack().has_drill() )
239 m_ViaDrill = nc.board().via_stack().drill().diameter().x_nm();
240 }
241
242 if( nc.board().has_color() )
243 m_pcbColor = UnpackColor( nc.board().color() );
244
245 if( nc.schematic().has_wire_width() )
246 m_wireWidth = nc.schematic().wire_width().value_nm();
247
248 if( nc.schematic().has_bus_width() )
249 m_busWidth = nc.schematic().bus_width().value_nm();
250
251 if( nc.schematic().has_color() )
252 m_schematicColor = UnpackColor( nc.schematic().color() );
253
254 // TODO(JE) resolve issues with moving to kicommon
255 // if( nc.schematic().has_line_style() )
256 // m_lineStyle = static_cast<int>( FromProtoEnum<LINE_STYLE>( nc.schematic().line_style() ) );
257
258 return true;
259}
260
261
262const std::vector<NETCLASS*>& NETCLASS::GetConstituentNetclasses() const
263{
264 return m_constituents;
265}
266
267
268void NETCLASS::SetConstituentNetclasses( std::vector<NETCLASS*>&& constituents )
269{
270 m_constituents = std::move( constituents );
271}
272
273
274bool NETCLASS::ContainsNetclassWithName( const wxString& netclass ) const
275{
276 return std::any_of( m_constituents.begin(), m_constituents.end(),
277 [&netclass]( const NETCLASS* nc )
278 {
279 return nc->GetName() == netclass;
280 } );
281}
282
283
284const wxString NETCLASS::GetName() const
285{
286 if( m_constituents.size() == 1 )
287 return m_Name;
288
289 wxASSERT( m_constituents.size() >= 2 );
290
291 wxString name;
292
293 if( m_constituents.size() == 2 )
294 {
295 name.Printf( _( "%s and %s" ), m_constituents[0]->GetName(), m_constituents[1]->GetName() );
296 }
297 else if( m_constituents.size() == 3 )
298 {
299 name.Printf( _( "%s, %s and %s" ), m_constituents[0]->GetName(),
301 }
302 else if( m_constituents.size() > 3 )
303 {
304 name.Printf( _( "%s, %s and %d more" ), m_constituents[0]->GetName(),
305 m_constituents[1]->GetName(), static_cast<int>( m_constituents.size() - 2 ) );
306 }
307
308 return name;
309}
310
311
313{
314 if( m_constituents.size() == 1 )
315 return m_Name;
316
317 wxASSERT( m_constituents.size() >= 2 );
318
319 wxString name = m_constituents[0]->GetName();
320
321 for( std::size_t i = 1; i < m_constituents.size(); ++i )
322 {
323 name += ",";
324 name += m_constituents[i]->GetName();
325 }
326
327 return name;
328}
const char * name
Definition: DXF_plotter.cpp:57
constexpr EDA_IU_SCALE schIUScale
Definition: base_units.h:110
constexpr EDA_IU_SCALE pcbIUScale
Definition: base_units.h:108
A collection of nets and the parameters used to route or test these nets.
Definition: netclass.h:45
void SetViaDiameter(int aDia)
Definition: netclass.h:126
void SetViaDrill(int aSize)
Definition: netclass.h:134
void SetWireWidthParent(NETCLASS *parent)
Definition: netclass.h:199
COLOR4D m_pcbColor
Optional PCB color override for this netclass.
Definition: netclass.h:262
static const char Default[]
the name of the default NETCLASS
Definition: netclass.h:47
void SetuViaDrillParent(NETCLASS *parent)
Definition: netclass.h:152
void SetPriority(int aPriority)
Definition: netclass.h:232
void SetDiffPairWidthParent(NETCLASS *parent)
Definition: netclass.h:160
void SetuViaDiameter(int aSize)
Definition: netclass.h:142
void SetDiffPairWidth(int aSize)
Definition: netclass.h:158
std::optional< int > m_ViaDrill
via drill hole diameter
Definition: netclass.h:248
void SetViaDrillParent(NETCLASS *parent)
Definition: netclass.h:136
std::optional< int > m_wireWidth
Definition: netclass.h:257
std::optional< int > m_busWidth
Definition: netclass.h:258
void SetDiffPairGapParent(NETCLASS *parent)
Definition: netclass.h:168
bool ContainsNetclassWithName(const wxString &netclass) const
Determines if the given netclass name is a constituent of this (maybe aggregate) netclass.
Definition: netclass.cpp:274
void ResetParents()
Resets all parent fields to point to this netclass.
Definition: netclass.cpp:86
void SetLineStyle(int aStyle)
Definition: netclass.h:227
std::optional< int > m_TrackWidth
track width used to route nets
Definition: netclass.h:246
int m_Priority
The priority for multiple netclass resolution.
Definition: netclass.h:241
NETCLASS(const wxString &aName, bool aInitWithDefaults=true)
Create a NETCLASS instance with aName.
Definition: netclass.cpp:54
void SetSchematicColor(COLOR4D aColor)
Definition: netclass.h:220
std::optional< int > m_diffPairWidth
Definition: netclass.h:253
bool Deserialize(const google::protobuf::Any &aContainer) override
Deserializes the given protobuf message into this object.
Definition: netclass.cpp:201
const wxString GetName() const
Gets the consolidated name of this netclass (which may be an aggregate)
Definition: netclass.cpp:284
COLOR4D m_schematicColor
Definition: netclass.h:259
std::optional< int > m_diffPairViaGap
Definition: netclass.h:255
void SetConstituentNetclasses(std::vector< NETCLASS * > &&constituents)
Sets the netclasses which make up this netclass.
Definition: netclass.cpp:268
void SetLineStyleParent(NETCLASS *parent)
Definition: netclass.h:229
std::optional< int > m_ViaDia
via diameter
Definition: netclass.h:247
void SetDiffPairViaGap(int aSize)
Definition: netclass.h:174
void ResetParameters()
Resets all parameters (except Name and Description)
Definition: netclass.cpp:105
void SetTrackWidthParent(NETCLASS *parent)
Definition: netclass.h:120
std::optional< int > m_lineStyle
Definition: netclass.h:260
const std::vector< NETCLASS * > & GetConstituentNetclasses() const
Gets the netclasses which make up this netclass.
Definition: netclass.cpp:262
void SetViaDiameterParent(NETCLASS *parent)
Definition: netclass.h:128
void SetPcbColor(const COLOR4D &aColor)
Definition: netclass.h:190
std::optional< int > m_Clearance
clearance when routing
Definition: netclass.h:244
void SetuViaDrill(int aSize)
Definition: netclass.h:150
const wxString GetVariableSubstitutionName() const
Gets the name of this (maybe aggregate) netclass in a format for label variable substitutions.
Definition: netclass.cpp:312
void SetDiffPairGap(int aSize)
Definition: netclass.h:166
void SetBusWidthParent(NETCLASS *parent)
Definition: netclass.h:207
void SetClearance(int aClearance)
Definition: netclass.h:110
void Serialize(google::protobuf::Any &aContainer) const override
Serializes this object to the given Any message.
Definition: netclass.cpp:132
bool operator==(const NETCLASS &other) const
Definition: netclass.cpp:126
void SetPcbColorParent(NETCLASS *parent)
Definition: netclass.h:191
void SetBusWidth(int aWidth)
Definition: netclass.h:205
void SetClearanceParent(NETCLASS *parent)
Definition: netclass.h:112
void SetWireWidth(int aWidth)
Definition: netclass.h:197
void SetDiffPairViaGapParent(NETCLASS *parent)
Definition: netclass.h:176
void SetuViaDiameterParent(NETCLASS *parent)
Definition: netclass.h:144
void SetSchematicColorParent(NETCLASS *parent)
Definition: netclass.h:221
void SetName(const wxString &aName)
Definition: netclass.h:96
void SetTrackWidth(int aWidth)
Definition: netclass.h:118
std::vector< NETCLASS * > m_constituents
NETCLASSes contributing to an aggregate.
Definition: netclass.h:238
std::optional< int > m_diffPairGap
Definition: netclass.h:254
wxString m_Name
Name of the net class.
Definition: netclass.h:240
#define _(s)
This file contains miscellaneous commonly used macros and functions.
const int DEFAULT_UVIA_DIAMETER
Definition: netclass.cpp:41
const int DEFAULT_DIFF_PAIR_WIDTH
Definition: netclass.cpp:44
const int DEFAULT_DIFF_PAIR_GAP
Definition: netclass.cpp:45
const int DEFAULT_DIFF_PAIR_VIAGAP
Definition: netclass.cpp:46
const int DEFAULT_LINE_STYLE
Definition: netclass.cpp:51
const int DEFAULT_CLEARANCE
Definition: netclass.cpp:38
const int DEFAULT_UVIA_DRILL
Definition: netclass.cpp:42
const int DEFAULT_TRACK_WIDTH
Definition: netclass.cpp:43
const int DEFAULT_WIRE_WIDTH
Definition: netclass.cpp:48
const int DEFAULT_VIA_DRILL
Definition: netclass.cpp:40
const int DEFAULT_VIA_DIAMETER
Definition: netclass.cpp:39
const int DEFAULT_BUS_WIDTH
Definition: netclass.cpp:49
constexpr int MilsToIU(int mils) const
Definition: base_units.h:93
constexpr int mmToIU(double mm) const
Definition: base_units.h:88