Resolve variables in LIB_SYMBOLs.

Fixes https://gitlab.com/kicad/code/kicad/-/issues/18099
This commit is contained in:
Jeff Young 2024-05-29 22:52:39 +01:00
parent d82522304c
commit ac59a65d8d
3 changed files with 128 additions and 7 deletions

View File

@ -737,6 +737,107 @@ wxString LIB_SYMBOL::LetterSubReference( int aUnit, int aFirstId )
} }
bool LIB_SYMBOL::ResolveTextVar( wxString* token, int aDepth ) const
{
wxString footprint;
for( const SCH_ITEM& item : m_drawings )
{
if( item.Type() == SCH_FIELD_T )
{
const SCH_FIELD& field = static_cast<const SCH_FIELD&>( item );
if( field.GetId() == FOOTPRINT_FIELD )
footprint = field.GetShownText( nullptr, false, aDepth + 1 );
if( token->IsSameAs( field.GetCanonicalName().Upper() )
|| token->IsSameAs( field.GetName(), false ) )
{
*token = field.GetShownText( nullptr, false, aDepth + 1 );
return true;
}
}
}
// Consider missing simulation fields as empty, not un-resolved
if( token->IsSameAs( wxT( "SIM.DEVICE" ) )
|| token->IsSameAs( wxT( "SIM.TYPE" ) )
|| token->IsSameAs( wxT( "SIM.PINS" ) )
|| token->IsSameAs( wxT( "SIM.PARAMS" ) )
|| token->IsSameAs( wxT( "SIM.LIBRARY" ) )
|| token->IsSameAs( wxT( "SIM.NAME" ) ) )
{
*token = wxEmptyString;
return true;
}
if( token->IsSameAs( wxT( "FOOTPRINT_LIBRARY" ) ) )
{
wxArrayString parts = wxSplit( footprint, ':' );
if( parts.Count() > 0 )
*token = parts[ 0 ];
else
*token = wxEmptyString;
return true;
}
else if( token->IsSameAs( wxT( "FOOTPRINT_NAME" ) ) )
{
wxArrayString parts = wxSplit( footprint, ':' );
if( parts.Count() > 1 )
*token = parts[ std::min( 1, (int) parts.size() - 1 ) ];
else
*token = wxEmptyString;
return true;
}
else if( token->IsSameAs( wxT( "SYMBOL_LIBRARY" ) ) )
{
*token = m_libId.GetUniStringLibNickname();
return true;
}
else if( token->IsSameAs( wxT( "SYMBOL_NAME" ) ) )
{
*token = m_libId.GetUniStringLibItemName();
return true;
}
else if( token->IsSameAs( wxT( "SYMBOL_DESCRIPTION" ) ) )
{
*token = GetDescription();
return true;
}
else if( token->IsSameAs( wxT( "SYMBOL_KEYWORDS" ) ) )
{
*token = GetKeyWords();
return true;
}
else if( token->IsSameAs( wxT( "EXCLUDE_FROM_BOM" ) ) )
{
*token = this->GetExcludedFromBOM() ? _( "Excluded from BOM" ) : wxString( "" );
return true;
}
else if( token->IsSameAs( wxT( "EXCLUDE_FROM_BOARD" ) ) )
{
*token = this->GetExcludedFromBoard() ? _( "Excluded from board" ) : wxString( "" );
return true;
}
else if( token->IsSameAs( wxT( "EXCLUDE_FROM_SIM" ) ) )
{
*token = this->GetExcludedFromSim() ? _( "Excluded from simulation" ) : wxString( "" );
return true;
}
else if( token->IsSameAs( wxT( "DNP" ) ) )
{
*token = this->GetDNP() ? _( "DNP" ) : wxString( "" );
return true;
}
return false;
}
void LIB_SYMBOL::Print( const SCH_RENDER_SETTINGS* aSettings, int aUnit, int aBodyStyle, void LIB_SYMBOL::Print( const SCH_RENDER_SETTINGS* aSettings, int aUnit, int aBodyStyle,
const VECTOR2I& aOffset, bool aForceNoFill, bool aDimmed ) const VECTOR2I& aOffset, bool aForceNoFill, bool aDimmed )
{ {

View File

@ -346,6 +346,13 @@ public:
int GetNextAvailableFieldId() const; int GetNextAvailableFieldId() const;
/**
* Resolve any references to system tokens supported by the symbol.
*
* @param aDepth a counter to limit recursion and circular references.
*/
bool ResolveTextVar( wxString* token, int aDepth = 0 ) const;
void Print( const SCH_RENDER_SETTINGS* aSettings, int aUnit, int aBodyStyle, void Print( const SCH_RENDER_SETTINGS* aSettings, int aUnit, int aBodyStyle,
const VECTOR2I& aOffset, bool aForceNoFill, bool aDimmed ) override; const VECTOR2I& aOffset, bool aForceNoFill, bool aDimmed ) override;

View File

@ -197,6 +197,13 @@ wxString SCH_FIELD::GetShownName() const
wxString SCH_FIELD::GetShownText( const SCH_SHEET_PATH* aPath, bool aAllowExtraText, wxString SCH_FIELD::GetShownText( const SCH_SHEET_PATH* aPath, bool aAllowExtraText,
int aDepth ) const int aDepth ) const
{ {
std::function<bool( wxString* )> libSymbolResolver =
[&]( wxString* token ) -> bool
{
LIB_SYMBOL* symbol = static_cast<LIB_SYMBOL*>( m_parent );
return symbol->ResolveTextVar( token, aDepth + 1 );
};
std::function<bool( wxString* )> symbolResolver = std::function<bool( wxString* )> symbolResolver =
[&]( wxString* token ) -> bool [&]( wxString* token ) -> bool
{ {
@ -207,6 +214,9 @@ wxString SCH_FIELD::GetShownText( const SCH_SHEET_PATH* aPath, bool aAllowExtraT
std::function<bool( wxString* )> schematicResolver = std::function<bool( wxString* )> schematicResolver =
[&]( wxString* token ) -> bool [&]( wxString* token ) -> bool
{ {
if( !aPath )
return false;
if( SCHEMATIC* schematic = Schematic() ) if( SCHEMATIC* schematic = Schematic() )
return schematic->ResolveTextVar( aPath, token, aDepth + 1 ); return schematic->ResolveTextVar( aPath, token, aDepth + 1 );
@ -216,6 +226,9 @@ wxString SCH_FIELD::GetShownText( const SCH_SHEET_PATH* aPath, bool aAllowExtraT
std::function<bool( wxString* )> sheetResolver = std::function<bool( wxString* )> sheetResolver =
[&]( wxString* token ) -> bool [&]( wxString* token ) -> bool
{ {
if( !aPath )
return false;
SCH_SHEET* sheet = static_cast<SCH_SHEET*>( m_parent ); SCH_SHEET* sheet = static_cast<SCH_SHEET*>( m_parent );
SCHEMATIC* schematic = Schematic(); SCHEMATIC* schematic = Schematic();
@ -233,6 +246,9 @@ wxString SCH_FIELD::GetShownText( const SCH_SHEET_PATH* aPath, bool aAllowExtraT
std::function<bool( wxString* )> labelResolver = std::function<bool( wxString* )> labelResolver =
[&]( wxString* token ) -> bool [&]( wxString* token ) -> bool
{ {
if( !aPath )
return false;
SCH_LABEL_BASE* label = static_cast<SCH_LABEL_BASE*>( m_parent ); SCH_LABEL_BASE* label = static_cast<SCH_LABEL_BASE*>( m_parent );
return label->ResolveTextVar( aPath, token, aDepth + 1 ); return label->ResolveTextVar( aPath, token, aDepth + 1 );
}; };
@ -242,19 +258,16 @@ wxString SCH_FIELD::GetShownText( const SCH_SHEET_PATH* aPath, bool aAllowExtraT
if( IsNameShown() && aAllowExtraText ) if( IsNameShown() && aAllowExtraText )
text = GetShownName() << wxS( ": " ) << text; text = GetShownName() << wxS( ": " ) << text;
if( !aPath )
return text;
if( text == wxS( "~" ) ) // Legacy placeholder for empty string if( text == wxS( "~" ) ) // Legacy placeholder for empty string
{
text = wxS( "" ); text = wxS( "" );
}
for( int ii = 0; ii < 10 && text.Contains( wxT( "${" ) ); ++ii ) for( int ii = 0; ii < 10 && text.Contains( wxT( "${" ) ); ++ii )
{ {
if( aDepth < 10 ) if( aDepth < 10 )
{ {
if( m_parent && m_parent->Type() == SCH_SYMBOL_T ) if( m_parent && m_parent->Type() == LIB_SYMBOL_T )
text = ExpandTextVars( text, &libSymbolResolver );
else if( m_parent && m_parent->Type() == SCH_SYMBOL_T )
text = ExpandTextVars( text, &symbolResolver ); text = ExpandTextVars( text, &symbolResolver );
else if( m_parent && m_parent->Type() == SCH_SHEET_T ) else if( m_parent && m_parent->Type() == SCH_SHEET_T )
text = ExpandTextVars( text, &sheetResolver ); text = ExpandTextVars( text, &sheetResolver );
@ -275,7 +288,7 @@ wxString SCH_FIELD::GetShownText( const SCH_SHEET_PATH* aPath, bool aAllowExtraT
{ {
SCH_SYMBOL* parentSymbol = static_cast<SCH_SYMBOL*>( m_parent ); SCH_SYMBOL* parentSymbol = static_cast<SCH_SYMBOL*>( m_parent );
if( m_id == REFERENCE_FIELD ) if( m_id == REFERENCE_FIELD && aPath )
{ {
// For more than one part per package, we must add the part selection // For more than one part per package, we must add the part selection
// A, B, ... or 1, 2, .. to the reference. // A, B, ... or 1, 2, .. to the reference.