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
127void COMPONENT::Format( OUTPUTFORMATTER* aOut, int aNestLevel, int aCtl )
128{
129 int nl = aNestLevel;
130
131 aOut->Print( nl, "(ref %s ", aOut->Quotew( m_reference ).c_str() );
132 aOut->Print( 0, "(fpid %s)\n", aOut->Quotew( m_fpid.Format() ).c_str() );
133
134 if( !( aCtl & CTL_OMIT_EXTRA ) )
135 {
136 aOut->Print( nl+1, "(value %s)\n", aOut->Quotew( m_value ).c_str() );
137 aOut->Print( nl+1, "(name %s)\n", aOut->Quotew( m_name ).c_str() );
138 aOut->Print( nl+1, "(library %s)\n", aOut->Quotew( m_library ).c_str() );
139
140 wxString path;
141
142 for( const KIID& pathStep : m_path )
143 path += '/' + pathStep.AsString();
144
145 if( !( aCtl & CTL_OMIT_FP_UUID ) && !m_kiids.empty() )
146 path += '/' + m_kiids.front().AsString();
147
148 aOut->Print( nl+1, "(timestamp %s)\n", aOut->Quotew( path ).c_str() );
149
150 // Add all fields as a (field) under a (fields) node
151 aOut->Print( nl + 1, "(fields" );
152
153 for( std::pair<wxString, wxString> field : m_fields )
154 aOut->Print( nl + 2, "\n(field (name %s) %s)", aOut->Quotew( field.first ).c_str(),
155 aOut->Quotew( field.second ).c_str() );
156
157 aOut->Print( 0, ")\n" );
158
159 // Add DNP and Exclude from BOM properties if we have them
160 if( m_properties.count( "dnp" ) )
161 aOut->Print( nl + 1, "(property (name \"dnp\"))\n" );
162
163 if( m_properties.count( "exclude_from_bom" ) )
164 aOut->Print( nl + 1, "(property (name \"exclude_from_bom\"))\n" );
165 }
166
167 if( !( aCtl & CTL_OMIT_FILTERS ) && m_footprintFilters.GetCount() )
168 {
169 aOut->Print( nl+1, "(fp_filters" );
170
171 for( unsigned i = 0; i < m_footprintFilters.GetCount(); ++i )
172 aOut->Print( 0, " %s", aOut->Quotew( m_footprintFilters[i] ).c_str() );
173
174 aOut->Print( 0, ")\n" );
175 }
176
177 if( !( aCtl & CTL_OMIT_NETS ) && m_nets.size() )
178 {
179 int llen = aOut->Print( nl+1, "(nets " );
180
181 for( unsigned i = 0; i < m_nets.size(); ++i )
182 {
183 if( llen > 80 )
184 {
185 aOut->Print( 0, "\n" );
186 llen = aOut->Print( nl+1, " " );
187 }
188
189 llen += m_nets[i].Format( aOut, 0, aCtl );
190 }
191
192 aOut->Print( 0, ")\n" );
193 }
194
195 aOut->Print( nl, ")\n" ); // </ref>
196}
197
198
199void NETLIST::Format( const char* aDocName, OUTPUTFORMATTER* aOut, int aNestLevel, int aCtl )
200{
201 int nl = aNestLevel;
202
203 aOut->Print( nl, "(%s\n", aDocName );
204
205 for( unsigned i = 0; i < m_components.size(); i++ )
206 {
207 m_components[i].Format( aOut, nl+1, aCtl );
208 }
209
210 aOut->Print( nl, ")\n" );
211}
212
213
215{
216 m_components.push_back( aComponent );
217}
218
219
221{
222 m_groups.push_back( aComponent );
223}
224
226{
228 {
229 if( group.uuid == aUuid )
230 return &group;
231 }
232
233 return nullptr;
234}
235
236
237COMPONENT* NETLIST::GetComponentByReference( const wxString& aReference )
238{
239 COMPONENT* component = nullptr;
240
241 for( unsigned i = 0; i < m_components.size(); i++ )
242 {
243 if( m_components[i].GetReference() == aReference )
244 {
245 component = &m_components[i];
246 break;
247 }
248 }
249
250 return component;
251}
252
253
255{
256 if( aUuidPath.empty() )
257 return nullptr;
258
259 KIID comp_uuid = aUuidPath.back();
260 KIID_PATH base = aUuidPath;
261
262 if( !base.empty() )
263 base.pop_back();
264
265 for( COMPONENT& component : m_components )
266 {
267 const std::vector<KIID>& kiids = component.GetKIIDs();
268
269 if( base != component.GetPath() )
270 continue;
271
272 if( std::find( kiids.begin(), kiids.end(), comp_uuid ) != kiids.end() )
273 return &component;
274 }
275
276 return nullptr;
277}
278
279
281{
282 if( aUuid == 0 )
283 return nullptr;
284
285 for( COMPONENT& component : m_components )
286 {
287 for( const KIID& compUuid : component.GetKIIDs() )
288 {
289 if( aUuid == compUuid )
290 return &component;
291 }
292 }
293
294 return nullptr;
295}
296
297
301static bool ByFPID( const COMPONENT& ref, const COMPONENT& cmp )
302{
303 return ref.GetFPID() > cmp.GetFPID();
304}
305
306
308{
309 m_components.sort( ByFPID );
310}
311
312
316bool operator < ( const COMPONENT& item1, const COMPONENT& item2 )
317{
318 return StrNumCmp( item1.GetReference(), item2.GetReference(), true ) < 0;
319}
320
321
323{
324 m_components.sort();
325}
326
327
329{
330 for( unsigned i = 0; i < m_components.size(); i++ )
331 {
332 if( !m_components[i].GetFPID().empty() )
333 return true;
334 }
335
336 return false;
337}
338
339
341{
343 {
344 for( KIID& member : group.members )
345 {
346 COMPONENT* component = GetComponentByUuid( member );
347
348 if( component )
349 component->SetGroup( &group );
350 }
351 }
352}
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
std::vector< UNIT_INFO > m_units
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
void SetUnitInfo(const std::vector< FP_UNIT_INFO > &aUnits)
Definition footprint.h:759
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:550
int PRINTF_FUNC_N Print(int nestLevel, const char *fmt,...)
Format and write text to the output stream.
Definition richio.cpp:465
#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 ...