From d028b381fa85dce016924f967c3cadc5efe2df65 Mon Sep 17 00:00:00 2001 From: Jeff Young Date: Tue, 28 Feb 2023 12:20:45 +0000 Subject: [PATCH] Make SIM_MODEL_SPICE_FALLBACK act more like the model it's a fallback for. In particular, install/show the parameters, and keep the existing name field. Fixes https://gitlab.com/kicad/code/kicad/issues/14102 (cherry picked from commit 281fde71b949ecf9aaf98854ccc6c495a8e21219) --- eeschema/dialogs/dialog_sim_model.cpp | 47 ++++++++++++----------- eeschema/sim/sim_model_spice_fallback.cpp | 33 +++++++++++++++- eeschema/sim/sim_model_spice_fallback.h | 6 ++- 3 files changed, 60 insertions(+), 26 deletions(-) diff --git a/eeschema/dialogs/dialog_sim_model.cpp b/eeschema/dialogs/dialog_sim_model.cpp index 8f67565503..08ca27e438 100644 --- a/eeschema/dialogs/dialog_sim_model.cpp +++ b/eeschema/dialogs/dialog_sim_model.cpp @@ -162,6 +162,14 @@ bool DIALOG_SIM_MODEL::TransferDataToWindow() m_fields[ VALUE_FIELD ].SetText( wxT( "${SIM.PARAMS}" ) ); } + std::vector sourcePins = m_symbol.GetAllLibPins(); + + std::sort( sourcePins.begin(), sourcePins.end(), + []( const LIB_PIN* lhs, const LIB_PIN* rhs ) + { + return StrNumCmp( lhs->GetNumber(), rhs->GetNumber(), true ) < 0; + } ); + std::string libraryFilename = SIM_MODEL::GetFieldValue( &m_fields, SIM_LIBRARY::LIBRARY_FIELD ); if( libraryFilename != "" ) @@ -172,8 +180,11 @@ bool DIALOG_SIM_MODEL::TransferDataToWindow() if( !loadLibrary( libraryFilename ) ) { m_libraryPathText->ChangeValue( libraryFilename ); - m_modelNameChoice->SetSelection( -1 ); m_curModelType = SIM_MODEL::ReadTypeFromFields( m_fields ); + m_libraryModelsMgr.CreateModel( nullptr, sourcePins, m_fields ); + + m_modelNameChoice->Append( _( "" ) ); + m_modelNameChoice->SetSelection( 0 ); } else { @@ -248,14 +259,6 @@ bool DIALOG_SIM_MODEL::TransferDataToWindow() m_curModelType = SIM_MODEL::ReadTypeFromFields( m_fields ); } - std::vector sourcePins = m_symbol.GetAllLibPins(); - - std::sort( sourcePins.begin(), sourcePins.end(), - []( const LIB_PIN* lhs, const LIB_PIN* rhs ) - { - return StrNumCmp( lhs->GetNumber(), rhs->GetNumber(), true ) < 0; - } ); - for( SIM_MODEL::TYPE type : SIM_MODEL::TYPE_ITERATOR() ) { wxString msg; @@ -298,25 +301,25 @@ bool DIALOG_SIM_MODEL::TransferDataFromWindow() if( !DIALOG_SIM_MODEL_BASE::TransferDataFromWindow() ) return false; - if( !m_modelNameChoice->IsEmpty() ) - { - std::string modelName = m_modelNameChoice->GetStringSelection().ToStdString(); - - SIM_MODEL::SetFieldValue( m_fields, SIM_LIBRARY::NAME_FIELD, modelName ); - } - std::string path; + std::string name; - if( m_useLibraryModelRadioButton->GetValue() || isIbisLoaded() ) + if( m_useLibraryModelRadioButton->GetValue() ) { path = m_libraryPathText->GetValue(); wxFileName fn( path ); if( fn.MakeRelativeTo( Prj().GetProjectPath() ) && !fn.GetFullPath().StartsWith( ".." ) ) path = fn.GetFullPath(); + + if( dynamic_cast( &curModel() ) ) + name = SIM_MODEL::GetFieldValue( &m_fields, SIM_LIBRARY::NAME_FIELD, false ); + else if( !m_modelNameChoice->IsEmpty() ) + name = m_modelNameChoice->GetStringSelection().ToStdString(); } SIM_MODEL::SetFieldValue( m_fields, SIM_LIBRARY::LIBRARY_FIELD, path ); + SIM_MODEL::SetFieldValue( m_fields, SIM_LIBRARY::NAME_FIELD, name ); if( isIbisLoaded() ) { @@ -438,15 +441,10 @@ void DIALOG_SIM_MODEL::updateInstanceWidgets( SIM_MODEL* aMod m_typeChoice->Enable( !m_useLibraryModelRadioButton->GetValue() || isIbisLoaded() ); - if( dynamic_cast( aModel ) - || dynamic_cast( aModel ) ) - { + if( dynamic_cast( aModel ) ) m_modelNotebook->SetSelection( 1 ); - } else - { m_modelNotebook->SetSelection( 0 ); - } if( aModel->HasPrimaryValue() ) { @@ -564,6 +562,9 @@ void DIALOG_SIM_MODEL::updateModelParamsTab( SIM_MODEL* aMode template void DIALOG_SIM_MODEL::updateModelCodeTab( SIM_MODEL* aModel ) { + if( dynamic_cast( aModel ) ) + return; + wxString text; SPICE_ITEM item; diff --git a/eeschema/sim/sim_model_spice_fallback.cpp b/eeschema/sim/sim_model_spice_fallback.cpp index 6098413677..691b92791d 100644 --- a/eeschema/sim/sim_model_spice_fallback.cpp +++ b/eeschema/sim/sim_model_spice_fallback.cpp @@ -1,7 +1,7 @@ /* * This program source code file is part of KiCad, a free EDA CAD application. * - * Copyright (C) 2022 KiCad Developers, see AUTHORS.txt for contributors. + * Copyright (C) 2022-2023 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 @@ -23,11 +23,18 @@ #include #include +#include SIM_MODEL_SPICE_FALLBACK::SIM_MODEL_SPICE_FALLBACK( TYPE aType, const std::string& aRawSpiceCode ) : SIM_MODEL_SPICE( aType, std::make_unique( *this ) ) { + // Create the model we *should* have had to copy its parameter list + std::unique_ptr model = SIM_MODEL::Create( aType ); + + for( const SIM_MODEL::PARAM& param : model->GetParams() ) + AddParam( param.info ); + m_spiceCode = aRawSpiceCode; } @@ -53,4 +60,26 @@ std::vector SIM_MODEL_SPICE_FALLBACK::GetPinNames() const // If we're a fall-back for a paticular model type, then return its pin names std::unique_ptr model = SIM_MODEL::Create( GetType() ); return model->GetPinNames(); -} \ No newline at end of file +} + + +int SIM_MODEL_SPICE_FALLBACK::doFindParam( const std::string& aParamName ) const +{ + // Special case to allow escaped model parameters (suffixed with "_") + + std::string lowerParamName = boost::to_lower_copy( aParamName ); + + std::vector> params = GetParams(); + + for( int ii = 0; ii < (int) params.size(); ++ii ) + { + const PARAM& param = params[ii]; + + if( param.info.name == lowerParamName || param.info.name == lowerParamName + "_" ) + return ii; + } + + return -1; +} + + diff --git a/eeschema/sim/sim_model_spice_fallback.h b/eeschema/sim/sim_model_spice_fallback.h index c5b5bbaafa..0495fe0843 100644 --- a/eeschema/sim/sim_model_spice_fallback.h +++ b/eeschema/sim/sim_model_spice_fallback.h @@ -1,7 +1,7 @@ /* * This program source code file is part of KiCad, a free EDA CAD application. * - * Copyright (C) 2022 KiCad Developers, see AUTHORS.txt for contributors. + * Copyright (C) 2022-2023 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 @@ -36,6 +36,10 @@ public: const std::string& aSymbolPinNumber ) override; std::vector GetPinNames() const override; + +protected: + int doFindParam( const std::string& aParamName ) const override; + }; #endif // SIM_MODEL_SPICE_FALLBACK_H