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 <netlist.h>
36 #include <sch_reference_list.h>
37 
38 
39 wxString NETLIST_EXPORTER::MakeCommandLine( const wxString& aFormatString,
40  const wxString& aNetlistFile, const wxString& aFinalFile, const wxString& aProjectPath )
41 {
42  // Expand format symbols in the command line:
43  // %B => base filename of selected output file, minus path and extension.
44  // %P => project directory name, without trailing '/' or '\'.
45  // %I => full filename of the input file (the intermediate net file).
46  // %O => complete filename and path (but without extension) of the user chosen output file.
47 
48  wxString ret = aFormatString;
49  wxFileName in = aNetlistFile;
50  wxFileName out = aFinalFile;
51  wxString str_out = out.GetFullPath();
52 
53  ret.Replace( "%P", aProjectPath, true );
54  ret.Replace( "%B", out.GetName(), true );
55  ret.Replace( "%I", in.GetFullPath(), true );
56 
57 #ifdef __WINDOWS__
58  // A ugly hack to run xsltproc that has a serious bug on Window since a long time:
59  // the filename given after -o option (output filename) cannot use '\' in filename
60  // so replace if by '/' if possible (I mean if the filename does not start by "\\"
61  // that is a filename on a Windows server)
62 
63  if( !str_out.StartsWith( "\\\\" ) )
64  str_out.Replace( "\\", "/" );
65 #endif
66 
67  ret.Replace( "%O", str_out, true );
68 
69  return ret;
70 }
71 
72 
73 void NETLIST_EXPORTER::sprintPinNetName( wxString& aResult,
74  const wxString& aNetNameFormat, NETLIST_OBJECT* aPin,
75  bool aUseNetcodeAsNetName )
76 {
77  int netcode = aPin->GetNet();
78 
79  // Not wxString::Clear(), which would free memory. We want the worst
80  // case wxString memory to grow to avoid reallocation from within the
81  // caller's loop.
82  aResult.Empty();
83 
84  if( netcode != 0 && aPin->GetConnectionType() == PAD_CONNECT )
85  {
86  if( aUseNetcodeAsNetName )
87  {
88  aResult.Printf( "%d", netcode );
89  }
90  else
91  {
92  aResult = aPin->GetNetName();
93 
94  if( aResult.IsEmpty() ) // No net name: give a name from net code
95  aResult.Printf( aNetNameFormat.GetData(), netcode );
96  }
97  }
98 }
99 
100 
102 {
103  wxString ref;
104 
105  // continue searching from the middle of a linked list (the draw list)
106  for( ; aItem; aItem = aItem->Next() )
107  {
108  if( aItem->Type() != SCH_COMPONENT_T )
109  continue;
110 
111  // found next component
112  SCH_COMPONENT* comp = (SCH_COMPONENT*) aItem;
113 
114  // Power symbols and other components which have the reference starting
115  // with "#" are not included in netlist (pseudo or virtual components)
116  ref = comp->GetRef( aSheetPath );
117 
118  if( ref[0] == wxChar( '#' ) )
119  continue;
120 
121  // if( Component->m_FlagControlMulti == 1 )
122  // continue; /* yes */
123  // removed because with multiple instances of one schematic
124  // (several sheets pointing to 1 screen), this will be erroneously be
125  // toggled.
126 
127  if( !comp->GetPartRef() )
128  continue;
129 
130  // If component is a "multi parts per package" type
131  if( comp->GetPartRef()->GetUnitCount() > 1 )
132  {
133  // test if this reference has already been processed, and if so skip
134  if( m_ReferencesAlreadyFound.Lookup( ref ) )
135  continue;
136  }
137 
138  // record the usage of this library component entry.
139  m_LibParts.insert( comp->GetPartRef().get() ); // rejects non-unique pointers
140 
141  return comp;
142  }
143 
144  return NULL;
145 }
146 
147 
149 static bool sortPinsByNum( NETLIST_OBJECT* aPin1, NETLIST_OBJECT* aPin2 )
150 {
151  // return "lhs < rhs"
152  return UTIL::RefDesStringCompare( aPin1->GetPinNumText(), aPin2->GetPinNumText() ) < 0;
153 }
154 
155 
157  EDA_ITEM* aItem, SCH_SHEET_PATH* aSheetPath )
158 {
159  wxString ref;
160 
161  m_SortedComponentPinList.clear();
162 
163  // continue searching from the middle of a linked list (the draw list)
164  for( ; aItem; aItem = aItem->Next() )
165  {
166  if( aItem->Type() != SCH_COMPONENT_T )
167  continue;
168 
169  // found next component
170  SCH_COMPONENT* comp = (SCH_COMPONENT*) aItem;
171 
172  // Power symbols and other components which have the reference starting
173  // with "#" are not included in netlist (pseudo or virtual components)
174  ref = comp->GetRef( aSheetPath );
175 
176  if( ref[0] == wxChar( '#' ) )
177  continue;
178 
179  // if( Component->m_FlagControlMulti == 1 )
180  // continue; /* yes */
181  // removed because with multiple instances of one schematic
182  // (several sheets pointing to 1 screen), this will be erroneously be
183  // toggled.
184 
185  if( !comp->GetPartRef() )
186  continue;
187 
188  // If component is a "multi parts per package" type
189  if( comp->GetPartRef()->GetUnitCount() > 1 )
190  {
191  // test if this reference has already been processed, and if so skip
192  if( m_ReferencesAlreadyFound.Lookup( ref ) )
193  continue;
194 
195  // Collect all pins for this reference designator by searching
196  // the entire design for other parts with the same reference designator.
197  // This is only done once, it would be too expensive otherwise.
198  findAllUnitsOfComponent( comp, comp->GetPartRef().get(), aSheetPath );
199  }
200 
201  else // entry->GetUnitCount() <= 1 means one part per package
202  {
203  LIB_PINS pins; // constructed once here
204 
205  comp->GetPartRef()->GetPins( pins, comp->GetUnitSelection( aSheetPath ),
206  comp->GetConvert() );
207 
208  for( size_t i = 0; i < pins.size(); i++ )
209  {
210  LIB_PIN* pin = pins[i];
211 
212  wxASSERT( pin->Type() == LIB_PIN_T );
213 
214  addPinToComponentPinList( comp, aSheetPath, pin );
215  }
216  }
217 
218  // Sort pins in m_SortedComponentPinList by pin number
219  sort( m_SortedComponentPinList.begin(),
221 
222  // Remove duplicate Pins in m_SortedComponentPinList
224 
225  // record the usage of this library component entry.
226  m_LibParts.insert( comp->GetPartRef().get() ); // rejects non-unique pointers
227 
228  return comp;
229  }
230 
231  return NULL;
232 }
233 
234 
236  SCH_SHEET_PATH* aSheetPath, LIB_PIN* aPin )
237 {
238  // Search the PIN description for Pin in g_NetObjectslist
239  for( unsigned ii = 0; ii < m_masterList->size(); ii++ )
240  {
241  NETLIST_OBJECT* pin = m_masterList->GetItem( ii );
242 
243  if( pin->m_Type != NET_PIN )
244  continue;
245 
246  if( pin->m_Link != aComponent )
247  continue;
248 
249  if( pin->m_PinNum != aPin->GetNumber() )
250  continue;
251 
252  // most expensive test at the end.
253  if( pin->m_SheetPath != *aSheetPath )
254  continue;
255 
256  m_SortedComponentPinList.push_back( pin );
257 
258  if( m_SortedComponentPinList.size() >= MAXPIN )
259  {
260  // Log message for Internal error
261  DisplayError( NULL, wxT( "addPinToComponentPinList err: MAXPIN reached" ) );
262  }
263 
264  return true; // we're done, we appended.
265  }
266 
267  return false;
268 }
269 
270 
272 {
273  for( unsigned ii = 0; ii < m_SortedComponentPinList.size(); ii++ )
274  {
275  if( m_SortedComponentPinList[ii] == NULL ) /* already deleted */
276  continue;
277 
278  /* Search for duplicated pins
279  * If found, remove duplicates. The priority is to keep connected pins
280  * and remove unconnected
281  * - So this allows (for instance when using multi op amps per package
282  * - to connect only one op amp to power
283  * Because the pin list is sorted by m_PinNum value, duplicated pins
284  * are necessary successive in list
285  */
286  int idxref = ii;
287  for( unsigned jj = ii + 1; jj < m_SortedComponentPinList.size(); jj++ )
288  {
289  if( m_SortedComponentPinList[jj] == NULL ) // Already removed
290  continue;
291 
292  // if other pin num, stop search,
293  // because all pins having the same number are consecutive in list.
294  if( m_SortedComponentPinList[idxref]->m_PinNum != m_SortedComponentPinList[jj]->m_PinNum )
295  break;
296 
297  if( m_SortedComponentPinList[idxref]->GetConnectionType() == PAD_CONNECT )
298  {
299  m_SortedComponentPinList[jj]->m_Flag = 1;
300  m_SortedComponentPinList[jj] = NULL;
301  }
302  else /* the reference pin is not connected: remove this pin if the
303  * other pin is connected */
304  {
305  if( m_SortedComponentPinList[jj]->GetConnectionType() == PAD_CONNECT )
306  {
307  m_SortedComponentPinList[idxref]->m_Flag = 1;
308  m_SortedComponentPinList[idxref] = NULL;
309  idxref = jj;
310  }
311  else // the 2 pins are not connected: remove the tested pin,
312  { // and continue ...
313  m_SortedComponentPinList[jj]->m_Flag = 1;
314  m_SortedComponentPinList[jj] = NULL;
315  }
316  }
317  }
318  }
319 }
320 
321 
323  LIB_PART* aEntry, SCH_SHEET_PATH* aSheetPath )
324 {
325  wxString ref = aComponent->GetRef( aSheetPath );
326  wxString ref2;
327 
328  SCH_SHEET_LIST sheetList( g_RootSheet );
329 
330  for( unsigned i = 0; i < sheetList.size(); i++ )
331  {
332  for( EDA_ITEM* item = sheetList[i].LastDrawList(); item; item = item->Next() )
333  {
334  if( item->Type() != SCH_COMPONENT_T )
335  continue;
336 
337  SCH_COMPONENT* comp2 = (SCH_COMPONENT*) item;
338 
339  ref2 = comp2->GetRef( &sheetList[i] );
340 
341  if( ref2.CmpNoCase( ref ) != 0 )
342  continue;
343 
344  int unit2 = comp2->GetUnitSelection( &sheetList[i] ); // slow
345 
346  for( LIB_PIN* pin = aEntry->GetNextPin(); pin; pin = aEntry->GetNextPin( pin ) )
347  {
348  wxASSERT( pin->Type() == LIB_PIN_T );
349 
350  if( pin->GetUnit() && pin->GetUnit() != unit2 )
351  continue;
352 
353  if( pin->GetConvert() && pin->GetConvert() != comp2->GetConvert() )
354  continue;
355 
356  // A suitable pin is found: add it to the current list
357  addPinToComponentPinList( comp2, &sheetList[i], pin );
358  }
359  }
360  }
361 }
362 
void DisplayError(wxWindow *aParent, const wxString &aText, int aDisplayTime)
Display an error or warning message box with aMessage.
Definition: confirm.cpp:236
Class SCH_SHEET_LIST.
NET_CONNECTION_T GetConnectionType() const
bool Lookup(const wxString &aString)
Function Lookup returns true if aString already exists in the set, otherwise returns false and adds a...
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.
This file is part of the common library.
std::vector< LIB_PIN * > LIB_PINS
Helper for defining a list of pin object pointers.
Definition: lib_item.h:55
int GetNet() const
static void sprintPinNetName(wxString &aResult, const wxString &aNetNameFormat, NETLIST_OBJECT *aPin, bool aUseNetcodeAsNetName=false)
Function sprintPinNetName formats the net name for aPin using aNetNameFormat into aResult.
int GetUnitSelection(SCH_SHEET_PATH *aSheet)
Collection of utility functions for component reference designators (refdes)
#define MAXPIN
Definition: netlist.h:53
SCH_SHEET_PATH m_SheetPath
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)
NETLIST_ITEM_T m_Type
void findAllUnitsOfComponent(SCH_COMPONENT *aComponent, LIB_PART *aEntry, SCH_SHEET_PATH *aSheetPath)
Function findAllUnitsOfComponent is used for "multiple parts per package" components.
NETLIST_OBJECT_LIST * m_masterList
EDA_ITEM * Next() const
Definition: base_struct.h:218
static bool sortPinsByNum(NETLIST_OBJECT *aPin1, NETLIST_OBJECT *aPin2)
Comparison routine for sorting by pin numbers.
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. LIB_PART items are sorted by names
SCH_SHEET * g_RootSheet
Definition: eeschema.cpp:47
Define a library symbol object.
SCH_COMPONENT * findNextComponentAndCreatePinList(EDA_ITEM *aItem, SCH_SHEET_PATH *aSheetPath)
Function findNextComponentAndCreatePinList finds a component from the DrawList and builds its pin lis...
Class SCH_SHEET_PATH.
std::unique_ptr< LIB_PART > & GetPartRef()
const wxString GetRef(const SCH_SHEET_PATH *aSheet)
Return the reference for the given sheet path.
const wxString & GetPinNumText() const
returns a pin number in wxString form.
LIB_PIN * GetNextPin(LIB_PIN *aItem=NULL)
Return the next pin object from the draw list.
const wxString & GetNumber() const
Definition: lib_pin.h:177
NETLIST_OBJECTS m_SortedComponentPinList
yes ownership, connected items flat list
NETLIST_OBJECT * GetItem(unsigned aIdx) const
Acces to an item in list.
see class PGM_BASE
wxString GetNetName(bool adoptTimestamp=false) const
Function GetNetName.
size_t i
Definition: json11.cpp:649
Class SCH_COMPONENT describes a real schematic component.
Definition: sch_component.h:73
Class EDA_ITEM is a base class for most all the KiCad significant classes, used in schematics and boa...
Definition: base_struct.h:163
bool addPinToComponentPinList(SCH_COMPONENT *Component, SCH_SHEET_PATH *sheet, LIB_PIN *PinEntry)
Function addPinToComponentPinList adds a new pin description to the pin list m_SortedComponentPinList...
int GetConvert() const
Definition for part library class.
SCH_ITEM * m_Link
KICAD_T Type() const
Function Type()
Definition: base_struct.h:210
void eraseDuplicatePins()
Function eraseDuplicatePins erase duplicate Pins from m_SortedComponentPinList (i....