From df186a2049e3f7e5b50bb51739e024ef03122c43 Mon Sep 17 00:00:00 2001 From: Wayne Stambaugh Date: Tue, 6 Apr 2021 14:27:24 -0400 Subject: [PATCH] Eeschema: fix root sheet UUID changing in project file. This change is a bit more invasive than the description above would suggest. UUIDs have been added to all schematics. For now, it's only useful to the root schematic so that the UUID in the project file does not change every time the project file is save. In the future, it may be useful to store and check the schematic UUIDs against the one's saved in the project file. Fixes https://gitlab.com/kicad/code/kicad/issues/7763 --- eeschema/files-io.cpp | 21 ++++++++++++++++++- eeschema/sch_file_versions.h | 6 +++--- .../sch_plugins/kicad/sch_sexpr_parser.cpp | 6 ++++++ .../sch_plugins/kicad/sch_sexpr_plugin.cpp | 2 ++ eeschema/sch_screen.h | 13 ++++++++++++ 5 files changed, 44 insertions(+), 4 deletions(-) diff --git a/eeschema/files-io.cpp b/eeschema/files-io.cpp index 00e6f4fc3f..00fe5ace07 100644 --- a/eeschema/files-io.cpp +++ b/eeschema/files-io.cpp @@ -109,6 +109,10 @@ bool SCH_EDIT_FRAME::SaveEEFile( SCH_SHEET* aSheet, bool aSaveUnderNewName ) if( !IsWritable( schematicFileName ) ) return false; + // This is a new schematic file so make sure it has a unique ID. + if( aSaveUnderNewName ) + screen->AssignNewUuid(); + wxFileName tempFile( schematicFileName ); tempFile.SetName( wxT( "." ) + tempFile.GetName() ); tempFile.SetExt( tempFile.GetExt() + wxT( "$" ) ); @@ -853,7 +857,22 @@ bool SCH_EDIT_FRAME::SaveProject() for( SCH_SHEET_PATH& sheetPath : Schematic().GetSheets() ) { SCH_SHEET* sheet = sheetPath.Last(); - sheets.emplace_back( std::make_pair( sheet->m_Uuid, sheet->GetName() ) ); + + wxCHECK2( sheet, continue ); + + // Use the schematic UUID for the root sheet. + if( sheet->IsRootSheet() ) + { + screen = sheet->GetScreen(); + + wxCHECK2( screen, continue ); + + sheets.emplace_back( std::make_pair( screen->GetUuid(), sheet->GetName() ) ); + } + else + { + sheets.emplace_back( std::make_pair( sheet->m_Uuid, sheet->GetName() ) ); + } } if( !Prj().IsNullProject() ) diff --git a/eeschema/sch_file_versions.h b/eeschema/sch_file_versions.h index d02d0959b3..411cdb71a5 100644 --- a/eeschema/sch_file_versions.h +++ b/eeschema/sch_file_versions.h @@ -1,7 +1,7 @@ /* * This program source code file is part of KiCad, a free EDA CAD application. * - * Copyright (C) 2020 CERN + * Copyright (C) 2020-2021 CERN * * @author Wayne Stambaugh * @@ -26,7 +26,6 @@ * * @note Comment out the last version and add the new version as a date time stamp in the * YYYYMMDD format. Comment the changes to the file format for historical purposes. - * */ /** @@ -60,4 +59,5 @@ //#define SEXPR_SCHEMATIC_FILE_VERSION 20201015 // Add sheet instance properties. //#define SEXPR_SCHEMATIC_FILE_VERSION 20210123 // Rename "unconnected" pintype to "no_connect". //#define SEXPR_SCHEMATIC_FILE_VERSION 20210125 // R/W uuids for pins, labels, wires, etc. -#define SEXPR_SCHEMATIC_FILE_VERSION 20210126 // Fix bug with writing pin uuids. +//#define SEXPR_SCHEMATIC_FILE_VERSION 20210126 // Fix bug with writing pin uuids. +#define SEXPR_SCHEMATIC_FILE_VERSION 20210406 // Add schematic level uuids. diff --git a/eeschema/sch_plugins/kicad/sch_sexpr_parser.cpp b/eeschema/sch_plugins/kicad/sch_sexpr_parser.cpp index 8ec33244b8..53e92e1709 100644 --- a/eeschema/sch_plugins/kicad/sch_sexpr_parser.cpp +++ b/eeschema/sch_plugins/kicad/sch_sexpr_parser.cpp @@ -2046,6 +2046,12 @@ void SCH_SEXPR_PARSER::ParseSchematic( SCH_SHEET* aSheet, bool aIsCopyableOnly, switch( token ) { + case T_uuid: + NeedSYMBOL(); + screen->m_uuid = KIID( FromUTF8() ); + NeedRIGHT(); + break; + case T_paper: { if( aIsCopyableOnly ) diff --git a/eeschema/sch_plugins/kicad/sch_sexpr_plugin.cpp b/eeschema/sch_plugins/kicad/sch_sexpr_plugin.cpp index efc5fed865..b8d7d2420f 100644 --- a/eeschema/sch_plugins/kicad/sch_sexpr_plugin.cpp +++ b/eeschema/sch_plugins/kicad/sch_sexpr_plugin.cpp @@ -587,6 +587,8 @@ void SCH_SEXPR_PLUGIN::Format( SCH_SHEET* aSheet ) m_out->Print( 0, "(kicad_sch (version %d) (generator eeschema)\n\n", SEXPR_SCHEMATIC_FILE_VERSION ); + m_out->Print( 1, "(uuid %s)\n\n", TO_UTF8( screen->m_uuid.AsString() ) ); + screen->GetPageSettings().Format( m_out, 1, 0 ); m_out->Print( 0, "\n" ); screen->GetTitleBlock().Format( m_out, 1, 0 ); diff --git a/eeschema/sch_screen.h b/eeschema/sch_screen.h index 026a31899f..47e2b0caac 100644 --- a/eeschema/sch_screen.h +++ b/eeschema/sch_screen.h @@ -37,6 +37,7 @@ #include #include #include +#include #include #include #include @@ -457,6 +458,10 @@ public: return m_sheetInstances; } + const KIID& GetUuid() const { return m_uuid; } + + void AssignNewUuid() { m_uuid = KIID(); } + #if defined(DEBUG) void Show( int nestLevel, std::ostream& os ) const override; #endif @@ -521,6 +526,14 @@ private: */ std::vector m_symbolInstances; std::vector m_sheetInstances; + + /** + * A unique identifier for each schematic file. + * + * As of right now, this only has meaning for the root schematic. In the future, it may + * be useful to detect unexpected hierarchy changes. + */ + KIID m_uuid; };