Fixed Spice netlist export options and added a few new flags

This commit is contained in:
Maciej Suminski 2016-08-11 14:41:20 +02:00
parent 5795a2dbf3
commit 951d16c655
6 changed files with 44 additions and 61 deletions

View File

@ -306,7 +306,8 @@ void DIALOG_EDIT_COMPONENT_IN_SCHEMATIC::EditSpiceFields( wxCommandEvent& event
if( schField->GetText().IsEmpty() )
{
schField->SetText( NETLIST_EXPORTER_PSPICE::GetSpiceFieldDefVal( fieldName, m_cmp ) );
schField->SetText( NETLIST_EXPORTER_PSPICE::GetSpiceFieldDefVal( fieldName, m_cmp,
NET_USE_X_PREFIX | NET_ADJUST_INCLUDE_PATHS | NET_ADJUST_PASSIVE_VALS ) );
}
}

View File

@ -52,6 +52,7 @@
#include <wildcards_and_files_ext.h>
#include <wildcards_and_files_ext.h>
#include <invoke_sch_dialog.h>
#include <netlist_exporters/netlist_exporter_pspice.h>
#include <eeschema_id.h>

View File

@ -47,13 +47,6 @@ enum NETLIST_TYPE_ID {
};
/// Options for Spice netlist generation (OR'ed bits
enum netlistOptions {
NET_USE_X_PREFIX = 2, // for Spice netlist : change "U" and "IC" reference prefix to "X"
NET_USE_NETCODES_AS_NETNAMES = 4 // for Spice netlist : use netcode numbers as netnames
};
#define NETLIST_HEAD_STRING "EESchema Netlist Version 1.1"
// Max pin number per component and footprint

View File

