KiCad PCB EDA Suite
NETLIST_EXPORTER_PSPICE Class Reference

NETLIST_EXPORTER_PSPICE generates a PSPICE compatible netlist. More...

#include <netlist_exporter_pspice.h>

Inheritance diagram for NETLIST_EXPORTER_PSPICE:
NETLIST_EXPORTER NETLIST_EXPORTER_PSPICE_SIM

Public Types

typedef std::list< SPICE_ITEMSPICE_ITEM_LIST
 
typedef std::map< wxString, int > NET_INDEX_MAP
 

Net name to circuit node number mapping

More...
 

Public Member Functions

 NETLIST_EXPORTER_PSPICE (NETLIST_OBJECT_LIST *aMasterList, PROJECT *aProject=NULL)
 
virtual ~NETLIST_EXPORTER_PSPICE ()
 
const SPICE_ITEM_LISTGetSpiceItems () const
 Returns list of items representing schematic components in the Spice world. More...
 
wxString GetSpiceDevice (const wxString &aComponent) const
 Returns name of Spice device corresponding to a schematic component. More...
 
bool WriteNetlist (const wxString &aOutFileName, unsigned aNetlistOptions) override
 Function WriteNetlist writes to specified output file. More...
 
bool Format (OUTPUTFORMATTER *aFormatter, unsigned aCtl)
 

More...
 
bool ProcessNetlist (unsigned aCtl)
 Processes the netlist to create net mapping and a list of SPICE_ITEMs. More...
 
const NET_INDEX_MAPGetNetIndexMap () const
 Returns a map of circuit nodes to net names. More...
 
void UpdateDirectives (unsigned aCtl)
 Updates the vector of Spice directives placed in the schematics. More...
 
const std::vector< wxString > GetDirectives () const
 Returnss a vector of Spice directives found in the schematics. More...
 

Static Public Member Functions

static void ReplaceForbiddenChars (wxString &aNetName)
 some chars are not accepted in netnames in spice netlists. More...
 
static const std::vector< wxString > & GetSpiceFields ()
 Returns a vector of component field names related to Spice simulation. More...
 
static const wxString & GetSpiceFieldName (SPICE_FIELD aField)
 Returns a string used for a particular component field related to Spice simulation. More...
 
static wxString GetSpiceField (SPICE_FIELD aField, SCH_COMPONENT *aComponent, unsigned aCtl)
 Retrieves either the requested field value or the default value. More...
 
static wxString GetSpiceFieldDefVal (SPICE_FIELD aField, SCH_COMPONENT *aComponent, unsigned aCtl)
 Retrieves the default value for a given field. More...
 
static bool StringToBool (const wxString &aStr)
 Convertes 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)
 Function MakeCommandLine builds 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
 Saves the Spice directives. More...
 
void CreatePinList (SCH_COMPONENT *aItem, SCH_SHEET_PATH *aSheetPath)
 Function findNextComponentAndCreatePinList finds a component from the DrawList and builds its pin list in m_SortedComponentPinList. More...
 
SCH_COMPONENTfindNextComponent (EDA_ITEM *aItem, SCH_SHEET_PATH *aSheetPath)
 
