KiCad PCB EDA Suite
NETLIST_EXPORTER_PSPICE Class Reference

Generate a PSPICE compatible netlist. More...

#include <netlist_exporter_pspice.h>

Inheritance diagram for NETLIST_EXPORTER_PSPICE:
NETLIST_EXPORTER_BASE NETLIST_EXPORTER_PSPICE_SIM

Public Member Functions

 NETLIST_EXPORTER_PSPICE (SCHEMATIC_IFACE *aSchematic)
 
virtual ~NETLIST_EXPORTER_PSPICE ()
 
const std::list< SPICE_ITEM > & GetSpiceItems () const
 Return list of items representing schematic components in the Spice world. More...
 
wxString GetSpiceDevice (const wxString &aSymbol) const
 Return name of Spice device corresponding to a schematic symbol. More...
 
bool WriteNetlist (const wxString &aOutFileName, unsigned aNetlistOptions) override
 Write to specified output file. More...
 
bool Format (OUTPUTFORMATTER *aFormatter, unsigned aCtl)
 
bool ProcessNetlist (unsigned aCtl)
 Process the netlist to create net mapping and a list of SPICE_ITEMs. More...
 
const std::map< wxString, int > & GetNetIndexMap () const
 Return a map of circuit nodes to net names. More...
 
void UpdateDirectives (unsigned aCtl)
 Update the vector of Spice directives placed in the schematics. More...
 
const std::vector< wxString > GetDirectives () const
 Return a vector of Spice directives found in the schematics. More...
 

Static Public Member Functions

static void ReplaceForbiddenChars (wxString &aNetName)
 Replace illegal spice net name characters with an underscore. More...
 
static const std::vector< wxString > & GetSpiceFields ()
 Return a vector of component field names related to Spice simulation. More...
 
static const wxString & GetSpiceFieldName (SPICE_FIELD aField)
 Return a string used for a particular component field related to Spice simulation. More...
 
static wxString GetSpiceField (SPICE_FIELD aField, SCH_SYMBOL *aSymbol, unsigned aCtl)
 Retrieve either the requested field value or the default value. More...
 
static wxString GetSpiceFieldDefVal (SPICE_FIELD aField, SCH_SYMBOL *aSymbol, unsigned aCtl)
 Retrieve the default value for a given field. More...
 
static bool StringToBool (const wxString &aStr)
 Convert typical boolean string values (no/yes, true/false, 1/0) to a boolean value. More...
 
static wxString MakeCommandLine (const wxString &aFormatString, const wxString &aNetlistFile, const wxString &aFinalFile, const wxString &aProjectDirectory)
 Build up a string that describes a command line for executing a child process. More...
 

Protected Member Functions

virtual void writeDirectives (OUTPUTFORMATTER *aFormatter, unsigned aCtl) const
 Save the Spice directives. More...
 
void CreatePinList (SCH_SYMBOL *aSymbol, SCH_SHEET_PATH *aSheetPath, bool aKeepUnconnectedPins)
 Find a symbol from the DrawList and builds its pin list in m_sortedSymbolPinList. More...
 
SCH_SYMBOLfindNextSymbol (EDA_ITEM *aItem, SCH_SHEET_PATH *aSheetPath)
 Check if the given symbol should be processed for netlisting. More...
 
