KiCad PCB EDA Suite
Loading...
Searching...
No Matches
common/netlist_reader/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 The KiCad Developers, see AUTHORS.txt for contributors.
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version 2
9 * of the License, or (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, you may find one here:
18 * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
19 * or you may search the http://www.gnu.org website for the version 2 license,
20 * or you may write to the Free Software Foundation, Inc.,
21 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
22 */
23
24
26
27#include <richio.h>
28#include <string_utils.h>
29#include <wx/tokenzr.h>
30#include <wx/log.h>
31
32
33int COMPONENT_NET::Format( OUTPUTFORMATTER* aOut, int aNestLevel, int aCtl )
34{
35 return aOut->Print( aNestLevel, "(pin_net %s %s)",
36 aOut->Quotew( m_pinName ).c_str(),
37 aOut->Quotew( m_netName ).c_str() );
38}
39
40
42
43
44const COMPONENT_NET& COMPONENT::GetNet( const wxString& aPinName ) const
45{
46 wxLogTrace( wxT( "NETLIST_STACKED_PINS" ),
47 wxT( "Looking for pin '%s' in component '%s'" ),
48 aPinName, m_reference );
49
50 for( const COMPONENT_NET& net : m_nets )
51 {
52 wxLogTrace( wxT( "NETLIST_STACKED_PINS" ),
53 wxT( " Checking net pin name '%s'" ),
54 net.GetPinName() );
55
56 if( net.GetPinName() == aPinName )
57 {
58 wxLogTrace( wxT( "NETLIST_STACKED_PINS" ),
59 wxT( " Found exact match for pin '%s'" ),
60 aPinName );
61 return net;
62 }
63
64 // Check if this net's pin name is a stacked pin notation that expands to include aPinName
65 std::vector<wxString> expandedPins = ExpandStackedPinNotation( net.GetPinName() );
66 if( !expandedPins.empty() )
67 {
68 wxLogTrace( wxT( "NETLIST_STACKED_PINS" ),
69 wxT( " Pin name '%s' expanded to %zu pins" ),
70 net.GetPinName(), expandedPins.size() );
71
72 for( const wxString& expandedPin : expandedPins )
73 {
74 wxLogTrace( wxT( "NETLIST_STACKED_PINS" ),
75 wxT( " Checking expanded pin '%s'" ),
76 expandedPin );
77 if( expandedPin == aPinName )
78 {
79 wxLogTrace( wxT( "NETLIST_STACKED_PINS" ),
80 wxT( " Found match for pin '%s' in stacked notation '%s'" ),
81 aPinName, net.GetPinName() );
82 return net;
83 }
84 }
85 }
86 }
87
88 wxLogTrace( wxT( "NETLIST_STACKED_PINS" ),
89 wxT( " No net found for pin '%s'" ),
90 aPinName );
91 return m_emptyNet;
92}
93
94
95void COMPONENT::Format( OUTPUTFORMATTER* aOut, int aNestLevel, int aCtl )
96{
97 int nl = aNestLevel;
98
99 aOut->Print( nl, "(ref %s ", aOut->Quotew( m_reference ).c_str() );
100 aOut->Print( 0, "(fpid %s)\n", aOut->Quotew( m_fpid.Format() ).c_str() );
101
102 if( !( aCtl & CTL_OMIT_EXTRA ) )
103 {
104 aOut->Print( nl+1, "(value %s)\n", aOut->Quotew( m_value ).c_str() );
105 aOut->Print( nl+1, "(name %s)\n", aOut->Quotew( m_name ).c_str() );
106 aOut->Print( nl+1, "(library %s)\n", aOut->Quotew( m_library ).c_str() );
107
108 wxString path;
109
110 for( const KIID& pathStep : m_path )
111 path += '/' + pathStep.AsString();
112
113 if( !( aCtl & CTL_OMIT_FP_UUID ) && !m_kiids.empty() )
114 path += '/' + m_kiids.front().AsString();
115
116 aOut->Print( nl+1, "(timestamp %s)\n", aOut->Quotew( path ).c_str() );
117
118 // Add all fields as a (field) under a (fields) node
119 aOut->Print( nl + 1, "(fields" );
120
121 for( std::pair<wxString, wxString> field : m_fields )
122 aOut->Print( nl + 2, "\n(field (name %s) %s)", aOut->Quotew( field.first ).c_str(),
123 aOut->Quotew( field.second ).c_str() );
124
125 aOut->Print( 0, ")\n" );
126
127 // Add DNP and Exclude from BOM properties if we have them
128 if( m_properties.count( "dnp" ) )
129 aOut->Print( nl + 1, "(property (name \"dnp\"))\n" );
130
131 if( m_properties.count( "exclude_from_bom" ) )
132 aOut->Print( nl + 1, "(property (name \"exclude_from_bom\"))\n" );
133 }
134
135 if( !( aCtl & CTL_OMIT_FILTERS ) && m_footprintFilters.GetCount() )
136 {
137 aOut->Print( nl+1, "(fp_filters" );
138
139 for( unsigned i = 0; i < m_footprintFilters.GetCount(); ++i )
140 aOut->Print( 0, " %s", aOut->Quotew( m_footprintFilters[i] ).c_str() );
141
142 aOut->Print( 0, ")\n" );
143 }
144
145 if( !( aCtl & CTL_OMIT_NETS ) && m_nets.size() )
146 {
147 int llen = aOut->Print( nl+1, "(nets " );
148
149 for( unsigned i = 0; i < m_nets.size(); ++i )
150 {
151 if( llen > 80 )
152 {
153 aOut->Print( 0, "\n" );
154 llen = aOut->Print( nl+1, " " );
155 }
156
157 llen += m_nets[i].Format( aOut, 0, aCtl );
158 }
159
160 aOut->Print( 0, ")\n" );
161 }
162
163 aOut->Print( nl, ")\n" ); // </ref>
164}
165
166
167void NETLIST::Format( const char* aDocName, OUTPUTFORMATTER* aOut, int aNestLevel, int aCtl )
168{
169 int nl = aNestLevel;
170
171 aOut->Print( nl, "(%s\n", aDocName );
172
173 for( unsigned i = 0; i < m_components.size(); i++ )
174 {
175 m_components[i].Format( aOut, nl+1, aCtl );
176 }
177
178 aOut->Print( nl, ")\n" );
179}
180
181
183{
184 m_components.push_back( aComponent );
185}
186
187
189{
190 m_groups.push_back( aComponent );
191}
192
194{
196 {
197 if( group.uuid == aUuid )
198 return &group;
199 }
200
201 return nullptr;
202}
203
204
205COMPONENT* NETLIST::GetComponentByReference( const wxString& aReference )
206{
207 COMPONENT* component = nullptr;
208
209 for( unsigned i = 0; i < m_components.size(); i++ )
210 {
211 if( m_components[i].GetReference() == aReference )
212 {
213 component = &m_components[i];
214 break;
215 }
216 }
217
218 return component;
219}
220
221
223{
224 if( aUuidPath.empty() )
225 return nullptr;
226
227 KIID comp_uuid = aUuidPath.back();
228 KIID_PATH base = aUuidPath;
229
230 if( !base.empty() )
231 base.pop_back();
232
233 for( COMPONENT& component : m_components )
234 {
235 const std::vector<KIID>& kiids = component.GetKIIDs();
236
237 if( base != component.GetPath() )
238 continue;
239
240 if( std::find( kiids.begin(), kiids.end(), comp_uuid ) != kiids.end() )
241 return &component;
242 }
243
244 return nullptr;
245}
246
247
249{
250 if( aUuid == 0 )
251 return nullptr;
252
253 for( COMPONENT& component : m_components )
254 {
255 for( const KIID& compUuid : component.GetKIIDs() )
256 {
257 if( aUuid == compUuid )
258 return &component;
259 }
260 }
261
262 return nullptr;
263}
264
265
269static bool ByFPID( const COMPONENT& ref, const COMPONENT& cmp )
270{
271 return ref.GetFPID() > cmp.GetFPID();
272}
273
274
276{
277 m_components.sort( ByFPID );
278}
279
280
284bool operator < ( const COMPONENT& item1, const COMPONENT& item2 )
285{
286 return StrNumCmp( item1.GetReference(), item2.GetReference(), true ) < 0;
287}
288
289
291{
292 m_components.sort();
293}
294
295
297{
298 for( unsigned i = 0; i < m_components.size(); i++ )
299 {
300 if( !m_components[i].GetFPID().empty() )
301 return true;
302 }
303
304 return false;
305}
306
307
309{
311 {
312 for( KIID& member : group.members )
313 {
314 COMPONENT* component = GetComponentByUuid( member );
315
316 if( component )
317 component->SetGroup( &group );
318 }
319 }
320}
Used to store the component pin name to net name (and pin function) associations stored in a netlist.
int Format(OUTPUTFORMATTER *aOut, int aNestLevel, int aCtl)
Store all of the related component information found in a netlist.
const COMPONENT_NET & GetNet(unsigned aIndex) const
std::vector< COMPONENT_NET > m_nets
list of nets shared by the component pins
std::map< wxString, wxString > m_properties
Component-specific properties 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,...
nlohmann::ordered_map< wxString, wxString > m_fields
Component-specific user fields found in the netlist.
wxString m_name
The name of the component in m_library used when it was placed on the schematic.
std::vector< KIID > m_kiids
A vector of possible KIIDs corresponding to all units in a symbol.
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.
static COMPONENT_NET m_emptyNet
const LIB_ID & GetFPID() const
void SetGroup(NETLIST_GROUP *aGroup)
Definition kiid.h:49
void Format(const char *aDocName, OUTPUTFORMATTER *aOut, int aNestLevel, int aCtl=0)
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 ...
An interface used to output 8 bit text in a convenient way.
Definition richio.h:323
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
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 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)
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 ...