KiCad PCB EDA Suite
netlist_exporter.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-2017 jp.charras at wanadoo.fr
5  * Copyright (C) 2013-2017 SoftPLC Corporation, Dick Hollenbeck <dick@softplc.com>
6  * Copyright (C) 1992-2017 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 #include <netlist_exporter.h>
27 
28 #include <confirm.h>
29 #include <fctsys.h>
30 #include <gestfich.h>
31 #include <pgm_base.h>
32 #include <refdes_utils.h>
33 
34 #include <class_library.h>
35 #include <connection_graph.h>
36 #include <sch_reference_list.h>
37 #include <sch_screen.h>
38 #include <schematic.h>
39 
40 
41 wxString NETLIST_EXPORTER::MakeCommandLine( const wxString& aFormatString,
42  const wxString& aNetlistFile, const wxString& aFinalFile, const wxString& aProjectPath )
43 {
44  // Expand format symbols in the command line:
45  // %B => base filename of selected output file, minus path and extension.
46  // %P => project directory name, without trailing '/' or '\'.
47  // %I => full filename of the input file (the intermediate net file).
48  // %O => complete filename and path (but without extension) of the user chosen output file.
49 
50  wxString ret = aFormatString;
51  wxFileName in = aNetlistFile;
52  wxFileName out = aFinalFile;
53  wxString str_out = out.GetFullPath();
54 
55  ret.Replace( "%P", aProjectPath, true );
56  ret.Replace( "%B", out.GetName(), true );
57  ret.Replace( "%I", in.GetFullPath(), true );
58 
59 #ifdef __WINDOWS__
60  // A ugly hack to run xsltproc that has a serious bug on Window since a long time:
61  // the filename given after -o option (output filename) cannot use '\' in filename
62  // so replace if by '/' if possible (I mean if the filename does not start by "\\"
63  // that is a filename on a Windows server)
64 
65  if( !str_out.StartsWith( "\\\\" ) )
66  str_out.Replace( "\\", "/" );
67 #endif
68 
69  ret.Replace( "%O", str_out, true );
70 
71  return ret;
72 }
73 
74 
76 {
77  wxString ref;
78 
79  if( aItem->Type() != SCH_COMPONENT_T )
80  return nullptr;
81 
82  // found next component
83  SCH_COMPONENT* comp = (SCH_COMPONENT*) aItem;
84 
85  // Power symbols and other components which have the reference starting
86  // with "#" are not included in netlist (pseudo or virtual components)
87  ref = comp->GetRef( aSheetPath );
88 
89  if( ref[0] == wxChar( '#' ) )
90  return nullptr;
91 
92  // if( Component->m_FlagControlMulti == 1 )
93  // continue; /* yes */
94  // removed because with multiple instances of one schematic
95  // (several sheets pointing to 1 screen), this will be erroneously be
96  // toggled.
97 
98  if( !comp->GetPartRef() )
99  return nullptr;
100 
101  // If component is a "multi parts per package" type
102  if( comp->GetPartRef()->GetUnitCount() > 1 )
103  {
104  // test if this reference has already been processed, and if so skip
105  if( m_ReferencesAlreadyFound.Lookup( ref ) )
106  return nullptr;
107  }
108 
109  // record the usage of this library component entry.
110  m_LibParts.insert( comp->GetPartRef().get() ); // rejects non-unique pointers
111 
112  return comp;
113 }
114 
115 
117 static bool sortPinsByNum( PIN_INFO& aPin1, PIN_INFO& aPin2 )
118 {
119  // return "lhs < rhs"
120  return UTIL::RefDesStringCompare( aPin1.num, aPin2.num ) < 0;
121 }
122 
123 
125 {
126  wxString ref( comp->GetRef( aSheetPath ) );
127 
128  // Power symbols and other components which have the reference starting
129  // with "#" are not included in netlist (pseudo or virtual components)
130 
131  if( ref[0] == wxChar( '#' ) )
132  return;
133 
134  // if( Component->m_FlagControlMulti == 1 )
135  // continue; /* yes */
136  // removed because with multiple instances of one schematic
137  // (several sheets pointing to 1 screen), this will be erroneously be
138  // toggled.
139 
140  if( !comp->GetPartRef() )
141  return;
142 
143  m_SortedComponentPinList.clear();
144 
145  // If component is a "multi parts per package" type
146  if( comp->GetPartRef()->GetUnitCount() > 1 )
147  {
148  // Collect all pins for this reference designator by searching
149  // the entire design for other parts with the same reference designator.
150  // This is only done once, it would be too expensive otherwise.
151  findAllUnitsOfComponent( comp, comp->GetPartRef().get(), aSheetPath );
152  }
153 
154  else // entry->GetUnitCount() <= 1 means one part per package
155  {
156  for( const auto& pin : comp->GetSchPins( aSheetPath ) )
157  {
158  if( auto conn = pin->Connection( *aSheetPath ) )
159  {
160  const wxString& netName = conn->Name();
161 
162  // Skip unconnected pins
163  CONNECTION_SUBGRAPH* sg =
164  m_schematic->ConnectionGraph()->FindSubgraphByName( netName, *aSheetPath );
165 
166  if( !sg || sg->m_no_connect || sg->m_items.size() < 2 )
167  continue;
168 
169  m_SortedComponentPinList.emplace_back( pin->GetNumber(), netName );
170  }
171  }
172  }
173 
174  // Sort pins in m_SortedComponentPinList by pin number
176 
177  // Remove duplicate Pins in m_SortedComponentPinList
179 
180  // record the usage of this library component entry.
181  m_LibParts.insert( comp->GetPartRef().get() ); // rejects non-unique pointers
182 }
183 
184 
186 {
187  for( unsigned ii = 0; ii < m_SortedComponentPinList.size(); ii++ )
188  {
189  if( m_SortedComponentPinList[ii].num.empty() ) /* already deleted */
190  continue;
191 
192  /* Search for duplicated pins
193  * If found, remove duplicates. The priority is to keep connected pins
194  * and remove unconnected
195  * - So this allows (for instance when using multi op amps per package
196  * - to connect only one op amp to power
197  * Because the pin list is sorted by m_PinNum value, duplicated pins
198  * are necessary successive in list
199  */
200  int idxref = ii;
201 
202  for( unsigned jj = ii + 1; jj < m_SortedComponentPinList.size(); jj++ )
203  {
204  if( m_SortedComponentPinList[jj].num.empty() ) // Already removed
205  continue;
206 
207  // if other pin num, stop search,
208  // because all pins having the same number are consecutive in list.
209  if( m_SortedComponentPinList[idxref].num != m_SortedComponentPinList[jj].num )
210  break;
211 
212  m_SortedComponentPinList[jj].num.clear();
213  }
214  }
215 }
216 
217 
219  LIB_PART* aEntry, SCH_SHEET_PATH* aSheetPath )
220 {
221  wxString ref = aComponent->GetRef( aSheetPath );
222  wxString ref2;
223 
224  SCH_SHEET_LIST sheetList = m_schematic->GetSheets();
225 
226  for( unsigned i = 0; i < sheetList.size(); i++ )
227  {
228  for( auto item : sheetList[i].LastScreen()->Items().OfType( SCH_COMPONENT_T ) )
229  {
230  SCH_COMPONENT* comp2 = static_cast<SCH_COMPONENT*>( item );
231 
232  ref2 = comp2->GetRef( &sheetList[i] );
233 
234  if( ref2.CmpNoCase( ref ) != 0 )
235  continue;
236 
237  for( const auto& pin : comp2->GetSchPins( aSheetPath ) )
238  {
239  if( auto conn = pin->Connection( *aSheetPath ) )
240  {
241  const wxString& netName = conn->Name();
242 
243  // Skip unconnected pins
245  netName, *aSheetPath );
246 
247  if( !sg || sg->m_no_connect || sg->m_items.size() < 2 )
248  continue;
249 
250  m_SortedComponentPinList.emplace_back( pin->GetNumber(), netName );
251  }
252  }
253  }
254  }
255 }
CONNECTION_SUBGRAPH * FindSubgraphByName(const wxString &aNetName, const SCH_SHEET_PATH &aPath)
Returns the subgraph for a given net name on a given sheet.
SCH_SHEET_LIST.
bool Lookup(const wxString &aString)
Function Lookup returns true if aString already exists in the set, otherwise returns false and adds a...
SCH_SHEET_LIST GetSheets() const
Builds and returns an updated schematic hierarchy TODO: can this be cached?
Definition: schematic.h:92
This file is part of the common library TODO brief description.
UNIQUE_STRINGS m_ReferencesAlreadyFound
Used for "multi parts per package" components, avoids processing a lib component more than once.
SCHEMATIC * m_schematic
The schematic we're generating a netlist for.
CONNECTION_GRAPH * ConnectionGraph() const
Definition: schematic.h:132
This file is part of the common library.
Collection of utility functions for component reference designators (refdes)
static wxString MakeCommandLine(const wxString &aFormatString, const wxString &aNetlistFile, const wxString &aFinalFile, const wxString &aProjectDirectory)
Function MakeCommandLine builds up a string that describes a command line for executing a child proce...
SCH_COMPONENT * findNextComponent(EDA_ITEM *aItem, SCH_SHEET_PATH *aSheetPath)
Checks if the given component should be processed for netlisting.
void findAllUnitsOfComponent(SCH_COMPONENT *aComponent, LIB_PART *aEntry, SCH_SHEET_PATH *aSheetPath)
Function findAllUnitsOfComponent is used for "multiple parts per package" components.
A subgraph is a set of items that are electrically connected on a single sheet.
int RefDesStringCompare(const wxString &aFirst, const wxString &aSecond)
Acts just like the strcmp function but treats numbers within the string text correctly for sorting.
std::set< LIB_PART *, LIB_PART_LESS_THAN > m_LibParts
unique library parts used.
std::vector< SCH_ITEM * > m_items
void CreatePinList(SCH_COMPONENT *aItem, SCH_SHEET_PATH *aSheetPath)
Function findNextComponentAndCreatePinList finds a component from the DrawList and builds its pin lis...
Define a library symbol object.
std::vector< PIN_INFO > m_SortedComponentPinList
Used to temporarily store and filter the list of pins of a schematic component when generating schema...
SCH_SHEET_PATH.
std::unique_ptr< LIB_PART > & GetPartRef()
const wxString GetRef(const SCH_SHEET_PATH *aSheet, bool aIncludeUnit=false)
Return the reference for the given sheet path.
static bool sortPinsByNum(PIN_INFO &aPin1, PIN_INFO &aPin2)
Comparison routine for sorting by pin numbers.
see class PGM_BASE
Schematic symbol object.
Definition: sch_component.h:88
EDA_ITEM is a base class for most all the KiCad significant classes, used in schematics and boards.
Definition: base_struct.h:159
Definition for part library class.
SCH_ITEM * m_no_connect
No-connect item in graph, if any.
wxString num
SCH_PIN_PTRS GetSchPins(const SCH_SHEET_PATH *aSheet=nullptr) const
Retrieves a list of the SCH_PINs for the given sheet path.
KICAD_T Type() const
Function Type()
Definition: base_struct.h:193
void eraseDuplicatePins()
Function eraseDuplicatePins erase duplicate Pins from m_SortedComponentPinList (i....