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