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@verizon.net>.
6  * Copyright (C) 1992-2020 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 <class_module.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 
40 void COMPONENT::SetModule( MODULE* aModule )
41 {
42  m_footprint.reset( aModule );
43 
44  if( aModule == NULL )
45  return;
46 
47  aModule->SetReference( m_reference );
48  aModule->SetValue( m_value );
49  aModule->SetFPID( m_fpid );
50  aModule->SetPath( m_path );
51  aModule->SetProperties( m_properties );
52 }
53 
54 
56 
57 
58 const COMPONENT_NET& COMPONENT::GetNet( const wxString& aPinName ) const
59 {
60  for( const COMPONENT_NET& net : m_nets )
61  {
62  if( net.GetPinName() == aPinName )
63  return net;
64  }
65 
66  return m_emptyNet;
67 }
68 
69 
70 void COMPONENT::Format( OUTPUTFORMATTER* aOut, int aNestLevel, int aCtl )
71 {
72  int nl = aNestLevel;
73 
74  aOut->Print( nl, "(ref %s ", aOut->Quotew( m_reference ).c_str() );
75  aOut->Print( 0, "(fpid %s)\n", aOut->Quotew( m_fpid.Format() ).c_str() );
76 
77  if( ! ( aCtl & CTL_OMIT_EXTRA ) )
78  {
79  aOut->Print( nl+1, "(value %s)\n", aOut->Quotew( m_value ).c_str() );
80  aOut->Print( nl+1, "(name %s)\n", aOut->Quotew( m_name ).c_str() );
81  aOut->Print( nl+1, "(library %s)\n", aOut->Quotew( m_library ).c_str() );
82 
83  wxString path;
84 
85  for( const KIID& pathStep : m_path )
86  path += '/' + pathStep.AsString();
87 
88  aOut->Print( nl+1, "(timestamp %s)\n", aOut->Quotew( path ).c_str() );
89  }
90 
91  if( !( aCtl & CTL_OMIT_FILTERS ) && m_footprintFilters.GetCount() )
92  {
93  aOut->Print( nl+1, "(fp_filters" );
94 
95  for( unsigned i = 0; i < m_footprintFilters.GetCount(); ++i )
96  aOut->Print( 0, " %s", aOut->Quotew( m_footprintFilters[i] ).c_str() );
97 
98  aOut->Print( 0, ")\n" );
99  }
100 
101  if( !( aCtl & CTL_OMIT_NETS ) && m_nets.size() )
102  {
103  int llen = aOut->Print( nl+1, "(nets " );
104 
105  for( unsigned i = 0; i < m_nets.size(); ++i )
106  {
107  if( llen > 80 )
108  {
109  aOut->Print( 0, "\n" );
110  llen = aOut->Print( nl+1, " " );
111  }
112 
113  llen += m_nets[i].Format( aOut, 0, aCtl );
114  }
115 
116  aOut->Print( 0, ")\n" );
117  }
118 
119  aOut->Print( nl, ")\n" ); // </ref>
120 }
121 
122 
123 void NETLIST::Format( const char* aDocName, OUTPUTFORMATTER* aOut, int aNestLevel, int aCtl )
124 {
125  int nl = aNestLevel;
126 
127  aOut->Print( nl, "(%s\n", aDocName );
128 
129  for( unsigned i = 0; i < m_components.size(); i++ )
130  {
131  m_components[i].Format( aOut, nl+1, aCtl );
132  }
133 
134  aOut->Print( nl, ")\n" );
135 }
136 
137 
138 void NETLIST::AddComponent( COMPONENT* aComponent )
139 {
140  m_components.push_back( aComponent );
141 }
142 
143 
144 COMPONENT* NETLIST::GetComponentByReference( const wxString& aReference )
145 {
146  COMPONENT* component = NULL;
147 
148  for( unsigned i = 0; i < m_components.size(); i++ )
149  {
150  if( m_components[i].GetReference() == aReference )
151  {
152  component = &m_components[i];
153  break;
154  }
155  }
156 
157  return component;
158 }
159 
160 
162 {
163  for( COMPONENT& component : m_components )
164  {
165  if( component.GetPath() == aUuidPath )
166  return &component;
167  }
168 
169  return nullptr;
170 }
171 
172 
177 static bool ByFPID( const COMPONENT& ref, const COMPONENT& cmp )
178 {
179  return ref.GetFPID() > cmp.GetFPID();
180 }
181 
182 
184 {
185  m_components.sort( ByFPID );
186 }
187 
188 
193 bool operator < ( const COMPONENT& item1, const COMPONENT& item2 )
194 {
195  return UTIL::RefDesStringCompare( item1.GetReference(), item2.GetReference() ) < 0;
196 }
197 
198 
200 {
201  m_components.sort();
202 }
203 
204 
206 {
207  for( unsigned i = 0; i < m_components.size(); i++ )
208  {
209  if( !m_components[i].GetFPID().empty() )
210  return true;
211  }
212 
213  return false;
214 }
215 
216 
KIID_PATH m_path
A fully specified path to the component: [ sheetUUID, sheetUUID, .., componentUUID ].
Definition: pcb_netlist.h:89
void SortByFPID()
void Format(OUTPUTFORMATTER *aOut, int aNestLevel, int aCtl)
Definition: pcb_netlist.cpp:70
wxString m_name
The name of the component in m_library used when it was placed on the schematic..
Definition: pcb_netlist.h:92
COMPONENT * GetComponentByPath(const KIID_PATH &aPath)
Function GetComponentByPath returns a COMPONENT by aPath.
wxString m_library
The name of the component library where m_name was found.
Definition: pcb_netlist.h:95
bool AnyFootprintsLinked() const
Function AnyFootprintsLinked.
static bool ByFPID(const COMPONENT &ref, const COMPONENT &cmp)
Function ByFPID is a helper function used to sort the component list used by loadNewModules.
COMPONENTS m_components
Definition: pcb_netlist.h:196
Collection of utility functions for component reference designators (refdes)
OUTPUTFORMATTER is an important interface (abstract class) used to output 8 bit text in a convenient ...
Definition: richio.h:327
wxArrayString m_footprintFilters
Footprint filters found in netlist.
Definition: pcb_netlist.h:83
bool operator<(const COMPONENT &item1, const COMPONENT &item2)
Operator < compares two COMPONENT objects by reference designator.
static COMPONENT_NET m_emptyNet
Definition: pcb_netlist.h:111
void AddComponent(COMPONENT *aComponent)
Function AddComponent adds 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:157
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:82
COMPONENT_NET is used to store the component pin name to net name (and pin function) associations sto...
Definition: pcb_netlist.h:44
Definition: common.h:68
#define NULL
#define CTL_OMIT_FILTERS
Definition: pcb_netlist.h:283
const wxString & GetReference() const
Definition: pcb_netlist.h:147
void SetReference(const wxString &aReference)
Function SetReference.
Definition: class_module.h:461
std::string Quotew(const wxString &aWrapee)
Definition: richio.cpp:472
wxString m_netName
Definition: pcb_netlist.h:47
#define CTL_OMIT_NETS
Omit pads net names (useless in library)
const COMPONENT_NET & GetNet(unsigned aIndex) const
Definition: pcb_netlist.h:135
std::map< wxString, wxString > m_properties
Component-specific properties found in the netlist.
Definition: pcb_netlist.h:109
UTF8 Format() const
Definition: lib_id.cpp:237
COMPONENT is used to store components and all of their related information found in a netlist.
Definition: pcb_netlist.h:80
LIB_ID m_fpid
The LIB_ID of the footprint assigned to the component.
Definition: pcb_netlist.h:98
void SetProperties(const std::map< wxString, wxString > &aProps)
Definition: class_module.h:499
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:86
void SetValue(const wxString &aValue)
Function SetValue.
Definition: class_module.h:485
COMPONENT * GetComponentByReference(const wxString &aReference)
Function GetComponentByReference returns a COMPONENT by aReference.
wxString m_reference
The component reference designator found in netlist.
Definition: pcb_netlist.h:85
void SetModule(MODULE *aModule)
Definition: pcb_netlist.cpp:40
void SetPath(const KIID_PATH &aPath)
Definition: class_module.h:238
int PRINTF_FUNC Print(int nestLevel, const char *fmt,...)
Function Print formats and writes text to the output stream.
Definition: richio.cpp:404
std::unique_ptr< MODULE > m_footprint
The MODULE loaded for m_fpid.
Definition: pcb_netlist.h:106
void SetFPID(const LIB_ID &aFPID)
Definition: class_module.h:229
wxString m_pinName
Definition: pcb_netlist.h:46
#define CTL_OMIT_EXTRA
Definition: pcb_netlist.h:281