KiCad PCB EDA Suite
Loading...
Searching...
No Matches
eeschema/test_easyedapro_v3_import.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 The KiCad Developers, see AUTHORS.txt for contributors.
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version 2
9 * of the License, or (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, you may find one here:
18 * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
19 * or you may search the http://www.gnu.org website for the version 2 license,
20 * or you may write to the Free Software Foundation, Inc.,
21 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
22 */
23
28
29#include <boost/test/unit_test.hpp>
30
31#include <array>
32#include <set>
33
34#include <qa_utils/file_utils.h>
36
39
41
42#include <pgm_base.h>
43#include <schematic.h>
44#include <sch_io/sch_io.h>
45#include <sch_io/sch_io_mgr.h>
46#include <sch_label.h>
47#include <sch_line.h>
48#include <sch_pin.h>
49#include <sch_screen.h>
50#include <sch_symbol.h>
51#include <sch_text.h>
52
54
55#include <wx/filefn.h>
56#include <wx/filename.h>
57#include <wx/wfstream.h>
58#include <wx/zipstrm.h>
59
60
61namespace
62{
63
64static wxString getEasyEdaProV3ArchivePath()
65{
66 return wxString::FromUTF8( KI_TEST::GetTestDataRootDir()
67 + "pcbnew/plugins/easyedapro/ProProject_LS2K0300Core_2025-11-14.epro2" );
68}
69
70
71static void writeZipEntry( wxZipOutputStream& aZip, const wxString& aName, const std::string& aContent )
72{
73 aZip.PutNextEntry( aName );
74 aZip.Write( aContent.data(), aContent.size() );
75 aZip.CloseEntry();
76}
77
78
79static wxString makeTempElibz2Path( const wxString& aPrefix )
80{
81 wxString tempPath = wxFileName::CreateTempFileName( aPrefix );
82 wxRemoveFile( tempPath );
83
84 wxFileName fn( tempPath );
85 fn.SetExt( wxS( "elibz2" ) );
86
87 return fn.GetFullPath();
88}
89
90
91static void writeV3LibraryArchive( const wxString& aPath, const std::string& aSymbolIndex,
92 const std::string& aFootprintIndex, const std::string& aElibu )
93{
94 wxFFileOutputStream out( aPath );
95 BOOST_REQUIRE( out.IsOk() );
96
97 wxZipOutputStream zip( out, -1, wxConvUTF8 );
98
99 if( !aSymbolIndex.empty() )
100 writeZipEntry( zip, wxS( "symbol2.json" ), aSymbolIndex );
101
102 if( !aFootprintIndex.empty() )
103 writeZipEntry( zip, wxS( "footprint2.json" ), aFootprintIndex );
104
105 writeZipEntry( zip, wxS( "library.elibu" ), aElibu );
106}
107
109struct LOADED_SAMPLE
110{
111 LOADED_SAMPLE() :
112 m_project( Pgm().GetSettingsManager(), wxS( "easyedapro_v3_import" ), wxS( "easyedapro_v3_test" ) ),
113 m_schematic( nullptr ),
115 {
116 wxFileName archiveFile( m_project.DirStr(), "sample", "epro2" );
117 BOOST_REQUIRE( wxCopyFile( getEasyEdaProV3ArchivePath(), archiveFile.GetFullPath() ) );
118
119 m_schematic.SetProject( &m_project.Project() );
120
121 m_plugin.reset( SCH_IO_MGR::FindPlugin( SCH_IO_MGR::SCH_EASYEDAPRO_V3 ) );
122 BOOST_REQUIRE( m_plugin );
123
124 BOOST_REQUIRE_NO_THROW( m_rootSheet = m_plugin->LoadSchematicFile( archiveFile.GetFullPath(), &m_schematic,
125 nullptr, nullptr ) );
126 BOOST_REQUIRE( m_rootSheet );
127
128 m_schematic.RefreshHierarchy();
129
130 BOOST_REQUIRE_NO_THROW( m_docs.Load() );
131
132 s_instance = this;
133 }
134
135 ~LOADED_SAMPLE() { s_instance = nullptr; }
136
137 static LOADED_SAMPLE& Get() { return *s_instance; }
138
139 KI_TEST::SCOPED_TEMP_PROJECT m_project;
140 SCHEMATIC m_schematic;
141 IO_RELEASER<SCH_IO> m_plugin;
142 EASYEDAPRO::V3_DOC_PARSER m_docs;
143 SCH_SHEET* m_rootSheet = nullptr;
144
145 static inline LOADED_SAMPLE* s_instance = nullptr;
146};
147
148} // namespace
149
150
151BOOST_AUTO_TEST_SUITE( EasyEdaProV3Import )
152
153
154BOOST_AUTO_TEST_CASE( LibraryIndexMergeKeepsSymbolAndFootprintNames )
155{
156 wxString archivePath = makeTempElibz2Path( wxS( "easyedapro_v3_mixed" ) );
157
158 const std::string symbolIndex = R"({
159 "devices": {},
160 "symbols": {
161 "sym_uuid": { "uuid": "sym_uuid", "display_title": "SymbolName" }
162 },
163 "footprints": {},
164 "panelLibs": {}
165 })";
166
167 const std::string footprintIndex = R"({
168 "devices": {},
169 "symbols": {},
170 "footprints": {
171 "fp_uuid": { "uuid": "fp_uuid", "display_title": "FootprintName" }
172 },
173 "panelLibs": {}
174 })";
175
176 const std::string elibu = "{\"type\":\"DOCHEAD\"}||{\"docType\":\"SYMBOL\",\"uuid\":\"sym_uuid\"}|\n"
177 "{\"type\":\"DOCHEAD\"}||{\"docType\":\"FOOTPRINT\",\"uuid\":\"fp_uuid\"}|\n";
178
179 writeV3LibraryArchive( archivePath, symbolIndex, footprintIndex, elibu );
180
181 EASYEDAPRO::V3_DOC_PARSER parser( archivePath );
182 BOOST_REQUIRE_NO_THROW( parser.LoadLibrary() );
183
184 std::map<wxString, wxString> symbols = EASYEDAPRO::BuildV3LibraryItemMap( parser, "symbols", wxS( "SYMBOL" ) );
185 std::map<wxString, wxString> footprints =
186 EASYEDAPRO::BuildV3LibraryItemMap( parser, "footprints", wxS( "FOOTPRINT" ) );
187
188 BOOST_CHECK_EQUAL( symbols.at( wxS( "SymbolName" ) ), wxString( wxS( "sym_uuid" ) ) );
189 BOOST_CHECK_EQUAL( footprints.at( wxS( "FootprintName" ) ), wxString( wxS( "fp_uuid" ) ) );
190
191 BOOST_CHECK( wxRemoveFile( archivePath ) );
192}
193
194
195BOOST_AUTO_TEST_CASE( LibraryItemDuplicateNamesGetNumericSuffix )
196{
197 wxString archivePath = makeTempElibz2Path( wxS( "easyedapro_v3_dupes" ) );
198
199 const std::string symbolIndex = R"({
200 "devices": {},
201 "symbols": {
202 "aaaaaaaa0001": { "uuid": "aaaaaaaa0001", "display_title": "DUP" },
203 "aaaaaaaa0002": { "uuid": "aaaaaaaa0002", "display_title": "DUP" },
204 "aaaaaaaa0003": { "uuid": "aaaaaaaa0003", "display_title": "DUP" }
205 },
206 "footprints": {},
207 "panelLibs": {}
208 })";
209
210 const std::string elibu = "{\"type\":\"DOCHEAD\"}||{\"docType\":\"SYMBOL\",\"uuid\":\"aaaaaaaa0001\"}|\n"
211 "{\"type\":\"DOCHEAD\"}||{\"docType\":\"SYMBOL\",\"uuid\":\"aaaaaaaa0002\"}|\n"
212 "{\"type\":\"DOCHEAD\"}||{\"docType\":\"SYMBOL\",\"uuid\":\"aaaaaaaa0003\"}|\n";
213
214 writeV3LibraryArchive( archivePath, symbolIndex, std::string(), elibu );
215
216 EASYEDAPRO::V3_DOC_PARSER parser( archivePath );
217 BOOST_REQUIRE_NO_THROW( parser.LoadLibrary() );
218
219 std::map<wxString, wxString> symbols = EASYEDAPRO::BuildV3LibraryItemMap( parser, "symbols", wxS( "SYMBOL" ) );
220
221 BOOST_REQUIRE_EQUAL( symbols.size(), 3 );
222 BOOST_CHECK( symbols.contains( wxS( "DUP" ) ) );
223 BOOST_CHECK( symbols.contains( wxS( "DUP_2" ) ) );
224 BOOST_CHECK( symbols.contains( wxS( "DUP_3" ) ) );
225
226 BOOST_CHECK( wxRemoveFile( archivePath ) );
227}
228
229
230BOOST_AUTO_TEST_CASE( RawDocNullTombstonesRemovedAfterDeduplication )
231{
232 wxString archivePath = makeTempElibz2Path( wxS( "easyedapro_v3_tombstone" ) );
233
234 const std::string footprintIndex = R"({
235 "devices": {},
236 "symbols": {},
237 "footprints": {},
238 "panelLibs": {}
239 })";
240
241 // p1: live row superseded by a newer null tombstone (must disappear).
242 // p2: null tombstone superseded by a newer live row (must survive).
243 // p3: plain live row (must survive).
244 const std::string elibu =
245 "{\"type\":\"DOCHEAD\"}||{\"docType\":\"FOOTPRINT\",\"uuid\":\"fp_tombstone\"}|\n"
246 "{\"type\":\"POLY\",\"id\":\"p1\",\"ticket\":1}||{\"path\":[[0,0],[10,10]],\"width\":1}|\n"
247 "{\"type\":\"POLY\",\"id\":\"p1\",\"ticket\":2}|||\n"
248 "{\"type\":\"POLY\",\"id\":\"p2\",\"ticket\":1}|||\n"
249 "{\"type\":\"POLY\",\"id\":\"p2\",\"ticket\":2}||{\"path\":[[0,0],[10,10]],\"width\":1}|\n"
250 "{\"type\":\"FILL\",\"id\":\"p3\",\"ticket\":1}||{\"path\":[[0,0],[10,10]]}|\n";
251
252 writeV3LibraryArchive( archivePath, std::string(), footprintIndex, elibu );
253
254 EASYEDAPRO::V3_DOC_PARSER parser( archivePath );
255 BOOST_REQUIRE_NO_THROW( parser.LoadLibrary() );
256
257 const EASYEDAPRO::V3_DOC_RAW* doc = parser.FindRawDoc( wxS( "FOOTPRINT" ), wxS( "fp_tombstone" ) );
258 BOOST_REQUIRE( doc );
259
260 // Every surviving row carries a non-null inner payload: null tombstones are dropped.
261 for( const EASYEDAPRO::V3_ROW& row : doc->rows )
262 BOOST_CHECK( !row.inner.is_null() );
263
264 // p1 was superseded by a newer null tombstone and must be gone.
265 BOOST_CHECK( !doc->rowById.contains( wxS( "p1" ) ) );
266
267 // p2 and p3 must survive.
268 BOOST_REQUIRE( doc->rowById.contains( wxS( "p2" ) ) );
269 BOOST_REQUIRE( doc->rowById.contains( wxS( "p3" ) ) );
270 BOOST_CHECK_EQUAL( doc->rows.size(), 2 );
271
272 // rowById must index every surviving row at its compacted position.
273 for( const auto& [id, index] : doc->rowById )
274 {
276 BOOST_CHECK_EQUAL( doc->rows[index].id, id );
277 }
278
279 BOOST_CHECK( wxRemoveFile( archivePath ) );
280}
281
282
283BOOST_AUTO_TEST_SUITE( LS2K0300Sample, *boost::unit_test::fixture<LOADED_SAMPLE>() )
284
285BOOST_AUTO_TEST_CASE( BuildProjectIndexHasSchematicSheets )
286{
287 const EASYEDAPRO::V3_DOC_PARSER& v3 = LOADED_SAMPLE::Get().m_docs;
288
290
291 BOOST_REQUIRE( project.contains( "schematics" ) );
292 BOOST_REQUIRE( project.at( "schematics" ).is_object() );
293 BOOST_REQUIRE( !project.at( "schematics" ).empty() );
294
295 bool hasAtLeastOneSheet = false;
296
297 for( const auto& [schUuid, sch] : project.at( "schematics" ).items() )
298 {
299 if( sch.contains( "sheets" ) && sch.at( "sheets" ).is_array()
300 && !sch.at( "sheets" ).empty() )
301 {
302 hasAtLeastOneSheet = true;
303 break;
304 }
305 }
306
307 BOOST_CHECK( hasAtLeastOneSheet );
308}
309
310
311BOOST_AUTO_TEST_CASE( RawSchematicPageContainsTextWireAndNetAttr )
312{
313 const EASYEDAPRO::V3_DOC_PARSER& v3 = LOADED_SAMPLE::Get().m_docs;
314
315 const auto& pages = v3.GetRawDocs( wxS( "SCH_PAGE" ) );
316 BOOST_REQUIRE( !pages.empty() );
317
318 bool hasTextValue = false;
319 bool hasLineStartEnd = false;
320 bool hasWireNetAttr = false;
321
322 for( const auto& [pageUuid, pageDoc] : pages )
323 {
324 for( const EASYEDAPRO::V3_ROW& row : pageDoc.rows )
325 {
326 if( row.type == wxS( "TEXT" ) && row.inner.contains( "value" ) )
327 hasTextValue = true;
328
329 if( row.type == wxS( "LINE" )
330 && row.inner.contains( "startX" ) && row.inner.contains( "startY" )
331 && row.inner.contains( "endX" ) && row.inner.contains( "endY" ) )
332 {
333 hasLineStartEnd = true;
334 }
335
336 if( row.type == wxS( "ATTR" )
337 && EASYEDAPRO::V3GetString( row.inner, "key" ) == wxS( "NET" ) )
338 {
339 hasWireNetAttr = true;
340 }
341 }
342 }
343
344 BOOST_CHECK( hasTextValue );
345 BOOST_CHECK( hasLineStartEnd );
346 BOOST_CHECK( hasWireNetAttr );
347}
348
349
350BOOST_AUTO_TEST_CASE( RawSymbolContainsBlobObjectReference )
351{
352 const EASYEDAPRO::V3_DOC_PARSER& v3 = LOADED_SAMPLE::Get().m_docs;
353
354 const auto& symbols = v3.GetRawDocs( wxS( "SYMBOL" ) );
355 BOOST_REQUIRE( !symbols.empty() );
356
357 bool hasBlobObject = false;
358
359 for( const auto& [symbolUuid, symbolDoc] : symbols )
360 {
361 for( const EASYEDAPRO::V3_ROW& row : symbolDoc.rows )
362 {
363 if( row.type != wxS( "OBJ" ) )
364 continue;
365
366 wxString content = EASYEDAPRO::V3GetString( row.inner, "content" );
367
368 if( content.StartsWith( wxS( "blob:" ) ) )
369 {
370 hasBlobObject = true;
371 break;
372 }
373 }
374
375 if( hasBlobObject )
376 break;
377 }
378
379 BOOST_CHECK( hasBlobObject );
380}
381
382
383BOOST_AUTO_TEST_CASE( RawSymbolPartUnitsAndPinOrientationAreStable )
384{
385 const EASYEDAPRO::V3_DOC_PARSER& v3 = LOADED_SAMPLE::Get().m_docs;
386
388 std::map<wxString, EASYEDAPRO::PRJ_DEVICE> devices = project.at( "devices" );
389
390 auto findDevice = [&]( const wxString& aTitle ) -> const EASYEDAPRO::PRJ_DEVICE*
391 {
392 for( const auto& [devUuid, dev] : devices )
393 {
394 if( dev.title == aTitle )
395 return &dev;
396 }
397
398 return nullptr;
399 };
400
401 const EASYEDAPRO::PRJ_DEVICE* ls2kDevice = findDevice( wxS( "LS2K0300" ) );
402 BOOST_REQUIRE( ls2kDevice );
403
404 const EASYEDAPRO::PRJ_DEVICE* sgmDevice = findDevice( wxS( "SGM61032BXKB6G/TR" ) );
405 BOOST_REQUIRE( sgmDevice );
406
407 SCH_EASYEDAPRO_V3_PARSER parser( nullptr, nullptr );
408
409 wxString ls2kSymbolUuid = ls2kDevice->attributes.at( wxS( "Symbol" ) );
410 const EASYEDAPRO::V3_DOC_RAW* ls2kRaw = v3.FindRawDoc( wxS( "SYMBOL" ), ls2kSymbolUuid );
411 BOOST_REQUIRE( ls2kRaw );
412
413 EASYEDAPRO::SYM_INFO ls2kInfo = parser.ParseSymbol( *ls2kRaw, ls2kDevice->attributes );
414 BOOST_REQUIRE( ls2kInfo.libSymbol );
415 BOOST_CHECK_EQUAL( ls2kInfo.libSymbol->GetUnitCount(), 5 );
416
417 std::array<int, 6> unitPinCount = {};
418
420 {
421 int unit = pin->GetUnit();
422
423 if( unit >= 1 && unit <= 5 )
424 unitPinCount[unit]++;
425 }
426
427 for( int unit = 1; unit <= 5; ++unit )
428 BOOST_CHECK_MESSAGE( unitPinCount[unit] > 0, "LS2K0300 should have pins in each unit" );
429
430 wxString sgmSymbolUuid = sgmDevice->attributes.at( wxS( "Symbol" ) );
431 const EASYEDAPRO::V3_DOC_RAW* sgmRaw = v3.FindRawDoc( wxS( "SYMBOL" ), sgmSymbolUuid );
432 BOOST_REQUIRE( sgmRaw );
433
434 EASYEDAPRO::SYM_INFO sgmInfo = parser.ParseSymbol( *sgmRaw, sgmDevice->attributes );
435 BOOST_REQUIRE( sgmInfo.libSymbol );
436
437 bool hasLeftPins = false;
438 bool hasRightPins = false;
439
441 {
442 if( pin->GetPosition().x < 0 )
443 {
444 hasLeftPins = true;
445 BOOST_CHECK_EQUAL( static_cast<int>( pin->GetOrientation() ),
446 static_cast<int>( PIN_ORIENTATION::PIN_RIGHT ) );
447 }
448 else if( pin->GetPosition().x > 0 )
449 {
450 hasRightPins = true;
451 BOOST_CHECK_EQUAL( static_cast<int>( pin->GetOrientation() ),
452 static_cast<int>( PIN_ORIENTATION::PIN_LEFT ) );
453 }
454 }
455
456 BOOST_CHECK( hasLeftPins );
457 BOOST_CHECK( hasRightPins );
458}
459
460
461BOOST_AUTO_TEST_CASE( PluginLoadProducesWireTextAndLabel )
462{
463 SCHEMATIC& schematic = LOADED_SAMPLE::Get().m_schematic;
464
465 int wireCount = 0;
466 int textCount = 0;
467 int labelCount = 0;
468
469 for( const SCH_SHEET_PATH& sheetPath : schematic.Hierarchy() )
470 {
471 SCH_SCREEN* screen = sheetPath.LastScreen();
472
473 if( !screen )
474 continue;
475
476 for( SCH_ITEM* item : screen->Items().OfType( SCH_LINE_T ) )
477 {
478 SCH_LINE* line = static_cast<SCH_LINE*>( item );
479
480 if( line->GetLayer() == LAYER_WIRE )
481 wireCount++;
482 }
483
484 for( SCH_ITEM* item : screen->Items().OfType( SCH_TEXT_T ) )
485 {
486 SCH_TEXT* text = static_cast<SCH_TEXT*>( item );
487
488 if( !text->GetText().IsEmpty() )
489 textCount++;
490 }
491
492 for( SCH_ITEM* item : screen->Items().OfType( SCH_LABEL_T ) )
493 {
494 SCH_LABEL* label = static_cast<SCH_LABEL*>( item );
495
496 if( !label->GetText().IsEmpty() )
497 labelCount++;
498 }
499 }
500
501 BOOST_CHECK( wireCount > 0 );
502 BOOST_CHECK( textCount > 0 );
503 BOOST_CHECK( labelCount > 0 );
504}
505
506
507BOOST_AUTO_TEST_CASE( PluginLoadPreservesWireNetLabelAlignment )
508{
509 // Pick a stable, unique NET label from raw docs where align indicates the text is anchored
510 // on the right side of the connection point.
511 const EASYEDAPRO::V3_DOC_PARSER& v3 = LOADED_SAMPLE::Get().m_docs;
512
513 struct NET_ATTR
514 {
515 wxString value;
516 wxString align;
517 int rotation;
518 };
519
520 std::vector<NET_ATTR> candidates;
521 std::map<wxString, int> valueCounts;
522
523 for( const auto& [pageUuid, pageDoc] : v3.GetRawDocs( wxS( "SCH_PAGE" ) ) )
524 {
525 for( const EASYEDAPRO::V3_ROW& row : pageDoc.rows )
526 {
527 if( row.type != wxS( "ATTR" ) )
528 continue;
529
530 if( EASYEDAPRO::V3GetString( row.inner, "key" ) != wxS( "NET" ) )
531 continue;
532
533 if( !EASYEDAPRO::V3GetBool( row.inner, "valueVisible" ) )
534 continue;
535
536 wxString value =
537 EASYEDAPRO::V3JsonToString( row.inner.value( "value", nlohmann::json() ) );
538
539 if( value.IsEmpty() )
540 continue;
541
542 valueCounts[value]++;
543
544 wxString align = EASYEDAPRO::V3GetString( row.inner, "align", wxS( "LEFT_BOTTOM" ) );
545 int rotation = EASYEDAPRO::V3GetInt( row.inner, "rotation", 0 );
546
547 if( align.StartsWith( wxS( "RIGHT" ) ) )
548 candidates.push_back( { value, align, rotation } );
549 }
550 }
551
552 BOOST_REQUIRE( !candidates.empty() );
553
554 NET_ATTR selected = candidates.front();
555
556 for( const NET_ATTR& candidate : candidates )
557 {
558 if( valueCounts[candidate.value] == 1 )
559 {
560 selected = candidate;
561 break;
562 }
563 }
564
565 SPIN_STYLE expectedSpinStyle = SPIN_STYLE::RIGHT;
566
567 if( selected.align.StartsWith( wxS( "RIGHT" ) ) )
568 expectedSpinStyle = SPIN_STYLE::LEFT;
569 else if( selected.align.StartsWith( wxS( "LEFT" ) ) )
570 expectedSpinStyle = SPIN_STYLE::RIGHT;
571
572 int rot = selected.rotation % 360;
573
574 if( rot < 0 )
575 rot += 360;
576
577 if( rot % 90 == 0 )
578 {
579 for( int i = 0; i < rot; i += 90 )
580 expectedSpinStyle = expectedSpinStyle.RotateCCW();
581 }
582
583 // Load via the plugin and validate the resulting schematic keeps label spin style.
584 SCHEMATIC& schematic = LOADED_SAMPLE::Get().m_schematic;
585
586 int foundCount = 0;
587 bool hasExpectedSpinStyle = false;
588
589 for( const SCH_SHEET_PATH& sheetPath : schematic.Hierarchy() )
590 {
591 SCH_SCREEN* screen = sheetPath.LastScreen();
592
593 if( !screen )
594 continue;
595
596 for( SCH_ITEM* item : screen->Items().OfType( SCH_LABEL_T ) )
597 {
598 SCH_LABEL* label = static_cast<SCH_LABEL*>( item );
599
600 if( label->GetText() != selected.value )
601 continue;
602
603 foundCount++;
604
605 if( static_cast<int>( label->GetSpinStyle() ) == static_cast<int>( expectedSpinStyle ) )
606 hasExpectedSpinStyle = true;
607 }
608 }
609
610 BOOST_CHECK_EQUAL( foundCount, 1 );
611 BOOST_CHECK_MESSAGE( hasExpectedSpinStyle,
612 wxString::Format( "Label '%s' spin style mismatch", selected.value ) );
613}
614
615
616BOOST_AUTO_TEST_CASE( PluginLoadConvertsNetportsToGlobalLabels )
617{
618 // Extract expected netport instance names from the raw v3 docs.
619 const EASYEDAPRO::V3_DOC_PARSER& v3 = LOADED_SAMPLE::Get().m_docs;
620
621 std::set<wxString> netportSymbolUuids;
622
623 for( const auto& [uuid, symDoc] : v3.GetRawDocs( wxS( "SYMBOL" ) ) )
624 {
625 for( const EASYEDAPRO::V3_ROW& row : symDoc.rows )
626 {
627 if( row.type == wxS( "META" )
628 && EASYEDAPRO::V3GetInt( row.inner, "docType", 0 ) == 19 )
629 {
630 netportSymbolUuids.insert( uuid );
631 break;
632 }
633 }
634 }
635
636 BOOST_REQUIRE( !netportSymbolUuids.empty() );
637
638 std::set<wxString> netportComponentIds;
639
640 for( const auto& [pageUuid, pageDoc] : v3.GetRawDocs( wxS( "SCH_PAGE" ) ) )
641 {
642 for( const EASYEDAPRO::V3_ROW& row : pageDoc.rows )
643 {
644 if( row.type != wxS( "ATTR" ) )
645 continue;
646
647 if( EASYEDAPRO::V3GetString( row.inner, "key" ) != wxS( "Symbol" ) )
648 continue;
649
650 wxString symbolUuid = EASYEDAPRO::V3GetString( row.inner, "value" );
651
652 if( netportSymbolUuids.count( symbolUuid ) )
653 netportComponentIds.insert( EASYEDAPRO::V3GetString( row.inner, "parentId" ) );
654 }
655 }
656
657 BOOST_REQUIRE( !netportComponentIds.empty() );
658
659 std::set<wxString> expectedNetportNames;
660
661 for( const auto& [pageUuid, pageDoc] : v3.GetRawDocs( wxS( "SCH_PAGE" ) ) )
662 {
663 for( const EASYEDAPRO::V3_ROW& row : pageDoc.rows )
664 {
665 if( row.type != wxS( "ATTR" ) )
666 continue;
667
668 wxString parentId = EASYEDAPRO::V3GetString( row.inner, "parentId" );
669
670 if( !netportComponentIds.count( parentId ) )
671 continue;
672
673 if( EASYEDAPRO::V3GetString( row.inner, "key" ) != wxS( "Name" ) )
674 continue;
675
677 row.inner.value( "value", nlohmann::json() ) );
678
679 if( !name.IsEmpty() )
680 expectedNetportNames.insert( name );
681 }
682 }
683
684 BOOST_REQUIRE( !expectedNetportNames.empty() );
685
686 // Load via the plugin and validate the resulting schematic contains global labels (not symbols).
687 SCHEMATIC& schematic = LOADED_SAMPLE::Get().m_schematic;
688
689 int netportSymbolCount = 0;
690 int globalLabelCount = 0;
691 bool hasInputLabel = false;
692 bool hasOutputLabel = false;
693 bool hasBidiLabel = false;
694
695 std::set<wxString> importedGlobalLabelNames;
696
697 for( const SCH_SHEET_PATH& sheetPath : schematic.Hierarchy() )
698 {
699 SCH_SCREEN* screen = sheetPath.LastScreen();
700
701 if( !screen )
702 continue;
703
704 for( SCH_ITEM* item : screen->Items().OfType( SCH_SYMBOL_T ) )
705 {
706 SCH_SYMBOL* symbol = static_cast<SCH_SYMBOL*>( item );
707 wxString ref = symbol->GetRef( &sheetPath, false );
708
709 if( ref.StartsWith( wxS( "#NET" ) ) )
710 netportSymbolCount++;
711 }
712
713 for( SCH_ITEM* item : screen->Items().OfType( SCH_GLOBAL_LABEL_T ) )
714 {
715 SCH_GLOBALLABEL* label = static_cast<SCH_GLOBALLABEL*>( item );
716
717 if( label->GetText().IsEmpty() )
718 continue;
719
720 globalLabelCount++;
721 importedGlobalLabelNames.insert( label->GetText() );
722
723 switch( label->GetShape() )
724 {
725 case LABEL_FLAG_SHAPE::L_INPUT: hasInputLabel = true; break;
726 case LABEL_FLAG_SHAPE::L_OUTPUT: hasOutputLabel = true; break;
727 case LABEL_FLAG_SHAPE::L_BIDI: hasBidiLabel = true; break;
728 default: break;
729 }
730 }
731 }
732
733 BOOST_CHECK_EQUAL( netportSymbolCount, 0 );
734 BOOST_CHECK( globalLabelCount > 0 );
735
736 // The test project contains Netport-IN/OUT/BI symbols; ensure we preserve their direction.
737 BOOST_CHECK( hasInputLabel );
738 BOOST_CHECK( hasOutputLabel );
739 BOOST_CHECK( hasBidiLabel );
740
741 // All netport instance names should import as global labels.
742 for( const wxString& expected : expectedNetportNames )
743 {
744 BOOST_CHECK_MESSAGE( importedGlobalLabelNames.count( expected ) > 0,
745 wxString::Format( "Missing global label '%s'", expected ) );
746 }
747}
748
749
750BOOST_AUTO_TEST_CASE( PluginLoadKeepsSchematicVerticalOrderAndFootprints )
751{
752 SCHEMATIC& schematic = LOADED_SAMPLE::Get().m_schematic;
753
754 SCH_SYMBOL* u6 = nullptr;
755 SCH_SYMBOL* u7 = nullptr;
756 SCH_SYMBOL* u1 = nullptr;
757
758 for( const SCH_SHEET_PATH& sheetPath : schematic.Hierarchy() )
759 {
760 SCH_SCREEN* screen = sheetPath.LastScreen();
761
762 if( !screen )
763 continue;
764
765 for( SCH_ITEM* item : screen->Items().OfType( SCH_SYMBOL_T ) )
766 {
767 SCH_SYMBOL* symbol = static_cast<SCH_SYMBOL*>( item );
768 wxString ref = symbol->GetRef( &sheetPath, false );
769
770 if( ref == wxS( "U6" ) )
771 u6 = symbol;
772 else if( ref == wxS( "U7" ) )
773 u7 = symbol;
774 else if( ref == wxS( "U1" ) )
775 u1 = symbol;
776 }
777 }
778
779 BOOST_REQUIRE( u6 );
780 BOOST_REQUIRE( u7 );
781 BOOST_REQUIRE( u1 );
782
783 BOOST_CHECK_MESSAGE( u7->GetPosition().y < u6->GetPosition().y,
784 "U7 should remain above U6 after EasyEDA Pro v3 import" );
785
786 wxString u6Footprint = u6->GetFootprintFieldText( nullptr, RAW_VALUE );
787 wxString u1Footprint = u1->GetFootprintFieldText( nullptr, RAW_VALUE );
788
789 BOOST_CHECK( !u6Footprint.IsEmpty() );
790 BOOST_CHECK( u6Footprint.Contains( wxS( "SOT-563-6" ) ) );
791
792 BOOST_CHECK( !u1Footprint.IsEmpty() );
793 BOOST_CHECK( u1Footprint.Contains( wxS( "BGA-286_17x17_12.0x12.0mm" ) ) );
794}
795
796
797BOOST_AUTO_TEST_CASE( PluginLoadPowerSymbolsKeepValueAndVisibility )
798{
799 SCHEMATIC& schematic = LOADED_SAMPLE::Get().m_schematic;
800
801 int plus5vCount = 0;
802 int plus5vShownCount = 0;
803 int plus5vOffsetCount = 0;
804 int plus5vUprightCount = 0;
805 int plus5vUprightAtSymbolXCount = 0;
806 int powerValueOnPinBodySideCheckedCount = 0;
807 int powerValueOnWrongPinSideCount = 0;
808 int otherPowerValueCount = 0;
809 int otherPowerShownCount = 0;
810
811 for( const SCH_SHEET_PATH& sheetPath : schematic.Hierarchy() )
812 {
813 SCH_SCREEN* screen = sheetPath.LastScreen();
814
815 if( !screen )
816 continue;
817
818 for( SCH_ITEM* item : screen->Items().OfType( SCH_SYMBOL_T ) )
819 {
820 SCH_SYMBOL* symbol = static_cast<SCH_SYMBOL*>( item );
821
822 if( !symbol->IsGlobalPower() )
823 continue;
824
825 SCH_FIELD* valueField = symbol->GetField( FIELD_T::VALUE );
826
827 if( !valueField )
828 continue;
829
830 wxString valueText = valueField->GetShownText( &sheetPath, INTERNAL );
831
832 if( valueField->IsVisible() && !valueText.IsEmpty() )
833 {
834 VECTOR2I delta = valueField->GetPosition() - symbol->GetPosition();
835
836 if( delta != VECTOR2I() )
837 {
838 std::vector<SCH_PIN*> pins = symbol->GetPins( &sheetPath );
839
840 if( !pins.empty() )
841 {
842 VECTOR2I dir;
843
844 switch( pins[0]->PinDrawOrient( symbol->GetTransform() ) )
845 {
846 case PIN_ORIENTATION::PIN_LEFT: dir = VECTOR2I( -1, 0 ); break;
847 case PIN_ORIENTATION::PIN_RIGHT: dir = VECTOR2I( 1, 0 ); break;
848 case PIN_ORIENTATION::PIN_UP: dir = VECTOR2I( 0, -1 ); break;
849 case PIN_ORIENTATION::PIN_DOWN: dir = VECTOR2I( 0, 1 ); break;
850 default: dir = VECTOR2I(); break;
851 }
852
853 if( dir != VECTOR2I() )
854 {
855 long long dot = static_cast<long long>( delta.x ) * dir.x
856 + static_cast<long long>( delta.y ) * dir.y;
857
858 powerValueOnPinBodySideCheckedCount++;
859
860 if( dot < 0 )
861 powerValueOnWrongPinSideCount++;
862 }
863 }
864 }
865 }
866
867 if( valueText == wxS( "+5V" ) )
868 {
869 plus5vCount++;
870
871 if( valueField->IsVisible() )
872 plus5vShownCount++;
873
874 if( valueField->GetPosition().y != symbol->GetPosition().y )
875 plus5vOffsetCount++;
876
877 if( symbol->GetOrientation() == SYM_ORIENT_0 )
878 {
879 plus5vUprightCount++;
880
881 if( valueField->GetPosition().x == symbol->GetPosition().x )
882 plus5vUprightAtSymbolXCount++;
883
886 }
887 }
888 else if( valueText.StartsWith( wxS( "+" ) ) )
889 {
890 otherPowerValueCount++;
891
892 if( valueField->IsVisible() )
893 otherPowerShownCount++;
894 }
895 }
896 }
897
898 BOOST_CHECK( plus5vCount > 0 );
899 BOOST_CHECK_EQUAL( plus5vShownCount, plus5vCount );
900 BOOST_CHECK( plus5vOffsetCount > 0 );
901 BOOST_CHECK( plus5vUprightCount > 0 );
902 BOOST_CHECK_EQUAL( plus5vUprightAtSymbolXCount, plus5vUprightCount );
903
904 BOOST_CHECK( powerValueOnPinBodySideCheckedCount > 0 );
905 BOOST_CHECK_EQUAL( powerValueOnWrongPinSideCount, 0 );
906
907 BOOST_CHECK( otherPowerValueCount > 0 );
908 BOOST_CHECK_EQUAL( otherPowerShownCount, otherPowerValueCount );
909}
910
911
916BOOST_AUTO_TEST_CASE( PluginLoadFillsRootSheetWithFirstPage )
917{
918 SCHEMATIC& schematic = LOADED_SAMPLE::Get().m_schematic;
919
920 int rootSymbols = 0;
921
922 SCH_SHEET* rootSheet = LOADED_SAMPLE::Get().m_rootSheet;
923
924 for( SCH_ITEM* item : rootSheet->GetScreen()->Items().OfType( SCH_SYMBOL_T ) )
925 (void) item, rootSymbols++;
926
927 // The root screen must carry real schematic content, not just sub-sheet links.
928 BOOST_CHECK( rootSymbols > 0 );
929 BOOST_CHECK( !rootSheet->GetScreen()->GetPageNumber().IsEmpty() );
930}
931
932
934
935
936
941BOOST_AUTO_TEST_CASE( AlignTokenMapsToJustify )
942{
943 BOOST_CHECK_EQUAL( EASYEDAPRO::AlignToFontV( wxS( "CENTER_TOP" ) ), 0 );
944 BOOST_CHECK_EQUAL( EASYEDAPRO::AlignToFontV( wxS( "CENTER_MIDDLE" ) ), 1 );
945 BOOST_CHECK_EQUAL( EASYEDAPRO::AlignToFontV( wxS( "CENTER_BOTTOM" ) ), 2 );
946 BOOST_CHECK_EQUAL( EASYEDAPRO::AlignToFontV( wxS( "LEFT_BOTTOM" ) ), 2 );
947 BOOST_CHECK_EQUAL( EASYEDAPRO::AlignToFontV( wxS( "RIGHT_MIDDLE" ) ), 1 );
948
949 BOOST_CHECK_EQUAL( EASYEDAPRO::AlignToFontH( wxS( "CENTER_BOTTOM" ) ), 1 );
950 BOOST_CHECK_EQUAL( EASYEDAPRO::AlignToFontH( wxS( "LEFT_BOTTOM" ) ), 0 );
951 BOOST_CHECK_EQUAL( EASYEDAPRO::AlignToFontH( wxS( "RIGHT_TOP" ) ), 2 );
952}
953
954
int index
const char * name
const wxString & GetPageNumber() const
Parses EasyEDA Pro v3 .epro2 archives.
const V3_DOC_RAW * FindRawDoc(const wxString &aDocType, const wxString &aUuid) const
void LoadLibrary()
Load all v3 docs from a library archive.
virtual const wxString & GetText() const
Return the string associated with the text object.
Definition eda_text.h:118
virtual bool IsVisible() const
Definition eda_text.h:226
GR_TEXT_H_ALIGN_T GetHorizJustify() const
Definition eda_text.h:239
GR_TEXT_V_ALIGN_T GetVertJustify() const
Definition eda_text.h:242
EE_TYPE OfType(KICAD_T aType) const
Definition sch_rtree.h:248
int GetUnitCount() const override
std::vector< SCH_PIN * > GetGraphicalPins(int aUnit=0, int aBodyStyle=0) const override
Graphical pins: Return schematic pin objects as drawn (unexpanded), filtered by unit/body.
Holds all the data relating to one schematic.
Definition schematic.h:155
SCH_SHEET_LIST Hierarchy() const
Return the full schematic flattened hierarchical sheet list.
EASYEDAPRO::SYM_INFO ParseSymbol(const EASYEDAPRO::V3_DOC_RAW &aDoc, const std::map< wxString, wxString > &aDeviceAttributes, const std::map< wxString, EASYEDAPRO::BLOB > &aBlobMap={})
VECTOR2I GetPosition() const override
wxString GetShownText(const SCH_SHEET_PATH *aPath, RESOLUTION_CONTEXT aContext, const wxString &aVariantName=wxEmptyString, int aDepth=0) const
Base class for any item which can be embedded within the SCHEMATIC container class,...
Definition sch_item.h:170
SCH_LAYER_ID GetLayer() const
Return the layer this item is on.
Definition sch_item.h:353
SPIN_STYLE GetSpinStyle() const
LABEL_FLAG_SHAPE GetShape() const
Definition sch_label.h:178
Segment description base class to describe items which have 2 end points (track, wire,...
Definition sch_line.h:39
EE_RTREE & Items()
Get the full RTree, usually for iterating.
Definition sch_screen.h:122
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:50
SCH_SCREEN * GetScreen() const
Definition sch_sheet.h:187
Schematic symbol object.
Definition sch_symbol.h:76
const wxString GetFootprintFieldText(const SCH_SHEET_PATH *aPath, RESOLUTION_CONTEXT aContext, const wxString &aVariantName=wxEmptyString) const
std::vector< const SCH_PIN * > GetPins(const SCH_SHEET_PATH *aSheet) const
Retrieve a list of the SCH_PINs for the given sheet path.
bool IsGlobalPower() const override
VECTOR2I GetPosition() const override
Definition sch_symbol.h:958
int GetOrientation() const override
Get the display symbol orientation.
const wxString GetRef(const SCH_SHEET_PATH *aSheet, bool aIncludeUnit=false) const override
SCH_FIELD * GetField(FIELD_T aFieldType)
Return a mandatory field in this symbol.
Spin style for labels of all kinds on schematics.
Definition sch_label.h:38
SPIN_STYLE RotateCCW()
const TRANSFORM & GetTransform() const
Definition symbol.h:247
@ RAW_VALUE
Definition common.h:98
@ INTERNAL
Definition common.h:96
BOOST_AUTO_TEST_CASE(LibraryIndexMergeKeepsSymbolAndFootprintNames)
std::unique_ptr< T > IO_RELEASER
Helper to hold and release an IO_BASE object when exceptions are thrown.
Definition io_mgr.h:33
@ LAYER_WIRE
Definition layer_ids.h:483
nlohmann::json BuildV3ProjectIndexFromRawDocs(const V3_DOC_PARSER &aParser, bool aIncludeLibraryMetadata=true)
Build a minimal legacy-style project index from parsed v3 raw documents.
bool V3GetBool(const nlohmann::json &aObj, const char *aKey, bool aDefault)
int AlignToFontV(const wxString &aAlign)
Map an EasyEDA "HORIZ_VERT" alignment token to a KiCad justify code (0 top/left, 1 center,...
wxString V3JsonToString(const nlohmann::json &aValue, const wxString &aDefault)
int V3GetInt(const nlohmann::json &aObj, const char *aKey, int aDefault)
int AlignToFontH(const wxString &aAlign)
std::map< wxString, wxString > BuildV3LibraryItemMap(const V3_DOC_PARSER &aParser, const char *aIndexKey, const wxString &aDocType)
wxString V3GetString(const nlohmann::json &aObj, const char *aKey, const wxString &aDefault)
std::string GetTestDataRootDir()
PGM_BASE & Pgm()
The global program "get" accessor.
see class PGM_BASE
@ PIN_UP
The pin extends upwards from the connection point: Probably on the bottom side of the symbol.
Definition pin_type.h:123
@ PIN_RIGHT
The pin extends rightwards from the connection point.
Definition pin_type.h:107
@ PIN_LEFT
The pin extends leftwards from the connection point: Probably on the right side of the symbol.
Definition pin_type.h:114
@ PIN_DOWN
The pin extends downwards from the connection: Probably on the top side of the symbol.
Definition pin_type.h:131
@ L_BIDI
Definition sch_label.h:100
@ L_OUTPUT
Definition sch_label.h:99
@ L_INPUT
Definition sch_label.h:98
std::map< wxString, wxString > attributes
std::unique_ptr< LIB_SYMBOL > libSymbol
Raw parsed document from an EasyEDA Pro v3 .epru stream.
std::map< wxString, size_t > rowById
std::vector< V3_ROW > rows
One parsed row from an EasyEDA Pro v3 .epru document stream.
#define ALL_UNITS
Definition symbol.h:150
#define ALL_BODY_STYLES
Definition symbol.h:151
@ SYM_ORIENT_0
Definition symbol.h:36
@ VALUE
Field Value of part, i.e. "3.3K".
BOOST_AUTO_TEST_CASE(HorizontalAlignment)
BOOST_AUTO_TEST_SUITE(CadstarPartParser)
BOOST_CHECK(res.column_datatype(0)==SQL_NUMERIC||res.column_datatype(0)==SQL_DECIMAL)
BOOST_REQUIRE_EQUAL(rows.size(), 1)
BOOST_CHECK_EQUAL(std::any_cast< std::string >(rows[0].at("val")), "12345678901234567.89")
std::vector< DATABASE_CONNECTION::ROW > rows
BOOST_REQUIRE(res.columns()==1)
static wxString getEasyEdaProV3ArchivePath()
BOOST_AUTO_TEST_SUITE_END()
KIBIS_PIN * pin
VECTOR3I expected(15, 30, 45)
static std::vector< int > candidates(const SEGMENT_INDEX &aIndex, const SEG &aQuery, int aPadding)
VECTOR2I v3(-2, 1)
int delta
@ GR_TEXT_H_ALIGN_CENTER
@ GR_TEXT_V_ALIGN_CENTER
@ SCH_LINE_T
Definition typeinfo.h:159
@ SCH_SYMBOL_T
Definition typeinfo.h:168
@ SCH_LABEL_T
Definition typeinfo.h:163
@ SCH_TEXT_T
Definition typeinfo.h:147
@ SCH_GLOBAL_LABEL_T
Definition typeinfo.h:164
VECTOR2< int32_t > VECTOR2I
Definition vector2d.h:708