KiCad PCB EDA Suite
Loading...
Searching...
No Matches
easyedapro_import_utils.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) 2023 Alex Shvartzkop <[email protected]>
5 * Copyright The KiCad Developers, see AUTHORS.txt for contributors.
6 *
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version 2
10 * of the License, or (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program. If not, see <https://www.gnu.org/licenses/>.
19 */
20
22#include "easyedapro_parser.h"
24
26
27#include <algorithm>
28#include <core/map_helpers.h>
29#include <ki_exception.h>
30#include <string_utils.h>
31#include <vector>
32#include <json_common.h>
34
35#include <wx/log.h>
36#include <wx/stream.h>
37#include <wx/zipstrm.h>
38#include <wx/wfstream.h>
39#include <wx/mstream.h>
40#include <wx/txtstrm.h>
41#include <trace_helpers.h>
42
43
44namespace
45{
46
47// clang-format off
48static const std::vector<wxString> c_deviceAttributesWhitelist = { wxS( "Value" ),
49 wxS( "Datasheet" ),
50 wxS( "Manufacturer Part" ),
51 wxS( "Manufacturer" ),
52 wxS( "BOM_Manufacturer Part" ),
53 wxS( "BOM_Manufacturer" ),
54 wxS( "Supplier Part" ),
55 wxS( "Supplier" ),
56 wxS( "BOM_Supplier Part" ),
57 wxS( "BOM_Supplier" ),
58 wxS( "LCSC Part Name" ) };
59// clang-format on
60
61
62static std::string ToStdString( const wxString& aStr )
63{
64 return std::string( aStr.ToUTF8() );
65}
66
67
68static wxString ToWxString( const std::string& aStr )
69{
70 return wxString::FromUTF8( aStr.c_str() );
71}
72
73
74static nlohmann::json EmptyV3ProjectIndex()
75{
76 nlohmann::json project = nlohmann::json::object();
77 project["schematics"] = nlohmann::json::object();
78 project["boards"] = nlohmann::json::object();
79 project["pcbs"] = nlohmann::json::object();
80 project["symbols"] = nlohmann::json::object();
81 project["footprints"] = nlohmann::json::object();
82 project["devices"] = nlohmann::json::object();
83
84 return project;
85}
86
87
88static const nlohmann::json* FindMetaRow( const EASYEDAPRO::V3_DOC_RAW& aDoc )
89{
90 for( const EASYEDAPRO::V3_ROW& row : aDoc.rows )
91 {
92 if( row.type == wxS( "META" ) )
93 return &row.inner;
94 }
95
96 return nullptr;
97}
98
99
100static wxString MetaGetString( const EASYEDAPRO::V3_DOC_RAW& aDoc, const char* aKey,
101 const wxString& aDefault = wxEmptyString )
102{
103 if( const nlohmann::json* meta = FindMetaRow( aDoc ) )
104 return EASYEDAPRO::V3GetString( *meta, aKey, aDefault );
105
106 return aDefault;
107}
108
109
110static int MetaGetInt( const EASYEDAPRO::V3_DOC_RAW& aDoc, const char* aKey, int aDefault = 0 )
111{
112 if( const nlohmann::json* meta = FindMetaRow( aDoc ) )
113 return EASYEDAPRO::V3GetInt( *meta, aKey, aDefault );
114
115 return aDefault;
116}
117
118
119static nlohmann::json MetaGetValue( const EASYEDAPRO::V3_DOC_RAW& aDoc, const char* aKey,
120 const nlohmann::json& aDefault )
121{
122 if( const nlohmann::json* meta = FindMetaRow( aDoc ) )
123 {
124 auto it = meta->find( aKey );
125
126 if( it != meta->end() )
127 return *it;
128 }
129
130 return aDefault;
131}
132
133} // namespace
134
135
136wxString EASYEDAPRO::ShortenLibName( wxString aProjectName )
137{
138 wxString shortenedName = aProjectName;
139 shortenedName.Replace( wxS( "ProProject_" ), wxS( "" ) );
140 shortenedName.Replace( wxS( "ProDocument_" ), wxS( "" ) );
141 shortenedName = shortenedName.substr( 0, 10 );
142
143 return LIB_ID::FixIllegalChars( shortenedName + wxS( "-easyedapro" ), true );
144}
145
146
147LIB_ID EASYEDAPRO::ToKiCadLibID( const wxString& aLibName, const wxString& aLibReference )
148{
149 wxString libName = LIB_ID::FixIllegalChars( aLibName, true );
150 wxString libReference = EscapeString( aLibReference, CTX_LIBID );
151
152 wxString key = !libName.empty() ? ( libName + ':' + libReference ) : libReference;
153
154 LIB_ID libId;
155 libId.Parse( key, true );
156
157 return libId;
158}
159
160
161std::vector<IMPORT_PROJECT_DESC>
162EASYEDAPRO::ProjectToSelectorDialog( const nlohmann::json& aProject, bool aPcbOnly, bool aSchOnly )
163{
164 std::vector<IMPORT_PROJECT_DESC> result;
165
166 const nlohmann::json empty = nlohmann::json::object();
167
168 std::map<wxString, EASYEDAPRO::PRJ_SCHEMATIC> prjSchematics = aProject.value( "schematics", empty );
169 std::map<wxString, EASYEDAPRO::PRJ_BOARD> prjBoards = aProject.value( "boards", empty );
170
171 std::map<wxString, wxString> prjPcbNames;
172 std::map<wxString, nlohmann::json> prjPcbs = aProject.value( "pcbs", empty );
173 std::set<wxString> boardPcbs;
174 std::set<wxString> boardSchematics;
175
176 for( const auto& [pcbUuid, pcbJsonEntry] : prjPcbs )
177 {
178 if( pcbJsonEntry.is_string() )
179 prjPcbNames.emplace( pcbUuid, pcbJsonEntry );
180 else if( pcbJsonEntry.is_object() )
181 prjPcbNames.emplace( pcbUuid, pcbJsonEntry.at( "title" ) );
182 }
183
184 for( const auto& [prjName, board] : prjBoards )
185 {
187 desc.ComboName = desc.ComboId = prjName;
188 desc.PCBId = board.pcb;
189 desc.SchematicId = board.schematic;
190
191 auto pcbNameIt = prjPcbNames.find( desc.PCBId );
192 if( pcbNameIt != prjPcbNames.end() )
193 {
194 desc.PCBName = pcbNameIt->second;
195
196 if( desc.PCBName.empty() )
197 desc.PCBName = pcbNameIt->first;
198
199 boardPcbs.insert( desc.PCBId );
200 }
201
202 auto schIt = prjSchematics.find( desc.SchematicId );
203 if( schIt != prjSchematics.end() )
204 {
205 desc.SchematicName = schIt->second.name;
206
207 if( desc.SchematicName.empty() )
208 desc.SchematicName = schIt->first;
209
210 boardSchematics.insert( desc.SchematicId );
211 }
212
213 result.emplace_back( desc );
214 }
215
216 if( !aSchOnly )
217 {
218 for( const auto& [pcbId, pcbName] : prjPcbNames )
219 {
220 if( boardPcbs.contains( pcbId ) )
221 continue;
222
224 desc.PCBId = pcbId;
225 desc.PCBName = pcbName;
226
227 if( desc.PCBName.empty() )
228 desc.PCBName = pcbId;
229
230 result.emplace_back( desc );
231 }
232 }
233
234 if( !aPcbOnly )
235 {
236 for( const auto& [schId, schData] : prjSchematics )
237 {
238 if( boardSchematics.contains( schId ) )
239 continue;
240
242 desc.SchematicId = schId;
243 desc.SchematicName = schData.name;
244
245 if( desc.SchematicName.empty() )
246 desc.SchematicName = schId;
247
248 result.emplace_back( desc );
249 }
250 }
251
252 return result;
253}
254
255
256nlohmann::json EASYEDAPRO::FindJsonFile( const wxString& aZipFileName,
257 const std::set<wxString>& aFileNames )
258{
259 std::shared_ptr<wxZipEntry> entry;
260 wxFFileInputStream in( aZipFileName );
261 wxZipInputStream zip( in );
262
263 while( entry.reset( zip.GetNextEntry() ), entry.get() )
264 {
265 wxString name = entry->GetName();
266
267 try
268 {
269 if( aFileNames.find( name ) != aFileNames.end() )
270 {
271 wxMemoryOutputStream memos;
272 memos << zip;
273 wxStreamBuffer* buf = memos.GetOutputStreamBuffer();
274
275 wxString str = wxString::FromUTF8( (char*) buf->GetBufferStart(), buf->GetBufferSize() );
276
277 return nlohmann::json::parse( str );
278 }
279 }
280 catch( nlohmann::json::exception& e )
281 {
282 THROW_IO_ERRORF( _( "JSON error reading '%s': %s" ), name, e.what() );
283 }
284 catch( std::exception& e )
285 {
286 THROW_IO_ERRORF( _( "Error reading '%s': %s" ), name, e.what() );
287 }
288 }
289
290 return nlohmann::json{};
291}
292
293
294nlohmann::json EASYEDAPRO::ReadProjectOrDeviceFile( const wxString& aZipFileName )
295{
296 static const std::set<wxString> c_files = { wxS( "project.json" ), wxS( "device.json" ),
297 wxS( "footprint.json" ), wxS( "symbol.json" ) };
298
299 nlohmann::json j = FindJsonFile( aZipFileName, c_files );
300
301 if( !j.is_null() )
302 return j;
303
304 THROW_IO_ERRORF( _( "'%s' does not appear to be a valid EasyEDA (JLCEDA) Pro project or library file. "
305 "Cannot find project.json or device.json." ), aZipFileName );
306}
307
308
310 const wxString& aFileName,
311 std::function<bool( const wxString&, const wxString&, wxInputStream& )> aCallback )
312{
313 std::shared_ptr<wxZipEntry> entry;
314 wxFFileInputStream in( aFileName );
315 wxZipInputStream zip( in );
316
317 if( !zip.IsOk() )
318 THROW_IO_ERRORF( _( "Cannot read ZIP archive '%s'" ), aFileName );
319
320 while( entry.reset( zip.GetNextEntry() ), entry.get() )
321 {
322 wxString name = entry->GetName();
323 wxString baseName = name.AfterLast( '\\' ).AfterLast( '/' ).BeforeFirst( '.' );
324
325 try
326 {
327 if( aCallback( name, baseName, zip ) )
328 break;
329 }
330 catch( nlohmann::json::exception& e )
331 {
332 THROW_IO_ERRORF( _( "JSON error reading '%s': %s" ), name, e.what() );
333 }
334 catch( std::exception& e )
335 {
336 THROW_IO_ERRORF( _( "Error reading '%s': %s" ), name, e.what() );
337 }
338 }
339}
340
341
342std::vector<nlohmann::json> EASYEDAPRO::ParseJsonLines( wxInputStream& aInput, const wxString& aSource )
343{
344 // wxTextInputStream reads a byte at a time, so an unbuffered zip stream inflates per character
345 wxBufferedInputStream buffered( aInput, 64 * 1024 );
346 wxTextInputStream txt( buffered, wxS( " " ), wxConvUTF8 );
347
348 int currentLine = 1;
349
350 std::vector<nlohmann::json> lines;
351 while( buffered.CanRead() )
352 {
353 try
354 {
355 wxString line = txt.ReadLine();
356
357 if( !line.IsEmpty() )
358 {
359 nlohmann::json js = nlohmann::json::parse( line );
360 lines.emplace_back( js );
361 }
362 else
363 {
364 lines.emplace_back( nlohmann::json() );
365 }
366 }
367 catch( nlohmann::json::exception& e )
368 {
369 wxLogTrace( traceEasyEdaIo, wxT( "Cannot parse JSON line %d in '%s': %s" ),
370 currentLine, aSource, e.what() );
371 }
372
373 currentLine++;
374 }
375
376 return lines;
377}
378
379
380std::vector<std::vector<nlohmann::json>>
381EASYEDAPRO::ParseJsonLinesWithSeparation( wxInputStream& aInput, const wxString& aSource )
382{
383 wxBufferedInputStream buffered( aInput, 64 * 1024 );
384 wxTextInputStream txt( buffered, wxS( " " ), wxConvUTF8 );
385
386 int currentLine = 1;
387
388 std::vector<std::vector<nlohmann::json>> lineBlocks;
389 lineBlocks.emplace_back();
390
391 while( buffered.CanRead() )
392 {
393 try
394 {
395 wxString line = txt.ReadLine();
396
397 if( !line.IsEmpty() )
398 {
399 nlohmann::json js = nlohmann::json::parse( line );
400 lineBlocks.back().emplace_back( js );
401 }
402 else
403 {
404 lineBlocks.emplace_back();
405 }
406 }
407 catch( nlohmann::json::exception& e )
408 {
409 wxLogTrace( traceEasyEdaIo, wxT( "Cannot parse JSON line %d in '%s': %s" ),
410 currentLine, aSource, e.what() );
411 }
412
413 currentLine++;
414 }
415
416 return lineBlocks;
417}
418
419
420std::map<wxString, wxString>
421EASYEDAPRO::AnyMapToStringMap( const std::map<wxString, nlohmann::json>& aInput )
422{
423 std::map<wxString, wxString> stringMap;
424
425 for( auto& [key, value] : aInput )
426 {
427 if( value.is_string() )
428 stringMap[key] = value.get<wxString>();
429 else if( value.is_number() )
430 stringMap[key] = wxString::FromCDouble( value.get<double>() );
431 }
432
433 return stringMap;
434}
435
436
437nlohmann::json EASYEDAPRO::BuildV3ProjectIndexFromRawDocs( const V3_DOC_PARSER& aParser, bool aIncludeLibraryMetadata )
438{
439 nlohmann::json project = EmptyV3ProjectIndex();
440
441 struct SHEET_INFO
442 {
443 wxString uuid;
444 wxString name;
445 int zIndex = 0;
446 int order = 0;
447 };
448
449 std::map<wxString, std::vector<SHEET_INFO>> sheetsBySch;
450 int pageOrder = 0;
451
452 for( const auto& [uuid, pageDoc] : aParser.GetRawDocs( wxS( "SCH_PAGE" ) ) )
453 {
454 wxString schematic = MetaGetString( pageDoc, "schematic" );
455
456 if( schematic.empty() )
457 schematic = V3GetString( pageDoc.head, "schematic" );
458
459 if( schematic.empty() )
460 continue;
461
462 SHEET_INFO info;
463 info.uuid = uuid;
464 info.name = MetaGetString( pageDoc, "title", uuid );
465 info.zIndex = MetaGetInt( pageDoc, "zIndex", 0 );
466 info.order = pageOrder++;
467
468 sheetsBySch[schematic].push_back( std::move( info ) );
469 }
470
471 std::map<wxString, wxString> schematicsByBoard;
472
473 for( const auto& [uuid, schDoc] : aParser.GetRawDocs( wxS( "SCH" ) ) )
474 {
475 nlohmann::json sch = nlohmann::json::object();
476 sch["name"] = ToStdString( MetaGetString( schDoc, "title", uuid ) );
477 sch["sheets"] = nlohmann::json::array();
478
479 auto pagesIt = sheetsBySch.find( uuid );
480
481 if( pagesIt != sheetsBySch.end() )
482 {
483 auto& pages = pagesIt->second;
484
485 std::sort( pages.begin(), pages.end(),
486 []( const SHEET_INFO& aLeft, const SHEET_INFO& aRight )
487 {
488 if( aLeft.zIndex != aRight.zIndex )
489 return aLeft.zIndex < aRight.zIndex;
490
491 return aLeft.order < aRight.order;
492 } );
493
494 int sheetId = 1;
495
496 for( const SHEET_INFO& page : pages )
497 {
498 sch["sheets"].push_back( nlohmann::json::object( { { "id", sheetId++ },
499 { "name", ToStdString( page.name ) },
500 { "uuid", ToStdString( page.uuid ) } } ) );
501 }
502 }
503
504 project["schematics"][ToStdString( uuid )] = std::move( sch );
505
506 wxString board = MetaGetString( schDoc, "board" );
507
508 if( !board.empty() )
509 schematicsByBoard[board] = uuid;
510 }
511
512 std::map<wxString, wxString> boardTitles;
513
514 for( const auto& [uuid, boardDoc] : aParser.GetRawDocs( wxS( "BOARD" ) ) )
515 boardTitles[uuid] = MetaGetString( boardDoc, "title", uuid );
516
517 std::map<wxString, wxString> pcbsByBoard;
518
519 for( const auto& [uuid, pcbDoc] : aParser.GetRawDocs( wxS( "PCB" ) ) )
520 {
521 nlohmann::json pcb = nlohmann::json::object();
522 pcb["title"] = ToStdString( MetaGetString( pcbDoc, "title", uuid ) );
523
524 project["pcbs"][ToStdString( uuid )] = std::move( pcb );
525
526 wxString board = MetaGetString( pcbDoc, "board" );
527
528 if( !board.empty() )
529 pcbsByBoard[board] = uuid;
530 }
531
532 std::set<wxString> allBoardRefs;
533
534 for( const auto& [boardRef, schUuid] : schematicsByBoard )
535 allBoardRefs.insert( boardRef );
536
537 for( const auto& [boardRef, pcbUuid] : pcbsByBoard )
538 allBoardRefs.insert( boardRef );
539
540 for( const wxString& boardRef : allBoardRefs )
541 {
542 wxString boardName = boardRef;
543
544 if( auto it = boardTitles.find( boardRef ); it != boardTitles.end() )
545 boardName = it->second;
546
547 if( boardName.empty() )
548 boardName = boardRef;
549
550 nlohmann::json board = nlohmann::json::object();
551 auto schIt = schematicsByBoard.find( boardRef );
552 auto pcbIt = pcbsByBoard.find( boardRef );
553
554 board["schematic"] = schIt != schematicsByBoard.end() ? ToStdString( schIt->second ) : "";
555 board["pcb"] = pcbIt != pcbsByBoard.end() ? ToStdString( pcbIt->second ) : "";
556
557 project["boards"][ToStdString( boardName )] = std::move( board );
558 }
559
560 if( project["boards"].empty() && project["pcbs"].is_object() && project["pcbs"].size() == 1
561 && project["schematics"].is_object() && project["schematics"].size() == 1 )
562 {
563 auto pcbIt = project["pcbs"].begin();
564 auto schIt = project["schematics"].begin();
565
566 wxString pcbId = wxString::FromUTF8( pcbIt.key() );
567 wxString schId = wxString::FromUTF8( schIt.key() );
568 wxString boardName = wxString::FromUTF8( pcbIt.value().value( "title", pcbIt.key() ) );
569
570 if( boardName.empty() )
571 boardName = schId;
572
573 nlohmann::json board = nlohmann::json::object();
574 board["schematic"] = ToStdString( schId );
575 board["pcb"] = ToStdString( pcbId );
576
577 project["boards"][ToStdString( boardName )] = std::move( board );
578 }
579
580 if( !aIncludeLibraryMetadata )
581 return project;
582
583 for( const auto& [uuid, symDoc] : aParser.GetRawDocs( wxS( "SYMBOL" ) ) )
584 {
585 nlohmann::json sym = nlohmann::json::object();
586 sym["source"] = ToStdString( MetaGetString( symDoc, "source" ) );
587 sym["description"] = ToStdString( MetaGetString( symDoc, "description" ) );
588 sym["title"] = ToStdString( MetaGetString( symDoc, "title", uuid ) );
589 sym["display_title"] = sym["title"];
590 sym["version"] = "3";
591 sym["type"] = MetaGetInt( symDoc, "docType", static_cast<int>( SYMBOL_TYPE::NORMAL ) );
592 sym["tags"] = MetaGetValue( symDoc, "tags", nlohmann::json::object() );
593
594 project["symbols"][ToStdString( uuid )] = std::move( sym );
595 }
596
597 for( const auto& [uuid, fpDoc] : aParser.GetRawDocs( wxS( "FOOTPRINT" ) ) )
598 {
599 nlohmann::json fp = nlohmann::json::object();
600 fp["source"] = ToStdString( MetaGetString( fpDoc, "source" ) );
601 fp["description"] = ToStdString( MetaGetString( fpDoc, "description" ) );
602 fp["title"] = ToStdString( MetaGetString( fpDoc, "title", uuid ) );
603 fp["display_title"] = fp["title"];
604 fp["version"] = "3";
605 fp["type"] = static_cast<int>( FOOTPRINT_TYPE::NORMAL );
606 fp["tags"] = MetaGetValue( fpDoc, "tags", nlohmann::json::object() );
607
608 project["footprints"][ToStdString( uuid )] = std::move( fp );
609 }
610
611 for( const auto& [uuid, deviceDoc] : aParser.GetRawDocs( wxS( "DEVICE" ) ) )
612 {
613 nlohmann::json dev = nlohmann::json::object();
614 dev["source"] = ToStdString( MetaGetString( deviceDoc, "source" ) );
615 dev["description"] = ToStdString( MetaGetString( deviceDoc, "description" ) );
616 dev["title"] = ToStdString( MetaGetString( deviceDoc, "title", uuid ) );
617 dev["version"] = "3";
618 dev["tags"] = MetaGetValue( deviceDoc, "tags", nlohmann::json::object() );
619 dev["attributes"] = MetaGetValue( deviceDoc, "attributes", nlohmann::json::object() );
620
621 project["devices"][ToStdString( uuid )] = std::move( dev );
622 }
623
624 return project;
625}
626
627
628std::map<wxString, EASYEDAPRO::BLOB> EASYEDAPRO::BuildV3BlobMap( const V3_DOC_PARSER& aParser )
629{
630 std::map<wxString, BLOB> blobs;
631
632 for( const auto& [blobDocUuid, rawDoc] : aParser.GetRawDocs( wxS( "BLOB" ) ) )
633 {
634 for( const V3_ROW& row : rawDoc.rows )
635 {
636 if( row.type != wxS( "BLOB" ) )
637 continue;
638
639 try
640 {
641 BLOB blob;
642 blob.objectId = V3GetString( row.outer, "id" );
643 blob.url = V3GetString( row.inner, "content" );
644 blobs[blob.objectId] = blob;
645 }
646 catch( nlohmann::json::exception& e )
647 {
648 wxLogTrace( traceEasyEdaIo, wxT( "EasyEDA Pro v3 blob in '%s' was skipped due to parse error: %s" ),
649 blobDocUuid, e.what() );
650 }
651 }
652 }
653
654 return blobs;
655}
656
657
658wxString EASYEDAPRO::GetV3LibraryItemTitle( const nlohmann::json& aMetadata, const wxString& aUuid )
659{
660 wxString title = EASYEDAPRO::V3GetString( aMetadata, "display_title" );
661
662 if( title.empty() )
663 title = EASYEDAPRO::V3GetString( aMetadata, "title" );
664
665 if( title.empty() )
666 title = aUuid;
667
668 return title;
669}
670
671
672wxString EASYEDAPRO::KeywordsFromV3Tags( const nlohmann::json& aTags )
673{
674 if( !aTags.is_object() )
675 return {};
676
677 wxString keywords;
678
679 auto appendTagName = [&]( const char* aKey )
680 {
681 if( !aTags.contains( aKey ) || !aTags.at( aKey ).is_object() )
682 return;
683
684 wxString name = V3GetString( aTags.at( aKey ), "name" );
685
686 if( name.empty() )
687 return;
688
689 if( !keywords.empty() )
690 keywords += wxS( " " );
691
692 keywords += name;
693 };
694
695 appendTagName( "parent_tag" );
696 appendTagName( "child_tag" );
697
698 return keywords;
699}
700
701
702wxString EASYEDAPRO::ResolveDeviceFieldVariables( const wxString& aInput,
703 const std::map<wxString, wxString>& aDeviceAttributes )
704{
705 wxString inputText = aInput;
706 wxString resolvedText;
707 int variableCount = 0;
708
709 // Resolve variables: ={Variable1}text{Variable2}
710 do
711 {
712 if( !inputText.StartsWith( wxS( "={" ) ) )
713 return inputText;
714
715 resolvedText.Clear();
716 variableCount = 0;
717
718 for( size_t i = 1; i < inputText.size(); )
719 {
720 wxUniChar c = inputText[i++];
721
722 if( c == '{' )
723 {
724 wxString varName;
725 bool endFound = false;
726
727 while( i < inputText.size() )
728 {
729 c = inputText[i++];
730
731 if( c == '}' )
732 {
733 endFound = true;
734 break;
735 }
736
737 varName << c;
738 }
739
740 if( !endFound )
741 return inputText;
742
743 wxString varValue =
744 get_def( aDeviceAttributes, varName, wxString::Format( wxS( "{%s!}" ), varName ) );
745
746 resolvedText << varValue;
747 variableCount++;
748 }
749 else
750 {
751 resolvedText << c;
752 }
753 }
754
755 inputText = resolvedText;
756 } while( variableCount > 0 );
757
758 return resolvedText;
759}
760
761
762wxString EASYEDAPRO::NormalizeEasyEDAText( wxString aText )
763{
764 // ℃ -> °C
765 aText.Replace( wxS( "\u2103" ), wxS( "\u00B0C" ), true );
766 return aText;
767}
768
769
770wxString EASYEDAPRO::MakeUniqueLibName( std::set<wxString>& aUsedNames, const wxString& aName,
771 const wxString& aFallback )
772{
773 wxString uniqueBase = aName.empty() ? aFallback : aName;
774 wxString uniqueName = uniqueBase;
775 int suffix = 2;
776
777 while( aUsedNames.contains( uniqueName ) )
778 uniqueName = uniqueBase + wxString::Format( wxS( "_%d" ), suffix++ );
779
780 aUsedNames.insert( uniqueName );
781 return uniqueName;
782}
783
784
785std::map<wxString, wxString> EASYEDAPRO::BuildV3DeviceLibNames( nlohmann::json& aProject,
786 const std::map<wxString, PRJ_DEVICE>& aDevices,
787 std::set<wxString>* aUsedNames )
788{
789 std::map<wxString, wxString> deviceLibNames;
790 std::set<wxString> localUsedNames;
791 std::set<wxString>& usedLibNames = aUsedNames ? *aUsedNames : localUsedNames;
792 nlohmann::json deviceLibNamesJson = nlohmann::json::object();
793
794 for( const auto& [devUuid, device] : aDevices )
795 {
796 wxString libNameForDevice = MakeUniqueLibName( usedLibNames, device.title, devUuid );
797 deviceLibNames[devUuid] = libNameForDevice;
798 deviceLibNamesJson[std::string( devUuid.ToUTF8() )] = std::string( libNameForDevice.ToUTF8() );
799 }
800
801 aProject["device_lib_names"] = std::move( deviceLibNamesJson );
802 return deviceLibNames;
803}
804
805
806wxString EASYEDAPRO::LookupV3DeviceLibName( const nlohmann::json& aProject, const wxString& aDeviceUuid )
807{
808 if( aDeviceUuid.empty() || !aProject.contains( "device_lib_names" )
809 || !aProject.at( "device_lib_names" ).is_object() )
810 {
811 return {};
812 }
813
814 std::string deviceIdUtf8 = std::string( aDeviceUuid.ToUTF8() );
815 const auto& names = aProject.at( "device_lib_names" );
816
817 if( names.contains( deviceIdUtf8 ) && names.at( deviceIdUtf8 ).is_string() )
818 return names.at( deviceIdUtf8 ).get<wxString>();
819
820 return {};
821}
822
823
825 const wxString& aDeviceUuid )
826{
827 V3_DEVICE_DATA data;
828
829 if( aDeviceUuid.empty() || !aProject.contains( "devices" ) || !aProject.at( "devices" ).is_object() )
830 return data;
831
832 std::string deviceIdUtf8 = std::string( aDeviceUuid.ToUTF8() );
833
834 if( !aProject.at( "devices" ).contains( deviceIdUtf8 ) )
835 return data;
836
837 const nlohmann::json& dev = aProject.at( "devices" ).at( deviceIdUtf8 );
838
839 data.found = true;
840 data.description = V3GetString( dev, "description" );
841
842 if( dev.contains( "attributes" ) && dev.at( "attributes" ).is_object() )
843 {
844 for( const auto& [key, value] : dev.at( "attributes" ).items() )
845 data.attributes[wxString::FromUTF8( key )] = V3JsonToString( value );
846 }
847
848 return data;
849}
850
851
852wxString EASYEDAPRO::ResolveV3DeviceValueText( const std::map<wxString, wxString>& aDeviceAttributes )
853{
854 wxString valueText = get_def( aDeviceAttributes, wxS( "Value" ), wxEmptyString );
855
856 if( valueText.empty() )
857 valueText = get_def( aDeviceAttributes, wxS( "Name" ), wxEmptyString );
858
859 return NormalizeEasyEDAText( ResolveDeviceFieldVariables( valueText, aDeviceAttributes ) );
860}
861
862
864 const std::map<wxString, wxString>& aDeviceAttributes, bool aIncludeValue,
865 const std::function<void( const wxString& aKey, const wxString& aValue )>& aCallback )
866{
867 for( const wxString& attrKey : c_deviceAttributesWhitelist )
868 {
869 if( !aIncludeValue && attrKey == wxS( "Value" ) )
870 continue;
871
872 auto valOpt = get_opt( aDeviceAttributes, attrKey );
873
874 if( !valOpt || valOpt->empty() )
875 continue;
876
877 aCallback( attrKey,
878 NormalizeEasyEDAText( ResolveDeviceFieldVariables( *valOpt, aDeviceAttributes ) ) );
879 }
880}
881
882
883template <typename Map>
884static wxString MakeUniqueV3LibraryName( const Map& aItems, const wxString& aName, const wxString& aFallback )
885{
886 wxString uniqueBase = aName.empty() ? aFallback : aName;
887 wxString uniqueName = uniqueBase;
888
889 int suffix = 2;
890 while( aItems.contains( uniqueName ) )
891 uniqueName = uniqueBase + wxString::Format( wxS( "_%d" ), suffix++ );
892
893 return uniqueName;
894}
895
896
897std::map<wxString, EASYEDAPRO::V3_SYMBOL_LIB_ITEM> EASYEDAPRO::BuildV3SymbolLibraryMap( const V3_DOC_PARSER& aParser )
898{
899 std::map<wxString, V3_SYMBOL_LIB_ITEM> items;
900 const nlohmann::json& index = aParser.GetLibraryIndex();
901
902 const bool hasDevices = index.is_object() && index.contains( "devices" ) && index.at( "devices" ).is_object()
903 && !index.at( "devices" ).empty();
904
905 if( hasDevices )
906 {
907 try
908 {
909 for( const auto& [devUuidKey, deviceJson] : index.at( "devices" ).items() )
910 {
911 if( !deviceJson.is_object() )
912 continue;
913
914 PRJ_DEVICE device = deviceJson;
915 auto symbolIt = device.attributes.find( wxS( "Symbol" ) );
916
917 if( symbolIt == device.attributes.end() || symbolIt->second.empty() )
918 continue;
919
920 if( !aParser.FindRawDoc( wxS( "SYMBOL" ), symbolIt->second ) )
921 continue;
922
923 wxString deviceUuid = V3GetString( deviceJson, "uuid", ToWxString( devUuidKey ) );
924 wxString name = device.title.empty() ? GetV3LibraryItemTitle( deviceJson, deviceUuid ) : device.title;
926 item.symbolUuid = symbolIt->second;
927 item.device = std::move( device );
928 item.hasDevice = true;
929
930 items.emplace( MakeUniqueV3LibraryName( items, name, deviceUuid ), std::move( item ) );
931 }
932 }
933 catch( nlohmann::json::exception& e )
934 {
935 wxLogTrace( traceEasyEdaIo, wxT( "Failed to parse EasyEDA Pro v3 device metadata: %s" ), e.what() );
936 return {};
937 }
938
939 return items;
940 }
941
942 for( const auto& [name, symbolUuid] : BuildV3LibraryItemMap( aParser, "symbols", wxS( "SYMBOL" ) ) )
943 {
945 item.symbolUuid = symbolUuid;
946 items.emplace( name, std::move( item ) );
947 }
948
949 return items;
950}
951
952
953std::map<wxString, wxString> EASYEDAPRO::BuildV3LibraryItemMap( const V3_DOC_PARSER& aParser, const char* aIndexKey,
954 const wxString& aDocType )
955{
956 std::map<wxString, wxString> titlesByUuid;
957 const nlohmann::json& index = aParser.GetLibraryIndex();
958
959 if( index.is_object() && index.contains( aIndexKey ) && index.at( aIndexKey ).is_object() )
960 {
961 for( const auto& [uuidKey, metadata] : index.at( aIndexKey ).items() )
962 {
963 wxString uuid = V3GetString( metadata, "uuid", ToWxString( uuidKey ) );
964 titlesByUuid[uuid] = GetV3LibraryItemTitle( metadata, uuid );
965 }
966 }
967
968 std::map<wxString, wxString> nameToUuid;
969
970 for( const auto& [uuid, rawDoc] : aParser.GetRawDocs( aDocType ) )
971 {
972 (void) rawDoc;
973
974 wxString name = uuid;
975
976 if( auto it = titlesByUuid.find( uuid ); it != titlesByUuid.end() )
977 name = it->second;
978
979 nameToUuid[MakeUniqueV3LibraryName( nameToUuid, name, uuid )] = uuid;
980 }
981
982 return nameToUuid;
983}
int index
const char * name
Parses EasyEDA Pro v3 .epro2 archives.
const V3_DOC_RAW * FindRawDoc(const wxString &aDocType, const wxString &aUuid) const
const std::map< wxString, V3_DOC_RAW > & GetRawDocs(const wxString &aDocType) const
const nlohmann::json & GetLibraryIndex() const
A logical library item identifier and consists of various portions much like a URI.
Definition lib_id.h:45
int Parse(const UTF8 &aId, bool aFix=false)
Parse LIB_ID with the information from aId.
Definition lib_id.cpp:65
static UTF8 FixIllegalChars(const UTF8 &aLibItemName, bool aLib)
Replace illegal LIB_ID item name characters with underscores '_'.
Definition lib_id.cpp:205
static bool empty(const wxTextEntryBase *aCtrl)
static wxString MakeUniqueV3LibraryName(const Map &aItems, const wxString &aName, const wxString &aFallback)
static std::string ToStdString(const wxString &aStr)
#define _(s)
#define THROW_IO_ERRORF(msg,...)
const wxChar *const traceEasyEdaIo
wxString get_def(const std::map< wxString, wxString > &aMap, const char *aKey, const char *aDefval="")
Definition map_helpers.h:60
std::optional< V > get_opt(const std::map< wxString, V > &aMap, const wxString &aKey)
Definition map_helpers.h:30
nlohmann::json BuildV3ProjectIndexFromRawDocs(const V3_DOC_PARSER &aParser, bool aIncludeLibraryMetadata=true)
Build a minimal legacy-style project index from parsed v3 raw documents.
LIB_ID ToKiCadLibID(const wxString &aLibName, const wxString &aLibReference)
wxString MakeUniqueLibName(std::set< wxString > &aUsedNames, const wxString &aName, const wxString &aFallback)
Allocate a unique library item name, recording it in aUsedNames.
wxString KeywordsFromV3Tags(const nlohmann::json &aTags)
Build KiCad keywords from a v3 tags object (parent_tag / child_tag name fields).
wxString GetV3LibraryItemTitle(const nlohmann::json &aMetadata, const wxString &aUuid)
void ForEachImportedDeviceField(const std::map< wxString, wxString > &aDeviceAttributes, bool aIncludeValue, const std::function< void(const wxString &aKey, const wxString &aValue)> &aCallback)
Invoke aCallback for each non-empty whitelisted Device field (resolved + normalized).
wxString ResolveDeviceFieldVariables(const wxString &aInput, const std::map< wxString, wxString > &aDeviceAttributes)
Resolve EasyEDA ={Var} / ={A}text{B} field expressions against device attributes.
void IterateZipFiles(const wxString &aFileName, std::function< bool(const wxString &, const wxString &, wxInputStream &)> aCallback)
std::vector< nlohmann::json > ParseJsonLines(wxInputStream &aInput, const wxString &aSource)
wxString NormalizeEasyEDAText(wxString aText)
Replace EasyEDA temperature glyph (℃) with °C.
V3_DEVICE_DATA GetV3DeviceData(const nlohmann::json &aProject, const wxString &aDeviceUuid)
std::vector< std::vector< nlohmann::json > > ParseJsonLinesWithSeparation(wxInputStream &aInput, const wxString &aSource)
Multiple document types (e.g.
wxString LookupV3DeviceLibName(const nlohmann::json &aProject, const wxString &aDeviceUuid)
nlohmann::json FindJsonFile(const wxString &aZipFileName, const std::set< wxString > &aFileNames)
std::vector< IMPORT_PROJECT_DESC > ProjectToSelectorDialog(const nlohmann::json &aProject, bool aPcbOnly=false, bool aSchOnly=false)
List the project's board entries, then the PCBs and schematics no entry names.
nlohmann::json ReadProjectOrDeviceFile(const wxString &aZipFileName)
wxString ShortenLibName(wxString aProjectName)
std::map< wxString, wxString > AnyMapToStringMap(const std::map< wxString, nlohmann::json > &aInput)
wxString V3JsonToString(const nlohmann::json &aValue, const wxString &aDefault)
wxString ResolveV3DeviceValueText(const std::map< wxString, wxString > &aDeviceAttributes)
Preferred Value text: Value attribute, else Name, with variables resolved.
int V3GetInt(const nlohmann::json &aObj, const char *aKey, int aDefault)
std::map< wxString, V3_SYMBOL_LIB_ITEM > BuildV3SymbolLibraryMap(const V3_DOC_PARSER &aParser)
std::map< wxString, wxString > BuildV3LibraryItemMap(const V3_DOC_PARSER &aParser, const char *aIndexKey, const wxString &aDocType)
std::map< wxString, BLOB > BuildV3BlobMap(const V3_DOC_PARSER &aParser)
wxString V3GetString(const nlohmann::json &aObj, const char *aKey, const wxString &aDefault)
std::map< wxString, wxString > BuildV3DeviceLibNames(nlohmann::json &aProject, const std::map< wxString, PRJ_DEVICE > &aDevices, std::set< wxString > *aUsedNames=nullptr)
Stable project-lib item names keyed by Device UUID (one entry per Device).
wxString EscapeString(const wxString &aSource, ESCAPE_CONTEXT aContext)
The Escape/Unescape routines use HTML-entity-reference-style encoding to handle characters which are:...
@ CTX_LIBID
std::map< wxString, wxString > attributes
std::map< wxString, wxString > attributes
Raw parsed document from an EasyEDA Pro v3 .epru stream.
std::vector< V3_ROW > rows
One parsed row from an EasyEDA Pro v3 .epru document stream.
Build the schematic-library name map for a v3 .elibz2.
Describes how non-KiCad boards and schematics should be imported as KiCad projects.
wxString result
Test unit parsing edge cases and error handling.
wxLogTrace helper definitions.