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_line.h>
38#include <sch_sheet_path.h>
41#include <wx/filename.h>
42#include <wx/stdpaths.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
77static SCH_SHEET* loadEagleSchematic( const wxFileName& aEagleFn,
78 const wxString& aProjectStem,
79 std::unique_ptr<SCHEMATIC>& aSchematic )
80{
81 // Stage the project in a private, freshly-emptied directory. The Eagle importer writes a
82 // "<stem>-eagle-import.kicad_sym" library plus a sym-lib-table into the project directory and
83 // keys duplicate-symbol-name disambiguation off the on-disk library, so leftover files from a
84 // previous run in the shared temp directory would make the imported symbol names
85 // non-deterministic.
86 wxString sep = wxFileName::GetPathSeparator();
87 wxString projectDir = wxStandardPaths::Get().GetTempDir() + sep + aProjectStem
88 + wxT( "-eagle-qa" );
89
90 if( wxDirExists( projectDir ) )
91 wxFileName::Rmdir( projectDir, wxPATH_RMDIR_RECURSIVE );
92
93 wxFileName::Mkdir( projectDir, wxS_DIR_DEFAULT, wxPATH_MKDIR_FULL );
94
95 wxString projectPath = projectDir + sep + aProjectStem + wxT( ".kicad_pro" );
96
97 Pgm().GetSettingsManager().LoadProject( projectPath );
99 Pgm().GetLibraryManager().LoadProjectTables( project.GetProjectDirectory() );
100
101 aSchematic = std::make_unique<SCHEMATIC>( &project );
102 aSchematic->Reset();
103
104 IO_RELEASER<SCH_IO> pi( SCH_IO_MGR::FindPlugin( SCH_IO_MGR::SCH_EAGLE ) );
105 BOOST_REQUIRE( pi.get() != nullptr );
106
107 SCH_SHEET* loadedSheet = nullptr;
108
109 try
110 {
111 loadedSheet = pi->LoadSchematicFile( aEagleFn.GetFullPath(), aSchematic.get() );
112 }
113 catch( const std::exception& e )
114 {
115 BOOST_FAIL( std::string( "LoadSchematicFile threw: " ) + e.what() );
116 }
117 catch( ... )
118 {
119 BOOST_FAIL( "LoadSchematicFile threw unknown exception" );
120 }
121
122 BOOST_REQUIRE( loadedSheet != nullptr );
123 return loadedSheet;
124}
125
126
135BOOST_AUTO_TEST_CASE( ImportHierarchy )
136{
137 const wxFileName eagleFn = getEagleTestSchematic( "eagle-import-testfile.sch" );
138 BOOST_REQUIRE( wxFileExists( eagleFn.GetFullPath() ) );
139
140 std::unique_ptr<SCHEMATIC> schematic;
141 SCH_SHEET* loadedSheet = loadEagleSchematic( eagleFn, wxS( "eagle_test" ), schematic );
142
143 // The returned sheet must be the virtual root (niluuid).
144 BOOST_CHECK( loadedSheet->m_Uuid == niluuid );
145
146 const std::vector<SCH_SHEET*>& topSheets = schematic->GetTopLevelSheets();
147
148 // The test file has exactly 2 Eagle pages; there must be no spurious default sheet.
149 BOOST_CHECK_EQUAL( topSheets.size(), 2 );
150
151 for( SCH_SHEET* sheet : topSheets )
152 {
153 // No top-level sheet should be the virtual root.
154 BOOST_CHECK( sheet != nullptr );
155 BOOST_CHECK( sheet->m_Uuid != niluuid );
156
157 // Every page must have a screen with a non-empty filename so SaveProject can
158 // write it to disk.
159 BOOST_REQUIRE( sheet->GetScreen() != nullptr );
160 BOOST_CHECK( !sheet->GetScreen()->GetFileName().IsEmpty() );
161 }
162
163 // Verify the hierarchy paths match the symbol instance paths so PCB update works.
164 // Each symbol's hierarchical reference must have a path matching one of the top-level
165 // sheet paths in the hierarchy.
166 SCH_SHEET_LIST hierarchy = schematic->BuildSheetListSortedByPageNumbers();
167 BOOST_CHECK_EQUAL( hierarchy.size(), 2 );
168
169 std::set<KIID_PATH> hierPaths;
170
171 for( const SCH_SHEET_PATH& path : hierarchy )
172 hierPaths.insert( path.Path() );
173
174 int totalSymbols = 0;
175 int orphanedSymbols = 0;
176
177 for( const SCH_SHEET_PATH& sheetPath : hierarchy )
178 {
179 SCH_SCREEN* screen = sheetPath.LastScreen();
180
181 if( !screen )
182 continue;
183
184 for( const EDA_ITEM* item : screen->Items().OfType( SCH_SYMBOL_T ) )
185 {
186 const SCH_SYMBOL* sym = static_cast<const SCH_SYMBOL*>( item );
187 ++totalSymbols;
188
189 bool foundMatch = false;
190
191 for( const SCH_SYMBOL_INSTANCE& inst : sym->GetInstances() )
192 {
193 if( hierPaths.count( inst.m_Path ) )
194 {
195 foundMatch = true;
196 break;
197 }
198 }
199
200 if( !foundMatch )
201 ++orphanedSymbols;
202 }
203 }
204
205 BOOST_CHECK_GT( totalSymbols, 0 );
206 BOOST_CHECK_EQUAL( orphanedSymbols, 0 );
207}
208
209
223BOOST_AUTO_TEST_CASE( LabelsRemainGlobalForFlatNamespace )
224{
225 const wxFileName eagleFn = getEagleTestSchematic( "eagle-import-testfile.sch" );
226 BOOST_REQUIRE( wxFileExists( eagleFn.GetFullPath() ) );
227
228 std::unique_ptr<SCHEMATIC> schematic;
229 loadEagleSchematic( eagleFn, wxS( "eagle_label_kind" ), schematic );
230
231 // Bucket every label in the imported schematic by kind + text so we can verify that
232 // each named Eagle net produced a global label and no leftover local SCH_LABEL exists
233 // with that name. Bus labels (e.g. "A[1..3]") must stay local.
234 SCH_SHEET_LIST hierarchy = schematic->BuildSheetListSortedByPageNumbers();
235
236 std::unordered_set<wxString> globalTexts;
237 std::unordered_set<wxString> localTexts;
238
239 for( const SCH_SHEET_PATH& sheetPath : hierarchy )
240 {
241 SCH_SCREEN* screen = sheetPath.LastScreen();
242
243 if( !screen )
244 continue;
245
246 for( SCH_ITEM* item : screen->Items().OfType( SCH_GLOBAL_LABEL_T ) )
247 globalTexts.insert( static_cast<SCH_LABEL_BASE*>( item )->GetText() );
248
249 for( SCH_ITEM* item : screen->Items().OfType( SCH_LABEL_T ) )
250 localTexts.insert( static_cast<SCH_LABEL_BASE*>( item )->GetText() );
251 }
252
253 // Named Eagle nets must produce at least one global label each, and none of these
254 // names may appear as a local SCH_LABEL — that would reintroduce the "/X" sheet-path
255 // prefix that broke board update.
256 const std::vector<wxString> expectedGlobalNets = {
257 wxS( "A1" ), wxS( "A2" ), wxS( "A3" ),
258 wxS( "B1" ), wxS( "B2" ), wxS( "B3" )
259 };
260
261 for( const wxString& netName : expectedGlobalNets )
262 {
263 BOOST_CHECK_MESSAGE( globalTexts.count( netName ) > 0,
264 "Missing global Eagle net label '" << netName.ToStdString()
265 << "' — flat namespace requires SCH_GLOBALLABEL." );
266 BOOST_CHECK_MESSAGE( localTexts.count( netName ) == 0,
267 "Found local SCH_LABEL '" << netName.ToStdString()
268 << "' — Eagle net should be global." );
269 }
270
271 // Bus labels (e.g. "A[1..3]") must stay local: Eagle buses are visual groupings, not
272 // electrical signals, so globalizing them would merge same-named buses project-wide.
273 const std::vector<wxString> expectedLocalBuses = {
274 wxS( "A[1..3]" ), wxS( "B[1..3]" )
275 };
276
277 for( const wxString& busName : expectedLocalBuses )
278 {
279 BOOST_CHECK_MESSAGE( globalTexts.count( busName ) == 0,
280 "Found global SCH_GLOBALLABEL '" << busName.ToStdString()
281 << "' — Eagle bus should remain a local SCH_LABEL." );
282 }
283}
284
285
296BOOST_AUTO_TEST_CASE( PinNameTagStripped )
297{
298 const wxFileName eagleFn = getEagleTestSchematic( "issue24483_pin_tag.sch" );
299 BOOST_REQUIRE( wxFileExists( eagleFn.GetFullPath() ) );
300
301 std::unique_ptr<SCHEMATIC> schematic;
302 loadEagleSchematic( eagleFn, wxS( "eagle_pin_tag" ), schematic );
303
304 SCH_SHEET_LIST hierarchy = schematic->BuildSheetListSortedByPageNumbers();
305
306 // Key symbols by their library item name (e.g. "MYDEV"); the annotated reference is
307 // unreliable here because the no-connect device is annotated with a '#' power prefix. A
308 // single Eagle library imports without a library-name prefix on the symbol; the prefix is
309 // only added to disambiguate duplicate names across multiple Eagle libraries.
310 std::map<wxString, const SCH_SYMBOL*> symbolByLibItem;
311
312 for( const SCH_SHEET_PATH& sheetPath : hierarchy )
313 {
314 SCH_SCREEN* screen = sheetPath.LastScreen();
315
316 if( !screen )
317 continue;
318
319 for( const EDA_ITEM* item : screen->Items().OfType( SCH_SYMBOL_T ) )
320 {
321 const SCH_SYMBOL* sym = static_cast<const SCH_SYMBOL*>( item );
322 symbolByLibItem[sym->GetLibId().GetLibItemName().wx_str()] = sym;
323 }
324 }
325
326 // The connected device maps pins to pads through <connect>; the no-connect device has no
327 // <connect> so pins are auto-numbered. Both paths must strip the tag from the display name.
328 BOOST_REQUIRE( symbolByLibItem.count( wxS( "MYDEV" ) ) == 1 );
329 BOOST_REQUIRE( symbolByLibItem.count( wxS( "MYDEV_NC" ) ) == 1 );
330
331 auto collectNames = []( const SCH_SYMBOL* aSym )
332 {
333 std::multiset<wxString> names;
334
335 for( const SCH_PIN* pin : aSym->GetAllLibPins() )
336 names.insert( pin->GetName() );
337
338 return names;
339 };
340
341 // No display name on either symbol may retain the "@<tag>" suffix.
342 for( const auto& [libItem, sym] : symbolByLibItem )
343 {
344 for( const SCH_PIN* pin : sym->GetAllLibPins() )
345 {
346 BOOST_CHECK_MESSAGE( pin->GetName().Find( '@' ) == wxNOT_FOUND,
347 libItem.ToStdString() << " pin kept Eagle tag '"
348 << pin->GetName().ToStdString() << "'." );
349 }
350 }
351
352 // For the connected device, the two "IN@n" pins must both display as "IN" yet keep their
353 // distinct pad mapping, and "NC@3" must strip to "NC".
354 std::map<wxString, wxString> nameByPad;
355
356 for( const SCH_PIN* pin : symbolByLibItem[wxS( "MYDEV" )]->GetAllLibPins() )
357 nameByPad[pin->GetNumber()] = pin->GetName();
358
359 BOOST_CHECK_EQUAL( nameByPad["1"], wxString( wxS( "IN" ) ) );
360 BOOST_CHECK_EQUAL( nameByPad["2"], wxString( wxS( "IN" ) ) );
361 BOOST_CHECK_EQUAL( nameByPad["3"], wxString( wxS( "GND" ) ) );
362 BOOST_CHECK_EQUAL( nameByPad["4"], wxString( wxS( "NC" ) ) );
363
364 // The no-connect device keeps every pin, so both tagged "IN" pins survive the strip.
365 std::multiset<wxString> ncNames = collectNames( symbolByLibItem[wxS( "MYDEV_NC" )] );
366 BOOST_CHECK_EQUAL( ncNames.count( wxS( "IN" ) ), 2 );
367 BOOST_CHECK_EQUAL( ncNames.count( wxS( "GND" ) ), 1 );
368 BOOST_CHECK_EQUAL( ncNames.count( wxS( "NC" ) ), 1 );
369}
370
371
383BOOST_AUTO_TEST_CASE( DetachedLabelProjectsOntoWire )
384{
385 const wxFileName eagleFn = getEagleTestSchematic( "eagle-import-testfile.sch" );
386 BOOST_REQUIRE( wxFileExists( eagleFn.GetFullPath() ) );
387
388 std::unique_ptr<SCHEMATIC> schematic;
389 loadEagleSchematic( eagleFn, wxS( "eagle_detached_label" ), schematic );
390
391 SCH_LABEL_BASE* detached = nullptr;
392 SCH_SCREEN* screen = nullptr;
393
394 for( const SCH_SHEET_PATH& sheetPath : schematic->BuildSheetListSortedByPageNumbers() )
395 {
396 SCH_SCREEN* sheetScreen = sheetPath.LastScreen();
397
398 if( !sheetScreen )
399 continue;
400
401 for( SCH_ITEM* item : sheetScreen->Items().OfType( SCH_GLOBAL_LABEL_T ) )
402 {
403 if( static_cast<SCH_LABEL_BASE*>( item )->GetText() == wxS( "DETACHEDLABEL" ) )
404 {
405 detached = static_cast<SCH_LABEL_BASE*>( item );
406 screen = sheetScreen;
407 }
408 }
409 }
410
411 BOOST_REQUIRE_MESSAGE( detached, "DETACHEDLABEL global label not imported" );
412
413 // Positions carry an import-time sheet translation, so all checks are made relative to
414 // the wire's own endpoints.
415 const VECTOR2I labelPos = detached->GetPosition();
416 SEG wire;
417 double wireDist = std::numeric_limits<double>::max();
418
419 for( SCH_ITEM* item : screen->Items().OfType( SCH_LINE_T ) )
420 {
421 SCH_LINE* line = static_cast<SCH_LINE*>( item );
422
423 if( !line->IsWire() )
424 continue;
425
426 SEG seg( line->GetStartPoint(), line->GetEndPoint() );
427 double d = seg.Distance( labelPos );
428
429 if( d < wireDist )
430 {
431 wireDist = d;
432 wire = seg;
433 }
434 }
435
436 // The label must be on the wire.
437 BOOST_CHECK_MESSAGE( wireDist <= schIUScale.MilsToIU( 1 ),
438 "Label is " << ( wireDist / schIUScale.IU_PER_MM )
439 << " mm from its wire" );
440
441 // The wire is 40 mm long and the label sat 10 mm from the left end. Perpendicular
442 // projection preserves that; the old endpoint/midpoint snap would put it at 0 mm
443 // (endpoint) or 20 mm (midpoint) from the left end.
444 const VECTOR2I leftEnd = wire.A.x <= wire.B.x ? wire.A : wire.B;
445 const double offsetMM = std::abs( labelPos.x - leftEnd.x ) / (double) schIUScale.IU_PER_MM;
446
447 BOOST_CHECK_MESSAGE( std::abs( offsetMM - 10.0 ) < 1.0,
448 "Label projected to " << offsetMM
449 << " mm from the wire's left end, expected 10 mm" );
450}
constexpr EDA_IU_SCALE schIUScale
Definition base_units.h:123
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
Segment description base class to describe items which have 2 end points (track, wire,...
Definition sch_line.h:38
bool IsWire() const
Return true if the line is a wire.
VECTOR2I GetEndPoint() const
Definition sch_line.h:144
VECTOR2I GetStartPoint() const
Definition sch_line.h:135
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
VECTOR2I GetPosition() const override
Definition sch_text.h:146
Definition seg.h:38
VECTOR2I A
Definition seg.h:45
VECTOR2I B
Definition seg.h:46
int Distance(const SEG &aSeg) const
Compute minimum Euclidean distance to segment aSeg.
Definition seg.cpp:698
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.
EDA_ANGLE abs(const EDA_ANGLE &aAngle)
Definition eda_angle.h:400
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_LINE_T
Definition typeinfo.h:160
@ SCH_SYMBOL_T
Definition typeinfo.h:169
@ SCH_LABEL_T
Definition typeinfo.h:164
@ SCH_GLOBAL_LABEL_T
Definition typeinfo.h:165
VECTOR2< int32_t > VECTOR2I
Definition vector2d.h:683
Definition of file extensions used in Kicad.