PCB_FIELD: add new field item similar to SCH_FIELD

This commit is contained in:
Mike Williams 2023-05-24 07:43:32 -04:00
parent 941d9c957d
commit 968785382e
4 changed files with 192 additions and 0 deletions

View File

@ -554,6 +554,7 @@ set( PCB_COMMON_SRCS
${CMAKE_SOURCE_DIR}/pcbnew/pad.cpp ${CMAKE_SOURCE_DIR}/pcbnew/pad.cpp
${CMAKE_SOURCE_DIR}/pcbnew/pcb_target.cpp ${CMAKE_SOURCE_DIR}/pcbnew/pcb_target.cpp
${CMAKE_SOURCE_DIR}/pcbnew/pcb_bitmap.cpp ${CMAKE_SOURCE_DIR}/pcbnew/pcb_bitmap.cpp
${CMAKE_SOURCE_DIR}/pcbnew/pcb_field.cpp
${CMAKE_SOURCE_DIR}/pcbnew/pcb_text.cpp ${CMAKE_SOURCE_DIR}/pcbnew/pcb_text.cpp
${CMAKE_SOURCE_DIR}/pcbnew/pcb_textbox.cpp ${CMAKE_SOURCE_DIR}/pcbnew/pcb_textbox.cpp
${CMAKE_SOURCE_DIR}/pcbnew/board_stackup_manager/board_stackup.cpp ${CMAKE_SOURCE_DIR}/pcbnew/board_stackup_manager/board_stackup.cpp

109
pcbnew/pcb_field.cpp Normal file
View File

@ -0,0 +1,109 @@
/*
* This program source code file is part of KiCad, a free EDA CAD application.
*
* Copyright (C) 2023 Mike Williams, mike@mikebwilliams.com
* Copyright (C) 1992-2023 KiCad Developers, see AUTHORS.txt for contributors.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, you may find one here:
* http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
* or you may search the http://www.gnu.org website for the version 2 license,
* or you may write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
*/
#include <pcb_field.h>
PCB_FIELD::PCB_FIELD( FOOTPRINT* aParent, int aFieldId, const wxString& aName ) :
PCB_TEXT( aParent, TEXT_TYPE( aFieldId ) )
{
m_name = aName;
SetId( aFieldId );
}
PCB_FIELD::PCB_FIELD( const PCB_TEXT& aText, int aFieldId, const wxString& aName ) :
PCB_TEXT( aText )
{
m_name = aName;
SetId( aFieldId );
}
wxString PCB_FIELD::GetName( bool aUseDefaultName ) const
{
if( m_parent && m_parent->Type() == PCB_FOOTPRINT_T )
{
if( m_id >= 0 && m_id < MANDATORY_FIELDS )
return TEMPLATE_FIELDNAME::GetDefaultFieldName( m_id );
else if( m_name.IsEmpty() && aUseDefaultName )
return TEMPLATE_FIELDNAME::GetDefaultFieldName( m_id );
else
return m_name;
}
else
{
wxFAIL_MSG( "Unhandled field owner type." );
return m_name;
}
}
wxString PCB_FIELD::GetCanonicalName() const
{
if( m_parent && m_parent->Type() == PCB_FOOTPRINT_T )
{
switch( m_id )
{
case REFERENCE_FIELD: return wxT( "Reference" );
case VALUE_FIELD: return wxT( "Value" );
case FOOTPRINT_FIELD: return wxT( "Footprint" );
case DATASHEET_FIELD: return wxT( "Datasheet" );
default: return m_name;
}
}
else
{
if( m_parent )
{
wxFAIL_MSG( wxString::Format( "Unhandled field owner type (id %d, parent type %d).",
m_id, m_parent->Type() ) );
}
return m_name;
}
}
void PCB_FIELD::SetId( int aId )
{
m_id = aId;
switch(m_id)
{
case REFERENCE_FIELD:
SetType(TEXT_is_REFERENCE);
break;
case VALUE_FIELD:
SetType(TEXT_is_VALUE);
break;
default:
SetType(TEXT_is_DIVERS);
break;
}
}
EDA_ITEM* PCB_FIELD::Clone() const
{
return new PCB_FIELD( *this );
}

80
pcbnew/pcb_field.h Normal file
View File

@ -0,0 +1,80 @@
/*
* This program source code file is part of KiCad, a free EDA CAD application.
*
* Copyright (C) 2023 Mike Williams, mike@mikebwilliams.com
* Copyright (C) 1992-2023 KiCad Developers, see AUTHORS.txt for contributors.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, you may find one here:
* http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
* or you may search the http://www.gnu.org website for the version 2 license,
* or you may write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
*/
#ifndef PCB_FIELD_H
#define PCB_FIELD_H
#include <pcb_text.h>
#include <template_fieldnames.h>
class PCB_FIELD : public PCB_TEXT
{
public:
PCB_FIELD( FOOTPRINT* aParent, int aFieldId, const wxString& aName = wxEmptyString );
PCB_FIELD( const PCB_TEXT& aText, int aFieldId, const wxString& aName = wxEmptyString );
EDA_ITEM* Clone() const override;
/**
* Same as Clone, but returns a PCB_FIELD item.
*
* Useful mainly for python scripts, because Clone returns an EDA_ITEM.
*/
PCB_FIELD* CloneField() const { return (PCB_FIELD*) Clone(); }
/**
* Return the field name (not translated)..
*
* @param aUseDefaultName When true return the default field name if the field name is
* empty. Otherwise the default field name is returned.
* @return the name of the field.
*/
wxString GetName( bool aUseDefaultName = true ) const;
/**
* Get a non-language-specific name for a field which can be used for storage, variable
* look-up, etc.
*/
wxString GetCanonicalName() const;
void SetName( const wxString& aName ) { m_name = aName; }
/**
* Get the initial name of the field set at creation (or set by SetName()).
* This is the raw field name with no translation and no change.
*/
const wxString& GetInternalName() { return m_name; }
int GetId() const { return m_id; }
void SetId( int aId );
private:
int m_id; ///< Field index, @see enum MANDATORY_FIELD_T
wxString m_name;
};
#endif

View File

@ -49,7 +49,9 @@ DECL_DEQ_FOR_SWIG( DRAWINGS, BOARD_ITEM* )
// Footprint-level items // Footprint-level items
class PAD; class PAD;
class PCB_FIELD;
DECL_DEQ_FOR_SWIG( PADS, PAD* ) DECL_DEQ_FOR_SWIG( PADS, PAD* )
DECL_DEQ_FOR_SWIG( PCB_FIELDS, PCB_FIELD* )
#endif // PCB_ITEM_CONTAINERS_H_ #endif // PCB_ITEM_CONTAINERS_H_