Fix usage of PCB_FIELD( PCB_TEXT& )

Passing a pointer to the CTOR invokes the PCB_FIELD( BOARD_ITEM* ) ctor,
which is not what is intended.  Passing just the reference is better but
still ends up creating a PCB_FIELD class with type PCB_TEXT because it
didn't correctly set the type.  So we need to properly set our type and
our parent, then copy the text attributes over.  We can't use the
default copy operator because this also copies the struct_id of PCB_TEXT

Fixes https://gitlab.com/kicad/code/kicad/-/issues/20875
This commit is contained in:
Seth Hillbrand 2025-05-13 17:41:17 -07:00
parent c2c987f9c0
commit 750dbe6336
4 changed files with 10 additions and 4 deletions

View File

@ -43,11 +43,17 @@ PCB_FIELD::PCB_FIELD( FOOTPRINT* aParent, FIELD_T aFieldId, const wxString& aNam
PCB_FIELD::PCB_FIELD( const PCB_TEXT& aText, FIELD_T aFieldId, const wxString& aName ) :
PCB_TEXT( aText ),
PCB_TEXT( aText.GetParent(), PCB_FIELD_T ),
m_id( aFieldId ),
m_ordinal( static_cast<int>( aFieldId ) ),
m_name( aName )
{
// Copy the text properties from the PCB_TEXT
SetText( aText.GetText() );
SetVisible( aText.IsVisible() );
SetLayer( aText.GetLayer() );
SetPosition( aText.GetPosition() );
SetAttributes( aText.GetAttributes() );
}

View File

@ -2593,7 +2593,7 @@ bool FABMASTER::loadFootprints( BOARD* aBoard )
// should be hidden by default to prevent clutter.
if( txt->GetLayer() != F_SilkS && txt->GetLayer() != B_SilkS )
{
PCB_FIELD* field = new PCB_FIELD( txt.get(), FIELD_T::USER );
PCB_FIELD* field = new PCB_FIELD( *txt, FIELD_T::USER );
field->SetVisible( false );
fp->Add( field, ADD_MODE::APPEND );
}

View File

@ -1245,7 +1245,7 @@ void PCB_IO_KICAD_LEGACY::loadFOOTPRINT( FOOTPRINT* aFootprint )
if( !text->IsVisible() && text->Type() == PCB_TEXT_T )
{
aFootprint->Remove( text );
aFootprint->Add( new PCB_FIELD( text, FIELD_T::USER ) );
aFootprint->Add( new PCB_FIELD( *text, FIELD_T::USER ) );
delete text;
}
}

View File

@ -3461,7 +3461,7 @@ PCB_TEXT* PCB_IO_KICAD_SEXPR_PARSER::parsePCB_TEXT( BOARD_ITEM* aParent, PCB_TEX
{
// Convert hidden footprint text (which is no longer supported) into a hidden field
if( !text->IsVisible() && text->Type() == PCB_TEXT_T )
return new PCB_FIELD( text.get(), FIELD_T::USER );
return new PCB_FIELD( *text.get(), FIELD_T::USER );
}
else
{