KiCad PCB EDA Suite
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 <stambaughw@gmail.com>.
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 <refdes_utils.h>
28 #include "pcb_netlist.h"
29 #include <footprint.h>
30 
31 
32 int 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  aFootprint->SetProperties( m_properties );
56 }
57 
58 
60 
61 
62 const COMPONENT_NET& COMPONENT::GetNet( const wxString& aPinName ) const
63 {
64  for( const COMPONENT_NET& net : m_nets )
65  {
66  if( net.GetPinName() == aPinName )
67  return net;
68  }
69 
70  return m_emptyNet;
71 }
72 
73 
74 void COMPONENT::Format( OUTPUTFORMATTER* aOut, int aNestLevel, int aCtl )
75 {
76  int nl = aNestLevel;
77 
78  aOut->Print( nl, "(ref %s ", aOut->Quotew( m_reference ).c_str() );
79  aOut->Print( 0, "(fpid %s)\n", aOut->Quotew( m_fpid.Format() ).c_str() );
80 
81  if( !( aCtl & CTL_OMIT_EXTRA ) )
82  {
83  aOut->Print( nl+1, "(value %s)\n", aOut->Quotew( m_value ).c_str() );
84  aOut->Print( nl+1, "(name %s)\n", aOut->Quotew( m_name ).c_str() );
85  aOut->Print( nl+1, "(library %s)\n", aOut->Quotew( m_library ).c_str() );
86 
87  wxString path;
88 
89  for( const KIID& pathStep : m_path )
90  path += '/' + pathStep.AsString();
91 
92  if( !( aCtl & CTL_OMIT_FP_UUID ) && !m_kiids.empty() )
93  path += '/' + m_kiids.front().AsString();
94 
95  aOut->Print( nl+1, "(timestamp %s)\n", aOut->Quotew( path ).c_str() );
96  }
97 
98  if( !( aCtl & CTL_OMIT_FILTERS ) && m_footprintFilters.GetCount() )
99  {
100  aOut->Print( nl+1, "(fp_filters" );
101 
102  for( unsigned i = 0; i < m_footprintFilters.GetCount(); ++i )
103  aOut->Print( 0, " %s", aOut->Quotew( m_footprintFilters[i] ).c_str() );
104 
105  aOut->Print( 0, ")\n" );
106  }
107 
108  if( !( aCtl & CTL_OMIT_NETS ) && m_nets.size() )
109  {
110  int llen = aOut->Print( nl+1, "(nets " );
111 
112  for( unsigned i = 0; i < m_nets.size(); ++i )
113  {
114  if( llen > 80 )
115  {
116  aOut->Print( 0, "\n" );
117  llen = aOut->Print( nl+1, " " );
118  }
119 
120  llen += m_nets[i].Format( aOut, 0, aCtl );
121  }
122 
123  aOut->Print( 0, ")\n" );
124  }
125 
126  aOut->Print( nl, ")\n" ); // </ref>
127 }
128 
129 
130 void NETLIST::Format( const char* aDocName, OUTPUTFORMATTER* aOut, int aNestLevel, int aCtl )
131 {
132  int nl = aNestLevel;
133 
134  aOut->Print( nl, "(%s\n", aDocName );
135 
136  for( unsigned i = 0; i < m_components.size(); i++ )
137  {
138  m_components[i].Format( aOut, nl+1, aCtl );
139  }
140 
141  aOut->Print( nl, ")\n" );
142 }
143 
144 
145 void NETLIST::AddComponent( COMPONENT* aComponent )
146 {
147  m_components.push_back( aComponent );
148 }
149 
150 
151 COMPONENT* NETLIST::GetComponentByReference( const wxString& aReference )
152 {
153  COMPONENT* component = nullptr;
154 
155  for( unsigned i = 0; i < m_components.size(); i++ )
156  {
157  if( m_components[i].GetReference() == aReference )
158  {
159  component = &m_components[i];
160  break;
161  }
162  }
163 
164  return component;
165 }
166 
167 
169 {
170  if( aUuidPath.empty() )
171  return nullptr;
172 
173  KIID comp_uuid = aUuidPath.back();
174  KIID_PATH base = aUuidPath;
175 
176  if( !base.empty() )
177  base.pop_back();
178 
179  for( COMPONENT& component : m_components )
180  {
181  const std::vector<KIID>& kiids = component.GetKIIDs();
182 
183  if( base != component.GetPath() )
184  continue;
185 
186  if( std::find( kiids.begin(), kiids.end(), comp_uuid ) != kiids.end() )
187  return &component;
188  }
189 
190  return nullptr;
191 }
192 
193 
197 static bool ByFPID( const COMPONENT& ref, const COMPONENT& cmp )
198 {
199  return ref.GetFPID() > cmp.GetFPID();
200 }
201 
202 
204 {
205  m_components.sort( ByFPID );
206 }
207 
208 
212 bool operator < ( const COMPONENT& item1, const COMPONENT& item2 )
213 {
214  return UTIL::RefDesStringCompare( item1.GetReference(), item2.GetReference() ) < 0;
215 }
216 
217 
219 {
220  m_components.sort();
221 }
222 
223 
225 {
226  for( unsigned i = 0; i < m_components.size(); i++ )
227  {
228  if( !m_components[i].GetFPID().empty() )
229  return true;
230  }
231 
232  return false;
233 }
234 
235 
void SetReference(const wxString &aReference)
Definition: footprint.h:430
KIID_PATH m_path
A fully specified path to the component (but not the component: [ sheetUUID, sheetUUID,...
Definition: pcb_netlist.h:170
#define CTL_OMIT_FP_UUID
Don't prefix the footprint UUID to the sheet path.
Definition: pcb_netlist.h:283
std::unique_ptr< FOOTPRINT > m_footprint
The FOOTPRINT loaded for #m_FPID.
Definition: pcb_netlist.h:190
void SortByFPID()
void Format(OUTPUTFORMATTER *aOut, int aNestLevel, int aCtl)
Definition: pcb_netlist.cpp:74
wxString m_name
The name of the component in m_library used when it was placed on the schematic..
Definition: pcb_netlist.h:176
COMPONENT * GetComponentByPath(const KIID_PATH &aPath)
Return a COMPONENT by aPath.
wxString m_library
The name of the component library where m_name was found.
Definition: pcb_netlist.h:179
bool AnyFootprintsLinked() const
void SetPath(const KIID_PATH &aPath)
Definition: footprint.h:195
static bool ByFPID(const COMPONENT &ref, const COMPONENT &cmp)
A helper function used to sort the component list used by loadNewModules.
COMPONENTS m_components
Definition: pcb_netlist.h:293
#define CTL_OMIT_NETS
Definition: pcb_netlist.h:281
Collection of utility functions for component reference designators (refdes)
An interface used to output 8 bit text in a convenient way.
Definition: richio.h:309
wxArrayString m_footprintFilters
Footprint filters found in netlist.
Definition: pcb_netlist.h:164
bool operator<(const COMPONENT &item1, const COMPONENT &item2)
Compare two COMPONENT objects by reference designator.
std::vector< KIID > m_kiids
A vector of possible KIIDs corresponding to all units in a symbol.
Definition: pcb_netlist.h:173
static COMPONENT_NET m_emptyNet
Definition: pcb_netlist.h:195
void SetFootprint(FOOTPRINT *aFootprint)
Definition: pcb_netlist.cpp:40
void AddComponent(COMPONENT *aComponent)
Add aComponent to the NETLIST.
int Format(OUTPUTFORMATTER *aOut, int aNestLevel, int aCtl)
Definition: pcb_netlist.cpp:32
void SortByReference()
const LIB_ID & GetFPID() const
Definition: pcb_netlist.h:133
int RefDesStringCompare(const wxString &aFirst, const wxString &aSecond)
Acts just like the strcmp function but treats numbers within the string text correctly for sorting.
COMPONENT_NETS m_nets
list of nets shared by the component pins
Definition: pcb_netlist.h:163
Used to store the component pin name to net name (and pin function) associations stored in a netlist.
Definition: pcb_netlist.h:43
Definition: kiid.h:44
#define CTL_OMIT_FILTERS
Definition: pcb_netlist.h:282
const wxString & GetReference() const
Definition: pcb_netlist.h:123
wxString m_netName
Definition: pcb_netlist.h:73
void SetValue(const wxString &aValue)
Definition: footprint.h:451
const COMPONENT_NET & GetNet(unsigned aIndex) const
Definition: pcb_netlist.h:111
std::map< wxString, wxString > m_properties
Component-specific properties found in the netlist.
Definition: pcb_netlist.h:193
UTF8 Format() const
Definition: lib_id.cpp:116
Store all of the related footprint information found in a netlist.
Definition: pcb_netlist.h:84
void SetFPID(const LIB_ID &aFPID)
Definition: footprint.h:186
LIB_ID m_fpid
The LIB_ID of the footprint assigned to the component.
Definition: pcb_netlist.h:182
void Format(const char *aDocName, OUTPUTFORMATTER *aOut, int aNestLevel, int aCtl=0)
wxString m_value
The component value found in netlist.
Definition: pcb_netlist.h:167
COMPONENT * GetComponentByReference(const wxString &aReference)
Return a COMPONENT by aReference.
wxString m_reference
The component reference designator found in netlist.
Definition: pcb_netlist.h:166
void SetProperties(const std::map< wxString, wxString > &aProps)
Definition: footprint.h:465
int PRINTF_FUNC Print(int nestLevel, const char *fmt,...)
Format and write text to the output stream.
Definition: richio.cpp:426
wxString m_pinName
Definition: pcb_netlist.h:72
std::string Quotew(const wxString &aWrapee) const
Definition: richio.cpp:494
#define CTL_OMIT_EXTRA
Definition: pcb_netlist.h:280