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 <map>
29#include <set>
30
31#include <core/ignore.h>
32#include <kiway.h>
33#include <pgm_base.h>
34#include <sch_io/sch_io.h>
35#include <schematic.h>
36#include <sch_sheet.h>
37#include <sch_screen.h>
38#include <sch_symbol.h>
39#include <sch_pin.h>
40#include <sch_label.h>
41#include <sch_sheet_path.h>
44#include <wx/filename.h>
45#include <wx/stdpaths.h>
47
52{
53 IO_RELEASER<SCH_IO> pi( SCH_IO_MGR::FindPlugin( SCH_IO_MGR::SCH_EAGLE ) );
54 BOOST_CHECK_NE( pi.get(), nullptr );
55}
56
57
61static wxFileName getEagleTestSchematic( const wxString& sch_file )
62{
63 wxFileName fn( KI_TEST::GetEeschemaTestDataDir() );
64 fn.AppendDir( "io" );
65 fn.AppendDir( "eagle" );
66 fn.SetFullName( sch_file );
67
68 return fn;
69}
70
71
80static SCH_SHEET* loadEagleSchematic( const wxFileName& aEagleFn,
81 const wxString& aProjectStem,
82 std::unique_ptr<SCHEMATIC>& aSchematic )
83{
84 wxString tempDir = wxStandardPaths::Get().GetTempDir();
85 wxString projectPath = tempDir + wxFileName::GetPathSeparator() + aProjectStem
86 + wxT( ".kicad_pro" );
87
88 Pgm().GetSettingsManager().LoadProject( projectPath );
90 Pgm().GetLibraryManager().LoadProjectTables( project.GetProjectDirectory() );
91
92 aSchematic = std::make_unique<SCHEMATIC>( &project );
93 aSchematic->Reset();
94
95 IO_RELEASER<SCH_IO> pi( SCH_IO_MGR::FindPlugin( SCH_IO_MGR::SCH_EAGLE ) );
96 BOOST_REQUIRE( pi.get() != nullptr );
97
98 SCH_SHEET* loadedSheet = nullptr;
99
100 try
101 {
102 loadedSheet = pi->LoadSchematicFile( aEagleFn.GetFullPath(), aSchematic.get() );
103 }
104 catch( const std::exception& e )
105 {
106 BOOST_FAIL( std::string( "LoadSchematicFile threw: " ) + e.what() );
107 }
108 catch( ... )
109 {
110 BOOST_FAIL( "LoadSchematicFile threw unknown exception" );
111 }
112
113 BOOST_REQUIRE( loadedSheet != nullptr );
114 return loadedSheet;
115}
116
117
126BOOST_AUTO_TEST_CASE( ImportHierarchy )
127{
128 const wxFileName eagleFn = getEagleTestSchematic( "eagle-import-testfile.sch" );
129 BOOST_REQUIRE( wxFileExists( eagleFn.GetFullPath() ) );
130
131 std::unique_ptr<SCHEMATIC> schematic;
132 SCH_SHEET* loadedSheet = loadEagleSchematic( eagleFn, wxS( "eagle_test" ), schematic );
133
134 // The returned sheet must be the virtual root (niluuid).
135 BOOST_CHECK( loadedSheet->m_Uuid == niluuid );
136
137 const std::vector<SCH_SHEET*>& topSheets = schematic->GetTopLevelSheets();
138
139 // The test file has exactly 2 Eagle pages; there must be no spurious default sheet.
140 BOOST_CHECK_EQUAL( topSheets.size(), 2 );
141
142 for( SCH_SHEET* sheet : topSheets )
143 {
144 // No top-level sheet should be the virtual root.
145 BOOST_CHECK( sheet != nullptr );
146 BOOST_CHECK( sheet->m_Uuid != niluuid );
147
148 // Every page must have a screen with a non-empty filename so SaveProject can
149 // write it to disk.
150 BOOST_REQUIRE( sheet->GetScreen() != nullptr );
151 BOOST_CHECK( !sheet->GetScreen()->GetFileName().IsEmpty() );
152 }
153
154 // Verify the hierarchy paths match the symbol instance paths so PCB update works.
155 // Each symbol's hierarchical reference must have a path matching one of the top-level
156 // sheet paths in the hierarchy.
157 SCH_SHEET_LIST hierarchy = schematic->BuildSheetListSortedByPageNumbers();
158 BOOST_CHECK_EQUAL( hierarchy.size(), 2 );
159
160 std::set<KIID_PATH> hierPaths;
161
162 for( const SCH_SHEET_PATH& path : hierarchy )
163 hierPaths.insert( path.Path() );
164
165 int totalSymbols = 0;
166 int orphanedSymbols = 0;
167
168 for( const SCH_SHEET_PATH& sheetPath : hierarchy )
169 {
170 SCH_SCREEN* screen = sheetPath.LastScreen();
171
172 if( !screen )
173 continue;
174
175 for( const EDA_ITEM* item : screen->Items().OfType( SCH_SYMBOL_T ) )
176 {
177 const SCH_SYMBOL* sym = static_cast<const SCH_SYMBOL*>( item );
178 ++totalSymbols;
179
180 bool foundMatch = false;
181
182 for( const SCH_SYMBOL_INSTANCE& inst : sym->GetInstances() )
183 {
184 if( hierPaths.count( inst.m_Path ) )
185 {
186 foundMatch = true;
187 break;
188 }
189 }
190
191 if( !foundMatch )
192 ++orphanedSymbols;
193 }
194 }
195
196 BOOST_CHECK_GT( totalSymbols, 0 );
197 BOOST_CHECK_EQUAL( orphanedSymbols, 0 );
198}
199
200
214BOOST_AUTO_TEST_CASE( LabelsRemainGlobalForFlatNamespace )
215{
216 const wxFileName eagleFn = getEagleTestSchematic( "eagle-import-testfile.sch" );
217 BOOST_REQUIRE( wxFileExists( eagleFn.GetFullPath() ) );
218
219 std::unique_ptr<SCHEMATIC> schematic;
220 loadEagleSchematic( eagleFn, wxS( "eagle_label_kind" ), schematic );
221
222 // Bucket every label in the imported schematic by kind + text so we can verify that
223 // each named Eagle net produced a global label and no leftover local SCH_LABEL exists
224 // with that name. Bus labels (e.g. "A[1..3]") must stay local.
225 SCH_SHEET_LIST hierarchy = schematic->BuildSheetListSortedByPageNumbers();
226
227 std::unordered_set<wxString> globalTexts;
228 std::unordered_set<wxString> localTexts;
229
230 for( const SCH_SHEET_PATH& sheetPath : hierarchy )
231 {
232 SCH_SCREEN* screen = sheetPath.LastScreen();
233
234 if( !screen )
235 continue;
236
237 for( SCH_ITEM* item : screen->Items().OfType( SCH_GLOBAL_LABEL_T ) )
238 globalTexts.insert( static_cast<SCH_LABEL_BASE*>( item )->GetText() );
239
240 for( SCH_ITEM* item : screen->Items().OfType( SCH_LABEL_T ) )
241 localTexts.insert( static_cast<SCH_LABEL_BASE*>( item )->GetText() );
242 }
243
244 // Named Eagle nets must produce at least one global label each, and none of these
245 // names may appear as a local SCH_LABEL — that would reintroduce the "/X" sheet-path
246 // prefix that broke board update.
247 const std::vector<wxString> expectedGlobalNets = {
248 wxS( "A1" ), wxS( "A2" ), wxS( "A3" ),
249 wxS( "B1" ), wxS( "B2" ), wxS( "B3" )
250 };
251
252 for( const wxString& netName : expectedGlobalNets )
253 {
254 BOOST_CHECK_MESSAGE( globalTexts.count( netName ) > 0,
255 "Missing global Eagle net label '" << netName.ToStdString()
256 << "' — flat namespace requires SCH_GLOBALLABEL." );
257 BOOST_CHECK_MESSAGE( localTexts.count( netName ) == 0,
258 "Found local SCH_LABEL '" << netName.ToStdString()
259 << "' — Eagle net should be global." );
260 }
261
262 // Bus labels (e.g. "A[1..3]") must stay local: Eagle buses are visual groupings, not
263 // electrical signals, so globalizing them would merge same-named buses project-wide.
264 const std::vector<wxString> expectedLocalBuses = {
265 wxS( "A[1..3]" ), wxS( "B[1..3]" )
266 };
267
268 for( const wxString& busName : expectedLocalBuses )
269 {
270 BOOST_CHECK_MESSAGE( globalTexts.count( busName ) == 0,
271 "Found global SCH_GLOBALLABEL '" << busName.ToStdString()
272 << "' — Eagle bus should remain a local SCH_LABEL." );
273 }
274}
275
276
287BOOST_AUTO_TEST_CASE( PinNameTagStripped )
288{
289 const wxFileName eagleFn = getEagleTestSchematic( "issue24483_pin_tag.sch" );
290 BOOST_REQUIRE( wxFileExists( eagleFn.GetFullPath() ) );
291
292 std::unique_ptr<SCHEMATIC> schematic;
293 loadEagleSchematic( eagleFn, wxS( "eagle_pin_tag" ), schematic );
294
295 SCH_SHEET_LIST hierarchy = schematic->BuildSheetListSortedByPageNumbers();
296
297 // Key symbols by their library item name (e.g. "tagtest_MYDEV"); the annotated reference
298 // is unreliable here because the no-connect device is annotated with a '#' power prefix.
299 std::map<wxString, const SCH_SYMBOL*> symbolByLibItem;
300
301 for( const SCH_SHEET_PATH& sheetPath : hierarchy )
302 {
303 SCH_SCREEN* screen = sheetPath.LastScreen();
304
305 if( !screen )
306 continue;
307
308 for( const EDA_ITEM* item : screen->Items().OfType( SCH_SYMBOL_T ) )
309 {
310 const SCH_SYMBOL* sym = static_cast<const SCH_SYMBOL*>( item );
311 symbolByLibItem[sym->GetLibId().GetLibItemName().wx_str()] = sym;
312 }
313 }
314
315 // The connected device maps pins to pads through <connect>; the no-connect device has no
316 // <connect> so pins are auto-numbered. Both paths must strip the tag from the display name.
317 BOOST_REQUIRE( symbolByLibItem.count( wxS( "tagtest_MYDEV" ) ) == 1 );
318 BOOST_REQUIRE( symbolByLibItem.count( wxS( "tagtest_MYDEV_NC" ) ) == 1 );
319
320 auto collectNames = []( const SCH_SYMBOL* aSym )
321 {
322 std::multiset<wxString> names;
323
324 for( const SCH_PIN* pin : aSym->GetAllLibPins() )
325 names.insert( pin->GetName() );
326
327 return names;
328 };
329
330 // No display name on either symbol may retain the "@<tag>" suffix.
331 for( const auto& [libItem, sym] : symbolByLibItem )
332 {
333 for( const SCH_PIN* pin : sym->GetAllLibPins() )
334 {
335 BOOST_CHECK_MESSAGE( pin->GetName().Find( '@' ) == wxNOT_FOUND,
336 libItem.ToStdString() << " pin kept Eagle tag '"
337 << pin->GetName().ToStdString() << "'." );
338 }
339 }
340
341 // For the connected device, the two "IN@n" pins must both display as "IN" yet keep their
342 // distinct pad mapping, and "NC@3" must strip to "NC".
343 std::map<wxString, wxString> nameByPad;
344
345 for( const SCH_PIN* pin : symbolByLibItem[wxS( "tagtest_MYDEV" )]->GetAllLibPins() )
346 nameByPad[pin->GetNumber()] = pin->GetName();
347
348 BOOST_CHECK_EQUAL( nameByPad["1"], wxString( wxS( "IN" ) ) );
349 BOOST_CHECK_EQUAL( nameByPad["2"], wxString( wxS( "IN" ) ) );
350 BOOST_CHECK_EQUAL( nameByPad["3"], wxString( wxS( "GND" ) ) );
351 BOOST_CHECK_EQUAL( nameByPad["4"], wxString( wxS( "NC" ) ) );
352
353 // The no-connect device keeps every pin, so both tagged "IN" pins survive the strip.
354 std::multiset<wxString> ncNames = collectNames( symbolByLibItem[wxS( "tagtest_MYDEV_NC" )] );
355 BOOST_CHECK_EQUAL( ncNames.count( wxS( "IN" ) ), 2 );
356 BOOST_CHECK_EQUAL( ncNames.count( wxS( "GND" ) ), 1 );
357 BOOST_CHECK_EQUAL( ncNames.count( wxS( "NC" ) ), 1 );
358}
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:535
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
const UTF8 & GetLibItemName() const
Definition lib_id.h:102
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
Base class for any item which can be embedded within the SCHEMATIC container class,...
Definition sch_item.h:166
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:73
const std::vector< SCH_SYMBOL_INSTANCE > & GetInstances() const
Definition sch_symbol.h:132
const LIB_ID & GetLibId() const override
Definition sch_symbol.h:162
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:45
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:173
@ SCH_LABEL_T
Definition typeinfo.h:168
@ SCH_GLOBAL_LABEL_T
Definition typeinfo.h:169
Definition of file extensions used in Kicad.