KiCad PCB EDA Suite
Loading...
Searching...
No Matches
pcb_io_easyedapro.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
23#include <memory>
26#include <pcb_io/pcb_io.h>
27
28#include <board.h>
29#include <font/fontconfig.h>
30#include <footprint.h>
31#include <progress_reporter.h>
32#include <common.h>
33#include <macros.h>
34#include <reporter.h>
35
36#include <fstream>
37#include <wx/txtstrm.h>
38#include <wx/wfstream.h>
39#include <wx/mstream.h>
40#include <wx/zipstrm.h>
41#include <wx/log.h>
42
43#include <json_common.h>
45#include <core/map_helpers.h>
46
47
49{
50 std::map<wxString, std::unique_ptr<FOOTPRINT>> m_Footprints;
51 std::map<wxString, EASYEDAPRO::BLOB> m_Blobs;
52 std::map<wxString, std::multimap<wxString, EASYEDAPRO::POURED>> m_Poured;
53};
54
55
56PCB_IO_EASYEDAPRO::PCB_IO_EASYEDAPRO() : PCB_IO( wxS( "EasyEDA (JLCEDA) Professional" ) )
57{
58}
59
60
66
67
68bool PCB_IO_EASYEDAPRO::CanReadBoard( const wxString& aFileName ) const
69{
70 if( aFileName.Lower().EndsWith( wxS( ".epro" ) ) )
71 {
72 return true;
73 }
74 else if( aFileName.Lower().EndsWith( wxS( ".zip" ) ) )
75 {
76 std::shared_ptr<wxZipEntry> entry;
77 wxFFileInputStream in( aFileName );
78 wxZipInputStream zip( in );
79
80 if( !zip.IsOk() )
81 return false;
82
83 while( entry.reset( zip.GetNextEntry() ), entry.get() != NULL )
84 {
85 wxString name = entry->GetName();
86
87 if( name == wxS( "project.json" ) )
88 return true;
89 }
90
91 return false;
92 }
93
94 return false;
95}
96
97
98std::vector<std::pair<wxString, wxString>>
99PCB_IO_EASYEDAPRO::EnumerateProjectBoards( const wxString& aFileName ) const
100{
101 const nlohmann::json& projectIndex = EASYEDAPRO::ReadProjectOrDeviceFile( aFileName );
102 const nlohmann::json& pcbsIndex = projectIndex.at( "pcbs" );
103
104 std::vector<std::pair<wxString, wxString>> result;
105 result.reserve( pcbsIndex.size() );
106
107 for( const auto& [uuid, entry] : pcbsIndex.items() )
108 {
109 wxString title = wxString::FromUTF8( uuid );
110
111 if( entry.is_string() && !entry.get<std::string>().empty() )
112 title = wxString::FromUTF8( entry.get_ref<const std::string&>() );
113 else if( entry.is_object() )
114 {
115 auto titleIt = entry.find( "title" );
116
117 if( titleIt != entry.end() && titleIt->is_string() )
118 title = wxString::FromUTF8( titleIt->get_ref<const std::string&>() );
119 }
120
121 result.emplace_back( wxString::FromUTF8( uuid ), title );
122 }
123
124 return result;
125}
126
127
128std::vector<IMPORT_PROJECT_DESC> PCB_IO_EASYEDAPRO::EnumerateProjectEntries( const wxString& aFileName ) const
129{
131}
132
133
134void PCB_IO_EASYEDAPRO::loadBoard( const wxString& aFileName, BOARD& aBoard, bool aIsNewLoad,
135 const std::map<std::string, UTF8>* aProperties, PROJECT* aProject )
136{
137 m_props = aProperties;
138 m_importedDesignRules.clear();
139
140 m_board = &aBoard;
141
142 // Collect the font substitution warnings (RAII - automatically reset on scope exit)
144
146 {
147 m_progressReporter->Report( wxString::Format( _( "Loading %s..." ), aFileName ) );
148
149 if( !m_progressReporter->KeepRefreshing() )
151 }
152
153 PCB_IO_EASYEDAPRO_PARSER parser( nullptr, nullptr );
154
155 wxFileName fname( aFileName );
156 wxString fpLibName = EASYEDAPRO::ShortenLibName( fname.GetName() );
157
158 if( fname.GetExt() == wxS( "epro" ) || fname.GetExt() == wxS( "zip" ) )
159 {
160 nlohmann::json project = EASYEDAPRO::ReadProjectOrDeviceFile( aFileName );
161
162 wxString pcbToLoad;
163
164 if( m_props && m_props->contains( "pcb_id" ) )
165 {
166 pcbToLoad = wxString::FromUTF8( m_props->at( "pcb_id" ) );
167 }
168 else
169 {
170
171 std::map<wxString, nlohmann::json> prjPcbData = project.at( "pcbs" );
172
173 if( prjPcbData.size() == 1 )
174 {
175 pcbToLoad = prjPcbData.begin()->first;
176 }
177 else if( m_choose_project_handler )
178 {
179 std::vector<IMPORT_PROJECT_DESC> chosen = m_choose_project_handler(
181
182 // Quiet exit on cancel
183 if( chosen.size() == 0 )
185
186 pcbToLoad = chosen[0].PCBId;
187 }
188 else if( !prjPcbData.empty() )
189 {
190 // No chooser registered (headless), so take the first
191 pcbToLoad = prjPcbData.begin()->first;
192 }
193 }
194
195 if( pcbToLoad.empty() )
196 THROW_IO_ERROR( _( "No PCB was found in the project to import." ) );
197
198 LoadAllDataFromProject( aFileName, project );
199
200 if( !m_projectData )
201 THROW_IO_ERROR( _( "Failed to load the project data." ) );
202
203 auto cb = [&]( const wxString& name, const wxString& pcbUuid, wxInputStream& zip ) -> bool
204 {
205 if( !name.EndsWith( wxS( ".epcb" ) ) )
207
208 if( pcbUuid != pcbToLoad )
210
211 std::vector<nlohmann::json>* pcbLines = nullptr;
212
213 std::vector<std::vector<nlohmann::json>> lineBlocks =
215
216 if( lineBlocks.empty() )
218
219 if( lineBlocks.size() > 1 )
220 {
221 for( std::vector<nlohmann::json>& block : lineBlocks )
222 {
223 wxString docType;
224 nlohmann::json headData;
225
226 for( const nlohmann::json& line : block )
227 {
228 if( line.size() < 2 )
229 continue;
230
231 if( !line.at( 0 ).is_string() )
232 continue;
233
234 wxString lineType = line.at( 0 ).get<wxString>();
235
236 if( lineType == wxS( "DOCTYPE" ) )
237 {
238 if( !line.at( 1 ).is_string() )
239 continue;
240
241 docType = line.at( 1 ).get<wxString>();
242 }
243 else if( lineType == wxS( "HEAD" ) )
244 {
245 if( !line.at( 1 ).is_object() )
246 continue;
247
248 headData = line.at( 1 );
249 }
250 }
251
252 if( docType == wxS( "FOOTPRINT" ) )
253 {
254 wxString fpUuid = headData.at( "uuid" );
255 wxString fpTitle = headData.at( "title" );
256
257 std::unique_ptr<FOOTPRINT> footprint = parser.ParseFootprint( project, fpUuid, block );
258
259 if( !footprint )
261
262 LIB_ID fpID = EASYEDAPRO::ToKiCadLibID( fpLibName, fpTitle );
263 footprint->SetFPID( fpID );
264
265 m_projectData->m_Footprints.emplace( fpUuid, std::move( footprint ) );
266 }
267 else if( docType == wxS( "PCB" ) )
268 {
269 pcbLines = &block;
270 }
271 }
272 }
273
274 if( pcbLines == nullptr )
275 pcbLines = &lineBlocks[0];
276
277 wxString boardKey = pcbUuid + wxS( "_0" );
278 wxScopedCharBuffer boardKeyBuffer = boardKey.ToUTF8();
279 wxString boardPouredKey = wxBase64Encode( boardKeyBuffer.data(), boardKeyBuffer.length() );
280
281 const std::multimap<wxString, EASYEDAPRO::POURED>& boardPoured =
282 get_def( m_projectData->m_Poured, boardPouredKey );
283
284 parser.ParseBoard( m_board, project, m_projectData->m_Footprints,
285 m_projectData->m_Blobs, boardPoured, *pcbLines,
286 EASYEDAPRO::ShortenLibName( fname.GetName() ) );
288
289
291 };
292 EASYEDAPRO::IterateZipFiles( aFileName, cb );
293 }
294}
295
296
297long long PCB_IO_EASYEDAPRO::GetLibraryTimestamp( const wxString& aLibraryPath ) const
298{
299 return 0;
300}
301
302
303void PCB_IO_EASYEDAPRO::FootprintEnumerate( wxArrayString& aFootprintNames,
304 const wxString& aLibraryPath, bool aBestEfforts,
305 const std::map<std::string, UTF8>* aProperties )
306{
307 wxFileName fname( aLibraryPath );
308
309 if( fname.GetExt() == wxS( "efoo" ) )
310 {
311 wxFFileInputStream ffis( aLibraryPath );
312 wxTextInputStream txt( ffis, wxS( " " ), wxConvUTF8 );
313
314 while( ffis.CanRead() )
315 {
316 wxString line = txt.ReadLine();
317
318 if( !line.Contains( wxS( "ATTR" ) ) )
319 continue; // Don't bother parsing
320
321 nlohmann::json js = nlohmann::json::parse( line );
322 if( js.at( 0 ) == "ATTR" && js.at( 7 ) == "Footprint" )
323 {
324 aFootprintNames.Add( js.at( 8 ).get<wxString>() );
325 }
326 }
327 }
328 else if( fname.GetExt() == wxS( "elibz" ) || fname.GetExt() == wxS( "epro" )
329 || fname.GetExt() == wxS( "zip" ) )
330 {
331 nlohmann::json project = EASYEDAPRO::ReadProjectOrDeviceFile( aLibraryPath );
332 std::map<wxString, nlohmann::json> footprintMap = project.at( "footprints" );
333
334 for( auto& [key, value] : footprintMap )
335 {
336 wxString title;
337
338 if( value.contains( "display_title" ) )
339 title = value.at( "display_title" ).get<wxString>();
340 else
341 title = value.at( "title" ).get<wxString>();
342
343 aFootprintNames.Add( title );
344 }
345 }
346}
347
348
349void PCB_IO_EASYEDAPRO::LoadAllDataFromProject( const wxString& aProjectPath,
350 const nlohmann::json& aProject )
351{
352 if( m_projectData )
353 delete m_projectData;
354
355 m_projectData = new PRJ_DATA();
356
357 PCB_IO_EASYEDAPRO_PARSER parser( nullptr, nullptr );
358 wxFileName fname( aProjectPath );
359 wxString fpLibName = EASYEDAPRO::ShortenLibName( fname.GetName() );
360
361 std::map<wxString, std::unique_ptr<FOOTPRINT>> result;
362
363 auto cb = [&]( const wxString& name, const wxString& baseName, wxInputStream& zip ) -> bool
364 {
365 if( !name.EndsWith( wxS( ".efoo" ) ) && !name.EndsWith( wxS( ".eblob" ) )
366 && !name.EndsWith( wxS( ".ecop" ) ) )
367 {
369 }
370
371 std::vector<nlohmann::json> lines = EASYEDAPRO::ParseJsonLines( zip, name );
372
373 if( name.EndsWith( wxS( ".efoo" ) ) )
374 {
375 nlohmann::json fpData = aProject.at( "footprints" ).at( baseName );
376 wxString fpTitle = fpData.at( "title" );
377
378 std::unique_ptr<FOOTPRINT> footprint = parser.ParseFootprint( aProject, baseName, lines );
379
380 if( !footprint )
382
383 LIB_ID fpID = EASYEDAPRO::ToKiCadLibID( fpLibName, fpTitle );
384 footprint->SetFPID( fpID );
385
386 m_projectData->m_Footprints.emplace( baseName, std::move( footprint ) );
387 }
388 else if( name.EndsWith( wxS( ".eblob" ) ) )
389 {
390 for( const nlohmann::json& line : lines )
391 {
392 if( line.at( 0 ) == "BLOB" )
393 {
394 EASYEDAPRO::BLOB blob = line;
395 m_projectData->m_Blobs[blob.objectId] = blob;
396 }
397 }
398 }
399 else if( name.EndsWith( wxS( ".ecop" ) ) && EASYEDAPRO::IMPORT_POURED_ECOP )
400 {
401 for( const nlohmann::json& line : lines )
402 {
403 if( line.at( 0 ) == "POURED" )
404 {
405 if( !line.at( 2 ).is_string() )
406 continue; // Unknown type of POURED
407
408 EASYEDAPRO::POURED poured = line;
409 m_projectData->m_Poured[baseName].emplace( poured.parentId, poured );
410 }
411 }
412 }
414 };
415 EASYEDAPRO::IterateZipFiles( aProjectPath, cb );
416}
417
418
419std::unique_ptr<FOOTPRINT> PCB_IO_EASYEDAPRO::FootprintLoad( const wxString& aLibraryPath,
420 const wxString& aFootprintName, bool aKeepUUID,
421 const std::map<std::string, UTF8>* aProperties )
422{
423 // Suppress font substitution warnings (RAII - automatically restored on scope exit)
424 FONTCONFIG_REPORTER_SCOPE fontconfigScope( nullptr );
425
426 PCB_IO_EASYEDAPRO_PARSER parser( nullptr, nullptr );
427 std::unique_ptr<FOOTPRINT> footprint;
428
429 wxFileName libFname( aLibraryPath );
430
431 if( libFname.GetExt() == wxS( "efoo" ) )
432 {
433 wxFFileInputStream ffis( aLibraryPath );
434 wxTextInputStream txt( ffis, wxS( " " ), wxConvUTF8 );
435
436 std::vector<nlohmann::json> lines = EASYEDAPRO::ParseJsonLines( ffis, aLibraryPath );
437
438 for( const nlohmann::json& js : lines )
439 {
440 if( js.at( 0 ) == "ATTR" )
441 {
442 EASYEDAPRO::PCB_ATTR attr = js;
443
444 if( attr.key == wxS( "Footprint" ) && attr.value != aFootprintName )
445 return nullptr;
446 }
447 }
448
449 footprint = parser.ParseFootprint( nlohmann::json(), wxEmptyString, lines );
450
451 if( !footprint )
452 THROW_IO_ERRORF( _( "Cannot load footprint '%s' from '%s'" ), aFootprintName, aLibraryPath );
453
454 LIB_ID fpID = EASYEDAPRO::ToKiCadLibID( wxEmptyString, aFootprintName );
455 footprint->SetFPID( fpID );
456
457 footprint->Reference().SetVisible( true );
458 footprint->Value().SetText( aFootprintName );
459 footprint->Value().SetVisible( true );
460 footprint->AutoPositionFields();
461 }
462 else if( libFname.GetExt() == wxS( "elibz" ) || libFname.GetExt() == wxS( "epro" )
463 || libFname.GetExt() == wxS( "zip" ) )
464 {
465 nlohmann::json project = EASYEDAPRO::ReadProjectOrDeviceFile( aLibraryPath );
466
467 wxString fpUuid;
468
469 std::map<wxString, nlohmann::json> footprintMap = project.at( "footprints" );
470 for( auto& [uuid, data] : footprintMap )
471 {
472 wxString title;
473
474 if( data.contains( "display_title" ) )
475 title = data.at( "display_title" ).get<wxString>();
476 else
477 title = data.at( "title" ).get<wxString>();
478
479 if( title == aFootprintName )
480 {
481 fpUuid = uuid;
482 break;
483 }
484 }
485
486 if( !fpUuid )
487 THROW_IO_ERRORF( _( "Footprint '%s' not found in project '%s'" ), aFootprintName, aLibraryPath );
488
489 auto cb = [&]( const wxString& name, const wxString& baseName, wxInputStream& zip ) -> bool
490 {
491 if( !name.EndsWith( wxS( ".efoo" ) ) )
493
494 if( baseName != fpUuid )
496
497 std::vector<nlohmann::json> lines = EASYEDAPRO::ParseJsonLines( zip, name );
498
499 footprint = parser.ParseFootprint( project, fpUuid, lines );
500
501 if( !footprint )
502 THROW_IO_ERRORF( _( "Cannot load footprint '%s' from '%s'" ), aFootprintName, aLibraryPath );
503
504 LIB_ID fpID = EASYEDAPRO::ToKiCadLibID( wxEmptyString, aFootprintName );
505 footprint->SetFPID( fpID );
506
507 footprint->Reference().SetVisible( true );
508 footprint->Value().SetText( aFootprintName );
509 footprint->Value().SetVisible( true );
510 footprint->AutoPositionFields();
511
513 };
514 EASYEDAPRO::IterateZipFiles( aLibraryPath, cb );
515 }
516
517 return footprint;
518}
519
520
522{
523 std::vector<FOOTPRINT*> result;
524
525 if( !m_projectData )
526 return result;
527
528 for( auto& [fpUuid, footprint] : m_projectData->m_Footprints )
529 {
530 result.push_back( static_cast<FOOTPRINT*>( footprint->Clone() ) );
531 }
532
533 return result;
534}
const char * name
Information pertinent to a Pcbnew printed circuit board.
Definition board.h:411
RAII class to set and restore the fontconfig reporter.
Definition reporter.h:385
PROGRESS_REPORTER * m_progressReporter
Progress reporter to track the progress of the operation, may be nullptr.
Definition io_base.h:267
A logical library item identifier and consists of various portions much like a URI.
Definition lib_id.h:45
static LOAD_INFO_REPORTER & GetInstance()
Definition reporter.cpp:351
std::unique_ptr< FOOTPRINT > ParseFootprint(const nlohmann::json &aProject, const wxString &aFpUuid, const std::vector< nlohmann::json > &aLines)
void ParseBoard(BOARD *aBoard, const nlohmann::json &aProject, std::map< wxString, std::unique_ptr< FOOTPRINT > > &aFootprintMap, const std::map< wxString, EASYEDAPRO::BLOB > &aBlobMap, const std::multimap< wxString, EASYEDAPRO::POURED > &aPouredMap, const std::vector< nlohmann::json > &aLines, const wxString &aFpLibName)
void LoadAllDataFromProject(const wxString &aLibraryPath, const nlohmann::json &aProject)
std::vector< FOOTPRINT * > GetImportedCachedLibraryFootprints() override
Return a container with the cached library footprints generated in the last call to Load.
bool CanReadBoard(const wxString &aFileName) const override
long long GetLibraryTimestamp(const wxString &aLibraryPath) const override
Generate a timestamp representing all the files in the library (including the library directory).
std::unique_ptr< FOOTPRINT > FootprintLoad(const wxString &aLibraryPath, const wxString &aFootprintName, bool aKeepUUID=false, const std::map< std::string, UTF8 > *aProperties=nullptr) override
Load a footprint having aFootprintName from the aLibraryPath containing a library format that this PC...
void loadBoard(const wxString &aFileName, BOARD &aBoard, bool aIsNewLoad, const std::map< std::string, UTF8 > *aProperties=nullptr, PROJECT *aProject=nullptr) override
Parse aFileName into aBoard.
std::vector< std::pair< wxString, wxString > > EnumerateProjectBoards(const wxString &aFileName) const override
Check if this PCB_IO can read the specified board file.
std::vector< IMPORT_PROJECT_DESC > EnumerateProjectEntries(const wxString &aFileName) const override
Return the KiCad projects a project container splits into, one per board entry and one per PCB or sch...
void FootprintEnumerate(wxArrayString &aFootprintNames, const wxString &aLibraryPath, bool aBestEfforts, const std::map< std::string, UTF8 > *aProperties=nullptr) override
Return a list of footprint names contained within the library at aLibraryPath.
BOARD * m_board
The board BOARD being worked on, no ownership here.
Definition pcb_io.h:404
PCB_IO(const wxString &aName)
Definition pcb_io.h:383
const std::map< std::string, UTF8 > * m_props
Properties passed via Save() or Load(), no ownership, may be NULL.
Definition pcb_io.h:407
CHOOSE_PROJECT_HANDLER m_choose_project_handler
Callback to choose projects to import.
Container for project specific data.
Definition project.h:63
#define EASY_IT_BREAK
#define EASY_IT_CONTINUE
#define _(s)
#define THROW_IO_ERROR(msg)
macro which captures the "call site" values of FILE_, __FUNCTION & LINE
#define THROW_IO_ERRORF(msg,...)
#define THROW_IO_CANCELLED()
This file contains miscellaneous commonly used macros and functions.
wxString get_def(const std::map< wxString, wxString > &aMap, const char *aKey, const char *aDefval="")
Definition map_helpers.h:60
LIB_ID ToKiCadLibID(const wxString &aLibName, const wxString &aLibReference)
void IterateZipFiles(const wxString &aFileName, std::function< bool(const wxString &, const wxString &, wxInputStream &)> aCallback)
std::vector< nlohmann::json > ParseJsonLines(wxInputStream &aInput, const wxString &aSource)
static const bool IMPORT_POURED_ECOP
std::vector< std::vector< nlohmann::json > > ParseJsonLinesWithSeparation(wxInputStream &aInput, const wxString &aSource)
Multiple document types (e.g.
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, std::multimap< wxString, EASYEDAPRO::POURED > > m_Poured
std::map< wxString, EASYEDAPRO::BLOB > m_Blobs
std::map< wxString, std::unique_ptr< FOOTPRINT > > m_Footprints
wxString result
Test unit parsing edge cases and error handling.