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  path.push_back( m_kiids.front() );
46 
47  if( aFootprint == NULL )
48  return;
49 
50  aFootprint->SetReference( m_reference );
51  aFootprint->SetValue( m_value );
52  aFootprint->SetFPID( m_fpid );
53  aFootprint->SetPath( path );
54  aFootprint->SetProperties( m_properties );
55 }
56 
57 
59 
60 
61 const COMPONENT_NET& COMPONENT::GetNet( const wxString& aPinName ) const
62 {
63  for( const COMPONENT_NET& net : m_nets )
64  {
65  if( net.GetPinName() == aPinName )
66  return net;
67  }
68 
69  return m_emptyNet;
70 }
71 
72 
73 void COMPONENT::Format( OUTPUTFORMATTER* aOut, int aNestLevel, int aCtl )
74 {
75  int nl = aNestLevel;
76 
77  aOut->Print( nl, "(ref %s ", aOut->Quotew( m_reference ).c_str() );
78  aOut->Print( 0, "(fpid %s)\n", aOut->Quotew( m_fpid.Format() ).c_str() );
79 
80  if( !( aCtl & CTL_OMIT_EXTRA ) )
81  {
82  aOut->Print( nl+1, "(value %s)\n", aOut->Quotew( m_value ).c_str() );
83  aOut->Print( nl+1, "(name %s)\n", aOut->Quotew( m_name ).c_str() );
84  aOut->Print( nl+1, "(library %s)\n", aOut->Quotew( m_library ).c_str() );
85 
86  wxString path;
87 
88  for( const KIID& pathStep : m_path )
89  path += '/' + pathStep.AsString();
90 
91  if( !( aCtl & CTL_OMIT_FP_UUID ) && !m_kiids.empty() )
92  path += '/' + m_kiids.front().AsString();
93 
94  aOut->Print( nl+1, "(timestamp %s)\n", aOut->Quotew( path ).c_str() );
95  }
96 
97  if( !( aCtl & CTL_OMIT_FILTERS ) && m_footprintFilters.GetCount() )
98  {
99  aOut->Print( nl+1, "(fp_filters" );
100 
101  for( unsigned i = 0; i < m_footprintFilters.GetCount(); ++i )
102  aOut->Print( 0, " %s", aOut->Quotew( m_footprintFilters[i] ).c_str() );
103 
104  aOut->Print( 0, ")\n" );
105  }
106 
107  if( !( aCtl & CTL_OMIT_NETS ) && m_nets.size() )
108  {
109  int llen = aOut->Print( nl+1, "(nets " );
110 
111  for( unsigned i = 0; i < m_nets.size(); ++i )
112  {
113  if( llen > 80 )
114  {
115  aOut->Print( 0, "\n" );
116  llen = aOut->Print( nl+1, " " );
117  }
118 
119  llen += m_nets[i].Format( aOut, 0, aCtl );
120  }
121 
122  aOut->Print( 0, ")\n" );
123  }
124 
125  aOut->Print( nl, ")\n" ); // </ref>
126 }
127 
128 
129 void NETLIST::Format( const char* aDocName, OUTPUTFORMATTER* aOut, int aNestLevel, int aCtl )
130 {
131  int nl = aNestLevel;
132 
133  aOut->Print( nl, "(%s\n", aDocName );
134 
135  for( unsigned i = 0; i < m_components.size(); i++ )
136  {
137  m_components[i].Format( aOut, nl+1, aCtl );
138  }
139 
140  aOut->Print( nl, ")\n" );
141 }
142 
143 
144 void NETLIST::AddComponent( COMPONENT* aComponent )
145 {
146  m_components.push_back( aComponent );
147 }
148 
149 
150 COMPONENT* NETLIST::GetComponentByReference( const wxString& aReference )
151 {
152  COMPONENT* component = NULL;
153 
154  for( unsigned i = 0; i < m_components.size(); i++ )
155  {
156  if( m_components[i].GetReference() == aReference )
157  {
158  component = &m_components[i];
159  break;
160  }
161  }
162 
163  return component;
164 }
165 
166 
168 {
169  if( aUuidPath.empty() )
170  return nullptr;
171 
172  KIID comp_uuid = aUuidPath.back();
173  KIID_PATH base = aUuidPath;
174 
175  if( !base.empty() )
176  base.pop_back();
177 
178  for( COMPONENT& component : m_components )
179  {
180  const std::vector<KIID>& kiids = component.GetKIIDs();
181 
182  if( base != component.GetPath() )
183  continue;
184 
185  if( std::find( kiids.begin(), kiids.end(), comp_uuid ) != kiids.end() )
186  return &component;
187  }
188 
189  return nullptr;
190 }
191 
192 
196 static bool ByFPID( const COMPONENT& ref, const COMPONENT& cmp )
197 {
198  return ref.GetFPID() > cmp.GetFPID();
199 }
200 
201 
203 {
204  m_components.sort( ByFPID );
205 }
206 
207 
211 bool operator < ( const COMPONENT& item1, const COMPONENT& item2 )
212 {
213  return UTIL::RefDesStringCompare( item1.GetReference(), item2.GetReference() ) < 0;
214 }
215 
216 
218 {
219  m_components.sort();
220 }
221 
222 
224 {
225  for( unsigned i = 0; i < m_components.size(); i++ )
226  {
227  if( !m_components[i].GetFPID().empty() )
228  return true;
229  }
230 
231  return false;
232 }
233 
234 
void SetReference(const wxString &aReference)
Definition: footprint.h:435
KIID_PATH m_path
A fully specified path to the component (but not the component: [ sheetUUID, sheetUUID,...
Definition: pcb_netlist.h:169
#define CTL_OMIT_FP_UUID
Don't prefix the footprint UUID to the sheet path.
Definition: pcb_netlist.h:282
std::unique_ptr< FOOTPRINT > m_footprint
The FOOTPRINT loaded for #m_FPID.
Definition: pcb_netlist.h:189
void SortByFPID()
void Format(OUTPUTFORMATTER *aOut, int aNestLevel, int aCtl)
Definition: pcb_netlist.cpp:73
wxString m_name
The name of the component in m_library used when it was placed on the schematic..
Definition: pcb_netlist.h:175
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:178
bool AnyFootprintsLinked() const
void SetPath(const KIID_PATH &aPath)
Definition: footprint.h:200
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:292
#define CTL_OMIT_NETS
Definition: pcb_netlist.h:280
Collection of utility functions for component reference designators (refdes)
An interface used to output 8 bit text in a convenient way.
Definition: richio.h:306
wxArrayString m_footprintFilters
Footprint filters found in netlist.
Definition: pcb_netlist.h:163
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:172
static COMPONENT_NET m_emptyNet
Definition: pcb_netlist.h:194
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:132
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:162
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 NULL
#define CTL_OMIT_FILTERS
Definition: pcb_netlist.h:281
const wxString & GetReference() const
Definition: pcb_netlist.h:122
wxString m_netName
Definition: pcb_netlist.h:46
void SetValue(const wxString &aValue)
Definition: footprint.h:456
const COMPONENT_NET & GetNet(unsigned aIndex) const
Definition: pcb_netlist.h:110
std::map< wxString, wxString > m_properties
Component-specific properties found in the netlist.
Definition: pcb_netlist.h:192
UTF8 Format() const
Definition: lib_id.cpp:233
Store all of the related footprint information found in a netlist.
Definition: pcb_netlist.h:83
void SetFPID(const LIB_ID &aFPID)
Definition: footprint.h:191
LIB_ID m_fpid
The LIB_ID of the footprint assigned to the component.
Definition: pcb_netlist.h:181
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:166
COMPONENT * GetComponentByReference(const wxString &aReference)
Return a COMPONENT by aReference.
wxString m_reference
The component reference designator found in netlist.
Definition: pcb_netlist.h:165
void SetProperties(const std::map< wxString, wxString > &aProps)
Definition: footprint.h:470
int PRINTF_FUNC Print(int nestLevel, const char *fmt,...)
Format and write text to the output stream.
Definition: richio.cpp:408
wxString m_pinName
Definition: pcb_netlist.h:45
std::string Quotew(const wxString &aWrapee) const
Definition: richio.cpp:476
#define CTL_OMIT_EXTRA
Definition: pcb_netlist.h:279