Spice simulator and netlist generation: use only netnames (outdated option "use net numbers as net names removed" removed)

netlist generation: remove option "replace IC and U ref by X" (broken option).
All component refs are now prefixed by the Spice_Primitive field value.
Therefore the netlist used by Kicad ngsipce simulator and the spice netlist generated from dialog are the same.
(note: like previously, forbidden chars like ( and ) are replaced by _ in netnames)
This commit is contained in:
jean-pierre charras 2016-09-16 20:36:19 +02:00
parent c689c8cc3f
commit cd94da7972
10 changed files with 72 additions and 96 deletions

View File

@ -3,7 +3,7 @@
*
* Copyright (C) 2013-2015 Jean-Pierre Charras, jp.charras@wanadoo.fr
* Copyright (C) 2013-2015 Wayne Stambaugh <stambaughw@verizon.net>
* Copyright (C) 1992-2015 KiCad Developers, see AUTHORS.txt for contributors.
* Copyright (C) 1992-2016 KiCad Developers, see AUTHORS.txt for contributors.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
@ -78,8 +78,8 @@ class NETLIST_PAGE_DIALOG : public wxPanel
public:
NETLIST_TYPE_ID m_IdNetType;
wxCheckBox* m_IsCurrentFormat;
wxCheckBox* m_AddSubPrefix;
wxCheckBox* m_SpiceUseNetcodeAsNetname;
// opt to reformat passive component values (e.g. 1M -> 1Meg):
wxCheckBox* m_SpiceAjustPassiveValues;
wxTextCtrl* m_CommandStringCtrl;
wxTextCtrl* m_TitleStringCtrl;
wxButton* m_ButtonCancel;
@ -117,11 +117,16 @@ public:
/* Dialog frame for creating netlists */
class NETLIST_DIALOG : public NETLIST_DIALOG_BASE
{
friend class NETLIST_PAGE_DIALOG;
public:
SCH_EDIT_FRAME* m_Parent;
wxString m_NetFmtName;
NETLIST_PAGE_DIALOG* m_PanelNetType[4 + CUSTOMPANEL_COUNTMAX];
protected:
bool m_asFormatSelected;
private:
wxConfigBase* m_config;
@ -140,7 +145,6 @@ private:
void GenNetlist( wxCommandEvent& event );
void RunSimulator( wxCommandEvent& event );
void NetlistUpdateOpt();
void OnCancelClick( wxCommandEvent& event );
void OnNetlistTypeSelection( wxNotebookEvent& event );
void SelectDefaultNetlistType( wxCommandEvent& event );
@ -216,7 +220,6 @@ private:
* Validate info relative to a new netlist plugin
*/
void OnOKClick( wxCommandEvent& event );
void OnCancelClick( wxCommandEvent& event );
/*
* Browse plugin files, and set m_CommandStringCtrl field
@ -262,21 +265,13 @@ NETLIST_PAGE_DIALOG::NETLIST_PAGE_DIALOG( wxNotebook* parent,
m_CommandStringCtrl = NULL;
m_TitleStringCtrl = NULL;
m_IsCurrentFormat = NULL;
m_AddSubPrefix = NULL;
m_SpiceUseNetcodeAsNetname = NULL;
m_SpiceAjustPassiveValues = NULL;
m_ButtonCancel = NULL;
wxString netfmtName = ((NETLIST_DIALOG*)parent->GetParent())->m_NetFmtName;
bool selected = m_pageNetFmtName == netfmtName;
// PCBNEW Format is a special type:
if( id_NetType == NET_TYPE_PCBNEW )
{
selected = true;
}
parent->AddPage( this, title, selected );
wxBoxSizer* MainBoxSizer = new wxBoxSizer( wxVERTICAL );
@ -300,6 +295,9 @@ NETLIST_PAGE_DIALOG::NETLIST_PAGE_DIALOG( wxNotebook* parent,
_( "Default format" ) );
m_LeftBoxSizer->Add( m_IsCurrentFormat, 0, wxGROW | wxALL, 5 );
m_IsCurrentFormat->SetValue( selected );
if( selected )
((NETLIST_DIALOG*)parent->GetParent())->m_asFormatSelected = true;
}
@ -321,6 +319,7 @@ NETLIST_DIALOG::NETLIST_DIALOG( SCH_EDIT_FRAME* parent ) :
}
// Add notebook pages:
m_asFormatSelected = false; // Will be set to true, if a format is selected
// Add Panel FORMAT PCBNEW
m_PanelNetType[PANELPCBNEW] =
@ -343,6 +342,9 @@ NETLIST_DIALOG::NETLIST_DIALOG( SCH_EDIT_FRAME* parent ) :
// Add custom panels:
InstallCustomPages();
if( !m_asFormatSelected )
m_PanelNetType[PANELPCBNEW]->m_IsCurrentFormat->SetValue( true );
SetDefaultItem( m_buttonNetlist );
FixOSXCancelButtonIssue();
@ -380,15 +382,12 @@ void NETLIST_DIALOG::InstallPageSpice()
page = m_PanelNetType[PANELSPICE] =
new NETLIST_PAGE_DIALOG( m_NoteBook, title, NET_TYPE_SPICE );
page->m_AddSubPrefix = new wxCheckBox( page, ID_ADD_SUBCIRCUIT_PREFIX,
_( "Prefix references 'U' and 'IC' with 'X'" ) );
page->m_AddSubPrefix->SetValue( m_Parent->GetSpiceAddReferencePrefix() );
page->m_LeftBoxSizer->Add( page->m_AddSubPrefix, 0, wxGROW | wxALL, 5 );
page->m_SpiceUseNetcodeAsNetname = new wxCheckBox( page, ID_USE_NETCODE_AS_NETNAME,
_( "Use net number as net name" ) );
page->m_SpiceUseNetcodeAsNetname->SetValue( m_Parent->GetSpiceUseNetcodeAsNetname() );
page->m_LeftBoxSizer->Add( page->m_SpiceUseNetcodeAsNetname, 0, wxGROW | wxALL, 5 );
page->m_SpiceAjustPassiveValues = new wxCheckBox( page, ID_USE_NETCODE_AS_NETNAME,
_( "Reformat passive component values" ) );
page->m_SpiceAjustPassiveValues->SetToolTip(
_( "Reformat passive component values e.g. 1M -> 1Meg" ) );
page->m_SpiceAjustPassiveValues->SetValue( m_Parent->GetSpiceAjustPassiveValues() );
page->m_LeftBoxSizer->Add( page->m_SpiceAjustPassiveValues, 0, wxGROW | wxALL, 5 );
page->m_LowBoxSizer->Add( new wxStaticText( page, -1, _( "Simulator command:" ) ), 0,
wxGROW | wxLEFT | wxRIGHT | wxTOP, 5 );
@ -515,8 +514,7 @@ void NETLIST_DIALOG::NetlistUpdateOpt()
{
int ii;
m_Parent->SetSpiceAddReferencePrefix( m_PanelNetType[PANELSPICE]->m_AddSubPrefix->IsChecked() );
m_Parent->SetSpiceUseNetcodeAsNetname( m_PanelNetType[PANELSPICE]->m_SpiceUseNetcodeAsNetname->IsChecked() );
m_Parent->SetSpiceAjustPassiveValues( m_PanelNetType[PANELSPICE]->m_SpiceAjustPassiveValues->IsChecked() );
m_Parent->SetSimulatorCommand( m_PanelNetType[PANELSPICE]->m_CommandStringCtrl->GetValue() );
m_Parent->SetNetListFormatName( wxEmptyString );
@ -554,11 +552,8 @@ void NETLIST_DIALOG::GenNetlist( wxCommandEvent& event )
{
case NET_TYPE_SPICE:
// Set spice netlist options:
if( currPage->m_AddSubPrefix->GetValue() )
netlist_opt |= NET_USE_X_PREFIX;
if( currPage->m_SpiceUseNetcodeAsNetname->GetValue() )
netlist_opt |= NET_USE_NETCODES_AS_NETNAMES;
if( currPage->m_SpiceAjustPassiveValues->GetValue() )
netlist_opt |= NET_ADJUST_PASSIVE_VALS;
break;
case NET_TYPE_CADSTAR:
@ -651,12 +646,6 @@ bool NETLIST_DIALOG::FilenamePrms( NETLIST_TYPE_ID aNetTypeId,
}
void NETLIST_DIALOG::OnCancelClick( wxCommandEvent& event )
{
EndModal( wxID_CANCEL );
}
void NETLIST_DIALOG::RunSimulator( wxCommandEvent& event )
{
wxFileName fn;
@ -682,11 +671,8 @@ void NETLIST_DIALOG::RunSimulator( wxCommandEvent& event )
// Set spice netlist options:
unsigned netlist_opt = 0;
if( currPage->m_AddSubPrefix && currPage->m_AddSubPrefix->GetValue() )
netlist_opt |= NET_USE_X_PREFIX;
if( currPage->m_SpiceUseNetcodeAsNetname && currPage->m_SpiceUseNetcodeAsNetname->GetValue() )
netlist_opt |= NET_USE_NETCODES_AS_NETNAMES;
if( currPage->m_SpiceAjustPassiveValues && currPage->m_SpiceAjustPassiveValues->GetValue() )
netlist_opt |= NET_ADJUST_PASSIVE_VALS;
if( ! m_Parent->CreateNetlist( currPage->m_IdNetType, fn.GetFullPath(),
netlist_opt ) )
@ -824,12 +810,6 @@ void NETLIST_DIALOG_ADD_PLUGIN::OnOKClick( wxCommandEvent& event )
}
void NETLIST_DIALOG_ADD_PLUGIN::OnCancelClick( wxCommandEvent& event )
{
EndModal( wxID_CANCEL );
}
void NETLIST_DIALOG_ADD_PLUGIN::OnBrowsePlugins( wxCommandEvent& event )
{
wxString FullFileName, Mask, Path;

View File

@ -1,5 +1,5 @@
///////////////////////////////////////////////////////////////////////////
// C++ code generated with wxFormBuilder (version Mar 28 2015)
// C++ code generated with wxFormBuilder (version Sep 8 2016)
// http://www.wxformbuilder.org/
//
// PLEASE DO "NOT" EDIT THIS FILE!
@ -72,7 +72,6 @@ NETLIST_DIALOG_BASE::NETLIST_DIALOG_BASE( wxWindow* parent, wxWindowID id, const
bMainSizer->Add( m_staticTextDefaultFN, 0, wxTOP|wxRIGHT|wxLEFT, 5 );
m_textCtrlDefaultFileName = new wxTextCtrl( this, wxID_ANY, wxEmptyString, wxDefaultPosition, wxDefaultSize, wxTE_READONLY );
m_textCtrlDefaultFileName->SetMaxLength( 0 );
bMainSizer->Add( m_textCtrlDefaultFileName, 0, wxEXPAND|wxBOTTOM|wxRIGHT|wxLEFT, 5 );
@ -107,7 +106,6 @@ NETLIST_DIALOG_ADD_PLUGIN_BASE::NETLIST_DIALOG_ADD_PLUGIN_BASE( wxWindow* parent
bSizerLeft->Add( m_staticTextCmd, 0, wxTOP|wxRIGHT|wxLEFT, 5 );
m_textCtrlCommand = new wxTextCtrl( this, wxID_ANY, wxEmptyString, wxDefaultPosition, wxDefaultSize, 0 );
m_textCtrlCommand->SetMaxLength( 0 );
m_textCtrlCommand->SetMinSize( wxSize( 300,-1 ) );
bSizerLeft->Add( m_textCtrlCommand, 0, wxEXPAND|wxBOTTOM|wxRIGHT|wxLEFT, 5 );
@ -117,7 +115,6 @@ NETLIST_DIALOG_ADD_PLUGIN_BASE::NETLIST_DIALOG_ADD_PLUGIN_BASE( wxWindow* parent
bSizerLeft->Add( m_staticTextName, 0, wxTOP|wxRIGHT|wxLEFT, 5 );
m_textCtrlName = new wxTextCtrl( this, wxID_ANY, wxEmptyString, wxDefaultPosition, wxDefaultSize, 0 );
m_textCtrlName->SetMaxLength( 0 );
bSizerLeft->Add( m_textCtrlName, 0, wxEXPAND|wxBOTTOM|wxRIGHT|wxLEFT, 5 );

View File

@ -1,5 +1,5 @@
///////////////////////////////////////////////////////////////////////////
// C++ code generated with wxFormBuilder (version Mar 28 2015)
// C++ code generated with wxFormBuilder (version Sep 8 2016)
// http://www.wxformbuilder.org/
//
// PLEASE DO "NOT" EDIT THIS FILE!

View File

@ -232,7 +232,7 @@ bool DIALOG_SPICE_MODEL::TransferDataToWindow()
m_fieldsTmp[idx] = fieldIt->GetText();
else
m_fieldsTmp[idx] = NETLIST_EXPORTER_PSPICE::GetSpiceFieldDefVal( (SPICE_FIELD) idx, &m_component,
NET_USE_X_PREFIX | NET_ADJUST_INCLUDE_PATHS | NET_ADJUST_PASSIVE_VALS );
NET_ADJUST_INCLUDE_PATHS | NET_ADJUST_PASSIVE_VALS );
}
// Analyze the component fields to fill out the dialog

View File

@ -419,10 +419,8 @@ PARAM_CFG_ARRAY& SCH_EDIT_FRAME::GetProjectFileParametersList()
m_projectFileParams.push_back( new PARAM_CFG_WXSTRING( wxT( "NetFmtName" ),
&m_netListFormat) );
m_projectFileParams.push_back( new PARAM_CFG_BOOL( wxT( "SpiceForceRefPrefix" ),
&m_spiceNetlistAddReferencePrefix, false ) );
m_projectFileParams.push_back( new PARAM_CFG_BOOL( wxT( "SpiceUseNetNumbers" ),
&m_spiceNetlistUseNetcodeAsNetname, false ) );
m_projectFileParams.push_back( new PARAM_CFG_BOOL( wxT( "SpiceAjustPassiveValues" ),
&m_spiceAjustPassiveValues, false ) );
m_projectFileParams.push_back( new PARAM_CFG_INT( wxT( "LabSize" ),
&s_defaultTextSize,

View File

@ -46,11 +46,21 @@ bool NETLIST_EXPORTER_PSPICE::WriteNetlist( const wxString& aOutFileName, unsign
return Format( &outputFile, aNetlistOptions );
}
void NETLIST_EXPORTER_PSPICE::ReplaceForbiddenChars( wxString &aNetName )
{
// some chars are not accepted in netnames in spice netlists, because they are separators
// they are replaced an underscore or some other allowed char.
// Note: this is a static function
aNetName.Replace( "(", "_" );
aNetName.Replace( ")", "_" );
}
bool NETLIST_EXPORTER_PSPICE::Format( OUTPUTFORMATTER* aFormatter, unsigned aCtl )
{
// Netlist options
const bool useNetcodeAsNetName = aCtl & NET_USE_NETCODES_AS_NETNAMES;
const bool useNetcodeAsNetName = false;//aCtl & NET_USE_NETCODES_AS_NETNAMES;
if( !ProcessNetlist( aCtl ) )
return false;
@ -66,17 +76,15 @@ bool NETLIST_EXPORTER_PSPICE::Format( OUTPUTFORMATTER* aFormatter, unsigned aCtl
{
// Look for the library in known search locations
full_path = m_paths->FindValidPath( lib );
if( full_path.IsEmpty() )
{
DisplayError( NULL, wxString::Format( _( "Could not find library file %s" ), lib ) );
full_path = lib;
}
}
if( full_path.IsEmpty() )
{
DisplayError( NULL, wxString::Format( wxT( "Could not find library file %s" ), lib ) );
return false;
}
else
{
aFormatter->Print( 0, ".include \"%s\"\n", (const char*) full_path.c_str() );
}
aFormatter->Print( 0, ".include \"%s\"\n", (const char*) full_path.c_str() );
}
for( const auto& item : m_spiceItems )
@ -128,9 +136,8 @@ bool NETLIST_EXPORTER_PSPICE::Format( OUTPUTFORMATTER* aFormatter, unsigned aCtl
{
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
ReplaceForbiddenChars( netName );
if( netName.IsEmpty() )
netName = wxT( "?" );
@ -167,12 +174,7 @@ wxString NETLIST_EXPORTER_PSPICE::GetSpiceFieldDefVal( SPICE_FIELD aField,
case SF_PRIMITIVE:
{
const wxString& refName = aComponent->GetField( REFERENCE )->GetText();
// Convert ICs to subcircuits
if( aCtl & NET_USE_X_PREFIX && ( refName.StartsWith( "IC" ) || refName.StartsWith( "U" ) ) )
return wxString( "X" );
else
return refName.GetChar( 0 );
return refName.GetChar( 0 );
break;
}

View File

@ -34,8 +34,6 @@ 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)
NET_ALL_FLAGS = 0xffff
@ -141,6 +139,15 @@ public:
*/
bool ProcessNetlist( unsigned aCtl );
/**
* @brief some chars are not accepted in netnames in spice netlists.
* for instance '(' and ')'
* ReplaceForbiddenChars replace these chars by an underscore.
* @param aNetName = the netname to modify.
*/
static void ReplaceForbiddenChars( wxString& aNetName );
/**
* @brief Returns a map of circuit nodes to net names.
*/

View File

@ -358,8 +358,7 @@ SCH_EDIT_FRAME::SCH_EDIT_FRAME( KIWAY* aKiway, wxWindow* aParent ):
m_hasAutoSave = true;
SetForceHVLines( true );
SetSpiceAddReferencePrefix( false );
SetSpiceUseNetcodeAsNetname( false );
SetSpiceAjustPassiveValues( false );
// Give an icon
wxIcon icon;

View File

@ -166,11 +166,8 @@ private:
/// The name of the format to use when generating a net list.
wxString m_netListFormat;
/// Add X prefix to component references when generating spice net lists.
bool m_spiceNetlistAddReferencePrefix;
/// Use netcodes (net number) as net names when generating spice net lists.
bool m_spiceNetlistUseNetcodeAsNetname;
bool m_spiceAjustPassiveValues;
/* these are PROJECT specific, not schematic editor specific
wxString m_userLibraryPath;
@ -250,13 +247,9 @@ public:
void SetNetListFormatName( const wxString& aFormat ) { m_netListFormat = aFormat; }
bool GetSpiceAddReferencePrefix() const { return m_spiceNetlistAddReferencePrefix; }
bool GetSpiceAjustPassiveValues() const { return m_spiceAjustPassiveValues; }
void SetSpiceAddReferencePrefix( bool aEnable ) { m_spiceNetlistAddReferencePrefix = aEnable; }
bool GetSpiceUseNetcodeAsNetname() const { return m_spiceNetlistUseNetcodeAsNetname; }
void SetSpiceUseNetcodeAsNetname( bool aEnable ) { m_spiceNetlistUseNetcodeAsNetname = aEnable; }
void SetSpiceAjustPassiveValues( bool aEnable ) { m_spiceAjustPassiveValues = aEnable; }
/* These are PROJECT specific, not schematic editor specific
wxString GetUserLibraryPath() const { return m_userLibraryPath; }

View File

@ -35,13 +35,13 @@ wxString NETLIST_EXPORTER_PSPICE_SIM::GetSpiceVector( const wxString& aName, SIM
if( aType & SPT_VOLTAGE )
{
const auto& netMapping = GetNetIndexMap();
auto it = netMapping.find( aName );
// Spice netlist netnames does not accept some chars, whicyh are replaced
// by eeschema netlist generator.
// Replace these forbidden chars to find the actual spice net name
wxString spicenet = aName;
NETLIST_EXPORTER_PSPICE::ReplaceForbiddenChars( spicenet );
if( it == netMapping.end() )
return "";
return wxString::Format( "V(%d)", it->second );
return wxString::Format( "V(%s)", spicenet.GetData() );
}
else if( aType & SPT_CURRENT )