From e8762da4f43678f59b45ee51c6413e20186ddf56 Mon Sep 17 00:00:00 2001 From: Mikolaj Wielgus Date: Mon, 7 Nov 2022 19:33:31 +0100 Subject: [PATCH] Revert "Support for variable references in SPICE models." This reverts commit 95ebfeeed1ae05d308efff2f24319283b755db37. This reverts commit 672ce650a9d19f90d73dfa974b6b937e4bb2a9a7. --- eeschema/dialogs/dialog_sim_model.cpp | 25 ++-- eeschema/erc.cpp | 7 +- .../netlist_exporter_spice.cpp | 20 +-- eeschema/sch_field.cpp | 2 +- eeschema/sim/sim_lib_mgr.cpp | 47 +++---- eeschema/sim/sim_lib_mgr.h | 3 +- eeschema/sim/sim_library_kibis.cpp | 5 +- eeschema/sim/sim_model.cpp | 125 +++++------------- eeschema/sim/sim_model.h | 26 ++-- eeschema/sim/sim_model_kibis.cpp | 27 ++-- eeschema/sim/sim_model_kibis.h | 8 +- eeschema/sim/sim_model_spice.cpp | 4 +- eeschema/sim/sim_plot_frame.cpp | 3 +- eeschema/sim/sim_property.cpp | 113 +++++++++++++--- eeschema/sim/sim_property.h | 47 ++++++- eeschema/sim/spice_generator.h | 2 +- eeschema/tools/sch_editor_control.cpp | 5 +- 17 files changed, 239 insertions(+), 230 deletions(-) diff --git a/eeschema/dialogs/dialog_sim_model.cpp b/eeschema/dialogs/dialog_sim_model.cpp index 3c65d5ecd8..61c58ef016 100644 --- a/eeschema/dialogs/dialog_sim_model.cpp +++ b/eeschema/dialogs/dialog_sim_model.cpp @@ -66,7 +66,7 @@ DIALOG_SIM_MODEL::DIALOG_SIM_MODEL( wxWindow* aParent, SCH_SYMBOL& aSymbol, for( SIM_MODEL::TYPE type : SIM_MODEL::TYPE_ITERATOR() ) { - m_models.push_back( SIM_MODEL::Create( type, m_sortedSymbolPins.size(), false ) ); + m_models.push_back( SIM_MODEL::Create( type, m_sortedSymbolPins.size() ) ); SIM_MODEL::DEVICE_TYPE_ deviceType = SIM_MODEL::TypeInfo( type ).deviceType; @@ -138,7 +138,7 @@ bool DIALOG_SIM_MODEL::TransferDataToWindow() { wxCommandEvent dummyEvent; - unsigned pinCount = m_sortedSymbolPins.size(); + int pinCount = m_sortedSymbolPins.size(); std::string ref = SIM_MODEL::GetFieldValue( &m_fields, SIM_MODEL::REFERENCE_FIELD ); std::string libraryFilename = SIM_MODEL::GetFieldValue( &m_fields, SIM_LIBRARY::LIBRARY_FIELD ); @@ -207,7 +207,7 @@ bool DIALOG_SIM_MODEL::TransferDataToWindow() try { - m_models.at( static_cast( type ) ) = SIM_MODEL::Create( pinCount, m_fields, false ); + m_models.at( static_cast( type ) ) = SIM_MODEL::Create( pinCount, m_fields ); } catch( const IO_ERROR& e ) { @@ -638,7 +638,6 @@ void DIALOG_SIM_MODEL::loadLibrary( const wxString& aFilePath ) return; } - unsigned pinCount = m_sortedSymbolPins.size(); m_tclibraryPathName->ChangeValue( aFilePath ); m_libraryModels.clear(); @@ -656,12 +655,12 @@ void DIALOG_SIM_MODEL::loadLibrary( const wxString& aFilePath ) { //TODO: it's not cur model. - m_libraryModels.push_back( SIM_MODEL::Create( baseModel, pinCount, m_fields, - false ) ); + m_libraryModels.push_back( + SIM_MODEL::Create( baseModel, m_sortedSymbolPins.size(), m_fields ) ); } else { - m_libraryModels.push_back( SIM_MODEL::Create( baseModel, pinCount, false ) ); + m_libraryModels.push_back( SIM_MODEL::Create( baseModel, m_sortedSymbolPins.size() ) ); } } } @@ -783,12 +782,12 @@ wxPGProperty* DIALOG_SIM_MODEL::newParamProperty( int aParamIndex ) const case SIM_VALUE::TYPE_INT: prop = new SIM_STRING_PROPERTY( paramDescription, param.info.name, m_library, - curModelSharedPtr(), aParamIndex ); + curModelSharedPtr(), aParamIndex, SIM_VALUE::TYPE_INT ); break; case SIM_VALUE::TYPE_FLOAT: prop = new SIM_STRING_PROPERTY( paramDescription, param.info.name, m_library, - curModelSharedPtr(), aParamIndex ); + curModelSharedPtr(), aParamIndex, SIM_VALUE::TYPE_FLOAT ); break; //case TYPE_COMPLEX: @@ -798,12 +797,14 @@ wxPGProperty* DIALOG_SIM_MODEL::newParamProperty( int aParamIndex ) const if( param.info.enumValues.empty() ) { prop = new SIM_STRING_PROPERTY( paramDescription, param.info.name, m_library, - curModelSharedPtr(), aParamIndex ); + curModelSharedPtr(), aParamIndex, + SIM_VALUE::TYPE_STRING ); } else { prop = new SIM_ENUM_PROPERTY( paramDescription, param.info.name, m_library, - curModelSharedPtr(), aParamIndex ); + curModelSharedPtr(), aParamIndex, + SIM_VALUE::TYPE_STRING ); } break; @@ -1144,7 +1145,7 @@ void DIALOG_SIM_MODEL::onTypeChoice( wxCommandEvent& aEvent ) m_libraryModels.at( m_modelNameCombobox->GetSelection() ) = std::shared_ptr( dynamic_cast( - new SIM_MODEL_KIBIS( type, *kibismodel, m_fields, false ) ) ); + new SIM_MODEL_KIBIS( type, *kibismodel, m_fields ) ) ); } m_curModelType = type; diff --git a/eeschema/erc.cpp b/eeschema/erc.cpp index 225684c8b7..f88e52d6e7 100644 --- a/eeschema/erc.cpp +++ b/eeschema/erc.cpp @@ -24,7 +24,7 @@ */ #include "connection_graph.h" -#include +#include "sim/sim_model.h" #include // for ExpandEnvVarSubstitutions #include #include @@ -869,7 +869,6 @@ int ERC_TESTER::TestOffGridEndpoints( int aGridSize ) int ERC_TESTER::TestSimModelIssues() { - SIM_LIB_MGR libMgr( m_schematic->Prj() ); SCH_SHEET_LIST sheets = m_schematic->GetSheets(); int err_count = 0; @@ -883,7 +882,9 @@ int ERC_TESTER::TestSimModelIssues() try { - libMgr.CreateModel( &sheet, *symbol, true ); + /* JEY TODO + std::unique_ptr model = SIM_MODEL::Create( &sheet, symbol, true ); + */ } catch( IO_ERROR& e ) { diff --git a/eeschema/netlist_exporters/netlist_exporter_spice.cpp b/eeschema/netlist_exporters/netlist_exporter_spice.cpp index 42b4c7588b..0604666d7f 100644 --- a/eeschema/netlist_exporters/netlist_exporter_spice.cpp +++ b/eeschema/netlist_exporters/netlist_exporter_spice.cpp @@ -210,19 +210,11 @@ bool NETLIST_EXPORTER_SPICE::ReadSchematicAndLibraries( unsigned aNetlistOptions SPICE_ITEM spiceItem; - for( int i = 0; i < symbol->GetFieldCount(); ++i ) - { - spiceItem.fields.emplace_back( VECTOR2I(), i, symbol, symbol->GetFields()[ i ].GetName() ); - - if( i == REFERENCE_FIELD ) - spiceItem.fields.back().SetText( symbol->GetRef( &sheet ) ); - else if( i == VALUE_FIELD ) - spiceItem.fields.back().SetText( symbol->GetValue( &sheet, true ) ); - else if( i == FOOTPRINT_FIELD ) - spiceItem.fields.back().SetText( symbol->GetFootprint( &sheet, true ) ); - else - spiceItem.fields.back().SetText( symbol->GetFields()[ i ].GetShownText( 0, false ) ); - } + // This is a little bit dangerous as any value fetched from the fields will not + // be instance-data aware, and will just fetch the value of some random sheet + // (whatever sheet the user happens to be looking at). However, we currently only + // use it to fetch "Sim_*" fields, which have no instance data. + spiceItem.fields = &symbol->GetFields(); try { @@ -384,7 +376,7 @@ bool NETLIST_EXPORTER_SPICE::readRefName( SCH_SHEET_PATH& aSheet, SCH_SYMBOL& aS void NETLIST_EXPORTER_SPICE::readModel( SCH_SHEET_PATH& aSheet, SCH_SYMBOL& aSymbol, SPICE_ITEM& aItem ) { - SIM_LIBRARY::MODEL libModel = m_libMgr.CreateModel( &aSheet, aSymbol, true ); + SIM_LIBRARY::MODEL libModel = m_libMgr.CreateModel( aSymbol ); aItem.baseModelName = libModel.name; aItem.model = &libModel.model; diff --git a/eeschema/sch_field.cpp b/eeschema/sch_field.cpp index df90459292..93c40500ee 100644 --- a/eeschema/sch_field.cpp +++ b/eeschema/sch_field.cpp @@ -210,7 +210,7 @@ wxString SCH_FIELD::GetShownText( int aDepth, bool aAllowExtraText ) const PROJECT* project = nullptr; wxString text = EDA_TEXT::GetShownText(); - if( IsNameShown() && aAllowExtraText ) + if( IsNameShown() ) text = GetName() << wxS( ": " ) << text; if( text == wxS( "~" ) ) // Legacy placeholder for empty string diff --git a/eeschema/sim/sim_lib_mgr.cpp b/eeschema/sim/sim_lib_mgr.cpp index 42e0521f40..0bc86fa661 100644 --- a/eeschema/sim/sim_lib_mgr.cpp +++ b/eeschema/sim/sim_lib_mgr.cpp @@ -35,31 +35,12 @@ SIM_LIB_MGR::SIM_LIB_MGR( const PROJECT& aPrj ) : m_project( aPrj ) } -SIM_LIBRARY::MODEL SIM_LIB_MGR::CreateModel( const SCH_SHEET_PATH* aSheetPath, SCH_SYMBOL& aSymbol, - bool aResolve ) +SIM_LIBRARY::MODEL SIM_LIB_MGR::CreateModel( SCH_SYMBOL& aSymbol ) { - int pinCount = aSymbol.GetLibPins().size(); - SCH_FIELD* libraryField = aSymbol.FindField( SIM_LIBRARY::LIBRARY_FIELD ); + std::vector pins = aSymbol.GetLibPins(); + SCH_FIELD* libraryField = aSymbol.FindField( SIM_LIBRARY::LIBRARY_FIELD ); std::string baseModelName; - std::vector fields; - - for( int i = 0; i < aSymbol.GetFieldCount(); ++i ) - { - fields.emplace_back( VECTOR2I(), i, &aSymbol, aSymbol.GetFields()[ i ].GetName() ); - - if( i == REFERENCE_FIELD ) - fields.back().SetText( aSymbol.GetRef( aSheetPath ) ); - else if( i == VALUE_FIELD ) - fields.back().SetText( aSymbol.GetValue( aSheetPath, aResolve ) ); - else if( i == FOOTPRINT_FIELD ) - fields.back().SetText( aSymbol.GetFootprint( aSheetPath, aResolve ) ); - else if( aResolve ) - fields.back().SetText( aSymbol.GetFields()[ i ].GetShownText( 0, false ) ); - else - fields.back().SetText( aSymbol.GetFields()[ i ].GetText() ); - } - if( libraryField ) { wxString path = libraryField->GetShownText(); @@ -74,9 +55,10 @@ SIM_LIBRARY::MODEL SIM_LIB_MGR::CreateModel( const SCH_SHEET_PATH* aSheetPath, S } catch( const IO_ERROR& e ) { - THROW_IO_ERROR( wxString::Format( _( "Error loading simulation model library '%s': %s" ), - absolutePath, - e.What() ) ); + THROW_IO_ERROR( + wxString::Format( _( "Error loading simulation model library '%s': %s" ), + absolutePath, + e.What() ) ); } SCH_FIELD* nameField = aSymbol.FindField( SIM_LIBRARY::NAME_FIELD ); @@ -92,17 +74,20 @@ SIM_LIBRARY::MODEL SIM_LIB_MGR::CreateModel( const SCH_SHEET_PATH* aSheetPath, S if( !baseModel ) { - THROW_IO_ERROR( wxString::Format( _( "Error loading simulation model: could not find " - "base model '%s' in library '%s'" ), - baseModelName, - absolutePath ) ); + THROW_IO_ERROR( + wxString::Format( _( "Error loading simulation model: could not find base model '%s' in library '%s'" ), + baseModelName, + absolutePath ) ); } - m_models.push_back( SIM_MODEL::Create( *baseModel, pinCount, fields, aResolve ) ); + m_models.push_back( SIM_MODEL::Create( *baseModel, + static_cast( pins.size() ), + aSymbol.GetFields() ) ); } else { - m_models.push_back( SIM_MODEL::Create( pinCount, fields, aResolve ) ); + m_models.push_back( SIM_MODEL::Create( static_cast( pins.size() ), + aSymbol.GetFields() ) ); } return { baseModelName, *m_models.back() }; diff --git a/eeschema/sim/sim_lib_mgr.h b/eeschema/sim/sim_lib_mgr.h index e70b5345a0..84c5c19a36 100644 --- a/eeschema/sim/sim_lib_mgr.h +++ b/eeschema/sim/sim_lib_mgr.h @@ -44,8 +44,7 @@ public: virtual ~SIM_LIB_MGR() = default; // TODO: The argument can be made const. - SIM_LIBRARY::MODEL CreateModel( const SCH_SHEET_PATH* aSheetPath, SCH_SYMBOL& aSymbol, - bool aResolve ); + SIM_LIBRARY::MODEL CreateModel( SCH_SYMBOL& aSymbol ); SIM_LIBRARY& CreateLibrary( const std::string& aLibraryPath ); diff --git a/eeschema/sim/sim_library_kibis.cpp b/eeschema/sim/sim_library_kibis.cpp index 2e551a70d4..4d2feb0e06 100644 --- a/eeschema/sim/sim_library_kibis.cpp +++ b/eeschema/sim/sim_library_kibis.cpp @@ -23,8 +23,11 @@ #include #include +#include #include #include +#include +#include void SIM_LIBRARY_KIBIS::ReadFile( const std::string& aFilePath, SIM_MODEL::TYPE aType ) @@ -42,7 +45,7 @@ void SIM_LIBRARY_KIBIS::ReadFile( const std::string& aFilePath, SIM_MODEL::TYPE for( KIBIS_COMPONENT& kcomp : m_kibis.m_components ) { - m_models.push_back( SIM_MODEL::Create( aType, pinNumber, true ) ); + m_models.push_back( SIM_MODEL::Create( aType, pinNumber ) ); m_modelNames.emplace_back( kcomp.m_name ); SIM_MODEL_KIBIS* libcomp = dynamic_cast( m_models.back().get() ); diff --git a/eeschema/sim/sim_model.cpp b/eeschema/sim/sim_model.cpp index d53b248dfc..a8083e0039 100644 --- a/eeschema/sim/sim_model.cpp +++ b/eeschema/sim/sim_model.cpp @@ -42,6 +42,7 @@ #include #include +#include #include #include #include @@ -565,9 +566,9 @@ void SIM_MODEL::WriteDataLibFields( std::vector& aFields ) const } -std::unique_ptr SIM_MODEL::Create( TYPE aType, unsigned aSymbolPinCount, bool aResolve ) +std::unique_ptr SIM_MODEL::Create( TYPE aType, unsigned aSymbolPinCount ) { - std::unique_ptr model = Create( aType, aResolve ); + std::unique_ptr model = Create( aType ); // Passing nullptr to ReadDataFields will make it act as if all fields were empty. model->ReadDataFields( aSymbolPinCount, static_cast*>( nullptr ) ); @@ -575,10 +576,10 @@ std::unique_ptr SIM_MODEL::Create( TYPE aType, unsigned aSymbolPinCou } -std::unique_ptr SIM_MODEL::Create( const SIM_MODEL& aBaseModel, unsigned aSymbolPinCount, - bool aResolve ) +std::unique_ptr SIM_MODEL::Create( const SIM_MODEL& aBaseModel, + unsigned aSymbolPinCount ) { - std::unique_ptr model = Create( aBaseModel.GetType(), aResolve ); + std::unique_ptr model = Create( aBaseModel.GetType() ); model->SetBaseModel( aBaseModel ); model->ReadDataFields( aSymbolPinCount, static_cast*>( nullptr ) ); @@ -588,7 +589,7 @@ std::unique_ptr SIM_MODEL::Create( const SIM_MODEL& aBaseModel, unsig template std::unique_ptr SIM_MODEL::Create( const SIM_MODEL& aBaseModel, unsigned aSymbolPinCount, - const std::vector& aFields, bool aResolve ) + const std::vector& aFields ) { TYPE type = ReadTypeFromFields( aFields, aSymbolPinCount ); @@ -596,7 +597,7 @@ std::unique_ptr SIM_MODEL::Create( const SIM_MODEL& aBaseModel, unsig if( type == TYPE::NONE ) type = aBaseModel.GetType(); - std::unique_ptr model = Create( type, aResolve ); + std::unique_ptr model = Create( type ); model->SetBaseModel( aBaseModel ); model->ReadDataFields( aSymbolPinCount, &aFields ); @@ -605,36 +606,32 @@ std::unique_ptr SIM_MODEL::Create( const SIM_MODEL& aBaseModel, unsig template std::unique_ptr SIM_MODEL::Create( const SIM_MODEL& aBaseModel, unsigned aSymbolPinCount, - const std::vector& aFields, - bool aResolve ); + const std::vector& aFields ); template std::unique_ptr SIM_MODEL::Create( const SIM_MODEL& aBaseModel, unsigned aSymbolPinCount, - const std::vector& aFields, - bool aResolve ); + const std::vector& aFields ); template std::unique_ptr SIM_MODEL::Create( unsigned aSymbolPinCount, - const std::vector& aFields, bool aResolve ) + const std::vector& aFields ) { TYPE type = ReadTypeFromFields( aFields, aSymbolPinCount ); if( type == TYPE::NONE ) THROW_IO_ERROR( wxString::Format( _( "Failed to read simulation model from fields." ) ) ); - std::unique_ptr model = SIM_MODEL::Create( type, aResolve ); + std::unique_ptr model = SIM_MODEL::Create( type ); model->ReadDataFields( aSymbolPinCount, &aFields ); return model; } template std::unique_ptr SIM_MODEL::Create( unsigned aSymbolPinCount, - const std::vector& aFields, - bool aResolve ); + const std::vector& aFields ); template std::unique_ptr SIM_MODEL::Create( unsigned aSymbolPinCount, - const std::vector& aFields, - bool aResolve ); + const std::vector& aFields ); template @@ -750,17 +747,10 @@ std::vector> SIM_MODEL::GetPins() c const SIM_MODEL::PARAM& SIM_MODEL::GetParam( unsigned aParamIndex ) const { - const SIM_MODEL::PARAM& param = m_params.at( aParamIndex ); - - if( m_baseModel ) - { - std::string value = param.resolved ? param.value->ToString() : param.source.ToStdString(); - - if( value.empty() ) - return m_baseModel->GetParam( aParamIndex ); - } - - return param; + if( m_baseModel && m_params.at( aParamIndex ).value->ToString() == "" ) + return m_baseModel->GetParam( aParamIndex ); + else + return m_params.at( aParamIndex ); } @@ -859,33 +849,6 @@ void SIM_MODEL::SetParamValue( const std::string& aParamName, const std::string& } -void SIM_MODEL::SetParamSource( const std::string& aParamName, const wxString& aSource ) -{ - for( PARAM& param : m_params ) - { - if( param.info.name == boost::to_lower_copy( aParamName ) ) - { - param.source = aSource; - return; - } - } - - THROW_IO_ERROR( wxString::Format( _( "Could not find a parameter named '%s' in simulation model of type '%s'" ), - aParamName, - GetTypeInfo().fieldValue ) ); -} - - -void SIM_MODEL::SetParamSource( int aParamIndex, const wxString& aSource ) -{ - SIM_MODEL::PARAM& param = m_params.at( aParamIndex ); - - wxASSERT( !param.resolved ); - - param.source = aSource; -} - - bool SIM_MODEL::HasOverrides() const { for( const PARAM& param : m_params ) @@ -922,7 +885,7 @@ bool SIM_MODEL::HasSpiceNonInstanceOverrides() const } -std::unique_ptr SIM_MODEL::doCreate( TYPE aType ) +std::unique_ptr SIM_MODEL::Create( TYPE aType ) { switch( aType ) { @@ -1003,23 +966,6 @@ std::unique_ptr SIM_MODEL::doCreate( TYPE aType ) } -std::unique_ptr SIM_MODEL::Create( TYPE aType, bool aResolve ) -{ - std::unique_ptr model = doCreate( aType ); - - if( !aResolve ) - { - for( PARAM& param : model->m_params ) - { - param.source = param.value->ToString(); - param.resolved = false; - } - } - - return model; -} - - SIM_MODEL::SIM_MODEL( TYPE aType ) : SIM_MODEL( aType, std::make_unique( *this ) ) { @@ -1106,12 +1052,11 @@ std::string SIM_MODEL::GenerateParamValuePair( const PARAM& aParam, bool& aIsFir if( boost::ends_with( aParam.info.name, "_" ) ) name = aParam.info.name.substr( 0, aParam.info.name.length() - 1); - std::string value = aParam.resolved ? aParam.value->ToString() : aParam.source.ToStdString(); - + std::string value = aParam.value->ToString(); if( value.find( " " ) != std::string::npos ) value = "\"" + value + "\""; - result.append( fmt::format( "{}={}", name, value ) ); + result.append( fmt::format( "{}={}", aParam.info.name, value ) ); return result; } @@ -1152,9 +1097,7 @@ std::string SIM_MODEL::GenerateParamsField( const std::string& aPairSeparator ) for( const PARAM& param : m_params ) { - std::string value = param.resolved ? param.value->ToString() : param.source.ToStdString(); - - if( value.empty() ) + if( param.value->ToString() == "" ) continue; result.append( GenerateParamValuePair( param, isFirst ) ); @@ -1173,10 +1116,12 @@ void SIM_MODEL::ParseParamsField( const std::string& aParamsField ) { // Using parse tree instead of actions because we don't care about performance that much, // and having a tree greatly simplifies things. - root = tao::pegtl::parse_tree::parse< SIM_MODEL_PARSER::fieldParamValuePairsGrammar, - SIM_MODEL_PARSER::fieldParamValuePairsSelector, - tao::pegtl::nothing, - SIM_MODEL_PARSER::control>( in ); + root = tao::pegtl::parse_tree::parse< + SIM_MODEL_PARSER::fieldParamValuePairsGrammar, + SIM_MODEL_PARSER::fieldParamValuePairsSelector, + tao::pegtl::nothing, + SIM_MODEL_PARSER::control> + ( in ); } catch( const tao::pegtl::parse_error& e ) { @@ -1188,9 +1133,7 @@ void SIM_MODEL::ParseParamsField( const std::string& aParamsField ) for( const auto& node : root->children ) { if( node->is_type() ) - { paramName = node->string(); - } // TODO: Do something with number. // It doesn't seem too useful? else if( node->is_type() @@ -1199,10 +1142,7 @@ void SIM_MODEL::ParseParamsField( const std::string& aParamsField ) wxASSERT( paramName != "" ); // TODO: Shouldn't be named "...fromSpiceCode" here... - if( FindParam( paramName )->resolved ) - SetParamValue( paramName, node->string(), SIM_VALUE_GRAMMAR::NOTATION::SI ); - else - SetParamSource( paramName, node->string() ); + SetParamValue( paramName, node->string(), SIM_VALUE_GRAMMAR::NOTATION::SI ); } else if( node->is_type() ) { @@ -1211,10 +1151,7 @@ void SIM_MODEL::ParseParamsField( const std::string& aParamsField ) // Unescape quotes. boost::replace_all( str, "\\\"", "\"" ); - if( FindParam( paramName )->resolved ) - SetParamValue( paramName, str, SIM_VALUE_GRAMMAR::NOTATION::SI ); - else - SetParamSource( paramName, str ); + SetParamValue( paramName, str, SIM_VALUE_GRAMMAR::NOTATION::SI ); } else { @@ -1338,9 +1275,7 @@ void SIM_MODEL::InferredReadDataFields( unsigned aSymbolPinCount, const std::vec } } else if( node->is_type() ) - { ParseParamsField( node->string() ); - } } } catch( const tao::pegtl::parse_error& e ) diff --git a/eeschema/sim/sim_model.h b/eeschema/sim/sim_model.h index 8daea6fce0..cda2396484 100644 --- a/eeschema/sim/sim_model.h +++ b/eeschema/sim/sim_model.h @@ -463,15 +463,12 @@ public: } }; - bool resolved; - wxString source; std::unique_ptr value; - const INFO& info; - bool isOtherVariant = false; // Legacy. + const INFO& info; + bool isOtherVariant = false; // Legacy. PARAM( const INFO& aInfo, bool aIsOtherVariant = false ) - : resolved( true ), - value( SIM_VALUE::Create( aInfo.type ) ), + : value( SIM_VALUE::Create( aInfo.type ) ), info( aInfo ), isOtherVariant( aIsOtherVariant ) {} @@ -495,17 +492,17 @@ public: static TYPE InferTypeFromLegacyFields( const std::vector& aFields ); - static std::unique_ptr Create( TYPE aType, unsigned aSymbolPinCount, bool aResolve ); - static std::unique_ptr Create( const SIM_MODEL& aBaseModel, unsigned aSymbolPinCount, - bool aResolve ); + static std::unique_ptr Create( TYPE aType, unsigned aSymbolPinCount ); + static std::unique_ptr Create( const SIM_MODEL& aBaseModel, + unsigned aSymbolPinCount ); template static std::unique_ptr Create( const SIM_MODEL& aBaseModel, unsigned aSymbolPinCount, - const std::vector& aFields, bool aResolve ); + const std::vector& aFields ); template static std::unique_ptr Create( unsigned aSymbolPinCount, - const std::vector& aFields, bool aResolve ); + const std::vector& aFields ); template static std::string GetFieldValue( const std::vector* aFields, const std::string& aFieldName ); @@ -602,9 +599,6 @@ public: void SetParamValue( const std::string& aParamName, const std::string& aValue, SIM_VALUE::NOTATION aNotation = SIM_VALUE::NOTATION::SI ); - void SetParamSource( const std::string& aParamName, const wxString& aSource ); - void SetParamSource( int aParamIndex, const wxString& aSource ); - bool HasOverrides() const; bool HasNonInstanceOverrides() const; bool HasSpiceNonInstanceOverrides() const; @@ -619,7 +613,7 @@ public: bool IsInferred() const { return m_isInferred; } protected: - static std::unique_ptr Create( TYPE aType, bool aResolve ); + static std::unique_ptr Create( TYPE aType ); SIM_MODEL( TYPE aType ); SIM_MODEL( TYPE aType, std::unique_ptr aSpiceGenerator ); @@ -644,8 +638,6 @@ protected: const SIM_MODEL* m_baseModel; private: - static std::unique_ptr doCreate( TYPE aType ); - static TYPE readTypeFromSpiceStrings( const std::string& aTypeString, const std::string& aLevel = "", const std::string& aVersion = "", diff --git a/eeschema/sim/sim_model_kibis.cpp b/eeschema/sim/sim_model_kibis.cpp index 154eeb8e50..44f6e6e272 100644 --- a/eeschema/sim/sim_model_kibis.cpp +++ b/eeschema/sim/sim_model_kibis.cpp @@ -61,11 +61,11 @@ std::vector SPICE_GENERATOR_KIBIS::CurrentNames( const SPICE_ITEM& std::string SPICE_GENERATOR_KIBIS::IbisDevice( const SPICE_ITEM& aItem, const std::string aCwd, const std::string aCacheDir ) const { - std::string ibisLibFilename = SIM_MODEL::GetFieldValue( &aItem.fields, SIM_LIBRARY::LIBRARY_FIELD ); - std::string ibisCompName = SIM_MODEL::GetFieldValue( &aItem.fields, SIM_LIBRARY::NAME_FIELD ); - std::string ibisPinName = SIM_MODEL::GetFieldValue( &aItem.fields, SIM_LIBRARY_KIBIS::PIN_FIELD ); - std::string ibisModelName = SIM_MODEL::GetFieldValue( &aItem.fields, SIM_LIBRARY_KIBIS::MODEL_FIELD ); - bool diffMode = SIM_MODEL::GetFieldValue( &aItem.fields, SIM_LIBRARY_KIBIS::DIFF_FIELD ) == "1"; + std::string ibisLibFilename = SIM_MODEL::GetFieldValue( aItem.fields, SIM_LIBRARY::LIBRARY_FIELD ); + std::string ibisCompName = SIM_MODEL::GetFieldValue( aItem.fields, SIM_LIBRARY::NAME_FIELD ); + std::string ibisPinName = SIM_MODEL::GetFieldValue( aItem.fields, SIM_LIBRARY_KIBIS::PIN_FIELD ); + std::string ibisModelName = SIM_MODEL::GetFieldValue( aItem.fields, SIM_LIBRARY_KIBIS::MODEL_FIELD ); + bool diffMode = SIM_MODEL::GetFieldValue( aItem.fields, SIM_LIBRARY_KIBIS::DIFF_FIELD ) == "1"; wxFileName libPath = wxFileName( wxString( ibisLibFilename ) ); @@ -267,8 +267,7 @@ void SIM_MODEL_KIBIS::SwitchSingleEndedDiff( bool aDiff ) } } -SIM_MODEL_KIBIS::SIM_MODEL_KIBIS( TYPE aType, const SIM_MODEL_KIBIS& aSource, bool aResolve ) : - SIM_MODEL_KIBIS( aType ) +SIM_MODEL_KIBIS::SIM_MODEL_KIBIS( TYPE aType, const SIM_MODEL_KIBIS& aSource ) : SIM_MODEL_KIBIS( aType ) { for( PARAM& param1 : m_params ) { @@ -279,12 +278,6 @@ SIM_MODEL_KIBIS::SIM_MODEL_KIBIS( TYPE aType, const SIM_MODEL_KIBIS& aSource, bo if( param1.info.name == param2.info.name ) *( param1.value ) = *( param2.value ); } - - if( !aResolve ) - { - param1.source = param1.value->ToString(); - param1.resolved = false; - } } m_componentName = aSource.m_componentName; @@ -294,15 +287,15 @@ SIM_MODEL_KIBIS::SIM_MODEL_KIBIS( TYPE aType, const SIM_MODEL_KIBIS& aSource, bo } SIM_MODEL_KIBIS::SIM_MODEL_KIBIS( TYPE aType, SIM_MODEL_KIBIS& aSource, - const std::vector& aFields, bool aResolve ) : - SIM_MODEL_KIBIS( aType, aSource, aResolve ) + const std::vector& aFields ) : + SIM_MODEL_KIBIS( aType, aSource ) { ReadDataFields( 2, &aFields ); } SIM_MODEL_KIBIS::SIM_MODEL_KIBIS( TYPE aType, SIM_MODEL_KIBIS& aSource, - const std::vector& aFields, bool aResolve ) : - SIM_MODEL_KIBIS( aType, aSource, aResolve ) + const std::vector& aFields ) : + SIM_MODEL_KIBIS( aType, aSource ) { ReadDataFields( 2, &aFields ); } diff --git a/eeschema/sim/sim_model_kibis.h b/eeschema/sim/sim_model_kibis.h index 8a243b4197..97f5d6139d 100644 --- a/eeschema/sim/sim_model_kibis.h +++ b/eeschema/sim/sim_model_kibis.h @@ -63,12 +63,10 @@ public: // @brief Special copy constructor // creates a a model with aType, but tries to match parameters from aSource. - SIM_MODEL_KIBIS( TYPE aType, const SIM_MODEL_KIBIS& aSource, bool aResolve ); + SIM_MODEL_KIBIS( TYPE aType, const SIM_MODEL_KIBIS& aSource ); - SIM_MODEL_KIBIS( TYPE aType, SIM_MODEL_KIBIS& aSource, const std::vector& aFields, - bool aResolve ); - SIM_MODEL_KIBIS( TYPE aType, SIM_MODEL_KIBIS& aSource, const std::vector& aFields, - bool aResolve ); + SIM_MODEL_KIBIS( TYPE aType, SIM_MODEL_KIBIS& aSource, const std::vector& aFields ); + SIM_MODEL_KIBIS( TYPE aType, SIM_MODEL_KIBIS& aSource, const std::vector& aFields ); std::vector> GetIbisPins() const { diff --git a/eeschema/sim/sim_model_spice.cpp b/eeschema/sim/sim_model_spice.cpp index a257eea02b..c302bc6ea0 100644 --- a/eeschema/sim/sim_model_spice.cpp +++ b/eeschema/sim/sim_model_spice.cpp @@ -56,8 +56,8 @@ std::string SPICE_GENERATOR_SPICE::Preview( const SPICE_ITEM& aItem ) const std::unique_ptr SIM_MODEL_SPICE::Create( const SIM_LIBRARY_SPICE& aLibrary, const std::string& aSpiceCode ) { - SIM_MODEL::TYPE modelType = SPICE_MODEL_PARSER::ReadType( aLibrary, aSpiceCode ); - auto model = static_cast( SIM_MODEL::Create( modelType, true ).release() ); + auto model = static_cast( + SIM_MODEL::Create( SPICE_MODEL_PARSER::ReadType( aLibrary, aSpiceCode ) ).release() ); try { diff --git a/eeschema/sim/sim_plot_frame.cpp b/eeschema/sim/sim_plot_frame.cpp index 1fb77f7d27..346d440776 100644 --- a/eeschema/sim/sim_plot_frame.cpp +++ b/eeschema/sim/sim_plot_frame.cpp @@ -588,8 +588,7 @@ void SIM_PLOT_FRAME::UpdateTunerValue( SCH_SYMBOL* aSymbol, const wxString& aVal if( item == aSymbol ) { SIM_LIB_MGR mgr( Prj() ); - SIM_MODEL& model = mgr.CreateModel( &m_schematicFrame->GetCurrentSheet(), *aSymbol, - true ).model; + SIM_MODEL& model = mgr.CreateModel( *aSymbol ).model; const SIM_MODEL::PARAM* tunerParam = model.GetTunerParam(); diff --git a/eeschema/sim/sim_property.cpp b/eeschema/sim/sim_property.cpp index b73f74381e..67177a2dd5 100644 --- a/eeschema/sim/sim_property.cpp +++ b/eeschema/sim/sim_property.cpp @@ -25,7 +25,10 @@ #include #include #include +#include #include +#include +#include wxBEGIN_EVENT_TABLE( SIM_VALIDATOR, wxValidator ) @@ -167,12 +170,87 @@ void SIM_VALIDATOR::onKeyDown( wxKeyEvent& aEvent ) } -bool SIM_VALIDATOR::Validate( wxWindow* aParent ) +wxBEGIN_EVENT_TABLE( SIM_BOOL_VALIDATOR, SIM_VALIDATOR ) +wxEND_EVENT_TABLE() + + +bool SIM_BOOL_VALIDATOR::Validate( wxWindow* aParent ) { return true; } +SIM_STRING_VALIDATOR::SIM_STRING_VALIDATOR( SIM_VALUE::TYPE aValueType, + SIM_VALUE_GRAMMAR::NOTATION aNotation ) + : SIM_VALIDATOR(), + m_valueType( aValueType ), + m_notation( aNotation ) +{ +} + + +wxObject* SIM_STRING_VALIDATOR::Clone() const +{ + return new SIM_STRING_VALIDATOR( *this ); +} + + +bool SIM_STRING_VALIDATOR::Validate( wxWindow* aParent ) +{ + if( !m_validatorWindow->IsEnabled() ) + return true; + + wxTextEntry* const textEntry = getTextEntry(); + + if( !textEntry ) + return false; + + return isValid( textEntry->GetValue() ); +} + + +bool SIM_STRING_VALIDATOR::TransferToWindow() +{ + return true; +} + + +bool SIM_STRING_VALIDATOR::TransferFromWindow() +{ + return true; +} + + +bool SIM_STRING_VALIDATOR::isValid( const wxString& aString ) +{ + return SIM_VALUE_GRAMMAR::IsValid( aString.ToStdString(), m_valueType, m_notation ); +} + + +wxTextEntry* SIM_STRING_VALIDATOR::getTextEntry() +{ + if( !m_validatorWindow ) + return nullptr; + + // Taken from wxTextValidator. + + if( wxDynamicCast( m_validatorWindow, wxTextCtrl ) ) + return ( wxTextCtrl* ) m_validatorWindow; + + if( wxDynamicCast( m_validatorWindow, wxComboBox ) ) + return ( wxComboBox* ) m_validatorWindow; + + if( wxDynamicCast( m_validatorWindow, wxComboCtrl ) ) + return ( wxComboCtrl* ) m_validatorWindow; + + wxFAIL_MSG( + "SIM_STRING_VALIDATOR can only be used with wxTextCtrl, wxComboBox, or wxComboCtrl" + ); + + return nullptr; +} + + SIM_PROPERTY::SIM_PROPERTY( std::shared_ptr aLibrary, std::shared_ptr aModel, int aParamIndex ) @@ -201,7 +279,7 @@ SIM_BOOL_PROPERTY::SIM_BOOL_PROPERTY( const wxString& aLabel, const wxString& aN wxValidator* SIM_BOOL_PROPERTY::DoGetValidator() const { - return new SIM_VALIDATOR(); + return new SIM_BOOL_VALIDATOR(); } @@ -224,21 +302,21 @@ void SIM_BOOL_PROPERTY::OnSetValue() SIM_STRING_PROPERTY::SIM_STRING_PROPERTY( const wxString& aLabel, const wxString& aName, std::shared_ptr aLibrary, std::shared_ptr aModel, - int aParamIndex ) + int aParamIndex, + SIM_VALUE::TYPE aValueType, + SIM_VALUE_GRAMMAR::NOTATION aNotation ) : wxStringProperty( aLabel, aName ), - SIM_PROPERTY( aLibrary, aModel, aParamIndex ) + SIM_PROPERTY( aLibrary, aModel, aParamIndex ), + m_valueType( aValueType ), + m_notation( aNotation ) { - const SIM_MODEL::PARAM& param = GetParam(); - - wxASSERT( !param.resolved ); - - SetValueFromString( param.source ); + SetValueFromString( GetParam().value->ToString() ); } wxValidator* SIM_STRING_PROPERTY::DoGetValidator() const { - return new SIM_VALIDATOR(); + return new SIM_STRING_VALIDATOR( m_valueType, m_notation ); } @@ -251,13 +329,6 @@ bool SIM_STRING_PROPERTY::StringToValue( wxVariant& aVariant, const wxString& aT // TODO: Don't use string comparison. if( m_model->GetBaseModel() && ( aText == "" || aText == baseParamValue ) ) { - // TODO: do we want this magic of clearing overrides? - // Consider the case where someone uses a library model set to 220u and overrides it to - // 330u. Someone then modifies the library to use 330u. But that doesn't work either, - // so they modify the library again to 470u. If the overridden symbol was edited in the - // middle (to set some other parameter perhaps), it suddenly gets changed to 470u, which - // will be a surprise to the user. - // NOTE: other properties also contain this magic. try { m_model->SetParamValue( m_paramIndex, "" ); // Nullify. @@ -271,8 +342,8 @@ bool SIM_STRING_PROPERTY::StringToValue( wxVariant& aVariant, const wxString& aT } else { - m_model->SetParamSource( m_paramIndex, aText ); - aVariant = aText; + m_model->SetParamValue( m_paramIndex, std::string( aText.ToUTF8() ) ); + aVariant = GetParam().value->ToString(); } return true; @@ -293,7 +364,9 @@ static wxArrayString convertStringsToWx( const std::vector& aString SIM_ENUM_PROPERTY::SIM_ENUM_PROPERTY( const wxString& aLabel, const wxString& aName, std::shared_ptr aLibrary, std::shared_ptr aModel, - int aParamIndex ) + int aParamIndex, + SIM_VALUE::TYPE aValueType, + SIM_VALUE_GRAMMAR::NOTATION aNotation ) : wxEnumProperty( aLabel, aName, convertStringsToWx( aModel->GetParam( aParamIndex ).info.enumValues ) ), SIM_PROPERTY( aLibrary, aModel, aParamIndex ) diff --git a/eeschema/sim/sim_property.h b/eeschema/sim/sim_property.h index 794b900346..74ddab541a 100644 --- a/eeschema/sim/sim_property.h +++ b/eeschema/sim/sim_property.h @@ -33,8 +33,6 @@ #include -// This doesn't actually do any validation, but it's a convenient place to fix some navigation -// issues with wxPropertyGrid. class SIM_VALIDATOR : public wxValidator { private: @@ -42,8 +40,41 @@ private: void onKeyDown( wxKeyEvent& aEvent ); + wxDECLARE_EVENT_TABLE(); +}; + + +class SIM_STRING_VALIDATOR : public SIM_VALIDATOR +{ +public: + SIM_STRING_VALIDATOR( SIM_VALUE::TYPE aValueType, SIM_VALUE_GRAMMAR::NOTATION aNotation ); + SIM_STRING_VALIDATOR( const SIM_STRING_VALIDATOR& aValidator ) = default; + + wxObject* Clone() const override; + + bool Validate( wxWindow* aParent ) override; + bool TransferToWindow() override; + bool TransferFromWindow() override; + +private: + bool isValid( const wxString& aString ); + + wxTextEntry* getTextEntry(); + + SIM_VALUE::TYPE m_valueType; + SIM_VALUE_GRAMMAR::NOTATION m_notation; +}; + + +class SIM_BOOL_VALIDATOR : public SIM_VALIDATOR +{ +public: + SIM_BOOL_VALIDATOR() : SIM_VALIDATOR() {} + SIM_BOOL_VALIDATOR( const SIM_BOOL_VALIDATOR& aValidator ) = default; + bool Validate( wxWindow* aParent ) override; +private: wxDECLARE_EVENT_TABLE(); }; @@ -86,12 +117,18 @@ public: SIM_STRING_PROPERTY( const wxString& aLabel, const wxString& aName, std::shared_ptr aLibrary, std::shared_ptr aModel, - int aParamIndex ); + int aParamIndex, + SIM_VALUE::TYPE aValueType = SIM_VALUE::TYPE_FLOAT, + SIM_VALUE_GRAMMAR::NOTATION aNotation = SIM_VALUE_GRAMMAR::NOTATION::SI ); wxValidator* DoGetValidator() const override; bool StringToValue( wxVariant& aVariant, const wxString& aText, int aArgFlags = 0 ) const override; + +protected: + SIM_VALUE::TYPE m_valueType; + SIM_VALUE_GRAMMAR::NOTATION m_notation; }; @@ -101,7 +138,9 @@ public: SIM_ENUM_PROPERTY( const wxString& aLabel, const wxString& aName, std::shared_ptr aLibrary, std::shared_ptr aModel, - int aParamIndex ); + int aParamIndex, + SIM_VALUE::TYPE aValueType = SIM_VALUE::TYPE_FLOAT, + SIM_VALUE_GRAMMAR::NOTATION aNotation = SIM_VALUE_GRAMMAR::NOTATION::SI ); bool IntToValue( wxVariant& aVariant, int aNumber, int aArgFlags = 0 ) const override; }; diff --git a/eeschema/sim/spice_generator.h b/eeschema/sim/spice_generator.h index 278bf1d5d8..fe9e38b661 100644 --- a/eeschema/sim/spice_generator.h +++ b/eeschema/sim/spice_generator.h @@ -38,7 +38,7 @@ struct SPICE_ITEM std::string baseModelName; std::string modelName; const SIM_MODEL* model = nullptr; - std::vector fields; + const std::vector* fields = nullptr; }; diff --git a/eeschema/tools/sch_editor_control.cpp b/eeschema/tools/sch_editor_control.cpp index 1ad9834ee7..d25ddeced9 100644 --- a/eeschema/tools/sch_editor_control.cpp +++ b/eeschema/tools/sch_editor_control.cpp @@ -874,7 +874,6 @@ int SCH_EDITOR_CONTROL::SimProbe( const TOOL_EVENT& aEvent ) { EE_SELECTION_TOOL* selTool = m_toolMgr->GetTool(); EDA_ITEM* item = selTool->GetNode( aPosition ); - SCH_SHEET_PATH& sheet = m_frame->GetCurrentSheet(); if( !item ) return false; @@ -888,10 +887,10 @@ int SCH_EDITOR_CONTROL::SimProbe( const TOOL_EVENT& aEvent ) std::vector pins = symbol->GetLibPins(); SIM_LIB_MGR mgr( m_frame->Prj() ); - SIM_MODEL& model = mgr.CreateModel( &sheet, *symbol, true ).model; + SIM_MODEL& model = mgr.CreateModel( *symbol ).model; SPICE_ITEM spiceItem; - spiceItem.refName = std::string( symbol->GetRef( &sheet ).ToUTF8() ); + spiceItem.refName = std::string( symbol->GetRef( &m_frame->GetCurrentSheet() ).ToUTF8() ); std::vector currentNames = model.SpiceGenerator().CurrentNames( spiceItem );