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