You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

164 lines
4.9 KiB

  1. /*
  2. * This program source code file is part of KiCad, a free EDA CAD application.
  3. *
  4. * Copyright (C) 2022 KiCad Developers, see AUTHORS.TXT for contributors.
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU General Public License
  8. * as published by the Free Software Foundation; either version 3
  9. * of the License, or (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, you may find one at
  18. * http://www.gnu.org/licenses/
  19. */
  20. #include <schematic_file_util.h>
  21. #include <settings/settings_manager.h>
  22. #include <connection_graph.h>
  23. #include <lib_textbox.h>
  24. #include <schematic.h>
  25. #include <sch_screen.h>
  26. // For SCH parsing
  27. #include <sch_plugins/kicad/sch_sexpr_plugin.h>
  28. #include <sch_plugins/kicad/sch_sexpr_parser.h>
  29. #include <richio.h>
  30. #include <qa_utils/stdstream_line_reader.h>
  31. namespace KI_TEST
  32. {
  33. #ifndef QA_EESCHEMA_DATA_LOCATION
  34. #define QA_EESCHEMA_DATA_LOCATION "???"
  35. #endif
  36. std::string getEeschemaTestDataDir()
  37. {
  38. const char* env = std::getenv( "KICAD_TEST_EESCHEMA_DATA_DIR" );
  39. std::string fn;
  40. if( !env )
  41. {
  42. // Use the compiled-in location of the data dir (i.e. where the files were at build time)
  43. fn = QA_EESCHEMA_DATA_LOCATION;
  44. }
  45. else
  46. {
  47. // Use whatever was given in the env var
  48. fn = env;
  49. }
  50. // Ensure the string ends in / to force a directory interpretation
  51. fn += "/";
  52. return fn;
  53. }
  54. void DumpSchematicToFile( SCHEMATIC& aSchematic, SCH_SHEET& aSheet, const std::string& aFilename )
  55. {
  56. SCH_SEXPR_PLUGIN io;
  57. io.Save( aFilename, &aSheet, &aSchematic );
  58. }
  59. std::unique_ptr<SCHEMATIC> ReadSchematicFromStream( std::istream& aStream, PROJECT* aProject )
  60. {
  61. STDISTREAM_LINE_READER reader;
  62. reader.SetStream( aStream );
  63. SCH_SEXPR_PARSER parser( &reader );
  64. std::unique_ptr<SCHEMATIC> schematic( new SCHEMATIC( nullptr ) );
  65. try
  66. {
  67. schematic->SetProject( aProject );
  68. SCH_SHEET* newSheet = new SCH_SHEET( schematic.get() );
  69. schematic->SetRoot( newSheet );
  70. SCH_SCREEN* rootScreen = new SCH_SCREEN( schematic.get() );
  71. schematic->Root().SetScreen( rootScreen );
  72. parser.ParseSchematic( newSheet );
  73. }
  74. catch( const IO_ERROR& )
  75. {
  76. }
  77. return schematic;
  78. }
  79. std::unique_ptr<SCHEMATIC> ReadSchematicFromFile( const std::string& aFilename, PROJECT* aProject )
  80. {
  81. std::ifstream file_stream;
  82. file_stream.open( aFilename );
  83. wxASSERT( file_stream.is_open() );
  84. return ReadSchematicFromStream( file_stream, aProject );
  85. }
  86. void LoadSchematic( SETTINGS_MANAGER& aSettingsManager, const wxString& aRelPath,
  87. std::unique_ptr<SCHEMATIC>& aSchematic )
  88. {
  89. if( aSchematic )
  90. {
  91. PROJECT* prj = &aSchematic->Prj();
  92. aSchematic->SetProject( nullptr );
  93. aSettingsManager.UnloadProject( prj, false );
  94. aSchematic->Reset();
  95. }
  96. std::string absPath = getEeschemaTestDataDir() + aRelPath.ToStdString();
  97. wxFileName projectFile( absPath + ".kicad_pro" );
  98. wxFileName legacyProject( absPath + ".pro" );
  99. std::string schematicPath = absPath + ".kicad_sch";
  100. if( projectFile.Exists() )
  101. aSettingsManager.LoadProject( projectFile.GetFullPath() );
  102. else if( legacyProject.Exists() )
  103. aSettingsManager.LoadProject( legacyProject.GetFullPath() );
  104. else
  105. aSettingsManager.LoadProject( "" );
  106. aSettingsManager.Prj().SetElem( PROJECT::ELEM_SCH_SYMBOL_LIBS, nullptr );
  107. aSchematic = ReadSchematicFromFile( schematicPath, &aSettingsManager.Prj() );
  108. aSchematic->CurrentSheet().push_back( &aSchematic->Root() );
  109. SCH_SCREENS screens( aSchematic->Root() );
  110. for( SCH_SCREEN* screen = screens.GetFirst(); screen; screen = screens.GetNext() )
  111. screen->UpdateLocalLibSymbolLinks();
  112. SCH_SHEET_LIST sheets = aSchematic->GetSheets();
  113. // Restore all of the loaded symbol instances from the root sheet screen.
  114. sheets.UpdateSymbolInstances( aSchematic->RootScreen()->GetSymbolInstances() );
  115. sheets.UpdateSheetInstances( aSchematic->RootScreen()->GetSheetInstances() );
  116. sheets.AnnotatePowerSymbols();
  117. // NOTE: This is required for multi-unit symbols to be correct
  118. // Normally called from SCH_EDIT_FRAME::FixupJunctions() but could be refactored
  119. for( SCH_SHEET_PATH& sheet : sheets )
  120. sheet.UpdateAllScreenReferences();
  121. // NOTE: SchematicCleanUp is not called; QA schematics must already be clean or else
  122. // SchematicCleanUp must be freed from its UI dependencies.
  123. aSchematic->ConnectionGraph()->Recalculate( sheets, true );
  124. }
  125. } // namespace KI_TEST