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
61
63
64
65
66
67
68const COMPONENT_NET& COMPONENT::GetNet( const wxString& aPinName ) const
69{
70 wxLogTrace( wxT( "NETLIST_STACKED_PINS" ),
71 wxT( "Looking for pin '%s' in component '%s'" ),
72 aPinName, m_reference );
73
74 for( const COMPONENT_NET& net : m_nets )
75 {
76 wxLogTrace( wxT( "NETLIST_STACKED_PINS" ),
77 wxT( " Checking net pin name '%s'" ),
78 net.GetPinName() );
79
80 if( net.GetPinName() == aPinName )
81 {
82 wxLogTrace( wxT( "NETLIST_STACKED_PINS" ),
83 wxT( " Found exact match for pin '%s'" ),
84 aPinName );
85 return net;
86 }
87
88 // Check if this net's pin name is a stacked pin notation that expands to include aPinName
89 std::vector<wxString> expandedPins = ExpandStackedPinNotation( net.GetPinName() );
90 if( !expandedPins.empty() )
91 {
92 wxLogTrace( wxT( "NETLIST_STACKED_PINS" ),
93 wxT( " Pin name '%s' expanded to %zu pins" ),
94 net.GetPinName(), expandedPins.size() );
95
96 for( const wxString& expandedPin : expandedPins )
97 {
98 wxLogTrace( wxT( "NETLIST_STACKED_PINS" ),
99 wxT( " Checking expanded pin '%s'" ),
100 expandedPin );
101 if( expandedPin == aPinName )
102 {
103 wxLogTrace( wxT( "NETLIST_STACKED_PINS" ),
104 wxT( " Found match for pin '%s' in stacked notation '%s'" ),
105 aPinName, net.GetPinName() );
106 return net;
107 }
108 }
109 }
110 }
111
112 wxLogTrace( wxT( "NETLIST_STACKED_PINS" ),
113 wxT( " No net found for pin '%s'" ),
114 aPinName );
115 return m_emptyNet;
116}
117
118
119void COMPONENT::Format( OUTPUTFORMATTER* aOut, int aNestLevel, int aCtl )
120{
121 int nl = aNestLevel;
122
123 aOut->Print( nl, "(ref %s ", aOut->Quotew( m_reference ).c_str() );
124 aOut->Print( 0, "(fpid %s)\n", aOut->Quotew( m_fpid.Format() ).c_str() );
125
126 if( !( aCtl & CTL_OMIT_EXTRA ) )
127 {
128 aOut->Print( nl+1, "(value %s)\n", aOut->Quotew( m_value ).c_str() );
129 aOut->Print( nl+1, "(name %s)\n", aOut->Quotew( m_name ).c_str() );
130 aOut->Print( nl+1, "(library %s)\n", aOut->Quotew( m_library ).c_str() );
131
132 wxString path;
133
134 for( const KIID& pathStep : m_path )
135 path += '/' + pathStep.AsString();
136
137 if( !( aCtl & CTL_OMIT_FP_UUID ) && !m_kiids.empty() )
138 path += '/' + m_kiids.front().AsString();
139
140 aOut->Print( nl+1, "(timestamp %s)\n", aOut->Quotew( path ).c_str() );
141
142 // Add all fields as a (field) under a (fields) node
143 aOut->Print( nl + 1, "(fields" );
144
145 for( std::pair<wxString, wxString> field : m_fields )
146 aOut->Print( nl + 2, "\n(field (name %s) %s)", aOut->Quotew( field.first ).c_str(),
147 aOut->Quotew( field.second ).c_str() );
148
149 aOut->Print( 0, ")\n" );
150
151 // Add DNP and Exclude from BOM properties if we have them
152 if( m_properties.count( "dnp" ) )
153 aOut->Print( nl + 1, "(property (name \"dnp\"))\n" );
154
155 if( m_properties.count( "exclude_from_bom" ) )
156 aOut->Print( nl + 1, "(property (name \"exclude_from_bom\"))\n" );
157 }
158
159 if( !( aCtl & CTL_OMIT_FILTERS ) && m_footprintFilters.GetCount() )
160 {
161 aOut->Print( nl+1, "(fp_filters" );
162
163 for( unsigned i = 0; i < m_footprintFilters.GetCount(); ++i )
164 aOut->Print( 0, " %s", aOut->Quotew( m_footprintFilters[i] ).c_str() );
165
166 aOut->Print( 0, ")\n" );
167 }
168
169 if( !( aCtl & CTL_OMIT_NETS ) && m_nets.size() )
170 {
171 int llen = aOut->Print( nl+1, "(nets " );
172
173 for( unsigned i = 0; i < m_nets.size(); ++i )
174 {
175 if( llen > 80 )
176 {
177 aOut->Print( 0, "\n" );
178 llen = aOut->Print( nl+1, " " );
179 }
180
181 llen += m_nets[i].Format( aOut, 0, aCtl );
182 }
183
184 aOut->Print( 0, ")\n" );
185 }
186
187 aOut->Print( nl, ")\n" ); // </ref>
188}
189
190
191void NETLIST::Format( const char* aDocName, OUTPUTFORMATTER* aOut, int aNestLevel, int aCtl )
192{
193 int nl = aNestLevel;
194
195 aOut->Print( nl, "(%s\n", aDocName );
196
197 for( unsigned i = 0; i < m_components.size(); i++ )
198 {
199 m_components[i].Format( aOut, nl+1, aCtl );
200 }
201
202 aOut->Print( nl, ")\n" );
203}
204
205
207{
208 m_components.push_back( aComponent );
209}
210
211
213{
214 m_groups.push_back( aComponent );
215}
216
218{
220 {
221 if( group.uuid == aUuid )
222 return &group;
223 }
224
225 return nullptr;
226}
227
228
229COMPONENT* NETLIST::GetComponentByReference( const wxString& aReference )
230{
231 COMPONENT* component = nullptr;
232
233 for( unsigned i = 0; i < m_components.size(); i++ )
234 {
235 if( m_components[i].GetReference() == aReference )
236 {
237 component = &m_components[i];
238 break;
239 }
240 }
241
242 return component;
243}
244
245
247{
248 if( aUuidPath.empty() )
249 return nullptr;
250
251 KIID comp_uuid = aUuidPath.back();
252 KIID_PATH base = aUuidPath;
253
254 if( !base.empty() )
255 base.pop_back();
256
257 for( COMPONENT& component : m_components )
258 {
259 const std::vector<KIID>& kiids = component.GetKIIDs();
260
261 if( base != component.GetPath() )
262 continue;
263
264 if( std::find( kiids.begin(), kiids.end(), comp_uuid ) != kiids.end() )
265 return &component;
266 }
267
268 return nullptr;
269}
270
271
273{
274 if( aUuid == 0 )
275 return nullptr;
276
277 for( COMPONENT& component : m_components )
278 {
279 for( const KIID& compUuid : component.GetKIIDs() )
280 {
281 if( aUuid == compUuid )
282 return &component;
283 }
284 }
285
286 return nullptr;
287}
288
289
293static bool ByFPID( const COMPONENT& ref, const COMPONENT& cmp )
294{
295 return ref.GetFPID() > cmp.GetFPID();
296}
297
298
300{
301 m_components.sort( ByFPID );
302}
303
304
308bool operator < ( const COMPONENT& item1, const COMPONENT& item2 )
309{
310 return StrNumCmp( item1.GetReference(), item2.GetReference(), true ) < 0;
311}
312
313
315{
316 m_components.sort();
317}
318
319
321{
322 for( unsigned i = 0; i < m_components.size(); i++ )
323 {
324 if( !m_components[i].GetFPID().empty() )
325 return true;
326 }
327
328 return false;
329}
330
331
333{
335 {
336 for( KIID& member : group.members )
337 {
338 COMPONENT* component = GetComponentByUuid( member );
339
340 if( component )
341 component->SetGroup( &group );
342 }
343 }
344}
Used to store the component pin name to net name (and pin function) associations stored in a netlist.
Definition pcb_netlist.h:47
wxString m_pinName
Definition pcb_netlist.h:75
int Format(OUTPUTFORMATTER *aOut, int aNestLevel, int aCtl)
wxString m_netName
Definition pcb_netlist.h:76
Store all of the related footprint information found in a netlist.
const COMPONENT_NET & GetNet(unsigned aIndex) const
nlohmann::ordered_map< wxString, wxString > m_fields
Component-specific user fields found in the netlist.
wxArrayString m_footprintFilters
const wxString & GetReference() const
void Format(OUTPUTFORMATTER *aOut, int aNestLevel, int aCtl)
KIID_PATH m_path
A fully specified path to the component (but not the component: [ sheetUUID, sheetUUID,...
wxString m_value
std::vector< KIID > m_kiids
A vector of possible KIIDs corresponding to all units in a symbol.
wxString m_reference
wxString m_name
The name of the component in m_library used when it was placed on the schematic.
void SetFootprint(FOOTPRINT *aFootprint)
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.
std::vector< COMPONENT_NET > m_nets
list of nets shared by the component pins
static COMPONENT_NET m_emptyNet
std::unique_ptr< FOOTPRINT > m_footprint
The FOOTPRINT loaded for #m_FPID.
std::map< wxString, wxString > m_properties
Component-specific properties found in the netlist.
const LIB_ID & GetFPID() const
void SetGroup(NETLIST_GROUP *aGroup)
void SetFPID(const LIB_ID &aFPID)
Definition footprint.h:270
void SetPath(const KIID_PATH &aPath)
Definition footprint.h:285
void SetReference(const wxString &aReference)
Definition footprint.h:667
void SetValue(const wxString &aValue)
Definition footprint.h:688
Definition kiid.h:49
void Format(const char *aDocName, OUTPUTFORMATTER *aOut, int aNestLevel, int aCtl=0)
void SortByFPID()
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 ...
COMPONENTS m_components
NETLIST_GROUPS m_groups
void SortByReference()
bool AnyFootprintsLinked() const
An interface used to output 8 bit text in a convenient way.
Definition richio.h:322
std::string Quotew(const wxString &aWrapee) const
Definition richio.cpp:548
int PRINTF_FUNC_N Print(int nestLevel, const char *fmt,...)
Format and write text to the output stream.
Definition richio.cpp:463
#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 ...