2016-08-11 12:41:40 +00:00
|
|
|
/*
|
|
|
|
* This program source code file is part of KiCad, a free EDA CAD application.
|
|
|
|
*
|
2021-06-10 14:10:55 +00:00
|
|
|
* Copyright (C) 2020-2021 KiCad Developers, see AUTHORS.txt for contributors.
|
2017-10-26 17:54:53 +00:00
|
|
|
* Copyright (C) 2016-2017 CERN
|
2016-08-11 12:41:40 +00:00
|
|
|
* @author Maciej Suminski <maciej.suminski@cern.ch>
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU General Public License
|
2016-08-11 12:41:43 +00:00
|
|
|
* as published by the Free Software Foundation; either version 3
|
2016-08-11 12:41:40 +00:00
|
|
|
* of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, you may find one here:
|
2016-08-11 12:41:43 +00:00
|
|
|
* https://www.gnu.org/licenses/gpl-3.0.html
|
|
|
|
* or you may search the http://www.gnu.org website for the version 3 license,
|
2016-08-11 12:41:40 +00:00
|
|
|
* or you may write to the Free Software Foundation, Inc.,
|
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
|
|
|
|
*/
|
|
|
|
|
2017-11-12 00:31:38 +00:00
|
|
|
#include "wildcards_and_files_ext.h"
|
2016-08-11 12:41:40 +00:00
|
|
|
#include "dialog_spice_model.h"
|
2016-08-11 12:41:43 +00:00
|
|
|
|
|
|
|
#include <sim/spice_value.h>
|
2016-08-11 12:41:56 +00:00
|
|
|
#include <confirm.h>
|
2016-08-11 12:42:08 +00:00
|
|
|
#include <project.h>
|
2016-08-11 12:41:40 +00:00
|
|
|
|
2020-10-24 14:45:37 +00:00
|
|
|
#include <wx/textfile.h>
|
2016-08-11 12:41:40 +00:00
|
|
|
#include <wx/tokenzr.h>
|
2018-07-03 13:28:59 +00:00
|
|
|
#include <wx/wupdlock.h>
|
2021-05-01 07:50:29 +00:00
|
|
|
#include <wx/filedlg.h>
|
|
|
|
|
2016-08-11 12:41:40 +00:00
|
|
|
|
2018-09-22 20:32:43 +00:00
|
|
|
#include <cctype>
|
2019-06-25 23:39:58 +00:00
|
|
|
#include <cstring>
|
2018-09-22 20:32:43 +00:00
|
|
|
|
2016-08-11 12:41:40 +00:00
|
|
|
// Helper function to shorten conditions
|
|
|
|
static bool empty( const wxTextCtrl* aCtrl )
|
|
|
|
{
|
|
|
|
return aCtrl->GetValue().IsEmpty();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Function to sort PWL values list
|
2021-06-10 14:10:55 +00:00
|
|
|
static int wxCALLBACK comparePwlValues( wxIntPtr aItem1, wxIntPtr aItem2,
|
|
|
|
wxIntPtr WXUNUSED( aSortData ) )
|
2016-08-11 12:41:40 +00:00
|
|
|
{
|
|
|
|
float* t1 = reinterpret_cast<float*>( &aItem1 );
|
|
|
|
float* t2 = reinterpret_cast<float*>( &aItem2 );
|
|
|
|
|
|
|
|
if( *t1 > *t2 )
|
|
|
|
return 1;
|
|
|
|
|
|
|
|
if( *t1 < *t2 )
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-09-22 14:05:32 +00:00
|
|
|
// Structure describing a type of Spice model
|
|
|
|
struct SPICE_MODEL_INFO
|
|
|
|
{
|
|
|
|
SPICE_PRIMITIVE type; ///< Character identifying the model
|
|
|
|
wxString description; ///< Human-readable description
|
|
|
|
std::vector<std::string> keywords; ///< Keywords indicating the model
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
// Recognized model types
|
|
|
|
static const std::vector<SPICE_MODEL_INFO> modelTypes =
|
|
|
|
{
|
|
|
|
{ SP_DIODE, _( "Diode" ), { "d" } },
|
|
|
|
{ SP_BJT, _( "BJT" ), { "npn", "pnp" } },
|
|
|
|
{ SP_MOSFET, _( "MOSFET" ), { "nmos", "pmos", "vdmos" } },
|
2018-09-22 14:08:40 +00:00
|
|
|
{ SP_JFET, _( "JFET" ), { "njf", "pjf" } },
|
2018-09-22 14:05:32 +00:00
|
|
|
{ SP_SUBCKT, _( "Subcircuit" ), {} },
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2020-03-23 16:49:21 +00:00
|
|
|
enum TRRANDOM_TYPE
|
|
|
|
{
|
|
|
|
TRRANDOM_UNIFORM = 0,
|
|
|
|
TRRANDOM_GAUSSIAN = 1,
|
|
|
|
TRRANDOM_EXPONENTIAL = 2,
|
|
|
|
TRRANDOM_POISSON = 3,
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2018-09-22 14:05:32 +00:00
|
|
|
// Returns index of an entry in modelTypes array (above) corresponding to a Spice primitive
|
|
|
|
static int getModelTypeIdx( char aPrimitive )
|
|
|
|
{
|
|
|
|
const char prim = std::toupper( aPrimitive );
|
|
|
|
|
2018-09-16 13:47:19 +00:00
|
|
|
for( size_t i = 0; i < modelTypes.size(); ++i )
|
2018-09-22 14:05:32 +00:00
|
|
|
{
|
|
|
|
if( modelTypes[i].type == prim )
|
|
|
|
return i;
|
|
|
|
}
|
|
|
|
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-06-10 14:10:55 +00:00
|
|
|
DIALOG_SPICE_MODEL::DIALOG_SPICE_MODEL( wxWindow* aParent, SCH_SYMBOL& aSymbol,
|
|
|
|
SCH_FIELDS* aFields )
|
|
|
|
: DIALOG_SPICE_MODEL_BASE( aParent ), m_symbol( aSymbol ), m_schfields( aFields ),
|
2019-02-16 04:14:40 +00:00
|
|
|
m_libfields( nullptr ), m_useSchFields( true ),
|
2019-01-10 05:42:14 +00:00
|
|
|
m_spiceEmptyValidator( true ), m_notEmptyValidator( wxFILTER_EMPTY )
|
|
|
|
{
|
|
|
|
Init();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-06-10 14:10:55 +00:00
|
|
|
DIALOG_SPICE_MODEL::DIALOG_SPICE_MODEL( wxWindow* aParent, SCH_SYMBOL& aSymbol,
|
2020-12-15 15:06:19 +00:00
|
|
|
std::vector<LIB_FIELD>* aFields ) :
|
|
|
|
DIALOG_SPICE_MODEL_BASE( aParent ),
|
2021-06-10 14:10:55 +00:00
|
|
|
m_symbol( aSymbol ),
|
2020-12-15 15:06:19 +00:00
|
|
|
m_schfields( nullptr ),
|
|
|
|
m_libfields( aFields ),
|
|
|
|
m_useSchFields( false ),
|
|
|
|
m_spiceEmptyValidator( true ),
|
|
|
|
m_notEmptyValidator( wxFILTER_EMPTY )
|
2019-01-10 05:42:14 +00:00
|
|
|
{
|
|
|
|
Init();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void DIALOG_SPICE_MODEL::Init()
|
2016-08-11 12:41:40 +00:00
|
|
|
{
|
2016-08-11 12:41:56 +00:00
|
|
|
m_pasValue->SetValidator( m_spiceValidator );
|
|
|
|
|
2017-10-26 17:54:53 +00:00
|
|
|
m_modelType->SetValidator( m_notEmptyValidator );
|
2018-09-22 14:05:32 +00:00
|
|
|
m_modelType->Clear();
|
|
|
|
|
|
|
|
// Create a list of handled models
|
|
|
|
for( const auto& model : modelTypes )
|
|
|
|
m_modelType->Append( model.description );
|
|
|
|
|
2017-10-26 17:54:53 +00:00
|
|
|
m_modelName->SetValidator( m_notEmptyValidator );
|
2016-08-11 12:41:56 +00:00
|
|
|
|
|
|
|
m_genDc->SetValidator( m_spiceEmptyValidator );
|
|
|
|
m_genAcMag->SetValidator( m_spiceEmptyValidator );
|
|
|
|
m_genAcPhase->SetValidator( m_spiceEmptyValidator );
|
|
|
|
|
2016-08-11 12:42:09 +00:00
|
|
|
m_pulseInit->SetValidator( m_spiceEmptyValidator );
|
|
|
|
m_pulseNominal->SetValidator( m_spiceEmptyValidator );
|
2016-08-11 12:41:56 +00:00
|
|
|
m_pulseDelay->SetValidator( m_spiceEmptyValidator );
|
|
|
|
m_pulseRise->SetValidator( m_spiceEmptyValidator );
|
|
|
|
m_pulseFall->SetValidator( m_spiceEmptyValidator );
|
|
|
|
m_pulseWidth->SetValidator( m_spiceEmptyValidator );
|
|
|
|
m_pulsePeriod->SetValidator( m_spiceEmptyValidator );
|
|
|
|
|
2016-08-11 12:42:09 +00:00
|
|
|
m_sinOffset->SetValidator( m_spiceEmptyValidator );
|
|
|
|
m_sinAmplitude->SetValidator( m_spiceEmptyValidator );
|
2016-08-11 12:41:56 +00:00
|
|
|
m_sinFreq->SetValidator( m_spiceEmptyValidator );
|
|
|
|
m_sinDelay->SetValidator( m_spiceEmptyValidator );
|
|
|
|
m_sinDampFactor->SetValidator( m_spiceEmptyValidator );
|
|
|
|
|
2016-08-11 12:42:09 +00:00
|
|
|
m_expInit->SetValidator( m_spiceEmptyValidator );
|
|
|
|
m_expPulsed->SetValidator( m_spiceEmptyValidator );
|
2016-08-11 12:41:56 +00:00
|
|
|
m_expRiseDelay->SetValidator( m_spiceEmptyValidator );
|
|
|
|
m_expRiseConst->SetValidator( m_spiceEmptyValidator );
|
|
|
|
m_expFallDelay->SetValidator( m_spiceEmptyValidator );
|
|
|
|
m_expFallConst->SetValidator( m_spiceEmptyValidator );
|
|
|
|
|
2020-03-23 16:49:21 +00:00
|
|
|
m_fmOffset->SetValidator( m_spiceEmptyValidator );
|
|
|
|
m_fmAmplitude->SetValidator( m_spiceEmptyValidator );
|
|
|
|
m_fmFcarrier->SetValidator( m_spiceEmptyValidator );
|
|
|
|
m_fmModIndex->SetValidator( m_spiceEmptyValidator );
|
|
|
|
m_fmFsignal->SetValidator( m_spiceEmptyValidator );
|
|
|
|
m_fmPhaseC->SetValidator( m_spiceEmptyValidator );
|
|
|
|
m_fmPhaseS->SetValidator( m_spiceEmptyValidator );
|
|
|
|
|
|
|
|
m_amAmplitude->SetValidator( m_spiceEmptyValidator );
|
|
|
|
m_amOffset->SetValidator( m_spiceEmptyValidator );
|
|
|
|
m_amModulatingFreq->SetValidator( m_spiceEmptyValidator );
|
|
|
|
m_amCarrierFreq->SetValidator( m_spiceEmptyValidator );
|
|
|
|
m_amSignalDelay->SetValidator( m_spiceEmptyValidator );
|
|
|
|
m_amPhase->SetValidator( m_spiceEmptyValidator );
|
|
|
|
|
|
|
|
m_rnTS->SetValidator( m_spiceEmptyValidator );
|
|
|
|
m_rnTD->SetValidator( m_spiceEmptyValidator );
|
|
|
|
m_rnParam1->SetValidator( m_spiceEmptyValidator );
|
|
|
|
m_rnParam2->SetValidator( m_spiceEmptyValidator );
|
|
|
|
|
2016-08-11 12:41:40 +00:00
|
|
|
m_pwlTimeCol = m_pwlValList->AppendColumn( "Time [s]", wxLIST_FORMAT_LEFT, 100 );
|
|
|
|
m_pwlValueCol = m_pwlValList->AppendColumn( "Value [V/A]", wxLIST_FORMAT_LEFT, 100 );
|
|
|
|
|
|
|
|
m_sdbSizerOK->SetDefault();
|
2020-03-23 16:49:21 +00:00
|
|
|
|
2020-10-20 19:51:16 +00:00
|
|
|
m_staticTextF1->SetLabel( wxS( "f" ) );
|
|
|
|
m_staticTextP1->SetLabel( wxS( "p" ) );
|
|
|
|
m_staticTextN1->SetLabel( wxS( "n" ) );
|
|
|
|
m_staticTextU1->SetLabel( wxS( "u" ) );
|
|
|
|
m_staticTextM1->SetLabel( wxS( "m" ) );
|
|
|
|
m_staticTextK1->SetLabel( wxS( "k" ) );
|
|
|
|
m_staticTextMeg1->SetLabel( wxS( "meg" ) );
|
|
|
|
m_staticTextG1->SetLabel( wxS( "g" ) );
|
|
|
|
m_staticTextT1->SetLabel( wxS( "t" ) );
|
|
|
|
|
|
|
|
m_staticTextF2->SetLabel( wxS( "femto" ) );
|
|
|
|
m_staticTextP2->SetLabel( wxS( "pico" ) );
|
|
|
|
m_staticTextN2->SetLabel( wxS( "nano" ) );
|
|
|
|
m_staticTextU2->SetLabel( wxS( "micro" ) );
|
|
|
|
m_staticTextM2->SetLabel( wxS( "milli" ) );
|
|
|
|
m_staticTextK2->SetLabel( wxS( "kilo" ) );
|
|
|
|
m_staticTextMeg2->SetLabel( wxS( "mega" ) );
|
|
|
|
m_staticTextG2->SetLabel( wxS( "giga" ) );
|
|
|
|
m_staticTextT2->SetLabel( wxS( "terra" ) );
|
|
|
|
|
|
|
|
m_staticTextF3->SetLabel( wxS( "1e-15" ) );
|
|
|
|
m_staticTextP3->SetLabel( wxS( "1e-12" ) );
|
|
|
|
m_staticTextN3->SetLabel( wxS( "1e-9" ) );
|
|
|
|
m_staticTextU3->SetLabel( wxS( "1e-6" ) );
|
|
|
|
m_staticTextM3->SetLabel( wxS( "1e-3" ) );
|
|
|
|
m_staticTextK3->SetLabel( wxS( "1e3" ) );
|
|
|
|
m_staticTextMeg3->SetLabel( wxS( "1e6" ) );
|
|
|
|
m_staticTextG3->SetLabel( wxS( "1e9" ) );
|
|
|
|
m_staticTextT3->SetLabel( wxS( "1e12" ) );
|
|
|
|
|
2020-03-23 16:49:21 +00:00
|
|
|
// Hide pages that aren't fully implemented yet
|
|
|
|
// wxPanel::Hide() isn't enough on some platforms
|
|
|
|
m_powerNotebook->RemovePage( m_powerNotebook->FindPage( m_pwrTransNoise ) );
|
|
|
|
m_powerNotebook->RemovePage( m_powerNotebook->FindPage( m_pwrExtData ) );
|
2016-08-11 12:41:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool DIALOG_SPICE_MODEL::TransferDataFromWindow()
|
|
|
|
{
|
|
|
|
if( !DIALOG_SPICE_MODEL_BASE::TransferDataFromWindow() )
|
|
|
|
return false;
|
|
|
|
|
|
|
|
wxWindow* page = m_notebook->GetCurrentPage();
|
|
|
|
|
|
|
|
// Passive
|
|
|
|
if( page == m_passive )
|
|
|
|
{
|
2021-03-13 20:48:19 +00:00
|
|
|
if( !m_disabled->GetValue() && !m_passive->Validate() )
|
2016-08-11 12:41:56 +00:00
|
|
|
return false;
|
|
|
|
|
2016-08-11 12:41:40 +00:00
|
|
|
switch( m_pasType->GetSelection() )
|
|
|
|
{
|
2016-08-11 12:41:51 +00:00
|
|
|
case 0: m_fieldsTmp[SF_PRIMITIVE] = (char) SP_RESISTOR; break;
|
|
|
|
case 1: m_fieldsTmp[SF_PRIMITIVE] = (char) SP_CAPACITOR; break;
|
|
|
|
case 2: m_fieldsTmp[SF_PRIMITIVE] = (char) SP_INDUCTOR; break;
|
2016-08-11 12:41:40 +00:00
|
|
|
|
|
|
|
default:
|
|
|
|
wxASSERT_MSG( false, "Unhandled passive type" );
|
|
|
|
return false;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2016-08-11 12:41:51 +00:00
|
|
|
m_fieldsTmp[SF_MODEL] = m_pasValue->GetValue();
|
2016-08-11 12:41:40 +00:00
|
|
|
}
|
|
|
|
|
2017-10-26 17:54:53 +00:00
|
|
|
// Model
|
|
|
|
else if( page == m_model )
|
2016-08-11 12:41:40 +00:00
|
|
|
{
|
2017-10-26 17:54:53 +00:00
|
|
|
if( !m_model->Validate() )
|
2016-08-11 12:41:56 +00:00
|
|
|
return false;
|
|
|
|
|
2018-09-22 14:05:32 +00:00
|
|
|
int modelIdx = m_modelType->GetSelection();
|
2016-08-11 12:41:40 +00:00
|
|
|
|
2018-09-16 13:47:19 +00:00
|
|
|
if( modelIdx > 0 && modelIdx < (int)modelTypes.size() )
|
2018-09-22 14:05:32 +00:00
|
|
|
m_fieldsTmp[SF_PRIMITIVE] = static_cast<char>( modelTypes[modelIdx].type );
|
2016-08-11 12:41:40 +00:00
|
|
|
|
2017-10-26 17:54:53 +00:00
|
|
|
m_fieldsTmp[SF_MODEL] = m_modelName->GetValue();
|
2016-08-11 12:41:40 +00:00
|
|
|
|
2017-10-26 17:54:53 +00:00
|
|
|
if( !empty( m_modelLibrary ) )
|
|
|
|
m_fieldsTmp[SF_LIB_FILE] = m_modelLibrary->GetValue();
|
2016-08-11 12:41:40 +00:00
|
|
|
}
|
2021-06-10 14:10:55 +00:00
|
|
|
else if( page == m_power ) // Power source
|
2016-08-11 12:41:40 +00:00
|
|
|
{
|
|
|
|
wxString model;
|
|
|
|
|
|
|
|
if( !generatePowerSource( model ) )
|
|
|
|
return false;
|
|
|
|
|
2016-08-11 12:41:51 +00:00
|
|
|
m_fieldsTmp[SF_PRIMITIVE] = (char)( m_pwrType->GetSelection() ? SP_ISOURCE : SP_VSOURCE );
|
|
|
|
m_fieldsTmp[SF_MODEL] = model;
|
2016-08-11 12:41:40 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
wxASSERT_MSG( false, "Unhandled model type" );
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2016-08-11 12:41:51 +00:00
|
|
|
m_fieldsTmp[SF_ENABLED] = !m_disabled->GetValue() ? "Y" : "N"; // note bool inversion
|
|
|
|
m_fieldsTmp[SF_NODE_SEQUENCE] = m_nodeSeqCheck->IsChecked() ? m_nodeSeqVal->GetValue() : "";
|
2016-08-11 12:41:40 +00:00
|
|
|
|
|
|
|
// Apply the settings
|
2016-08-11 12:41:51 +00:00
|
|
|
for( int i = 0; i < SF_END; ++i )
|
2016-08-11 12:41:40 +00:00
|
|
|
{
|
|
|
|
if( m_fieldsTmp.count( (SPICE_FIELD) i ) > 0 && !m_fieldsTmp.at( i ).IsEmpty() )
|
|
|
|
{
|
2019-01-10 05:42:14 +00:00
|
|
|
if( m_useSchFields )
|
|
|
|
getSchField( i ).SetText( m_fieldsTmp[i] );
|
|
|
|
else
|
|
|
|
getLibField( i ).SetText( m_fieldsTmp[i] );
|
2016-08-11 12:41:40 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// Erase empty fields (having empty fields causes a warning in the properties dialog)
|
2021-06-10 14:10:55 +00:00
|
|
|
const wxString& spiceField =
|
|
|
|
NETLIST_EXPORTER_PSPICE::GetSpiceFieldName( (SPICE_FIELD ) i );
|
2016-08-11 12:41:40 +00:00
|
|
|
|
2019-01-10 05:42:14 +00:00
|
|
|
if( m_useSchFields )
|
2020-03-26 11:02:59 +00:00
|
|
|
{
|
2019-01-10 05:42:14 +00:00
|
|
|
m_schfields->erase( std::remove_if( m_schfields->begin(), m_schfields->end(),
|
2020-03-26 11:02:59 +00:00
|
|
|
[&]( const SCH_FIELD& f )
|
|
|
|
{
|
|
|
|
return f.GetName() == spiceField;
|
|
|
|
} ),
|
|
|
|
m_schfields->end() );
|
|
|
|
}
|
2019-01-10 05:42:14 +00:00
|
|
|
else
|
2020-03-26 11:02:59 +00:00
|
|
|
{
|
2019-01-10 05:42:14 +00:00
|
|
|
m_libfields->erase( std::remove_if( m_libfields->begin(), m_libfields->end(),
|
2020-03-26 11:02:59 +00:00
|
|
|
[&]( const LIB_FIELD& f )
|
|
|
|
{
|
|
|
|
return f.GetName() == spiceField;
|
|
|
|
} ),
|
|
|
|
m_libfields->end() );
|
|
|
|
}
|
2016-08-11 12:41:40 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool DIALOG_SPICE_MODEL::TransferDataToWindow()
|
|
|
|
{
|
|
|
|
const auto& spiceFields = NETLIST_EXPORTER_PSPICE::GetSpiceFields();
|
|
|
|
|
|
|
|
// Fill out the working buffer
|
|
|
|
for( unsigned int idx = 0; idx < spiceFields.size(); ++idx )
|
|
|
|
{
|
|
|
|
const wxString& spiceField = spiceFields[idx];
|
|
|
|
|
2021-06-10 14:10:55 +00:00
|
|
|
m_fieldsTmp[idx] = NETLIST_EXPORTER_PSPICE::GetSpiceFieldDefVal(
|
|
|
|
(SPICE_FIELD ) idx, &m_symbol,
|
|
|
|
NET_ADJUST_INCLUDE_PATHS | NET_ADJUST_PASSIVE_VALS );
|
2016-08-11 12:41:40 +00:00
|
|
|
|
|
|
|
// Do not modify the existing value, just add missing fields with default values
|
2019-01-12 14:49:45 +00:00
|
|
|
if( m_useSchFields && m_schfields )
|
2019-01-10 05:42:14 +00:00
|
|
|
{
|
2019-12-05 15:20:59 +00:00
|
|
|
for( const auto& field : *m_schfields )
|
2019-01-10 05:42:14 +00:00
|
|
|
{
|
|
|
|
if( field.GetName() == spiceField && !field.GetText().IsEmpty() )
|
|
|
|
{
|
|
|
|
m_fieldsTmp[idx] = field.GetText();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-06-10 14:10:55 +00:00
|
|
|
else if( m_libfields )
|
2019-01-10 05:42:14 +00:00
|
|
|
{
|
|
|
|
// TODO: There must be a good way to template out these repetitive calls
|
2020-02-16 12:51:44 +00:00
|
|
|
for( const LIB_FIELD& field : *m_libfields )
|
2019-01-10 05:42:14 +00:00
|
|
|
{
|
2020-03-26 11:02:59 +00:00
|
|
|
if( field.GetName() == spiceField && !field.GetText().IsEmpty() )
|
2019-01-10 05:42:14 +00:00
|
|
|
{
|
|
|
|
m_fieldsTmp[idx] = field.GetText();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-08-11 12:41:40 +00:00
|
|
|
}
|
|
|
|
|
2021-06-14 18:00:08 +00:00
|
|
|
// Analyze the symbol fields to fill out the dialog.
|
2020-05-23 07:48:05 +00:00
|
|
|
unsigned int primitive = toupper( m_fieldsTmp[SF_PRIMITIVE][0] );
|
2016-08-11 12:41:40 +00:00
|
|
|
|
|
|
|
switch( primitive )
|
|
|
|
{
|
|
|
|
case SP_RESISTOR:
|
|
|
|
case SP_CAPACITOR:
|
|
|
|
case SP_INDUCTOR:
|
|
|
|
m_notebook->SetSelection( m_notebook->FindPage( m_passive ) );
|
|
|
|
m_pasType->SetSelection( primitive == SP_RESISTOR ? 0
|
|
|
|
: primitive == SP_CAPACITOR ? 1
|
|
|
|
: primitive == SP_INDUCTOR ? 2
|
|
|
|
: -1 );
|
2016-08-11 12:41:51 +00:00
|
|
|
m_pasValue->SetValue( m_fieldsTmp[SF_MODEL] );
|
2016-08-11 12:41:40 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case SP_DIODE:
|
|
|
|
case SP_BJT:
|
|
|
|
case SP_MOSFET:
|
2018-09-22 14:08:40 +00:00
|
|
|
case SP_JFET:
|
2018-09-22 14:05:32 +00:00
|
|
|
case SP_SUBCKT:
|
2017-10-26 17:54:53 +00:00
|
|
|
m_notebook->SetSelection( m_notebook->FindPage( m_model ) );
|
2018-09-22 14:05:32 +00:00
|
|
|
m_modelType->SetSelection( getModelTypeIdx( primitive ) );
|
2017-10-26 17:54:53 +00:00
|
|
|
m_modelName->SetValue( m_fieldsTmp[SF_MODEL] );
|
|
|
|
m_modelLibrary->SetValue( m_fieldsTmp[SF_LIB_FILE] );
|
2016-08-11 12:41:40 +00:00
|
|
|
|
2017-10-26 17:54:53 +00:00
|
|
|
if( !empty( m_modelLibrary ) )
|
2016-08-11 12:41:40 +00:00
|
|
|
{
|
2017-10-26 17:54:53 +00:00
|
|
|
const wxString& libFile = m_modelLibrary->GetValue();
|
2016-08-11 12:41:51 +00:00
|
|
|
m_fieldsTmp[SF_LIB_FILE] = libFile;
|
2017-10-26 17:54:53 +00:00
|
|
|
loadLibrary( libFile );
|
2016-08-11 12:41:40 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case SP_VSOURCE:
|
|
|
|
case SP_ISOURCE:
|
2016-08-11 12:41:51 +00:00
|
|
|
if( !parsePowerSource( m_fieldsTmp[SF_MODEL] ) )
|
2016-08-11 12:41:40 +00:00
|
|
|
return false;
|
|
|
|
|
|
|
|
m_notebook->SetSelection( m_notebook->FindPage( m_power ) );
|
|
|
|
m_pwrType->SetSelection( primitive == SP_ISOURCE ? 1 : 0 );
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
2016-08-11 12:41:47 +00:00
|
|
|
//wxASSERT_MSG( false, "Unhandled Spice primitive type" );
|
2016-08-11 12:41:40 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2016-08-11 12:41:51 +00:00
|
|
|
m_disabled->SetValue( !NETLIST_EXPORTER_PSPICE::StringToBool( m_fieldsTmp[SF_ENABLED] ) );
|
2016-08-11 12:41:40 +00:00
|
|
|
|
|
|
|
// Check if node sequence is different than the default one
|
2016-08-11 12:41:51 +00:00
|
|
|
if( m_fieldsTmp[SF_NODE_SEQUENCE]
|
2021-06-10 14:10:55 +00:00
|
|
|
!= NETLIST_EXPORTER_PSPICE::GetSpiceFieldDefVal( SF_NODE_SEQUENCE, &m_symbol, 0 ) )
|
2016-08-11 12:41:40 +00:00
|
|
|
{
|
|
|
|
m_nodeSeqCheck->SetValue( true );
|
2016-08-11 12:41:51 +00:00
|
|
|
m_nodeSeqVal->SetValue( m_fieldsTmp[SF_NODE_SEQUENCE] );
|
2016-08-11 12:41:40 +00:00
|
|
|
}
|
|
|
|
|
2020-05-23 07:48:05 +00:00
|
|
|
showPinOrderNote( primitive );
|
|
|
|
|
2016-08-11 12:41:40 +00:00
|
|
|
return DIALOG_SPICE_MODEL_BASE::TransferDataToWindow();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-05-23 07:48:05 +00:00
|
|
|
void DIALOG_SPICE_MODEL::showPinOrderNote( int aModelType )
|
|
|
|
{
|
|
|
|
// Display a note info about pin order, according to aModelType
|
|
|
|
wxString msg;
|
|
|
|
|
|
|
|
msg = _( "Symbol pin numbering don't always match the required SPICE pin order\n"
|
2020-05-26 15:17:29 +00:00
|
|
|
"Check the symbol and use \"Alternate node sequence\" to reorder the pins"
|
2020-05-23 07:48:05 +00:00
|
|
|
", if necessary" );
|
|
|
|
|
|
|
|
msg += '\n';
|
|
|
|
|
|
|
|
switch( aModelType )
|
|
|
|
{
|
|
|
|
case SP_DIODE:
|
|
|
|
msg += _( "For a Diode, pin order is anode, cathode" );
|
|
|
|
break;
|
|
|
|
|
|
|
|
case SP_BJT:
|
|
|
|
msg += _( "For a BJT, pin order is collector, base, emitter, substrate (optional)" );
|
|
|
|
break;
|
|
|
|
|
|
|
|
case SP_MOSFET:
|
2021-04-05 13:25:19 +00:00
|
|
|
msg += _( "For a MOSFET, pin order is drain, gate, source" );
|
2020-05-23 07:48:05 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case SP_JFET:
|
2021-04-05 13:25:19 +00:00
|
|
|
msg += _( "For a JFET, pin order is drain, gate, source" );
|
2020-05-23 07:48:05 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
m_stInfoNote->SetLabel( msg );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-08-11 12:41:40 +00:00
|
|
|
bool DIALOG_SPICE_MODEL::parsePowerSource( const wxString& aModel )
|
|
|
|
{
|
|
|
|
if( aModel.IsEmpty() )
|
|
|
|
return false;
|
|
|
|
|
|
|
|
wxStringTokenizer tokenizer( aModel, " ()" );
|
|
|
|
wxString tkn = tokenizer.GetNextToken().Lower();
|
|
|
|
|
2016-08-11 12:42:09 +00:00
|
|
|
while( tokenizer.HasMoreTokens() )
|
2016-08-11 12:41:40 +00:00
|
|
|
{
|
2016-08-11 12:42:09 +00:00
|
|
|
// Variables used for generic values processing (filling out wxTextCtrls in sequence)
|
|
|
|
bool genericProcessing = false;
|
|
|
|
unsigned int genericReqParamsCount = 0;
|
|
|
|
std::vector<wxTextCtrl*> genericControls;
|
|
|
|
|
2016-08-11 12:41:40 +00:00
|
|
|
if( tkn == "dc" )
|
|
|
|
{
|
2016-08-11 12:42:09 +00:00
|
|
|
// There might be an optional "dc" or "trans" directive, skip it
|
2016-08-11 12:41:40 +00:00
|
|
|
if( tkn == "dc" || tkn == "trans" )
|
|
|
|
tkn = tokenizer.GetNextToken().Lower();
|
|
|
|
|
|
|
|
// DC value
|
2016-08-11 12:42:09 +00:00
|
|
|
try
|
|
|
|
{
|
|
|
|
m_genDc->SetValue( SPICE_VALUE( tkn ).ToSpiceString() );
|
|
|
|
}
|
|
|
|
catch( ... )
|
|
|
|
{
|
2016-08-11 12:41:40 +00:00
|
|
|
return false;
|
2016-08-11 12:42:09 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else if( tkn == "ac" )
|
|
|
|
{
|
2016-08-11 12:41:40 +00:00
|
|
|
// AC magnitude
|
2016-08-11 12:42:09 +00:00
|
|
|
try
|
|
|
|
{
|
|
|
|
tkn = tokenizer.GetNextToken().Lower();
|
|
|
|
m_genAcMag->SetValue( SPICE_VALUE( tkn ).ToSpiceString() );
|
|
|
|
}
|
|
|
|
catch( ... )
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
2016-08-11 12:41:40 +00:00
|
|
|
|
2016-08-11 12:42:09 +00:00
|
|
|
// AC phase (optional)
|
|
|
|
try
|
|
|
|
{
|
|
|
|
tkn = tokenizer.GetNextToken().Lower();
|
|
|
|
m_genAcPhase->SetValue( SPICE_VALUE( tkn ).ToSpiceString() );
|
|
|
|
}
|
|
|
|
catch( ... )
|
|
|
|
{
|
|
|
|
continue; // perhaps another directive
|
|
|
|
}
|
2016-08-11 12:41:40 +00:00
|
|
|
}
|
|
|
|
else if( tkn == "pulse" )
|
|
|
|
{
|
|
|
|
m_powerNotebook->SetSelection( m_powerNotebook->FindPage( m_pwrPulse ) );
|
|
|
|
|
|
|
|
genericProcessing = true;
|
|
|
|
genericReqParamsCount = 2;
|
|
|
|
genericControls = { m_pulseInit, m_pulseNominal, m_pulseDelay,
|
|
|
|
m_pulseRise, m_pulseFall, m_pulseWidth, m_pulsePeriod };
|
|
|
|
}
|
|
|
|
else if( tkn == "sin" )
|
|
|
|
{
|
|
|
|
m_powerNotebook->SetSelection( m_powerNotebook->FindPage( m_pwrSin ) );
|
|
|
|
|
|
|
|
genericProcessing = true;
|
|
|
|
genericReqParamsCount = 2;
|
2021-06-10 14:10:55 +00:00
|
|
|
genericControls = { m_sinOffset, m_sinAmplitude, m_sinFreq, m_sinDelay,
|
|
|
|
m_sinDampFactor };
|
2016-08-11 12:41:40 +00:00
|
|
|
}
|
|
|
|
else if( tkn == "exp" )
|
|
|
|
{
|
|
|
|
m_powerNotebook->SetSelection( m_powerNotebook->FindPage( m_pwrExp ) );
|
|
|
|
|
|
|
|
genericProcessing = true;
|
|
|
|
genericReqParamsCount = 2;
|
|
|
|
genericControls = { m_expInit, m_expPulsed,
|
|
|
|
m_expRiseDelay, m_expRiseConst, m_expFallDelay, m_expFallConst };
|
|
|
|
}
|
|
|
|
else if( tkn == "pwl" )
|
|
|
|
{
|
|
|
|
m_powerNotebook->SetSelection( m_powerNotebook->FindPage( m_pwrPwl ) );
|
|
|
|
|
2016-08-11 12:42:09 +00:00
|
|
|
try
|
2016-08-11 12:41:40 +00:00
|
|
|
{
|
2016-08-11 12:42:09 +00:00
|
|
|
while( tokenizer.HasMoreTokens() )
|
|
|
|
{
|
|
|
|
tkn = tokenizer.GetNextToken();
|
|
|
|
SPICE_VALUE time( tkn );
|
2016-08-11 12:41:40 +00:00
|
|
|
|
2016-08-11 12:42:09 +00:00
|
|
|
tkn = tokenizer.GetNextToken();
|
|
|
|
SPICE_VALUE value( tkn );
|
2016-08-11 12:41:40 +00:00
|
|
|
|
2016-08-11 12:42:09 +00:00
|
|
|
addPwlValue( time.ToSpiceString(), value.ToSpiceString() );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
catch( ... )
|
|
|
|
{
|
|
|
|
return false;
|
2016-08-11 12:41:40 +00:00
|
|
|
}
|
|
|
|
}
|
2020-03-23 16:49:21 +00:00
|
|
|
else if( tkn == "sffm" )
|
|
|
|
{
|
|
|
|
m_powerNotebook->SetSelection( m_powerNotebook->FindPage( m_pwrFm ) );
|
2016-08-11 12:41:40 +00:00
|
|
|
|
2020-03-23 16:49:21 +00:00
|
|
|
genericProcessing = true;
|
|
|
|
genericReqParamsCount = 4;
|
|
|
|
genericControls = { m_fmOffset, m_fmAmplitude, m_fmFcarrier, m_fmModIndex, m_fmFsignal,
|
|
|
|
m_fmFsignal, m_fmPhaseC, m_fmPhaseS };
|
|
|
|
}
|
|
|
|
else if( tkn == "am" )
|
|
|
|
{
|
|
|
|
m_powerNotebook->SetSelection( m_powerNotebook->FindPage( m_pwrAm ) );
|
2016-08-11 12:41:40 +00:00
|
|
|
|
2020-03-23 16:49:21 +00:00
|
|
|
genericProcessing = true;
|
|
|
|
genericReqParamsCount = 5;
|
|
|
|
genericControls = { m_amAmplitude, m_amOffset, m_amModulatingFreq, m_amCarrierFreq,
|
|
|
|
m_amSignalDelay, m_amPhase };
|
|
|
|
}
|
|
|
|
else if( tkn == "trrandom" )
|
|
|
|
{
|
|
|
|
m_powerNotebook->SetSelection( m_powerNotebook->FindPage( m_pwrRandom ) );
|
|
|
|
|
|
|
|
// first token will configure drop-down list
|
|
|
|
if( !tokenizer.HasMoreTokens() )
|
|
|
|
return false;
|
|
|
|
|
|
|
|
tkn = tokenizer.GetNextToken().Lower();
|
|
|
|
long type;
|
|
|
|
if( !tkn.ToLong( &type ) )
|
|
|
|
return false;
|
|
|
|
|
|
|
|
m_rnType->SetSelection( type - 1 );
|
|
|
|
wxCommandEvent dummy;
|
|
|
|
onRandomSourceType( dummy );
|
|
|
|
|
|
|
|
// remaining parameters can be handled in generic way
|
|
|
|
genericProcessing = true;
|
|
|
|
genericReqParamsCount = 4;
|
|
|
|
genericControls = { m_rnTS, m_rnTD, m_rnParam1, m_rnParam2 };
|
|
|
|
}
|
2016-08-11 12:41:40 +00:00
|
|
|
else
|
|
|
|
{
|
|
|
|
// Unhandled power source type
|
2016-08-11 12:42:09 +00:00
|
|
|
wxASSERT_MSG( false, "Unhandled power source type" );
|
2016-08-11 12:41:40 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if( genericProcessing )
|
|
|
|
{
|
2016-08-11 12:42:09 +00:00
|
|
|
try
|
2016-08-11 12:41:40 +00:00
|
|
|
{
|
2016-08-11 12:42:09 +00:00
|
|
|
for( unsigned int i = 0; i < genericControls.size(); ++i )
|
|
|
|
{
|
|
|
|
// If there are no more tokens, let's check if we got at least required fields
|
|
|
|
if( !tokenizer.HasMoreTokens() )
|
|
|
|
return ( i >= genericReqParamsCount );
|
2016-08-11 12:41:40 +00:00
|
|
|
|
2016-08-11 12:42:09 +00:00
|
|
|
tkn = tokenizer.GetNextToken().Lower();
|
|
|
|
genericControls[i]->SetValue( SPICE_VALUE( tkn ).ToSpiceString() );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
catch( ... )
|
|
|
|
{
|
|
|
|
return false;
|
2016-08-11 12:41:40 +00:00
|
|
|
}
|
|
|
|
}
|
2016-08-11 12:42:09 +00:00
|
|
|
|
2019-08-20 17:22:30 +00:00
|
|
|
// Get the next token now, so if any of the branches catches an exception, try to
|
2016-08-11 12:42:09 +00:00
|
|
|
// process it in another branch
|
|
|
|
tkn = tokenizer.GetNextToken().Lower();
|
2016-08-11 12:41:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool DIALOG_SPICE_MODEL::generatePowerSource( wxString& aTarget ) const
|
|
|
|
{
|
2016-08-11 12:42:09 +00:00
|
|
|
wxString acdc, trans;
|
2016-08-11 12:41:40 +00:00
|
|
|
wxWindow* page = m_powerNotebook->GetCurrentPage();
|
2016-08-11 12:42:09 +00:00
|
|
|
bool useTrans = true; // shall we use the transient command part?
|
2016-08-11 12:41:40 +00:00
|
|
|
|
|
|
|
// Variables for generic processing
|
|
|
|
bool genericProcessing = false;
|
|
|
|
unsigned int genericReqParamsCount = 0;
|
|
|
|
std::vector<wxTextCtrl*> genericControls;
|
|
|
|
|
2016-08-11 12:42:09 +00:00
|
|
|
/// DC / AC section
|
|
|
|
// If SPICE_VALUE can be properly constructed, then it is a valid value
|
|
|
|
try
|
2016-08-11 12:41:40 +00:00
|
|
|
{
|
|
|
|
if( !empty( m_genDc ) )
|
2021-06-10 14:10:55 +00:00
|
|
|
acdc += wxString::Format( "dc %s ",
|
|
|
|
SPICE_VALUE( m_genDc->GetValue() ).ToSpiceString() );
|
2016-08-11 12:42:09 +00:00
|
|
|
}
|
|
|
|
catch( ... )
|
|
|
|
{
|
|
|
|
DisplayError( NULL, wxT( "Invalid DC value" ) );
|
|
|
|
return false;
|
|
|
|
}
|
2016-08-11 12:41:40 +00:00
|
|
|
|
2016-08-11 12:42:09 +00:00
|
|
|
try
|
|
|
|
{
|
2016-08-11 12:41:40 +00:00
|
|
|
if( !empty( m_genAcMag ) )
|
|
|
|
{
|
2021-06-10 14:10:55 +00:00
|
|
|
acdc += wxString::Format( "ac %s ",
|
|
|
|
SPICE_VALUE( m_genAcMag->GetValue() ).ToSpiceString() );
|
2016-08-11 12:41:40 +00:00
|
|
|
|
|
|
|
if( !empty( m_genAcPhase ) )
|
2021-06-10 14:10:55 +00:00
|
|
|
acdc += wxString::Format( "%s ",
|
|
|
|
SPICE_VALUE( m_genAcPhase->GetValue() ).ToSpiceString() );
|
2016-08-11 12:41:40 +00:00
|
|
|
}
|
|
|
|
}
|
2016-08-11 12:42:09 +00:00
|
|
|
catch( ... )
|
|
|
|
{
|
|
|
|
DisplayError( NULL, wxT( "Invalid AC magnitude or phase" ) );
|
|
|
|
return false;
|
|
|
|
}
|
2016-08-11 12:41:40 +00:00
|
|
|
|
2016-08-11 12:42:09 +00:00
|
|
|
/// Transient section
|
|
|
|
if( page == m_pwrPulse )
|
2016-08-11 12:41:40 +00:00
|
|
|
{
|
2016-08-11 12:41:56 +00:00
|
|
|
if( !m_pwrPulse->Validate() )
|
|
|
|
return false;
|
|
|
|
|
2016-08-11 12:41:40 +00:00
|
|
|
genericProcessing = true;
|
2020-03-23 16:49:21 +00:00
|
|
|
trans += "pulse(";
|
2016-08-11 12:41:40 +00:00
|
|
|
genericReqParamsCount = 2;
|
|
|
|
genericControls = { m_pulseInit, m_pulseNominal, m_pulseDelay,
|
|
|
|
m_pulseRise, m_pulseFall, m_pulseWidth, m_pulsePeriod };
|
|
|
|
}
|
|
|
|
else if( page == m_pwrSin )
|
|
|
|
{
|
2016-08-11 12:41:56 +00:00
|
|
|
if( !m_pwrSin->Validate() )
|
|
|
|
return false;
|
|
|
|
|
2016-08-11 12:41:40 +00:00
|
|
|
genericProcessing = true;
|
2020-03-23 16:49:21 +00:00
|
|
|
trans += "sin(";
|
2016-08-11 12:41:40 +00:00
|
|
|
genericReqParamsCount = 2;
|
|
|
|
genericControls = { m_sinOffset, m_sinAmplitude, m_sinFreq, m_sinDelay, m_sinDampFactor };
|
|
|
|
}
|
|
|
|
else if( page == m_pwrExp )
|
|
|
|
{
|
2016-08-11 12:41:56 +00:00
|
|
|
if( !m_pwrExp->Validate() )
|
|
|
|
return false;
|
|
|
|
|
2016-08-11 12:41:40 +00:00
|
|
|
genericProcessing = true;
|
2020-03-23 16:49:21 +00:00
|
|
|
trans += "exp(";
|
2016-08-11 12:41:40 +00:00
|
|
|
genericReqParamsCount = 2;
|
|
|
|
genericControls = { m_expInit, m_expPulsed,
|
|
|
|
m_expRiseDelay, m_expRiseConst, m_expFallDelay, m_expFallConst };
|
|
|
|
}
|
|
|
|
else if( page == m_pwrPwl )
|
|
|
|
{
|
2016-08-11 12:42:09 +00:00
|
|
|
if( m_pwlValList->GetItemCount() > 0 )
|
2016-08-11 12:41:56 +00:00
|
|
|
{
|
2016-08-11 12:42:09 +00:00
|
|
|
trans += "pwl(";
|
2016-08-11 12:41:56 +00:00
|
|
|
|
2016-08-11 12:42:09 +00:00
|
|
|
for( int i = 0; i < m_pwlValList->GetItemCount(); ++i )
|
|
|
|
{
|
|
|
|
trans += wxString::Format( "%s %s ", m_pwlValList->GetItemText( i, m_pwlTimeCol ),
|
|
|
|
m_pwlValList->GetItemText( i, m_pwlValueCol ) );
|
|
|
|
}
|
2016-08-11 12:41:40 +00:00
|
|
|
|
2016-08-11 12:42:09 +00:00
|
|
|
trans.Trim();
|
|
|
|
trans += ")";
|
2016-08-11 12:41:40 +00:00
|
|
|
}
|
|
|
|
}
|
2020-03-23 16:49:21 +00:00
|
|
|
else if( page == m_pwrFm )
|
|
|
|
{
|
|
|
|
if( !m_pwrFm->Validate() )
|
|
|
|
return false;
|
2016-08-11 12:41:40 +00:00
|
|
|
|
2020-03-23 16:49:21 +00:00
|
|
|
genericProcessing = true;
|
|
|
|
trans += "sffm(";
|
|
|
|
genericReqParamsCount = 4;
|
|
|
|
genericControls = { m_fmOffset, m_fmAmplitude, m_fmFcarrier, m_fmModIndex, m_fmFsignal,
|
|
|
|
m_fmFsignal, m_fmPhaseC, m_fmPhaseS };
|
|
|
|
}
|
|
|
|
else if( page == m_pwrAm )
|
2016-08-11 12:41:40 +00:00
|
|
|
{
|
2020-03-23 16:49:21 +00:00
|
|
|
if( !m_pwrAm->Validate() )
|
|
|
|
return false;
|
2016-08-11 12:41:40 +00:00
|
|
|
|
2020-03-23 16:49:21 +00:00
|
|
|
genericProcessing = true;
|
|
|
|
trans += "am(";
|
|
|
|
genericReqParamsCount = 5;
|
|
|
|
genericControls = { m_amAmplitude, m_amOffset, m_amModulatingFreq, m_amCarrierFreq,
|
|
|
|
m_amSignalDelay, m_amPhase };
|
|
|
|
}
|
|
|
|
else if( page == m_pwrRandom )
|
|
|
|
{
|
|
|
|
if( !m_pwrRandom->Validate() )
|
|
|
|
return false;
|
|
|
|
|
|
|
|
// first parameter must be retrieved from drop-down list selection
|
|
|
|
trans += "trrandom(";
|
|
|
|
trans.Append( wxString::Format( wxT( "%i " ), ( m_rnType->GetSelection() + 1 ) ) );
|
|
|
|
|
|
|
|
genericProcessing = true;
|
|
|
|
genericReqParamsCount = 4;
|
|
|
|
genericControls = { m_rnTS, m_rnTD, m_rnParam1, m_rnParam2 };
|
|
|
|
}
|
|
|
|
if( genericProcessing )
|
|
|
|
{
|
2018-01-17 09:37:54 +00:00
|
|
|
auto first_empty = std::find_if( genericControls.begin(), genericControls.end(), empty );
|
|
|
|
auto first_not_empty = std::find_if( genericControls.begin(), genericControls.end(),
|
|
|
|
[]( const wxTextCtrl* c ){ return !empty( c ); } );
|
2016-08-11 12:41:56 +00:00
|
|
|
|
2018-01-17 09:37:54 +00:00
|
|
|
if( std::distance( first_not_empty, genericControls.end() ) == 0 )
|
2018-01-16 20:45:16 +00:00
|
|
|
{
|
|
|
|
// all empty
|
|
|
|
useTrans = false;
|
|
|
|
}
|
2021-06-10 14:10:55 +00:00
|
|
|
else if( std::distance( genericControls.begin(),
|
|
|
|
first_empty ) < (int)genericReqParamsCount )
|
2018-01-16 20:45:16 +00:00
|
|
|
{
|
|
|
|
DisplayError( nullptr,
|
2021-06-10 14:10:55 +00:00
|
|
|
wxString::Format( _( "You need to specify at least the "
|
|
|
|
"first %d parameters for the transient source" ),
|
|
|
|
genericReqParamsCount ) );
|
2016-08-11 12:41:40 +00:00
|
|
|
|
2018-01-16 20:45:16 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
else if( std::find_if_not( first_empty, genericControls.end(),
|
2021-06-10 14:10:55 +00:00
|
|
|
empty ) != genericControls.end() )
|
2018-01-16 20:45:16 +00:00
|
|
|
{
|
2021-06-10 14:10:55 +00:00
|
|
|
DisplayError( nullptr, _( "You cannot leave interleaved empty fields "
|
|
|
|
"when defining a transient source" ) );
|
2018-01-16 20:45:16 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2021-06-10 14:10:55 +00:00
|
|
|
std::for_each( genericControls.begin(), first_empty, [&trans] ( wxTextCtrl* ctrl ) {
|
2018-01-16 20:45:16 +00:00
|
|
|
trans += wxString::Format( "%s ", ctrl->GetValue() );
|
|
|
|
} );
|
2016-08-11 12:41:40 +00:00
|
|
|
}
|
|
|
|
|
2016-08-11 12:42:09 +00:00
|
|
|
trans.Trim();
|
|
|
|
trans += ")";
|
2016-08-11 12:41:40 +00:00
|
|
|
}
|
|
|
|
|
2016-08-11 12:42:09 +00:00
|
|
|
aTarget = acdc;
|
|
|
|
|
|
|
|
if( useTrans )
|
|
|
|
aTarget += trans;
|
|
|
|
|
2018-01-17 09:37:54 +00:00
|
|
|
// Remove whitespaces from left and right side
|
2016-08-11 12:42:09 +00:00
|
|
|
aTarget.Trim( false );
|
|
|
|
aTarget.Trim( true );
|
2016-08-11 12:41:40 +00:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-10-26 17:54:53 +00:00
|
|
|
void DIALOG_SPICE_MODEL::loadLibrary( const wxString& aFilePath )
|
2016-08-11 12:41:40 +00:00
|
|
|
{
|
2017-10-26 17:54:53 +00:00
|
|
|
wxString curModel = m_modelName->GetValue();
|
|
|
|
m_models.clear();
|
2016-08-11 12:42:19 +00:00
|
|
|
wxFileName filePath( aFilePath );
|
2017-10-26 17:49:57 +00:00
|
|
|
bool in_subckt = false; // flag indicating that the parser is inside a .subckt section
|
2016-08-11 12:42:19 +00:00
|
|
|
|
2017-10-26 21:59:56 +00:00
|
|
|
// Look for the file in the project path
|
2016-08-11 12:42:19 +00:00
|
|
|
if( !filePath.Exists() )
|
|
|
|
{
|
2016-10-07 14:07:10 +00:00
|
|
|
filePath.SetPath( Prj().GetProjectPath() + filePath.GetPath() );
|
2016-08-11 12:42:19 +00:00
|
|
|
|
2017-10-26 21:59:56 +00:00
|
|
|
if( !filePath.Exists() )
|
|
|
|
return;
|
|
|
|
}
|
2016-08-11 12:41:40 +00:00
|
|
|
|
2017-10-26 21:59:56 +00:00
|
|
|
// Display the library contents
|
2018-07-03 13:28:59 +00:00
|
|
|
wxWindowUpdateLocker updateLock( this );
|
2020-08-24 15:24:48 +00:00
|
|
|
|
|
|
|
m_libraryContents->SetReadOnly( false );
|
2018-07-03 13:28:59 +00:00
|
|
|
m_libraryContents->Clear();
|
|
|
|
wxTextFile file;
|
|
|
|
file.Open( filePath.GetFullPath() );
|
|
|
|
int line_nr = 0;
|
2016-08-11 12:41:40 +00:00
|
|
|
|
2021-06-09 19:32:58 +00:00
|
|
|
// Stores the library content. It will be displayed after reading the full library
|
2019-11-21 10:28:43 +00:00
|
|
|
wxString fullText;
|
|
|
|
|
2021-06-14 18:00:08 +00:00
|
|
|
// Process the file, looking for symbols.
|
2018-07-03 13:28:59 +00:00
|
|
|
while( !file.Eof() )
|
2016-08-11 12:41:40 +00:00
|
|
|
{
|
2018-07-03 13:28:59 +00:00
|
|
|
const wxString& line = line_nr == 0 ? file.GetFirstLine() : file.GetNextLine();
|
2019-11-21 10:28:43 +00:00
|
|
|
fullText << line << '\n';
|
2018-07-03 13:28:59 +00:00
|
|
|
|
|
|
|
wxStringTokenizer tokenizer( line );
|
2016-08-11 12:41:40 +00:00
|
|
|
|
2016-08-11 12:42:08 +00:00
|
|
|
while( tokenizer.HasMoreTokens() )
|
2016-08-11 12:41:40 +00:00
|
|
|
{
|
2016-08-11 12:42:08 +00:00
|
|
|
wxString token = tokenizer.GetNextToken().Lower();
|
|
|
|
|
2017-10-26 17:49:57 +00:00
|
|
|
// some subckts contain .model clauses inside,
|
|
|
|
// skip them as they are a part of the subckt, not another model
|
|
|
|
if( token == ".model" && !in_subckt )
|
|
|
|
{
|
2017-10-26 17:54:53 +00:00
|
|
|
wxString name = tokenizer.GetNextToken();
|
|
|
|
|
|
|
|
if( name.IsEmpty() )
|
|
|
|
break;
|
|
|
|
|
|
|
|
token = tokenizer.GetNextToken();
|
2018-09-22 14:05:32 +00:00
|
|
|
SPICE_PRIMITIVE type = MODEL::parseModelType( token );
|
2017-10-26 17:54:53 +00:00
|
|
|
|
2018-09-22 14:05:32 +00:00
|
|
|
if( type != SP_UNKNOWN )
|
2017-10-26 21:59:56 +00:00
|
|
|
m_models.emplace( name, MODEL( line_nr, type ) );
|
2017-10-26 17:49:57 +00:00
|
|
|
}
|
2017-10-26 17:54:53 +00:00
|
|
|
|
2017-10-26 17:49:57 +00:00
|
|
|
else if( token == ".subckt" )
|
|
|
|
{
|
|
|
|
wxASSERT( !in_subckt );
|
|
|
|
in_subckt = true;
|
|
|
|
|
2017-10-26 17:54:53 +00:00
|
|
|
wxString name = tokenizer.GetNextToken();
|
|
|
|
|
|
|
|
if( name.IsEmpty() )
|
|
|
|
break;
|
|
|
|
|
2018-09-22 14:05:32 +00:00
|
|
|
m_models.emplace( name, MODEL( line_nr, SP_SUBCKT ) );
|
2017-10-26 17:49:57 +00:00
|
|
|
}
|
2017-10-26 17:54:53 +00:00
|
|
|
|
2017-10-26 17:49:57 +00:00
|
|
|
else if( token == ".ends" )
|
|
|
|
{
|
|
|
|
wxASSERT( in_subckt );
|
|
|
|
in_subckt = false;
|
|
|
|
}
|
2016-08-11 12:41:40 +00:00
|
|
|
}
|
2018-07-03 13:28:59 +00:00
|
|
|
|
|
|
|
++line_nr;
|
2016-08-11 12:41:40 +00:00
|
|
|
}
|
|
|
|
|
2019-11-21 10:28:43 +00:00
|
|
|
// display the full library content:
|
|
|
|
m_libraryContents->AppendText( fullText );
|
2020-08-24 15:24:48 +00:00
|
|
|
m_libraryContents->SetReadOnly( true );
|
2019-11-21 10:28:43 +00:00
|
|
|
|
2018-09-22 14:05:32 +00:00
|
|
|
wxArrayString modelsList;
|
2018-05-03 09:58:13 +00:00
|
|
|
|
2017-10-26 17:54:53 +00:00
|
|
|
// Refresh the model name combobox values
|
|
|
|
m_modelName->Clear();
|
|
|
|
|
|
|
|
for( const auto& model : m_models )
|
2018-05-03 09:58:13 +00:00
|
|
|
{
|
2017-10-26 17:54:53 +00:00
|
|
|
m_modelName->Append( model.first );
|
2018-09-22 14:05:32 +00:00
|
|
|
modelsList.Add( model.first );
|
2018-05-03 09:58:13 +00:00
|
|
|
}
|
|
|
|
|
2018-09-22 14:05:32 +00:00
|
|
|
m_modelName->AutoComplete( modelsList );
|
2017-10-26 17:54:53 +00:00
|
|
|
|
2016-08-11 12:42:08 +00:00
|
|
|
// Restore the previous value or if there is none - pick the first one from the loaded library
|
2017-10-26 17:54:53 +00:00
|
|
|
if( !curModel.IsEmpty() )
|
|
|
|
m_modelName->SetValue( curModel );
|
|
|
|
else if( m_modelName->GetCount() > 0 )
|
|
|
|
m_modelName->SetSelection( 0 );
|
2016-08-11 12:41:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-01-10 05:42:14 +00:00
|
|
|
SCH_FIELD& DIALOG_SPICE_MODEL::getSchField( int aFieldType )
|
|
|
|
{
|
2021-06-10 14:10:55 +00:00
|
|
|
const wxString& spiceField =
|
|
|
|
NETLIST_EXPORTER_PSPICE::GetSpiceFieldName( (SPICE_FIELD) aFieldType );
|
2019-01-10 05:42:14 +00:00
|
|
|
|
2021-06-10 14:10:55 +00:00
|
|
|
auto fieldIt = std::find_if( m_schfields->begin(), m_schfields->end(),
|
|
|
|
[&]( const SCH_FIELD& f ) {
|
2019-01-10 05:42:14 +00:00
|
|
|
return f.GetName() == spiceField;
|
|
|
|
} );
|
|
|
|
|
|
|
|
// Found one, so return it
|
|
|
|
if( fieldIt != m_schfields->end() )
|
|
|
|
return *fieldIt;
|
|
|
|
|
|
|
|
// Create a new field with requested name
|
2021-06-10 14:10:55 +00:00
|
|
|
m_schfields->emplace_back( wxPoint(), m_schfields->size(), &m_symbol, spiceField );
|
2019-01-10 05:42:14 +00:00
|
|
|
return m_schfields->back();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
LIB_FIELD& DIALOG_SPICE_MODEL::getLibField( int aFieldType )
|
2016-08-11 12:41:40 +00:00
|
|
|
{
|
2021-06-10 14:10:55 +00:00
|
|
|
const wxString& spiceField =
|
|
|
|
NETLIST_EXPORTER_PSPICE::GetSpiceFieldName( ( SPICE_FIELD ) aFieldType );
|
2016-08-11 12:41:40 +00:00
|
|
|
|
2020-03-26 11:02:59 +00:00
|
|
|
auto fieldIt = std::find_if( m_libfields->begin(), m_libfields->end(),
|
|
|
|
[&]( const LIB_FIELD& f )
|
|
|
|
{
|
|
|
|
return f.GetName() == spiceField;
|
|
|
|
} );
|
2016-08-11 12:41:40 +00:00
|
|
|
|
|
|
|
// Found one, so return it
|
2019-01-10 05:42:14 +00:00
|
|
|
if( fieldIt != m_libfields->end() )
|
2016-08-11 12:41:40 +00:00
|
|
|
return *fieldIt;
|
|
|
|
|
|
|
|
// Create a new field with requested name
|
2019-01-10 05:42:14 +00:00
|
|
|
LIB_FIELD new_field( m_libfields->size() );
|
|
|
|
m_libfields->front().Copy( &new_field );
|
|
|
|
new_field.SetName( spiceField );
|
|
|
|
|
|
|
|
m_libfields->push_back( new_field );
|
|
|
|
return m_libfields->back();
|
2016-08-11 12:41:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool DIALOG_SPICE_MODEL::addPwlValue( const wxString& aTime, const wxString& aValue )
|
|
|
|
{
|
|
|
|
// TODO execute validators
|
|
|
|
if( aTime.IsEmpty() || aValue.IsEmpty() )
|
|
|
|
return false;
|
|
|
|
|
|
|
|
long idx = m_pwlValList->InsertItem( m_pwlTimeCol, aTime );
|
|
|
|
m_pwlValList->SetItem( idx, m_pwlValueCol, aValue );
|
|
|
|
|
|
|
|
// There is no wxString::ToFloat, but we need to guarantee it fits in 4 bytes
|
|
|
|
double timeD;
|
|
|
|
float timeF;
|
|
|
|
m_pwlTime->GetValue().ToDouble( &timeD );
|
|
|
|
timeF = timeD;
|
2019-06-13 05:16:54 +00:00
|
|
|
long data;
|
|
|
|
std::memcpy( &data, &timeF, sizeof( timeF ) );
|
2016-08-11 12:41:40 +00:00
|
|
|
|
|
|
|
// Store the time value, so the entries can be sorted
|
2019-06-13 05:16:54 +00:00
|
|
|
m_pwlValList->SetItemData( idx, data );
|
2016-08-11 12:41:40 +00:00
|
|
|
|
|
|
|
// Sort items by timestamp
|
|
|
|
m_pwlValList->SortItems( comparePwlValues, -1 );
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-10-26 17:54:53 +00:00
|
|
|
void DIALOG_SPICE_MODEL::onSelectLibrary( wxCommandEvent& event )
|
2016-08-11 12:41:40 +00:00
|
|
|
{
|
2017-10-26 17:54:53 +00:00
|
|
|
wxString searchPath = wxFileName( m_modelLibrary->GetValue() ).GetPath();
|
2016-08-19 15:33:26 +00:00
|
|
|
|
|
|
|
if( searchPath.IsEmpty() )
|
|
|
|
searchPath = Prj().GetProjectPath();
|
|
|
|
|
2019-01-08 11:57:39 +00:00
|
|
|
wxString wildcards = SpiceLibraryFileWildcard() + "|" + AllFilesWildcard();
|
2017-11-12 00:31:38 +00:00
|
|
|
wxFileDialog openDlg( this, _( "Select library" ), searchPath, "", wildcards,
|
2016-08-11 12:41:40 +00:00
|
|
|
wxFD_OPEN | wxFD_FILE_MUST_EXIST );
|
|
|
|
|
|
|
|
if( openDlg.ShowModal() == wxID_CANCEL )
|
|
|
|
return;
|
|
|
|
|
2016-08-11 12:42:08 +00:00
|
|
|
wxFileName libPath( openDlg.GetPath() );
|
|
|
|
|
|
|
|
// Try to convert the path to relative to project
|
2021-06-10 14:10:55 +00:00
|
|
|
if( libPath.MakeRelativeTo( Prj().GetProjectPath() )
|
|
|
|
&& !libPath.GetFullPath().StartsWith( ".." ) )
|
2017-10-26 17:54:53 +00:00
|
|
|
m_modelLibrary->SetValue( libPath.GetFullPath() );
|
2016-08-11 12:42:08 +00:00
|
|
|
else
|
2017-10-26 17:54:53 +00:00
|
|
|
m_modelLibrary->SetValue( openDlg.GetPath() );
|
2016-08-11 12:42:08 +00:00
|
|
|
|
2017-10-26 17:54:53 +00:00
|
|
|
loadLibrary( openDlg.GetPath() );
|
|
|
|
m_modelName->Popup();
|
2016-08-11 12:41:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-10-26 17:54:53 +00:00
|
|
|
void DIALOG_SPICE_MODEL::onModelSelected( wxCommandEvent& event )
|
2016-08-11 12:41:40 +00:00
|
|
|
{
|
2017-10-26 17:54:53 +00:00
|
|
|
// autoselect the model type
|
|
|
|
auto it = m_models.find( m_modelName->GetValue() );
|
2016-08-19 15:33:26 +00:00
|
|
|
|
2017-10-26 17:54:53 +00:00
|
|
|
if( it != m_models.end() )
|
2017-10-26 21:59:56 +00:00
|
|
|
{
|
2018-09-22 14:05:32 +00:00
|
|
|
m_modelType->SetSelection( getModelTypeIdx( it->second.model ) );
|
2017-10-26 21:59:56 +00:00
|
|
|
|
|
|
|
// scroll to the bottom, so the model definition is shown in the first line
|
|
|
|
m_libraryContents->ShowPosition(
|
|
|
|
m_libraryContents->XYToPosition( 0, m_libraryContents->GetNumberOfLines() ) );
|
|
|
|
m_libraryContents->ShowPosition( m_libraryContents->XYToPosition( 0, it->second.line ) );
|
|
|
|
}
|
2016-08-11 12:42:08 +00:00
|
|
|
else
|
2017-10-26 21:59:56 +00:00
|
|
|
{
|
|
|
|
m_libraryContents->ShowPosition( 0 );
|
|
|
|
}
|
2016-08-11 12:41:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-05-23 07:48:05 +00:00
|
|
|
void DIALOG_SPICE_MODEL::onTypeSelected( wxCommandEvent& event )
|
|
|
|
{
|
|
|
|
int type = m_modelType->GetSelection();
|
|
|
|
int primitive = type >= 0 ? modelTypes[type].type : SP_SUBCKT;
|
|
|
|
showPinOrderNote( primitive );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-08-11 12:41:40 +00:00
|
|
|
void DIALOG_SPICE_MODEL::onPwlAdd( wxCommandEvent& event )
|
|
|
|
{
|
|
|
|
addPwlValue( m_pwlTime->GetValue(), m_pwlValue->GetValue() );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void DIALOG_SPICE_MODEL::onPwlRemove( wxCommandEvent& event )
|
|
|
|
{
|
|
|
|
long idx = m_pwlValList->GetNextItem( -1, wxLIST_NEXT_ALL, wxLIST_STATE_SELECTED );
|
|
|
|
m_pwlValList->DeleteItem( idx );
|
|
|
|
}
|
2017-10-26 17:54:53 +00:00
|
|
|
|
|
|
|
|
2020-03-23 16:49:21 +00:00
|
|
|
void DIALOG_SPICE_MODEL::onRandomSourceType( wxCommandEvent& event )
|
|
|
|
{
|
|
|
|
switch( m_rnType->GetSelection() )
|
|
|
|
{
|
|
|
|
case TRRANDOM_UNIFORM:
|
|
|
|
// uniform white noise
|
|
|
|
m_rnParam1Text->SetLabel( _( "Range:" ) );
|
|
|
|
m_rnParam2Text->SetLabel( _( "Offset:" ) );
|
|
|
|
break;
|
|
|
|
|
|
|
|
case TRRANDOM_GAUSSIAN:
|
|
|
|
// Gaussian
|
|
|
|
m_rnParam1Text->SetLabel( _( "Standard deviation:" ) );
|
|
|
|
m_rnParam2Text->SetLabel( _( "Mean:" ) );
|
|
|
|
break;
|
|
|
|
|
|
|
|
case TRRANDOM_EXPONENTIAL:
|
|
|
|
// exponential
|
|
|
|
m_rnParam1Text->SetLabel( _( "Mean:" ) );
|
|
|
|
m_rnParam2Text->SetLabel( _( "Offset:" ) );
|
|
|
|
break;
|
|
|
|
|
|
|
|
case TRRANDOM_POISSON:
|
|
|
|
// Poisson
|
|
|
|
m_rnParam1Text->SetLabel( _( "Lambda:" ) );
|
|
|
|
m_rnParam2Text->SetLabel( _( "Offset:" ) );
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
wxFAIL_MSG( _( "type of random generator for source is invalid" ) );
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-09-22 14:05:32 +00:00
|
|
|
SPICE_PRIMITIVE DIALOG_SPICE_MODEL::MODEL::parseModelType( const wxString& aValue )
|
2017-10-26 17:54:53 +00:00
|
|
|
{
|
2018-09-22 14:05:32 +00:00
|
|
|
wxCHECK( !aValue.IsEmpty(), SP_UNKNOWN );
|
2017-10-26 17:54:53 +00:00
|
|
|
const wxString val( aValue.Lower() );
|
|
|
|
|
2018-09-22 14:05:32 +00:00
|
|
|
for( const auto& model : modelTypes )
|
|
|
|
{
|
|
|
|
for( const auto& keyword : model.keywords )
|
|
|
|
{
|
|
|
|
if( val.StartsWith( keyword ) )
|
|
|
|
return model.type;
|
|
|
|
}
|
|
|
|
}
|
2017-10-26 17:54:53 +00:00
|
|
|
|
2018-09-22 14:05:32 +00:00
|
|
|
return SP_UNKNOWN;
|
2017-10-26 17:54:53 +00:00
|
|
|
}
|