void eraseDuplicatePins ()
 Function eraseDuplicatePins erase duplicate Pins from m_SortedComponentPinList (i.e. More...
 
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. More...
 
void findAllUnitsOfComponent (SCH_COMPONENT *aComponent, LIB_PART *aEntry, SCH_SHEET_PATH *aSheetPath)
 Function findAllUnitsOfComponent is used for "multiple parts per package" components. More...
 

Static Protected Member Functions

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. More...
 

Protected Attributes

NETLIST_OBJECT_LISTm_masterList
 
NETLIST_OBJECTS m_SortedComponentPinList
 yes ownership, connected items flat list More...
 
UNIQUE_STRINGS m_ReferencesAlreadyFound
 Used for "multi parts per package" components, avoids processing a lib component more than once. More...
 
std::set< LIB_PART *, LIB_PART_LESS_THANm_LibParts
 unique library parts used. LIB_PART items are sorted by names More...
 

Private Attributes

wxString m_title
 

Spice simulation title found in the processed schematic sheet

More...
 
std::vector< wxString > m_directives
 

Spice directives found in the processed schematic sheet

More...
 
std::set< wxString > m_libraries
 

Libraries used by the simulated circuit

More...
 
NET_INDEX_MAP m_netMap
 

Maps circuit nodes to net names

More...
 
SPICE_ITEM_LIST m_spiceItems
 

List of items representing schematic components in the Spice world

More...
 
PROJECTm_project
 

Project object to fetch its settings (e.g. paths)

More...
 

Static Private Attributes

static const std::vector< wxString > m_spiceFields
 

Detailed Description

NETLIST_EXPORTER_PSPICE generates a PSPICE compatible netlist.

Definition at line 101 of file netlist_exporter_pspice.h.

Member Typedef Documentation

◆ NET_INDEX_MAP

typedef std::map<wxString, int> NETLIST_EXPORTER_PSPICE::NET_INDEX_MAP

Net name to circuit node number mapping

Definition at line 117 of file netlist_exporter_pspice.h.

◆ SPICE_ITEM_LIST

Definition at line 114 of file netlist_exporter_pspice.h.

Constructor & Destructor Documentation

◆ NETLIST_EXPORTER_PSPICE()

NETLIST_EXPORTER_PSPICE::NETLIST_EXPORTER_PSPICE ( NETLIST_OBJECT_LIST aMasterList,
PROJECT aProject = NULL 
)
inline

Definition at line 104 of file netlist_exporter_pspice.h.

104  :
105  NETLIST_EXPORTER( aMasterList ),
106  m_project( aProject )
107  {
108  }
NETLIST_EXPORTER(NETLIST_OBJECT_LIST *aMasterList)
Constructor.
PROJECT * m_project
Project object to fetch its settings (e.g. paths)

◆ ~NETLIST_EXPORTER_PSPICE()

virtual NETLIST_EXPORTER_PSPICE::~NETLIST_EXPORTER_PSPICE ( )
inlinevirtual

Definition at line 110 of file netlist_exporter_pspice.h.

111  {
112  }

Member Function Documentation

◆ addPinToComponentPinList()

bool NETLIST_EXPORTER::addPinToComponentPinList ( SCH_COMPONENT Component,
SCH_SHEET_PATH sheet,
LIB_PIN PinEntry 
)
protectedinherited

Function addPinToComponentPinList adds a new pin description to the pin list m_SortedComponentPinList.

A pin description is a pointer to the corresponding structure created by BuildNetList() in the table g_NetObjectslist.

Definition at line 212 of file netlist_exporter.cpp.

214 {
215  // Search the PIN description for Pin in g_NetObjectslist
216  for( unsigned ii = 0; ii < m_masterList->size(); ii++ )
217  {
218  NETLIST_OBJECT* pin = m_masterList->GetItem( ii );
219 
220  if( pin->m_Type != NETLIST_ITEM::PIN )
221  continue;
222 
223  if( pin->m_Link != aComponent )
224  continue;
225 
226  if( pin->m_PinNum != aPin->GetNumber() )
227  continue;
228 
229  // most expensive test at the end.
230  if( pin->m_SheetPath != *aSheetPath )
231  continue;
232 
233  m_SortedComponentPinList.push_back( pin );
234 
235  if( m_SortedComponentPinList.size() >= MAXPIN )
236  {
237  // Log message for Internal error
238  DisplayError( NULL, wxT( "addPinToComponentPinList err: MAXPIN reached" ) );
239  }
240 
241  return true; // we're done, we appended.
242  }
243 
244  return false;
245 }
void DisplayError(wxWindow *aParent, const wxString &aText, int aDisplayTime)
Display an error or warning message box with aMessage.
Definition: confirm.cpp:236
#define MAXPIN
Definition: netlist.h:53
SCH_SHEET_PATH m_SheetPath
NETLIST_OBJECT_LIST * m_masterList
#define NULL
NETLIST_OBJECTS m_SortedComponentPinList
yes ownership, connected items flat list
NETLIST_OBJECT * GetItem(unsigned aIdx) const
Acces to an item in list.
NETLIST_ITEM m_Type
SCH_ITEM * m_Link

References DisplayError(), NETLIST_OBJECT_LIST::GetItem(), LIB_PIN::GetNumber(), NETLIST_OBJECT::m_Link, NETLIST_EXPORTER::m_masterList, NETLIST_OBJECT::m_PinNum, NETLIST_OBJECT::m_SheetPath, NETLIST_EXPORTER::m_SortedComponentPinList, NETLIST_OBJECT::m_Type, MAXPIN, NULL, and PIN.

Referenced by NETLIST_EXPORTER::CreatePinList(), and NETLIST_EXPORTER::findAllUnitsOfComponent().

◆ CreatePinList()

void NETLIST_EXPORTER::CreatePinList ( SCH_COMPONENT aItem,
SCH_SHEET_PATH aSheetPath 
)
protectedinherited

Function findNextComponentAndCreatePinList finds a component from the DrawList and builds its pin list in m_SortedComponentPinList.

This list is sorted by pin num. the component is the next actual component after aItem (power symbols and virtual components that have their reference starting by '#'are skipped).

Definition at line 150 of file netlist_exporter.cpp.

151 {
152  wxString ref( comp->GetRef( aSheetPath ) );
153 
154  // Power symbols and other components which have the reference starting
155  // with "#" are not included in netlist (pseudo or virtual components)
156 
157  if( ref[0] == wxChar( '#' ) )
158  return;
159 
160  // if( Component->m_FlagControlMulti == 1 )
161  // continue; /* yes */
162  // removed because with multiple instances of one schematic
163  // (several sheets pointing to 1 screen), this will be erroneously be
164  // toggled.
165 
166  if( !comp->GetPartRef() )
167  return;
168 
169  m_SortedComponentPinList.clear();
170 
171  // If component is a "multi parts per package" type
172  if( comp->GetPartRef()->GetUnitCount() > 1 )
173  {
174  // test if this reference has already been processed, and if so skip
175  if( m_ReferencesAlreadyFound.Lookup( ref ) )
176  return;
177 
178  // Collect all pins for this reference designator by searching
179  // the entire design for other parts with the same reference designator.
180  // This is only done once, it would be too expensive otherwise.
181  findAllUnitsOfComponent( comp, comp->GetPartRef().get(), aSheetPath );
182  }
183 
184  else // entry->GetUnitCount() <= 1 means one part per package
185  {
186  LIB_PINS pins; // constructed once here
187 
188  comp->GetPartRef()->GetPins(
189  pins, comp->GetUnitSelection( aSheetPath ), comp->GetConvert() );
190 
191  for( size_t i = 0; i < pins.size(); i++ )
192  {
193  LIB_PIN* pin = pins[i];
194 
195  wxASSERT( pin->Type() == LIB_PIN_T );
196 
197  addPinToComponentPinList( comp, aSheetPath, pin );
198  }
199  }
200 
201  // Sort pins in m_SortedComponentPinList by pin number
203 
204  // Remove duplicate Pins in m_SortedComponentPinList
206 
207  // record the usage of this library component entry.
208  m_LibParts.insert( comp->GetPartRef().get() ); // rejects non-unique pointers
209 }
bool Lookup(const wxString &aString)
Function Lookup returns true if aString already exists in the set, otherwise returns false and adds a...
UNIQUE_STRINGS m_ReferencesAlreadyFound
Used for "multi parts per package" components, avoids processing a lib component more than once.
std::vector< LIB_PIN * > LIB_PINS
Helper for defining a list of pin object pointers.
Definition: lib_item.h:54
void findAllUnitsOfComponent(SCH_COMPONENT *aComponent, LIB_PART *aEntry, SCH_SHEET_PATH *aSheetPath)
Function findAllUnitsOfComponent is used for "multiple parts per package" components.
static bool sortPinsByNum(NETLIST_OBJECT *aPin1, NETLIST_OBJECT *aPin2)
Comparison routine for sorting by pin numbers.
std::set< LIB_PART *, LIB_PART_LESS_THAN > m_LibParts
unique library parts used. LIB_PART items are sorted by names
NETLIST_OBJECTS m_SortedComponentPinList
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...
KICAD_T Type() const
Function Type()
Definition: base_struct.h:207
void eraseDuplicatePins()
Function eraseDuplicatePins erase duplicate Pins from m_SortedComponentPinList (i....

References NETLIST_EXPORTER::addPinToComponentPinList(), NETLIST_EXPORTER::eraseDuplicatePins(), NETLIST_EXPORTER::findAllUnitsOfComponent(), SCH_COMPONENT::GetConvert(), SCH_COMPONENT::GetPartRef(), SCH_COMPONENT::GetRef(), SCH_COMPONENT::GetUnitSelection(), LIB_PIN_T, UNIQUE_STRINGS::Lookup(), NETLIST_EXPORTER::m_LibParts, NETLIST_EXPORTER::m_ReferencesAlreadyFound, NETLIST_EXPORTER::m_SortedComponentPinList, sortPinsByNum(), and EDA_ITEM::Type().

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

◆ eraseDuplicatePins()

void NETLIST_EXPORTER::eraseDuplicatePins ( )
protectedinherited

Function eraseDuplicatePins erase duplicate Pins from m_SortedComponentPinList (i.e.

set pointer in this list to NULL). (This is a list of pins found in the whole schematic, for a single component.) These duplicate pins were put in list because some pins (powers... ) are found more than one time when we have a multiple parts per package component. For instance, a 74ls00 has 4 parts, 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 248 of file netlist_exporter.cpp.

249 {
250  for( unsigned ii = 0; ii < m_SortedComponentPinList.size(); ii++ )
251  {
252  if( m_SortedComponentPinList[ii] == NULL ) /* already deleted */
253  continue;
254 
255  /* Search for duplicated pins
256  * If found, remove duplicates. The priority is to keep connected pins
257  * and remove unconnected
258  * - So this allows (for instance when using multi op amps per package
259  * - to connect only one op amp to power
260  * Because the pin list is sorted by m_PinNum value, duplicated pins
261  * are necessary successive in list
262  */
263  int idxref = ii;
264  for( unsigned jj = ii + 1; jj < m_SortedComponentPinList.size(); jj++ )
265  {
266  if( m_SortedComponentPinList[jj] == NULL ) // Already removed
267  continue;
268 
269  // if other pin num, stop search,
270  // because all pins having the same number are consecutive in list.
271  if( m_SortedComponentPinList[idxref]->m_PinNum != m_SortedComponentPinList[jj]->m_PinNum )
272  break;
273 
274  if( m_SortedComponentPinList[idxref]->GetConnectionType()
276  {
277  m_SortedComponentPinList[jj]->m_Flag = 1;
279  }
280  else /* the reference pin is not connected: remove this pin if the
281  * other pin is connected */
282  {
283  if( m_SortedComponentPinList[jj]->GetConnectionType()
285  {
286  m_SortedComponentPinList[idxref]->m_Flag = 1;
287  m_SortedComponentPinList[idxref] = NULL;
288  idxref = jj;
289  }
290  else // the 2 pins are not connected: remove the tested pin,
291  { // and continue ...
292  m_SortedComponentPinList[jj]->m_Flag = 1;
294  }
295  }
296  }
297  }
298 }
#define NULL
NETLIST_OBJECTS m_SortedComponentPinList
yes ownership, connected items flat list

References NETLIST_EXPORTER::m_SortedComponentPinList, NULL, and PAD_CONNECT.

Referenced by NETLIST_EXPORTER::CreatePinList().

◆ findAllUnitsOfComponent()

void NETLIST_EXPORTER::findAllUnitsOfComponent ( SCH_COMPONENT aComponent,
LIB_PART aEntry,
SCH_SHEET_PATH aSheetPath 
)
protectedinherited

Function findAllUnitsOfComponent is used for "multiple parts per package" components.

Search the entire design for all units of aComponent based on matching reference designator, and for each unit, add all its pins to the temporary sorted pin list, m_SortedComponentPinList.

Definition at line 301 of file netlist_exporter.cpp.

303 {
304  wxString ref = aComponent->GetRef( aSheetPath );
305  wxString ref2;
306 
307  SCH_SHEET_LIST sheetList( g_RootSheet );
308 
309  for( unsigned i = 0; i < sheetList.size(); i++ )
310  {
311  for( auto item : sheetList[i].LastScreen()->Items().OfType( SCH_COMPONENT_T ) )
312  {
313  SCH_COMPONENT* comp2 = static_cast<SCH_COMPONENT*>( item );
314 
315  ref2 = comp2->GetRef( &sheetList[i] );
316 
317  if( ref2.CmpNoCase( ref ) != 0 )
318  continue;
319 
320  int unit2 = comp2->GetUnitSelection( &sheetList[i] ); // slow
321 
322  for( LIB_PIN* pin = aEntry->GetNextPin(); pin; pin = aEntry->GetNextPin( pin ) )
323  {
324  wxASSERT( pin->Type() == LIB_PIN_T );
325 
326  if( pin->GetUnit() && pin->GetUnit() != unit2 )
327  continue;
328 
329  if( pin->GetConvert() && pin->GetConvert() != comp2->GetConvert() )
330  continue;
331 
332  // A suitable pin is found: add it to the current list
333  addPinToComponentPinList( comp2, &sheetList[i], pin );
334  }
335  }
336  }
337 }
SCH_SHEET_LIST.
int GetUnitSelection(SCH_SHEET_PATH *aSheet)
const wxString GetRef(const SCH_SHEET_PATH *aSheet)
Return the reference for the given sheet path.
LIB_PIN * GetNextPin(LIB_PIN *aItem=NULL)
Return the next pin object from the draw list.
SCH_COMPONENT describes a real schematic component.
Definition: sch_component.h:89
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
SCH_SHEET * g_RootSheet

References NETLIST_EXPORTER::addPinToComponentPinList(), g_RootSheet, SCH_COMPONENT::GetConvert(), LIB_PART::GetNextPin(), SCH_COMPONENT::GetRef(), SCH_COMPONENT::GetUnitSelection(), LIB_PIN_T, and SCH_COMPONENT_T.

Referenced by NETLIST_EXPORTER::CreatePinList().

◆ findNextComponent()

SCH_COMPONENT * NETLIST_EXPORTER::findNextComponent ( EDA_ITEM aItem,
SCH_SHEET_PATH aSheetPath 
)
protectedinherited

Definition at line 101 of file netlist_exporter.cpp.

102 {
103  wxString ref;
104 
105  if( aItem->Type() != SCH_COMPONENT_T )
106  return nullptr;
107 
108  // found next component
109  SCH_COMPONENT* comp = (SCH_COMPONENT*) aItem;
110 
111  // Power symbols and other components which have the reference starting
112  // with "#" are not included in netlist (pseudo or virtual components)
113  ref = comp->GetRef( aSheetPath );
114 
115  if( ref[0] == wxChar( '#' ) )
116  return nullptr;
117 
118  // if( Component->m_FlagControlMulti == 1 )
119  // continue; /* yes */
120  // removed because with multiple instances of one schematic
121  // (several sheets pointing to 1 screen), this will be erroneously be
122  // toggled.
123 
124  if( !comp->GetPartRef() )
125  return nullptr;
126 
127  // If component is a "multi parts per package" type
128  if( comp->GetPartRef()->GetUnitCount() > 1 )
129  {
130  // test if this reference has already been processed, and if so skip
131  if( m_ReferencesAlreadyFound.Lookup( ref ) )
132  return nullptr;
133  }
134 
135  // record the usage of this library component entry.
136  m_LibParts.insert( comp->GetPartRef().get() ); // rejects non-unique pointers
137 
138  return comp;
139 }
bool Lookup(const wxString &aString)
Function Lookup returns true if aString already exists in the set, otherwise returns false and adds a...
UNIQUE_STRINGS m_ReferencesAlreadyFound
Used for "multi parts per package" components, avoids processing a lib component more than once.
std::set< LIB_PART *, LIB_PART_LESS_THAN > m_LibParts
unique library parts used. LIB_PART items are sorted by names
std::unique_ptr< LIB_PART > & GetPartRef()
const wxString GetRef(const SCH_SHEET_PATH *aSheet)
Return the reference for the given sheet path.
SCH_COMPONENT describes a real schematic component.
Definition: sch_component.h:89
KICAD_T Type() const
Function Type()
Definition: base_struct.h:207

References SCH_COMPONENT::GetPartRef(), SCH_COMPONENT::GetRef(), UNIQUE_STRINGS::Lookup(), NETLIST_EXPORTER::m_LibParts, NETLIST_EXPORTER::m_ReferencesAlreadyFound, SCH_COMPONENT_T, and EDA_ITEM::Type().

Referenced by NETLIST_EXPORTER_GENERIC::makeComponents(), ProcessNetlist(), NETLIST_EXPORTER_ORCADPCB2::WriteNetlist(), and NETLIST_EXPORTER_CADSTAR::WriteNetlist().

◆ Format()

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

Definition at line 79 of file netlist_exporter_pspice.cpp.

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_project );
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  NETLIST_OBJECT* pin = item.m_pins[activePinIndex];
139  assert( pin );
140  wxString netName = pin->GetNetName();
141 
142  if( useNetcodeAsNetName )
143  {
144  assert( m_netMap.count( netName ) );
145  aFormatter->Print( 0, "%d ", m_netMap[netName] );
146  }
147  else
148  {
149  sprintPinNetName( netName , wxT( "N-%.6d" ), pin, useNetcodeAsNetName );
150 
151  // Replace parenthesis with underscore to prevent parse issues with simulators
152  ReplaceForbiddenChars( netName );
153 
154  // unescape net names that contain a escaped sequence ("{slash}"):
155  netName = UnescapeString( netName );
156 
157  // Borrow LTSpice's nomenclature for unconnected nets
158  if( netName.IsEmpty() )
159  netName = wxString::Format( wxT( "NC_%.2u" ), NC_counter++ );
160 
161  aFormatter->Print( 0, "%s ", TO_UTF8( netName ) );
162  }
163  }
164 
165  aFormatter->Print( 0, "%s\n", (const char*) item.m_model.c_str() );
166  }
167 
168  // Print out all directives found in the text fields on the schematics
169  writeDirectives( aFormatter, aCtl );
170 
171  aFormatter->Print( 0, ".end\n" );
172 
173  return true;
174 }
void DisplayError(wxWindow *aParent, const wxString &aText, int aDisplayTime)
Display an error or warning message box with aMessage.
Definition: confirm.cpp:236
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:138
KIWAY Kiway & Pgm(), KFCTL_STANDALONE
The global Program "get" accessor.
Definition: single_top.cpp:98
std::set< wxString > m_libraries
Libraries used by the simulated circuit
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.
SPICE_ITEM_LIST m_spiceItems
List of items representing schematic components in the Spice world
#define TO_UTF8(wxstring)
Macro TO_UTF8 converts a wxString to a UTF8 encoded C string for all wxWidgets build modes.
Definition: macros.h:48
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.
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.
PROJECT * m_project
Project object to fetch its settings (e.g. paths)
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:31
wxString UnescapeString(const wxString &aSource)
Definition: string.cpp:131
wxString GetNetName(bool adoptTimestamp=false) const
Function GetNetName.
wxString GetSpiceDevice(const wxString &aComponent) const
Returns name of Spice device corresponding to a schematic component.
int PRINTF_FUNC Print(int nestLevel, const char *fmt,...)
Function Print formats and writes text to the output stream.
Definition: richio.cpp:404

