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