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
47{
48 IO_RELEASER<SCH_IO> pi( SCH_IO_MGR::FindPlugin( SCH_IO_MGR::SCH_EAGLE ) );
49 BOOST_CHECK_NE( pi.get(), nullptr );
50}
51
52
56static wxFileName getEagleTestSchematic( const wxString& sch_file )
57{
58 wxFileName fn( KI_TEST::GetEeschemaTestDataDir() );
59 fn.AppendDir( "io" );
60 fn.AppendDir( "eagle" );
61 fn.SetFullName( sch_file );
62
63 return fn;
64}
65
66
75BOOST_AUTO_TEST_CASE( ImportHierarchy )
76{
77 const wxFileName eagleFn = getEagleTestSchematic( "eagle-import-testfile.sch" );
78 BOOST_REQUIRE( wxFileExists( eagleFn.GetFullPath() ) );
79
80 // Eagle import writes a .kicad_sym library to the project directory. A temp dir
81 // is used so the generated files don't pollute the source tree.
82 wxString tempDir = wxStandardPaths::Get().GetTempDir();
83 wxString projectPath = tempDir + wxFileName::GetPathSeparator() + wxT( "eagle_test.kicad_pro" );
84
85 // Eagle import uses Pgm().GetLibraryManager() for the symbol library adapter, so we
86 // must load the project through the global settings manager and set up project tables.
87 Pgm().GetSettingsManager().LoadProject( projectPath );
89 Pgm().GetLibraryManager().LoadProjectTables( project.GetProjectDirectory() );
90
91 std::unique_ptr<SCHEMATIC> schematic = std::make_unique<SCHEMATIC>( &project );
92 schematic->Reset();
93
94 IO_RELEASER<SCH_IO> pi( SCH_IO_MGR::FindPlugin( SCH_IO_MGR::SCH_EAGLE ) );
95 BOOST_REQUIRE( pi.get() != nullptr );
96
97 SCH_SHEET* loadedSheet = nullptr;
98
99 try
100 {
101 loadedSheet = pi->LoadSchematicFile( eagleFn.GetFullPath(), schematic.get() );
102 }
103 catch( const std::exception& e )
104 {
105 BOOST_FAIL( std::string( "LoadSchematicFile threw: " ) + e.what() );
106 }
107 catch( ... )
108 {
109 BOOST_FAIL( "LoadSchematicFile threw unknown exception" );
110 }
111
112 BOOST_REQUIRE( loadedSheet != nullptr );
113
114 // The returned sheet must be the virtual root (niluuid).
115 BOOST_CHECK( loadedSheet->m_Uuid == niluuid );
116
117 const std::vector<SCH_SHEET*>& topSheets = schematic->GetTopLevelSheets();
118
119 // The test file has exactly 2 Eagle pages; there must be no spurious default sheet.
120 BOOST_CHECK_EQUAL( topSheets.size(), 2 );
121
122 for( SCH_SHEET* sheet : topSheets )
123 {
124 // No top-level sheet should be the virtual root.
125 BOOST_CHECK( sheet != nullptr );
126 BOOST_CHECK( sheet->m_Uuid != niluuid );
127
128 // Every page must have a screen with a non-empty filename so SaveProject can
129 // write it to disk.
130 BOOST_REQUIRE( sheet->GetScreen() != nullptr );
131 BOOST_CHECK( !sheet->GetScreen()->GetFileName().IsEmpty() );
132 }
133
134 // Verify the hierarchy paths match the symbol instance paths so PCB update works.
135 // Each symbol's hierarchical reference must have a path matching one of the top-level
136 // sheet paths in the hierarchy.
137 SCH_SHEET_LIST hierarchy = schematic->BuildSheetListSortedByPageNumbers();
138 BOOST_CHECK_EQUAL( hierarchy.size(), 2 );
139
140 std::set<KIID_PATH> hierPaths;
141
142 for( const SCH_SHEET_PATH& path : hierarchy )
143 hierPaths.insert( path.Path() );
144
145 int totalSymbols = 0;
146 int orphanedSymbols = 0;
147
148 for( const SCH_SHEET_PATH& sheetPath : hierarchy )
149 {
150 SCH_SCREEN* screen = sheetPath.LastScreen();
151
152 if( !screen )
153 continue;
154
155 for( const EDA_ITEM* item : screen->Items().OfType( SCH_SYMBOL_T ) )
156 {
157 const SCH_SYMBOL* sym = static_cast<const SCH_SYMBOL*>( item );
158 ++totalSymbols;
159
160 bool foundMatch = false;
161
162 for( const SCH_SYMBOL_INSTANCE& inst : sym->GetInstances() )
163 {
164 if( hierPaths.count( inst.m_Path ) )
165 {
166 foundMatch = true;
167 break;
168 }
169 }
170
171 if( !foundMatch )
172 ++orphanedSymbols;
173 }
174 }
175
176 BOOST_CHECK_GT( totalSymbols, 0 );
177 BOOST_CHECK_EQUAL( orphanedSymbols, 0 );
178}
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.