2022-09-06 20:26:36 +00:00
|
|
|
/*
|
|
|
|
* This program source code file is part of KiCad, a free EDA CAD application.
|
|
|
|
*
|
|
|
|
* Copyright (C) 2022 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 3
|
|
|
|
* 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 at
|
|
|
|
* http://www.gnu.org/licenses/
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <schematic_file_util.h>
|
2022-12-04 15:01:19 +00:00
|
|
|
#include <qa_utils/wx_utils/unit_test_utils.h> // GetEeschemaTestDataDir()
|
2022-09-06 20:26:36 +00:00
|
|
|
|
|
|
|
#include <settings/settings_manager.h>
|
|
|
|
|
|
|
|
#include <connection_graph.h>
|
|
|
|
#include <lib_textbox.h>
|
|
|
|
#include <schematic.h>
|
|
|
|
#include <sch_screen.h>
|
|
|
|
|
|
|
|
// For SCH parsing
|
|
|
|
#include <sch_plugins/kicad/sch_sexpr_plugin.h>
|
|
|
|
#include <sch_plugins/kicad/sch_sexpr_parser.h>
|
|
|
|
#include <richio.h>
|
|
|
|
|
|
|
|
#include <qa_utils/stdstream_line_reader.h>
|
|
|
|
|
|
|
|
namespace KI_TEST
|
|
|
|
{
|
|
|
|
|
|
|
|
void DumpSchematicToFile( SCHEMATIC& aSchematic, SCH_SHEET& aSheet, const std::string& aFilename )
|
|
|
|
{
|
|
|
|
SCH_SEXPR_PLUGIN io;
|
2023-08-26 19:28:53 +00:00
|
|
|
io.SaveSchematicFile( aFilename, &aSheet, &aSchematic );
|
2022-09-06 20:26:36 +00:00
|
|
|
}
|
|
|
|
|
2023-01-20 19:01:35 +00:00
|
|
|
void LoadSheetSchematicContents( const std::string& fileName, SCH_SHEET* sheet )
|
2022-09-06 20:26:36 +00:00
|
|
|
{
|
2023-01-20 19:01:35 +00:00
|
|
|
std::ifstream fileStream;
|
|
|
|
fileStream.open( fileName );
|
|
|
|
wxASSERT( fileStream.is_open() );
|
2022-09-06 20:26:36 +00:00
|
|
|
STDISTREAM_LINE_READER reader;
|
2023-01-20 19:01:35 +00:00
|
|
|
reader.SetStream( fileStream );
|
|
|
|
SCH_SEXPR_PARSER parser( &reader );
|
|
|
|
parser.ParseSchematic( sheet );
|
|
|
|
}
|
2022-09-06 20:26:36 +00:00
|
|
|
|
2023-01-20 19:01:35 +00:00
|
|
|
void LoadHierarchy( SCHEMATIC* schematic, SCH_SHEET* sheet, const std::string& sheetFilename,
|
|
|
|
std::unordered_map<std::string, SCH_SCREEN*>& parsedScreens )
|
|
|
|
{
|
|
|
|
SCH_SCREEN* screen = nullptr;
|
2022-09-06 20:26:36 +00:00
|
|
|
|
2023-01-20 19:01:35 +00:00
|
|
|
if( !sheet->GetScreen() )
|
2022-09-06 20:26:36 +00:00
|
|
|
{
|
2023-01-20 19:01:35 +00:00
|
|
|
// Construct paths
|
|
|
|
const wxFileName fileName( sheetFilename );
|
|
|
|
const std::string filePath( fileName.GetPath( wxPATH_GET_VOLUME | wxPATH_GET_SEPARATOR ) );
|
|
|
|
const std::string fileBareName( fileName.GetFullName() );
|
|
|
|
|
|
|
|
// Check for existing screen
|
|
|
|
auto screenFound = parsedScreens.find( fileBareName );
|
|
|
|
if( screenFound != parsedScreens.end() )
|
|
|
|
screen = screenFound->second;
|
|
|
|
|
|
|
|
// Configure sheet with existing screen, or load screen
|
|
|
|
if( screen )
|
|
|
|
{
|
|
|
|
// Screen already loaded - assign to sheet
|
|
|
|
sheet->SetScreen( screen );
|
|
|
|
sheet->GetScreen()->SetParent( schematic );
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// Load screen and assign to sheet
|
|
|
|
screen = new SCH_SCREEN( schematic );
|
|
|
|
parsedScreens.insert( { fileBareName, screen } );
|
|
|
|
sheet->SetScreen( screen );
|
|
|
|
sheet->GetScreen()->SetFileName( sheetFilename );
|
|
|
|
LoadSheetSchematicContents( sheetFilename, sheet );
|
|
|
|
}
|
|
|
|
|
|
|
|
// Recurse through child sheets
|
|
|
|
for( SCH_ITEM* item : sheet->GetScreen()->Items().OfType( SCH_SHEET_T ) )
|
|
|
|
{
|
|
|
|
SCH_SHEET* childSheet = static_cast<SCH_SHEET*>( item );
|
|
|
|
wxFileName childSheetFilename = childSheet->GetFileName();
|
|
|
|
if( !childSheetFilename.IsAbsolute() )
|
|
|
|
childSheetFilename.MakeAbsolute( filePath );
|
|
|
|
std::string childSheetFullFilename( childSheetFilename.GetFullPath() );
|
|
|
|
LoadHierarchy( schematic, childSheet, childSheetFullFilename, parsedScreens );
|
|
|
|
}
|
2022-09-06 20:26:36 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-20 19:01:35 +00:00
|
|
|
std::unique_ptr<SCHEMATIC> LoadHierarchyFromRoot( const std::string& rootFilename,
|
|
|
|
PROJECT* project )
|
2022-09-06 20:26:36 +00:00
|
|
|
{
|
2023-01-20 19:01:35 +00:00
|
|
|
std::unique_ptr<SCHEMATIC> schematic( new SCHEMATIC( nullptr ) );
|
|
|
|
std::unordered_map<std::string, SCH_SCREEN*> parsedScreens;
|
2022-09-06 20:26:36 +00:00
|
|
|
|
2023-01-20 19:01:35 +00:00
|
|
|
schematic->SetProject( project );
|
|
|
|
SCH_SHEET* rootSheet = new SCH_SHEET( schematic.get() );
|
|
|
|
schematic->SetRoot( rootSheet );
|
|
|
|
LoadHierarchy( schematic.get(), rootSheet, rootFilename, parsedScreens );
|
2022-09-06 20:26:36 +00:00
|
|
|
|
2023-01-20 19:01:35 +00:00
|
|
|
return schematic;
|
2022-09-06 20:26:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void LoadSchematic( SETTINGS_MANAGER& aSettingsManager, const wxString& aRelPath,
|
2022-12-06 14:00:11 +00:00
|
|
|
std::unique_ptr<SCHEMATIC>& aSchematic )
|
2022-09-06 20:26:36 +00:00
|
|
|
{
|
|
|
|
if( aSchematic )
|
|
|
|
{
|
|
|
|
PROJECT* prj = &aSchematic->Prj();
|
|
|
|
|
|
|
|
aSchematic->SetProject( nullptr );
|
|
|
|
aSettingsManager.UnloadProject( prj, false );
|
|
|
|
aSchematic->Reset();
|
|
|
|
}
|
|
|
|
|
2022-12-04 15:01:19 +00:00
|
|
|
std::string absPath = GetEeschemaTestDataDir() + aRelPath.ToStdString();
|
2022-09-06 20:26:36 +00:00
|
|
|
wxFileName projectFile( absPath + ".kicad_pro" );
|
|
|
|
wxFileName legacyProject( absPath + ".pro" );
|
|
|
|
std::string schematicPath = absPath + ".kicad_sch";
|
|
|
|
|
|
|
|
if( projectFile.Exists() )
|
|
|
|
aSettingsManager.LoadProject( projectFile.GetFullPath() );
|
|
|
|
else if( legacyProject.Exists() )
|
|
|
|
aSettingsManager.LoadProject( legacyProject.GetFullPath() );
|
|
|
|
else
|
|
|
|
aSettingsManager.LoadProject( "" );
|
|
|
|
|
|
|
|
aSettingsManager.Prj().SetElem( PROJECT::ELEM_SCH_SYMBOL_LIBS, nullptr );
|
|
|
|
|
2023-01-20 19:01:35 +00:00
|
|
|
aSchematic = LoadHierarchyFromRoot( schematicPath, &aSettingsManager.Prj() );
|
2022-09-06 20:26:36 +00:00
|
|
|
|
2022-12-07 01:34:10 +00:00
|
|
|
SCH_SCREENS screens( aSchematic->Root() );
|
2022-09-06 20:26:36 +00:00
|
|
|
|
2022-12-07 01:34:10 +00:00
|
|
|
for( SCH_SCREEN* screen = screens.GetFirst(); screen; screen = screens.GetNext() )
|
|
|
|
screen->UpdateLocalLibSymbolLinks();
|
2022-09-06 20:26:36 +00:00
|
|
|
|
2022-12-07 01:34:10 +00:00
|
|
|
SCH_SHEET_LIST sheets = aSchematic->GetSheets();
|
2022-12-06 14:00:11 +00:00
|
|
|
|
2022-12-07 01:34:10 +00:00
|
|
|
// Restore all of the loaded symbol instances from the root sheet screen.
|
|
|
|
sheets.UpdateSymbolInstanceData( aSchematic->RootScreen()->GetSymbolInstances() );
|
|
|
|
sheets.UpdateSheetInstanceData( aSchematic->RootScreen()->GetSheetInstances() );
|
2022-09-06 20:26:36 +00:00
|
|
|
|
2023-02-20 17:55:02 +00:00
|
|
|
if( aSchematic->RootScreen()->GetFileFormatVersionAtLoad() < 20230221 )
|
|
|
|
screens.FixLegacyPowerSymbolMismatches();
|
|
|
|
|
2022-12-07 01:34:10 +00:00
|
|
|
if( aSchematic->RootScreen()->GetFileFormatVersionAtLoad() < 20221206 )
|
|
|
|
{
|
|
|
|
for( SCH_SCREEN* screen = screens.GetFirst(); screen; screen = screens.GetNext() )
|
|
|
|
screen->MigrateSimModels();
|
|
|
|
}
|
2022-09-06 20:26:36 +00:00
|
|
|
|
|
|
|
sheets.AnnotatePowerSymbols();
|
|
|
|
|
|
|
|
// NOTE: This is required for multi-unit symbols to be correct
|
|
|
|
// Normally called from SCH_EDIT_FRAME::FixupJunctions() but could be refactored
|
|
|
|
for( SCH_SHEET_PATH& sheet : sheets )
|
|
|
|
sheet.UpdateAllScreenReferences();
|
|
|
|
|
|
|
|
// NOTE: SchematicCleanUp is not called; QA schematics must already be clean or else
|
|
|
|
// SchematicCleanUp must be freed from its UI dependencies.
|
|
|
|
|
|
|
|
aSchematic->ConnectionGraph()->Recalculate( sheets, true );
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace KI_TEST
|