KiCad PCB EDA Suite
netlist_exporter_pspice.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-2019 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 
27 #include <fctsys.h>
28 #include <build_version.h>
29 #include <confirm.h>
30 
31 #include <map>
32 #include <search_stack.h>
33 
34 #include <connection_graph.h>
35 #include <sch_edit_frame.h>
36 #include <sch_reference_list.h>
37 #include <env_paths.h>
38 
39 #include <wx/tokenzr.h>
40 #include <wx/regex.h>
41 
42 
43 wxString NETLIST_EXPORTER_PSPICE::GetSpiceDevice( const wxString& aComponent ) const
44 {
45  const auto& spiceItems = GetSpiceItems();
46 
47  auto it = std::find_if( spiceItems.begin(), spiceItems.end(), [&]( const SPICE_ITEM& item ) {
48  return item.m_refName == aComponent;
49  } );
50 
51  if( it == spiceItems.end() )
52  return wxEmptyString;
53 
54  // Prefix the device type if plain reference would result in a different device type
55  return it->m_primitive != it->m_refName[0] ?
56  wxString( it->m_primitive + it->m_refName ) : it->m_refName;
57 }
58 
59 
60 bool NETLIST_EXPORTER_PSPICE::WriteNetlist( const wxString& aOutFileName, unsigned aNetlistOptions )
61 {
62  FILE_OUTPUTFORMATTER outputFile( aOutFileName, wxT( "wt" ), '\'' );
63 
64  return Format( &outputFile, aNetlistOptions );
65 }
66 
68 {
69  // some chars are not accepted in netnames in spice netlists, because they are separators
70  // they are replaced an underscore or some other allowed char.
71  // Note: this is a static function
72 
73  aNetName.Replace( "(", "_" );
74  aNetName.Replace( ")", "_" );
75  aNetName.Replace( " ", "_" );
76 }
77 
78 
79 bool NETLIST_EXPORTER_PSPICE::Format( OUTPUTFORMATTER* aFormatter, unsigned aCtl )
80 {
81  // Netlist options
82  const bool useNetcodeAsNetName = false;//aCtl & NET_USE_NETCODES_AS_NETNAMES;
83 
84  // default title
85  m_title = "KiCad schematic";
86 
87  if( !ProcessNetlist( aCtl ) )
88  return false;
89 
90  aFormatter->Print( 0, ".title %s\n", (const char*) m_title.c_str() );
91 
92  // Write .include directives
93  for( const auto& lib : m_libraries )
94  {
95  wxString full_path;
96 
97  if( ( aCtl & NET_ADJUST_INCLUDE_PATHS ) )
98  {
99  // Look for the library in known search locations
100  full_path = ResolveFile( lib, &Pgm().GetLocalEnvVariables(), &m_schematic->Prj() );
101 
102  if( full_path.IsEmpty() )
103  {
104  DisplayError( NULL, wxString::Format( _( "Could not find library file %s" ), lib ) );
105  full_path = lib;
106  }
107  }
108  else
109  full_path = lib; // just use the unaltered path
110 
111  aFormatter->Print( 0, ".include \"%s\"\n", (const char*) full_path.c_str() );
112  }
113 
114  unsigned int NC_counter = 1;
115 
116  for( const auto& item : m_spiceItems )
117  {
118  if( !item.m_enabled )
119  continue;
120 
121  wxString device = GetSpiceDevice( item.m_refName );
122  aFormatter->Print( 0, "%s ", (const char*) device.c_str() );
123 
124  size_t pspiceNodes = item.m_pinSequence.empty() ? item.m_pins.size() : item.m_pinSequence.size();
125 
126  for( size_t ii = 0; ii < pspiceNodes; ii++ )
127  {
128  // Use the custom order if defined, otherwise use the standard pin order as defined in the compon
129  size_t activePinIndex = item.m_pinSequence.empty() ? ii : item.m_pinSequence[ii];
130  // Valid used Node Indexes are in the set
131  // {0,1,2,...m_item.m_pin.size()-1}
132  if( activePinIndex >= item.m_pins.size() )
133  {
134  wxASSERT_MSG( false, "Used an invalid pin number in node sequence" );
135  continue;
136  }
137 
138  wxString netName = item.m_pins[activePinIndex];
139 
140  wxASSERT( m_netMap.count( netName ) );
141 
142  if( useNetcodeAsNetName )
143  {
144  aFormatter->Print( 0, "%d ", m_netMap[netName] );
145  }
146  else
147  {
148  // Replace parenthesis with underscore to prevent parse issues with simulators
149  ReplaceForbiddenChars( netName );
150 
151  // unescape net names that contain a escaped sequence ("{slash}"):
152  netName = UnescapeString( netName );
153 
154  // Borrow LTSpice's nomenclature for unconnected nets
155  if( netName.IsEmpty() )
156  netName = wxString::Format( wxT( "NC_%.2u" ), NC_counter++ );
157 
158  aFormatter->Print( 0, "%s ", TO_UTF8( netName ) );
159  }
160  }
161 
162  aFormatter->Print( 0, "%s\n", (const char*) item.m_model.c_str() );
163  }
164 
165  // Print out all directives found in the text fields on the schematics
166  writeDirectives( aFormatter, aCtl );
167 
168  aFormatter->Print( 0, ".end\n" );
169 
170  return true;
171 }
172 
173 
175  SCH_COMPONENT* aComponent, unsigned aCtl )
176 {
177  SCH_FIELD* field = aComponent->FindField( GetSpiceFieldName( aField ) );
178  return field ? field->GetText() : GetSpiceFieldDefVal( aField, aComponent, aCtl );
179 }
180 
181 
183  SCH_COMPONENT* aComponent, unsigned aCtl )
184 {
185  switch( aField )
186  {
187  case SF_PRIMITIVE:
188  {
189  const wxString refName = aComponent->GetField( REFERENCE )->GetText();
190  return refName.GetChar( 0 );
191  break;
192  }
193 
194  case SF_MODEL:
195  {
196  wxChar prim = aComponent->GetField( REFERENCE )->GetText().GetChar( 0 );
197  wxString value = aComponent->GetField( VALUE )->GetText();
198 
199  // Is it a passive component?
200  if( aCtl & NET_ADJUST_PASSIVE_VALS && ( prim == 'C' || prim == 'L' || prim == 'R' ) )
201  {
202  // Regular expression to match common formats used for passive parts description
203  // (e.g. 100k, 2k3, 1 uF)
204  wxRegEx passiveVal( "^([0-9\\. ]+)([fFpPnNuUmMkKgGtT]|M(e|E)(g|G))?([fFhH]|ohm)?([-1-9 ]*)$" );
205 
206  if( passiveVal.Matches( value ) )
207  {
208  wxString prefix( passiveVal.GetMatch( value, 1 ) );
209  wxString unit( passiveVal.GetMatch( value, 2 ) );
210  wxString suffix( passiveVal.GetMatch( value, 6 ) );
211 
212  prefix.Trim(); prefix.Trim( false );
213  unit.Trim(); unit.Trim( false );
214  suffix.Trim(); suffix.Trim( false );
215 
216  // Make 'mega' units comply with the Spice expectations
217  if( unit == "M" )
218  unit = "Meg";
219 
220  value = prefix + unit + suffix;
221  }
222  }
223 
224  return value;
225  break;
226  }
227 
228  case SF_ENABLED:
229  return wxString( "Y" );
230  break;
231 
232  case SF_NODE_SEQUENCE:
233  {
234  wxString nodeSeq;
235  std::vector<LIB_PIN*> pins;
236 
237  wxCHECK( aComponent->GetPartRef(), wxString() );
238  aComponent->GetPartRef()->GetPins( pins );
239 
240  for( auto pin : pins )
241  nodeSeq += pin->GetNumber() + " ";
242 
243  nodeSeq.Trim();
244 
245  return nodeSeq;
246  break;
247  }
248 
249  case SF_LIB_FILE:
250  // There is no default Spice library
251  return wxEmptyString;
252  break;
253 
254  default:
255  wxASSERT_MSG( false, "Missing default value definition for a Spice field" );
256  break;
257  }
258 
259 
260  return wxString( "<unknown>" );
261 }
262 
263 
265 {
266  const wxString delimiters( "{:,; }" );
267 
268  SCH_SHEET_LIST sheetList = m_schematic->GetSheets();
269  // Set of reference names, to check for duplications
270  std::set<wxString> refNames;
271 
272  m_netMap.clear();
273  m_netMap["GND"] = 0; // 0 is reserved for "GND"
274  int netIdx = 1;
275 
276  m_libraries.clear();
278  m_LibParts.clear();
279 
280  UpdateDirectives( aCtl );
281 
282  for( unsigned sheet_idx = 0; sheet_idx < sheetList.size(); sheet_idx++ )
283  {
284  SCH_SHEET_PATH sheet = sheetList[sheet_idx];
285 
286  // Process component attributes to find Spice directives
287  for( auto item : sheet.LastScreen()->Items().OfType( SCH_COMPONENT_T ) )
288  {
289  SCH_COMPONENT* comp = findNextComponent( item, &sheet );
290 
291  if( !comp )
292  continue;
293 
294  CreatePinList( comp, &sheet );
295  SPICE_ITEM spiceItem;
296  spiceItem.m_parent = comp;
297 
298  // Obtain Spice fields
299  SCH_FIELD* fieldLibFile = comp->FindField( GetSpiceFieldName( SF_LIB_FILE ) );
300  SCH_FIELD* fieldSeq = comp->FindField( GetSpiceFieldName( SF_NODE_SEQUENCE ) );
301 
302  spiceItem.m_primitive = GetSpiceField( SF_PRIMITIVE, comp, aCtl )[0];
303  spiceItem.m_model = GetSpiceField( SF_MODEL, comp, aCtl );
304  spiceItem.m_refName = comp->GetRef( &sheet );
305 
306  // Duplicate references will result in simulation errors
307  if( refNames.count( spiceItem.m_refName ) )
308  {
309  DisplayError( NULL, wxT( "There are duplicate components. "
310  "You need to annotate schematics first." ) );
311  return false;
312  }
313 
314  refNames.insert( spiceItem.m_refName );
315 
316  // Check to see if component should be removed from Spice netlist
317  spiceItem.m_enabled = StringToBool( GetSpiceField( SF_ENABLED, comp, aCtl ) );
318 
319  if( fieldLibFile && !fieldLibFile->GetText().IsEmpty() )
320  m_libraries.insert( fieldLibFile->GetText() );
321 
322  wxArrayString pinNames;
323 
324  // Store pin information
325  for( const PIN_INFO& pin : m_SortedComponentPinList )
326  {
327  // Create net mapping
328  spiceItem.m_pins.push_back( pin.netName );
329  pinNames.Add( pin.num );
330 
331  if( m_netMap.count( pin.netName ) == 0 )
332  m_netMap[pin.netName] = netIdx++;
333  }
334 
335  // Check if an alternative pin sequence is available:
336  if( fieldSeq )
337  {
338  // Get the string containing the sequence of nodes:
339  const wxString& nodeSeqIndexLineStr = fieldSeq->GetText();
340 
341  // Verify field exists and is not empty:
342  if( !nodeSeqIndexLineStr.IsEmpty() )
343  {
344  // Get Alt Pin Name Array From User:
345  wxStringTokenizer tkz( nodeSeqIndexLineStr, delimiters );
346 
347  while( tkz.HasMoreTokens() )
348  {
349  wxString pinIndex = tkz.GetNextToken();
350  int seq;
351 
352  // Find PinName In Standard List assign Standard List Index to Name:
353  seq = pinNames.Index( pinIndex );
354 
355  if( seq != wxNOT_FOUND )
356  spiceItem.m_pinSequence.push_back( seq );
357  }
358  }
359  }
360 
361  m_spiceItems.push_back( spiceItem );
362  }
363  }
364 
365  return true;
366 }
367 
368 
370 {
371  const SCH_SHEET_LIST& sheetList = m_schematic->GetSheets();
372  wxRegEx couplingK( "^[kK][[:digit:]]*[[:space:]]+[[:alnum:]]+[[:space:]]+[[:alnum:]]+",
373  wxRE_ADVANCED );
374 
375  m_directives.clear();
376  bool controlBlock = false;
377  bool circuitBlock = false;
378 
379  for( unsigned i = 0; i < sheetList.size(); i++ )
380  {
381  for( auto item : sheetList[i].LastScreen()->Items().OfType( SCH_TEXT_T ) )
382  {
383  wxString text = static_cast<SCH_TEXT*>( item )->GetText();
384 
385  if( text.IsEmpty() )
386  continue;
387 
388  // Analyze each line of a text field
389  wxStringTokenizer tokenizer( text, "\r\n" );
390 
391  // Flag to follow multiline directives
392  bool directiveStarted = false;
393 
394  while( tokenizer.HasMoreTokens() )
395  {
396  wxString line( tokenizer.GetNextToken() );
397 
398  // Cleanup: remove preceding and trailing white-space characters
399  line.Trim( true ).Trim( false );
400  // Convert to lower-case for parsing purposes only
401  wxString lowercaseline = line;
402  lowercaseline.MakeLower();
403 
404  // 'Include' directive stores the library file name, so it
405  // can be later resolved using a list of paths
406  if( lowercaseline.StartsWith( ".inc" ) )
407  {
408  wxString lib = line.AfterFirst( ' ' );
409 
410  if( lib.IsEmpty() )
411  continue;
412 
413  // Strip quotes if present
414  if( ( lib.StartsWith( "\"" ) && lib.EndsWith( "\"" ) )
415  || ( lib.StartsWith( "'" ) && lib.EndsWith( "'" ) ) )
416  {
417  lib = lib.Mid( 1, lib.Length() - 2 );
418  }
419 
420  m_libraries.insert( lib );
421  }
422 
423  // Store the title to be sure it appears
424  // in the first line of output
425  else if( lowercaseline.StartsWith( ".title " ) )
426  {
427  m_title = line.AfterFirst( ' ' );
428  }
429 
430  else if( line.StartsWith( '.' ) // one-line directives
431  || controlBlock // .control .. .endc block
432  || circuitBlock // .subckt .. .ends block
433  || couplingK.Matches( line ) // K## L## L## coupling constant
434  || ( directiveStarted && line.StartsWith( '+' ) ) ) // multiline directives
435  {
436  m_directives.push_back( line );
437  }
438 
439 
440  // Handle .control .. .endc blocks
441  if( lowercaseline.IsSameAs( ".control" ) && ( !controlBlock ) )
442  controlBlock = true;
443 
444  if( lowercaseline.IsSameAs( ".endc" ) && controlBlock )
445  controlBlock = false;
446 
447  // Handle .subckt .. .ends blocks
448  if( lowercaseline.StartsWith( ".subckt" ) && ( !circuitBlock ) )
449  circuitBlock = true;
450 
451  if( lowercaseline.IsSameAs( ".ends" ) && circuitBlock )
452  circuitBlock = false;
453 
454  // Mark directive as started or continued in case it is a multi-line one
455  directiveStarted = line.StartsWith( '.' )
456  || ( directiveStarted && line.StartsWith( '+' ) );
457  }
458  }
459  }
460 }
461 
462 
463 void NETLIST_EXPORTER_PSPICE::writeDirectives( OUTPUTFORMATTER* aFormatter, unsigned aCtl ) const
464 {
465  for( auto& dir : m_directives )
466  {
467  aFormatter->Print( 0, "%s\n", (const char*) dir.c_str() );
468  }
469 }
470 
471 
472 // Entries in the vector below have to follow the order in SPICE_FIELD enum
473 const std::vector<wxString> NETLIST_EXPORTER_PSPICE::m_spiceFields = {
474  "Spice_Primitive",
475  "Spice_Model",
476  "Spice_Netlist_Enabled",
477  "Spice_Node_Sequence",
478  "Spice_Lib_File"
479 };
void DisplayError(wxWindow *aParent, const wxString &aText, int aDisplayTime)
Display an error or warning message box with aMessage.
Definition: confirm.cpp:239
SCH_SHEET_LIST.
SCH_FIELD instances are attached to a component and provide a place for the component's value,...
Definition: sch_field.h:52
bool Format(OUTPUTFORMATTER *aFormatter, unsigned aCtl)
SCH_FIELD * FindField(const wxString &aFieldName, bool aIncludeDefaultFields=true)
Search for a SCH_FIELD with aFieldName.
const SPICE_ITEM_LIST & GetSpiceItems() const
Returns list of items representing schematic components in the Spice world.
wxString ResolveFile(const wxString &aFileName, const ENV_VAR_MAP *aEnvVars, const PROJECT *aProject)
Searches the default paths trying to find one with the requested file.
Definition: env_paths.cpp:151
KIWAY Kiway & Pgm(), KFCTL_STANDALONE
The global Program "get" accessor.
Definition: single_top.cpp:104
SCH_SHEET_LIST GetSheets() const
Builds and returns an updated schematic hierarchy TODO: can this be cached?
Definition: schematic.h:92
UNIQUE_STRINGS m_ReferencesAlreadyFound
Used for "multi parts per package" components, avoids processing a lib component more than once.
std::set< wxString > m_libraries
Libraries used by the simulated circuit
SCHEMATIC * m_schematic
The schematic we're generating a netlist for.
This file is part of the common library.
SCH_COMPONENT * m_parent
Schematic component represented by this SPICE_ITEM.
Structure to represent a schematic component in the Spice simulation.
std::vector< wxString > m_pins
Array containing Standard Pin Name
static const wxString & GetSpiceFieldName(SPICE_FIELD aField)
Returns a string used for a particular component field related to Spice simulation.
EE_TYPE OfType(KICAD_T aType)
Definition: sch_rtree.h:219
OUTPUTFORMATTER is an important interface (abstract class) used to output 8 bit text in a convenient ...
Definition: richio.h:327
bool m_enabled
Flag to indicate whether the component should be used in simulation.
void Clear()
Function Clear erases the record.
std::vector< wxString > m_directives
Spice directives found in the processed schematic sheet
SCH_COMPONENT * findNextComponent(EDA_ITEM *aItem, SCH_SHEET_PATH *aSheetPath)
Checks if the given component should be processed for netlisting.
static bool StringToBool(const wxString &aStr)
Convertes typical boolean string values (no/yes, true/false, 1/0) to a boolean value.
wxString m_model
Library model (for semiconductors and subcircuits), component value (for passive components) or volta...
Field Reference of part, i.e. "IC21".
SPICE_ITEM_LIST m_spiceItems
List of items representing schematic components in the Spice world
static wxString GetSpiceFieldDefVal(SPICE_FIELD aField, SCH_COMPONENT *aComponent, unsigned aCtl)
Retrieves the default value for a given field.
std::set< LIB_PART *, LIB_PART_LESS_THAN > m_LibParts
unique library parts used.
wxString m_title
Spice simulation title found in the processed schematic sheet
NET_INDEX_MAP m_netMap
Maps circuit nodes to net names
#define NULL
virtual void writeDirectives(OUTPUTFORMATTER *aFormatter, unsigned aCtl) const
Saves the Spice directives.
void UpdateDirectives(unsigned aCtl)
Updates the vector of Spice directives placed in the schematics.
void CreatePinList(SCH_COMPONENT *aItem, SCH_SHEET_PATH *aSheetPath)
Function findNextComponentAndCreatePinList finds a component from the DrawList and builds its pin lis...
std::vector< PIN_INFO > m_SortedComponentPinList
Used to temporarily store and filter the list of pins of a schematic component when generating schema...
bool ProcessNetlist(unsigned aCtl)
Processes the netlist to create net mapping and a list of SPICE_ITEMs.
static void ReplaceForbiddenChars(wxString &aNetName)
some chars are not accepted in netnames in spice netlists.
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.
SCH_FIELD * GetField(int aFieldNdx)
Returns a field in this symbol.
bool WriteNetlist(const wxString &aOutFileName, unsigned aNetlistOptions) override
Function WriteNetlist writes to specified output file.
Field Value of part, i.e. "3.3K".
SCH_SCREEN * LastScreen()
Function LastScreen.
void Format(OUTPUTFORMATTER *out, int aNestLevel, int aCtl, CPTREE &aTree)
Function Format outputs a PTREE into s-expression format via an OUTPUTFORMATTER derivative.
Definition: ptree.cpp:205
#define _(s)
Definition: 3d_actions.cpp:33
wxString UnescapeString(const wxString &aSource)
Definition: string.cpp:131
EE_RTREE & Items()
Definition: sch_screen.h:158
static wxString GetSpiceField(SPICE_FIELD aField, SCH_COMPONENT *aComponent, unsigned aCtl)
Retrieves either the requested field value or the default value.
Schematic symbol object.
Definition: sch_component.h:88
PROJECT & Prj() const
Return a reference to the project this schematic is part of.
Definition: schematic.h:77
#define TO_UTF8(wxstring)
static const std::vector< wxString > m_spiceFields
wxChar m_primitive
Spice primitive type (
wxString GetSpiceDevice(const wxString &aComponent) const
Returns name of Spice device corresponding to a schematic component.
FILE_OUTPUTFORMATTER may be used for text file output.
Definition: richio.h:492
int PRINTF_FUNC Print(int nestLevel, const char *fmt,...)
Function Print formats and writes text to the output stream.
Definition: richio.cpp:404
std::vector< int > m_pinSequence
Numeric indices into m_SortedComponentPinList
virtual const wxString & GetText() const
Return the string associated with the text object.
Definition: eda_text.h:126