References _, DisplayError(), Format(), NETLIST_OBJECT::GetNetName(), GetSpiceDevice(), m_libraries, m_netMap, m_project, m_spiceItems, m_title, NET_ADJUST_INCLUDE_PATHS, NULL, Pgm(), OUTPUTFORMATTER::Print(), ProcessNetlist(), ReplaceForbiddenChars(), ResolveFile(), NETLIST_EXPORTER::sprintPinNetName(), TO_UTF8, UnescapeString(), and writeDirectives().

Referenced by WriteNetlist().

◆ GetDirectives()

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

Returnss a vector of Spice directives found in the schematics.

Definition at line 206 of file netlist_exporter_pspice.h.

207  {
208  return m_directives;
209  }
std::vector< wxString > m_directives
Spice directives found in the processed schematic sheet

References m_directives.

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

◆ GetNetIndexMap()

const NET_INDEX_MAP& NETLIST_EXPORTER_PSPICE::GetNetIndexMap ( ) const
inline

Returns a map of circuit nodes to net names.

Definition at line 167 of file netlist_exporter_pspice.h.

168  {
169  return m_netMap;
170  }
NET_INDEX_MAP m_netMap
Maps circuit nodes to net names

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 &  aComponent) const

Returns name of Spice device corresponding to a schematic component.

