kicad学习笔记7:获取和设置sch_symbol或lib_symbol的field值

1.从sch_symbol中获取field并传递给lib_symbol

    std::unique_ptr<LIB_SYMBOL> symbol = aSymbol->GetLibSymbolRef()->Flatten();
    wxCHECK( symbol, /* void */ );

    std::vector<LIB_FIELD> fullSetOfFields;

    for( int i = 0; i < (int) aSymbol->GetFields().size(); ++i )
    {
        const SCH_FIELD& field = aSymbol->GetFields()[i];
        VECTOR2I         pos = field.GetPosition() - aSymbol->GetPosition();
        LIB_FIELD        libField( symbol.get(), field.GetId() );

        if( i >= MANDATORY_FIELDS && !field.GetName( false ).IsEmpty() )
            libField.SetName( field.GetName( false ) );

        libField.SetText( field.GetText() );
        libField.SetAttributes( field );
        libField.SetPosition( VECTOR2I( pos.x, -pos.y ) );

        fullSetOfFields.emplace_back( std::move( libField ) );
    }

    symbol->SetFields( fullSetOfFields );

2.获取或修改sch_symbol中的field

sch_symbol.h文件中定义了一个关于SCH_Field的变量:

 std::vector<SCH_FIELD>                 m_fields;    ///< Variable length list of fields.

设置field的值:

设置某个field的值,比如reference的值

static constexpr auto REFERENCE_FIELD = "Reference";
wxString aRef="U";
 m_fields[ REFERENCE_FIELD ].SetText( aRef );

设置valuefield的值:

void SCH_SYMBOL::SetValueFieldText( const wxString& aValue )
{
    m_fields[ VALUE_FIELD ].SetText( aValue );
}

valuefield、referencefield都是模板属性,那么普通属性值如何设置呢?

  • 首先我们要获取sch_fied,通过名字来获取
SCH_FIELD* FindField( const wxString& aFieldName, bool aIncludeDefaultFields );
SCH_FIELD* SCH_SYMBOL::FindField( const wxString& aFieldName, bool aIncludeDefaultFields )
{
    unsigned start = aIncludeDefaultFields ? 0 : MANDATORY_FIELDS;

    for( unsigned i = start; i < m_fields.size(); ++i )
    {
        if( aFieldName == m_fields[i].GetName( false ) )
            return &m_fields[i];
    }

    return nullptr;
}
  • 然后调用sch_field中的SetText方法

获取field的值:

获取datasheetfield的值:

wxString SCH_SYMBOL::GetDataSheetFiledText(  bool aResolve ) const
{
    if( aResolve )
        return GetField( DATASHEET_FIELD )->GetShownText();

    return GetField( DATASHEET_FIELD )->GetText();
}

datasheetfield是symbol中的模板属性,每个symbol都会有改属性。也有很多属性不是模板属性,这些普通属性的值也可以通过GetFieldText()获得

wxString SCH_SYMBOL::GetFieldText( const wxString& aFieldName ) const
{
    for( const SCH_FIELD& field : m_fields )
    {
        if( aFieldName == field.GetName() || aFieldName == field.GetCanonicalName() )
            return field.GetText();
    }

    return wxEmptyString;
}

是否可见

field.IsVisible()

设置是否可见

field.SetVisible( BoolFromString( value ) );
template <class T>
bool FIELDS_GRID_TABLE<T>::BoolFromString( wxString aValue ) const
{
    if( aValue == wxS( "1" ) )
    {
        return true;
    }
    else if( aValue == wxS( "0" ) )
    {
        return false;
    }
    else
    {
        wxFAIL_MSG( wxString::Format( "string '%s' can't be converted to boolean correctly and "
                                      "will be perceived as FALSE", aValue ) );
        return false;
    }
}

你可能感兴趣的:(kicad,wxWidgets,C++,vector,setText)