KiCad PCB EDA Suite
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 (C) 1992-2021 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 <string_utils.h>
28#include "pcb_netlist.h"
29#include <footprint.h>
30
31
32int COMPONENT_NET::Format( OUTPUTFORMATTER* aOut, int aNestLevel, int aCtl )
33{
34 return aOut->Print( aNestLevel, "(pin_net %s %s)",
35 aOut->Quotew( m_pinName ).c_str(),
36 aOut->Quotew( m_netName ).c_str() );
37}
38
39
41{
42 m_footprint.reset( aFootprint );
44
45 if( !m_kiids.empty() )
46 path.push_back( m_kiids.front() );
47
48 if( aFootprint == nullptr )
49 return;
50
51 aFootprint->SetReference( m_reference );
52 aFootprint->SetValue( m_value );
53 aFootprint->SetFPID( m_fpid );
54 aFootprint->SetPath( path );
55 aFootprint->SetProperties( m_properties );
56}
57
58
60
61
62const COMPONENT_NET& COMPONENT::GetNet( const wxString& aPinName ) const
63{
64 for( const COMPONENT_NET& net : m_nets )
65 {
66 if( net.GetPinName() == aPinName )
67 return net;
68 }
69
70 return m_emptyNet;
71}
72
73
74void COMPONENT::Format( OUTPUTFORMATTER* aOut, int aNestLevel, int aCtl )
75{
76 int nl = aNestLevel;
77
78 aOut->Print( nl, "(ref %s ", aOut->Quotew( m_reference ).c_str() );
79 aOut->Print( 0, "(fpid %s)\n", aOut->Quotew( m_fpid.Format() ).c_str() );
80
81 if( !( aCtl & CTL_OMIT_EXTRA ) )
82 {
83 aOut->Print( nl+1, "(value %s)\n", aOut->Quotew( m_value ).c_str() );
84 aOut->Print( nl+1, "(name %s)\n", aOut->Quotew( m_name ).c_str() );
85 aOut->Print( nl+1, "(library %s)\n", aOut->Quotew( m_library ).c_str() );
86
87 wxString path;
88
89 for( const KIID& pathStep : m_path )
90 path += '/' + pathStep.AsString();
91
92 if( !( aCtl & CTL_OMIT_FP_UUID ) && !m_kiids.empty() )
93 path += '/' + m_kiids.front().AsString();
94
95 aOut->Print( nl+1, "(timestamp %s)\n", aOut->Quotew( path ).c_str() );
96 }
97
98 if( !( aCtl & CTL_OMIT_FILTERS ) && m_footprintFilters.GetCount() )
99 {
100 aOut->Print( nl+1, "(fp_filters" );
101
102 for( unsigned i = 0; i < m_footprintFilters.GetCount(); ++i )
103 aOut->Print( 0, " %s", aOut->Quotew( m_footprintFilters[i] ).c_str() );
104
105 aOut->Print( 0, ")\n" );
106 }
107
108 if( !( aCtl & CTL_OMIT_NETS ) && m_nets.size() )
109 {
110 int llen = aOut->Print( nl+1, "(nets " );
111
112 for( unsigned i = 0; i < m_nets.size(); ++i )
113 {
114 if( llen > 80 )
115 {
116 aOut->Print( 0, "\n" );
117 llen = aOut->Print( nl+1, " " );
118 }
119
120 llen += m_nets[i].Format( aOut, 0, aCtl );
121 }
122
123 aOut->Print( 0, ")\n" );
124 }
125
126 aOut->Print( nl, ")\n" ); // </ref>
127}
128
129
130void NETLIST::Format( const char* aDocName, OUTPUTFORMATTER* aOut, int aNestLevel, int aCtl )
131{
132 int nl = aNestLevel;
133
134 aOut->Print( nl, "(%s\n", aDocName );
135
136 for( unsigned i = 0; i < m_components.size(); i++ )
137 {
138 m_components[i].Format( aOut, nl+1, aCtl );
139 }
140
141 aOut->Print( nl, ")\n" );
142}
143
144
146{
147 m_components.push_back( aComponent );
148}
149
150
151COMPONENT* NETLIST::GetComponentByReference( const wxString& aReference )
152{
153 COMPONENT* component = nullptr;
154
155 for( unsigned i = 0; i < m_components.size(); i++ )
156 {
157 if( m_components[i].GetReference() == aReference )
158 {
159 component = &m_components[i];
160 break;
161 }
162 }
163
164 return component;
165}
166
167
169{
170 if( aUuidPath.empty() )
171 return nullptr;
172
173 KIID comp_uuid = aUuidPath.back();
174 KIID_PATH base = aUuidPath;
175
176 if( !base.empty() )
177 base.pop_back();
178
179 for( COMPONENT& component : m_components )
180 {
181 const std::vector<KIID>& kiids = component.GetKIIDs();
182
183 if( base != component.GetPath() )
184 continue;
185
186 if( std::find( kiids.begin(), kiids.end(), comp_uuid ) != kiids.end() )
187 return &component;
188 }
189
190 return nullptr;
191}
192
193
197static bool ByFPID( const COMPONENT& ref, const COMPONENT& cmp )
198{
199 return ref.GetFPID() > cmp.GetFPID();
200}
201
202
204{
205 m_components.sort( ByFPID );
206}
207
208
212bool operator < ( const COMPONENT& item1, const COMPONENT& item2 )
213{
214 return StrNumCmp( item1.GetReference(), item2.GetReference(), true ) < 0;
215}
216
217
219{
220 m_components.sort();
221}
222
223
225{
226 for( unsigned i = 0; i < m_components.size(); i++ )
227 {
228 if( !m_components[i].GetFPID().empty() )
229 return true;
230 }
231
232 return false;
233}
234
235
Used to store the component pin name to net name (and pin function) associations stored in a netlist.
Definition: pcb_netlist.h:44
wxString m_pinName
Definition: pcb_netlist.h:72
int Format(OUTPUTFORMATTER *aOut, int aNestLevel, int aCtl)
Definition: pcb_netlist.cpp:32
wxString m_netName
Definition: pcb_netlist.h:73
Store all of the related footprint information found in a netlist.
Definition: pcb_netlist.h:85
const COMPONENT_NET & GetNet(unsigned aIndex) const
Definition: pcb_netlist.h:111
wxArrayString m_footprintFilters
Definition: pcb_netlist.h:170
const wxString & GetReference() const
Definition: pcb_netlist.h:126
void Format(OUTPUTFORMATTER *aOut, int aNestLevel, int aCtl)
Definition: pcb_netlist.cpp:74
KIID_PATH m_path
A fully specified path to the component (but not the component: [ sheetUUID, sheetUUID,...
Definition: pcb_netlist.h:176
wxString m_value
Definition: pcb_netlist.h:173
std::vector< KIID > m_kiids
A vector of possible KIIDs corresponding to all units in a symbol.
Definition: pcb_netlist.h:179
wxString m_reference
Definition: pcb_netlist.h:172
wxString m_name
The name of the component in m_library used when it was placed on the schematic.
Definition: pcb_netlist.h:182
void SetFootprint(FOOTPRINT *aFootprint)
Definition: pcb_netlist.cpp:40
wxString m_library
The name of the component library where m_name was found.
Definition: pcb_netlist.h:185
LIB_ID m_fpid
The LIB_ID of the footprint assigned to the component.
Definition: pcb_netlist.h:188
std::vector< COMPONENT_NET > m_nets
list of nets shared by the component pins
Definition: pcb_netlist.h:168
static COMPONENT_NET m_emptyNet
Definition: pcb_netlist.h:201
std::unique_ptr< FOOTPRINT > m_footprint
The FOOTPRINT loaded for #m_FPID.
Definition: pcb_netlist.h:196
std::map< wxString, wxString > m_properties
Component-specific properties found in the netlist.
Definition: pcb_netlist.h:199
const LIB_ID & GetFPID() const
Definition: pcb_netlist.h:138
void SetFPID(const LIB_ID &aFPID)
Definition: footprint.h:204
void SetPath(const KIID_PATH &aPath)
Definition: footprint.h:216
void SetReference(const wxString &aReference)
Definition: footprint.h:519
void SetValue(const wxString &aValue)
Definition: footprint.h:546
void SetProperties(const std::map< wxString, wxString > &aProps)
Definition: footprint.h:566
Definition: kiid.h:48
UTF8 Format() const
Definition: lib_id.cpp:117
void Format(const char *aDocName, OUTPUTFORMATTER *aOut, int aNestLevel, int aCtl=0)
void SortByFPID()
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.
COMPONENTS m_components
Definition: pcb_netlist.h:299
void SortByReference()
bool AnyFootprintsLinked() const
An interface used to output 8 bit text in a convenient way.
Definition: richio.h:310
std::string Quotew(const wxString &aWrapee) const
Definition: richio.cpp:499
int PRINTF_FUNC Print(int nestLevel, const char *fmt,...)
Format and write text to the output stream.
Definition: richio.cpp:431
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.
#define CTL_OMIT_EXTRA
Definition: pcb_netlist.h:286
#define CTL_OMIT_NETS
Definition: pcb_netlist.h:287
#define CTL_OMIT_FP_UUID
Don't prefix the footprint UUID to the sheet path.
Definition: pcb_netlist.h:289
#define CTL_OMIT_FILTERS
Definition: pcb_netlist.h:288
int StrNumCmp(const wxString &aString1, const wxString &aString2, bool aIgnoreCase)
Compare two strings with alphanumerical content.