schematic: legacy kicad schematic files don't have a default description field

Without this, we would parse a legacy schematic symbol field at the same
index as the new description field's index into description, instead of into
a user field.

Fixes: https://gitlab.com/kicad/code/kicad/-/issues/17202
This commit is contained in:
Mike Williams 2024-03-01 10:15:32 -05:00
parent b57184efe0
commit 1b5e18d95f
1 changed files with 20 additions and 7 deletions

View File

@ -1288,20 +1288,33 @@ SCH_SYMBOL* SCH_IO_KICAD_LEGACY::loadSymbol( LINE_READER& aReader )
int size = schIUScale.MilsToIU( parseInt( aReader, line, &line ) );
int attributes = parseHex( aReader, line, &line );
if( index >= symbol->GetFieldCount() )
// Description was not mandatory until v8.0, so any fields with an index
// past this point should be user fields
if( index > DATASHEET_FIELD )
{
// The first MANDATOR_FIELDS _must_ be constructed within the SCH_SYMBOL
// constructor. This assert is simply here to guard against a change in that
// constructor.
wxASSERT( symbol->GetFieldCount() >= MANDATORY_FIELDS );
// Ignore the _supplied_ fieldNdx. It is not important anymore if within the
// user defined fields region (i.e. >= MANDATORY_FIELDS).
// We freely renumber the index to fit the next available field slot.
index = symbol->GetFieldCount(); // new has this index after insertion
// We need to check for an existing field by name that happens to have the same
// name and index as any field that was made mandatory after this point, e.g. Description
// is now mandatory but a user could have easily added their own user field named Description
// after Datasheet before it was mandatory, and we don't want to add a second Description field.
SCH_FIELD* existingField = symbol->GetFieldByName( name );
SCH_FIELD field( VECTOR2I( 0, 0 ), index, symbol.get(), name );
symbol->AddField( field );
if( !existingField )
{
// Ignore the _supplied_ fieldNdx. It is not important anymore if within the
// user defined fields region (i.e. >= MANDATORY_FIELDS).
// We freely renumber the index to fit the next available field slot.
index = symbol->GetFieldCount(); // new has this index after insertion
SCH_FIELD field( VECTOR2I( 0, 0 ), index, symbol.get(), name );
symbol->AddField( field );
}
else
index = existingField->GetId();
}
SCH_FIELD& field = symbol->GetFields()[index];