KiCad PCB EDA Suite
Loading...
Searching...
No Matches
test_eagle_plugin.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) 2017 CERN
5 * Copyright The KiCad Developers, see AUTHORS.txt for contributors.
6 * @author Alejandro García Montoro <[email protected]>
7 *
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License
10 * as published by the Free Software Foundation; either version 2
11 * of the License, or (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, you may find one here:
20 * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
21 * or you may search the http://www.gnu.org website for the version 2 license,
22 * or you may write to the Free Software Foundation, Inc.,
23 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
24 */
25
27
28#include <core/ignore.h>
29#include <kiway.h>
30#include <pgm_base.h>
31#include <sch_io/sch_io.h>
32#include <schematic.h>
33#include <sch_sheet.h>
34#include <sch_screen.h>
35#include <sch_symbol.h>
36#include <sch_sheet_path.h>
39#include <wx/filename.h>
40#include <wx/stdpaths.h>
42
43#include "eeschema_test_utils.h"
44
49{
50 IO_RELEASER<SCH_IO> pi( SCH_IO_MGR::FindPlugin( SCH_IO_MGR::SCH_EAGLE ) );
51 BOOST_CHECK_NE( pi.get(), nullptr );
52}
53
54
58static wxFileName getEagleTestSchematic( const wxString& sch_file )
59{
60 wxFileName fn( KI_TEST::GetEeschemaTestDataDir() );
61 fn.AppendDir( "io" );
62 fn.AppendDir( "eagle" );
63 fn.SetFullName( sch_file );
64
65 return fn;
66}
67
68
77BOOST_AUTO_TEST_CASE( ImportHierarchy )
78{
79 const wxFileName eagleFn = getEagleTestSchematic( "eagle-import-testfile.sch" );
80 BOOST_REQUIRE( wxFileExists( eagleFn.GetFullPath() ) );
81
82 // Eagle import writes a .kicad_sym library to the project directory. A temp dir
83 // is used so the generated files don't pollute the source tree.
84 wxString tempDir = wxStandardPaths::Get().GetTempDir();
85 wxString projectPath = tempDir + wxFileName::GetPathSeparator() + wxT( "eagle_test.kicad_pro" );
86
87 // Eagle import uses Pgm().GetLibraryManager() for the symbol library adapter, so we
88 // must load the project through the global settings manager and set up project tables.
89 Pgm().GetSettingsManager().LoadProject( projectPath );
91 Pgm().GetLibraryManager().LoadProjectTables( project.GetProjectDirectory() );
92
93 std::unique_ptr<SCHEMATIC> schematic = std::make_unique<SCHEMATIC>( &project );
94 schematic->Reset();
95
96 IO_RELEASER<SCH_IO> pi( SCH_IO_MGR::FindPlugin( SCH_IO_MGR::SCH_EAGLE ) );
97 BOOST_REQUIRE( pi.get() != nullptr );
98
99 SCH_SHEET* loadedSheet = nullptr;
100
101 try
102 {
103 loadedSheet = pi->LoadSchematicFile( eagleFn.GetFullPath(), schematic.get() );
104 }
105 catch( const std::exception& e )
106 {
107 BOOST_FAIL( std::string( "LoadSchematicFile threw: " ) + e.what() );
108 }
109 catch( ... )
110 {
111 BOOST_FAIL( "LoadSchematicFile threw unknown exception" );
112 }
113
114 BOOST_REQUIRE( loadedSheet != nullptr );
115
116 // The returned sheet must be the virtual root (niluuid).
117 BOOST_CHECK( loadedSheet->m_Uuid == niluuid );
118
119 const std::vector<SCH_SHEET*>& topSheets = schematic->GetTopLevelSheets();
120
121 // The test file has exactly 2 Eagle pages; there must be no spurious default sheet.
122 BOOST_CHECK_EQUAL( topSheets.size(), 2 );
123
124 for( SCH_SHEET* sheet : topSheets )
125 {
126 // No top-level sheet should be the virtual root.
127 BOOST_CHECK( sheet != nullptr );
128 BOOST_CHECK( sheet->m_Uuid != niluuid );
129
130 // Every page must have a screen with a non-empty filename so SaveProject can
131 // write it to disk.
132 BOOST_REQUIRE( sheet->GetScreen() != nullptr );
133 BOOST_CHECK( !sheet->GetScreen()->GetFileName().IsEmpty() );
134 }
135
136 // Verify the hierarchy paths match the symbol instance paths so PCB update works.
137 // Each symbol's hierarchical reference must have a path matching one of the top-level
138 // sheet paths in the hierarchy.
139 SCH_SHEET_LIST hierarchy = schematic->BuildSheetListSortedByPageNumbers();
140 BOOST_CHECK_EQUAL( hierarchy.size(), 2 );
141
142 std::set<KIID_PATH> hierPaths;
143
144 for( const SCH_SHEET_PATH& path : hierarchy )
145 hierPaths.insert( path.Path() );
146
147 int totalSymbols = 0;
148 int orphanedSymbols = 0;
149
150 for( const SCH_SHEET_PATH& sheetPath : hierarchy )
151 {
152 SCH_SCREEN* screen = sheetPath.LastScreen();
153
154 if( !screen )
155 continue;
156
157 for( const EDA_ITEM* item : screen->Items().OfType( SCH_SYMBOL_T ) )
158 {
159 const SCH_SYMBOL* sym = static_cast<const SCH_SYMBOL*>( item );
160 ++totalSymbols;
161
162 bool foundMatch = false;
163
164 for( const SCH_SYMBOL_INSTANCE& inst : sym->GetInstances() )
165 {
166 if( hierPaths.count( inst.m_Path ) )
167 {
168 foundMatch = true;
169 break;
170 }
171 }
172
173 if( !foundMatch )
174 ++orphanedSymbols;
175 }
176 }
177
178 BOOST_CHECK_GT( totalSymbols, 0 );
179 BOOST_CHECK_EQUAL( orphanedSymbols, 0 );
180}
A base class for most all the KiCad significant classes used in schematics and boards.
Definition eda_item.h:100
const KIID m_Uuid
Definition eda_item.h:528
EE_TYPE OfType(KICAD_T aType) const
Definition sch_rtree.h:225
void LoadProjectTables(std::initializer_list< LIBRARY_TABLE_TYPE > aTablesToLoad={})
(Re)loads the project library tables in the given list, or all tables if no list is given
virtual SETTINGS_MANAGER & GetSettingsManager() const
Definition pgm_base.h:130
virtual LIBRARY_MANAGER & GetLibraryManager() const
Definition pgm_base.h:132
Container for project specific data.
Definition project.h:66
EE_RTREE & Items()
Get the full RTree, usually for iterating.
Definition sch_screen.h:119
A container for handling SCH_SHEET_PATH objects in a flattened hierarchy.
Handle access to a stack of flattened SCH_SHEET objects by way of a path for creating a flattened sch...
Sheet symbol placed in a schematic, and is the entry point for a sub schematic.
Definition sch_sheet.h:48
Schematic symbol object.
Definition sch_symbol.h:76
const std::vector< SCH_SYMBOL_INSTANCE > & GetInstances() const
Definition sch_symbol.h:135
bool LoadProject(const wxString &aFullPath, bool aSetActive=true)
Load a project or sets up a new project with a specified path.
PROJECT & Prj() const
A helper while we are not MDI-capable – return the one and only project.
std::unique_ptr< T > IO_RELEASER
Helper to hold and release an IO_BASE object when exceptions are thrown.
Definition io_mgr.h:33
KIID niluuid(0)
std::string GetEeschemaTestDataDir()
Get the configured location of Eeschema test data.
PGM_BASE & Pgm()
The global program "get" accessor.
see class PGM_BASE
Definition of the SCH_SHEET_PATH and SCH_SHEET_LIST classes for Eeschema.
A simple container for schematic symbol instance information.
BOOST_AUTO_TEST_CASE(FindPlugin)
Checks that the SCH_IO manager finds the Eagle plugin.
static wxFileName getEagleTestSchematic(const wxString &sch_file)
Get a schematic file from the test data eagle subdir.
BOOST_REQUIRE(intersection.has_value()==c.ExpectedIntersection.has_value())
std::string path
BOOST_CHECK_EQUAL(result, "25.4")
@ SCH_SYMBOL_T
Definition typeinfo.h:173
Definition of file extensions used in Kicad.