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, see <https://www.gnu.org/licenses/>.
20 */
21
22#ifndef PCB_NETLIST_H
23#define PCB_NETLIST_H
24
25#include <boost/ptr_container/ptr_vector.hpp>
26#include <wx/arrstr.h>
27#include <json_common.h>
28#include <unordered_set>
29#include <set>
30#include <map>
31
32#include <kiid.h>
33#include <lib_id.h>
34#include <ctl_flags.h>
36#include <jumper_group.h>
37
38
39class REPORTER;
40class FOOTPRINT;
41class OUTPUTFORMATTER;
42
43
48class COMPONENT_NET
49{
50public:
52
53 COMPONENT_NET( const wxString& aPinName, const wxString& aNetName,
54 const wxString& aPinFunction, const wxString& aPinType ) :
55 m_pinName( aPinName ),
56 m_netName( aNetName ),
57 m_pinFunction( aPinFunction ),
58 m_pinType( aPinType )
59 {
60 }
61
62 const wxString& GetPinName() const { return m_pinName; }
63 const wxString& GetNetName() const { return m_netName; }
64 const wxString& GetPinFunction() const { return m_pinFunction; }
65 const wxString& GetPinType() const { return m_pinType; }
66
67 bool IsValid() const { return !m_pinName.IsEmpty(); }
68
69 bool operator <( const COMPONENT_NET& aNet ) const
70 {
71 return m_pinName < aNet.m_pinName;
72 }
73
74 int Format( OUTPUTFORMATTER* aOut, int aNestLevel, int aCtl );
75
76private:
77 wxString m_pinName;
78 wxString m_netName;
79 wxString m_pinFunction;
80 wxString m_pinType;
81};
82
83
84typedef std::vector< COMPONENT_NET > COMPONENT_NETS;
85
86
87struct NETLIST_GROUP
88{
89 wxString name;
90 KIID uuid;
92
93 // Full instance paths of the member symbols (sheet path + symbol uuid)
94 std::vector<KIID_PATH> members;
95};
96
97
99{
100 COMPONENT_VARIANT( const wxString& aName = wxEmptyString ) : m_name( aName ) {}
101
102 wxString m_name;
103 bool m_dnp = false;
104 bool m_excludedFromBOM = false;
105 bool m_excludedFromSim = false;
107
108 bool m_hasDnp = false;
112
113 nlohmann::ordered_map<wxString, wxString> m_fields;
114};
115
116
120class COMPONENT
121{
122public:
123 COMPONENT( const LIB_ID& aFPID,
124 const wxString& aReference,
125 const wxString& aValue,
126 const KIID_PATH& aPath,
127 const std::vector<KIID>& aKiids );
128
129 virtual ~COMPONENT();
130
131 void AddNet( const wxString& aPinName, const wxString& aNetName, const wxString& aPinFunction,
132 const wxString& aPinType )
133 {
134 m_nets.emplace_back( aPinName, aNetName, aPinFunction, aPinType );
135 }
136
137 unsigned GetNetCount() const { return m_nets.size(); }
138
139 const COMPONENT_NET& GetNet( unsigned aIndex ) const { return m_nets[aIndex]; }
140
141 const COMPONENT_NET& GetNet( const wxString& aPinName ) const;
142
143 void ClearNets() { m_nets.clear(); }
144
145 void SortPins() { sort( m_nets.begin(), m_nets.end() ); }
146
147 void SetName( const wxString& aName ) { m_name = aName;}
148 const wxString& GetName() const { return m_name; }
149
150 void SetLibrary( const wxString& aLibrary ) { m_library = aLibrary; }
151 const wxString& GetLibrary() const { return m_library; }
152
153 void SetReference( const wxString& aReference ) { m_reference = aReference; }
154 const wxString& GetReference() const { return m_reference; }
155
156 void SetValue( const wxString& aValue ) { m_value = aValue; }
157 const wxString& GetValue() const { return m_value; }
158
159 void SetFields( nlohmann::ordered_map<wxString, wxString> aFields )
160 {
161 m_fields = std::move( aFields );
162 }
163 const nlohmann::ordered_map<wxString, wxString>& GetFields() const { return m_fields; }
164
165 void SetProperties( std::map<wxString, wxString> aProps )
166 {
167 m_properties = std::move( aProps );
168 }
169 const std::map<wxString, wxString>& GetProperties() const { return m_properties; }
170
171 void SetFPID( const LIB_ID& aFPID ) { m_fpid = aFPID; }
172 const LIB_ID& GetFPID() const { return m_fpid; }
173
174 void SetAltFPID( const LIB_ID& aFPID ) { m_altFpid = aFPID; }
175 const LIB_ID& GetAltFPID() const { return m_altFpid; }
176
177 const KIID_PATH& GetPath() const { return m_path; }
178
179 const std::vector<KIID>& GetKIIDs() const { return m_kiids; }
180
181 void SetFootprintFilters( const wxArrayString& aFilters ) { m_footprintFilters = aFilters; }
182 const wxArrayString& GetFootprintFilters() const { return m_footprintFilters; }
183
184 void SetPinCount( int aPinCount ) { m_pinCount = aPinCount; }
185 int GetPinCount() const { return m_pinCount; }
186
187 FOOTPRINT* GetFootprint( bool aRelease = false );
188
189 void SetFootprint( FOOTPRINT* aFootprint );
190
191 bool IsLibSource( const wxString& aLibrary, const wxString& aName ) const
192 {
193 return aLibrary == m_library && aName == m_name;
194 }
195
196 void Format( OUTPUTFORMATTER* aOut, int aNestLevel, int aCtl );
197
198 void SetHumanReadablePath( const wxString& aPath ) { m_humanReadablePath = aPath; }
199 const wxString& GetHumanReadablePath() const { return m_humanReadablePath; }
200
201 void SetComponentClassNames( const std::unordered_set<wxString>& aClassNames )
202 {
203 m_componentClassNames = aClassNames;
204 }
205
206 std::unordered_set<wxString>& GetComponentClassNames() { return m_componentClassNames; }
207
210
213
214 NETLIST_GROUP* GetGroup() const { return m_group; }
215 void SetGroup( NETLIST_GROUP* aGroup ) { m_group = aGroup; }
216
217 // Unit info for multi-unit symbols
218 struct UNIT_INFO
219 {
220 wxString m_unitName; // e.g. A
221 std::vector<wxString> m_pins; // pin numbers in this unit
222 };
223
224 void SetUnitInfo( const std::vector<UNIT_INFO>& aUnits ) { m_units = aUnits; }
225 const std::vector<UNIT_INFO>& GetUnitInfo() const { return m_units; }
226
228 const COMPONENT_VARIANT* GetVariant( const wxString& aVariantName ) const;
229 COMPONENT_VARIANT* GetVariant( const wxString& aVariantName );
230 void AddVariant( const COMPONENT_VARIANT& aVariant );
231
232private:
233 std::vector<COMPONENT_NET> m_nets;
234
235 wxArrayString m_footprintFilters;
236 int m_pinCount;
237 wxString m_reference;
238 wxString m_value;
239
240 // human-readable hierarchical sheet path (e.g. /root/block0/sheet1)
241 wxString m_humanReadablePath;
242
245
247 std::vector<KIID> m_kiids;
248
250 wxString m_name;
251
253 wxString m_library;
254
257
262
264 std::unique_ptr<FOOTPRINT> m_footprint;
265
267 std::map<wxString, wxString> m_properties;
268
270 nlohmann::ordered_map<wxString, wxString> m_fields;
271
273 std::unordered_set<wxString> m_componentClassNames;
274
277
281
284
286
287 // Unit information parsed from the netlist (optional)
288 std::vector<UNIT_INFO> m_units;
289
291};
292
293
298class NETLIST
299{
300public:
302 m_findByTimeStamp( false ),
303 m_replaceFootprints( false )
304 {
305 }
306
310 bool IsEmpty() const { return m_components.empty(); }
311
315 void Clear()
316 {
317 m_components.clear();
318 m_groups.clear();
319 m_netSignals.clear();
320 m_netChainTerminals.clear();
321 m_netChainNetClasses.clear();
322 m_netChainColors.clear();
323 m_signalChainClasses.clear();
324 m_variantNames.clear();
325 m_variantDescriptions.clear();
326 }
327
331 unsigned GetCount() const { return m_components.size(); }
332
339 COMPONENT* GetComponent( unsigned aIndex ) { return &m_components[ aIndex ]; }
340
349 void AddComponent( COMPONENT* aComponent );
350
357 void AddGroup( NETLIST_GROUP* aGroup );
358
359 void SetNetChainFor( const wxString& aNet, const wxString& aNetChain )
360 {
361 m_netSignals[aNet] = aNetChain;
362 }
363
364 void AddSignalTerminalPin( const wxString& aNetChain, const wxString& aRef, const wxString& aPin )
365 {
366 m_netChainTerminals[aNetChain].emplace_back( aRef, aPin );
367 }
368
369 const std::vector<std::pair<wxString, wxString>>& GetNetChainTerminals( const wxString& aNetChain ) const
370 {
371 static const std::vector<std::pair<wxString, wxString>> empty;
372 auto it = m_netChainTerminals.find( aNetChain );
373 return it != m_netChainTerminals.end() ? it->second : empty;
374 }
375
376 const std::map<wxString, std::vector<std::pair<wxString, wxString>>>& GetNetChainTerminalPins() const
377 {
378 return m_netChainTerminals;
379 }
380
381 wxString GetNetChainFor( const wxString& aNet ) const
382 {
383 auto it = m_netSignals.find( aNet );
384 return it != m_netSignals.end() ? it->second : wxString();
385 }
386
387 void SetSignalNetClass( const wxString& aNetChain, const wxString& aNetClass )
388 {
389 m_netChainNetClasses[aNetChain] = aNetClass;
390 }
391
392 wxString GetSignalNetClass( const wxString& aNetChain ) const
393 {
394 auto it = m_netChainNetClasses.find( aNetChain );
395 return it != m_netChainNetClasses.end() ? it->second : wxString();
396 }
397
398 const std::map<wxString, wxString>& GetNetChainNetClasses() const
399 {
401 }
402
404 void SetSignalColor( const wxString& aNetChain, const wxString& aColor )
405 {
406 m_netChainColors[aNetChain] = aColor;
407 }
408
409 wxString GetSignalColor( const wxString& aNetChain ) const
410 {
411 auto it = m_netChainColors.find( aNetChain );
412 return it != m_netChainColors.end() ? it->second : wxString();
413 }
414
415 const std::map<wxString, wxString>& GetNetChainColors() const
416 {
417 return m_netChainColors;
418 }
419
421 void SetSignalChainClass( const wxString& aNetChain, const wxString& aClass )
422 {
423 m_signalChainClasses[aNetChain] = aClass;
424 }
425
426 wxString GetSignalChainClass( const wxString& aNetChain ) const
427 {
428 auto it = m_signalChainClasses.find( aNetChain );
429 return it != m_signalChainClasses.end() ? it->second : wxString();
430 }
431
432 const std::map<wxString, wxString>& GetSignalChainClasses() const
433 {
435 }
436
445
451
458 COMPONENT* GetComponentByReference( const wxString& aReference );
459
467
475
478
479 void SetFindByTimeStamp( bool aFindByTimeStamp ) { m_findByTimeStamp = aFindByTimeStamp; }
480 bool IsFindByTimeStamp() const { return m_findByTimeStamp; }
481
482 void SetReplaceFootprints( bool aReplace ) { m_replaceFootprints = aReplace; }
484
489
490 void Format( const char* aDocName, OUTPUTFORMATTER* aOut, int aNestLevel, int aCtl = 0 );
491
492#define CTL_FOR_CVPCB (CTL_OMIT_NETS | CTL_OMIT_FILTERS | CTL_OMIT_EXTRA)
493
495 {
496 Format( "cvpcb_netlist", aOut, 0, CTL_FOR_CVPCB );
497 }
498
502 const std::vector<wxString>& GetVariantNames() const { return m_variantNames; }
503
507 void AddVariant( const wxString& aName, const wxString& aDescription = wxEmptyString )
508 {
509 if( aName.IsEmpty() )
510 return;
511
512 for( const wxString& existingName : m_variantNames )
513 {
514 if( existingName.CmpNoCase( aName ) == 0 )
515 {
516 if( aDescription.IsEmpty() )
517 m_variantDescriptions.erase( existingName );
518 else
519 m_variantDescriptions[existingName] = aDescription;
520
521 return;
522 }
523 }
524
525 m_variantNames.push_back( aName );
526
527 if( aDescription.IsEmpty() )
528 return;
529
530 m_variantDescriptions[aName] = aDescription;
531 }
532
536 wxString GetVariantDescription( const wxString& aVariantName ) const
537 {
538 wxString actualName;
539
540 for( const wxString& existingName : m_variantNames )
541 {
542 if( existingName.CmpNoCase( aVariantName ) == 0 )
543 {
544 actualName = existingName;
545 break;
546 }
547 }
548
549 if( !actualName.IsEmpty() )
550 {
551 auto it = m_variantDescriptions.find( actualName );
552
553 if( it != m_variantDescriptions.end() )
554 return it->second;
555 }
556
557 return wxEmptyString;
558 }
559
563 const std::map<wxString, wxString>& GetVariantDescriptions() const
564 {
566 }
567
568private:
569 boost::ptr_vector<COMPONENT> m_components; // Components found in the netlist.
570 boost::ptr_vector<NETLIST_GROUP> m_groups; // Groups found in the netlist.
571 std::map<wxString, wxString> m_netSignals;
572 std::map<wxString, std::vector<std::pair<wxString, wxString>>> m_netChainTerminals;
573 std::map<wxString, wxString> m_netChainNetClasses;
574 std::map<wxString, wxString> m_netChainColors;
575 std::map<wxString, wxString> m_signalChainClasses;
576
577 std::vector<wxString> m_variantNames; // Variant names in order.
578 std::map<wxString, wxString> m_variantDescriptions; // Variant descriptions.
579
580 bool m_findByTimeStamp; // Associate components by KIID (or refdes if false)
581 bool m_replaceFootprints; // Update footprints to match footprints defined in netlist
582};
583
584
585#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:63
COMPONENT_NET(const wxString &aPinName, const wxString &aNetName, const wxString &aPinFunction, const wxString &aPinType)
Definition pcb_netlist.h:53
int Format(OUTPUTFORMATTER *aOut, int aNestLevel, int aCtl)
bool operator<(const COMPONENT_NET &aNet) const
bool IsValid() const
Definition pcb_netlist.h:67
const wxString & GetPinFunction() const
Definition pcb_netlist.h:64
const wxString & GetPinName() const
Definition pcb_netlist.h:62
const wxString & GetPinType() const
Definition pcb_netlist.h:65
Store all of the related component information found in a netlist.
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)
JUMPER_GROUP_SET m_jumperPadGroups
Jumper pad groups for this footprint.
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
unsigned GetNetCount() const
std::unordered_set< wxString > & GetComponentClassNames()
JUMPER_GROUP_SET & JumperPadGroups()
const wxString & GetLibrary() const
void SetComponentClassNames(const std::unordered_set< wxString > &aClassNames)
const JUMPER_GROUP_SET & JumperPadGroups() const
const wxString & GetName() const
void SetName(const wxString &aName)
void SetReference(const wxString &aReference)
Represents an (ordered) list of JUMPER_GROUP objects.
Definition kiid.h:46
A logical library item identifier and consists of various portions much like a URI.
Definition lib_id.h:45
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.
boost::ptr_vector< COMPONENT > m_components
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".
boost::ptr_vector< NETLIST_GROUP > m_groups
An interface used to output 8 bit text in a convenient way.
Definition richio.h:294
A pure virtual class used to derive REPORTER objects from.
Definition reporter.h:73
std::vector< COMPONENT_NET > COMPONENT_NETS
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