KiCad PCB EDA Suite
Loading...
Searching...
No Matches
pcb_netlist.h
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) 2012 Jean-Pierre Charras.
5 * Copyright (C) 2013-2016 Wayne Stambaugh <[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#ifndef PCB_NETLIST_H
27#define PCB_NETLIST_H
28
29#include <boost/ptr_container/ptr_vector.hpp>
30#include <wx/arrstr.h>
31#include <json_common.h>
32#include <unordered_set>
33#include <set>
34#include <map>
35
36#include <kiid.h>
37#include <lib_id.h>
38#include <ctl_flags.h>
40
41
42class REPORTER;
43class FOOTPRINT;
44class OUTPUTFORMATTER;
45
46
51class COMPONENT_NET
52{
53public:
55
56 COMPONENT_NET( const wxString& aPinName, const wxString& aNetName,
57 const wxString& aPinFunction, const wxString& aPinType ) :
58 m_pinName( aPinName ),
59 m_netName( aNetName ),
60 m_pinFunction( aPinFunction ),
61 m_pinType( aPinType )
62 {
63 }
64
65 const wxString& GetPinName() const { return m_pinName; }
66 const wxString& GetNetName() const { return m_netName; }
67 const wxString& GetPinFunction() const { return m_pinFunction; }
68 const wxString& GetPinType() const { return m_pinType; }
69
70 bool IsValid() const { return !m_pinName.IsEmpty(); }
71
72 bool operator <( const COMPONENT_NET& aNet ) const
73 {
74 return m_pinName < aNet.m_pinName;
75 }
76
77 int Format( OUTPUTFORMATTER* aOut, int aNestLevel, int aCtl );
78
79private:
80 wxString m_pinName;
81 wxString m_netName;
82 wxString m_pinFunction;
83 wxString m_pinType;
84};
85
86
87typedef std::vector< COMPONENT_NET > COMPONENT_NETS;
88
89
90struct NETLIST_GROUP
91{
92 wxString name;
93 KIID uuid;
95 std::vector<KIID> members;
96};
97
98typedef boost::ptr_vector< NETLIST_GROUP > NETLIST_GROUPS;
99
100
102{
103 COMPONENT_VARIANT( const wxString& aName = wxEmptyString ) : m_name( aName ) {}
104
105 wxString m_name;
106 bool m_dnp = false;
107 bool m_excludedFromBOM = false;
108 bool m_excludedFromSim = false;
110
111 bool m_hasDnp = false;
115
116 nlohmann::ordered_map<wxString, wxString> m_fields;
117};
118
119
123class COMPONENT
124{
125public:
126 COMPONENT( const LIB_ID& aFPID,
127 const wxString& aReference,
128 const wxString& aValue,
129 const KIID_PATH& aPath,
130 const std::vector<KIID>& aKiids );
131
132 virtual ~COMPONENT();
133
134 void AddNet( const wxString& aPinName, const wxString& aNetName, const wxString& aPinFunction,
135 const wxString& aPinType )
136 {
137 m_nets.emplace_back( aPinName, aNetName, aPinFunction, aPinType );
138 }
139
140 unsigned GetNetCount() const { return m_nets.size(); }
141
142 const COMPONENT_NET& GetNet( unsigned aIndex ) const { return m_nets[aIndex]; }
143
144 const COMPONENT_NET& GetNet( const wxString& aPinName ) const;
145
146 void ClearNets() { m_nets.clear(); }
147
148 void SortPins() { sort( m_nets.begin(), m_nets.end() ); }
149
150 void SetName( const wxString& aName ) { m_name = aName;}
151 const wxString& GetName() const { return m_name; }
152
153 void SetLibrary( const wxString& aLibrary ) { m_library = aLibrary; }
154 const wxString& GetLibrary() const { return m_library; }
155
156 void SetReference( const wxString& aReference ) { m_reference = aReference; }
157 const wxString& GetReference() const { return m_reference; }
158
159 void SetValue( const wxString& aValue ) { m_value = aValue; }
160 const wxString& GetValue() const { return m_value; }
161
162 void SetFields( nlohmann::ordered_map<wxString, wxString> aFields )
163 {
164 m_fields = std::move( aFields );
165 }
166 const nlohmann::ordered_map<wxString, wxString>& GetFields() const { return m_fields; }
167
168 void SetProperties( std::map<wxString, wxString> aProps )
169 {
170 m_properties = std::move( aProps );
171 }
172 const std::map<wxString, wxString>& GetProperties() const { return m_properties; }
173
174 void SetFPID( const LIB_ID& aFPID ) { m_fpid = aFPID; }
175 const LIB_ID& GetFPID() const { return m_fpid; }
176
177 void SetAltFPID( const LIB_ID& aFPID ) { m_altFpid = aFPID; }
178 const LIB_ID& GetAltFPID() const { return m_altFpid; }
179
180 const KIID_PATH& GetPath() const { return m_path; }
181
182 const std::vector<KIID>& GetKIIDs() const { return m_kiids; }
183
184 void SetFootprintFilters( const wxArrayString& aFilters ) { m_footprintFilters = aFilters; }
185 const wxArrayString& GetFootprintFilters() const { return m_footprintFilters; }
186
187 void SetPinCount( int aPinCount ) { m_pinCount = aPinCount; }
188 int GetPinCount() const { return m_pinCount; }
189
190 FOOTPRINT* GetFootprint( bool aRelease = false );
191
192 void SetFootprint( FOOTPRINT* aFootprint );
193
194 bool IsLibSource( const wxString& aLibrary, const wxString& aName ) const
195 {
196 return aLibrary == m_library && aName == m_name;
197 }
198
199 void Format( OUTPUTFORMATTER* aOut, int aNestLevel, int aCtl );
200
201 void SetHumanReadablePath( const wxString& aPath ) { m_humanReadablePath = aPath; }
202 const wxString& GetHumanReadablePath() const { return m_humanReadablePath; }
203
204 void SetComponentClassNames( const std::unordered_set<wxString>& aClassNames )
205 {
206 m_componentClassNames = aClassNames;
207 }
208
209 std::unordered_set<wxString>& GetComponentClassNames() { return m_componentClassNames; }
210
213
214 std::vector<std::set<wxString>>& JumperPadGroups() { return m_jumperPadGroups; }
215 const std::vector<std::set<wxString>>& JumperPadGroups() const { return m_jumperPadGroups; }
216
217 NETLIST_GROUP* GetGroup() const { return m_group; }
218 void SetGroup( NETLIST_GROUP* aGroup ) { m_group = aGroup; }
219
220 // Unit info for multi-unit symbols
221 struct UNIT_INFO
222 {
223 wxString m_unitName; // e.g. A
224 std::vector<wxString> m_pins; // pin numbers in this unit
225 };
226
227 void SetUnitInfo( const std::vector<UNIT_INFO>& aUnits ) { m_units = aUnits; }
228 const std::vector<UNIT_INFO>& GetUnitInfo() const { return m_units; }
229
231 const COMPONENT_VARIANT* GetVariant( const wxString& aVariantName ) const;
232 COMPONENT_VARIANT* GetVariant( const wxString& aVariantName );
233 void AddVariant( const COMPONENT_VARIANT& aVariant );
234
235private:
236 std::vector<COMPONENT_NET> m_nets;
237
238 wxArrayString m_footprintFilters;
239 int m_pinCount;
240 wxString m_reference;
241 wxString m_value;
242
243 // human-readable hierarchical sheet path (e.g. /root/block0/sheet1)
244 wxString m_humanReadablePath;
245
248
250 std::vector<KIID> m_kiids;
251
253 wxString m_name;
254
256 wxString m_library;
257
260
265
267 std::unique_ptr<FOOTPRINT> m_footprint;
268
270 std::map<wxString, wxString> m_properties;
271
273 nlohmann::ordered_map<wxString, wxString> m_fields;
274
276 std::unordered_set<wxString> m_componentClassNames;
277
279 std::vector<std::set<wxString>> m_jumperPadGroups;
280
284
287
289
290 // Unit information parsed from the netlist (optional)
291 std::vector<UNIT_INFO> m_units;
292
294};
295
296
297typedef boost::ptr_vector< COMPONENT > COMPONENTS;
298
299
304class NETLIST
305{
306public:
308 m_findByTimeStamp( false ),
309 m_replaceFootprints( false )
310 {
311 }
312
316 bool IsEmpty() const { return m_components.empty(); }
317
321 void Clear() { m_components.clear(); }
322
326 unsigned GetCount() const { return m_components.size(); }
327
334 COMPONENT* GetComponent( unsigned aIndex ) { return &m_components[ aIndex ]; }
335
344 void AddComponent( COMPONENT* aComponent );
345
352 void AddGroup( NETLIST_GROUP* aGroup );
353
354 void SetNetChainFor( const wxString& aNet, const wxString& aNetChain )
355 {
356 m_netSignals[aNet] = aNetChain;
357 }
358
359 void AddSignalTerminalPin( const wxString& aNetChain, const wxString& aRef, const wxString& aPin )
360 {
361 m_netChainTerminals[aNetChain].emplace_back( aRef, aPin );
362 }
363
364 const std::vector<std::pair<wxString, wxString>>& GetNetChainTerminals( const wxString& aNetChain ) const
365 {
366 static const std::vector<std::pair<wxString, wxString>> empty;
367 auto it = m_netChainTerminals.find( aNetChain );
368 return it != m_netChainTerminals.end() ? it->second : empty;
369 }
370
371 const std::map<wxString, std::vector<std::pair<wxString, wxString>>>& GetNetChainTerminalPins() const
372 {
373 return m_netChainTerminals;
374 }
375
376 wxString GetNetChainFor( const wxString& aNet ) const
377 {
378 auto it = m_netSignals.find( aNet );
379 return it != m_netSignals.end() ? it->second : wxString();
380 }
381
382 void SetSignalNetClass( const wxString& aNetChain, const wxString& aNetClass )
383 {
384 m_netChainNetClasses[aNetChain] = aNetClass;
385 }
386
387 wxString GetSignalNetClass( const wxString& aNetChain ) const
388 {
389 auto it = m_netChainNetClasses.find( aNetChain );
390 return it != m_netChainNetClasses.end() ? it->second : wxString();
391 }
392
393 const std::map<wxString, wxString>& GetNetChainNetClasses() const
394 {
396 }
397
399 void SetSignalColor( const wxString& aNetChain, const wxString& aColor )
400 {
401 m_netChainColors[aNetChain] = aColor;
402 }
403
404 wxString GetSignalColor( const wxString& aNetChain ) const
405 {
406 auto it = m_netChainColors.find( aNetChain );
407 return it != m_netChainColors.end() ? it->second : wxString();
408 }
409
410 const std::map<wxString, wxString>& GetNetChainColors() const
411 {
412 return m_netChainColors;
413 }
414
416 void SetSignalChainClass( const wxString& aNetChain, const wxString& aClass )
417 {
418 m_signalChainClasses[aNetChain] = aClass;
419 }
420
421 wxString GetSignalChainClass( const wxString& aNetChain ) const
422 {
423 auto it = m_signalChainClasses.find( aNetChain );
424 return it != m_signalChainClasses.end() ? it->second : wxString();
425 }
426
427 const std::map<wxString, wxString>& GetSignalChainClasses() const
428 {
430 }
431
440
446
453 COMPONENT* GetComponentByReference( const wxString& aReference );
454
462
470
473
474 void SetFindByTimeStamp( bool aFindByTimeStamp ) { m_findByTimeStamp = aFindByTimeStamp; }
475 bool IsFindByTimeStamp() const { return m_findByTimeStamp; }
476
477 void SetReplaceFootprints( bool aReplace ) { m_replaceFootprints = aReplace; }
479
484
485 void Format( const char* aDocName, OUTPUTFORMATTER* aOut, int aNestLevel, int aCtl = 0 );
486
487#define CTL_FOR_CVPCB (CTL_OMIT_NETS | CTL_OMIT_FILTERS | CTL_OMIT_EXTRA)
488
490 {
491 Format( "cvpcb_netlist", aOut, 0, CTL_FOR_CVPCB );
492 }
493
497 const std::vector<wxString>& GetVariantNames() const { return m_variantNames; }
498
502 void AddVariant( const wxString& aName, const wxString& aDescription = wxEmptyString )
503 {
504 if( aName.IsEmpty() )
505 return;
506
507 for( const wxString& existingName : m_variantNames )
508 {
509 if( existingName.CmpNoCase( aName ) == 0 )
510 {
511 if( aDescription.IsEmpty() )
512 m_variantDescriptions.erase( existingName );
513 else
514 m_variantDescriptions[existingName] = aDescription;
515
516 return;
517 }
518 }
519
520 m_variantNames.push_back( aName );
521
522 if( aDescription.IsEmpty() )
523 return;
524
525 m_variantDescriptions[aName] = aDescription;
526 }
527
531 wxString GetVariantDescription( const wxString& aVariantName ) const
532 {
533 wxString actualName;
534
535 for( const wxString& existingName : m_variantNames )
536 {
537 if( existingName.CmpNoCase( aVariantName ) == 0 )
538 {
539 actualName = existingName;
540 break;
541 }
542 }
543
544 if( !actualName.IsEmpty() )
545 {
546 auto it = m_variantDescriptions.find( actualName );
547
548 if( it != m_variantDescriptions.end() )
549 return it->second;
550 }
551
552 return wxEmptyString;
553 }
554
558 const std::map<wxString, wxString>& GetVariantDescriptions() const
559 {
561 }
562
563private:
564 COMPONENTS m_components; // Components found in the netlist.
565 NETLIST_GROUPS m_groups; // Groups found in the netlist.
566 std::map<wxString, wxString> m_netSignals;
567 std::map<wxString, std::vector<std::pair<wxString, wxString>>> m_netChainTerminals;
568 std::map<wxString, wxString> m_netChainNetClasses;
569 std::map<wxString, wxString> m_netChainColors;
570 std::map<wxString, wxString> m_signalChainClasses;
571
572 std::vector<wxString> m_variantNames; // Variant names in order.
573 std::map<wxString, wxString> m_variantDescriptions; // Variant descriptions.
574
575 bool m_findByTimeStamp; // Associate components by KIID (or refdes if false)
576 bool m_replaceFootprints; // Update footprints to match footprints defined in netlist
577};
578
579
580#endif // PCB_NETLIST_H
const char * name
std::map< wxString, ValueType, DETAIL::CASE_INSENSITIVE_COMPARER > CASE_INSENSITIVE_MAP
Used to store the component pin name to net name (and pin function) associations stored in a netlist.
const wxString & GetNetName() const
Definition pcb_netlist.h:66
COMPONENT_NET(const wxString &aPinName, const wxString &aNetName, const wxString &aPinFunction, const wxString &aPinType)
Definition pcb_netlist.h:56
int Format(OUTPUTFORMATTER *aOut, int aNestLevel, int aCtl)
bool operator<(const COMPONENT_NET &aNet) const
bool IsValid() const
Definition pcb_netlist.h:70
const wxString & GetPinFunction() const
Definition pcb_netlist.h:67
const wxString & GetPinName() const
Definition pcb_netlist.h:65
const wxString & GetPinType() const
Definition pcb_netlist.h:68
Store all of the related component information found in a netlist.
const std::vector< std::set< wxString > > & JumperPadGroups() const
const std::vector< UNIT_INFO > & GetUnitInfo() const
const wxString & GetHumanReadablePath() const
const COMPONENT_NET & GetNet(unsigned aIndex) const
const KIID_PATH & GetPath() const
void SetValue(const wxString &aValue)
std::vector< COMPONENT_NET > m_nets
list of nets shared by the component pins
void SetLibrary(const wxString &aLibrary)
std::map< wxString, wxString > m_properties
Component-specific properties found in the netlist.
wxArrayString m_footprintFilters
const wxString & GetReference() const
virtual ~COMPONENT()
void AddVariant(const COMPONENT_VARIANT &aVariant)
CASE_INSENSITIVE_MAP< COMPONENT_VARIANT > m_variants
void SetAltFPID(const LIB_ID &aFPID)
void Format(OUTPUTFORMATTER *aOut, int aNestLevel, int aCtl)
const COMPONENT_VARIANT * GetVariant(const wxString &aVariantName) const
const wxString & GetValue() const
void SetProperties(std::map< wxString, wxString > aProps)
KIID_PATH m_path
A fully specified path to the component (but not the component: [ sheetUUID, sheetUUID,...
const nlohmann::ordered_map< wxString, wxString > & GetFields() const
NETLIST_GROUP * m_group
Group membership for this footprint. Nullptr if none.
bool m_duplicatePadNumbersAreJumpers
Flag that this footprint should automatically treat sets of two or more pads with the same number as ...
const std::map< wxString, wxString > & GetProperties() const
nlohmann::ordered_map< wxString, wxString > m_fields
Component-specific user fields found in the netlist.
int GetPinCount() const
void SetFPID(const LIB_ID &aFPID)
void SortPins()
void SetPinCount(int aPinCount)
void SetUnitInfo(const std::vector< UNIT_INFO > &aUnits)
const COMPONENT_NET & GetNet(const wxString &aPinName) const
std::unordered_set< wxString > m_componentClassNames
Component classes for this footprint.
void ClearNets()
wxString m_name
The name of the component in m_library used when it was placed on the schematic.
LIB_ID m_altFpid
The alt LIB_ID of the footprint, when there are 2 different assigned footprints, One from the netlist...
const LIB_ID & GetAltFPID() const
void SetFootprint(FOOTPRINT *aFootprint)
bool IsLibSource(const wxString &aLibrary, const wxString &aName) const
const CASE_INSENSITIVE_MAP< COMPONENT_VARIANT > & GetVariants() const
std::vector< KIID > m_kiids
A vector of possible KIIDs corresponding to all units in a symbol.
FOOTPRINT * GetFootprint(bool aRelease=false)
NETLIST_GROUP * GetGroup() const
void SetFootprintFilters(const wxArrayString &aFilters)
void AddNet(const wxString &aPinName, const wxString &aNetName, const wxString &aPinFunction, const wxString &aPinType)
wxString m_library
The name of the component library where m_name was found.
LIB_ID m_fpid
The LIB_ID of the footprint assigned to the component.
const wxArrayString & GetFootprintFilters() const
static COMPONENT_NET m_emptyNet
void SetFields(nlohmann::ordered_map< wxString, wxString > aFields)
const std::vector< KIID > & GetKIIDs() const
COMPONENT(const LIB_ID &aFPID, const wxString &aReference, const wxString &aValue, const KIID_PATH &aPath, const std::vector< KIID > &aKiids)
void SetHumanReadablePath(const wxString &aPath)
std::unique_ptr< FOOTPRINT > m_footprint
The FOOTPRINT loaded for #m_FPID.
void SetDuplicatePadNumbersAreJumpers(bool aEnabled)
bool GetDuplicatePadNumbersAreJumpers() const
const LIB_ID & GetFPID() const
void SetGroup(NETLIST_GROUP *aGroup)
std::vector< UNIT_INFO > m_units
std::vector< std::set< wxString > > m_jumperPadGroups
Jumper pad groups for this footprint.
unsigned GetNetCount() const
std::unordered_set< wxString > & GetComponentClassNames()
const wxString & GetLibrary() const
void SetComponentClassNames(const std::unordered_set< wxString > &aClassNames)
const wxString & GetName() const
std::vector< std::set< wxString > > & JumperPadGroups()
void SetName(const wxString &aName)
void SetReference(const wxString &aReference)
Definition kiid.h:48
A logical library item identifier and consists of various portions much like a URI.
Definition lib_id.h:49
Store information read from a netlist along with the flags used to update the NETLIST in the BOARD.
void Format(const char *aDocName, OUTPUTFORMATTER *aOut, int aNestLevel, int aCtl=0)
wxString GetSignalColor(const wxString &aNetChain) const
wxString GetSignalChainClass(const wxString &aNetChain) const
bool GetReplaceFootprints() const
const std::map< wxString, std::vector< std::pair< wxString, wxString > > > & GetNetChainTerminalPins() const
void AddVariant(const wxString &aName, const wxString &aDescription=wxEmptyString)
Add a variant name to the netlist.
void SetReplaceFootprints(bool aReplace)
void SortByFPID()
void SetSignalChainClass(const wxString &aNetChain, const wxString &aClass)
Net chain class assignment carried through the netlist for downstream tools.
std::vector< wxString > m_variantNames
const std::vector< wxString > & GetVariantNames() const
unsigned GetCount() const
void AddGroup(NETLIST_GROUP *aGroup)
void AddComponent(COMPONENT *aComponent)
Add aComponent to the NETLIST.
bool IsEmpty() const
COMPONENT * GetComponentByPath(const KIID_PATH &aPath)
Return a COMPONENT by aPath.
COMPONENT * GetComponentByReference(const wxString &aReference)
Return a COMPONENT by aReference.
void Clear()
Remove all components from the netlist.
const std::map< wxString, wxString > & GetNetChainNetClasses() const
COMPONENT * GetComponentByUuid(const KIID &aUuid)
Return a COMPONENT by aUuid.
const std::map< wxString, wxString > & GetSignalChainClasses() const
void SetFindByTimeStamp(bool aFindByTimeStamp)
void AddSignalTerminalPin(const wxString &aNetChain, const wxString &aRef, const wxString &aPin)
std::map< wxString, wxString > m_variantDescriptions
NETLIST_GROUP * GetGroupByUuid(const KIID &aUuid)
Return a NETLIST_GROUP by aUuid.
const std::map< wxString, wxString > & GetNetChainColors() const
COMPONENT * GetComponent(unsigned aIndex)
Return the COMPONENT at aIndex.
std::map< wxString, wxString > m_netChainColors
void ApplyGroupMembership()
After groups and components are parsed, apply the group memberships to the internal components based ...
wxString GetNetChainFor(const wxString &aNet) const
const std::map< wxString, wxString > & GetVariantDescriptions() const
std::map< wxString, std::vector< std::pair< wxString, wxString > > > m_netChainTerminals
std::map< wxString, wxString > m_netChainNetClasses
wxString GetSignalNetClass(const wxString &aNetChain) const
wxString GetVariantDescription(const wxString &aVariantName) const
bool IsFindByTimeStamp() const
std::map< wxString, wxString > m_netSignals
void SetSignalNetClass(const wxString &aNetChain, const wxString &aNetClass)
void SortByReference()
const std::vector< std::pair< wxString, wxString > > & GetNetChainTerminals(const wxString &aNetChain) const
void SetNetChainFor(const wxString &aNet, const wxString &aNetChain)
std::map< wxString, wxString > m_signalChainClasses
void FormatCvpcbNetlist(OUTPUTFORMATTER *aOut)
bool AnyFootprintsLinked() const
void SetSignalColor(const wxString &aNetChain, const wxString &aColor)
Stored as a #RRGGBB or #RRGGBBAA string; empty means "no override".
An interface used to output 8 bit text in a convenient way.
Definition richio.h:295
A pure virtual class used to derive REPORTER objects from.
Definition reporter.h:75
std::vector< COMPONENT_NET > COMPONENT_NETS
boost::ptr_vector< COMPONENT > COMPONENTS
boost::ptr_vector< NETLIST_GROUP > NETLIST_GROUPS
static bool empty(const wxTextEntryBase *aCtrl)
#define CTL_FOR_CVPCB
COMPONENT_VARIANT(const wxString &aName=wxEmptyString)
bool m_hasExcludedFromPosFiles
nlohmann::ordered_map< wxString, wxString > m_fields
std::vector< KIID > members