diff --git a/eeschema/dialogs/dialog_sim_model.cpp b/eeschema/dialogs/dialog_sim_model.cpp index 300a51994f..6c0857ddbe 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() ) ); + m_models.push_back( SIM_MODEL::Create( type, m_sortedSymbolPins.size(), false ) ); SIM_MODEL::DEVICE_TYPE_ deviceType = SIM_MODEL::TypeInfo( type ).deviceType; @@ -138,7 +138,7 @@ bool DIALOG_SIM_MODEL::TransferDataToWindow() { wxCommandEvent dummyEvent; - int pinCount = m_sortedSymbolPins.size(); + unsigned 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 ); + m_models.at( static_cast( type ) ) = SIM_MODEL::Create( pinCount, m_fields, false ); } catch( const IO_ERROR& e ) { @@ -638,6 +638,7 @@ void DIALOG_SIM_MODEL::loadLibrary( const wxString& aFilePath ) return; } + unsigned pinCount = m_sortedSymbolPins.size(); m_tclibraryPathName->ChangeValue( aFilePath ); m_libraryModels.clear(); @@ -655,12 +656,12 @@ void DIALOG_SIM_MODEL::loadLibrary( const wxString& aFilePath ) { //TODO: it's not cur model. - m_libraryModels.push_back( - SIM_MODEL::Create( baseModel, m_sortedSymbolPins.size(), m_fields ) ); + m_libraryModels.push_back( SIM_MODEL::Create( baseModel, pinCount, m_fields, + false ) ); } else { - m_libraryModels.push_back( SIM_MODEL::Create( baseModel, m_sortedSymbolPins.size() ) ); + m_libraryModels.push_back( SIM_MODEL::Create( baseModel, pinCount, false ) ); } } } @@ -782,12 +783,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, SIM_VALUE::TYPE_INT ); + curModelSharedPtr(), aParamIndex ); break; case SIM_VALUE::TYPE_FLOAT: prop = new SIM_STRING_PROPERTY( paramDescription, param.info.name, m_library, - curModelSharedPtr(), aParamIndex, SIM_VALUE::TYPE_FLOAT ); + curModelSharedPtr(), aParamIndex ); break; //case TYPE_COMPLEX: @@ -797,14 +798,12 @@ 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, - SIM_VALUE::TYPE_STRING ); + curModelSharedPtr(), aParamIndex ); } else { prop = new SIM_ENUM_PROPERTY( paramDescription, param.info.name, m_library, - curModelSharedPtr(), aParamIndex, - SIM_VALUE::TYPE_STRING ); + curModelSharedPtr(), aParamIndex ); } break; @@ -1145,7 +1144,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 ) ) ); + new SIM_MODEL_KIBIS( type, *kibismodel, m_fields, false ) ) ); } m_curModelType = type; diff --git a/eeschema/netlist_exporters/netlist_exporter_spice.cpp b/eeschema/netlist_exporters/netlist_exporter_spice.cpp index 0604666d7f..42b4c7588b 100644 --- a/eeschema/netlist_exporters/netlist_exporter_spice.cpp +++ b/eeschema/netlist_exporters/netlist_exporter_spice.cpp @@ -210,11 +210,19 @@ bool NETLIST_EXPORTER_SPICE::ReadSchematicAndLibraries( unsigned aNetlistOptions SPICE_ITEM spiceItem; - // 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(); + 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 ) ); + } try { @@ -376,7 +384,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( aSymbol ); + SIM_LIBRARY::MODEL libModel = m_libMgr.CreateModel( &aSheet, aSymbol, true ); aItem.baseModelName = libModel.name; aItem.model = &libModel.model; diff --git a/eeschema/sch_field.cpp b/eeschema/sch_field.cpp index 93c40500ee..df90459292 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() ) + if( IsNameShown() && aAllowExtraText ) 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 0bc86fa661..42e0521f40 100644 --- a/eeschema/sim/sim_lib_mgr.cpp +++ b/eeschema/sim/sim_lib_mgr.cpp @@ -35,12 +35,31 @@ SIM_LIB_MGR::SIM_LIB_MGR( const PROJECT& aPrj ) : m_project( aPrj ) } -SIM_LIBRARY::MODEL SIM_LIB_MGR::CreateModel( SCH_SYMBOL& aSymbol ) +SIM_LIBRARY::MODEL SIM_LIB_MGR::CreateModel( const SCH_SHEET_PATH* aSheetPath, SCH_SYMBOL& aSymbol, + bool aResolve ) { - std::vector pins = aSymbol.GetLibPins(); - SCH_FIELD* libraryField = aSymbol.FindField( SIM_LIBRARY::LIBRARY_FIELD ); + int pinCount = aSymbol.GetLibPins().size(); + 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(); @@ -55,10 +74,9 @@ SIM_LIBRARY::MODEL SIM_LIB_MGR::CreateModel( SCH_SYMBOL& aSymbol ) } 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 ); @@ -74,20 +92,17 @@ SIM_LIBRARY::MODEL SIM_LIB_MGR::CreateModel( SCH_SYMBOL& aSymbol ) 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, - static_cast( pins.size() ), - aSymbol.GetFields() ) ); + m_models.push_back( SIM_MODEL::Create( *baseModel, pinCount, fields, aResolve ) ); } else { - m_models.push_back( SIM_MODEL::Create( static_cast( pins.size() ), - aSymbol.GetFields() ) ); + m_models.push_back( SIM_MODEL::Create( pinCount, fields, aResolve ) ); } return { baseModelName, *m_models.back() }; diff --git a/eeschema/sim/sim_lib_mgr.h b/eeschema/sim/sim_lib_mgr.h index 84c5c19a36..e70b5345a0 100644 --- a/eeschema/sim/sim_lib_mgr.h +++ b/eeschema/sim/sim_lib_mgr.h @@ -44,7 +44,8 @@ public: virtual ~SIM_LIB_MGR() = default; // TODO: The argument can be made const. - SIM_LIBRARY::MODEL CreateModel( SCH_SYMBOL& aSymbol ); + SIM_LIBRARY::MODEL CreateModel( const SCH_SHEET_PATH* aSheetPath, SCH_SYMBOL& aSymbol, + bool aResolve ); SIM_LIBRARY& CreateLibrary( const std::string& aLibraryPath ); diff --git a/eeschema/sim/sim_library_kibis.cpp b/eeschema/sim/sim_library_kibis.cpp index 4d2feb0e06..2e551a70d4 100644 --- a/eeschema/sim/sim_library_kibis.cpp +++ b/eeschema/sim/sim_library_kibis.cpp @@ -23,11 +23,8 @@ #include #include -#include #include #include -#include -#include void SIM_LIBRARY_KIBIS::ReadFile( const std::string& aFilePath, SIM_MODEL::TYPE aType ) @@ -45,7 +42,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 ) ); + m_models.push_back( SIM_MODEL::Create( aType, pinNumber, true ) ); 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 a8083e0039..d53b248dfc 100644 --- a/eeschema/sim/sim_model.cpp +++ b/eeschema/sim/sim_model.cpp @@ -42,7 +42,6 @@ #include #include -#include #include #include #include @@ -566,9 +565,9 @@ void SIM_MODEL::WriteDataLibFields( std::vector& aFields ) const } -std::unique_ptr SIM_MODEL::Create( TYPE aType, unsigned aSymbolPinCount ) +std::unique_ptr SIM_MODEL::Create( TYPE aType, unsigned aSymbolPinCount, bool aResolve ) { - std::unique_ptr model = Create( aType ); + std::unique_ptr model = Create( aType, aResolve ); // Passing nullptr to ReadDataFields will make it act as if all fields were empty. model->ReadDataFields( aSymbolPinCount, static_cast*>( nullptr ) ); @@ -576,10 +575,10 @@ std::unique_ptr SIM_MODEL::Create( TYPE aType, unsigned aSymbolPinCou } -std::unique_ptr SIM_MODEL::Create( const SIM_MODEL& aBaseModel, - unsigned aSymbolPinCount ) +std::unique_ptr SIM_MODEL::Create( const SIM_MODEL& aBaseModel, unsigned aSymbolPinCount, + bool aResolve ) { - std::unique_ptr model = Create( aBaseModel.GetType() ); + std::unique_ptr model = Create( aBaseModel.GetType(), aResolve ); model->SetBaseModel( aBaseModel ); model->ReadDataFields( aSymbolPinCount, static_cast*>( nullptr ) ); @@ -589,7 +588,7 @@ std::unique_ptr SIM_MODEL::Create( const SIM_MODEL& aBaseModel, template std::unique_ptr SIM_MODEL::Create( const SIM_MODEL& aBaseModel, unsigned aSymbolPinCount, - const std::vector& aFields ) + const std::vector& aFields, bool aResolve ) { TYPE type = ReadTypeFromFields( aFields, aSymbolPinCount ); @@ -597,7 +596,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 ); + std::unique_ptr model = Create( type, aResolve ); model->SetBaseModel( aBaseModel ); model->ReadDataFields( aSymbolPinCount, &aFields ); @@ -606,32 +605,36 @@ 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 ); + const std::vector& aFields, + bool aResolve ); template std::unique_ptr SIM_MODEL::Create( const SIM_MODEL& aBaseModel, unsigned aSymbolPinCount, - const std::vector& aFields ); + const std::vector& aFields, + bool aResolve ); template std::unique_ptr SIM_MODEL::Create( unsigned aSymbolPinCount, - const std::vector& aFields ) + const std::vector& aFields, bool aResolve ) { 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 ); + std::unique_ptr model = SIM_MODEL::Create( type, aResolve ); model->ReadDataFields( aSymbolPinCount, &aFields ); return model; } template std::unique_ptr SIM_MODEL::Create( unsigned aSymbolPinCount, - const std::vector& aFields ); + const std::vector& aFields, + bool aResolve ); template std::unique_ptr SIM_MODEL::Create( unsigned aSymbolPinCount, - const std::vector& aFields ); + const std::vector& aFields, + bool aResolve ); template @@ -747,10 +750,17 @@ std::vector> SIM_MODEL::GetPins() c const SIM_MODEL::PARAM& SIM_MODEL::GetParam( unsigned aParamIndex ) const { - if( m_baseModel && m_params.at( aParamIndex ).value->ToString() == "" ) - return m_baseModel->GetParam( aParamIndex ); - else - return m_params.at( aParamIndex ); + 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; } @@ -849,6 +859,33 @@ 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 ) @@ -885,7 +922,7 @@ bool SIM_MODEL::HasSpiceNonInstanceOverrides() const } -std::unique_ptr SIM_MODEL::Create( TYPE aType ) +std::unique_ptr SIM_MODEL::doCreate( TYPE aType ) { switch( aType ) { @@ -966,6 +1003,23 @@ std::unique_ptr SIM_MODEL::Create( 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 ) ) { @@ -1052,11 +1106,12 @@ 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.value->ToString(); + std::string value = aParam.resolved ? aParam.value->ToString() : aParam.source.ToStdString(); + if( value.find( " " ) != std::string::npos ) value = "\"" + value + "\""; - result.append( fmt::format( "{}={}", aParam.info.name, value ) ); + result.append( fmt::format( "{}={}", name, value ) ); return result; } @@ -1097,7 +1152,9 @@ std::string SIM_MODEL::GenerateParamsField( const std::string& aPairSeparator ) for( const PARAM& param : m_params ) { - if( param.value->ToString() == "" ) + std::string value = param.resolved ? param.value->ToString() : param.source.ToStdString(); + + if( value.empty() ) continue; result.append( GenerateParamValuePair( param, isFirst ) ); @@ -1116,12 +1173,10 @@ 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 ) { @@ -1133,7 +1188,9 @@ 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() @@ -1142,7 +1199,10 @@ void SIM_MODEL::ParseParamsField( const std::string& aParamsField ) wxASSERT( paramName != "" ); // TODO: Shouldn't be named "...fromSpiceCode" here... - SetParamValue( paramName, node->string(), SIM_VALUE_GRAMMAR::NOTATION::SI ); + if( FindParam( paramName )->resolved ) + SetParamValue( paramName, node->string(), SIM_VALUE_GRAMMAR::NOTATION::SI ); + else + SetParamSource( paramName, node->string() ); } else if( node->is_type() ) { @@ -1151,7 +1211,10 @@ void SIM_MODEL::ParseParamsField( const std::string& aParamsField ) // Unescape quotes. boost::replace_all( str, "\\\"", "\"" ); - SetParamValue( paramName, str, SIM_VALUE_GRAMMAR::NOTATION::SI ); + if( FindParam( paramName )->resolved ) + SetParamValue( paramName, str, SIM_VALUE_GRAMMAR::NOTATION::SI ); + else + SetParamSource( paramName, str ); } else { @@ -1275,7 +1338,9 @@ 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 cda2396484..8daea6fce0 100644 --- a/eeschema/sim/sim_model.h +++ b/eeschema/sim/sim_model.h @@ -463,12 +463,15 @@ 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 ) - : value( SIM_VALUE::Create( aInfo.type ) ), + : resolved( true ), + value( SIM_VALUE::Create( aInfo.type ) ), info( aInfo ), isOtherVariant( aIsOtherVariant ) {} @@ -492,17 +495,17 @@ public: static TYPE InferTypeFromLegacyFields( const std::vector& aFields ); - static std::unique_ptr Create( TYPE aType, unsigned aSymbolPinCount ); - static std::unique_ptr Create( const SIM_MODEL& aBaseModel, - unsigned aSymbolPinCount ); + static std::unique_ptr Create( TYPE aType, unsigned aSymbolPinCount, bool aResolve ); + static std::unique_ptr Create( const SIM_MODEL& aBaseModel, unsigned aSymbolPinCount, + bool aResolve ); template static std::unique_ptr Create( const SIM_MODEL& aBaseModel, unsigned aSymbolPinCount, - const std::vector& aFields ); + const std::vector& aFields, bool aResolve ); template static std::unique_ptr Create( unsigned aSymbolPinCount, - const std::vector& aFields ); + const std::vector& aFields, bool aResolve ); template static std::string GetFieldValue( const std::vector* aFields, const std::string& aFieldName ); @@ -599,6 +602,9 @@ 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; @@ -613,7 +619,7 @@ public: bool IsInferred() const { return m_isInferred; } protected: - static std::unique_ptr Create( TYPE aType ); + static std::unique_ptr Create( TYPE aType, bool aResolve ); SIM_MODEL( TYPE aType ); SIM_MODEL( TYPE aType, std::unique_ptr aSpiceGenerator ); @@ -638,6 +644,8 @@ 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 44f6e6e272..154eeb8e50 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,7 +267,8 @@ void SIM_MODEL_KIBIS::SwitchSingleEndedDiff( bool aDiff ) } } -SIM_MODEL_KIBIS::SIM_MODEL_KIBIS( TYPE aType, const SIM_MODEL_KIBIS& aSource ) : SIM_MODEL_KIBIS( aType ) +SIM_MODEL_KIBIS::SIM_MODEL_KIBIS( TYPE aType, const SIM_MODEL_KIBIS& aSource, bool aResolve ) : + SIM_MODEL_KIBIS( aType ) { for( PARAM& param1 : m_params ) { @@ -278,6 +279,12 @@ SIM_MODEL_KIBIS::SIM_MODEL_KIBIS( TYPE aType, const SIM_MODEL_KIBIS& aSource ) : 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; @@ -287,15 +294,15 @@ SIM_MODEL_KIBIS::SIM_MODEL_KIBIS( TYPE aType, const SIM_MODEL_KIBIS& aSource ) : } SIM_MODEL_KIBIS::SIM_MODEL_KIBIS( TYPE aType, SIM_MODEL_KIBIS& aSource, - const std::vector& aFields ) : - SIM_MODEL_KIBIS( aType, aSource ) + const std::vector& aFields, bool aResolve ) : + SIM_MODEL_KIBIS( aType, aSource, aResolve ) { ReadDataFields( 2, &aFields ); } SIM_MODEL_KIBIS::SIM_MODEL_KIBIS( TYPE aType, SIM_MODEL_KIBIS& aSource, - const std::vector& aFields ) : - SIM_MODEL_KIBIS( aType, aSource ) + const std::vector& aFields, bool aResolve ) : + SIM_MODEL_KIBIS( aType, aSource, aResolve ) { ReadDataFields( 2, &aFields ); } diff --git a/eeschema/sim/sim_model_kibis.h b/eeschema/sim/sim_model_kibis.h index 97f5d6139d..8a243b4197 100644 --- a/eeschema/sim/sim_model_kibis.h +++ b/eeschema/sim/sim_model_kibis.h @@ -63,10 +63,12 @@ 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 ); + SIM_MODEL_KIBIS( TYPE aType, const SIM_MODEL_KIBIS& aSource, 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 ); + 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 ); std::vector> GetIbisPins() const { diff --git a/eeschema/sim/sim_model_spice.cpp b/eeschema/sim/sim_model_spice.cpp index c302bc6ea0..a257eea02b 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 ) { - auto model = static_cast( - SIM_MODEL::Create( SPICE_MODEL_PARSER::ReadType( aLibrary, aSpiceCode ) ).release() ); + SIM_MODEL::TYPE modelType = SPICE_MODEL_PARSER::ReadType( aLibrary, aSpiceCode ); + auto model = static_cast( SIM_MODEL::Create( modelType, true ).release() ); try { diff --git a/eeschema/sim/sim_plot_frame.cpp b/eeschema/sim/sim_plot_frame.cpp index 346d440776..1fb77f7d27 100644 --- a/eeschema/sim/sim_plot_frame.cpp +++ b/eeschema/sim/sim_plot_frame.cpp @@ -588,7 +588,8 @@ void SIM_PLOT_FRAME::UpdateTunerValue( SCH_SYMBOL* aSymbol, const wxString& aVal if( item == aSymbol ) { SIM_LIB_MGR mgr( Prj() ); - SIM_MODEL& model = mgr.CreateModel( *aSymbol ).model; + SIM_MODEL& model = mgr.CreateModel( &m_schematicFrame->GetCurrentSheet(), *aSymbol, + true ).model; const SIM_MODEL::PARAM* tunerParam = model.GetTunerParam(); diff --git a/eeschema/sim/sim_property.cpp b/eeschema/sim/sim_property.cpp index 67177a2dd5..b73f74381e 100644 --- a/eeschema/sim/sim_property.cpp +++ b/eeschema/sim/sim_property.cpp @@ -25,10 +25,7 @@ #include #include #include -#include #include -#include -#include wxBEGIN_EVENT_TABLE( SIM_VALIDATOR, wxValidator ) @@ -170,87 +167,12 @@ void SIM_VALIDATOR::onKeyDown( wxKeyEvent& aEvent ) } -wxBEGIN_EVENT_TABLE( SIM_BOOL_VALIDATOR, SIM_VALIDATOR ) -wxEND_EVENT_TABLE() - - -bool SIM_BOOL_VALIDATOR::Validate( wxWindow* aParent ) +bool SIM_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 ) @@ -279,7 +201,7 @@ SIM_BOOL_PROPERTY::SIM_BOOL_PROPERTY( const wxString& aLabel, const wxString& aN wxValidator* SIM_BOOL_PROPERTY::DoGetValidator() const { - return new SIM_BOOL_VALIDATOR(); + return new SIM_VALIDATOR(); } @@ -302,21 +224,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, - SIM_VALUE::TYPE aValueType, - SIM_VALUE_GRAMMAR::NOTATION aNotation ) + int aParamIndex ) : wxStringProperty( aLabel, aName ), - SIM_PROPERTY( aLibrary, aModel, aParamIndex ), - m_valueType( aValueType ), - m_notation( aNotation ) + SIM_PROPERTY( aLibrary, aModel, aParamIndex ) { - SetValueFromString( GetParam().value->ToString() ); + const SIM_MODEL::PARAM& param = GetParam(); + + wxASSERT( !param.resolved ); + + SetValueFromString( param.source ); } wxValidator* SIM_STRING_PROPERTY::DoGetValidator() const { - return new SIM_STRING_VALIDATOR( m_valueType, m_notation ); + return new SIM_VALIDATOR(); } @@ -329,6 +251,13 @@ 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. @@ -342,8 +271,8 @@ bool SIM_STRING_PROPERTY::StringToValue( wxVariant& aVariant, const wxString& aT } else { - m_model->SetParamValue( m_paramIndex, std::string( aText.ToUTF8() ) ); - aVariant = GetParam().value->ToString(); + m_model->SetParamSource( m_paramIndex, aText ); + aVariant = aText; } return true; @@ -364,9 +293,7 @@ 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, - SIM_VALUE::TYPE aValueType, - SIM_VALUE_GRAMMAR::NOTATION aNotation ) + int aParamIndex ) : 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 74ddab541a..794b900346 100644 --- a/eeschema/sim/sim_property.h +++ b/eeschema/sim/sim_property.h @@ -33,6 +33,8 @@ #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: @@ -40,41 +42,8 @@ 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(); }; @@ -117,18 +86,12 @@ public: SIM_STRING_PROPERTY( const wxString& aLabel, const wxString& aName, std::shared_ptr aLibrary, std::shared_ptr aModel, - int aParamIndex, - SIM_VALUE::TYPE aValueType = SIM_VALUE::TYPE_FLOAT, - SIM_VALUE_GRAMMAR::NOTATION aNotation = SIM_VALUE_GRAMMAR::NOTATION::SI ); + int aParamIndex ); 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; }; @@ -138,9 +101,7 @@ public: SIM_ENUM_PROPERTY( const wxString& aLabel, const wxString& aName, std::shared_ptr aLibrary, std::shared_ptr aModel, - int aParamIndex, - SIM_VALUE::TYPE aValueType = SIM_VALUE::TYPE_FLOAT, - SIM_VALUE_GRAMMAR::NOTATION aNotation = SIM_VALUE_GRAMMAR::NOTATION::SI ); + int aParamIndex ); 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 fe9e38b661..278bf1d5d8 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; - const std::vector* fields = nullptr; + std::vector fields; }; diff --git a/eeschema/tools/sch_editor_control.cpp b/eeschema/tools/sch_editor_control.cpp index d25ddeced9..1ad9834ee7 100644 --- a/eeschema/tools/sch_editor_control.cpp +++ b/eeschema/tools/sch_editor_control.cpp @@ -874,6 +874,7 @@ 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; @@ -887,10 +888,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( *symbol ).model; + SIM_MODEL& model = mgr.CreateModel( &sheet, *symbol, true ).model; SPICE_ITEM spiceItem; - spiceItem.refName = std::string( symbol->GetRef( &m_frame->GetCurrentSheet() ).ToUTF8() ); + spiceItem.refName = std::string( symbol->GetRef( &sheet ).ToUTF8() ); std::vector currentNames = model.SpiceGenerator().CurrentNames( spiceItem );