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 <kicad_string.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_timeStamp );
58 }
59 
60 
62 
63 
64 const COMPONENT_NET& COMPONENT::GetNet( const wxString& aPinName )
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  aOut->Print( nl+1, "(timestamp %s)\n", aOut->Quotew( m_timeStamp ).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 
161 COMPONENT* NETLIST::GetComponentByTimeStamp( const wxString& aTimeStamp )
162 {
163  COMPONENT* component = NULL;
164 
165  for( unsigned i = 0; i < m_components.size(); i++ )
166  {
167  if( m_components[i].GetTimeStamp() == aTimeStamp )
168  {
169  component = &m_components[i];
170  break;
171  }
172  }
173 
174  return component;
175 }
176 
177 
182 static bool ByFPID( const COMPONENT& ref, const COMPONENT& cmp )
183 {
184  return ref.GetFPID() > cmp.GetFPID();
185 }
186 
187 
189 {
190  m_components.sort( ByFPID );
191 }
192 
193 
198 bool operator < ( const COMPONENT& item1, const COMPONENT& item2 )
199 {
200  return RefDesStringCompare(item1.GetReference(), item2.GetReference() ) < 0;
201 }
202 
203 
205 {
206  m_components.sort();
207 }
208 
209 
211 {
212  for( unsigned i = 0; i < m_components.size(); i++ )
213  {
214  if( !m_components[i].GetFPID().empty() )
215  return true;
216  }
217 
218  return false;
219 }
220 
221 
223 {
224  for( unsigned i = 0; i < m_components.size(); i++ )
225  {
226  if( m_components[i].GetFPID().empty() )
227  return false;
228  }
229 
230  return true;
231 }
232 
const wxString & GetPinName() const
Definition: pcb_netlist.h:61
void SortByFPID()
void Format(OUTPUTFORMATTER *aOut, int aNestLevel, int aCtl)
Definition: pcb_netlist.cpp:76
const LIB_ID & GetFPID() const
Definition: pcb_netlist.h:166
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.
Class OUTPUTFORMATTER is an important interface (abstract class) used to output 8 bit text in a conve...
Definition: richio.h:327
static COMPONENT_NET m_emptyNet
Definition: pcb_netlist.h:114
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 wxString & GetReference() const
Definition: pcb_netlist.h:151
Class COMPONENT_NET is used to store the component pin name to net name associations stored in a netl...
Definition: pcb_netlist.h:48
#define CTL_OMIT_FILTERS
Definition: pcb_netlist.h:351
void SetReference(const wxString &aReference)
Function SetReference.
Definition: class_module.h:473
std::string Quotew(const wxString &aWrapee)
Definition: richio.cpp:482
wxString m_netName
Definition: pcb_netlist.h:51
#define CTL_OMIT_NETS
Omit pads net names (useless in library)
Class COMPONENT is used to store components and all of their related information found in a netlist...
Definition: pcb_netlist.h:83
bool operator<(const COMPONENT_NET &aNet) const
Definition: pcb_netlist.h:67
void Format(const char *aDocName, OUTPUTFORMATTER *aOut, int aNestLevel, int aCtl=0)
size_t i
Definition: json11.cpp:597
void SetValue(const wxString &aValue)
Function SetValue.
Definition: class_module.h:506
bool AllFootprintsLinked() const
Function AllFootprintsLinked.
COMPONENT * GetComponentByReference(const wxString &aReference)
Function GetComponentByReference returns a COMPONENT by aReference.
const COMPONENT_NET & GetNet(unsigned aIndex) const
Definition: pcb_netlist.h:139
void SetModule(MODULE *aModule)
Definition: pcb_netlist.cpp:47
Module description (excepted pads)
int PRINTF_FUNC Print(int nestLevel, const char *fmt,...)
Function Print formats and writes text to the output stream.
Definition: richio.cpp:404
void SetFPID(const LIB_ID &aFPID)
Definition: class_module.h:194
void SetPath(const wxString &aPath)
Definition: class_module.h:203
wxString m_pinName
Definition: pcb_netlist.h:50
COMPONENT * GetComponentByTimeStamp(const wxString &aTimeStamp)
Function GetComponentByTimeStamp returns a COMPONENT by aTimeStamp.
#define CTL_OMIT_EXTRA
Definition: pcb_netlist.h:349
int RefDesStringCompare(wxString strFWord, wxString strSWord)
Function RefDesStringCompare acts just like the strcmp function but treats numbers within the string ...
Definition: string.cpp:542