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 (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}
56
57
59
60
61const COMPONENT_NET& COMPONENT::GetNet( const wxString& aPinName ) const
62{
63 for( const COMPONENT_NET& net : m_nets )
64 {
65 if( net.GetPinName() == aPinName )
66 return net;
67 }
68
69 return m_emptyNet;
70}
71
72
73void COMPONENT::Format( OUTPUTFORMATTER* aOut, int aNestLevel, int aCtl )
74{
75 int nl = aNestLevel;
76
77 aOut->Print( nl, "(ref %s ", aOut->Quotew( m_reference ).c_str() );
78 aOut->Print( 0, "(fpid %s)\n", aOut->Quotew( m_fpid.Format() ).c_str() );
79
80 if( !( aCtl & CTL_OMIT_EXTRA ) )
81 {
82 aOut->Print( nl+1, "(value %s)\n", aOut->Quotew( m_value ).c_str() );
83 aOut->Print( nl+1, "(name %s)\n", aOut->Quotew( m_name ).c_str() );
84 aOut->Print( nl+1, "(library %s)\n", aOut->Quotew( m_library ).c_str() );
85
86 wxString path;
87
88 for( const KIID& pathStep : m_path )
89 path += '/' + pathStep.AsString();
90
91 if( !( aCtl & CTL_OMIT_FP_UUID ) && !m_kiids.empty() )
92 path += '/' + m_kiids.front().AsString();
93
94 aOut->Print( nl+1, "(timestamp %s)\n", aOut->Quotew( path ).c_str() );
95
96 // Add all fields as a (field) under a (fields) node
97 aOut->Print( nl + 1, "(fields" );
98
99 for( std::pair<wxString, wxString> field : m_fields )
100 aOut->Print( nl + 2, "\n(field (name %s) %s)", aOut->Quotew( field.first ).c_str(),
101 aOut->Quotew( field.second ).c_str() );
102
103 aOut->Print( 0, ")\n" );
104
105 // Add DNP and Exclude from BOM properties if we have them
106 if( m_properties.count( "dnp" ) )
107 aOut->Print( nl + 1, "(property (name \"dnp\"))\n" );
108
109 if( m_properties.count( "exclude_from_bom" ) )
110 aOut->Print( nl + 1, "(property (name \"exclude_from_bom\"))\n" );
111 }
112
113 if( !( aCtl & CTL_OMIT_FILTERS ) && m_footprintFilters.GetCount() )
114 {
115 aOut->Print( nl+1, "(fp_filters" );
116
117 for( unsigned i = 0; i < m_footprintFilters.GetCount(); ++i )
118 aOut->Print( 0, " %s", aOut->Quotew( m_footprintFilters[i] ).c_str() );
119
120 aOut->Print( 0, ")\n" );
121 }
122
123 if( !( aCtl & CTL_OMIT_NETS ) && m_nets.size() )
124 {
125 int llen = aOut->Print( nl+1, "(nets " );
126
127 for( unsigned i = 0; i < m_nets.size(); ++i )
128 {
129 if( llen > 80 )
130 {
131 aOut->Print( 0, "\n" );
132 llen = aOut->Print( nl+1, " " );
133 }
134
135 llen += m_nets[i].Format( aOut, 0, aCtl );
136 }
137
138 aOut->Print( 0, ")\n" );
139 }
140
141 aOut->Print( nl, ")\n" ); // </ref>
142}
143
144
145void NETLIST::Format( const char* aDocName, OUTPUTFORMATTER* aOut, int aNestLevel, int aCtl )
146{
147 int nl = aNestLevel;
148
149 aOut->Print( nl, "(%s\n", aDocName );
150
151 for( unsigned i = 0; i < m_components.size(); i++ )
152 {
153 m_components[i].Format( aOut, nl+1, aCtl );
154 }
155
156 aOut->Print( nl, ")\n" );
157}
158
159
161{
162 m_components.push_back( aComponent );
163}
164
165
166COMPONENT* NETLIST::GetComponentByReference( const wxString& aReference )
167{
168 COMPONENT* component = nullptr;
169
170 for( unsigned i = 0; i < m_components.size(); i++ )
171 {
172 if( m_components[i].GetReference() == aReference )
173 {
174 component = &m_components[i];
175 break;
176 }
177 }
178
179 return component;
180}
181
182
184{
185 if( aUuidPath.empty() )
186 return nullptr;
187
188 KIID comp_uuid = aUuidPath.back();
189 KIID_PATH base = aUuidPath;
190
191 if( !base.empty() )
192 base.pop_back();
193
194 for( COMPONENT& component : m_components )
195 {
196 const std::vector<KIID>& kiids = component.GetKIIDs();
197
198 if( base != component.GetPath() )
199 continue;
200
201 if( std::find( kiids.begin(), kiids.end(), comp_uuid ) != kiids.end() )
202 return &component;
203 }
204
205 return nullptr;
206}
207
208
212static bool ByFPID( const COMPONENT& ref, const COMPONENT& cmp )
213{
214 return ref.GetFPID() > cmp.GetFPID();
215}
216
217
219{
220 m_components.sort( ByFPID );
221}
222
223
227bool operator < ( const COMPONENT& item1, const COMPONENT& item2 )
228{
229 return StrNumCmp( item1.GetReference(), item2.GetReference(), true ) < 0;
230}
231
232
234{
235 m_components.sort();
236}
237
238
240{
241 for( unsigned i = 0; i < m_components.size(); i++ )
242 {
243 if( !m_components[i].GetFPID().empty() )
244 return true;
245 }
246
247 return false;
248}
249
250
Used to store the component pin name to net name (and pin function) associations stored in a netlist.
Definition: pcb_netlist.h:45
wxString m_pinName
Definition: pcb_netlist.h:73
int Format(OUTPUTFORMATTER *aOut, int aNestLevel, int aCtl)
Definition: pcb_netlist.cpp:32
wxString m_netName
Definition: pcb_netlist.h:74
Store all of the related footprint information found in a netlist.
Definition: pcb_netlist.h:86
const COMPONENT_NET & GetNet(unsigned aIndex) const
Definition: pcb_netlist.h:112
nlohmann::ordered_map< wxString, wxString > m_fields
Component-specific user fields found in the netlist.
Definition: pcb_netlist.h:209
wxArrayString m_footprintFilters
Definition: pcb_netlist.h:177
const wxString & GetReference() const
Definition: pcb_netlist.h:127
void Format(OUTPUTFORMATTER *aOut, int aNestLevel, int aCtl)
Definition: pcb_netlist.cpp:73
KIID_PATH m_path
A fully specified path to the component (but not the component: [ sheetUUID, sheetUUID,...
Definition: pcb_netlist.h:183
wxString m_value
Definition: pcb_netlist.h:180
std::vector< KIID > m_kiids
A vector of possible KIIDs corresponding to all units in a symbol.
Definition: pcb_netlist.h:186
wxString m_reference
Definition: pcb_netlist.h:179
wxString m_name
The name of the component in m_library used when it was placed on the schematic.
Definition: pcb_netlist.h:189
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:192
LIB_ID m_fpid
The LIB_ID of the footprint assigned to the component.
Definition: pcb_netlist.h:195
std::vector< COMPONENT_NET > m_nets
list of nets shared by the component pins
Definition: pcb_netlist.h:175
static COMPONENT_NET m_emptyNet
Definition: pcb_netlist.h:211
std::unique_ptr< FOOTPRINT > m_footprint
The FOOTPRINT loaded for #m_FPID.
Definition: pcb_netlist.h:203
std::map< wxString, wxString > m_properties
Component-specific properties found in the netlist.
Definition: pcb_netlist.h:206
const LIB_ID & GetFPID() const
Definition: pcb_netlist.h:145
void SetFPID(const LIB_ID &aFPID)
Definition: footprint.h:234
void SetPath(const KIID_PATH &aPath)
Definition: footprint.h:250
void SetReference(const wxString &aReference)
Definition: footprint.h:594
void SetValue(const wxString &aValue)
Definition: footprint.h:615
Definition: kiid.h:49
UTF8 Format() const
Definition: lib_id.cpp:118
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:309
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:526
int PRINTF_FUNC Print(int nestLevel, const char *fmt,...)
Format and write text to the output stream.
Definition: richio.cpp:458
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:296
#define CTL_OMIT_NETS
Definition: pcb_netlist.h:297
#define CTL_OMIT_FP_UUID
Don't prefix the footprint UUID to the sheet path.
Definition: pcb_netlist.h:299
#define CTL_OMIT_FILTERS
Definition: pcb_netlist.h:298
int StrNumCmp(const wxString &aString1, const wxString &aString2, bool aIgnoreCase)
Compare two strings with alphanumerical content.