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, see <https://www.gnu.org/licenses/>.
18 */
19
20
22
23#include <richio.h>
24#include <string_utils.h>
25#include <wx/tokenzr.h>
26#include <wx/log.h>
27
28
29int COMPONENT_NET::Format( OUTPUTFORMATTER* aOut, int aNestLevel, int aCtl )
30{
31 return aOut->Print( aNestLevel, "(pin_net %s %s)",
32 aOut->Quotew( m_pinName ).c_str(),
33 aOut->Quotew( m_netName ).c_str() );
34}
35
36
38
39
40const COMPONENT_NET& COMPONENT::GetNet( const wxString& aPinName ) const
41{
42 wxLogTrace( wxT( "NETLIST_STACKED_PINS" ),
43 wxT( "Looking for pin '%s' in component '%s'" ),
44 aPinName, m_reference );
45
46 for( const COMPONENT_NET& net : m_nets )
47 {
48 wxLogTrace( wxT( "NETLIST_STACKED_PINS" ),
49 wxT( " Checking net pin name '%s'" ),
50 net.GetPinName() );
51
52 if( net.GetPinName() == aPinName )
53 {
54 wxLogTrace( wxT( "NETLIST_STACKED_PINS" ),
55 wxT( " Found exact match for pin '%s'" ),
56 aPinName );
57 return net;
58 }
59
60 // Check if this net's pin name is a stacked pin notation that expands to include aPinName
61 std::vector<wxString> expandedPins = ExpandStackedPinNotation( net.GetPinName() );
62 if( !expandedPins.empty() )
63 {
64 wxLogTrace( wxT( "NETLIST_STACKED_PINS" ),
65 wxT( " Pin name '%s' expanded to %zu pins" ),
66 net.GetPinName(), expandedPins.size() );
67
68 for( const wxString& expandedPin : expandedPins )
69 {
70 wxLogTrace( wxT( "NETLIST_STACKED_PINS" ),
71 wxT( " Checking expanded pin '%s'" ),
72 expandedPin );
73 if( expandedPin == aPinName )
74 {
75 wxLogTrace( wxT( "NETLIST_STACKED_PINS" ),
76 wxT( " Found match for pin '%s' in stacked notation '%s'" ),
77 aPinName, net.GetPinName() );
78 return net;
79 }
80 }
81 }
82 }
83
84 wxLogTrace( wxT( "NETLIST_STACKED_PINS" ),
85 wxT( " No net found for pin '%s'" ),
86 aPinName );
87 return m_emptyNet;
88}
89
90
91void COMPONENT::Format( OUTPUTFORMATTER* aOut, int aNestLevel, int aCtl )
92{
93 int nl = aNestLevel;
94
95 aOut->Print( nl, "(ref %s ", aOut->Quotew( m_reference ).c_str() );
96 aOut->Print( 0, "(fpid %s)\n", aOut->Quotew( m_fpid.Format() ).c_str() );
97
98 if( !( aCtl & CTL_OMIT_EXTRA ) )
99 {
100 aOut->Print( nl+1, "(value %s)\n", aOut->Quotew( m_value ).c_str() );
101 aOut->Print( nl+1, "(name %s)\n", aOut->Quotew( m_name ).c_str() );
102 aOut->Print( nl+1, "(library %s)\n", aOut->Quotew( m_library ).c_str() );
103
104 wxString path;
105
106 for( const KIID& pathStep : m_path )
107 path += '/' + pathStep.AsString();
108
109 if( !( aCtl & CTL_OMIT_FP_UUID ) && !m_kiids.empty() )
110 path += '/' + m_kiids.front().AsString();
111
112 aOut->Print( nl+1, "(timestamp %s)\n", aOut->Quotew( path ).c_str() );
113
114 // Add all fields as a (field) under a (fields) node
115 aOut->Print( nl + 1, "(fields" );
116
117 for( std::pair<wxString, wxString> field : m_fields )
118 aOut->Print( nl + 2, "\n(field (name %s) %s)", aOut->Quotew( field.first ).c_str(),
119 aOut->Quotew( field.second ).c_str() );
120
121 aOut->Print( 0, ")\n" );
122
123 // Add DNP and Exclude from BOM properties if we have them
124 if( m_properties.count( "dnp" ) )
125 aOut->Print( nl + 1, "(property (name \"dnp\"))\n" );
126
127 if( m_properties.count( "exclude_from_bom" ) )
128 aOut->Print( nl + 1, "(property (name \"exclude_from_bom\"))\n" );
129 }
130
131 if( !( aCtl & CTL_OMIT_FILTERS ) && m_footprintFilters.GetCount() )
132 {
133 aOut->Print( nl+1, "(fp_filters" );
134
135 for( unsigned i = 0; i < m_footprintFilters.GetCount(); ++i )
136 aOut->Print( 0, " %s", aOut->Quotew( m_footprintFilters[i] ).c_str() );
137
138 aOut->Print( 0, ")\n" );
139 }
140
141 if( !( aCtl & CTL_OMIT_NETS ) && m_nets.size() )
142 {
143 int llen = aOut->Print( nl+1, "(nets " );
144
145 for( unsigned i = 0; i < m_nets.size(); ++i )
146 {
147 if( llen > 80 )
148 {
149 aOut->Print( 0, "\n" );
150 llen = aOut->Print( nl+1, " " );
151 }
152
153 llen += m_nets[i].Format( aOut, 0, aCtl );
154 }
155
156 aOut->Print( 0, ")\n" );
157 }
158
159 aOut->Print( nl, ")\n" ); // </ref>
160}
161
162
163void NETLIST::Format( const char* aDocName, OUTPUTFORMATTER* aOut, int aNestLevel, int aCtl )
164{
165 int nl = aNestLevel;
166
167 aOut->Print( nl, "(%s\n", aDocName );
168
169 for( unsigned i = 0; i < m_components.size(); i++ )
170 {
171 m_components[i].Format( aOut, nl+1, aCtl );
172 }
173
174 aOut->Print( nl, ")\n" );
175}
176
177
179{
180 m_components.push_back( aComponent );
181}
182
183
185{
186 m_groups.push_back( aComponent );
187}
188
190{
192 {
193 if( group.uuid == aUuid )
194 return &group;
195 }
196
197 return nullptr;
198}
199
200
201COMPONENT* NETLIST::GetComponentByReference( const wxString& aReference )
202{
203 COMPONENT* component = nullptr;
204
205 for( unsigned i = 0; i < m_components.size(); i++ )
206 {
207 if( m_components[i].GetReference() == aReference )
208 {
209 component = &m_components[i];
210 break;
211 }
212 }
213
214 return component;
215}
216
217
219{
220 if( aUuidPath.empty() )
221 return nullptr;
222
223 KIID comp_uuid = aUuidPath.back();
224 KIID_PATH base = aUuidPath;
225
226 if( !base.empty() )
227 base.pop_back();
228
229 for( COMPONENT& component : m_components )
230 {
231 const std::vector<KIID>& kiids = component.GetKIIDs();
232
233 if( base != component.GetPath() )
234 continue;
235
236 if( std::find( kiids.begin(), kiids.end(), comp_uuid ) != kiids.end() )
237 return &component;
238 }
239
240 return nullptr;
241}
242
243
245{
246 if( aUuid == 0 )
247 return nullptr;
248
249 for( COMPONENT& component : m_components )
250 {
251 for( const KIID& compUuid : component.GetKIIDs() )
252 {
253 if( aUuid == compUuid )
254 return &component;
255 }
256 }
257
258 return nullptr;
259}
260
261
265static bool ByFPID( const COMPONENT& ref, const COMPONENT& cmp )
266{
267 return ref.GetFPID() > cmp.GetFPID();
268}
269
270
272{
273 m_components.sort( ByFPID );
274}
275
276
280bool operator < ( const COMPONENT& item1, const COMPONENT& item2 )
281{
282 return StrNumCmp( item1.GetReference(), item2.GetReference(), true ) < 0;
283}
284
285
287{
288 m_components.sort();
289}
290
291
293{
294 for( unsigned i = 0; i < m_components.size(); i++ )
295 {
296 if( !m_components[i].GetFPID().empty() )
297 return true;
298 }
299
300 return false;
301}
302
303
305{
307 {
308 for( KIID& member : group.members )
309 {
310 COMPONENT* component = GetComponentByUuid( member );
311
312 if( component )
313 component->SetGroup( &group );
314 }
315 }
316}
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:44
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:291
std::string Quotew(const wxString &aWrapee) const
Definition richio.cpp:507
int PRINTF_FUNC_N Print(int nestLevel, const char *fmt,...)
Format and write text to the output stream.
Definition richio.cpp:422
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 ...
std::string path