diff --git a/eeschema/dialogs/dialog_sim_model.cpp b/eeschema/dialogs/dialog_sim_model.cpp index b569a84f93..dceaa531dc 100644 --- a/eeschema/dialogs/dialog_sim_model.cpp +++ b/eeschema/dialogs/dialog_sim_model.cpp @@ -139,8 +139,8 @@ bool DIALOG_SIM_MODEL::TransferDataToWindow() wxString pinMap; // Infer RLC and VI models if they aren't specified - if( SIM_MODEL::InferSimModel( m_symbol, false, SIM_VALUE_GRAMMAR::NOTATION::SI, - &deviceType, &modelType, &modelParams, &pinMap ) ) + if( SIM_MODEL::InferSimModel( m_symbol, &m_fields, false, SIM_VALUE_GRAMMAR::NOTATION::SI, + &deviceType, &modelType, &modelParams, &pinMap ) ) { m_fields.emplace_back( &m_symbol, -1, SIM_MODEL::DEVICE_TYPE_FIELD ); m_fields.back().SetText( deviceType ); diff --git a/eeschema/netlist_exporters/netlist_exporter_spice.cpp b/eeschema/netlist_exporters/netlist_exporter_spice.cpp index d00e770fb2..f7e1c32116 100644 --- a/eeschema/netlist_exporters/netlist_exporter_spice.cpp +++ b/eeschema/netlist_exporters/netlist_exporter_spice.cpp @@ -238,10 +238,9 @@ bool NETLIST_EXPORTER_SPICE::ReadSchematicAndLibraries( unsigned aNetlistOptions wxString pinMap; // Infer RLC and VI models if they aren't specified - if( SIM_MODEL::InferSimModel( *symbol, true, - SIM_VALUE_GRAMMAR::NOTATION::SPICE, - &deviceType, &modelType, - &modelParams, &pinMap ) ) + if( SIM_MODEL::InferSimModel( *symbol, &spiceItem.fields, true, + SIM_VALUE_GRAMMAR::NOTATION::SPICE, &deviceType, + &modelType, &modelParams, &pinMap ) ) { spiceItem.fields.emplace_back( symbol, -1, SIM_MODEL::DEVICE_TYPE_FIELD ); spiceItem.fields.back().SetText( deviceType ); diff --git a/eeschema/sim/sim_lib_mgr.cpp b/eeschema/sim/sim_lib_mgr.cpp index 55350549a1..c628461116 100644 --- a/eeschema/sim/sim_lib_mgr.cpp +++ b/eeschema/sim/sim_lib_mgr.cpp @@ -203,10 +203,8 @@ SIM_LIBRARY::MODEL SIM_LIB_MGR::CreateModel( const SCH_SHEET_PATH* aSheetPath, S wxString pinMap; // Infer RLC and VI models if they aren't specified - if( SIM_MODEL::InferSimModel( aSymbol, true, - SIM_VALUE_GRAMMAR::NOTATION::SI, - &deviceType, &modelType, &modelParams, - &pinMap ) ) + if( SIM_MODEL::InferSimModel( aSymbol, &fields, true, SIM_VALUE_GRAMMAR::NOTATION::SI, + &deviceType, &modelType, &modelParams, &pinMap ) ) { fields.emplace_back( &aSymbol, -1, SIM_MODEL::DEVICE_TYPE_FIELD ); fields.back().SetText( deviceType ); diff --git a/eeschema/sim/sim_model.cpp b/eeschema/sim/sim_model.cpp index 570596bbaa..d032aa823a 100644 --- a/eeschema/sim/sim_model.cpp +++ b/eeschema/sim/sim_model.cpp @@ -524,7 +524,8 @@ template std::unique_ptr SIM_MODEL::Create( const std::vector -std::string SIM_MODEL::GetFieldValue( const std::vector* aFields, const std::string& aFieldName ) +std::string SIM_MODEL::GetFieldValue( const std::vector* aFields, const std::string& aFieldName, + bool aResolve ) { static_assert( std::is_same::value || std::is_same::value ); @@ -537,17 +538,19 @@ std::string SIM_MODEL::GetFieldValue( const std::vector* aFields, const std:: return field.GetName() == aFieldName; } ); - if( it != aFields->end() ) + if( it == aFields->end() ) + return ""; + else if( aResolve ) + return std::string( it->GetShownText().ToUTF8() ); + else return std::string( it->GetText().ToUTF8() ); - - return ""; } // This specialization is used when no fields are passed. template <> std::string SIM_MODEL::GetFieldValue( const std::vector* aFields, - const std::string& aFieldName ) + const std::string& aFieldName, bool aResolve ) { return ""; } @@ -1050,7 +1053,7 @@ bool SIM_MODEL::requiresSpiceModelLine() const template -bool SIM_MODEL::InferSimModel( T_symbol& aSymbol, bool aResolve, +bool SIM_MODEL::InferSimModel( T_symbol& aSymbol, std::vector* aFields, bool aResolve, SIM_VALUE_GRAMMAR::NOTATION aNotation, wxString* aDeviceType, wxString* aModelType, wxString* aModelParams, wxString* aPinMap ) { @@ -1071,38 +1074,25 @@ bool SIM_MODEL::InferSimModel( T_symbol& aSymbol, bool aResolve, return units; }; - auto getFieldValue = - [&]( const wxString& fieldName ) -> wxString - { - T_field* field = aSymbol.FindField( fieldName ); - - if( field ) - { - if( aResolve ) - return field->GetShownText(); - else - return field->GetText(); - } - - return wxEmptyString; - }; - wxString prefix = aSymbol.GetPrefix(); - wxString value = getFieldValue( VALUE_FIELD ); + wxString value = GetFieldValue( aFields, VALUE_FIELD, aResolve ); std::vector pins = aSymbol.GetAllLibPins(); - *aDeviceType = getFieldValue( DEVICE_TYPE_FIELD ); - *aModelType = getFieldValue( TYPE_FIELD ); - *aModelParams = getFieldValue( PARAMS_FIELD ); - *aPinMap = getFieldValue( PINS_FIELD ); + *aDeviceType = GetFieldValue( aFields, DEVICE_TYPE_FIELD, aResolve ); + *aModelType = GetFieldValue( aFields, TYPE_FIELD, aResolve ); + *aModelParams = GetFieldValue( aFields, PARAMS_FIELD, aResolve ); + *aPinMap = GetFieldValue( aFields, PINS_FIELD, aResolve ); if( pins.size() != 2 ) return false; - if( aDeviceType->IsEmpty() - && aModelType->IsEmpty() - && !value.IsEmpty() - && ( prefix.StartsWith( "R" ) || prefix.StartsWith( "L" ) || prefix.StartsWith( "C" ) ) ) + if( ( ( *aDeviceType == "R" || *aDeviceType == "L" || *aDeviceType == "C" ) + && aModelType->IsEmpty() ) + || + ( aDeviceType->IsEmpty() + && aModelType->IsEmpty() + && !value.IsEmpty() + && ( prefix.StartsWith( "R" ) || prefix.StartsWith( "L" ) || prefix.StartsWith( "C" ) ) ) ) { if( aDeviceType->IsEmpty() ) *aDeviceType = prefix.Left( 1 ); @@ -1123,7 +1113,7 @@ bool SIM_MODEL::InferSimModel( T_symbol& aSymbol, bool aResolve, wxString valueExponent( idealVal.GetMatch( value, 2 ) ); wxString valueFraction( idealVal.GetMatch( value, 6 ) ); - if( valueMantissa.Contains( wxT( "." ) ) ) + if( valueMantissa.Contains( wxT( "." ) ) || valueFraction.IsEmpty() ) { aModelParams->Printf( wxT( "%s=\"%s%s\"" ), prefix.Left(1).Lower(), @@ -1152,13 +1142,13 @@ bool SIM_MODEL::InferSimModel( T_symbol& aSymbol, bool aResolve, return true; } - if( ( ( *aDeviceType == wxT( "V" ) || *aDeviceType == wxT( "I" ) ) - && aModelType->IsEmpty() ) - || - ( aDeviceType->IsEmpty() - && aModelType->IsEmpty() - && !value.IsEmpty() - && ( prefix.StartsWith( "V" ) || prefix.StartsWith( "I" ) ) ) ) + if( ( ( *aDeviceType == wxT( "V" ) || *aDeviceType == wxT( "I" ) ) + && aModelType->IsEmpty() ) + || + ( aDeviceType->IsEmpty() + && aModelType->IsEmpty() + && !value.IsEmpty() + && ( prefix.StartsWith( "V" ) || prefix.StartsWith( "I" ) ) ) ) { if( aDeviceType->IsEmpty() ) *aDeviceType = prefix.Left( 1 ); @@ -1185,7 +1175,7 @@ bool SIM_MODEL::InferSimModel( T_symbol& aSymbol, bool aResolve, wxString valueExponent( sourceVal.GetMatch( value, 2 ) ); wxString valueFraction( sourceVal.GetMatch( value, 6 ) ); - if( valueMantissa.Contains( wxT( "." ) ) ) + if( valueMantissa.Contains( wxT( "." ) ) || valueFraction.IsEmpty() ) { aModelParams->Printf( wxT( "dc=\"%s%s\"" ), valueMantissa, @@ -1215,13 +1205,17 @@ bool SIM_MODEL::InferSimModel( T_symbol& aSymbol, bool aResolve, } -template bool SIM_MODEL::InferSimModel( SCH_SYMBOL& aSymbol, bool aResolve, +template bool SIM_MODEL::InferSimModel( SCH_SYMBOL& aSymbol, + std::vector* aFields, + bool aResolve, SIM_VALUE_GRAMMAR::NOTATION aNotation, wxString* aDeviceType, wxString* aModelType, wxString* aModelParams, wxString* aPinMap ); -template bool SIM_MODEL::InferSimModel( LIB_SYMBOL& aSymbol, bool aResolve, +template bool SIM_MODEL::InferSimModel( LIB_SYMBOL& aSymbol, + std::vector* aFields, + bool aResolve, SIM_VALUE_GRAMMAR::NOTATION aNotation, wxString* aDeviceType, wxString* aModelType, diff --git a/eeschema/sim/sim_model.h b/eeschema/sim/sim_model.h index b71c7ce897..2aa0ff4f81 100644 --- a/eeschema/sim/sim_model.h +++ b/eeschema/sim/sim_model.h @@ -415,7 +415,8 @@ public: const std::vector& aPins ); template - static std::string GetFieldValue( const std::vector* aFields, const std::string& aFieldName ); + static std::string GetFieldValue( const std::vector* aFields, const std::string& aFieldName, + bool aResolve = true ); template static void SetFieldValue( std::vector& aFields, const std::string& aFieldName, @@ -519,7 +520,7 @@ public: virtual void SwitchSingleEndedDiff( bool aDiff ) { }; template - static bool InferSimModel( T_symbol& aSymbol, bool aResolve, + static bool InferSimModel( T_symbol& aSymbol, std::vector* aFields, bool aResolve, SIM_VALUE_GRAMMAR::NOTATION aNotation, wxString* aDeviceType, wxString* aModelType, wxString* aModelParams, wxString* aPinMap ); diff --git a/eeschema/sim/sim_value.cpp b/eeschema/sim/sim_value.cpp index ca5f6871fa..f6002336e9 100644 --- a/eeschema/sim/sim_value.cpp +++ b/eeschema/sim/sim_value.cpp @@ -509,6 +509,8 @@ bool SIM_VALUE_FLOAT::FromString( const std::string& aString, NOTATION aNotation try { + LOCALE_IO toggle; + m_value = std::stod( parseResult.significand ) * std::pow( 10, exponent ); } catch( const std::invalid_argument& )