Add text var cross-reference processing to SCH_FIELDs.

This commit is contained in:
Jeff Young 2020-07-29 18:02:39 +01:00
parent 5a1b1c544a
commit 0804f487ec
7 changed files with 182 additions and 153 deletions

View File

@ -143,117 +143,6 @@ DIALOG_LABEL_EDITOR::~DIALOG_LABEL_EDITOR()
}
wxString DIALOG_LABEL_EDITOR::convertKIIDsToReferences( const wxString& aSource ) const
{
wxString newbuf;
size_t sourceLen = aSource.length();
for( size_t i = 0; i < sourceLen; ++i )
{
if( aSource[i] == '$' && i + 1 < sourceLen && aSource[i+1] == '{' )
{
wxString token;
bool isCrossRef = false;
for( i = i + 2; i < sourceLen; ++i )
{
if( aSource[i] == '}' )
break;
if( aSource[i] == ':' )
isCrossRef = true;
token.append( aSource[i] );
}
if( isCrossRef )
{
SCH_SHEET_LIST sheetList = m_Parent->Schematic().GetSheets();
wxString remainder;
wxString ref = token.BeforeFirst( ':', &remainder );
SCH_SHEET_PATH refSheetPath;
SCH_ITEM* refItem = sheetList.GetItem( KIID( ref ), &refSheetPath );
if( refItem && refItem->Type() == SCH_COMPONENT_T )
{
SCH_COMPONENT* refComponent = static_cast<SCH_COMPONENT*>( refItem );
token = refComponent->GetRef( &refSheetPath, true ) + ":" + remainder;
}
}
newbuf.append( "${" + token + "}" );
}
else
{
newbuf.append( aSource[i] );
}
}
return newbuf;
}
wxString DIALOG_LABEL_EDITOR::convertReferencesToKIIDs( const wxString& aSource ) const
{
wxString newbuf;
size_t sourceLen = aSource.length();
for( size_t i = 0; i < sourceLen; ++i )
{
if( aSource[i] == '$' && i + 1 < sourceLen && aSource[i+1] == '{' )
{
wxString token;
bool isCrossRef = false;
for( i = i + 2; i < sourceLen; ++i )
{
if( aSource[i] == '}' )
break;
if( aSource[i] == ':' )
isCrossRef = true;
token.append( aSource[i] );
}
if( isCrossRef )
{
SCH_SHEET_LIST sheets = m_Parent->Schematic().GetSheets();
wxString remainder;
wxString ref = token.BeforeFirst( ':', &remainder );
SCH_REFERENCE_LIST references;
sheets.GetComponents( references );
for( size_t jj = 0; jj < references.GetCount(); jj++ )
{
SCH_COMPONENT* refComponent = references[ jj ].GetComp();
if( ref == refComponent->GetRef( &references[ jj ].GetSheetPath(), true ) )
{
wxString test( remainder );
if( refComponent->ResolveTextVar( &test ) )
token = refComponent->m_Uuid.AsString() + ":" + remainder;
break;
}
}
}
newbuf.append( "${" + token + "}" );
}
else
{
newbuf.append( aSource[i] );
}
}
return newbuf;
}
bool DIALOG_LABEL_EDITOR::TransferDataToWindow()
{
if( !wxDialog::TransferDataToWindow() )
@ -261,8 +150,10 @@ bool DIALOG_LABEL_EDITOR::TransferDataToWindow()
if( m_CurrentText->Type() == SCH_TEXT_T )
{
SCHEMATIC& schematic = m_Parent->Schematic();
// show text variable cross-references in a human-readable format
m_valueMultiLine->SetValue( convertKIIDsToReferences( m_CurrentText->GetText() ) );
m_valueMultiLine->SetValue( schematic.ConvertKIIDsToRefs( m_CurrentText->GetText() ) );
}
else
{
@ -407,7 +298,7 @@ bool DIALOG_LABEL_EDITOR::TransferDataFromWindow()
if( m_CurrentText->Type() == SCH_TEXT_T )
{
// convert any text variable cross-references to their UUIDs
text = convertReferencesToKIIDs( m_valueMultiLine->GetValue() );
text = m_Parent->Schematic().ConvertRefsToKIIDs( m_valueMultiLine->GetValue() );
}
else
{

View File

@ -71,10 +71,6 @@ private:
bool TransferDataToWindow() override;
bool TransferDataFromWindow() override;
wxString convertKIIDsToReferences( const wxString& aSource ) const;
wxString convertReferencesToKIIDs( const wxString& aSource ) const;
SCH_EDIT_FRAME* m_Parent;
SCH_TEXT* m_CurrentText;
wxWindow* m_activeTextCtrl;

View File

@ -287,6 +287,9 @@ DIALOG_SCH_EDIT_ONE_FIELD::DIALOG_SCH_EDIT_ONE_FIELD( SCH_BASE_FRAME* aParent,
}
}
// show text variable cross-references in a human-readable format
m_text = aField->Schematic()->ConvertKIIDsToRefs( aField->GetText() );
m_isPower = false;
m_textLabel->SetLabel( m_field->GetName() + ":" );
@ -410,6 +413,9 @@ void DIALOG_SCH_EDIT_ONE_FIELD::UpdateField( SCH_FIELD* aField, SCH_SHEET_PATH*
if( aField->GetVertJustify() != EDA_TEXT::MapVertJustify( m_verticalJustification - 1 ) )
positioningModified = true;
// convert any text variable cross-references to their UUIDs
m_text = aField->Schematic()->ConvertRefsToKIIDs( m_text );
aField->SetText( m_text );
updateText( aField );

View File

@ -103,6 +103,13 @@ wxString SCH_FIELD::GetShownText( int aDepth ) const
{
std::function<bool( wxString* )> symbolResolver =
[&]( wxString* token ) -> bool
{
if( token->Contains( ':' ) )
{
if( Schematic()->ResolveCrossReference( token, aDepth ) )
return true;
}
else
{
SCH_COMPONENT* component = static_cast<SCH_COMPONENT*>( m_Parent );
@ -114,6 +121,7 @@ wxString SCH_FIELD::GetShownText( int aDepth ) const
if( sheet && sheet->ResolveTextVar( token, aDepth + 1 ) )
return true;
}
return false;
};

View File

@ -507,36 +507,9 @@ wxString SCH_TEXT::GetShownText( int aDepth ) const
{
if( token->Contains( ':' ) )
{
wxCHECK_MSG( Schematic(), wxEmptyString,
"No parent SCHEMATIC set for SCH_TEXT!" );
SCH_SHEET_LIST sheetList = Schematic()->GetSheets();
wxString remainder;
wxString ref = token->BeforeFirst( ':', &remainder );
SCH_SHEET_PATH dummy;
SCH_ITEM* refItem = sheetList.GetItem( KIID( ref ), &dummy );
if( refItem && refItem->Type() == SCH_COMPONENT_T )
{
SCH_COMPONENT* refComponent = static_cast<SCH_COMPONENT*>( refItem );
if( refComponent->ResolveTextVar( &remainder, aDepth + 1 ) )
{
*token = remainder;
if( Schematic()->ResolveCrossReference( token, aDepth ) )
return true;
}
}
else if( refItem && refItem->Type() == SCH_SHEET_T )
{
SCH_SHEET* refSheet = static_cast<SCH_SHEET*>( refItem );
if( refSheet->ResolveTextVar( &remainder, aDepth + 1 ) )
{
*token = remainder;
return true;
}
}
}
else
{
SCHEMATIC* schematic = Schematic();

View File

@ -240,3 +240,147 @@ std::vector<wxString> SCHEMATIC::GetNetClassAssignmentCandidates()
}
bool SCHEMATIC::ResolveCrossReference( wxString* token, int aDepth ) const
{
SCH_SHEET_LIST sheetList = GetSheets();
wxString remainder;
wxString ref = token->BeforeFirst( ':', &remainder );
SCH_SHEET_PATH dummy;
SCH_ITEM* refItem = sheetList.GetItem( KIID( ref ), &dummy );
if( refItem && refItem->Type() == SCH_COMPONENT_T )
{
SCH_COMPONENT* refComponent = static_cast<SCH_COMPONENT*>( refItem );
if( refComponent->ResolveTextVar( &remainder, aDepth + 1 ) )
{
*token = remainder;
return true;
}
}
else if( refItem && refItem->Type() == SCH_SHEET_T )
{
SCH_SHEET* refSheet = static_cast<SCH_SHEET*>( refItem );
if( refSheet->ResolveTextVar( &remainder, aDepth + 1 ) )
{
*token = remainder;
return true;
}
}
return false;
}
wxString SCHEMATIC::ConvertRefsToKIIDs( const wxString& aSource ) const
{
wxString newbuf;
size_t sourceLen = aSource.length();
for( size_t i = 0; i < sourceLen; ++i )
{
if( aSource[i] == '$' && i + 1 < sourceLen && aSource[i+1] == '{' )
{
wxString token;
bool isCrossRef = false;
for( i = i + 2; i < sourceLen; ++i )
{
if( aSource[i] == '}' )
break;
if( aSource[i] == ':' )
isCrossRef = true;
token.append( aSource[i] );
}
if( isCrossRef )
{
SCH_SHEET_LIST sheetList = GetSheets();
wxString remainder;
wxString ref = token.BeforeFirst( ':', &remainder );
SCH_REFERENCE_LIST references;
sheetList.GetComponents( references );
for( size_t jj = 0; jj < references.GetCount(); jj++ )
{
SCH_COMPONENT* refComponent = references[ jj ].GetComp();
if( ref == refComponent->GetRef( &references[ jj ].GetSheetPath(), true ) )
{
wxString test( remainder );
if( refComponent->ResolveTextVar( &test ) )
token = refComponent->m_Uuid.AsString() + ":" + remainder;
break;
}
}
}
newbuf.append( "${" + token + "}" );
}
else
{
newbuf.append( aSource[i] );
}
}
return newbuf;
}
wxString SCHEMATIC::ConvertKIIDsToRefs( const wxString& aSource ) const
{
wxString newbuf;
size_t sourceLen = aSource.length();
for( size_t i = 0; i < sourceLen; ++i )
{
if( aSource[i] == '$' && i + 1 < sourceLen && aSource[i+1] == '{' )
{
wxString token;
bool isCrossRef = false;
for( i = i + 2; i < sourceLen; ++i )
{
if( aSource[i] == '}' )
break;
if( aSource[i] == ':' )
isCrossRef = true;
token.append( aSource[i] );
}
if( isCrossRef )
{
SCH_SHEET_LIST sheetList = GetSheets();
wxString remainder;
wxString ref = token.BeforeFirst( ':', &remainder );
SCH_SHEET_PATH refSheetPath;
SCH_ITEM* refItem = sheetList.GetItem( KIID( ref ), &refSheetPath );
if( refItem && refItem->Type() == SCH_COMPONENT_T )
{
SCH_COMPONENT* refComponent = static_cast<SCH_COMPONENT*>( refItem );
token = refComponent->GetRef( &refSheetPath, true ) + ":" + remainder;
}
}
newbuf.append( "${" + token + "}" );
}
else
{
newbuf.append( aSource[i] );
}
}
return newbuf;
}

View File

@ -151,6 +151,17 @@ public:
*/
std::vector<wxString> GetNetClassAssignmentCandidates();
/**
* Resolves text vars that refer to other items.
* Note that the actual resolve is delegated to the symbol/sheet in question. This routine
* just does the look-up and delegation.
*/
bool ResolveCrossReference( wxString* token, int aDepth ) const;
wxString ConvertRefsToKIIDs( const wxString& aSource ) const;
wxString ConvertKIIDsToRefs( const wxString& aSource ) const;
#if defined(DEBUG)
void Show( int nestLevel, std::ostream& os ) const override {}
#endif