Enforce SCH_SHEET_PATH processing in more places.

Making assumptions about the current SCH_SHEET_PATH has caused no end
of pain.
This commit is contained in:
Jeff Young 2023-11-11 17:41:44 +00:00
parent abcbba5635
commit 3819b2d5e7
7 changed files with 60 additions and 39 deletions

View File

@ -191,20 +191,26 @@ wxString SCH_FIELD::GetShownText( const SCH_SHEET_PATH* aPath, bool aAllowExtraT
std::function<bool( wxString* )> symbolResolver =
[&]( wxString* token ) -> bool
{
return static_cast<SCH_SYMBOL*>( m_parent )->ResolveTextVar( aPath, token, aDepth + 1 );
SCH_SYMBOL* symbol = static_cast<SCH_SYMBOL*>( m_parent );
return symbol->ResolveTextVar( aPath, token, aDepth + 1 );
};
std::function<bool( wxString* )> sheetResolver =
[&]( wxString* token ) -> bool
{
return static_cast<SCH_SHEET*>( m_parent )->ResolveTextVar( token, aDepth + 1 );
SCH_SHEET* sheet = static_cast<SCH_SHEET*>( m_parent );
SCH_SHEET_PATH path = *aPath;
path.push_back( sheet );
return sheet->ResolveTextVar( &path, token, aDepth + 1 );
};
std::function<bool( wxString* )> labelResolver =
[&]( wxString* token ) -> bool
{
return static_cast<SCH_LABEL_BASE*>( m_parent )->ResolveTextVar( aPath, token,
aDepth + 1 );
SCH_LABEL_BASE* label = static_cast<SCH_LABEL_BASE*>( m_parent );
return label->ResolveTextVar( aPath, token, aDepth + 1 );
};
wxString text = EDA_TEXT::GetShownText( aAllowExtraText, aDepth );

View File

@ -769,7 +769,11 @@ bool SCH_LABEL_BASE::ResolveTextVar( const SCH_SHEET_PATH* aPath, wxString* toke
"(.([0-9])?([a-zA-Z]*))?"
"$" ) );
if( !Schematic() )
wxCHECK( aPath, false );
SCHEMATIC* schematic = Schematic();
if( !schematic )
return false;
if( operatingPoint.Matches( *token ) )
@ -788,14 +792,14 @@ bool SCH_LABEL_BASE::ResolveTextVar( const SCH_SHEET_PATH* aPath, wxString* toke
*token = wxS( "?" );
if( connection )
*token = Schematic()->GetOperatingPoint( connection->Name( false ), precision, range );
*token = schematic->GetOperatingPoint( connection->Name( false ), precision, range );
return true;
}
if( token->Contains( ':' ) )
{
if( Schematic()->ResolveCrossReference( token, aDepth + 1 ) )
if( schematic->ResolveCrossReference( token, aDepth + 1 ) )
return true;
}
@ -852,7 +856,10 @@ bool SCH_LABEL_BASE::ResolveTextVar( const SCH_SHEET_PATH* aPath, wxString* toke
{
SCH_SHEET* sheet = static_cast<SCH_SHEET*>( m_parent );
if( sheet->ResolveTextVar( token, aDepth + 1 ) )
SCH_SHEET_PATH path = *aPath;
path.push_back( sheet );
if( sheet->ResolveTextVar( &path, token, aDepth + 1 ) )
return true;
}
else if( aPath && aPath->Last() )
@ -860,7 +867,7 @@ bool SCH_LABEL_BASE::ResolveTextVar( const SCH_SHEET_PATH* aPath, wxString* toke
if( aPath->Last()->ResolveTextVar( aPath, token, aDepth + 1 ) )
return true;
}
else if( SCH_SHEET* sheet = Schematic()->CurrentSheet().Last() )
else if( SCH_SHEET* sheet = schematic->CurrentSheet().Last() )
{
if( sheet->ResolveTextVar( aPath, token, aDepth + 1 ) )
return true;
@ -1769,13 +1776,20 @@ void SCH_GLOBALLABEL::SetSpinStyle( SPIN_STYLE aSpinStyle )
bool SCH_GLOBALLABEL::ResolveTextVar( const SCH_SHEET_PATH* aPath, wxString* token,
int aDepth ) const
{
if( token->IsSameAs( wxT( "INTERSHEET_REFS" ) ) && Schematic() )
{
SCHEMATIC_SETTINGS& settings = Schematic()->Settings();
wxString ref;
auto it = Schematic()->GetPageRefsMap().find( GetText() );
wxCHECK( aPath, false );
if( it == Schematic()->GetPageRefsMap().end() )
SCHEMATIC* schematic = Schematic();
if( !schematic )
return false;
if( token->IsSameAs( wxT( "INTERSHEET_REFS" ) ) )
{
SCHEMATIC_SETTINGS& settings = schematic->Settings();
wxString ref;
auto it = schematic->GetPageRefsMap().find( GetText() );
if( it == schematic->GetPageRefsMap().end() )
{
ref = "?";
}
@ -1788,11 +1802,11 @@ bool SCH_GLOBALLABEL::ResolveTextVar( const SCH_SHEET_PATH* aPath, wxString* tok
if( !settings.m_IntersheetRefsListOwnPage )
{
int currentPage = Schematic()->CurrentSheet().GetVirtualPageNumber();
int currentPage = schematic->CurrentSheet().GetVirtualPageNumber();
alg::delete_matching( pageListCopy, currentPage );
}
std::map<int, wxString> sheetPages = Schematic()->GetVirtualPageToSheetPagesMap();
std::map<int, wxString> sheetPages = schematic->GetVirtualPageToSheetPagesMap();
if( ( settings.m_IntersheetRefsFormatShort ) && ( pageListCopy.size() > 2 ) )
{

View File

@ -236,12 +236,16 @@ void SCH_SHEET::GetContextualTextVars( wxArrayString* aVars ) const
bool SCH_SHEET::ResolveTextVar( const SCH_SHEET_PATH* aPath, wxString* token, int aDepth ) const
{
if( !Schematic() )
wxCHECK( aPath, false );
SCHEMATIC* schematic = Schematic();
if( !schematic )
return false;
if( token->Contains( ':' ) )
{
if( Schematic()->ResolveCrossReference( token, aDepth + 1 ) )
if( schematic->ResolveCrossReference( token, aDepth + 1 ) )
return true;
}
@ -263,8 +267,7 @@ bool SCH_SHEET::ResolveTextVar( const SCH_SHEET_PATH* aPath, wxString* token, in
}
}
SCH_SHEET_PATH sheetPath = aPath ? *aPath : findSelf();
PROJECT* project = &Schematic()->Prj();
PROJECT* project = &schematic->Prj();
// We cannot resolve text variables initially on load as we need to first load the screen and
// then parse the hierarchy. So skip the resolution if the screen isn't set yet
@ -275,33 +278,34 @@ bool SCH_SHEET::ResolveTextVar( const SCH_SHEET_PATH* aPath, wxString* token, in
if( token->IsSameAs( wxT( "#" ) ) )
{
*token = wxString::Format( "%s", sheetPath.GetPageNumber() );
*token = wxString::Format( "%s", aPath->GetPageNumber() );
return true;
}
else if( token->IsSameAs( wxT( "##" ) ) )
{
SCH_SHEET_LIST sheetList = Schematic()->GetSheets();
SCH_SHEET_LIST sheetList = schematic->GetSheets();
*token = wxString::Format( wxT( "%d" ), (int) sheetList.size() );
return true;
}
else if( token->IsSameAs( wxT( "SHEETPATH" ) ) )
{
*token = sheetPath.PathHumanReadable();
*token = aPath->PathHumanReadable();
return true;
}
// See if parent can resolve it (these will recurse to ancestors)
if( sheetPath.size() >= 2 )
if( aPath->size() >= 2 )
{
sheetPath.pop_back();
SCH_SHEET_PATH path = *aPath;
path.pop_back();
if( sheetPath.Last()->ResolveTextVar( &sheetPath, token, aDepth + 1 ) )
if( path.Last()->ResolveTextVar( aPath, token, aDepth + 1 ) )
return true;
}
else
{
if( Schematic()->ResolveTextVar( token, aDepth + 1 ) )
if( schematic->ResolveTextVar( token, aDepth + 1 ) )
return true;
}

View File

@ -157,10 +157,6 @@ public:
* @param aDepth is a counter to limit recursion and circular references.
*/
bool ResolveTextVar( const SCH_SHEET_PATH* aPath, wxString* token, int aDepth = 0 ) const;
bool ResolveTextVar( wxString* token, int aDepth = 0 )
{
return ResolveTextVar( nullptr, token, aDepth );
}
void GetMsgPanelInfo( EDA_DRAW_FRAME* aFrame, std::vector<MSG_PANEL_ITEM>& aList ) override;

View File

@ -1274,10 +1274,11 @@ bool SCH_SYMBOL::ResolveTextVar( const SCH_SHEET_PATH* aPath, wxString* token, i
"(.([0-9])?([a-zA-Z]*))?" // format
"$" ) );
wxCHECK( aPath, false );
SCHEMATIC* schematic = Schematic();
// SCH_SYMBOL object has no context outside a schematic and the instance on a path.
if( !schematic || !aPath )
if( !schematic )
return false;
if( operatingPoint.Matches( *token ) )

View File

@ -292,15 +292,13 @@ wxString SCH_TEXTBOX::GetShownText( const SCH_SHEET_PATH* aPath, bool aAllowExtr
if( aPath )
sheet = aPath->Last();
else if( Schematic() )
sheet = Schematic()->CurrentSheet().Last();
std::function<bool( wxString* )> textResolver =
[&]( wxString* token ) -> bool
{
if( sheet )
{
if( sheet->ResolveTextVar( token, aDepth + 1 ) )
if( sheet->ResolveTextVar( aPath, token, aDepth + 1 ) )
return true;
}
@ -320,7 +318,7 @@ wxString SCH_TEXTBOX::GetShownText( const SCH_SHEET_PATH* aPath, bool aAllowExtr
if( !font )
font = KIFONT::FONT::GetFont( GetDefaultFont(), IsBold(), IsItalic() );
VECTOR2D size = GetEnd() - GetStart();
VECTOR2I size = GetEnd() - GetStart();
int colWidth = GetTextAngle() == ANGLE_HORIZONTAL ? size.x : size.y;
colWidth = abs( colWidth ) - GetTextMargin() * 2;

View File

@ -349,7 +349,9 @@ bool SCHEMATIC::ResolveCrossReference( wxString* token, int aDepth ) const
{
SCH_SHEET* refSheet = static_cast<SCH_SHEET*>( refItem );
if( refSheet->ResolveTextVar( &remainder, aDepth + 1 ) )
sheetPath.push_back( refSheet );
if( refSheet->ResolveTextVar( &sheetPath, &remainder, aDepth + 1 ) )
*token = remainder;
return true; // Cross-reference is resolved whether or not the actual textvar was