From 2a9186d9e997f53dd4e5ff75063b028997e3f22b Mon Sep 17 00:00:00 2001 From: Jeff Young Date: Tue, 9 Mar 2021 15:08:51 +0000 Subject: [PATCH] Don't re-use FieldIDs when assigning ids on save. Fixes https://gitlab.com/kicad/code/kicad/issues/7849 --- .../sch_plugins/kicad/sch_sexpr_plugin.cpp | 26 +++++++++++-------- eeschema/sch_plugins/kicad/sch_sexpr_plugin.h | 5 ++-- 2 files changed, 17 insertions(+), 14 deletions(-) diff --git a/eeschema/sch_plugins/kicad/sch_sexpr_plugin.cpp b/eeschema/sch_plugins/kicad/sch_sexpr_plugin.cpp index 28784b90e7..2806eb2b4a 100644 --- a/eeschema/sch_plugins/kicad/sch_sexpr_plugin.cpp +++ b/eeschema/sch_plugins/kicad/sch_sexpr_plugin.cpp @@ -378,13 +378,13 @@ SCH_SEXPR_PLUGIN::~SCH_SEXPR_PLUGIN() void SCH_SEXPR_PLUGIN::init( SCHEMATIC* aSchematic, const PROPERTIES* aProperties ) { - m_version = 0; - m_rootSheet = nullptr; - m_props = aProperties; - m_schematic = aSchematic; - m_cache = nullptr; - m_out = nullptr; - m_fieldId = 100; // number arbitrarily > MANDATORY_FIELDS or SHEET_MANDATORY_FIELDS + m_version = 0; + m_rootSheet = nullptr; + m_props = aProperties; + m_schematic = aSchematic; + m_cache = nullptr; + m_out = nullptr; + m_nextFreeFieldId = 100; // number arbitrarily > MANDATORY_FIELDS or SHEET_MANDATORY_FIELDS } SCH_SHEET* SCH_SEXPR_PLUGIN::Load( const wxString& aFileName, SCHEMATIC* aSchematic, @@ -957,7 +957,7 @@ void SCH_SEXPR_PLUGIN::saveSymbol( SCH_COMPONENT* aSymbol, SCH_SHEET_PATH* aShee m_out->Print( aNestLevel + 1, "(uuid %s)\n", TO_UTF8( aSymbol->m_Uuid.AsString() ) ); - m_fieldId = MANDATORY_FIELDS; + m_nextFreeFieldId = MANDATORY_FIELDS; for( SCH_FIELD& field : aSymbol->GetFields() ) { @@ -1006,8 +1006,12 @@ void SCH_SEXPR_PLUGIN::saveField( SCH_FIELD* aField, int aNestLevel ) if( aField->GetId() == -1 /* undefined ID */ ) { - aField->SetId( m_fieldId ); - m_fieldId += 1; + aField->SetId( m_nextFreeFieldId ); + m_nextFreeFieldId += 1; + } + else if( aField->GetId() >= m_nextFreeFieldId ) + { + m_nextFreeFieldId = aField->GetId() + 1; } m_out->Print( aNestLevel, "(property %s %s (id %d) (at %s %s %s)", @@ -1106,7 +1110,7 @@ void SCH_SEXPR_PLUGIN::saveSheet( SCH_SHEET* aSheet, int aNestLevel ) m_out->Print( aNestLevel + 1, "(uuid %s)\n", TO_UTF8( aSheet->m_Uuid.AsString() ) ); - m_fieldId = SHEET_MANDATORY_FIELDS; + m_nextFreeFieldId = SHEET_MANDATORY_FIELDS; for( SCH_FIELD& field : aSheet->GetFields() ) { diff --git a/eeschema/sch_plugins/kicad/sch_sexpr_plugin.h b/eeschema/sch_plugins/kicad/sch_sexpr_plugin.h index aed2618ba7..7dadaf09c5 100644 --- a/eeschema/sch_plugins/kicad/sch_sexpr_plugin.h +++ b/eeschema/sch_plugins/kicad/sch_sexpr_plugin.h @@ -147,10 +147,9 @@ private: protected: int m_version; ///< Version of file being loaded. - int m_fieldId; ///< Non-mandatory schematic field ID counter. + int m_nextFreeFieldId; - /** For throwing exceptions or errors on partial schematic loads. */ - wxString m_error; + wxString m_error; ///< For throwing exceptions or errors on partial loads. wxString m_path; ///< Root project path for loading child sheets. std::stack m_currentPath;///< Stack to maintain nested sheet paths