diff --git a/eeschema/netlist_exporters/netlist_exporter_spice.cpp b/eeschema/netlist_exporters/netlist_exporter_spice.cpp index 63dbb172ea..7a00f3abad 100644 --- a/eeschema/netlist_exporters/netlist_exporter_spice.cpp +++ b/eeschema/netlist_exporters/netlist_exporter_spice.cpp @@ -230,20 +230,28 @@ bool NETLIST_EXPORTER_SPICE::ReadSchematicAndLibraries( unsigned aNetlistOptions spiceItem.fields.back().SetText( symbol->GetFields()[i].GetShownText( 0, false ) ); } - // Infer RLC passive models if they aren't specified + // Infer RLC models if they aren't specified if( !symbol->FindField( SIM_MODEL::DEVICE_TYPE_FIELD, false ) && !symbol->FindField( SIM_MODEL::PARAMS_FIELD, false ) ) { - wxString simParams = SIM_MODEL_IDEAL::InferSimParams( symbol->GetPrefix(), - symbol->GetValueFieldText( true ) ); + // pair.first: wxString sim model type + // pair.second: wxString sim model parameters + auto model = SIM_MODEL_IDEAL::InferSimModel( symbol->GetPrefix(), + symbol->GetValueFieldText( true ) ); - if( !simParams.IsEmpty() ) + if( !model.second.IsEmpty() ) { spiceItem.fields.emplace_back( VECTOR2I(), -1, symbol, SIM_MODEL::DEVICE_TYPE_FIELD ); spiceItem.fields.back().SetText( symbol->GetPrefix() ); + if( !model.first.IsEmpty() ) + { + spiceItem.fields.emplace_back( VECTOR2I(), -1, symbol, SIM_MODEL::TYPE_FIELD ); + spiceItem.fields.back().SetText( model.first ); + } + spiceItem.fields.emplace_back( VECTOR2I(), -1, symbol, SIM_MODEL::PARAMS_FIELD ); - spiceItem.fields.back().SetText( simParams ); + spiceItem.fields.back().SetText( model.second ); } } diff --git a/eeschema/sim/sim_lib_mgr.cpp b/eeschema/sim/sim_lib_mgr.cpp index e34a6cfd74..bbfeb52609 100644 --- a/eeschema/sim/sim_lib_mgr.cpp +++ b/eeschema/sim/sim_lib_mgr.cpp @@ -140,20 +140,28 @@ SIM_LIBRARY::MODEL SIM_LIB_MGR::CreateModel( const SCH_SHEET_PATH* aSheetPath, S fields.back().SetText( aSymbol.GetFields()[ i ].GetShownText( 0, false ) ); } - // Infer RLC passive models if they aren't specified + // Infer RLC models if they aren't specified if( !aSymbol.FindField( SIM_MODEL::DEVICE_TYPE_FIELD, false ) && !aSymbol.FindField( SIM_MODEL::PARAMS_FIELD, false ) ) { - wxString simParams = SIM_MODEL_IDEAL::InferSimParams( aSymbol.GetPrefix(), - aSymbol.GetValueFieldText( true ) ); + // pair.first: wxString sim model type + // pair.second: wxString sim model parameters + auto model = SIM_MODEL_IDEAL::InferSimModel( aSymbol.GetPrefix(), + aSymbol.GetValueFieldText( true ) ); - if( !simParams.IsEmpty() ) + if( !model.second.IsEmpty() ) { fields.emplace_back( VECTOR2I(), -1, &aSymbol, SIM_MODEL::DEVICE_TYPE_FIELD ); fields.back().SetText( aSymbol.GetPrefix() ); + if( !model.first.IsEmpty() ) + { + fields.emplace_back( VECTOR2I(), -1, &aSymbol, SIM_MODEL::TYPE_FIELD ); + fields.back().SetText( model.first ); + } + fields.emplace_back( VECTOR2I(), -1, &aSymbol, SIM_MODEL::PARAMS_FIELD ); - fields.back().SetText( simParams ); + fields.back().SetText( model.second ); } } diff --git a/eeschema/sim/sim_model.cpp b/eeschema/sim/sim_model.cpp index 0d61d2f213..8cc220f40c 100644 --- a/eeschema/sim/sim_model.cpp +++ b/eeschema/sim/sim_model.cpp @@ -51,6 +51,7 @@ #include #include +#include "wx/regex.h" using TYPE = SIM_MODEL::TYPE; @@ -1006,6 +1007,51 @@ bool SIM_MODEL::requiresSpiceModelLine() const } +std::pair SIM_MODEL::InferSimModel( const wxString& aPrefix, + const wxString& aValue ) +{ + wxString spiceModelType; + wxString spiceModelParams; + + if( !aValue.IsEmpty() ) + { + if( aPrefix.StartsWith( wxT( "R" ) ) + || aPrefix.StartsWith( wxT( "L" ) ) + || aPrefix.StartsWith( wxT( "C" ) ) ) + { + wxRegEx passiveVal( wxT( "^" + "([0-9\\. ]+)" + "([fFpPnNuUmMkKgGtTμµ𝛍𝜇𝝁 ]|M(e|E)(g|G))?" + "([fFhHΩΩ𝛀𝛺𝝮]|ohm)?" + "([-1-9 ]*)" + "$" ) ); + + if( passiveVal.Matches( aValue ) ) + { + wxString valuePrefix( passiveVal.GetMatch( aValue, 1 ) ); + wxString valueUnits( passiveVal.GetMatch( aValue, 2 ) ); + wxString valueSuffix( passiveVal.GetMatch( aValue, 6 ) ); + + if( valueUnits == wxT( "M" ) ) + valueUnits = wxT( "Meg" ); + + spiceModelParams = wxString::Format( wxT( "%s=\"%s%s\"" ), + aPrefix.Lower(), + valuePrefix, + valueUnits ); + } + else + { + spiceModelType = wxT( "=" ); + spiceModelParams = wxString::Format( wxT( "%s=\"%s\"" ), aPrefix.Lower(), aValue ); + } + } + } + + return std::make_pair( spiceModelType, spiceModelParams ); +} + + template void SIM_MODEL::MigrateSimModel( T_symbol& aSymbol ) { @@ -1120,7 +1166,7 @@ void SIM_MODEL::MigrateSimModel( T_symbol& aSymbol ) wxStringSplit( legacyPins->GetText(), pinIndexes, ' ' ); - if( SIM_MODEL_IDEAL::InferSimParams( prefix, value ).length() ) + if( SIM_MODEL::InferSimModel( prefix, value ).second.length() ) { if( pinIndexes[0] == wxT( "2" ) ) pins = "1=- 2=+"; diff --git a/eeschema/sim/sim_model.h b/eeschema/sim/sim_model.h index f8a3c2dc3b..c8e591c5de 100644 --- a/eeschema/sim/sim_model.h +++ b/eeschema/sim/sim_model.h @@ -519,6 +519,9 @@ public: } bool IsStoredInValue() const { return m_isStoredInValue; } + static std::pair InferSimModel( const wxString& aPrefix, + const wxString& aValue ); + template static void MigrateSimModel( T_symbol& aSymbol ); diff --git a/eeschema/sim/sim_model_ideal.cpp b/eeschema/sim/sim_model_ideal.cpp index cd23a3526f..520f037660 100644 --- a/eeschema/sim/sim_model_ideal.cpp +++ b/eeschema/sim/sim_model_ideal.cpp @@ -73,41 +73,6 @@ SIM_MODEL_IDEAL::SIM_MODEL_IDEAL( TYPE aType ) : } -wxString SIM_MODEL_IDEAL::InferSimParams( const wxString& aPrefix, const wxString& aValue ) -{ - wxString spiceModel; - - if( aPrefix.StartsWith( wxT( "R" ) ) - || aPrefix.StartsWith( wxT( "L" ) ) - || aPrefix.StartsWith( wxT( "C" ) ) ) - { - wxRegEx passiveVal( - wxT( "^([0-9\\. ]+)([fFpPnNuUmMkKgGtTμµ𝛍𝜇𝝁 ]|M(e|E)(g|G))?([fFhHΩΩ𝛀𝛺𝝮]|ohm)?([-1-9 ]*)$" ) ); - - if( passiveVal.Matches( aValue ) ) - { - wxString valuePrefix( passiveVal.GetMatch( aValue, 1 ) ); - wxString valueUnits( passiveVal.GetMatch( aValue, 2 ) ); - wxString valueSuffix( passiveVal.GetMatch( aValue, 6 ) ); - - if( valueUnits == wxT( "M" ) ) - valueUnits = wxT( "Meg" ); - - spiceModel = valuePrefix + valueUnits; - } - else - { - spiceModel = aValue; - } - } - - if( !spiceModel.IsEmpty() ) - return wxString::Format( wxT( "%s=\"%s\"" ), aPrefix.Lower(), spiceModel ); - else - return wxEmptyString; -} - - SIM_MODEL::PARAM::INFO SIM_MODEL_IDEAL::makeParamInfo( std::string aName, std::string aDescription, std::string aUnit ) { diff --git a/eeschema/sim/sim_model_ideal.h b/eeschema/sim/sim_model_ideal.h index 1c97af0c1f..2cd57d2a8a 100644 --- a/eeschema/sim/sim_model_ideal.h +++ b/eeschema/sim/sim_model_ideal.h @@ -49,8 +49,6 @@ public: const PARAM* GetTunerParam() const override { return &GetParam( 0 ); } bool HasPrimaryValue() const override { return true; } - static wxString InferSimParams( const wxString& aPrefix, const wxString& aValue ); - private: std::vector getPinNames() const override { return { "+", "-" }; }