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, see <https://www.gnu.org/licenses/>.
20 */
21
23
24#include <map>
25#include <set>
26
27#include <core/ignore.h>
28#include <kiway.h>
29#include <pgm_base.h>
30#include <sch_io/sch_io.h>
31#include <schematic.h>
32#include <sch_sheet.h>
33#include <sch_screen.h>
34#include <sch_symbol.h>
35#include <sch_pin.h>
36#include <sch_label.h>
37#include <sch_sheet_path.h>
40#include <wx/filename.h>
41#include <wx/stdpaths.h>
43
48{
49 IO_RELEASER<SCH_IO> pi( SCH_IO_MGR::FindPlugin( SCH_IO_MGR::SCH_EAGLE ) );
50 BOOST_CHECK_NE( pi.get(), nullptr );
51}
52
53
57static wxFileName getEagleTestSchematic( const wxString& sch_file )
58{
59 wxFileName fn( KI_TEST::GetEeschemaTestDataDir() );
60 fn.AppendDir( "io" );
61 fn.AppendDir( "eagle" );
62 fn.SetFullName( sch_file );
63
64 return fn;
65}
66
67
76static SCH_SHEET* loadEagleSchematic( const wxFileName& aEagleFn,
77 const wxString& aProjectStem,
78 std::unique_ptr<SCHEMATIC>& aSchematic )
79{
80 // Stage the project in a private, freshly-emptied directory. The Eagle importer writes a
81 // "<stem>-eagle-import.kicad_sym" library plus a sym-lib-table into the project directory and
82 // keys duplicate-symbol-name disambiguation off the on-disk library, so leftover files from a
83 // previous run in the shared temp directory would make the imported symbol names
84 // non-deterministic.
85 wxString sep = wxFileName::GetPathSeparator();
86 wxString projectDir = wxStandardPaths::Get().GetTempDir() + sep + aProjectStem
87 + wxT( "-eagle-qa" );
88
89 if( wxDirExists( projectDir ) )
90 wxFileName::Rmdir( projectDir, wxPATH_RMDIR_RECURSIVE );
91
92 wxFileName::Mkdir( projectDir, wxS_DIR_DEFAULT, wxPATH_MKDIR_FULL );
93
94 wxString projectPath = projectDir + sep + aProjectStem + wxT( ".kicad_pro" );
95
96 Pgm().GetSettingsManager().LoadProject( projectPath );
98 Pgm().GetLibraryManager().LoadProjectTables( project.GetProjectDirectory() );
99
100 aSchematic = std::make_unique<SCHEMATIC>( &project );
101 aSchematic->Reset();
102
103 IO_RELEASER<SCH_IO> pi( SCH_IO_MGR::FindPlugin( SCH_IO_MGR::SCH_EAGLE ) );
104 BOOST_REQUIRE( pi.get() != nullptr );
105
106 SCH_SHEET* loadedSheet = nullptr;
107
108 try
109 {
110 loadedSheet = pi->LoadSchematicFile( aEagleFn.GetFullPath(), aSchematic.get() );
111 }
112 catch( const std::exception& e )
113 {
114 BOOST_FAIL( std::string( "LoadSchematicFile threw: " ) + e.what() );
115 }
116 catch( ... )
117 {
118 BOOST_FAIL( "LoadSchematicFile threw unknown exception" );
119 }
120
121 BOOST_REQUIRE( loadedSheet != nullptr );
122 return loadedSheet;
123}
124
125
134BOOST_AUTO_TEST_CASE( ImportHierarchy )
135{
136 const wxFileName eagleFn = getEagleTestSchematic( "eagle-import-testfile.sch" );
137 BOOST_REQUIRE( wxFileExists( eagleFn.GetFullPath() ) );
138
139 std::unique_ptr<SCHEMATIC> schematic;
140 SCH_SHEET* loadedSheet = loadEagleSchematic( eagleFn, wxS( "eagle_test" ), schematic );
141
142 // The returned sheet must be the virtual root (niluuid).
143 BOOST_CHECK( loadedSheet->m_Uuid == niluuid );
144
145 const std::vector<SCH_SHEET*>& topSheets = schematic->GetTopLevelSheets();
146
147 // The test file has exactly 2 Eagle pages; there must be no spurious default sheet.
148 BOOST_CHECK_EQUAL( topSheets.size(), 2 );
149
150 for( SCH_SHEET* sheet : topSheets )
151 {
152 // No top-level sheet should be the virtual root.
153 BOOST_CHECK( sheet != nullptr );
154 BOOST_CHECK( sheet->m_Uuid != niluuid );
155
156 // Every page must have a screen with a non-empty filename so SaveProject can
157 // write it to disk.
158 BOOST_REQUIRE( sheet->GetScreen() != nullptr );
159 BOOST_CHECK( !sheet->GetScreen()->GetFileName().IsEmpty() );
160 }
161
162 // Verify the hierarchy paths match the symbol instance paths so PCB update works.
163 // Each symbol's hierarchical reference must have a path matching one of the top-level
164 // sheet paths in the hierarchy.
165 SCH_SHEET_LIST hierarchy = schematic->BuildSheetListSortedByPageNumbers();
166 BOOST_CHECK_EQUAL( hierarchy.size(), 2 );
167
168 std::set<KIID_PATH> hierPaths;
169
170 for( const SCH_SHEET_PATH& path : hierarchy )
171 hierPaths.insert( path.Path() );
172
173 int totalSymbols = 0;
174 int orphanedSymbols = 0;
175
176 for( const SCH_SHEET_PATH& sheetPath : hierarchy )
177 {
178 SCH_SCREEN* screen = sheetPath.LastScreen();
179
180 if( !screen )
181 continue;
182
183 for( const EDA_ITEM* item : screen->Items().OfType( SCH_SYMBOL_T ) )
184 {
185 const SCH_SYMBOL* sym = static_cast<const SCH_SYMBOL*>( item );
186 ++totalSymbols;
187
188 bool foundMatch = false;
189
190 for( const SCH_SYMBOL_INSTANCE& inst : sym->GetInstances() )
191 {
192 if( hierPaths.count( inst.m_Path ) )
193 {
194 foundMatch = true;
195 break;
196 }
197 }
198
199 if( !foundMatch )
200 ++orphanedSymbols;
201 }
202 }
203
204 BOOST_CHECK_GT( totalSymbols, 0 );
205 BOOST_CHECK_EQUAL( orphanedSymbols, 0 );
206}
207
208
222BOOST_AUTO_TEST_CASE( LabelsRemainGlobalForFlatNamespace )
223{
224 const wxFileName eagleFn = getEagleTestSchematic( "eagle-import-testfile.sch" );
225 BOOST_REQUIRE( wxFileExists( eagleFn.GetFullPath() ) );
226
227 std::unique_ptr<SCHEMATIC> schematic;
228 loadEagleSchematic( eagleFn, wxS( "eagle_label_kind" ), schematic );
229
230 // Bucket every label in the imported schematic by kind + text so we can verify that
231 // each named Eagle net produced a global label and no leftover local SCH_LABEL exists
232 // with that name. Bus labels (e.g. "A[1..3]") must stay local.
233 SCH_SHEET_LIST hierarchy = schematic->BuildSheetListSortedByPageNumbers();
234
235 std::unordered_set<wxString> globalTexts;
236 std::unordered_set<wxString> localTexts;
237
238 for( const SCH_SHEET_PATH& sheetPath : hierarchy )
239 {
240 SCH_SCREEN* screen = sheetPath.LastScreen();
241
242 if( !screen )
243 continue;
244
245 for( SCH_ITEM* item : screen->Items().OfType( SCH_GLOBAL_LABEL_T ) )
246 globalTexts.insert( static_cast<SCH_LABEL_BASE*>( item )->GetText() );
247
248 for( SCH_ITEM* item : screen->Items().OfType( SCH_LABEL_T ) )
249 localTexts.insert( static_cast<SCH_LABEL_BASE*>( item )->GetText() );
250 }
251
252 // Named Eagle nets must produce at least one global label each, and none of these
253 // names may appear as a local SCH_LABEL — that would reintroduce the "/X" sheet-path
254 // prefix that broke board update.
255 const std::vector<wxString> expectedGlobalNets = {
256 wxS( "A1" ), wxS( "A2" ), wxS( "A3" ),
257 wxS( "B1" ), wxS( "B2" ), wxS( "B3" )
258 };
259
260 for( const wxString& netName : expectedGlobalNets )
261 {
262 BOOST_CHECK_MESSAGE( globalTexts.count( netName ) > 0,
263 "Missing global Eagle net label '" << netName.ToStdString()
264 << "' — flat namespace requires SCH_GLOBALLABEL." );
265 BOOST_CHECK_MESSAGE( localTexts.count( netName ) == 0,
266 "Found local SCH_LABEL '" << netName.ToStdString()
267 << "' — Eagle net should be global." );
268 }
269
270 // Bus labels (e.g. "A[1..3]") must stay local: Eagle buses are visual groupings, not
271 // electrical signals, so globalizing them would merge same-named buses project-wide.
272 const std::vector<wxString> expectedLocalBuses = {
273 wxS( "A[1..3]" ), wxS( "B[1..3]" )
274 };
275
276 for( const wxString& busName : expectedLocalBuses )
277 {
278 BOOST_CHECK_MESSAGE( globalTexts.count( busName ) == 0,
279 "Found global SCH_GLOBALLABEL '" << busName.ToStdString()
280 << "' — Eagle bus should remain a local SCH_LABEL." );
281 }
282}
283
284
295BOOST_AUTO_TEST_CASE( PinNameTagStripped )
296{
297 const wxFileName eagleFn = getEagleTestSchematic( "issue24483_pin_tag.sch" );
298 BOOST_REQUIRE( wxFileExists( eagleFn.GetFullPath() ) );
299
300 std::unique_ptr<SCHEMATIC> schematic;
301 loadEagleSchematic( eagleFn, wxS( "eagle_pin_tag" ), schematic );
302
303 SCH_SHEET_LIST hierarchy = schematic->BuildSheetListSortedByPageNumbers();
304
305 // Key symbols by their library item name (e.g. "MYDEV"); the annotated reference is
306 // unreliable here because the no-connect device is annotated with a '#' power prefix. A
307 // single Eagle library imports without a library-name prefix on the symbol; the prefix is
308 // only added to disambiguate duplicate names across multiple Eagle libraries.
309 std::map<wxString, const SCH_SYMBOL*> symbolByLibItem;
310
311 for( const SCH_SHEET_PATH& sheetPath : hierarchy )
312 {
313 SCH_SCREEN* screen = sheetPath.LastScreen();
314
315 if( !screen )
316 continue;
317
318 for( const EDA_ITEM* item : screen->Items().OfType( SCH_SYMBOL_T ) )
319 {
320 const SCH_SYMBOL* sym = static_cast<const SCH_SYMBOL*>( item );
321 symbolByLibItem[sym->GetLibId().GetLibItemName().wx_str()] = sym;
322 }
323 }
324
325 // The connected device maps pins to pads through <connect>; the no-connect device has no
326 // <connect> so pins are auto-numbered. Both paths must strip the tag from the display name.
327 BOOST_REQUIRE( symbolByLibItem.count( wxS( "MYDEV" ) ) == 1 );
328 BOOST_REQUIRE( symbolByLibItem.count( wxS( "MYDEV_NC" ) ) == 1 );
329
330 auto collectNames = []( const SCH_SYMBOL* aSym )
331 {
332 std::multiset<wxString> names;
333
334 for( const SCH_PIN* pin : aSym->GetAllLibPins() )
335 names.insert( pin->GetName() );
336
337 return names;
338 };
339
340 // No display name on either symbol may retain the "@<tag>" suffix.
341 for( const auto& [libItem, sym] : symbolByLibItem )
342 {
343 for( const SCH_PIN* pin : sym->GetAllLibPins() )
344 {
345 BOOST_CHECK_MESSAGE( pin->GetName().Find( '@' ) == wxNOT_FOUND,
346 libItem.ToStdString() << " pin kept Eagle tag '"
347 << pin->GetName().ToStdString() << "'." );
348 }
349 }
350
351 // For the connected device, the two "IN@n" pins must both display as "IN" yet keep their
352 // distinct pad mapping, and "NC@3" must strip to "NC".
353 std::map<wxString, wxString> nameByPad;
354
355 for( const SCH_PIN* pin : symbolByLibItem[wxS( "MYDEV" )]->GetAllLibPins() )
356 nameByPad[pin->GetNumber()] = pin->GetName();
357
358 BOOST_CHECK_EQUAL( nameByPad["1"], wxString( wxS( "IN" ) ) );
359 BOOST_CHECK_EQUAL( nameByPad["2"], wxString( wxS( "IN" ) ) );
360 BOOST_CHECK_EQUAL( nameByPad["3"], wxString( wxS( "GND" ) ) );
361 BOOST_CHECK_EQUAL( nameByPad["4"], wxString( wxS( "NC" ) ) );
362
363 // The no-connect device keeps every pin, so both tagged "IN" pins survive the strip.
364 std::multiset<wxString> ncNames = collectNames( symbolByLibItem[wxS( "MYDEV_NC" )] );
365 BOOST_CHECK_EQUAL( ncNames.count( wxS( "IN" ) ), 2 );
366 BOOST_CHECK_EQUAL( ncNames.count( wxS( "GND" ) ), 1 );
367 BOOST_CHECK_EQUAL( ncNames.count( wxS( "NC" ) ), 1 );
368}
A base class for most all the KiCad significant classes used in schematics and boards.
Definition eda_item.h:96
const KIID m_Uuid
Definition eda_item.h:531
EE_TYPE OfType(KICAD_T aType) const
Definition sch_rtree.h:221
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
const UTF8 & GetLibItemName() const
Definition lib_id.h:98
virtual SETTINGS_MANAGER & GetSettingsManager() const
Definition pgm_base.h:124
virtual LIBRARY_MANAGER & GetLibraryManager() const
Definition pgm_base.h:126
Container for project specific data.
Definition project.h:62
Base class for any item which can be embedded within the SCHEMATIC container class,...
Definition sch_item.h:162
EE_RTREE & Items()
Get the full RTree, usually for iterating.
Definition sch_screen.h:115
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:44
Schematic symbol object.
Definition sch_symbol.h:69
const std::vector< SCH_SYMBOL_INSTANCE > & GetInstances() const
Definition sch_symbol.h:128
const LIB_ID & GetLibId() const override
Definition sch_symbol.h:158
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.
wxString wx_str() const
Definition utf8.cpp:41
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.
static SCH_SHEET * loadEagleSchematic(const wxFileName &aEagleFn, const wxString &aProjectStem, std::unique_ptr< SCHEMATIC > &aSchematic)
Common Eagle-import scaffolding: stage a fresh project under tempDir, configure the library manager,...
BOOST_REQUIRE(intersection.has_value()==c.ExpectedIntersection.has_value())
std::string path
KIBIS_PIN * pin
BOOST_CHECK_MESSAGE(totalMismatches==0, std::to_string(totalMismatches)+" board(s) with strategy disagreements")
BOOST_CHECK_EQUAL(result, "25.4")
@ SCH_SYMBOL_T
Definition typeinfo.h:169
@ SCH_LABEL_T
Definition typeinfo.h:164
@ SCH_GLOBAL_LABEL_T
Definition typeinfo.h:165
Definition of file extensions used in Kicad.