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
37
38class REPORTER;
39class FOOTPRINT;
40class OUTPUTFORMATTER;
41
42
47class COMPONENT_NET
48{
49public:
51
52 COMPONENT_NET( const wxString& aPinName, const wxString& aNetName,
53 const wxString& aPinFunction, const wxString& aPinType ) :
54 m_pinName( aPinName ),
55 m_netName( aNetName ),
56 m_pinFunction( aPinFunction ),
57 m_pinType( aPinType )
58 {
59 }
60
61 const wxString& GetPinName() const { return m_pinName; }
62 const wxString& GetNetName() const { return m_netName; }
63 const wxString& GetPinFunction() const { return m_pinFunction; }
64 const wxString& GetPinType() const { return m_pinType; }
65
66 bool IsValid() const { return !m_pinName.IsEmpty(); }
67
68 bool operator <( const COMPONENT_NET& aNet ) const
69 {
70 return m_pinName < aNet.m_pinName;
71 }
72
73 int Format( OUTPUTFORMATTER* aOut, int aNestLevel, int aCtl );
74
75private:
76 wxString m_pinName;
77 wxString m_netName;
78 wxString m_pinFunction;
79 wxString m_pinType;
80};
81
82
83typedef std::vector< COMPONENT_NET > COMPONENT_NETS;
84
85
86struct NETLIST_GROUP
87{
88 wxString name;
89 KIID uuid;
91 std::vector<KIID> members;
92};
93
94typedef boost::ptr_vector< NETLIST_GROUP > NETLIST_GROUPS;
95
96
98{
99 COMPONENT_VARIANT( const wxString& aName = wxEmptyString ) : m_name( aName ) {}
100
101 wxString m_name;
102 bool m_dnp = false;
103 bool m_excludedFromBOM = false;
104 bool m_excludedFromSim = false;
106
107 bool m_hasDnp = false;
111
112 nlohmann::ordered_map<wxString, wxString> m_fields;
113};
114
115
119class COMPONENT
120{
121public:
122 COMPONENT( const LIB_ID& aFPID,
123 const wxString& aReference,
124 const wxString& aValue,
125 const KIID_PATH& aPath,
126 const std::vector<KIID>& aKiids );
127
128 virtual ~COMPONENT();
129
130 void AddNet( const wxString& aPinName, const wxString& aNetName, const wxString& aPinFunction,
131 const wxString& aPinType )
132 {
133 m_nets.emplace_back( aPinName, aNetName, aPinFunction, aPinType );
134 }
135
136 unsigned GetNetCount() const { return m_nets.size(); }
137
138 const COMPONENT_NET& GetNet( unsigned aIndex ) const { return m_nets[aIndex]; }
139
140 const COMPONENT_NET& GetNet( const wxString& aPinName ) const;
141
142 void ClearNets() { m_nets.clear(); }
143
144 void SortPins() { sort( m_nets.begin(), m_nets.end() ); }
145
146 void SetName( const wxString& aName ) { m_name = aName;}
147 const wxString& GetName() const { return m_name; }
148
149 void SetLibrary( const wxString& aLibrary ) { m_library = aLibrary; }
150 const wxString& GetLibrary() const { return m_library; }
151
152 void SetReference( const wxString& aReference ) { m_reference = aReference; }
153 const wxString& GetReference() const { return m_reference; }
154
155 void SetValue( const wxString& aValue ) { m_value = aValue; }
156 const wxString& GetValue() const { return m_value; }
157
158 void SetFields( nlohmann::ordered_map<wxString, wxString> aFields )
159 {
160 m_fields = std::move( aFields );
161 }
162 const nlohmann::ordered_map<wxString, wxString>& GetFields() const { return m_fields; }
163
164 void SetProperties( std::map<wxString, wxString> aProps )
165 {
166 m_properties = std::move( aProps );
167 }
168 const std::map<wxString, wxString>& GetProperties() const { return m_properties; }
169
170 void SetFPID( const LIB_ID& aFPID ) { m_fpid = aFPID; }
171 const LIB_ID& GetFPID() const { return m_fpid; }
172
173 void SetAltFPID( const LIB_ID& aFPID ) { m_altFpid = aFPID; }
174 const LIB_ID& GetAltFPID() const { return m_altFpid; }
175
176 const KIID_PATH& GetPath() const { return m_path; }
177
178 const std::vector<KIID>& GetKIIDs() const { return m_kiids; }
179
180 void SetFootprintFilters( const wxArrayString& aFilters ) { m_footprintFilters = aFilters; }
181 const wxArrayString& GetFootprintFilters() const { return m_footprintFilters; }
182
183 void SetPinCount( int aPinCount ) { m_pinCount = aPinCount; }
184 int GetPinCount() const { return m_pinCount; }
185
186 FOOTPRINT* GetFootprint( bool aRelease = false );
187
188 void SetFootprint( FOOTPRINT* aFootprint );
189
190 bool IsLibSource( const wxString& aLibrary, const wxString& aName ) const
191 {
192 return aLibrary == m_library && aName == m_name;
193 }
194
195 void Format( OUTPUTFORMATTER* aOut, int aNestLevel, int aCtl );
196
197 void SetHumanReadablePath( const wxString& aPath ) { m_humanReadablePath = aPath; }
198 const wxString& GetHumanReadablePath() const { return m_humanReadablePath; }
199
200 void SetComponentClassNames( const std::unordered_set<wxString>& aClassNames )
201 {
202 m_componentClassNames = aClassNames;
203 }
204
205 std::unordered_set<wxString>& GetComponentClassNames() { return m_componentClassNames; }
206
209
210 std::vector<std::set<wxString>>& JumperPadGroups() { return m_jumperPadGroups; }
211 const std::vector<std::set<wxString>>& JumperPadGroups() const { return m_jumperPadGroups; }
212
213 NETLIST_GROUP* GetGroup() const { return m_group; }
214 void SetGroup( NETLIST_GROUP* aGroup ) { m_group = aGroup; }
215
216 // Unit info for multi-unit symbols
217 struct UNIT_INFO
218 {
219 wxString m_unitName; // e.g. A
220 std::vector<wxString> m_pins; // pin numbers in this unit
221 };
222
223 void SetUnitInfo( const std::vector<UNIT_INFO>& aUnits ) { m_units = aUnits; }
224 const std::vector<UNIT_INFO>& GetUnitInfo() const { return m_units; }
225
227 const COMPONENT_VARIANT* GetVariant( const wxString& aVariantName ) const;
228 COMPONENT_VARIANT* GetVariant( const wxString& aVariantName );
229 void AddVariant( const COMPONENT_VARIANT& aVariant );
230
231private:
232 std::vector<COMPONENT_NET> m_nets;
233
234 wxArrayString m_footprintFilters;
235 int m_pinCount;
236 wxString m_reference;
237 wxString m_value;
238
239 // human-readable hierarchical sheet path (e.g. /root/block0/sheet1)
240 wxString m_humanReadablePath;
241
244
246 std::vector<KIID> m_kiids;
247
249 wxString m_name;
250
252 wxString m_library;
253
256
261
263 std::unique_ptr<FOOTPRINT> m_footprint;
264
266 std::map<wxString, wxString> m_properties;
267
269 nlohmann::ordered_map<wxString, wxString> m_fields;
270
272 std::unordered_set<wxString> m_componentClassNames;
273
275 std::vector<std::set<wxString>> m_jumperPadGroups;
276
280
283
285
286 // Unit information parsed from the netlist (optional)
287 std::vector<UNIT_INFO> m_units;
288
290};
291
292
293typedef boost::ptr_vector< COMPONENT > COMPONENTS;
294
295
300class NETLIST
301{
302public:
304 m_findByTimeStamp( false ),
305 m_replaceFootprints( false )
306 {
307 }
308
312 bool IsEmpty() const { return m_components.empty(); }
313
317 void Clear() { m_components.clear(); }
318
322 unsigned GetCount() const { return m_components.size(); }
323
330 COMPONENT* GetComponent( unsigned aIndex ) { return &m_components[ aIndex ]; }
331
340 void AddComponent( COMPONENT* aComponent );
341
348 void AddGroup( NETLIST_GROUP* aGroup );
349
350 void SetNetChainFor( const wxString& aNet, const wxString& aNetChain )
351 {
352 m_netSignals[aNet] = aNetChain;
353 }
354
355 void AddSignalTerminalPin( const wxString& aNetChain, const wxString& aRef, const wxString& aPin )
356 {
357 m_netChainTerminals[aNetChain].emplace_back( aRef, aPin );
358 }
359
360 const std::vector<std::pair<wxString, wxString>>& GetNetChainTerminals( const wxString& aNetChain ) const
361 {
362 static const std::vector<std::pair<wxString, wxString>> empty;
363 auto it = m_netChainTerminals.find( aNetChain );
364 return it != m_netChainTerminals.end() ? it->second : empty;
365 }
366
367 const std::map<wxString, std::vector<std::pair<wxString, wxString>>>& GetNetChainTerminalPins() const
368 {
369 return m_netChainTerminals;
370 }
371
372 wxString GetNetChainFor( const wxString& aNet ) const
373 {
374 auto it = m_netSignals.find( aNet );
375 return it != m_netSignals.end() ? it->second : wxString();
376 }
377
378 void SetSignalNetClass( const wxString& aNetChain, const wxString& aNetClass )
379 {
380 m_netChainNetClasses[aNetChain] = aNetClass;
381 }
382
383 wxString GetSignalNetClass( const wxString& aNetChain ) const
384 {
385 auto it = m_netChainNetClasses.find( aNetChain );
386 return it != m_netChainNetClasses.end() ? it->second : wxString();
387 }
388
389 const std::map<wxString, wxString>& GetNetChainNetClasses() const
390 {
392 }
393
395 void SetSignalColor( const wxString& aNetChain, const wxString& aColor )
396 {
397 m_netChainColors[aNetChain] = aColor;
398 }
399
400 wxString GetSignalColor( const wxString& aNetChain ) const
401 {
402 auto it = m_netChainColors.find( aNetChain );
403 return it != m_netChainColors.end() ? it->second : wxString();
404 }
405
406 const std::map<wxString, wxString>& GetNetChainColors() const
407 {
408 return m_netChainColors;
409 }
410
412 void SetSignalChainClass( const wxString& aNetChain, const wxString& aClass )
413 {
414 m_signalChainClasses[aNetChain] = aClass;
415 }
416
417 wxString GetSignalChainClass( const wxString& aNetChain ) const
418 {
419 auto it = m_signalChainClasses.find( aNetChain );
420 return it != m_signalChainClasses.end() ? it->second : wxString();
421 }
422
423 const std::map<wxString, wxString>& GetSignalChainClasses() const
424 {
426 }
427
436
442
449 COMPONENT* GetComponentByReference( const wxString& aReference );
450
458
466
469
470 void SetFindByTimeStamp( bool aFindByTimeStamp ) { m_findByTimeStamp = aFindByTimeStamp; }
471 bool IsFindByTimeStamp() const { return m_findByTimeStamp; }
472
473 void SetReplaceFootprints( bool aReplace ) { m_replaceFootprints = aReplace; }
475
480
481 void Format( const char* aDocName, OUTPUTFORMATTER* aOut, int aNestLevel, int aCtl = 0 );
482
483#define CTL_FOR_CVPCB (CTL_OMIT_NETS | CTL_OMIT_FILTERS | CTL_OMIT_EXTRA)
484
486 {
487 Format( "cvpcb_netlist", aOut, 0, CTL_FOR_CVPCB );
488 }
489
493 const std::vector<wxString>& GetVariantNames() const { return m_variantNames; }
494
498 void AddVariant( const wxString& aName, const wxString& aDescription = wxEmptyString )
499 {
500 if( aName.IsEmpty() )
501 return;
502
503 for( const wxString& existingName : m_variantNames )
504 {
505 if( existingName.CmpNoCase( aName ) == 0 )
506 {
507 if( aDescription.IsEmpty() )
508 m_variantDescriptions.erase( existingName );
509 else
510 m_variantDescriptions[existingName] = aDescription;
511
512 return;
513 }
514 }
515
516 m_variantNames.push_back( aName );
517
518 if( aDescription.IsEmpty() )
519 return;
520
521 m_variantDescriptions[aName] = aDescription;
522 }
523
527 wxString GetVariantDescription( const wxString& aVariantName ) const
528 {
529 wxString actualName;
530
531 for( const wxString& existingName : m_variantNames )
532 {
533 if( existingName.CmpNoCase( aVariantName ) == 0 )
534 {
535 actualName = existingName;
536 break;
537 }
538 }
539
540 if( !actualName.IsEmpty() )
541 {
542 auto it = m_variantDescriptions.find( actualName );
543
544 if( it != m_variantDescriptions.end() )
545 return it->second;
546 }
547
548 return wxEmptyString;
549 }
550
554 const std::map<wxString, wxString>& GetVariantDescriptions() const
555 {
557 }
558
559private:
560 COMPONENTS m_components; // Components found in the netlist.
561 NETLIST_GROUPS m_groups; // Groups found in the netlist.
562 std::map<wxString, wxString> m_netSignals;
563 std::map<wxString, std::vector<std::pair<wxString, wxString>>> m_netChainTerminals;
564 std::map<wxString, wxString> m_netChainNetClasses;
565 std::map<wxString, wxString> m_netChainColors;
566 std::map<wxString, wxString> m_signalChainClasses;
567
568 std::vector<wxString> m_variantNames; // Variant names in order.
569 std::map<wxString, wxString> m_variantDescriptions; // Variant descriptions.
570
571 bool m_findByTimeStamp; // Associate components by KIID (or refdes if false)
572 bool m_replaceFootprints; // Update footprints to match footprints defined in netlist
573};
574
575
576#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:62
COMPONENT_NET(const wxString &aPinName, const wxString &aNetName, const wxString &aPinFunction, const wxString &aPinType)
Definition pcb_netlist.h:52
int Format(OUTPUTFORMATTER *aOut, int aNestLevel, int aCtl)
bool operator<(const COMPONENT_NET &aNet) const
bool IsValid() const
Definition pcb_netlist.h:66
const wxString & GetPinFunction() const
Definition pcb_netlist.h:63
const wxString & GetPinName() const
Definition pcb_netlist.h:61
const wxString & GetPinType() const
Definition pcb_netlist.h:64
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:44
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.
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:291
A pure virtual class used to derive REPORTER objects from.
Definition reporter.h:71
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)
Definition pcb_netlist.h:99
bool m_hasExcludedFromPosFiles
nlohmann::ordered_map< wxString, wxString > m_fields
std::vector< KIID > members