From e29d77c8914ab59cebbb712294860cd34373e4e2 Mon Sep 17 00:00:00 2001 From: Maciej Suminski Date: Fri, 25 Aug 2017 15:27:06 +0200 Subject: [PATCH] 'Update Field Values' tool for eeschema --- eeschema/eeschema_id.h | 1 + eeschema/menubar.cpp | 6 +++ eeschema/sch_component.cpp | 86 ++++++++++++++++++++++---------------- eeschema/sch_component.h | 7 ++++ eeschema/schframe.cpp | 28 +++++++++++++ eeschema/schframe.h | 1 + 6 files changed, 92 insertions(+), 37 deletions(-) diff --git a/eeschema/eeschema_id.h b/eeschema/eeschema_id.h index d42143c3ff..9317b23c12 100644 --- a/eeschema/eeschema_id.h +++ b/eeschema/eeschema_id.h @@ -71,6 +71,7 @@ enum id_eeschema_frm ID_GET_ANNOTATE, ID_GET_ERC, ID_BACKANNO_ITEMS, + ID_UPDATE_FIELDS, ID_GEN_PLOT_SCHEMATIC, /* Schematic editor veritcal toolbar IDs */ diff --git a/eeschema/menubar.cpp b/eeschema/menubar.cpp index 62042250ce..056a8acb19 100644 --- a/eeschema/menubar.cpp +++ b/eeschema/menubar.cpp @@ -443,6 +443,12 @@ void prepareEditMenu( wxMenu* aParentMenu ) _( "Import Footprint Association File" ), HELP_IMPORT_FOOTPRINTS, KiBitmap( import_footprint_names_xpm ) ); + + // Update field values + AddMenuItem( aParentMenu, ID_UPDATE_FIELDS, + _( "Update Field Values" ), + _( "Sets component fields to original library values" ), + KiBitmap( update_fields_xpm ) ); } diff --git a/eeschema/sch_component.cpp b/eeschema/sch_component.cpp index 77ff9477c3..e03b077f00 100644 --- a/eeschema/sch_component.cpp +++ b/eeschema/sch_component.cpp @@ -143,43 +143,8 @@ SCH_COMPONENT::SCH_COMPONENT( LIB_PART& aPart, SCH_SHEET_PATH* sheet, int unit, if( setNewItemFlag ) m_Flags = IS_NEW | IS_MOVED; - // Import user defined fields from the library component: - LIB_FIELDS libFields; - - aPart.GetFields( libFields ); - - for( LIB_FIELDS::iterator it = libFields.begin(); it!=libFields.end(); ++it ) - { - // Can no longer insert an empty name, since names are now keys. The - // field index is not used beyond the first MANDATORY_FIELDS - if( it->GetName().IsEmpty() ) - continue; - - // See if field already exists (mandatory fields always exist). - // for mandatory fields, the name and field id are fixed, so we use the - // known and fixed id to get them (more reliable than names, which can be translated) - // for other fields (custom fields), locate the field by same name - // (field id has no known meaning for custom fields) - int idx = it->GetId(); - SCH_FIELD* schField; - - if( idx < MANDATORY_FIELDS ) - schField = GetField( idx ); - else - schField = FindField( it->GetName() ); - - if( !schField ) - { - SCH_FIELD fld( wxPoint( 0, 0 ), GetFieldCount(), this, it->GetName() ); - schField = AddField( fld ); - } - - schField->ImportValues( *it ); - schField->SetText( it->GetText() ); - - // Now the field is initialized, place it to the right position: - schField->SetTextPos( m_Pos + it->GetTextPos() ); - } + // Import user defined fields from the library component + UpdateFields( true, true ); wxString msg = aPart.GetReferenceField().GetText(); @@ -885,6 +850,53 @@ SCH_FIELD* SCH_COMPONENT::FindField( const wxString& aFieldName, bool aIncludeDe } +void SCH_COMPONENT::UpdateFields( bool aResetStyle, bool aResetRef ) +{ + if( PART_SPTR part = m_part.lock() ) + { + LIB_FIELDS fields; + part->GetFields( fields ); + + for( const LIB_FIELD& field : fields ) + { + // Can no longer insert an empty name, since names are now keys. The + // field index is not used beyond the first MANDATORY_FIELDS + if( field.GetName().IsEmpty() ) + continue; + + // See if field already exists (mandatory fields always exist). + // for mandatory fields, the name and field id are fixed, so we use the + // known and fixed id to get them (more reliable than names, which can be translated) + // for other fields (custom fields), locate the field by same name + // (field id has no known meaning for custom fields) + int idx = field.GetId(); + SCH_FIELD* schField; + + if( idx == REFERENCE && !aResetRef ) + continue; + if( idx < MANDATORY_FIELDS ) + schField = GetField( idx ); + else + schField = FindField( field.GetName() ); + + if( !schField ) + { + SCH_FIELD fld( wxPoint( 0, 0 ), GetFieldCount(), this, field.GetName() ); + schField = AddField( fld ); + } + + if( aResetStyle ) + { + schField->ImportValues( field ); + schField->SetTextPos( m_Pos + field.GetTextPos() ); + } + + schField->SetText( field.GetText() ); + } + } +} + + LIB_PIN* SCH_COMPONENT::GetPin( const wxString& number ) { if( PART_SPTR part = m_part.lock() ) diff --git a/eeschema/sch_component.h b/eeschema/sch_component.h index 2d6d047f69..82f0bc1b37 100644 --- a/eeschema/sch_component.h +++ b/eeschema/sch_component.h @@ -363,6 +363,13 @@ public: m_Fields = aFields; // vector copying, length is changed possibly } + /** + * Restores fields to the original library values. + * @param aResetStyle selects whether fields should reset the position and text attribute. + * @param aResetRef selects whether the reference field should be restored. + */ + void UpdateFields( bool aResetStyle, bool aResetRef = false ); + /** * Return the number of fields in this symbol. */ diff --git a/eeschema/schframe.cpp b/eeschema/schframe.cpp index 0378b2ef36..8ee308f734 100644 --- a/eeschema/schframe.cpp +++ b/eeschema/schframe.cpp @@ -272,6 +272,7 @@ BEGIN_EVENT_TABLE( SCH_EDIT_FRAME, EDA_DRAW_FRAME ) EVT_TOOL( ID_FIND_ITEMS, SCH_EDIT_FRAME::OnFindItems ) EVT_TOOL( wxID_REPLACE, SCH_EDIT_FRAME::OnFindItems ) EVT_TOOL( ID_BACKANNO_ITEMS, SCH_EDIT_FRAME::OnLoadCmpToFootprintLinkFile ) + EVT_TOOL( ID_UPDATE_FIELDS, SCH_EDIT_FRAME::OnUpdateFields ) EVT_TOOL( ID_SCH_MOVE_ITEM, SCH_EDIT_FRAME::OnMoveItem ) EVT_TOOL( ID_AUTOPLACE_FIELDS, SCH_EDIT_FRAME::OnAutoplaceFields ) EVT_MENU( wxID_HELP, EDA_DRAW_FRAME::GetKicadHelp ) @@ -1003,6 +1004,33 @@ void SCH_EDIT_FRAME::OnLoadCmpToFootprintLinkFile( wxCommandEvent& event ) } +void SCH_EDIT_FRAME::OnUpdateFields( wxCommandEvent& event ) +{ + PICKED_ITEMS_LIST itemsList; + SCH_TYPE_COLLECTOR c; + c.Collect( GetScreen()->GetDrawItems(), SCH_COLLECTOR::ComponentsOnly ); + + // Create a single undo buffer entry for all components + for( int i = 0; i < c.GetCount(); ++i ) + { + SCH_COMPONENT* component = static_cast( c[i] ); + itemsList.PushItem( ITEM_PICKER( component, UR_CHANGED ) ); + } + + SaveCopyInUndoList( itemsList, UR_CHANGED ); + + // Update fields + for( int i = 0; i < c.GetCount(); ++i ) + { + SCH_COMPONENT* component = static_cast( c[i] ); + component->UpdateFields( false ); + } + + m_canvas->Refresh(); + DisplayInfoMessage( this, _( "Fields updated successfully" ) ); +} + + void SCH_EDIT_FRAME::OnNewProject( wxCommandEvent& event ) { // wxString pro_dir = wxPathOnly( Prj().GetProjectFullName() ); diff --git a/eeschema/schframe.h b/eeschema/schframe.h index ab07418ea2..c990fc2b0a 100644 --- a/eeschema/schframe.h +++ b/eeschema/schframe.h @@ -864,6 +864,7 @@ private: void OnLoadFile( wxCommandEvent& event ); void OnLoadCmpToFootprintLinkFile( wxCommandEvent& event ); + void OnUpdateFields( wxCommandEvent& event ); void OnNewProject( wxCommandEvent& event ); void OnLoadProject( wxCommandEvent& event ); void OnAppendProject( wxCommandEvent& event );