From a3a01a87e8f9d5ee38a1776feeda723d89f6358b Mon Sep 17 00:00:00 2001 From: Jeff Young Date: Mon, 16 Jan 2023 15:59:31 +0000 Subject: [PATCH] Resolve text variable references in LIB_TEXT and LIB_TEXTBOX. Fixes https://gitlab.com/kicad/code/kicad/issues/13558 --- eeschema/sch_painter.cpp | 53 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 53 insertions(+) diff --git a/eeschema/sch_painter.cpp b/eeschema/sch_painter.cpp index 5949690fa5..17cf11c7e7 100644 --- a/eeschema/sch_painter.cpp +++ b/eeschema/sch_painter.cpp @@ -65,6 +65,7 @@ #include #include "sch_painter.h" #include "sch_shape.h" +#include "common.h" namespace KIGFX @@ -2185,6 +2186,43 @@ static void orientSymbol( LIB_SYMBOL* symbol, int orientation ) } +wxString resolveTextVars( const wxString& aSourceText, const SCH_SYMBOL* aSymbolContext ) +{ + std::function symbolResolver = + [&]( wxString* token ) -> bool + { + if( token->Contains( ':' ) ) + { + if( aSymbolContext->Schematic()->ResolveCrossReference( token, 0 ) ) + return true; + } + else + { + if( aSymbolContext->ResolveTextVar( token, 0 ) ) + return true; + + SCHEMATIC* schematic = aSymbolContext->Schematic(); + SCH_SHEET* sheet = schematic ? schematic->CurrentSheet().Last() : nullptr; + + if( sheet && sheet->ResolveTextVar( token, 0 ) ) + return true; + } + + return false; + }; + + PROJECT* project = nullptr; + wxString text = aSourceText; + + if( aSymbolContext->Schematic() ) + project = &aSymbolContext->Schematic()->Prj(); + + text = ExpandTextVars( text, &symbolResolver, nullptr, project ); + + return text; +} + + void SCH_PAINTER::draw( const SCH_SYMBOL* aSymbol, int aLayer ) { bool drawingShadows = aLayer == LAYER_SELECTION_SHADOWS; @@ -2226,6 +2264,21 @@ void SCH_PAINTER::draw( const SCH_SYMBOL* aSymbol, int aLayer ) { tempItem.SetFlags( aSymbol->GetFlags() ); // SELECTED, HIGHLIGHTED, BRIGHTENED, tempItem.MoveTo( tempItem.GetPosition() + (VECTOR2I) mapCoords( aSymbol->GetPosition() ) ); + + if( tempItem.Type() == LIB_TEXT_T ) + { + LIB_TEXT* textItem = static_cast( &tempItem ); + + if( textItem->HasTextVars() ) + textItem->SetText( resolveTextVars( textItem->GetText(), aSymbol ) ); + } + else if( tempItem.Type() == LIB_TEXTBOX_T ) + { + LIB_TEXTBOX* textboxItem = static_cast( &tempItem ); + + if( textboxItem->HasTextVars() ) + textboxItem->SetText( resolveTextVars( textboxItem->GetText(), aSymbol ) ); + } } // Copy the pin info from the symbol to the temp pins