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