Parameters
aComponentis the component reference.
Returns
Spice device name or empty string if there is no such component 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 43 of file netlist_exporter_pspice.cpp.

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 }
const SPICE_ITEM_LIST & GetSpiceItems() const
Returns list of items representing schematic components in the Spice world.
Structure to represent a schematic component in the Spice simulation.

References GetSpiceItems().

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

◆ GetSpiceField()

wxString NETLIST_EXPORTER_PSPICE::GetSpiceField ( SPICE_FIELD  aField,
SCH_COMPONENT aComponent,
unsigned  aCtl 
)
static

Retrieves either the requested field value or the default value.

Definition at line 177 of file netlist_exporter_pspice.cpp.

179 {
180  SCH_FIELD* field = aComponent->FindField( GetSpiceFieldName( aField ) );
181  return field ? field->GetText() : GetSpiceFieldDefVal( aField, aComponent, aCtl );
182 }
SCH_FIELD instances are attached to a component and provide a place for the component's value,...
Definition: sch_field.h:52
SCH_FIELD * FindField(const wxString &aFieldName, bool aIncludeDefaultFields=true)
Search for a SCH_FIELD with aFieldName.
static const wxString & GetSpiceFieldName(SPICE_FIELD aField)
Returns a string used for a particular component field related to Spice simulation.
static wxString GetSpiceFieldDefVal(SPICE_FIELD aField, SCH_COMPONENT *aComponent, unsigned aCtl)
Retrieves the default value for a given field.
virtual const wxString & GetText() const
Return the string associated with the text object.
Definition: eda_text.h:123

References SCH_COMPONENT::FindField(), GetSpiceFieldDefVal(), GetSpiceFieldName(), and EDA_TEXT::GetText().

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

◆ GetSpiceFieldDefVal()

wxString NETLIST_EXPORTER_PSPICE::GetSpiceFieldDefVal ( SPICE_FIELD  aField,
SCH_COMPONENT aComponent,
unsigned  aCtl 
)
static

Retrieves the default value for a given field.

Definition at line 185 of file netlist_exporter_pspice.cpp.

187 {
188  switch( aField )
189  {
190  case SF_PRIMITIVE:
191  {
192  const wxString refName = aComponent->GetField( REFERENCE )->GetText();
193  return refName.GetChar( 0 );
194  break;
195  }
196 
197  case SF_MODEL:
198  {
199  wxChar prim = aComponent->GetField( REFERENCE )->GetText().GetChar( 0 );
200  wxString value = aComponent->GetField( VALUE )->GetText();
201 
202  // Is it a passive component?
203  if( aCtl & NET_ADJUST_PASSIVE_VALS && ( prim == 'C' || prim == 'L' || prim == 'R' ) )
204  {
205  // Regular expression to match common formats used for passive parts description
206  // (e.g. 100k, 2k3, 1 uF)
207  wxRegEx passiveVal( "^([0-9\\. ]+)([fFpPnNuUmMkKgGtT]|M(e|E)(g|G))?([fFhH]|ohm)?([-1-9 ]*)$" );
208 
209  if( passiveVal.Matches( value ) )
210  {
211  wxString prefix( passiveVal.GetMatch( value, 1 ) );
212  wxString unit( passiveVal.GetMatch( value, 2 ) );
213  wxString suffix( passiveVal.GetMatch( value, 6 ) );
214 
215  prefix.Trim(); prefix.Trim( false );
216  unit.Trim(); unit.Trim( false );
217  suffix.Trim(); suffix.Trim( false );
218 
219  // Make 'mega' units comply with the Spice expectations
220  if( unit == "M" )
221  unit = "Meg";
222 
223  value = prefix + unit + suffix;
224  }
225  }
226 
227  return value;
228  break;
229  }
230 
231  case SF_ENABLED:
232  return wxString( "Y" );
233  break;
234 
235  case SF_NODE_SEQUENCE:
236  {
237  wxString nodeSeq;
238  std::vector<LIB_PIN*> pins;
239 
240  wxCHECK( aComponent->GetPartRef(), wxString() );
241  aComponent->GetPartRef()->GetPins( pins );
242 
243  for( auto pin : pins )
244  nodeSeq += pin->GetNumber() + " ";
245 
246  nodeSeq.Trim();
247 
248  return nodeSeq;
249  break;
250  }
251 
252  case SF_LIB_FILE:
253  // There is no default Spice library
254  return wxEmptyString;
255  break;
256 
257  default:
258  wxASSERT_MSG( false, "Missing default value definition for a Spice field" );
259  break;
260  }
261 
262 
263  return wxString( "<unknown>" );
264 }
Field Reference of part, i.e. "IC21".
SCH_FIELD * GetField(int aFieldNdx) const
Returns a field in this symbol.
#define VALUE
std::unique_ptr< LIB_PART > & GetPartRef()
virtual const wxString & GetText() const
Return the string associated with the text object.
Definition: eda_text.h:123

References SCH_COMPONENT::GetField(), SCH_COMPONENT::GetPartRef(), EDA_TEXT::GetText(), NET_ADJUST_PASSIVE_VALS, REFERENCE, SF_ENABLED, SF_LIB_FILE, SF_MODEL, SF_NODE_SEQUENCE, SF_PRIMITIVE, and VALUE.

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

◆ GetSpiceFieldName()

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

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

Definition at line 183 of file netlist_exporter_pspice.h.

184  {
185  return m_spiceFields[(int) aField];
186  }
static const std::vector< wxString > m_spiceFields

References m_spiceFields.

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

◆ GetSpiceFields()

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

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

Definition at line 175 of file netlist_exporter_pspice.h.

176  {
177  return m_spiceFields;
178  }
static const std::vector< wxString > m_spiceFields

References m_spiceFields.

Referenced by DIALOG_SPICE_MODEL::TransferDataToWindow().

◆ GetSpiceItems()

const SPICE_ITEM_LIST& NETLIST_EXPORTER_PSPICE::GetSpiceItems ( ) const
inline

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

Definition at line 122 of file netlist_exporter_pspice.h.

123  {
124  return m_spiceItems;
125  }
SPICE_ITEM_LIST m_spiceItems
List of items representing schematic components in the Spice world

References m_spiceItems.

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

◆ MakeCommandLine()

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

Function MakeCommandLine builds 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 39 of file netlist_exporter.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( "%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 }

Referenced by SCH_EDIT_FRAME::WriteNetListFile().

◆ ProcessNetlist()

bool NETLIST_EXPORTER_PSPICE::ProcessNetlist ( unsigned  aCtl)

Processes 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 267 of file netlist_exporter_pspice.cpp.

268 {
269  const wxString delimiters( "{:,; }" );
270  SCH_SHEET_LIST sheetList( g_RootSheet );
271  // Set of reference names, to check for duplications
272  std::set<wxString> refNames;
273 
274  // Prepare list of nets generation (not used here, but...
275  for( unsigned ii = 0; ii < m_masterList->size(); ii++ )
276  m_masterList->GetItem( ii )->m_Flag = 0;
277 
278  m_netMap.clear();
279  m_netMap["GND"] = 0; // 0 is reserved for "GND"
280  int netIdx = 1;
281 
282  m_libraries.clear();
284 
285  UpdateDirectives( aCtl );
286 
287  for( unsigned sheet_idx = 0; sheet_idx < sheetList.size(); sheet_idx++ )
288  {
289  // Process component attributes to find Spice directives
290  for( auto item : sheetList[sheet_idx].LastScreen()->Items().OfType( SCH_COMPONENT_T ) )
291  {
292  SCH_COMPONENT* comp = findNextComponent( item, &sheetList[sheet_idx] );
293 
294  if( !comp )
295  continue;
296 
297  CreatePinList( comp, &sheetList[sheet_idx] );
298  SPICE_ITEM spiceItem;
299  spiceItem.m_parent = comp;
300 
301  // Obtain Spice fields
302  SCH_FIELD* fieldLibFile = comp->FindField( GetSpiceFieldName( SF_LIB_FILE ) );
303  SCH_FIELD* fieldSeq = comp->FindField( GetSpiceFieldName( SF_NODE_SEQUENCE ) );
304 
305  spiceItem.m_primitive = GetSpiceField( SF_PRIMITIVE, comp, aCtl )[0];
306  spiceItem.m_model = GetSpiceField( SF_MODEL, comp, aCtl );
307  spiceItem.m_refName = comp->GetRef( &sheetList[sheet_idx] );
308 
309  // Duplicate references will result in simulation errors
310  if( refNames.count( spiceItem.m_refName ) )
311  {
312  DisplayError( NULL, wxT( "There are duplicate components. "
313  "You need to annotate schematics first." ) );
314  return false;
315  }
316 
317  refNames.insert( spiceItem.m_refName );
318 
319  // Check to see if component should be removed from Spice netlist
320  spiceItem.m_enabled = StringToBool( GetSpiceField( SF_ENABLED, comp, aCtl ) );
321 
322  if( fieldLibFile && !fieldLibFile->GetText().IsEmpty() )
323  m_libraries.insert( fieldLibFile->GetText() );
324 
325  wxArrayString pinNames;
326 
327  // Store pin information
328  for( unsigned ii = 0; ii < m_SortedComponentPinList.size(); ii++ )
329  {
331 
332  // NETLIST_EXPORTER marks removed pins by setting them to NULL
333  if( !pin )
334  continue;
335 
336  spiceItem.m_pins.push_back( pin );
337  pinNames.Add( pin->GetPinNumText() );
338 
339  // Create net mapping
340  const wxString& netName = pin->GetNetName();
341  if( m_netMap.count( netName ) == 0 )
342  m_netMap[netName] = netIdx++;
343  }
344 
345  // Check if an alternative pin sequence is available:
346  if( fieldSeq )
347  {
348  // Get the string containing the sequence of nodes:
349  const wxString& nodeSeqIndexLineStr = fieldSeq->GetText();
350 
351  // Verify field exists and is not empty:
352  if( !nodeSeqIndexLineStr.IsEmpty() )
353  {
354  // Get Alt Pin Name Array From User:
355  wxStringTokenizer tkz( nodeSeqIndexLineStr, delimiters );
356 
357  while( tkz.HasMoreTokens() )
358  {
359  wxString pinIndex = tkz.GetNextToken();
360  int seq;
361 
362  // Find PinName In Standard List assign Standard List Index to Name:
363  seq = pinNames.Index( pinIndex );
364 
365  if( seq != wxNOT_FOUND )
366  spiceItem.m_pinSequence.push_back( seq );
367  }
368  }
369  }
370 
371  m_spiceItems.push_back( spiceItem );
372  }
373  }
374 
375  return true;
376 }
void DisplayError(wxWindow *aParent, const wxString &aText, int aDisplayTime)
Display an error or warning message box with aMessage.
Definition: confirm.cpp:236
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
std::vector< NETLIST_OBJECT * > m_pins
Array containing Standard Pin Name
SCH_FIELD * FindField(const wxString &aFieldName, bool aIncludeDefaultFields=true)
Search for a SCH_FIELD with aFieldName.
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
SCH_COMPONENT * m_parent
Schematic component represented by this SPICE_ITEM.
Structure to represent a schematic component in the Spice simulation.
static const wxString & GetSpiceFieldName(SPICE_FIELD aField)
Returns a string used for a particular component field related to Spice simulation.
bool m_enabled
Flag to indicate whether the component should be used in simulation.
void Clear()
Function Clear erases the record.
SCH_COMPONENT * findNextComponent(EDA_ITEM *aItem, SCH_SHEET_PATH *aSheetPath)
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...
NETLIST_OBJECT_LIST * m_masterList
SPICE_ITEM_LIST m_spiceItems
List of items representing schematic components in the Spice world
NET_INDEX_MAP m_netMap
Maps circuit nodes to net names
#define NULL
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...
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.
NETLIST_OBJECTS m_SortedComponentPinList
yes ownership, connected items flat list
NETLIST_OBJECT * GetItem(unsigned aIdx) const
Acces to an item in list.
wxString GetNetName(bool adoptTimestamp=false) const
Function GetNetName.
static wxString GetSpiceField(SPICE_FIELD aField, SCH_COMPONENT *aComponent, unsigned aCtl)
Retrieves either the requested field value or the default value.
SCH_COMPONENT describes a real schematic component.
Definition: sch_component.h:89
wxChar m_primitive
Spice primitive type (
SCH_SHEET * g_RootSheet
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:123

References UNIQUE_STRINGS::Clear(), NETLIST_EXPORTER::CreatePinList(), DisplayError(), SCH_COMPONENT::FindField(), NETLIST_EXPORTER::findNextComponent(), g_RootSheet, NETLIST_OBJECT_LIST::GetItem(), NETLIST_OBJECT::GetNetName(), NETLIST_OBJECT::GetPinNumText(), SCH_COMPONENT::GetRef(), GetSpiceField(), GetSpiceFieldName(), EDA_TEXT::GetText(), SPICE_ITEM::m_enabled, NETLIST_OBJECT::m_Flag, m_libraries, NETLIST_EXPORTER::m_masterList, SPICE_ITEM::m_model, m_netMap, SPICE_ITEM::m_parent, SPICE_ITEM::m_pins, SPICE_ITEM::m_pinSequence, SPICE_ITEM::m_primitive, NETLIST_EXPORTER::m_ReferencesAlreadyFound, SPICE_ITEM::m_refName, NETLIST_EXPORTER::m_SortedComponentPinList, m_spiceItems, NULL, SCH_COMPONENT_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

some chars are not accepted in netnames in spice netlists.

for instance '(' and ')' ReplaceForbiddenChars replace these chars by an underscore.

Parameters
aNetName= the netname to modify.

Definition at line 67 of file netlist_exporter_pspice.cpp.

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 }

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

◆ sprintPinNetName()

void NETLIST_EXPORTER::sprintPinNetName ( wxString &  aResult,
const wxString &  aNetNameFormat,
NETLIST_OBJECT aPin,
bool  aUseNetcodeAsNetName = false 
)
staticprotectedinherited

Function sprintPinNetName formats the net name for aPin using aNetNameFormat into aResult.

Net name is:

  • "?" if pin not connected
  • "netname" for global net (like gnd, vcc ..
  • "/path/netname" for the usual nets

if aUseNetcodeAsNetName is true, the net name is just the net code (SPICE only)

Definition at line 73 of file netlist_exporter.cpp.

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() == NET_CONNECTION::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 }
int GetNet() const
NET_CONNECTION GetConnectionType() const
wxString GetNetName(bool adoptTimestamp=false) const
Function GetNetName.

References NETLIST_OBJECT::GetConnectionType(), NETLIST_OBJECT::GetNet(), NETLIST_OBJECT::GetNetName(), and PAD_CONNECT.

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

◆ StringToBool()

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

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

Definition at line 214 of file netlist_exporter_pspice.h.

215  {
216  if( aStr.IsEmpty() )
217  return false;
218 
219  char c = tolower( aStr[0] );
220 
221  // Different ways of saying false (no/false/0)
222  return !( c == 'n' || c == 'f' || c == '0' );
223  }

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

◆ UpdateDirectives()

void NETLIST_EXPORTER_PSPICE::UpdateDirectives ( unsigned  aCtl)

Updates the vector of Spice directives placed in the schematics.

Definition at line 379 of file netlist_exporter_pspice.cpp.

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

References g_RootSheet, m_directives, m_libraries, m_title, and SCH_TEXT_T.

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

◆ writeDirectives()

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

Saves the Spice directives.

Reimplemented in NETLIST_EXPORTER_PSPICE_SIM.

Definition at line 475 of file netlist_exporter_pspice.cpp.

476 {
477  for( auto& dir : m_directives )
478  {
479  aFormatter->Print( 0, "%s\n", (const char*) dir.c_str() );
480  }
481 }
std::vector< wxString > m_directives
Spice directives found in the processed schematic sheet
int PRINTF_FUNC Print(int nestLevel, const char *fmt,...)
Function Print formats and writes text to the output stream.
Definition: richio.cpp:404

References m_directives, and OUTPUTFORMATTER::Print().

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

◆ WriteNetlist()

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

Function WriteNetlist writes to specified output file.

Reimplemented from NETLIST_EXPORTER.

Definition at line 60 of file netlist_exporter_pspice.cpp.

61 {
62  FILE_OUTPUTFORMATTER outputFile( aOutFileName, wxT( "wt" ), '\'' );
63 
64  return Format( &outputFile, aNetlistOptions );
65 }
bool Format(OUTPUTFORMATTER *aFormatter, unsigned aCtl)
FILE_OUTPUTFORMATTER may be used for text file output.
Definition: richio.h:492

References Format().

Member Data Documentation

◆ m_directives

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

Spice directives found in the processed schematic sheet

Definition at line 236 of file netlist_exporter_pspice.h.

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

◆ m_LibParts

std::set<LIB_PART*, LIB_PART_LESS_THAN> NETLIST_EXPORTER::m_LibParts
protectedinherited

◆ m_libraries

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

Libraries used by the simulated circuit

Definition at line 239 of file netlist_exporter_pspice.h.

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

◆ m_masterList

◆ m_netMap

NET_INDEX_MAP NETLIST_EXPORTER_PSPICE::m_netMap
private

Maps circuit nodes to net names

Definition at line 242 of file netlist_exporter_pspice.h.

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

◆ m_project

PROJECT* NETLIST_EXPORTER_PSPICE::m_project
private

Project object to fetch its settings (e.g. paths)

Definition at line 248 of file netlist_exporter_pspice.h.

Referenced by Format().

◆ m_ReferencesAlreadyFound

UNIQUE_STRINGS NETLIST_EXPORTER::m_ReferencesAlreadyFound
protectedinherited

Used for "multi parts per package" components, avoids processing a lib component more than once.

Definition at line 100 of file netlist_exporter.h.

Referenced by NETLIST_EXPORTER::CreatePinList(), NETLIST_EXPORTER::findNextComponent(), NETLIST_EXPORTER_GENERIC::makeComponents(), ProcessNetlist(), NETLIST_EXPORTER_ORCADPCB2::WriteNetlist(), and NETLIST_EXPORTER_CADSTAR::WriteNetlist().

◆ m_SortedComponentPinList

NETLIST_OBJECTS NETLIST_EXPORTER::m_SortedComponentPinList
protectedinherited

yes ownership, connected items flat list

Used to temporarily store and filter the list of pins of a schematic component when generating schematic component data in netlist (comp section). No ownership of members. TODO(snh): Descope this object

Definition at line 96 of file netlist_exporter.h.

Referenced by NETLIST_EXPORTER::addPinToComponentPinList(), NETLIST_EXPORTER::CreatePinList(), NETLIST_EXPORTER::eraseDuplicatePins(), ProcessNetlist(), NETLIST_EXPORTER_ORCADPCB2::WriteNetlist(), and NETLIST_EXPORTER_CADSTAR::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 251 of file netlist_exporter_pspice.h.

Referenced by GetSpiceFieldName(), and GetSpiceFields().

◆ m_spiceItems

SPICE_ITEM_LIST NETLIST_EXPORTER_PSPICE::m_spiceItems
private

List of items representing schematic components in the Spice world

Definition at line 245 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 processed schematic sheet

Definition at line 233 of file netlist_exporter_pspice.h.

Referenced by Format(), and UpdateDirectives().


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