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