@ -41,10 +41,6 @@
bool NETLIST_EXPORTER_PSPICE::WriteNetlist( const wxString& aOutFileName, unsigned aNetlistOptions )
{
/// @todo take options into account
//bool aUsePrefix = aNetlistOptions & NET_USE_X_PREFIX;
//bool aUseNetcodeAsNetName = aNetlistOptions & NET_USE_NETCODES_AS_NETNAMES;
FILE_OUTPUTFORMATTER outputFile( aOutFileName, wxT( "wt" ), '\'' );
return Format( &outputFile, aNetlistOptions );
@ -53,11 +49,13 @@ bool NETLIST_EXPORTER_PSPICE::WriteNetlist( const wxString& aOutFileName, unsign
bool NETLIST_EXPORTER_PSPICE::Format( OUTPUTFORMATTER* formatter, int aCtl )
{
int ret = 0;
std::vector<int> pinSequence; // numeric indices into m_SortedComponentPinList
wxArrayString stdPinNameArray; // Array containing Standard Pin Names
const wxString delimiters( "{:,; }" );
// Netlist options
bool useNetcodeAsNetName = aCtl & NET_USE_NETCODES_AS_NETNAMES;
// Prepare list of nets generation (not used here, but...
for( unsigned ii = 0; ii < m_masterList->size(); ii++ )
m_masterList->GetItem( ii )->m_Flag = 0;
@ -93,7 +91,7 @@ bool NETLIST_EXPORTER_PSPICE::Format( OUTPUTFORMATTER* formatter, int aCtl )
wxString directive( tokenizer.GetNextToken() );
// Fix paths for .include directives
if( m_paths && directive.StartsWith( ".inc" ) )
if( aCtl & NET_ADJUST_INCLUDE_PATHS && m_paths && directive.StartsWith( ".inc" ) )
{
wxString file( directive.AfterFirst( ' ' ) );
wxString path( m_paths->FindValidPath( file ) );
@ -134,10 +132,10 @@ bool NETLIST_EXPORTER_PSPICE::Format( OUTPUTFORMATTER* formatter, int aCtl )
SCH_FIELD* spiceSeqField = comp->FindField( wxT( "Spice_Node_Sequence" ) );
wxString model = spiceModel ? spiceModel->GetText()
: GetSpiceFieldDefVal( "Spice_Model", comp );
: GetSpiceFieldDefVal( "Spice_Model", comp, aCtl );
wxString primType = spicePrimitiveType ? spicePrimitiveType->GetText()
: GetSpiceFieldDefVal( "Spice_Primitive", comp );
: GetSpiceFieldDefVal( "Spice_Primitive", comp, aCtl );
const wxString& RefName = comp->GetRef( &sheetList[sheet_idx] );
@ -146,6 +144,7 @@ bool NETLIST_EXPORTER_PSPICE::Format( OUTPUTFORMATTER* formatter, int aCtl )
{
wxString netlistEnabled = netlistEnabledField->GetText();
// Different ways of saying 'disabled' (no/false/0)
if( netlistEnabled.CmpNoCase( "N" ) == 0
|| netlistEnabled.CmpNoCase( "F" ) == 0
|| netlistEnabled == "0" )
@ -237,24 +236,29 @@ bool NETLIST_EXPORTER_PSPICE::Format( OUTPUTFORMATTER* formatter, int aCtl )
{
netIdx = curNetIndex++;
m_netMap[netName] = netIdx;
} else {
}
else
{
netIdx = m_netMap[netName];
}
// TODO remove?
//printf("net %s index %d\n", (const char*)netName.c_str(), netIdx);
// sprintPinNetName( netName , wxT( "N-%.6d" ), pin, aUseNetcodeAsNetName );
if( useNetcodeAsNetName )
{
formatter->Print( 0, "%d ", netIdx );
}
else
{
sprintPinNetName( netName , wxT( "N-%.6d" ), pin, useNetcodeAsNetName );
//Replace parenthesis with underscore to prevent parse issues with Simulators:
// netName.Replace( wxT( "(" ), wxT( "_" ) );
// netName.Replace( wxT( ")" ), wxT( "_" ) );
//Replace parenthesis with underscore to prevent parse issues with simulators
netName.Replace( wxT( "(" ), wxT( "_" ) );
netName.Replace( wxT( ")" ), wxT( "_" ) );
// if( netName.IsEmpty() )
// netName = wxT( "?" );
if( netName.IsEmpty() )
netName = wxT( "?" );
// ret |= fprintf( f, " %s", TO_UTF8( netName ) );
formatter->Print( 0, "%d ", netIdx );
formatter->Print( 0, "%s ", TO_UTF8( netName ) );
}
}
formatter->Print( 0, "%s\n", (const char*) model.c_str() );
@ -269,44 +273,19 @@ bool NETLIST_EXPORTER_PSPICE::Format( OUTPUTFORMATTER* formatter, int aCtl )
formatter->Print( -1, ".end\n" );
// TODO remove?
#if 0
m_SortedComponentPinList.clear();
// Print texts starting with [+]pspice or [+]gnucap
nbitems = spiceCommandAtEndFile.GetCount();
if( nbitems )
{
ret |= fprintf( f, "\n" );
spiceCommandAtEndFile.Sort();
for( int ii = 0; ii < nbitems; ii++ )
{
spiceCommandAtEndFile[ii].Remove( 0, +BUFYPOS_LEN );
spiceCommandAtEndFile[ii].Trim( true );
spiceCommandAtEndFile[ii].Trim( false );
ret |= fprintf( f, "%s\n", TO_UTF8( spiceCommandAtEndFile[ii] ) );
}
}
ret |= fprintf( f, "\n.end\n" );
fclose( f );
#endif
return ret >= 0;
return true;
}
wxString NETLIST_EXPORTER_PSPICE::GetSpiceFieldDefVal( const wxString& aField,
SCH_COMPONENT* aComponent )
SCH_COMPONENT* aComponent, int aCtl )
{
if( aField == "Spice_Primitive" )
{
const wxString& refName = aComponent->GetField( REFERENCE )->GetText();
// Convert ICs to subcircuits
if( refName.StartsWith( "IC" ) || refName.StartsWith( "U" ) )
if( aCtl & NET_USE_X_PREFIX && ( refName.StartsWith( "IC" ) || refName.StartsWith( "U" ) ) )
return wxString( "X" );
else
return refName.GetChar( 0 );
@ -318,7 +297,7 @@ wxString NETLIST_EXPORTER_PSPICE::GetSpiceFieldDefVal( const wxString& aField,
wxString value = aComponent->GetField( VALUE )->GetText();
// Is it a passive component?
if( prim == 'C' || prim == 'L' || prim == 'R' )
if( aCtl & NET_ADJUST_PASSIVE_VALS && ( prim == 'C' || prim == 'L' || prim == 'R' ) )
{
// Regular expression to match common formats used for passive parts description
// (e.g. 100k, 2k3, 1 uF)
@ -338,9 +317,7 @@ wxString NETLIST_EXPORTER_PSPICE::GetSpiceFieldDefVal( const wxString& aField,
if( unit == "M" )
unit = "Meg";
wxLogDebug( "Changed passive value: %s..", value );
value = prefix + unit + suffix;
wxLogDebug( "..to: %s", value );
}
}

View File

@ -31,6 +31,16 @@
class SEARCH_STACK;
/// Flags for Spice netlist generation (can be combined)
enum SPICE_NETLIST_OPTIONS {
NET_USE_X_PREFIX = 2, // change "U" and "IC" reference prefix to "X"
NET_USE_NETCODES_AS_NETNAMES = 4, // use netcode numbers as netnames
NET_ADJUST_INCLUDE_PATHS = 8, // use full paths for included files (if they are in search path)
NET_ADJUST_PASSIVE_VALS = 16 // reformat passive component values (e.g. 1M -> 1Meg)
};
/// @todo add NET_ADJUST_INCLUDE_PATHS & NET_ADJUST_PASSIVE_VALS checkboxes in the netlist export dialog
/**
* Class NETLIST_EXPORTER_PSPICE
* generates a PSPICE compatible netlist
@ -43,6 +53,7 @@ public:
{
}
///> Net name to node number mapping
typedef std::map<wxString, int> NET_INDEX_MAP;
/**
@ -63,7 +74,7 @@ public:
return m_spiceFields;
}
static wxString GetSpiceFieldDefVal( const wxString& aField, SCH_COMPONENT* aComponent );
static wxString GetSpiceFieldDefVal( const wxString& aField, SCH_COMPONENT* aComponent, int aCtl );
private:
NET_INDEX_MAP m_netMap;

View File

@ -111,9 +111,9 @@ void SIM_PLOT_FRAME::StartSimulation()
m_simulator->Init();
NETLIST_OBJECT_LIST* net_atoms = m_schematicFrame->BuildNetListBase();
m_exporter = new NETLIST_EXPORTER_PSPICE( net_atoms, Prj().SchLibs(), Prj().SchSearchS() );
STRING_FORMATTER formatter;
m_exporter = new NETLIST_EXPORTER_PSPICE( net_atoms, Prj().SchLibs(), Prj().SchSearchS() );
m_exporter->Format( &formatter, GNL_ALL );
m_simulator->LoadNetlist( formatter.GetString() );
m_simulator->Run();