void eraseDuplicatePins ()
 Erase duplicate pins from m_sortedSymbolPinList (i.e. More...
 
void findAllUnitsOfSymbol (SCH_SYMBOL *aSchSymbol, LIB_SYMBOL *aLibSymbol, SCH_SHEET_PATH *aSheetPath, bool aKeepUnconnectedPins)
 Find all units for symbols with multiple symbols per package. More...
 

Protected Attributes

std::vector< PIN_INFOm_sortedSymbolPinList
 Used to temporarily store and filter the list of pins of a schematic symbol when generating schematic symbol data in netlist (comp section). More...
 
UNIQUE_STRINGS m_referencesAlreadyFound
 Used for "multiple symbols per package" symbols to avoid processing a lib symbol more than once. More...
 
std::set< LIB_SYMBOL *, LIB_SYMBOL_LESS_THANm_libParts
 unique library symbols used. LIB_SYMBOL items are sorted by names More...
 
SCHEMATIC_IFACEm_schematic
 The schematic we're generating a netlist for. More...
 
SCH_SHEET_PATH m_savedCurrentSheet
 The schematic's CurrentSheet when we entered. Restore on exiting. More...
 

Private Attributes

wxString m_title
 Spice simulation title found in the schematic sheet. More...
 
std::vector< wxString > m_directives
 Spice directives found in the schematic sheet. More...
 
std::set< wxString > m_libraries
 Spice libraries used by the simulated circuit. More...
 
std::map< wxString, int > m_netMap
 Map spice nodes to net codes. More...
 
std::list< SPICE_ITEMm_spiceItems
 Items representing schematic symbols in Spice world. More...
 

Static Private Attributes

static const std::vector< wxString > m_spiceFields
 

Detailed Description

Generate a PSPICE compatible netlist.

Definition at line 90 of file netlist_exporter_pspice.h.

Constructor & Destructor Documentation

◆ NETLIST_EXPORTER_PSPICE()

NETLIST_EXPORTER_PSPICE::NETLIST_EXPORTER_PSPICE ( SCHEMATIC_IFACE aSchematic)
inline

Definition at line 93 of file netlist_exporter_pspice.h.

93  :
94  NETLIST_EXPORTER_BASE( aSchematic )
95  {
96  }
NETLIST_EXPORTER_BASE(SCHEMATIC_IFACE *aSchematic)

◆ ~NETLIST_EXPORTER_PSPICE()

virtual NETLIST_EXPORTER_PSPICE::~NETLIST_EXPORTER_PSPICE ( )
inlinevirtual

Definition at line 98 of file netlist_exporter_pspice.h.

99  {
100  }

Member Function Documentation

◆ CreatePinList()

void NETLIST_EXPORTER_BASE::CreatePinList ( SCH_SYMBOL aSymbol,
SCH_SHEET_PATH aSheetPath,
bool  aKeepUnconnectedPins 
)
protectedinherited

Find a symbol from the DrawList and builds its pin list in m_sortedSymbolPinList.

This list is sorted by pin number. The symbol is the next actual symbol after aSymbol. Power symbols and virtual symbols that have their reference designators starting with '#' are skipped. if aKeepUnconnectedPins = false, unconnected pins will be removed from list but usually we need all pins in netlists.

Definition at line 124 of file netlist_exporter_base.cpp.

127 {
128  wxString ref( aSymbol->GetRef( aSheetPath ) );
129 
130  // Power symbols and other symbols which have the reference starting with "#" are not
131  // included in netlist (pseudo or virtual symbols)
132 
133  if( ref[0] == wxChar( '#' ) )
134  return;
135 
136  // if( aSymbol->m_FlagControlMulti == 1 )
137  // continue; /* yes */
138  // removed because with multiple instances of one schematic (several sheets pointing to
139  // 1 screen), this will be erroneously be toggled.
140 
141  if( !aSymbol->GetLibSymbolRef() )
142  return;
143 
144  m_sortedSymbolPinList.clear();
145 
146  // If symbol is a "multi parts per package" type
147  if( aSymbol->GetLibSymbolRef()->GetUnitCount() > 1 )
148  {
149  // Collect all pins for this reference designator by searching the entire design for
150  // other parts with the same reference designator.
151  // This is only done once, it would be too expensive otherwise.
152  findAllUnitsOfSymbol( aSymbol, aSymbol->GetLibSymbolRef().get(),
153  aSheetPath, aKeepUnconnectedPins );
154  }
155 
156  else // GetUnitCount() <= 1 means one part per package
157  {
159 
160  for( const SCH_PIN* pin : aSymbol->GetPins( aSheetPath ) )
161  {
162  if( SCH_CONNECTION* conn = pin->Connection( aSheetPath ) )
163  {
164  const wxString& netName = conn->Name();
165 
166  if( !aKeepUnconnectedPins ) // Skip unconnected pins if requested
167  {
168  CONNECTION_SUBGRAPH* sg = graph->FindSubgraphByName( netName, *aSheetPath );
169 
170  if( !sg || sg->m_no_connect || sg->m_items.size() < 2 )
171  continue;
172  }
173 
174  m_sortedSymbolPinList.emplace_back( pin->GetShownNumber(), netName );
175  }
176  }
177  }
178 
179  // Sort pins in m_SortedSymbolPinList by pin number
181 
182  // Remove duplicate Pins in m_SortedSymbolPinList
184 
185  // record the usage of this library symbol
186  m_libParts.insert( aSymbol->GetLibSymbolRef().get() ); // rejects non-unique pointers
187 }
CONNECTION_SUBGRAPH * FindSubgraphByName(const wxString &aNetName, const SCH_SHEET_PATH &aPath)
Returns the subgraph for a given net name on a given sheet.
std::vector< PIN_INFO > m_sortedSymbolPinList
Used to temporarily store and filter the list of pins of a schematic symbol when generating schematic...
Calculates the connectivity of a schematic and generates netlists.
void eraseDuplicatePins()
Erase duplicate pins from m_sortedSymbolPinList (i.e.
virtual CONNECTION_GRAPH * ConnectionGraph() const =0
const wxString GetRef(const SCH_SHEET_PATH *aSheet, bool aIncludeUnit=false) const
Return the reference for the given sheet path.
Definition: sch_symbol.cpp:464
static bool sortPinsByNum(PIN_INFO &aPin1, PIN_INFO &aPin2)
Comparison routine for sorting by pin numbers.
A subgraph is a set of items that are electrically connected on a single sheet.
std::unique_ptr< LIB_SYMBOL > & GetLibSymbolRef()
Definition: sch_symbol.h:165
SCHEMATIC_IFACE * m_schematic
The schematic we're generating a netlist for.
std::vector< SCH_ITEM * > m_items
Each graphical item can have a SCH_CONNECTION describing its logical connection (to a bus or net).
void findAllUnitsOfSymbol(SCH_SYMBOL *aSchSymbol, LIB_SYMBOL *aLibSymbol, SCH_SHEET_PATH *aSheetPath, bool aKeepUnconnectedPins)
Find all units for symbols with multiple symbols per package.
SCH_ITEM * m_no_connect
No-connect item in graph, if any.
std::set< LIB_SYMBOL *, LIB_SYMBOL_LESS_THAN > m_libParts
unique library symbols used. LIB_SYMBOL items are sorted by names
std::vector< SCH_PIN * > GetPins(const SCH_SHEET_PATH *aSheet=nullptr) const
Retrieve a list of the SCH_PINs for the given sheet path.
Definition: sch_symbol.cpp:896

References SCHEMATIC_IFACE::ConnectionGraph(), NETLIST_EXPORTER_BASE::eraseDuplicatePins(), NETLIST_EXPORTER_BASE::findAllUnitsOfSymbol(), CONNECTION_GRAPH::FindSubgraphByName(), SCH_SYMBOL::GetLibSymbolRef(), SCH_SYMBOL::GetPins(), SCH_SYMBOL::GetRef(), CONNECTION_SUBGRAPH::m_items, NETLIST_EXPORTER_BASE::m_libParts, CONNECTION_SUBGRAPH::m_no_connect, NETLIST_EXPORTER_BASE::m_schematic, NETLIST_EXPORTER_BASE::m_sortedSymbolPinList, pin, and sortPinsByNum().

Referenced by ProcessNetlist(), and NETLIST_EXPORTER_ORCADPCB2::WriteNetlist().

◆ eraseDuplicatePins()

void NETLIST_EXPORTER_BASE::eraseDuplicatePins ( )
protectedinherited

Erase duplicate pins from m_sortedSymbolPinList (i.e.

set pointer in this list to NULL).

(This is a list of pins found in the whole schematic, for a single symbol.) These duplicate pins were put in list because some pins (power pins...) are found more than once when in "multiple symbols per package" symbols. For instance, a 74ls00 has 4 symbols, and therefore the VCC pin and GND pin appears 4 times in the list. Note: this list MUST be sorted by pin number (.m_PinNum member value) Also set the m_Flag member of "removed" NETLIST_OBJECT pin item to 1

Definition at line 190 of file netlist_exporter_base.cpp.

191 {
192  for( unsigned ii = 0; ii < m_sortedSymbolPinList.size(); ii++ )
193  {
194  if( m_sortedSymbolPinList[ii].num.empty() ) /* already deleted */
195  continue;
196 
197  /* Search for duplicated pins
198  * If found, remove duplicates. The priority is to keep connected pins
199  * and remove unconnected
200  * - So this allows (for instance when using multi op amps per package
201  * - to connect only one op amp to power
202  * Because the pin list is sorted by m_PinNum value, duplicated pins
203  * are necessary successive in list
204  */
205  int idxref = ii;
206 
207  for( unsigned jj = ii + 1; jj < m_sortedSymbolPinList.size(); jj++ )
208  {
209  if( m_sortedSymbolPinList[jj].num.empty() ) // Already removed
210  continue;
211 
212  // if other pin num, stop search,
213  // because all pins having the same number are consecutive in list.
214  if( m_sortedSymbolPinList[idxref].num != m_sortedSymbolPinList[jj].num )
215  break;
216 
217  m_sortedSymbolPinList[jj].num.clear();
218  }
219  }
220 }
std::vector< PIN_INFO > m_sortedSymbolPinList
Used to temporarily store and filter the list of pins of a schematic symbol when generating schematic...

References NETLIST_EXPORTER_BASE::m_sortedSymbolPinList.

Referenced by NETLIST_EXPORTER_BASE::CreatePinList().

◆ findAllUnitsOfSymbol()

void NETLIST_EXPORTER_BASE::findAllUnitsOfSymbol ( SCH_SYMBOL aSchSymbol,
LIB_SYMBOL aLibSymbol,
SCH_SHEET_PATH aSheetPath,
bool  aKeepUnconnectedPins 
)
protectedinherited

Find all units for symbols with multiple symbols per package.

Search the entire design for all units of aSymbol based on matching reference designator, and for each unit, add all its pins to the temporary sorted pin list, m_sortedSymbolPinList. if aKeepUnconnectedPins = false, unconnected pins will be removed from list but usually we need all pins in netlists.

Definition at line 223 of file netlist_exporter_base.cpp.

226 {
227  wxString ref = aSchSymbol->GetRef( aSheetPath );
228  wxString ref2;
229 
230  SCH_SHEET_LIST sheetList = m_schematic->GetSheets();
232 
233  for( unsigned i = 0; i < sheetList.size(); i++ )
234  {
235  SCH_SHEET_PATH& sheet = sheetList[i];
236 
237  for( SCH_ITEM* item : sheetList[i].LastScreen()->Items().OfType( SCH_SYMBOL_T ) )
238  {
239  SCH_SYMBOL* comp2 = static_cast<SCH_SYMBOL*>( item );
240 
241  ref2 = comp2->GetRef( &sheet );
242 
243  if( ref2.CmpNoCase( ref ) != 0 )
244  continue;
245 
246  for( const SCH_PIN* pin : comp2->GetPins( &sheet ) )
247  {
248  if( SCH_CONNECTION* conn = pin->Connection( &sheet ) )
249  {
250  const wxString& netName = conn->Name();
251 
252  if( !aKeepUnconnectedPins ) // Skip unconnected pins if requested
253  {
254  CONNECTION_SUBGRAPH* sg = graph->FindSubgraphByName( netName, sheet );
255 
256  if( !sg || sg->m_no_connect || sg->m_items.size() < 2 )
257  continue;
258  }
259 
260  m_sortedSymbolPinList.emplace_back( pin->GetShownNumber(), netName );
261  }
262  }
263  }
264  }
265 }
CONNECTION_SUBGRAPH * FindSubgraphByName(const wxString &aNetName, const SCH_SHEET_PATH &aPath)
Returns the subgraph for a given net name on a given sheet.
A container for handling SCH_SHEET_PATH objects in a flattened hierarchy.
std::vector< PIN_INFO > m_sortedSymbolPinList
Used to temporarily store and filter the list of pins of a schematic symbol when generating schematic...
Calculates the connectivity of a schematic and generates netlists.
virtual CONNECTION_GRAPH * ConnectionGraph() const =0
const wxString GetRef(const SCH_SHEET_PATH *aSheet, bool aIncludeUnit=false) const
Return the reference for the given sheet path.
Definition: sch_symbol.cpp:464
A subgraph is a set of items that are electrically connected on a single sheet.
SCHEMATIC_IFACE * m_schematic
The schematic we're generating a netlist for.
std::vector< SCH_ITEM * > m_items
Handle access to a stack of flattened SCH_SHEET objects by way of a path for creating a flattened sch...
Schematic symbol object.
Definition: sch_symbol.h:78
Each graphical item can have a SCH_CONNECTION describing its logical connection (to a bus or net).
SCH_ITEM * m_no_connect
No-connect item in graph, if any.
virtual SCH_SHEET_LIST GetSheets() const =0
Base class for any item which can be embedded within the SCHEMATIC container class,...
Definition: sch_item.h:182
std::vector< SCH_PIN * > GetPins(const SCH_SHEET_PATH *aSheet=nullptr) const
Retrieve a list of the SCH_PINs for the given sheet path.
Definition: sch_symbol.cpp:896

References SCHEMATIC_IFACE::ConnectionGraph(), CONNECTION_GRAPH::FindSubgraphByName(), SCH_SYMBOL::GetPins(), SCH_SYMBOL::GetRef(), SCHEMATIC_IFACE::GetSheets(), CONNECTION_SUBGRAPH::m_items, CONNECTION_SUBGRAPH::m_no_connect, NETLIST_EXPORTER_BASE::m_schematic, NETLIST_EXPORTER_BASE::m_sortedSymbolPinList, pin, and SCH_SYMBOL_T.

Referenced by NETLIST_EXPORTER_BASE::CreatePinList().

◆ findNextSymbol()

SCH_SYMBOL * NETLIST_EXPORTER_BASE::findNextSymbol ( EDA_ITEM aItem,
SCH_SHEET_PATH aSheetPath 
)
protectedinherited

Check if the given symbol should be processed for netlisting.

Prevent processing multi-unit symbols more than once, etc.

Parameters
aItemis a symbol to check
aSheetPathis the sheet to check the symbol for
Returns
the symbol if it should be processed, or nullptr

Definition at line 73 of file netlist_exporter_base.cpp.

74 {
75  wxCHECK( aItem, nullptr );
76  wxCHECK( aSheetPath, nullptr );
77 
78  wxString ref;
79 
80  if( aItem->Type() != SCH_SYMBOL_T )
81  return nullptr;
82 
83  // found next symbol
84  SCH_SYMBOL* symbol = (SCH_SYMBOL*) aItem;
85 
86  // Power symbols and other symbols which have the reference starting with "#" are not
87  // included in netlist (pseudo or virtual symbols)
88  ref = symbol->GetRef( aSheetPath );
89 
90  if( ref[0] == wxChar( '#' ) )
91  return nullptr;
92 
93  SCH_SCREEN* screen = aSheetPath->LastScreen();
94 
95  wxCHECK( screen, nullptr );
96 
97  LIB_SYMBOL* libSymbol = screen->GetLibSymbols()[ symbol->GetSchSymbolLibraryName() ];
98 
99  wxCHECK( libSymbol, nullptr );
100 
101  // If symbol is a "multi parts per package" type
102  if( libSymbol->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 symbol entry.
110  m_libParts.insert( libSymbol ); // rejects non-unique pointers
111 
112  return symbol;
113 }
bool Lookup(const wxString &aString)
const wxString GetRef(const SCH_SHEET_PATH *aSheet, bool aIncludeUnit=false) const
Return the reference for the given sheet path.
Definition: sch_symbol.cpp:464
Define a library symbol object.
Definition: lib_symbol.h:96
UNIQUE_STRINGS m_referencesAlreadyFound
Used for "multiple symbols per package" symbols to avoid processing a lib symbol more than once.
wxString GetSchSymbolLibraryName() const
Definition: sch_symbol.cpp:271
int GetUnitCount() const override
For items with units, return the number of units.
std::map< wxString, LIB_SYMBOL * > & GetLibSymbols()
Fetch a list of unique LIB_SYMBOL object pointers required to properly render each SCH_SYMBOL in this...
Definition: sch_screen.h:451
Schematic symbol object.
Definition: sch_symbol.h:78
SCH_SCREEN * LastScreen()
std::set< LIB_SYMBOL *, LIB_SYMBOL_LESS_THAN > m_libParts
unique library symbols used. LIB_SYMBOL items are sorted by names
KICAD_T Type() const
Returns the type of object.
Definition: eda_item.h:112

References SCH_SCREEN::GetLibSymbols(), SCH_SYMBOL::GetRef(), SCH_SYMBOL::GetSchSymbolLibraryName(), LIB_SYMBOL::GetUnitCount(), SCH_SHEET_PATH::LastScreen(), UNIQUE_STRINGS::Lookup(), NETLIST_EXPORTER_BASE::m_libParts, NETLIST_EXPORTER_BASE::m_referencesAlreadyFound, SCH_SYMBOL_T, and EDA_ITEM::Type().

Referenced by NETLIST_EXPORTER_XML::makeSymbols(), ProcessNetlist(), NETLIST_EXPORTER_ORCADPCB2::WriteNetlist(), and NETLIST_EXPORTER_CADSTAR::WriteNetlist().

◆ Format()

bool NETLIST_EXPORTER_PSPICE::Format ( OUTPUTFORMATTER aFormatter,
unsigned  aCtl 
)

Definition at line 95 of file netlist_exporter_pspice.cpp.

96 {
97  // Netlist options
98  const bool useNetcodeAsNetName = false;//aCtl & NET_USE_NETCODES_AS_NETNAMES;
99 
100  // default title
101  m_title = "KiCad schematic";
102 
103  if( !ProcessNetlist( aCtl ) )
104  return false;
105 
106  aFormatter->Print( 0, ".title %s\n", TO_UTF8( m_title ) );
107 
108  // Write .include directives
109  for( const wxString& curr_lib : m_libraries )
110  {
111  // First, expand env vars, if any
112  wxString libname = ExpandEnvVarSubstitutions( curr_lib, &m_schematic->Prj() );
113  wxString full_path;
114 
115  if( ( aCtl & NET_ADJUST_INCLUDE_PATHS ) )
116  {
117  // Look for the library in known search locations
118  full_path = ResolveFile( libname, &Pgm().GetLocalEnvVariables(), &m_schematic->Prj() );
119 
120  if( full_path.IsEmpty() )
121  {
122  DisplayError( nullptr, wxString::Format( _( "Could not find library file %s." ),
123  libname ) );
124  full_path = libname;
125  }
126  }
127  else
128  {
129  full_path = libname; // just use the unaltered path
130  }
131 
132  aFormatter->Print( 0, ".include \"%s\"\n", TO_UTF8( full_path ) );
133  }
134 
135  unsigned int NC_counter = 1;
136 
137  for( const SPICE_ITEM& item : m_spiceItems )
138  {
139  if( !item.m_enabled )
140  continue;
141 
142  wxString device = GetSpiceDevice( item.m_refName );
143  aFormatter->Print( 0, "%s ", TO_UTF8( device ) );
144 
145  size_t pspiceNodes =
146  item.m_pinSequence.empty() ? item.m_pins.size() : item.m_pinSequence.size();
147 
148  for( size_t ii = 0; ii < pspiceNodes; ii++ )
149  {
150  // Use the custom order if defined, otherwise use the standard pin order as defined
151  // in the symbol.
152  size_t activePinIndex = item.m_pinSequence.empty() ? ii : item.m_pinSequence[ii];
153 
154  // Valid used Node Indexes are in the set
155  // {0,1,2,...m_item.m_pin.size()-1}
156  if( activePinIndex >= item.m_pins.size() )
157  {
158  wxASSERT_MSG( false, "Used an invalid pin number in node sequence" );
159  continue;
160  }
161 
162  wxString netName = item.m_pins[activePinIndex];
163 
164  wxASSERT( m_netMap.count( netName ) );
165 
166  if( useNetcodeAsNetName )
167  {
168  aFormatter->Print( 0, "%d ", m_netMap[netName] );
169  }
170  else
171  {
172  // Replace parenthesis with underscore to prevent parse issues with simulators
173  ReplaceForbiddenChars( netName );
174 
175  // unescape net names that contain a escaped sequence ("{slash}"):
176  netName = UnescapeString( netName );
177 
178  // Borrow LTSpice's nomenclature for unconnected nets
179  if( netName.IsEmpty() )
180  netName = wxString::Format( wxT( "NC_%.2u" ), NC_counter++ );
181 
182  aFormatter->Print( 0, "%s ", TO_UTF8( netName ) );
183  }
184  }
185 
186  aFormatter->Print( 0, "%s\n", TO_UTF8( item.m_model ) );
187  }
188 
189  // Print out all directives found in the text fields on the schematics
190  writeDirectives( aFormatter, aCtl );
191 
192  aFormatter->Print( 0, ".end\n" );
193 
194  return true;
195 }
void DisplayError(wxWindow *aParent, const wxString &aText, int aDisplayTime)
Display an error or warning message box with aMessage.
Definition: confirm.cpp:279
wxString ResolveFile(const wxString &aFileName, const ENV_VAR_MAP *aEnvVars, const PROJECT *aProject)
Search the default paths trying to find one with the requested file.
Definition: env_paths.cpp:162
std::set< wxString > m_libraries
Spice libraries used by the simulated circuit.
std::list< SPICE_ITEM > m_spiceItems
Items representing schematic symbols in Spice world.
Structure to represent a schematic symbol in the Spice simulation.
const wxString ExpandEnvVarSubstitutions(const wxString &aString, PROJECT *aProject)
Replace any environment variable & text variable references with their values.
Definition: common.cpp:267
KIWAY Kiway & Pgm(), KFCTL_STANDALONE
The global Program "get" accessor.
Definition: single_top.cpp:106
SCHEMATIC_IFACE * m_schematic
The schematic we're generating a netlist for.
virtual PROJECT & Prj() const =0
#define TO_UTF8(wxstring)
Convert a wxString to a UTF8 encoded C string for all wxWidgets build modes.
Definition: macros.h:96
wxString m_title
Spice simulation title found in the schematic sheet.
std::map< wxString, int > m_netMap
Map spice nodes to net codes.
virtual void writeDirectives(OUTPUTFORMATTER *aFormatter, unsigned aCtl) const
Save the Spice directives.
wxString GetSpiceDevice(const wxString &aSymbol) const
Return name of Spice device corresponding to a schematic symbol.
bool ProcessNetlist(unsigned aCtl)
Process the netlist to create net mapping and a list of SPICE_ITEMs.
#define _(s)
static void ReplaceForbiddenChars(wxString &aNetName)
Replace illegal spice net name characters with an underscore.
wxString UnescapeString(const wxString &aSource)
void Format(OUTPUTFORMATTER *out, int aNestLevel, int aCtl, const CPTREE &aTree)
Output a PTREE into s-expression format via an OUTPUTFORMATTER derivative.
Definition: ptree.cpp:200
int PRINTF_FUNC Print(int nestLevel, const char *fmt,...)
Format and write text to the output stream.
Definition: richio.cpp:426

References _, DisplayError(), ExpandEnvVarSubstitutions(), Format(), GetSpiceDevice(), m_libraries, m_netMap, NETLIST_EXPORTER_BASE::m_schematic, m_spiceItems, m_title, NET_ADJUST_INCLUDE_PATHS, Pgm(), OUTPUTFORMATTER::Print(), SCHEMATIC_IFACE::Prj(), ProcessNetlist(), ReplaceForbiddenChars(), ResolveFile(), TO_UTF8, UnescapeString(), and writeDirectives().

Referenced by WriteNetlist().

◆ GetDirectives()

const std::vector<wxString> NETLIST_EXPORTER_PSPICE::GetDirectives ( ) const
inline

Return a vector of Spice directives found in the schematics.

Definition at line 185 of file netlist_exporter_pspice.h.

186  {
187  return m_directives;
188  }
std::vector< wxString > m_directives
Spice directives found in the schematic sheet.

References m_directives.

Referenced by NETLIST_EXPORTER_PSPICE_SIM::GetSheetSimCommand(), and NETLIST_EXPORTER_PSPICE_SIM::writeDirectives().

◆ GetNetIndexMap()

const std::map<wxString, int>& NETLIST_EXPORTER_PSPICE::GetNetIndexMap ( ) const
inline

Return a map of circuit nodes to net names.

Definition at line 146 of file netlist_exporter_pspice.h.

147  {
148  return m_netMap;
149  }
std::map< wxString, int > m_netMap
Map spice nodes to net codes.

References m_netMap.

Referenced by DIALOG_SIM_SETTINGS::ShowModal(), DIALOG_SIM_SETTINGS::TransferDataFromWindow(), DIALOG_SIGNAL_LIST::TransferDataToWindow(), and NETLIST_EXPORTER_PSPICE_SIM::writeDirectives().

◆ GetSpiceDevice()

wxString NETLIST_EXPORTER_PSPICE::GetSpiceDevice ( const wxString &  aSymbol) const

Return name of Spice device corresponding to a schematic symbol.

Parameters
aSymbolis the component reference.
Returns
Spice device name or empty string if there is no such symbol in the netlist. The name is either plain reference if the first character of reference corresponds to the assigned device model type or it is the reference prefixed with a character defining the device model type.

Definition at line 45 of file netlist_exporter_pspice.cpp.

46 {
47  const std::list<SPICE_ITEM>& spiceItems = GetSpiceItems();
48 
49  auto it = std::find_if( spiceItems.begin(), spiceItems.end(),
50  [&]( const SPICE_ITEM& item )
51  {
52  return item.m_refName == aSymbol;
53  } );
54 
55  if( it == spiceItems.end() )
56  return wxEmptyString;
57 
58  // Prefix the device type if plain reference would result in a different device type
59  return it->m_primitive != it->m_refName[0] ? wxString( it->m_primitive + it->m_refName )
60  : it->m_refName;
61 }
Structure to represent a schematic symbol in the Spice simulation.
const std::list< SPICE_ITEM > & GetSpiceItems() const
Return list of items representing schematic components in the Spice world.

References GetSpiceItems().

Referenced by NETLIST_EXPORTER_PSPICE_SIM::ComponentToVector(), DIALOG_SIM_SETTINGS::evaluateDCControls(), Format(), DIALOG_SIM_SETTINGS::TransferDataFromWindow(), and TUNER_SLIDER::TUNER_SLIDER().

◆ GetSpiceField()

wxString NETLIST_EXPORTER_PSPICE::GetSpiceField ( SPICE_FIELD  aField,
SCH_SYMBOL aSymbol,
unsigned  aCtl 
)
static

Retrieve either the requested field value or the default value.

Definition at line 198 of file netlist_exporter_pspice.cpp.

200 {
201  SCH_FIELD* field = aSymbol->FindField( GetSpiceFieldName( aField ) );
202  return field ? field->GetShownText() : GetSpiceFieldDefVal( aField, aSymbol, aCtl );
203 }
Instances are attached to a symbol or sheet and provide a place for the symbol's value,...
Definition: sch_field.h:49
static const wxString & GetSpiceFieldName(SPICE_FIELD aField)
Return a string used for a particular component field related to Spice simulation.
wxString GetShownText(int aDepth=0) const override
Return the string actually shown after processing of the base text.
Definition: sch_field.cpp:105
static wxString GetSpiceFieldDefVal(SPICE_FIELD aField, SCH_SYMBOL *aSymbol, unsigned aCtl)
Retrieve the default value for a given field.
SCH_FIELD * FindField(const wxString &aFieldName, bool aIncludeDefaultFields=true)
Search for a SCH_FIELD with aFieldName.
Definition: sch_symbol.cpp:773

References SCH_SYMBOL::FindField(), SCH_FIELD::GetShownText(), GetSpiceFieldDefVal(), and GetSpiceFieldName().

Referenced by SIM_PLOT_FRAME::AddTuner(), and ProcessNetlist().

◆ GetSpiceFieldDefVal()

wxString NETLIST_EXPORTER_PSPICE::GetSpiceFieldDefVal ( SPICE_FIELD  aField,
SCH_SYMBOL aSymbol,
unsigned  aCtl 
)
static

Retrieve the default value for a given field.

Definition at line 206 of file netlist_exporter_pspice.cpp.

208 {
209  switch( aField )
210  {
211  case SF_PRIMITIVE:
212  {
213  const wxString refName = aSymbol->GetField( REFERENCE_FIELD )->GetShownText();
214  return refName.GetChar( 0 );
215  }
216 
217  case SF_MODEL:
218  {
219  wxChar prim = aSymbol->GetField( REFERENCE_FIELD )->GetShownText().GetChar( 0 );
220  wxString value = aSymbol->GetField( VALUE_FIELD )->GetShownText();
221 
222  // Is it a passive component?
223  if( ( aCtl & NET_ADJUST_PASSIVE_VALS ) && ( prim == 'C' || prim == 'L' || prim == 'R' ) )
224  {
225  // Regular expression to match common formats used for passive parts description
226  // (e.g. 100k, 2k3, 1 uF)
227  wxRegEx passiveVal(
228  wxT( "^([0-9\\. ]+)([fFpPnNuUmMkKgGtTμµ𝛍𝜇𝝁 ]|M(e|E)(g|G))?([fFhHΩΩ𝛀𝛺𝝮]|ohm)?([-1-9 ]*)$" ) );
229 
230  if( passiveVal.Matches( value ) )
231  {
232  wxString prefix( passiveVal.GetMatch( value, 1 ) );
233  wxString unit( passiveVal.GetMatch( value, 2 ) );
234  wxString suffix( passiveVal.GetMatch( value, 6 ) );
235 
236  prefix.Trim(); prefix.Trim( false );
237  unit.Trim(); unit.Trim( false );
238  suffix.Trim(); suffix.Trim( false );
239 
240  // Make 'mega' units comply with the Spice expectations
241  if( unit == "M" )
242  unit = "Meg";
243 
244  value = prefix + unit + suffix;
245  }
246  }
247 
248  return value;
249  }
250 
251  case SF_ENABLED:
252  return wxString( "Y" );
253 
254  case SF_NODE_SEQUENCE:
255  {
256  wxString nodeSeq;
257  std::vector<LIB_PIN*> pins;
258 
259  wxCHECK( aSymbol->GetLibSymbolRef(), wxString() );
260  aSymbol->GetLibSymbolRef()->GetPins( pins );
261 
262  for( LIB_PIN* pin : pins )
263  nodeSeq += pin->GetNumber() + " ";
264 
265  nodeSeq.Trim();
266 
267  return nodeSeq;
268  }
269 
270  case SF_LIB_FILE:
271  // There is no default Spice library
272  return wxEmptyString;
273 
274  default:
275  wxASSERT_MSG( false, "Missing default value definition for a Spice field." );
276  return wxString( "<unknown>" );
277  }
278 }
Field Reference of part, i.e. "IC21".
SCH_FIELD * GetField(MANDATORY_FIELD_T aFieldType)
Return a mandatory field in this symbol.
Definition: sch_symbol.cpp:705
wxString GetShownText(int aDepth=0) const override
Return the string actually shown after processing of the base text.
Definition: sch_field.cpp:105
std::unique_ptr< LIB_SYMBOL > & GetLibSymbolRef()
Definition: sch_symbol.h:165
Field Value of part, i.e. "3.3K".

References SCH_SYMBOL::GetField(), SCH_SYMBOL::GetLibSymbolRef(), SCH_FIELD::GetShownText(), NET_ADJUST_PASSIVE_VALS, pin, REFERENCE_FIELD, SF_ENABLED, SF_LIB_FILE, SF_MODEL, SF_NODE_SEQUENCE, SF_PRIMITIVE, and VALUE_FIELD.

Referenced by GetSpiceField(), and DIALOG_SPICE_MODEL::TransferDataToWindow().

◆ GetSpiceFieldName()

static const wxString& NETLIST_EXPORTER_PSPICE::GetSpiceFieldName ( SPICE_FIELD  aField)
inlinestatic

Return a string used for a particular component field related to Spice simulation.

Definition at line 162 of file netlist_exporter_pspice.h.

163  {
164  return m_spiceFields[(int) aField];
165  }
static const std::vector< wxString > m_spiceFields

References m_spiceFields.

Referenced by DIALOG_SPICE_MODEL::getLibField(), DIALOG_SPICE_MODEL::getSchField(), GetSpiceField(), ProcessNetlist(), DIALOG_SPICE_MODEL::TransferDataFromWindow(), and TUNER_SLIDER::TUNER_SLIDER().

◆ GetSpiceFields()

static const std::vector<wxString>& NETLIST_EXPORTER_PSPICE::GetSpiceFields ( )
inlinestatic

Return a vector of component field names related to Spice simulation.

Definition at line 154 of file netlist_exporter_pspice.h.

155  {
156  return m_spiceFields;
157  }
static const std::vector< wxString > m_spiceFields

References m_spiceFields.

Referenced by DIALOG_SPICE_MODEL::TransferDataToWindow().

◆ GetSpiceItems()

const std::list<SPICE_ITEM>& NETLIST_EXPORTER_PSPICE::GetSpiceItems ( ) const
inline

Return list of items representing schematic components in the Spice world.

Definition at line 105 of file netlist_exporter_pspice.h.

106  {
107  return m_spiceItems;
108  }
std::list< SPICE_ITEM > m_spiceItems
Items representing schematic symbols in Spice world.

References m_spiceItems.

Referenced by GetSpiceDevice(), DIALOG_SIGNAL_LIST::TransferDataToWindow(), DIALOG_SIM_SETTINGS::updateDCSources(), and NETLIST_EXPORTER_PSPICE_SIM::writeDirectives().

◆ MakeCommandLine()

wxString NETLIST_EXPORTER_BASE::MakeCommandLine ( const wxString &  aFormatString,
const wxString &  aNetlistFile,
const wxString &  aFinalFile,
const wxString &  aProjectDirectory 
)
staticinherited

Build up a string that describes a command line for executing a child process.

The input and output file names along with any options to the executable are all possibly in the returned string.

Parameters
aFormatStringholds:
  • the name of the external program
  • any options needed by that program
  • formatting sequences, see below.
aNetlistFileis the name of the input file for the external program, that is a intermediate netlist file in xml format.
aFinalFileis the name of the output file that the user expects.
aProjectDirectoryis used for P replacement, it should omit the trailing '/'.

Supported formatting sequences and their meaning:

  • B => base filename of selected output file, minus path and extension.
  • I => complete filename and path of the temporary input file.
  • O => complete filename and path of the user chosen output file.
  • P => project directory, without name and without trailing '/'

Definition at line 37 of file netlist_exporter_base.cpp.

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( wxT( "%P" ), aProjectPath, true );
54  ret.Replace( wxT( "%B" ), out.GetName(), true );
55  ret.Replace( wxT( "%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( wxT( "\\\\" ) ) )
64  str_out.Replace( wxT( "\\" ), wxT( "/" ) );
65 #endif
66 
67  ret.Replace( wxT( "%O" ), str_out, true );
68 
69  return ret;
70 }

Referenced by SCH_EDIT_FRAME::WriteNetListFile().

◆ ProcessNetlist()

bool NETLIST_EXPORTER_PSPICE::ProcessNetlist ( unsigned  aCtl)

Process the netlist to create net mapping and a list of SPICE_ITEMs.

It is automatically called by WriteNetlist(), but might be used separately, if only net mapping and the list of SPICE_ITEMs are required.

Returns
True if successful.

Definition at line 281 of file netlist_exporter_pspice.cpp.

282 {
283  const wxString delimiters( "{:,; }" );
284  SCH_SHEET_LIST sheetList = m_schematic->GetSheets();
285  std::set<wxString> refNames; // Set of reference names, to check for duplication
286 
287  m_netMap.clear();
288  m_netMap["GND"] = 0; // 0 is reserved for "GND"
289  int netIdx = 1;
290 
291  m_libraries.clear();
293  m_libParts.clear();
294 
295  UpdateDirectives( aCtl );
296 
297  for( unsigned sheet_idx = 0; sheet_idx < sheetList.size(); sheet_idx++ )
298  {
299  SCH_SHEET_PATH sheet = sheetList[sheet_idx];
300 
301  // Process symbol attributes to find Spice directives
302  for( SCH_ITEM* item : sheet.LastScreen()->Items().OfType( SCH_SYMBOL_T ) )
303  {
304  SCH_SYMBOL* symbol = findNextSymbol( item, &sheet );
305 
306  if( !symbol )
307  continue;
308 
309  CreatePinList( symbol, &sheet, true );
310  SPICE_ITEM spiceItem;
311  spiceItem.m_parent = symbol;
312 
313  // Obtain Spice fields
314  SCH_FIELD* fieldLibFile = symbol->FindField( GetSpiceFieldName( SF_LIB_FILE ) );
315  SCH_FIELD* fieldSeq = symbol->FindField( GetSpiceFieldName( SF_NODE_SEQUENCE ) );
316 
317  spiceItem.m_primitive = GetSpiceField( SF_PRIMITIVE, symbol, aCtl )[0];
318  spiceItem.m_model = GetSpiceField( SF_MODEL, symbol, aCtl );
319  spiceItem.m_refName = symbol->GetRef( &sheet );
320 
321  // Duplicate references will result in simulation errors
322  if( refNames.count( spiceItem.m_refName ) )
323  {
324  DisplayError( nullptr, _( "Multiple symbols have the same reference designator.\n"
325  "Annotation must be corrected before simulating." ) );
326  return false;
327  }
328 
329  refNames.insert( spiceItem.m_refName );
330 
331  // Check to see if symbol should be removed from Spice netlist
332  spiceItem.m_enabled = StringToBool( GetSpiceField( SF_ENABLED, symbol, aCtl ) );
333 
334  if( fieldLibFile && !fieldLibFile->GetShownText().IsEmpty() )
335  m_libraries.insert( fieldLibFile->GetShownText() );
336 
337  wxArrayString pinNames;
338 
339  // Store pin information
340  for( const PIN_INFO& pin : m_sortedSymbolPinList )
341  {
342  // Create net mapping
343  spiceItem.m_pins.push_back( pin.netName );
344  pinNames.Add( pin.num );
345 
346  if( m_netMap.count( pin.netName ) == 0 )
347  m_netMap[pin.netName] = netIdx++;
348  }
349 
350  // Check if an alternative pin sequence is available:
351  if( fieldSeq )
352  {
353  // Get the string containing the sequence of nodes:
354  const wxString& nodeSeqIndexLineStr = fieldSeq->GetShownText();
355 
356  // Verify field exists and is not empty:
357  if( !nodeSeqIndexLineStr.IsEmpty() )
358  {
359  // Get Alt Pin Name Array From User:
360  wxStringTokenizer tkz( nodeSeqIndexLineStr, delimiters );
361 
362  while( tkz.HasMoreTokens() )
363  {
364  wxString pinIndex = tkz.GetNextToken();
365  int seq;
366 
367  // Find PinName In Standard List assign Standard List Index to Name:
368  seq = pinNames.Index( pinIndex );
369 
370  if( seq != wxNOT_FOUND )
371  spiceItem.m_pinSequence.push_back( seq );
372  }
373  }
374  }
375 
376  m_spiceItems.push_back( spiceItem );
377  }
378  }
379 
380  return true;
381 }
void DisplayError(wxWindow *aParent, const wxString &aText, int aDisplayTime)
Display an error or warning message box with aMessage.
Definition: confirm.cpp:279
A container for handling SCH_SHEET_PATH objects in a flattened hierarchy.
EE_TYPE OfType(KICAD_T aType) const
Definition: sch_rtree.h:230
Instances are attached to a symbol or sheet and provide a place for the symbol's value,...
Definition: sch_field.h:49
std::set< wxString > m_libraries
Spice libraries used by the simulated circuit.
std::vector< PIN_INFO > m_sortedSymbolPinList
Used to temporarily store and filter the list of pins of a schematic symbol when generating schematic...
std::list< SPICE_ITEM > m_spiceItems
Items representing schematic symbols in Spice world.
Structure to represent a schematic symbol in the Spice simulation.
std::vector< wxString > m_pins
Array containing Standard Pin Name.
static const wxString & GetSpiceFieldName(SPICE_FIELD aField)
Return a string used for a particular component field related to Spice simulation.
bool m_enabled
Whether the symbol should be used in simulation.
void Clear()
Erase the record.
const wxString GetRef(const SCH_SHEET_PATH *aSheet, bool aIncludeUnit=false) const
Return the reference for the given sheet path.
Definition: sch_symbol.cpp:464
static bool StringToBool(const wxString &aStr)
Convert typical boolean string values (no/yes, true/false, 1/0) to a boolean value.
SCH_SYMBOL * m_parent
Schematic symbol represented by this SPICE_ITEM.
wxString m_model
Library model (for semiconductors and subcircuits), component value (for passive components) or volta...
wxString GetShownText(int aDepth=0) const override
Return the string actually shown after processing of the base text.
Definition: sch_field.cpp:105
SCH_SYMBOL * findNextSymbol(EDA_ITEM *aItem, SCH_SHEET_PATH *aSheetPath)
Check if the given symbol should be processed for netlisting.
SCHEMATIC_IFACE * m_schematic
The schematic we're generating a netlist for.
UNIQUE_STRINGS m_referencesAlreadyFound
Used for "multiple symbols per package" symbols to avoid processing a lib symbol more than once.
std::map< wxString, int > m_netMap
Map spice nodes to net codes.
void CreatePinList(SCH_SYMBOL *aSymbol, SCH_SHEET_PATH *aSheetPath, bool aKeepUnconnectedPins)
Find a symbol from the DrawList and builds its pin list in m_sortedSymbolPinList.
void UpdateDirectives(unsigned aCtl)
Update the vector of Spice directives placed in the schematics.
#define _(s)
Handle access to a stack of flattened SCH_SHEET objects by way of a path for creating a flattened sch...
SCH_FIELD * FindField(const wxString &aFieldName, bool aIncludeDefaultFields=true)
Search for a SCH_FIELD with aFieldName.
Definition: sch_symbol.cpp:773
static wxString GetSpiceField(SPICE_FIELD aField, SCH_SYMBOL *aSymbol, unsigned aCtl)
Retrieve either the requested field value or the default value.
Schematic symbol object.
Definition: sch_symbol.h:78
SCH_SCREEN * LastScreen()
EE_RTREE & Items()
Gets the full RTree, usually for iterating.
Definition: sch_screen.h:110
wxChar m_primitive
Spice primitive type (.
std::vector< int > m_pinSequence
Numeric indices into m_SortedSymbolPinList.
std::set< LIB_SYMBOL *, LIB_SYMBOL_LESS_THAN > m_libParts
unique library symbols used. LIB_SYMBOL items are sorted by names
virtual SCH_SHEET_LIST GetSheets() const =0
Base class for any item which can be embedded within the SCHEMATIC container class,...
Definition: sch_item.h:182

References _, UNIQUE_STRINGS::Clear(), NETLIST_EXPORTER_BASE::CreatePinList(), DisplayError(), SCH_SYMBOL::FindField(), NETLIST_EXPORTER_BASE::findNextSymbol(), SCH_SYMBOL::GetRef(), SCHEMATIC_IFACE::GetSheets(), SCH_FIELD::GetShownText(), GetSpiceField(), GetSpiceFieldName(), SCH_SCREEN::Items(), SCH_SHEET_PATH::LastScreen(), SPICE_ITEM::m_enabled, NETLIST_EXPORTER_BASE::m_libParts, m_libraries, SPICE_ITEM::m_model, m_netMap, SPICE_ITEM::m_parent, SPICE_ITEM::m_pins, SPICE_ITEM::m_pinSequence, SPICE_ITEM::m_primitive, NETLIST_EXPORTER_BASE::m_referencesAlreadyFound, SPICE_ITEM::m_refName, NETLIST_EXPORTER_BASE::m_schematic, NETLIST_EXPORTER_BASE::m_sortedSymbolPinList, m_spiceItems, EE_RTREE::OfType(), pin, SCH_SYMBOL_T, SF_ENABLED, SF_LIB_FILE, SF_MODEL, SF_NODE_SEQUENCE, SF_PRIMITIVE, StringToBool(), and UpdateDirectives().

Referenced by Format().

◆ ReplaceForbiddenChars()

void NETLIST_EXPORTER_PSPICE::ReplaceForbiddenChars ( wxString &  aNetName)
static

Replace illegal spice net name characters with an underscore.

Definition at line 83 of file netlist_exporter_pspice.cpp.

84 {
85  // some chars are not accepted in netnames in spice netlists, because they are separators
86  // they are replaced an underscore or some other allowed char.
87  // Note: this is a static function
88 
89  aNetName.Replace( "(", "_" );
90  aNetName.Replace( ")", "_" );
91  aNetName.Replace( " ", "_" );
92 }

Referenced by NETLIST_EXPORTER_PSPICE_SIM::ComponentToVector(), and Format().

◆ StringToBool()

static bool NETLIST_EXPORTER_PSPICE::StringToBool ( const wxString &  aStr)
inlinestatic

Convert typical boolean string values (no/yes, true/false, 1/0) to a boolean value.

Definition at line 193 of file netlist_exporter_pspice.h.

194  {
195  if( aStr.IsEmpty() )
196  return false;
197 
198  char c = tolower( aStr[0] );
199 
200  // Different ways of saying false (no/false/0)
201  return !( c == 'n' || c == 'f' || c == '0' );
202  }

Referenced by ProcessNetlist(), and DIALOG_SPICE_MODEL::TransferDataToWindow().

◆ UpdateDirectives()

void NETLIST_EXPORTER_PSPICE::UpdateDirectives ( unsigned  aCtl)

Update the vector of Spice directives placed in the schematics.

Definition at line 384 of file netlist_exporter_pspice.cpp.

385 {
386  const SCH_SHEET_LIST& sheetList = m_schematic->GetSheets();
387  wxRegEx couplingK( "^[kK][[:digit:]]*[[:space:]]+[[:alnum:]]+[[:space:]]+[[:alnum:]]+",
388  wxRE_ADVANCED );
389 
390  m_directives.clear();
391  bool controlBlock = false;
392  bool circuitBlock = false;
393 
394  for( unsigned i = 0; i < sheetList.size(); i++ )
395  {
396  for( SCH_ITEM* item : sheetList[i].LastScreen()->Items().OfType( SCH_TEXT_T ) )
397  {
398  wxString text = static_cast<SCH_TEXT*>( item )->GetShownText();
399 
400  if( text.IsEmpty() )
401  continue;
402 
403  // Analyze each line of a text field
404  wxStringTokenizer tokenizer( text, "\r\n" );
405 
406  // Flag to follow multiline directives
407  bool directiveStarted = false;
408 
409  while( tokenizer.HasMoreTokens() )
410  {
411  wxString line( tokenizer.GetNextToken() );
412 
413  // Cleanup: remove preceding and trailing white-space characters
414  line.Trim( true ).Trim( false );
415  // Convert to lower-case for parsing purposes only
416  wxString lowercaseline = line;
417  lowercaseline.MakeLower();
418 
419  // 'Include' directive stores the library file name, so it
420  // can be later resolved using a list of paths
421  if( lowercaseline.StartsWith( ".inc" ) )
422  {
423  wxString lib = line.AfterFirst( ' ' );
424 
425  if( lib.IsEmpty() )
426  continue;
427 
428  // Strip quotes if present
429  if( ( lib.StartsWith( "\"" ) && lib.EndsWith( "\"" ) )
430  || ( lib.StartsWith( "'" ) && lib.EndsWith( "'" ) ) )
431  {
432  lib = lib.Mid( 1, lib.Length() - 2 );
433  }
434 
435  m_libraries.insert( lib );
436  }
437 
438  // Store the title to be sure it appears
439  // in the first line of output
440  else if( lowercaseline.StartsWith( ".title " ) )
441  {
442  m_title = line.AfterFirst( ' ' );
443  }
444 
445  else if( line.StartsWith( '.' ) // one-line directives
446  || controlBlock // .control .. .endc block
447  || circuitBlock // .subckt .. .ends block
448  || couplingK.Matches( line ) // K## L## L## coupling constant
449  || ( directiveStarted && line.StartsWith( '+' ) ) ) // multiline directives
450  {
451  m_directives.push_back( line );
452  }
453 
454  // Handle .control .. .endc blocks
455  if( lowercaseline.IsSameAs( ".control" ) && ( !controlBlock ) )
456  controlBlock = true;
457 
458  if( lowercaseline.IsSameAs( ".endc" ) && controlBlock )
459  controlBlock = false;
460 
461  // Handle .subckt .. .ends blocks
462  if( lowercaseline.StartsWith( ".subckt" ) && ( !circuitBlock ) )
463  circuitBlock = true;
464 
465  if( lowercaseline.IsSameAs( ".ends" ) && circuitBlock )
466  circuitBlock = false;
467 
468  // Mark directive as started or continued in case it is a multi-line one
469  directiveStarted = line.StartsWith( '.' )
470  || ( directiveStarted && line.StartsWith( '+' ) );
471  }
472  }
473  }
474 }
A container for handling SCH_SHEET_PATH objects in a flattened hierarchy.
std::set< wxString > m_libraries
Spice libraries used by the simulated circuit.
std::vector< wxString > m_directives
Spice directives found in the schematic sheet.
SCHEMATIC_IFACE * m_schematic
The schematic we're generating a netlist for.
wxString m_title
Spice simulation title found in the schematic sheet.
virtual SCH_SHEET_LIST GetSheets() const =0
Base class for any item which can be embedded within the SCHEMATIC container class,...
Definition: sch_item.h:182

References SCHEMATIC_IFACE::GetSheets(), m_directives, m_libraries, NETLIST_EXPORTER_BASE::m_schematic, m_title, SCH_TEXT_T, and text.

Referenced by NETLIST_EXPORTER_PSPICE_SIM::GetSheetSimCommand(), and ProcessNetlist().

◆ writeDirectives()

void NETLIST_EXPORTER_PSPICE::writeDirectives ( OUTPUTFORMATTER aFormatter,
unsigned  aCtl 
) const
protectedvirtual

Save the Spice directives.

Reimplemented in NETLIST_EXPORTER_PSPICE_SIM.

Definition at line 477 of file netlist_exporter_pspice.cpp.

478 {
479  for( const wxString& dir : m_directives )
480  aFormatter->Print( 0, "%s\n", TO_UTF8( dir ) );
481 }
std::vector< wxString > m_directives
Spice directives found in the schematic sheet.
#define TO_UTF8(wxstring)
Convert a wxString to a UTF8 encoded C string for all wxWidgets build modes.
Definition: macros.h:96
int PRINTF_FUNC Print(int nestLevel, const char *fmt,...)
Format and write text to the output stream.
Definition: richio.cpp:426

References m_directives, OUTPUTFORMATTER::Print(), and TO_UTF8.

Referenced by Format(), and NETLIST_EXPORTER_PSPICE_SIM::writeDirectives().

◆ WriteNetlist()

bool NETLIST_EXPORTER_PSPICE::WriteNetlist ( const wxString &  aOutFileName,
unsigned  aNetlistOptions 
)
overridevirtual

Write to specified output file.

Output a PTREE into s-expression format via an OUTPUTFORMATTER derivative.

Reimplemented from NETLIST_EXPORTER_BASE.

Definition at line 64 of file netlist_exporter_pspice.cpp.

65 {
66  try
67  {
68  FILE_OUTPUTFORMATTER outputFile( aOutFileName, wxT( "wt" ), '\'' );
69 
70 
71  return Format( &outputFile, aNetlistOptions );
72  }
73  catch( IO_ERROR& )
74  {
75  wxString msg;
76  msg.Printf( _( "Failed to create file '%s'." ), aOutFileName );
77  DisplayError( nullptr, msg );
78  return false;
79  }
80 }
void DisplayError(wxWindow *aParent, const wxString &aText, int aDisplayTime)
Display an error or warning message box with aMessage.
Definition: confirm.cpp:279
bool Format(OUTPUTFORMATTER *aFormatter, unsigned aCtl)
#define _(s)
Used for text file output.
Definition: richio.h:456
Hold an error message and may be used when throwing exceptions containing meaningful error messages.
Definition: ki_exception.h:75

References _, DisplayError(), and Format().

Member Data Documentation

◆ m_directives

std::vector<wxString> NETLIST_EXPORTER_PSPICE::m_directives
private

Spice directives found in the schematic sheet.

Definition at line 213 of file netlist_exporter_pspice.h.

Referenced by GetDirectives(), UpdateDirectives(), and writeDirectives().

◆ m_libParts

std::set<LIB_SYMBOL*, LIB_SYMBOL_LESS_THAN> NETLIST_EXPORTER_BASE::m_libParts
protectedinherited

◆ m_libraries

std::set<wxString> NETLIST_EXPORTER_PSPICE::m_libraries
private

Spice libraries used by the simulated circuit.

Definition at line 214 of file netlist_exporter_pspice.h.

Referenced by Format(), ProcessNetlist(), and UpdateDirectives().

◆ m_netMap

std::map<wxString, int> NETLIST_EXPORTER_PSPICE::m_netMap
private

Map spice nodes to net codes.

Definition at line 215 of file netlist_exporter_pspice.h.

Referenced by Format(), GetNetIndexMap(), and ProcessNetlist().

◆ m_referencesAlreadyFound

UNIQUE_STRINGS NETLIST_EXPORTER_BASE::m_referencesAlreadyFound
protectedinherited

Used for "multiple symbols per package" symbols to avoid processing a lib symbol more than once.

Definition at line 206 of file netlist_exporter_base.h.

Referenced by NETLIST_EXPORTER_BASE::findNextSymbol(), NETLIST_EXPORTER_XML::makeSymbols(), ProcessNetlist(), NETLIST_EXPORTER_ORCADPCB2::WriteNetlist(), and NETLIST_EXPORTER_CADSTAR::WriteNetlist().

◆ m_savedCurrentSheet

SCH_SHEET_PATH NETLIST_EXPORTER_BASE::m_savedCurrentSheet
protectedinherited

The schematic's CurrentSheet when we entered. Restore on exiting.

Definition at line 215 of file netlist_exporter_base.h.

Referenced by NETLIST_EXPORTER_BASE::NETLIST_EXPORTER_BASE(), and NETLIST_EXPORTER_BASE::~NETLIST_EXPORTER_BASE().

◆ m_schematic

◆ m_sortedSymbolPinList

std::vector<PIN_INFO> NETLIST_EXPORTER_BASE::m_sortedSymbolPinList
protectedinherited

Used to temporarily store and filter the list of pins of a schematic symbol when generating schematic symbol data in netlist (comp section).

No ownership of members. TODO(snh): Descope this object

Definition at line 202 of file netlist_exporter_base.h.

Referenced by NETLIST_EXPORTER_BASE::CreatePinList(), NETLIST_EXPORTER_BASE::eraseDuplicatePins(), NETLIST_EXPORTER_BASE::findAllUnitsOfSymbol(), ProcessNetlist(), and NETLIST_EXPORTER_ORCADPCB2::WriteNetlist().

◆ m_spiceFields

const std::vector< wxString > NETLIST_EXPORTER_PSPICE::m_spiceFields
staticprivate
Initial value:
= {
"Spice_Primitive",
"Spice_Model",
"Spice_Netlist_Enabled",
"Spice_Node_Sequence",
"Spice_Lib_File"
}

Definition at line 219 of file netlist_exporter_pspice.h.

Referenced by GetSpiceFieldName(), and GetSpiceFields().

◆ m_spiceItems

std::list<SPICE_ITEM> NETLIST_EXPORTER_PSPICE::m_spiceItems
private

Items representing schematic symbols in Spice world.

Definition at line 216 of file netlist_exporter_pspice.h.

Referenced by Format(), GetSpiceItems(), and ProcessNetlist().

◆ m_title

wxString NETLIST_EXPORTER_PSPICE::m_title
private

Spice simulation title found in the schematic sheet.

Definition at line 212 of file netlist_exporter_pspice.h.

Referenced by Format(), and UpdateDirectives().


The documentation for this class was generated from the following files: