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
26#include <algorithm>
27#include <wx/tokenzr.h>
28#include <wx/log.h>
29
30
31int COMPONENT_NET::Format( OUTPUTFORMATTER* aOut, int aNestLevel, int aCtl )
32{
33 return aOut->Print( aNestLevel, "(pin_net %s %s)",
34 aOut->Quotew( m_pinName ).c_str(),
35 aOut->Quotew( m_netName ).c_str() );
36}
37
38
40
41
42const COMPONENT_NET& COMPONENT::GetNet( const wxString& aPinName ) const
43{
44 const COMPONENT_NET* fallback = nullptr;
45
46 for( const COMPONENT_NET& net : m_nets )
47 {
48 if( net.GetPinName() != aPinName )
49 {
50 if( !net.GetPinName().StartsWith( wxS( "[" ) ) )
51 continue;
52
53 const std::vector<wxString> expanded = ExpandStackedPinNotation( net.GetPinName() );
54
55 if( std::find( expanded.begin(), expanded.end(), aPinName ) == expanded.end() )
56 continue;
57 }
58
59 if( !fallback )
60 fallback = &net;
61
62 // Shared-unit pins can export both an explicit net and automatic unconnected nets.
63 if( !net.GetNetName().IsEmpty() && !IsAutoGeneratedNetName( net.GetNetName() ) )
64 return net;
65 }
66
67 return fallback ? *fallback : m_emptyNet;
68}
69
70
71void COMPONENT::Format( OUTPUTFORMATTER* aOut, int aNestLevel, int aCtl )
72{
73 int nl = aNestLevel;
74
75 aOut->Print( nl, "(ref %s ", aOut->Quotew( m_reference ).c_str() );
76 aOut->Print( 0, "(fpid %s)\n", aOut->Quotew( m_fpid.Format() ).c_str() );
77
78 if( !( aCtl & CTL_OMIT_EXTRA ) )
79 {
80 aOut->Print( nl+1, "(value %s)\n", aOut->Quotew( m_value ).c_str() );
81 aOut->Print( nl+1, "(name %s)\n", aOut->Quotew( m_name ).c_str() );
82 aOut->Print( nl+1, "(library %s)\n", aOut->Quotew( m_library ).c_str() );
83
84 wxString path;
85
86 for( const KIID& pathStep : m_path )
87 path += '/' + pathStep.AsString();
88
89 if( !( aCtl & CTL_OMIT_FP_UUID ) && !m_kiids.empty() )
90 path += '/' + m_kiids.front().AsString();
91
92 aOut->Print( nl+1, "(timestamp %s)\n", aOut->Quotew( path ).c_str() );
93
94 // Add all fields as a (field) under a (fields) node
95 aOut->Print( nl + 1, "(fields" );
96
97 for( std::pair<wxString, wxString> field : m_fields )
98 aOut->Print( nl + 2, "\n(field (name %s) %s)", aOut->Quotew( field.first ).c_str(),
99 aOut->Quotew( field.second ).c_str() );
100
101 aOut->Print( 0, ")\n" );
102
103 // Add DNP and Exclude from BOM properties if we have them
104 if( m_properties.count( "dnp" ) )
105 aOut->Print( nl + 1, "(property (name \"dnp\"))\n" );
106
107 if( m_properties.count( "exclude_from_bom" ) )
108 aOut->Print( nl + 1, "(property (name \"exclude_from_bom\"))\n" );
109 }
110
111 if( !( aCtl & CTL_OMIT_FILTERS ) && m_footprintFilters.GetCount() )
112 {
113 aOut->Print( nl+1, "(fp_filters" );
114
115 for( unsigned i = 0; i < m_footprintFilters.GetCount(); ++i )
116 aOut->Print( 0, " %s", aOut->Quotew( m_footprintFilters[i] ).c_str() );
117
118 aOut->Print( 0, ")\n" );
119 }
120
121 if( !( aCtl & CTL_OMIT_NETS ) && m_nets.size() )
122 {
123 int llen = aOut->Print( nl+1, "(nets " );
124
125 for( unsigned i = 0; i < m_nets.size(); ++i )
126 {
127 if( llen > 80 )
128 {
129 aOut->Print( 0, "\n" );
130 llen = aOut->Print( nl+1, " " );
131 }
132
133 llen += m_nets[i].Format( aOut, 0, aCtl );
134 }
135
136 aOut->Print( 0, ")\n" );
137 }
138
139 aOut->Print( nl, ")\n" ); // </ref>
140}
141
142
143void NETLIST::Format( const char* aDocName, OUTPUTFORMATTER* aOut, int aNestLevel, int aCtl )
144{
145 int nl = aNestLevel;
146
147 aOut->Print( nl, "(%s\n", aDocName );
148
149 for( unsigned i = 0; i < m_components.size(); i++ )
150 {
151 m_components[i].Format( aOut, nl+1, aCtl );
152 }
153
154 aOut->Print( nl, ")\n" );
155}
156
157
159{
160 m_components.push_back( aComponent );
161}
162
163
165{
166 m_groups.push_back( aComponent );
167}
168
170{
172 {
173 if( group.uuid == aUuid )
174 return &group;
175 }
176
177 return nullptr;
178}
179
180
181COMPONENT* NETLIST::GetComponentByReference( const wxString& aReference )
182{
183 COMPONENT* component = nullptr;
184
185 for( unsigned i = 0; i < m_components.size(); i++ )
186 {
187 if( m_components[i].GetReference() == aReference )
188 {
189 component = &m_components[i];
190 break;
191 }
192 }
193
194 return component;
195}
196
197
199{
200 if( aUuidPath.empty() )
201 return nullptr;
202
203 KIID comp_uuid = aUuidPath.back();
204 KIID_PATH base = aUuidPath;
205
206 if( !base.empty() )
207 base.pop_back();
208
209 for( COMPONENT& component : m_components )
210 {
211 const std::vector<KIID>& kiids = component.GetKIIDs();
212
213 if( base != component.GetPath() )
214 continue;
215
216 if( std::find( kiids.begin(), kiids.end(), comp_uuid ) != kiids.end() )
217 return &component;
218 }
219
220 return nullptr;
221}
222
223
225{
226 if( aUuid == 0 )
227 return nullptr;
228
229 for( COMPONENT& component : m_components )
230 {
231 for( const KIID& compUuid : component.GetKIIDs() )
232 {
233 if( aUuid == compUuid )
234 return &component;
235 }
236 }
237
238 return nullptr;
239}
240
241
245static bool ByFPID( const COMPONENT& ref, const COMPONENT& cmp )
246{
247 return ref.GetFPID() > cmp.GetFPID();
248}
249
250
252{
253 m_components.sort( ByFPID );
254}
255
256
260bool operator < ( const COMPONENT& item1, const COMPONENT& item2 )
261{
262 return StrNumCmp( item1.GetReference(), item2.GetReference(), true ) < 0;
263}
264
265
267{
268 m_components.sort();
269}
270
271
273{
274 for( unsigned i = 0; i < m_components.size(); i++ )
275 {
276 if( !m_components[i].GetFPID().empty() )
277 return true;
278 }
279
280 return false;
281}
282
283
285{
287 {
288 for( KIID& member : group.members )
289 {
290 COMPONENT* component = GetComponentByUuid( member );
291
292 if( component )
293 component->SetGroup( &group );
294 }
295 }
296}
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:46
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:294
std::string Quotew(const wxString &aWrapee) const
Definition richio.cpp:505
int PRINTF_FUNC_N Print(int nestLevel, const char *fmt,...)
Format and write text to the output stream.
Definition richio.cpp:432
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 ...
bool IsAutoGeneratedNetName(const wxString &aNetName)
Recognize the reserved prefixes used for generated pin-net names.
std::string path