From cfa0d4bfb7b4c97861446cd52ebfb4f1b2e877b3 Mon Sep 17 00:00:00 2001 From: Mike Williams Date: Wed, 12 Jul 2023 16:40:08 -0400 Subject: [PATCH] Symbol Fields Table: handle missing fields better Treat missing ones as empty, unless they are a named variable field and then resolve the field. --- eeschema/fields_data_model.cpp | 58 ++++++++++++++++++++++++---------- eeschema/fields_data_model.h | 6 ++++ 2 files changed, 48 insertions(+), 16 deletions(-) diff --git a/eeschema/fields_data_model.cpp b/eeschema/fields_data_model.cpp index d0e761469f..2298ef8188 100644 --- a/eeschema/fields_data_model.cpp +++ b/eeschema/fields_data_model.cpp @@ -1,6 +1,7 @@ #include #include #include +#include #include #include #include @@ -21,7 +22,16 @@ void FIELDS_EDITOR_GRID_DATA_MODEL::AddColumn( const wxString& aFieldName, const for( unsigned i = 0; i < m_symbolsList.GetCount(); ++i ) { if( SCH_SYMBOL* symbol = m_symbolsList[i].GetSymbol() ) - m_dataStore[symbol->m_Uuid][aFieldName] = symbol->GetFieldText( aFieldName ); + { + if( SCH_FIELD* field = symbol->GetFieldByName( aFieldName ) ) + m_dataStore[symbol->m_Uuid][aFieldName] = field->GetText(); + // Handle fields with variables as names that are not present in the symbol + // by giving them the correct value + else if( aFieldName.StartsWith( wxT( "${" ) ) ) + m_dataStore[symbol->m_Uuid][aFieldName] = aFieldName; + else + m_dataStore[symbol->m_Uuid][aFieldName] = wxEmptyString; + } } } @@ -142,12 +152,7 @@ wxString FIELDS_EDITOR_GRID_DATA_MODEL::GetValue( const DATA_MODEL_ROW& group, i wxString refFieldValue; if( resolveVars ) - { - SCH_FIELD* field = ref.GetSymbol()->GetFieldByName( m_cols[aCol].m_fieldName ); - - if( field != nullptr ) - refFieldValue = field->GetShownText( &ref.GetSheetPath(), false ); - } + refFieldValue = getFieldShownText( ref, m_cols[aCol].m_fieldName ); else refFieldValue = m_dataStore[symbolID][m_cols[aCol].m_fieldName]; @@ -345,15 +350,8 @@ bool FIELDS_EDITOR_GRID_DATA_MODEL::groupMatch( const SCH_REFERENCE& lhRef, if( !m_cols[i].m_group ) continue; - wxString fieldName = m_cols[i].m_fieldName; - SCH_FIELD* lhField = lhRef.GetSymbol()->GetFieldByName( m_cols[i].m_fieldName ); - SCH_FIELD* rhField = rhRef.GetSymbol()->GetFieldByName( m_cols[i].m_fieldName ); - - if( lhField == nullptr || rhField == nullptr ) - return false; - - if( lhField->GetShownText( &lhRef.GetSheetPath(), false ) - != rhField->GetShownText( &rhRef.GetSheetPath(), false ) ) + if( getFieldShownText( lhRef, m_cols[i].m_fieldName ) + != getFieldShownText( rhRef, m_cols[i].m_fieldName ) ) { return false; } @@ -365,6 +363,34 @@ bool FIELDS_EDITOR_GRID_DATA_MODEL::groupMatch( const SCH_REFERENCE& lhRef, } +wxString FIELDS_EDITOR_GRID_DATA_MODEL::getFieldShownText( const SCH_REFERENCE& aRef, + const wxString& aFieldName ) +{ + SCH_FIELD* field = aRef.GetSymbol()->GetFieldByName( aFieldName ); + + if( field ) + return field->GetShownText( &aRef.GetSheetPath(), false ); + + // Handle fields with variables as names that are not present in the symbol + // by giving them the correct value by resolving against the symbol + if( aFieldName.StartsWith( wxT( "${" ) ) ) + { + int depth = 0; + const SCH_SHEET_PATH& path = aRef.GetSheetPath(); + + std::function symbolResolver = + [&]( wxString* token ) -> bool + { + return aRef.GetSymbol()->ResolveTextVar( &path, token, depth + 1 ); + }; + + return ExpandTextVars( aFieldName, &symbolResolver ); + } + + return wxEmptyString; +} + + void FIELDS_EDITOR_GRID_DATA_MODEL::EnableRebuilds() { m_rebuildsEnabled = true; diff --git a/eeschema/fields_data_model.h b/eeschema/fields_data_model.h index 2efdbe8b18..65764e67fd 100644 --- a/eeschema/fields_data_model.h +++ b/eeschema/fields_data_model.h @@ -199,6 +199,12 @@ private: bool unitMatch( const SCH_REFERENCE& lhRef, const SCH_REFERENCE& rhRef ); bool groupMatch( const SCH_REFERENCE& lhRef, const SCH_REFERENCE& rhRef ); + /* Helper function to get the resolved field value. + * Handles symbols that are missing fields that would have a variable + * in their value because their name is the same as a variable. + * Example: BOM template provides ${DNP} as a field, but they symbol doesn't have the field. */ + wxString getFieldShownText( const SCH_REFERENCE& aRef, const wxString& aFieldName ); + void Sort();