KiCad PCB EDA Suite
Loading...
Searching...
No Matches
pcb_netlist.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) 1992-2011 Jean-Pierre Charras.
5 * Copyright (C) 2013 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#include "pcb_netlist.h"
23
24#include <wx/tokenzr.h>
25#include <wx/log.h>
26
27#include <footprint.h>
28#include <richio.h>
29#include <string_utils.h>
30
31
32int COMPONENT_NET::Format( OUTPUTFORMATTER* aOut, int aNestLevel, int aCtl )
33{
34 return aOut->Print( aNestLevel, "(pin_net %s %s)",
35 aOut->Quotew( m_pinName ).c_str(),
36 aOut->Quotew( m_netName ).c_str() );
37}
38
39COMPONENT::COMPONENT( const LIB_ID& aFPID,
40 const wxString& aReference,
41 const wxString& aValue,
42 const KIID_PATH& aPath,
43 const std::vector<KIID>& aKiids )
44{
45 m_fpid = aFPID;
46 m_reference = aReference;
47 m_value = aValue;
48 m_pinCount = 0;
49 m_path = aPath;
50 m_kiids = aKiids;
52 m_group = nullptr;
53}
54
55
57{
58}
59
60
62{
63 return ( aRelease ) ? m_footprint.release() : m_footprint.get();
64}
65
66
68{
69 m_footprint.reset( aFootprint );
71
72 if( !m_kiids.empty() )
73 path.push_back( m_kiids.front() );
74
75 if( aFootprint == nullptr )
76 return;
77
78 aFootprint->SetReference( m_reference );
79 aFootprint->SetValue( m_value );
80 aFootprint->SetFPID( m_fpid );
81 aFootprint->SetPath( path );
82
83 // Copy over unit info
84 std::vector<FOOTPRINT::FP_UNIT_INFO> fpUnits;
85
86 for( const UNIT_INFO& u : m_units )
87 fpUnits.push_back( { u.m_unitName, u.m_pins } );
88
89 aFootprint->SetUnitInfo( fpUnits );
90}
91
92
94
95
96
97
98
99const COMPONENT_NET& COMPONENT::GetNet( const wxString& aPinName ) const
100{
101 wxLogTrace( wxT( "NETLIST_STACKED_PINS" ),
102 wxT( "Looking for pin '%s' in component '%s'" ),
103 aPinName, m_reference );
104
105 for( const COMPONENT_NET& net : m_nets )
106 {
107 wxLogTrace( wxT( "NETLIST_STACKED_PINS" ),
108 wxT( " Checking net pin name '%s'" ),
109 net.GetPinName() );
110
111 if( net.GetPinName() == aPinName )
112 {
113 wxLogTrace( wxT( "NETLIST_STACKED_PINS" ),
114 wxT( " Found exact match for pin '%s'" ),
115 aPinName );
116 return net;
117 }
118
119 // Check if this net's pin name is a stacked pin notation that expands to include aPinName
120 std::vector<wxString> expandedPins = ExpandStackedPinNotation( net.GetPinName() );
121 if( !expandedPins.empty() )
122 {
123 wxLogTrace( wxT( "NETLIST_STACKED_PINS" ),
124 wxT( " Pin name '%s' expanded to %zu pins" ),
125 net.GetPinName(), expandedPins.size() );
126
127 for( const wxString& expandedPin : expandedPins )
128 {
129 wxLogTrace( wxT( "NETLIST_STACKED_PINS" ),
130 wxT( " Checking expanded pin '%s'" ),
131 expandedPin );
132 if( expandedPin == aPinName )
133 {
134 wxLogTrace( wxT( "NETLIST_STACKED_PINS" ),
135 wxT( " Found match for pin '%s' in stacked notation '%s'" ),
136 aPinName, net.GetPinName() );
137 return net;
138 }
139 }
140 }
141 }
142
143 wxLogTrace( wxT( "NETLIST_STACKED_PINS" ),
144 wxT( " No net found for pin '%s'" ),
145 aPinName );
146 return m_emptyNet;
147}
148
149
150const COMPONENT_VARIANT* COMPONENT::GetVariant( const wxString& aVariantName ) const
151{
152 auto it = m_variants.find( aVariantName );
153
154 return it != m_variants.end() ? &it->second : nullptr;
155}
156
157
158COMPONENT_VARIANT* COMPONENT::GetVariant( const wxString& aVariantName )
159{
160 auto it = m_variants.find( aVariantName );
161
162 return it != m_variants.end() ? &it->second : nullptr;
163}
164
165
167{
168 if( aVariant.m_name.IsEmpty() )
169 return;
170
171 auto it = m_variants.find( aVariant.m_name );
172
173 if( it != m_variants.end() )
174 {
175 COMPONENT_VARIANT updated = aVariant;
176 updated.m_name = it->first;
177 it->second = std::move( updated );
178 return;
179 }
180
181 m_variants.emplace( aVariant.m_name, aVariant );
182}
183
184
185void COMPONENT::Format( OUTPUTFORMATTER* aOut, int aNestLevel, int aCtl )
186{
187 int nl = aNestLevel;
188
189 aOut->Print( nl, "(ref %s ", aOut->Quotew( m_reference ).c_str() );
190 aOut->Print( 0, "(fpid %s)\n", aOut->Quotew( m_fpid.Format() ).c_str() );
191
192 if( !( aCtl & CTL_OMIT_EXTRA ) )
193 {
194 aOut->Print( nl+1, "(value %s)\n", aOut->Quotew( m_value ).c_str() );
195 aOut->Print( nl+1, "(name %s)\n", aOut->Quotew( m_name ).c_str() );
196 aOut->Print( nl+1, "(library %s)\n", aOut->Quotew( m_library ).c_str() );
197
198 wxString path;
199
200 for( const KIID& pathStep : m_path )
201 path += '/' + pathStep.AsString();
202
203 if( !( aCtl & CTL_OMIT_FP_UUID ) && !m_kiids.empty() )
204 path += '/' + m_kiids.front().AsString();
205
206 aOut->Print( nl+1, "(timestamp %s)\n", aOut->Quotew( path ).c_str() );
207
208 // Add all fields as a (field) under a (fields) node
209 aOut->Print( nl + 1, "(fields" );
210
211 for( std::pair<wxString, wxString> field : m_fields )
212 aOut->Print( nl + 2, "\n(field (name %s) %s)", aOut->Quotew( field.first ).c_str(),
213 aOut->Quotew( field.second ).c_str() );
214
215 aOut->Print( 0, ")\n" );
216
217 // Add DNP, Exclude from BOM, and Exclude from Position Files properties if we have them
218 if( m_properties.count( "dnp" ) )
219 aOut->Print( nl + 1, "(property (name \"dnp\"))\n" );
220
221 if( m_properties.count( "exclude_from_bom" ) )
222 aOut->Print( nl + 1, "(property (name \"exclude_from_bom\"))\n" );
223
224 if( m_properties.count( "exclude_from_pos_files" ) )
225 aOut->Print( nl + 1, "(property (name \"exclude_from_pos_files\"))\n" );
226
227 if( !m_variants.empty() )
228 {
229 aOut->Print( nl + 1, "(variants" );
230
231 for( const auto& [variantName, variant] : m_variants )
232 {
233 aOut->Print( nl + 2, "\n(variant (name %s)",
234 aOut->Quotew( variantName ).c_str() );
235
236 if( variant.m_hasDnp )
237 {
238 aOut->Print( 0, " (property (name \"dnp\") (value %s))",
239 aOut->Quotew( variant.m_dnp ? wxT( "1" ) : wxT( "0" ) ).c_str() );
240 }
241
242 if( variant.m_hasExcludedFromBOM )
243 {
244 aOut->Print( 0, " (property (name \"exclude_from_bom\") (value %s))",
245 aOut->Quotew( variant.m_excludedFromBOM ? wxT( "1" ) : wxT( "0" ) ).c_str() );
246 }
247
248 if( variant.m_hasExcludedFromSim )
249 {
250 aOut->Print( 0, " (property (name \"exclude_from_sim\") (value %s))",
251 aOut->Quotew( variant.m_excludedFromSim ? wxT( "1" ) : wxT( "0" ) ).c_str() );
252 }
253
254 if( variant.m_hasExcludedFromPosFiles )
255 {
256 aOut->Print( 0, " (property (name \"exclude_from_pos_files\") (value %s))",
257 aOut->Quotew( variant.m_excludedFromPosFiles ? wxT( "1" ) : wxT( "0" ) ).c_str() );
258 }
259
260 if( !variant.m_fields.empty() )
261 {
262 aOut->Print( 0, "\n" );
263 aOut->Print( nl + 3, "(fields" );
264
265 for( const auto& [fieldName, fieldValue] : variant.m_fields )
266 {
267 aOut->Print( nl + 4, "\n(field (name %s) %s)",
268 aOut->Quotew( fieldName ).c_str(),
269 aOut->Quotew( fieldValue ).c_str() );
270 }
271
272 aOut->Print( 0, ")" );
273 }
274
275 aOut->Print( 0, ")" );
276 }
277
278 aOut->Print( 0, ")\n" );
279 }
280 }
281
282 if( !( aCtl & CTL_OMIT_FILTERS ) && m_footprintFilters.GetCount() )
283 {
284 aOut->Print( nl+1, "(fp_filters" );
285
286 for( unsigned i = 0; i < m_footprintFilters.GetCount(); ++i )
287 aOut->Print( 0, " %s", aOut->Quotew( m_footprintFilters[i] ).c_str() );
288
289 aOut->Print( 0, ")\n" );
290 }
291
292 if( !( aCtl & CTL_OMIT_NETS ) && m_nets.size() )
293 {
294 int llen = aOut->Print( nl+1, "(nets " );
295
296 for( unsigned i = 0; i < m_nets.size(); ++i )
297 {
298 if( llen > 80 )
299 {
300 aOut->Print( 0, "\n" );
301 llen = aOut->Print( nl+1, " " );
302 }
303
304 llen += m_nets[i].Format( aOut, 0, aCtl );
305 }
306
307 aOut->Print( 0, ")\n" );
308 }
309
310 aOut->Print( nl, ")\n" ); // </ref>
311}
312
313
314void NETLIST::Format( const char* aDocName, OUTPUTFORMATTER* aOut, int aNestLevel, int aCtl )
315{
316 int nl = aNestLevel;
317
318 aOut->Print( nl, "(%s\n", aDocName );
319
320 for( unsigned i = 0; i < m_components.size(); i++ )
321 {
322 m_components[i].Format( aOut, nl+1, aCtl );
323 }
324
325 aOut->Print( nl, ")\n" );
326}
327
328
329void NETLIST::AddComponent( COMPONENT* aComponent )
330{
331 m_components.push_back( aComponent );
332}
333
334
335void NETLIST::AddGroup( NETLIST_GROUP* aComponent )
336{
337 m_groups.push_back( aComponent );
338}
339
341{
342 for( NETLIST_GROUP& group : m_groups )
343 {
344 if( group.uuid == aUuid )
345 return &group;
346 }
347
348 return nullptr;
349}
350
351
352COMPONENT* NETLIST::GetComponentByReference( const wxString& aReference )
353{
354 COMPONENT* component = nullptr;
355
356 for( unsigned i = 0; i < m_components.size(); i++ )
357 {
358 if( m_components[i].GetReference() == aReference )
359 {
360 component = &m_components[i];
361 break;
362 }
363 }
364
365 return component;
366}
367
368
370{
371 if( aUuidPath.empty() )
372 return nullptr;
373
374 KIID comp_uuid = aUuidPath.back();
375 KIID_PATH base = aUuidPath;
376
377 if( !base.empty() )
378 base.pop_back();
379
380 for( COMPONENT& component : m_components )
381 {
382 const std::vector<KIID>& kiids = component.GetKIIDs();
383
384 if( base != component.GetPath() )
385 continue;
386
387 if( std::find( kiids.begin(), kiids.end(), comp_uuid ) != kiids.end() )
388 return &component;
389 }
390
391 return nullptr;
392}
393
394
396{
397 if( aUuid == 0 )
398 return nullptr;
399
400 for( COMPONENT& component : m_components )
401 {
402 for( const KIID& compUuid : component.GetKIIDs() )
403 {
404 if( aUuid == compUuid )
405 return &component;
406 }
407 }
408
409 return nullptr;
410}
411
412
416static bool ByFPID( const COMPONENT& ref, const COMPONENT& cmp )
417{
418 return ref.GetFPID() > cmp.GetFPID();
419}
420
421
423{
424 m_components.sort( ByFPID );
425}
426
427
431bool operator < ( const COMPONENT& item1, const COMPONENT& item2 )
432{
433 return StrNumCmp( item1.GetReference(), item2.GetReference(), true ) < 0;
434}
435
436
438{
439 m_components.sort();
440}
441
442
444{
445 for( unsigned i = 0; i < m_components.size(); i++ )
446 {
447 if( !m_components[i].GetFPID().empty() )
448 return true;
449 }
450
451 return false;
452}
453
454
456{
457 for( NETLIST_GROUP& group : m_groups )
458 {
459 for( KIID& member : group.members )
460 {
461 COMPONENT* component = GetComponentByUuid( member );
462
463 if( component )
464 component->SetGroup( &group );
465 }
466 }
467}
Used to store the component pin name to net name (and pin function) associations stored in a netlist.
int Format(OUTPUTFORMATTER *aOut, int aNestLevel, int aCtl)
Store all of the related component information found in a netlist.
const COMPONENT_NET & GetNet(unsigned aIndex) const
const KIID_PATH & GetPath() const
std::vector< COMPONENT_NET > m_nets
list of nets shared by the component pins
std::map< wxString, wxString > m_properties
Component-specific properties found in the netlist.
wxArrayString m_footprintFilters
const wxString & GetReference() const
void AddVariant(const COMPONENT_VARIANT &aVariant)
CASE_INSENSITIVE_MAP< COMPONENT_VARIANT > m_variants
void Format(OUTPUTFORMATTER *aOut, int aNestLevel, int aCtl)
const COMPONENT_VARIANT * GetVariant(const wxString &aVariantName) const
KIID_PATH m_path
A fully specified path to the component (but not the component: [ sheetUUID, sheetUUID,...
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 ...
nlohmann::ordered_map< wxString, wxString > m_fields
Component-specific user fields found in the netlist.
wxString m_name
The name of the component in m_library used when it was placed on the schematic.
void SetFootprint(FOOTPRINT *aFootprint)
std::vector< KIID > m_kiids
A vector of possible KIIDs corresponding to all units in a symbol.
FOOTPRINT * GetFootprint(bool aRelease=false)
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.
static COMPONENT_NET m_emptyNet
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)
std::unique_ptr< FOOTPRINT > m_footprint
The FOOTPRINT loaded for #m_FPID.
const LIB_ID & GetFPID() const
void SetGroup(NETLIST_GROUP *aGroup)
std::vector< UNIT_INFO > m_units
void SetFPID(const LIB_ID &aFPID)
Definition footprint.h:442
void SetPath(const KIID_PATH &aPath)
Definition footprint.h:465
void SetReference(const wxString &aReference)
Definition footprint.h:847
void SetValue(const wxString &aValue)
Definition footprint.h:868
void SetUnitInfo(const std::vector< FP_UNIT_INFO > &aUnits)
Definition footprint.h:939
Definition kiid.h:44
A logical library item identifier and consists of various portions much like a URI.
Definition lib_id.h:45
UTF8 Format() const
Definition lib_id.cpp:115
void Format(const char *aDocName, OUTPUTFORMATTER *aOut, int aNestLevel, int aCtl=0)
void AddGroup(NETLIST_GROUP *aGroup)
void AddComponent(COMPONENT *aComponent)
Add aComponent to the NETLIST.
COMPONENT * GetComponentByPath(const KIID_PATH &aPath)
Return a COMPONENT by aPath.
COMPONENT * GetComponentByReference(const wxString &aReference)
Return a COMPONENT by aReference.
COMPONENT * GetComponentByUuid(const KIID &aUuid)
Return a COMPONENT by aUuid.
NETLIST_GROUP * GetGroupByUuid(const KIID &aUuid)
Return a NETLIST_GROUP by aUuid.
void ApplyGroupMembership()
After groups and components are parsed, apply the group memberships to the internal components based ...
An interface used to output 8 bit text in a convenient way.
Definition richio.h:291
std::string Quotew(const wxString &aWrapee) const
Definition richio.cpp:507
int PRINTF_FUNC_N Print(int nestLevel, const char *fmt,...)
Format and write text to the output stream.
Definition richio.cpp:422
static bool ByFPID(const COMPONENT &ref, const COMPONENT &cmp)
A helper function used to sort the component list used by loadNewModules.
#define CTL_OMIT_EXTRA
Definition ctl_flags.h:27
#define CTL_OMIT_NETS
Definition ctl_flags.h:28
#define CTL_OMIT_FP_UUID
Don't prefix the footprint UUID to the sheet path.
Definition ctl_flags.h:31
#define CTL_OMIT_FILTERS
Omit the ki_fp_filters attribute in .kicad_xxx files.
Definition ctl_flags.h:41
static bool empty(const wxTextEntryBase *aCtrl)
bool operator<(const COMPONENT &item1, const COMPONENT &item2)
Compare two COMPONENT objects by reference designator.
static bool ByFPID(const COMPONENT &ref, const COMPONENT &cmp)
A helper function used to sort the component list used by loadNewModules.
int StrNumCmp(const wxString &aString1, const wxString &aString2, bool aIgnoreCase)
Compare two strings with alphanumerical content.
std::vector< wxString > ExpandStackedPinNotation(const wxString &aPinName, bool *aValid)
Expand stacked pin notation like [1,2,3], [1-4], [A1-A4], or [AA1-AA3,AB4,CD12-CD14] into individual ...
std::string path