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#include <algorithm>
32
33
34int COMPONENT_NET::Format( OUTPUTFORMATTER* aOut, int aNestLevel, int aCtl )
35{
36 return aOut->Print( aNestLevel, "(pin_net %s %s)",
37 aOut->Quotew( m_pinName ).c_str(),
38 aOut->Quotew( m_netName ).c_str() );
39}
40
41COMPONENT::COMPONENT( const LIB_ID& aFPID,
42 const wxString& aReference,
43 const wxString& aValue,
44 const KIID_PATH& aPath,
45 const std::vector<KIID>& aKiids )
46{
47 m_fpid = aFPID;
48 m_reference = aReference;
49 m_value = aValue;
50 m_pinCount = 0;
51 m_path = aPath;
52 m_kiids = aKiids;
54 m_group = nullptr;
55}
56
57
59{
60}
61
62
64{
65 return ( aRelease ) ? m_footprint.release() : m_footprint.get();
66}
67
68
70{
71 m_footprint.reset( aFootprint );
73
74 if( !m_kiids.empty() )
75 path.push_back( m_kiids.front() );
76
77 if( aFootprint == nullptr )
78 return;
79
80 aFootprint->SetReference( m_reference );
81 aFootprint->SetValue( m_value );
82 aFootprint->SetFPID( m_fpid );
83 aFootprint->SetPath( path );
84
85 // Copy over unit info
86 std::vector<FOOTPRINT::FP_UNIT_INFO> fpUnits;
87
88 for( const UNIT_INFO& u : m_units )
89 fpUnits.push_back( { u.m_unitName, u.m_pins } );
90
91 aFootprint->SetUnitInfo( fpUnits );
92}
93
94
96
97
98
99
100const COMPONENT_NET& COMPONENT::GetNet( const wxString& aPinName ) const
101{
102 const COMPONENT_NET* fallback = nullptr;
103
104 for( const COMPONENT_NET& net : m_nets )
105 {
106 if( net.GetPinName() != aPinName )
107 {
108 if( !net.GetPinName().StartsWith( wxS( "[" ) ) )
109 continue;
110
111 const std::vector<wxString> expanded = ExpandStackedPinNotation( net.GetPinName() );
112
113 if( std::find( expanded.begin(), expanded.end(), aPinName ) == expanded.end() )
114 continue;
115 }
116
117 if( !fallback )
118 fallback = &net;
119
120 // Shared-unit pins can export both an explicit net and automatic unconnected nets.
121 if( !net.GetNetName().IsEmpty() && !IsAutoGeneratedNetName( net.GetNetName() ) )
122 return net;
123 }
124
125 return fallback ? *fallback : m_emptyNet;
126}
127
128
129const COMPONENT_VARIANT* COMPONENT::GetVariant( const wxString& aVariantName ) const
130{
131 auto it = m_variants.find( aVariantName );
132
133 return it != m_variants.end() ? &it->second : nullptr;
134}
135
136
137COMPONENT_VARIANT* COMPONENT::GetVariant( const wxString& aVariantName )
138{
139 auto it = m_variants.find( aVariantName );
140
141 return it != m_variants.end() ? &it->second : nullptr;
142}
143
144
146{
147 if( aVariant.m_name.IsEmpty() )
148 return;
149
150 auto it = m_variants.find( aVariant.m_name );
151
152 if( it != m_variants.end() )
153 {
154 COMPONENT_VARIANT updated = aVariant;
155 updated.m_name = it->first;
156 it->second = std::move( updated );
157 return;
158 }
159
160 m_variants.emplace( aVariant.m_name, aVariant );
161}
162
163
164void COMPONENT::Format( OUTPUTFORMATTER* aOut, int aNestLevel, int aCtl )
165{
166 int nl = aNestLevel;
167
168 aOut->Print( nl, "(ref %s ", aOut->Quotew( m_reference ).c_str() );
169 aOut->Print( 0, "(fpid %s)\n", aOut->Quotew( m_fpid.Format() ).c_str() );
170
171 if( !( aCtl & CTL_OMIT_EXTRA ) )
172 {
173 aOut->Print( nl+1, "(value %s)\n", aOut->Quotew( m_value ).c_str() );
174 aOut->Print( nl+1, "(name %s)\n", aOut->Quotew( m_name ).c_str() );
175 aOut->Print( nl+1, "(library %s)\n", aOut->Quotew( m_library ).c_str() );
176
177 wxString path;
178
179 for( const KIID& pathStep : m_path )
180 path += '/' + pathStep.AsString();
181
182 if( !( aCtl & CTL_OMIT_FP_UUID ) && !m_kiids.empty() )
183 path += '/' + m_kiids.front().AsString();
184
185 aOut->Print( nl+1, "(timestamp %s)\n", aOut->Quotew( path ).c_str() );
186
187 // Add all fields as a (field) under a (fields) node
188 aOut->Print( nl + 1, "(fields" );
189
190 for( std::pair<wxString, wxString> field : m_fields )
191 aOut->Print( nl + 2, "\n(field (name %s) %s)", aOut->Quotew( field.first ).c_str(),
192 aOut->Quotew( field.second ).c_str() );
193
194 aOut->Print( 0, ")\n" );
195
196 // Add DNP and exclusion properties if we have them
197 if( m_properties.count( "dnp" ) )
198 aOut->Print( nl + 1, "(property (name \"dnp\"))\n" );
199
200 if( m_properties.count( "exclude_from_bom" ) )
201 aOut->Print( nl + 1, "(property (name \"exclude_from_bom\"))\n" );
202
203 if( m_properties.count( "exclude_from_sim" ) )
204 aOut->Print( nl + 1, "(property (name \"exclude_from_sim\"))\n" );
205
206 if( m_properties.count( "exclude_from_pos_files" ) )
207 aOut->Print( nl + 1, "(property (name \"exclude_from_pos_files\"))\n" );
208
209 if( !m_variants.empty() )
210 {
211 aOut->Print( nl + 1, "(variants" );
212
213 for( const auto& [variantName, variant] : m_variants )
214 {
215 aOut->Print( nl + 2, "\n(variant (name %s)",
216 aOut->Quotew( variantName ).c_str() );
217
218 if( variant.m_hasDnp )
219 {
220 aOut->Print( 0, " (property (name \"dnp\") (value %s))",
221 aOut->Quotew( variant.m_dnp ? wxT( "1" ) : wxT( "0" ) ).c_str() );
222 }
223
224 if( variant.m_hasExcludedFromBOM )
225 {
226 aOut->Print( 0, " (property (name \"exclude_from_bom\") (value %s))",
227 aOut->Quotew( variant.m_excludedFromBOM ? wxT( "1" ) : wxT( "0" ) ).c_str() );
228 }
229
230 if( variant.m_hasExcludedFromSim )
231 {
232 aOut->Print( 0, " (property (name \"exclude_from_sim\") (value %s))",
233 aOut->Quotew( variant.m_excludedFromSim ? wxT( "1" ) : wxT( "0" ) ).c_str() );
234 }
235
236 if( variant.m_hasExcludedFromPosFiles )
237 {
238 aOut->Print( 0, " (property (name \"exclude_from_pos_files\") (value %s))",
239 aOut->Quotew( variant.m_excludedFromPosFiles ? wxT( "1" ) : wxT( "0" ) ).c_str() );
240 }
241
242 if( !variant.m_fields.empty() )
243 {
244 aOut->Print( 0, "\n" );
245 aOut->Print( nl + 3, "(fields" );
246
247 for( const auto& [fieldName, fieldValue] : variant.m_fields )
248 {
249 aOut->Print( nl + 4, "\n(field (name %s) %s)",
250 aOut->Quotew( fieldName ).c_str(),
251 aOut->Quotew( fieldValue ).c_str() );
252 }
253
254 aOut->Print( 0, ")" );
255 }
256
257 aOut->Print( 0, ")" );
258 }
259
260 aOut->Print( 0, ")\n" );
261 }
262 }
263
264 if( !( aCtl & CTL_OMIT_FILTERS ) && m_footprintFilters.GetCount() )
265 {
266 aOut->Print( nl+1, "(fp_filters" );
267
268 for( unsigned i = 0; i < m_footprintFilters.GetCount(); ++i )
269 aOut->Print( 0, " %s", aOut->Quotew( m_footprintFilters[i] ).c_str() );
270
271 aOut->Print( 0, ")\n" );
272 }
273
274 if( !( aCtl & CTL_OMIT_NETS ) && m_nets.size() )
275 {
276 int llen = aOut->Print( nl+1, "(nets " );
277
278 for( unsigned i = 0; i < m_nets.size(); ++i )
279 {
280 if( llen > 80 )
281 {
282 aOut->Print( 0, "\n" );
283 llen = aOut->Print( nl+1, " " );
284 }
285
286 llen += m_nets[i].Format( aOut, 0, aCtl );
287 }
288
289 aOut->Print( 0, ")\n" );
290 }
291
292 aOut->Print( nl, ")\n" ); // </ref>
293}
294
295
296void NETLIST::Format( const char* aDocName, OUTPUTFORMATTER* aOut, int aNestLevel, int aCtl )
297{
298 int nl = aNestLevel;
299
300 aOut->Print( nl, "(%s\n", aDocName );
301
302 for( unsigned i = 0; i < m_components.size(); i++ )
303 {
304 m_components[i].Format( aOut, nl+1, aCtl );
305 }
306
307 aOut->Print( nl, ")\n" );
308}
309
310
311void NETLIST::AddComponent( COMPONENT* aComponent )
312{
313 m_components.push_back( aComponent );
314}
315
316
317void NETLIST::AddGroup( NETLIST_GROUP* aComponent )
318{
319 m_groups.push_back( aComponent );
320}
321
323{
324 for( NETLIST_GROUP& group : m_groups )
325 {
326 if( group.uuid == aUuid )
327 return &group;
328 }
329
330 return nullptr;
331}
332
333
334COMPONENT* NETLIST::GetComponentByReference( const wxString& aReference )
335{
336 COMPONENT* component = nullptr;
337
338 for( unsigned i = 0; i < m_components.size(); i++ )
339 {
340 if( m_components[i].GetReference() == aReference )
341 {
342 component = &m_components[i];
343 break;
344 }
345 }
346
347 return component;
348}
349
350
352{
353 if( aUuidPath.empty() )
354 return nullptr;
355
356 KIID comp_uuid = aUuidPath.back();
357 KIID_PATH base = aUuidPath;
358
359 if( !base.empty() )
360 base.pop_back();
361
362 for( COMPONENT& component : m_components )
363 {
364 const std::vector<KIID>& kiids = component.GetKIIDs();
365
366 if( base != component.GetPath() )
367 continue;
368
369 if( std::find( kiids.begin(), kiids.end(), comp_uuid ) != kiids.end() )
370 return &component;
371 }
372
373 return nullptr;
374}
375
376
378{
379 if( aUuid == 0 )
380 return nullptr;
381
382 for( COMPONENT& component : m_components )
383 {
384 for( const KIID& compUuid : component.GetKIIDs() )
385 {
386 if( aUuid == compUuid )
387 return &component;
388 }
389 }
390
391 return nullptr;
392}
393
394
398static bool ByFPID( const COMPONENT& ref, const COMPONENT& cmp )
399{
400 return ref.GetFPID() > cmp.GetFPID();
401}
402
403
405{
406 m_components.sort( ByFPID );
407}
408
409
413bool operator < ( const COMPONENT& item1, const COMPONENT& item2 )
414{
415 return StrNumCmp( item1.GetReference(), item2.GetReference(), true ) < 0;
416}
417
418
420{
421 m_components.sort();
422}
423
424
426{
427 for( unsigned i = 0; i < m_components.size(); i++ )
428 {
429 if( !m_components[i].GetFPID().empty() )
430 return true;
431 }
432
433 return false;
434}
435
436
438{
439 for( NETLIST_GROUP& group : m_groups )
440 {
441 for( const KIID_PATH& member : group.members )
442 {
443 COMPONENT* component = nullptr;
444
445 if( member.size() > 1 )
446 {
447 // Instance path, match the exact instance. The same symbol uuid exists once
448 // per instance of a shared sheet, only the path tells them apart.
449 component = GetComponentByPath( member );
450 }
451 else if( member.size() == 1 )
452 {
453 // Bare symbol uuid
454 component = GetComponentByUuid( member.front() );
455 }
456
457 if( component )
458 component->SetGroup( &group );
459 }
460 }
461}
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:474
void SetPath(const KIID_PATH &aPath)
Definition footprint.h:497
void SetReference(const wxString &aReference)
Definition footprint.h:907
void SetValue(const wxString &aValue)
Definition footprint.h:930
void SetUnitInfo(const std::vector< FP_UNIT_INFO > &aUnits)
Definition footprint.h:1016
Definition kiid.h:46
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:132
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:294
std::string Quotew(const wxString &aWrapee) const
Definition richio.cpp:505
int PRINTF_FUNC_N Print(int nestLevel, const char *fmt,...)
Format and write text to the output stream.
Definition richio.cpp:432
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 ...
bool IsAutoGeneratedNetName(const wxString &aNetName)
Recognize the reserved prefixes used for generated pin-net names.
std::string path