KiCad PCB EDA Suite
eeschema_test_utils.cpp
Go to the documentation of this file.
1 /*
2  * This program source code file is part of KiCad, a free EDA CAD application.
3  *
4  * Copyright (C) 2019-2020 KiCad Developers, see CHANGELOG.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 2
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 here:
18  * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
19  * or you may search the http://www.gnu.org website for the version 2 license,
20  * or you may write to the Free Software Foundation, Inc.,
21  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
22  */
23 
24 #include "eeschema_test_utils.h"
25 
26 #include <cstdlib>
27 #include <memory>
28 
29 #include <eeschema/sch_io_mgr.h>
30 #include <eeschema/sch_screen.h>
31 #include <eeschema/schematic.h>
33 
34 
35 #ifndef QA_EESCHEMA_DATA_LOCATION
36  #define QA_EESCHEMA_DATA_LOCATION "???"
37 #endif
38 
40 {
41  const char* env = std::getenv( "KICAD_TEST_EESCHEMA_DATA_DIR" );
42  wxString fn;
43 
44  if( !env )
45  {
46  // Use the compiled-in location of the data dir
47  // (i.e. where the files were at build time)
49  }
50  else
51  {
52  // Use whatever was given in the env var
53  fn << env;
54  }
55 
56  // Ensure the string ends in / to force a directory interpretation
57  fn << "/";
58 
59  return wxFileName{ fn };
60 }
61 
62 
63 std::unique_ptr<SCHEMATIC> ReadSchematicFromFile( const std::string& aFilename )
64 {
65  auto pi = SCH_IO_MGR::FindPlugin( SCH_IO_MGR::SCH_KICAD );
66  std::unique_ptr<SCHEMATIC> schematic = std::make_unique<SCHEMATIC>( nullptr );
67 
68  schematic->Reset();
69  schematic->SetRoot( pi->Load( aFilename, schematic.get() ) );
70  schematic->CurrentSheet().push_back( &schematic->Root() );
71 
72  SCH_SCREENS screens( schematic->Root() );
73 
74  for( SCH_SCREEN* screen = screens.GetFirst(); screen; screen = screens.GetNext() )
75  screen->UpdateLocalLibSymbolLinks();
76 
77  SCH_SHEET_LIST sheets = schematic->GetSheets();
78 
79  // Restore all of the loaded symbol instances from the root sheet screen.
80  sheets.UpdateSymbolInstances( schematic->RootScreen()->GetSymbolInstances() );
81 
82  sheets.AnnotatePowerSymbols();
83 
84  // NOTE: This is required for multi-unit symbols to be correct
85  // Normally called from SCH_EDIT_FRAME::FixupJunctions() but could be refactored
86  for( SCH_SHEET_PATH& sheet : sheets )
87  sheet.UpdateAllScreenReferences();
88 
89  // NOTE: SchematicCleanUp is not called; QA schematics must already be clean or else
90  // SchematicCleanUp must be freed from its UI dependencies.
91 
92  schematic->ConnectionGraph()->Recalculate( sheets, true );
93 
94  return schematic;
95 }
A container for handling SCH_SHEET_PATH objects in a flattened hierarchy.
#define QA_EESCHEMA_DATA_LOCATION
std::unique_ptr< SCHEMATIC > ReadSchematicFromFile(const std::string &aFilename)
Handle access to a stack of flattened SCH_SHEET objects by way of a path for creating a flattened sch...
void UpdateSymbolInstances(const std::vector< SYMBOL_INSTANCE_REFERENCE > &aSymbolInstances)
Update all of the symbol instance information using aSymbolInstances.
wxFileName GetEeschemaTestDataDir()
Get the configured location of Eeschema test data.
void AnnotatePowerSymbols()
Silently annotate the not yet annotated power symbols of the entire hierarchy of the sheet path list.
Container class that holds multiple SCH_SCREEN objects in a hierarchy.
Definition: sch_screen.h:549