Don't confuse annotation prefix *letters* with root annotation.

Given a symbol U12A, some parts of the code were assuming the prefix was
'U' while others were assuming 'U12'.  Further complication arose when
there were '?' in the library version.

Fixes https://gitlab.com/kicad/code/kicad/issues/8223
This commit is contained in:
Jeff Young 2021-04-17 12:29:15 +01:00
parent 2ae264751f
commit f7ae819430
2 changed files with 43 additions and 51 deletions

View File

@ -92,6 +92,29 @@ static LIB_PART* dummy()
}
wxString refDesPrefix( const wxString& aSource )
{
wxString result;
size_t sourceLen = aSource.length();
for( size_t i = 0; i < sourceLen; ++i )
{
if( aSource[i] == '?' || wxIsdigit( aSource[i] ) )
break;
result += aSource[i];
}
return result;
}
wxString refDesUnannotated( const wxString& aSource )
{
return refDesPrefix( aSource ) + wxT( "?" );
}
SCH_COMPONENT::SCH_COMPONENT( const wxPoint& aPos, SCH_ITEM* aParent ) :
SCH_ITEM( aParent, SCH_COMPONENT_T )
{
@ -124,11 +147,10 @@ SCH_COMPONENT::SCH_COMPONENT( const LIB_PART& aPart, const LIB_ID& aLibId,
true, /* reset ref */
true /* reset other fields */ );
// Update the reference -- just the prefix for now.
m_prefix = refDesPrefix( m_part->GetReferenceField().GetText() );
if( aSheet )
SetRef( aSheet, m_part->GetReferenceField().GetText() + wxT( "?" ) );
else
m_prefix = m_part->GetReferenceField().GetText() + wxT( "?" );
SetRef( aSheet, refDesUnannotated( m_prefix ) );
// Inherit the include in bill of materials and board netlist settings from library symbol.
m_inBom = aPart.GetIncludeInBom();
@ -454,7 +476,7 @@ const wxString SCH_COMPONENT::GetRef( const SCH_SHEET_PATH* sheet, bool aInclude
}
if( ref.IsEmpty() )
ref = m_prefix;
ref = refDesUnannotated( m_prefix );
if( aIncludeUnit && GetUnitCount() > 1 )
ref += LIB_PART::SubReference( GetUnit() );
@ -465,17 +487,7 @@ const wxString SCH_COMPONENT::GetRef( const SCH_SHEET_PATH* sheet, bool aInclude
bool SCH_COMPONENT::IsReferenceStringValid( const wxString& aReferenceString )
{
wxString text = aReferenceString;
bool ok = true;
// Try to unannotate this reference
while( !text.IsEmpty() && ( text.Last() == '?' || wxIsdigit( text.Last() ) ) )
text.RemoveLast();
if( text.IsEmpty() )
ok = false;
return ok;
return !refDesPrefix( aReferenceString ).IsEmpty();
}
@ -514,20 +526,10 @@ void SCH_COMPONENT::SetRef( const SCH_SHEET_PATH* sheet, const wxString& ref )
rf->SetText( ref ); // for drawing.
// Reinit the m_prefix member if needed
wxString prefix = ref;
m_prefix = refDesPrefix( ref );
if( IsReferenceStringValid( prefix ) )
{
while( prefix.Last() == '?' || wxIsdigit( prefix.Last() ) )
prefix.RemoveLast();
}
else
{
prefix = wxT( "U" ); // Set to default ref prefix
}
if( m_prefix != prefix )
m_prefix = prefix;
if( m_prefix.IsEmpty() )
m_prefix = wxT( "U" );
// Power symbols have references starting with # and are not included in netlists
m_isInNetlist = ! ref.StartsWith( wxT( "#" ) );
@ -579,7 +581,7 @@ void SCH_COMPONENT::SetUnitSelection( const SCH_SHEET_PATH* aSheet, int aUnitSel
}
// didn't find it; better add it
AddHierarchicalReference( path, m_prefix, aUnitSelection );
AddHierarchicalReference( path, refDesUnannotated( m_prefix ), aUnitSelection );
}
@ -636,7 +638,7 @@ void SCH_COMPONENT::SetValue( const SCH_SHEET_PATH* sheet, const wxString& aValu
}
// didn't find it; better add it
AddHierarchicalReference( path, m_prefix, m_unit, aValue, wxEmptyString );
AddHierarchicalReference( path, refDesUnannotated( m_prefix ), m_unit, aValue, wxEmptyString );
}
@ -686,7 +688,8 @@ void SCH_COMPONENT::SetFootprint( const SCH_SHEET_PATH* sheet, const wxString& a
}
// didn't find it; better add it
AddHierarchicalReference( path, m_prefix, m_unit, wxEmptyString, aFootprint );
AddHierarchicalReference( path, refDesUnannotated( m_prefix ), m_unit, wxEmptyString,
aFootprint );
}
@ -1035,19 +1038,8 @@ bool SCH_COMPONENT::ResolveTextVar( wxString* token, int aDepth ) const
void SCH_COMPONENT::ClearAnnotation( const SCH_SHEET_PATH* aSheetPath )
{
// Build a reference with no annotation,
// i.e. a reference ended by only one '?'
wxString defRef = m_prefix;
if( !IsReferenceStringValid( defRef ) )
{ // This is a malformed reference: reinit this reference
m_prefix = defRef = wxT("U"); // Set to default ref prefix
}
while( defRef.Last() == '?' )
defRef.RemoveLast();
defRef.Append( wxT( "?" ) );
// Build a reference with no annotation, i.e. a reference ending with a single '?'
wxString defRef = refDesUnannotated( m_prefix );
if( aSheetPath )
{

View File

@ -673,12 +673,12 @@ private:
LIB_ID m_lib_id; ///< Name and library the symbol was loaded from, i.e. 74xx:74LS00.
int m_unit; ///< The unit for multiple part per package symbols.
int m_convert; ///< The alternate body style for symbols that have more than
///< one body style defined. Primarily used for symbols that
///< have a De Morgan conversion.
wxString m_prefix; ///< C, R, U, Q etc - the first character which typically indicates
///< what the symbol is. Determined, upon placement, from the
///< library symbol. Created upon file load, by the first
///< non-digits in the reference fields.
///< one body style defined. Primarily used for symbols that
///< have a De Morgan conversion.
wxString m_prefix; ///< C, R, U, Q etc - the first character(s) which typically
///< indicate what the symbol is. Determined, upon placement,
///< from the library symbol. Created upon file load, by the
///< first non-digits in the reference fields.
/**
* The name used to look up a symbol in the symbol library embedded in a schematic.