KiCad PCB EDA Suite
pcb_netlist.cpp
Go to the documentation of this file.
1 
4 /*
5  * This program source code file is part of KiCad, a free EDA CAD application.
6  *
7  * Copyright (C) 1992-2011 Jean-Pierre Charras.
8  * Copyright (C) 2013 Wayne Stambaugh <stambaughw@verizon.net>.
9  * Copyright (C) 1992-2017 KiCad Developers, see AUTHORS.txt for contributors.
10  *
11  * This program is free software; you can redistribute it and/or
12  * modify it under the terms of the GNU General Public License
13  * as published by the Free Software Foundation; either version 2
14  * of the License, or (at your option) any later version.
15  *
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19  * GNU General Public License for more details.
20  *
21  * You should have received a copy of the GNU General Public License
22  * along with this program; if not, you may find one here:
23  * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
24  * or you may search the http://www.gnu.org website for the version 2 license,
25  * or you may write to the Free Software Foundation, Inc.,
26  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
27  */
28 
29 
30 #include <macros.h>
31 #include <refdes_utils.h>
32 #include <reporter.h>
33 
34 #include "pcb_netlist.h"
35 #include <class_module.h>
36 #include <eda_pattern_match.h>
37 
38 
39 int COMPONENT_NET::Format( OUTPUTFORMATTER* aOut, int aNestLevel, int aCtl )
40 {
41  return aOut->Print( aNestLevel, "(pin_net %s %s)",
42  aOut->Quotew( m_pinName ).c_str(),
43  aOut->Quotew( m_netName ).c_str() );
44 }
45 
46 
47 void COMPONENT::SetModule( MODULE* aModule )
48 {
49  m_footprint.reset( aModule );
50 
51  if( aModule == NULL )
52  return;
53 
54  aModule->SetReference( m_reference );
55  aModule->SetValue( m_value );
56  aModule->SetFPID( m_fpid );
57  aModule->SetPath( m_path );
58 }
59 
60 
62 
63 
64 const COMPONENT_NET& COMPONENT::GetNet( const wxString& aPinName ) const
65 {
66  for( unsigned i = 0; i < m_nets.size(); i++ )
67  {
68  if( m_nets[i].GetPinName() == aPinName )
69  return m_nets[i];
70  }
71 
72  return m_emptyNet;
73 }
74 
75 
76 void COMPONENT::Format( OUTPUTFORMATTER* aOut, int aNestLevel, int aCtl )
77 {
78  int nl = aNestLevel;
79 
80  aOut->Print( nl, "(ref %s ", aOut->Quotew( m_reference ).c_str() );
81  aOut->Print( 0, "(fpid %s)\n", aOut->Quotew( m_fpid.Format() ).c_str() );
82 
83  if( ! ( aCtl & CTL_OMIT_EXTRA ) )
84  {
85  aOut->Print( nl+1, "(value %s)\n", aOut->Quotew( m_value ).c_str() );
86  aOut->Print( nl+1, "(name %s)\n", aOut->Quotew( m_name ).c_str() );
87  aOut->Print( nl+1, "(library %s)\n", aOut->Quotew( m_library ).c_str() );
88 
89  wxString path;
90 
91  for( const KIID& pathStep : m_path )
92  path += '/' + pathStep.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  for( COMPONENT& component : m_components )
170  {
171  if( component.GetPath() == aUuidPath )
172  return &component;
173  }
174 
175  return nullptr;
176 }
177 
178 
183 static bool ByFPID( const COMPONENT& ref, const COMPONENT& cmp )
184 {
185  return ref.GetFPID() > cmp.GetFPID();
186 }
187 
188 
190 {
191  m_components.sort( ByFPID );
192 }
193 
194 
199 bool operator < ( const COMPONENT& item1, const COMPONENT& item2 )
200 {
201  return UTIL::RefDesStringCompare( item1.GetReference(), item2.GetReference() ) < 0;
202 }
203 
204 
206 {
207  m_components.sort();
208 }
209 
210 
212 {
213  for( unsigned i = 0; i < m_components.size(); i++ )
214  {
215  if( !m_components[i].GetFPID().empty() )
216  return true;
217  }
218 
219  return false;
220 }
221 
222 
224 {
225  for( unsigned i = 0; i < m_components.size(); i++ )
226  {
227  if( m_components[i].GetFPID().empty() )
228  return false;
229  }
230 
231  return true;
232 }
233 
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:76
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
Components found in the netlist.
Definition: pcb_netlist.h:214
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:112
Abstract pattern-matching tool and implementations.
This file contains miscellaneous commonly used macros and functions.
void AddComponent(COMPONENT *aComponent)
Function AddComponent adds aComponent to the NETLIST.
int Format(OUTPUTFORMATTER *aOut, int aNestLevel, int aCtl)
Definition: pcb_netlist.cpp:39
void SortByReference()
const LIB_ID & GetFPID() const
Definition: pcb_netlist.h:164
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:349
bool AllFootprintsLinked() const
Function AllFootprintsLinked.
const wxString & GetReference() const
Definition: pcb_netlist.h:149
void SetReference(const wxString &aReference)
Function SetReference.
Definition: class_module.h:443
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:137
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 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:467
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:47
void SetPath(const KIID_PATH &aPath)
Definition: class_module.h:225
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:107
void SetFPID(const LIB_ID &aFPID)
Definition: class_module.h:216
wxString m_pinName
Definition: pcb_netlist.h:46
#define CTL_OMIT_EXTRA
Definition: pcb_netlist.h:347