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
128void PCB_IO_EASYEDAPRO::loadBoard( const wxString& aFileName, BOARD& aBoard, bool aIsNewLoad,
129 const std::map<std::string, UTF8>* aProperties, PROJECT* aProject )
130{
131 m_props = aProperties;
132 m_importedDesignRules.clear();
133
134 m_board = &aBoard;
135
136 // Collect the font substitution warnings (RAII - automatically reset on scope exit)
138
140 {
141 m_progressReporter->Report( wxString::Format( _( "Loading %s..." ), aFileName ) );
142
143 if( !m_progressReporter->KeepRefreshing() )
145 }
146
147 PCB_IO_EASYEDAPRO_PARSER parser( nullptr, nullptr );
148
149 wxFileName fname( aFileName );
150 wxString fpLibName = EASYEDAPRO::ShortenLibName( fname.GetName() );
151
152 if( fname.GetExt() == wxS( "epro" ) || fname.GetExt() == wxS( "zip" ) )
153 {
154 nlohmann::json project = EASYEDAPRO::ReadProjectOrDeviceFile( aFileName );
155
156 wxString pcbToLoad;
157
158 if( m_props && m_props->contains( "pcb_id" ) )
159 {
160 pcbToLoad = wxString::FromUTF8( m_props->at( "pcb_id" ) );
161 }
162 else
163 {
164 std::map<wxString, wxString> prjPcbNames = project.at( "pcbs" );
165
166 if( prjPcbNames.size() == 1 )
167 {
168 pcbToLoad = prjPcbNames.begin()->first;
169 }
170 else
171 {
172 std::vector<IMPORT_PROJECT_DESC> chosen = m_choose_project_handler(
174
175 // Quiet exit on cancel
176 if( chosen.size() == 0 )
178
179 pcbToLoad = chosen[0].PCBId;
180 }
181 }
182
183 if( pcbToLoad.empty() )
184 THROW_IO_ERROR( _( "No PCB was found in the project to import." ) );
185
186 LoadAllDataFromProject( aFileName, project );
187
188 if( !m_projectData )
189 THROW_IO_ERROR( _( "Failed to load the project data." ) );
190
191 auto cb = [&]( const wxString& name, const wxString& pcbUuid, wxInputStream& zip ) -> bool
192 {
193 if( !name.EndsWith( wxS( ".epcb" ) ) )
195
196 if( pcbUuid != pcbToLoad )
198
199 std::vector<nlohmann::json>* pcbLines = nullptr;
200
201 std::vector<std::vector<nlohmann::json>> lineBlocks =
203
204 if( lineBlocks.empty() )
206
207 if( lineBlocks.size() > 1 )
208 {
209 for( std::vector<nlohmann::json>& block : lineBlocks )
210 {
211 wxString docType;
212 nlohmann::json headData;
213
214 for( const nlohmann::json& line : block )
215 {
216 if( line.size() < 2 )
217 continue;
218
219 if( !line.at( 0 ).is_string() )
220 continue;
221
222 wxString lineType = line.at( 0 ).get<wxString>();
223
224 if( lineType == wxS( "DOCTYPE" ) )
225 {
226 if( !line.at( 1 ).is_string() )
227 continue;
228
229 docType = line.at( 1 ).get<wxString>();
230 }
231 else if( lineType == wxS( "HEAD" ) )
232 {
233 if( !line.at( 1 ).is_object() )
234 continue;
235
236 headData = line.at( 1 );
237 }
238 }
239
240 if( docType == wxS( "FOOTPRINT" ) )
241 {
242 wxString fpUuid = headData.at( "uuid" );
243 wxString fpTitle = headData.at( "title" );
244
245 std::unique_ptr<FOOTPRINT> footprint = parser.ParseFootprint( project, fpUuid, block );
246
247 if( !footprint )
249
250 LIB_ID fpID = EASYEDAPRO::ToKiCadLibID( fpLibName, fpTitle );
251 footprint->SetFPID( fpID );
252
253 m_projectData->m_Footprints.emplace( fpUuid, std::move( footprint ) );
254 }
255 else if( docType == wxS( "PCB" ) )
256 {
257 pcbLines = &block;
258 }
259 }
260 }
261
262 if( pcbLines == nullptr )
263 pcbLines = &lineBlocks[0];
264
265 wxString boardKey = pcbUuid + wxS( "_0" );
266 wxScopedCharBuffer boardKeyBuffer = boardKey.ToUTF8();
267 wxString boardPouredKey = wxBase64Encode( boardKeyBuffer.data(), boardKeyBuffer.length() );
268
269 const std::multimap<wxString, EASYEDAPRO::POURED>& boardPoured =
270 get_def( m_projectData->m_Poured, boardPouredKey );
271
272 parser.ParseBoard( m_board, project, m_projectData->m_Footprints,
273 m_projectData->m_Blobs, boardPoured, *pcbLines,
274 EASYEDAPRO::ShortenLibName( fname.GetName() ) );
276
277
279 };
280 EASYEDAPRO::IterateZipFiles( aFileName, cb );
281 }
282}
283
284
285long long PCB_IO_EASYEDAPRO::GetLibraryTimestamp( const wxString& aLibraryPath ) const
286{
287 return 0;
288}
289
290
291void PCB_IO_EASYEDAPRO::FootprintEnumerate( wxArrayString& aFootprintNames,
292 const wxString& aLibraryPath, bool aBestEfforts,
293 const std::map<std::string, UTF8>* aProperties )
294{
295 wxFileName fname( aLibraryPath );
296
297 if( fname.GetExt() == wxS( "efoo" ) )
298 {
299 wxFFileInputStream ffis( aLibraryPath );
300 wxTextInputStream txt( ffis, wxS( " " ), wxConvUTF8 );
301
302 while( ffis.CanRead() )
303 {
304 wxString line = txt.ReadLine();
305
306 if( !line.Contains( wxS( "ATTR" ) ) )
307 continue; // Don't bother parsing
308
309 nlohmann::json js = nlohmann::json::parse( line );
310 if( js.at( 0 ) == "ATTR" && js.at( 7 ) == "Footprint" )
311 {
312 aFootprintNames.Add( js.at( 8 ).get<wxString>() );
313 }
314 }
315 }
316 else if( fname.GetExt() == wxS( "elibz" ) || fname.GetExt() == wxS( "epro" )
317 || fname.GetExt() == wxS( "zip" ) )
318 {
319 nlohmann::json project = EASYEDAPRO::ReadProjectOrDeviceFile( aLibraryPath );
320 std::map<wxString, nlohmann::json> footprintMap = project.at( "footprints" );
321
322 for( auto& [key, value] : footprintMap )
323 {
324 wxString title;
325
326 if( value.contains( "display_title" ) )
327 title = value.at( "display_title" ).get<wxString>();
328 else
329 title = value.at( "title" ).get<wxString>();
330
331 aFootprintNames.Add( title );
332 }
333 }
334}
335
336
337void PCB_IO_EASYEDAPRO::LoadAllDataFromProject( const wxString& aProjectPath,
338 const nlohmann::json& aProject )
339{
340 if( m_projectData )
341 delete m_projectData;
342
343 m_projectData = new PRJ_DATA();
344
345 PCB_IO_EASYEDAPRO_PARSER parser( nullptr, nullptr );
346 wxFileName fname( aProjectPath );
347 wxString fpLibName = EASYEDAPRO::ShortenLibName( fname.GetName() );
348
349 std::map<wxString, std::unique_ptr<FOOTPRINT>> result;
350
351 auto cb = [&]( const wxString& name, const wxString& baseName, wxInputStream& zip ) -> bool
352 {
353 if( !name.EndsWith( wxS( ".efoo" ) ) && !name.EndsWith( wxS( ".eblob" ) )
354 && !name.EndsWith( wxS( ".ecop" ) ) )
355 {
357 }
358
359 std::vector<nlohmann::json> lines = EASYEDAPRO::ParseJsonLines( zip, name );
360
361 if( name.EndsWith( wxS( ".efoo" ) ) )
362 {
363 nlohmann::json fpData = aProject.at( "footprints" ).at( baseName );
364 wxString fpTitle = fpData.at( "title" );
365
366 std::unique_ptr<FOOTPRINT> footprint = parser.ParseFootprint( aProject, baseName, lines );
367
368 if( !footprint )
370
371 LIB_ID fpID = EASYEDAPRO::ToKiCadLibID( fpLibName, fpTitle );
372 footprint->SetFPID( fpID );
373
374 m_projectData->m_Footprints.emplace( baseName, std::move( footprint ) );
375 }
376 else if( name.EndsWith( wxS( ".eblob" ) ) )
377 {
378 for( const nlohmann::json& line : lines )
379 {
380 if( line.at( 0 ) == "BLOB" )
381 {
382 EASYEDAPRO::BLOB blob = line;
383 m_projectData->m_Blobs[blob.objectId] = blob;
384 }
385 }
386 }
387 else if( name.EndsWith( wxS( ".ecop" ) ) && EASYEDAPRO::IMPORT_POURED_ECOP )
388 {
389 for( const nlohmann::json& line : lines )
390 {
391 if( line.at( 0 ) == "POURED" )
392 {
393 if( !line.at( 2 ).is_string() )
394 continue; // Unknown type of POURED
395
396 EASYEDAPRO::POURED poured = line;
397 m_projectData->m_Poured[baseName].emplace( poured.parentId, poured );
398 }
399 }
400 }
402 };
403 EASYEDAPRO::IterateZipFiles( aProjectPath, cb );
404}
405
406
407std::unique_ptr<FOOTPRINT> PCB_IO_EASYEDAPRO::FootprintLoad( const wxString& aLibraryPath,
408 const wxString& aFootprintName, bool aKeepUUID,
409 const std::map<std::string, UTF8>* aProperties )
410{
411 // Suppress font substitution warnings (RAII - automatically restored on scope exit)
412 FONTCONFIG_REPORTER_SCOPE fontconfigScope( nullptr );
413
414 PCB_IO_EASYEDAPRO_PARSER parser( nullptr, nullptr );
415 std::unique_ptr<FOOTPRINT> footprint;
416
417 wxFileName libFname( aLibraryPath );
418
419 if( libFname.GetExt() == wxS( "efoo" ) )
420 {
421 wxFFileInputStream ffis( aLibraryPath );
422 wxTextInputStream txt( ffis, wxS( " " ), wxConvUTF8 );
423
424 std::vector<nlohmann::json> lines = EASYEDAPRO::ParseJsonLines( ffis, aLibraryPath );
425
426 for( const nlohmann::json& js : lines )
427 {
428 if( js.at( 0 ) == "ATTR" )
429 {
430 EASYEDAPRO::PCB_ATTR attr = js;
431
432 if( attr.key == wxS( "Footprint" ) && attr.value != aFootprintName )
433 return nullptr;
434 }
435 }
436
437 footprint = parser.ParseFootprint( nlohmann::json(), wxEmptyString, lines );
438
439 if( !footprint )
440 THROW_IO_ERRORF( _( "Cannot load footprint '%s' from '%s'" ), aFootprintName, aLibraryPath );
441
442 LIB_ID fpID = EASYEDAPRO::ToKiCadLibID( wxEmptyString, aFootprintName );
443 footprint->SetFPID( fpID );
444
445 footprint->Reference().SetVisible( true );
446 footprint->Value().SetText( aFootprintName );
447 footprint->Value().SetVisible( true );
448 footprint->AutoPositionFields();
449 }
450 else if( libFname.GetExt() == wxS( "elibz" ) || libFname.GetExt() == wxS( "epro" )
451 || libFname.GetExt() == wxS( "zip" ) )
452 {
453 nlohmann::json project = EASYEDAPRO::ReadProjectOrDeviceFile( aLibraryPath );
454
455 wxString fpUuid;
456
457 std::map<wxString, nlohmann::json> footprintMap = project.at( "footprints" );
458 for( auto& [uuid, data] : footprintMap )
459 {
460 wxString title;
461
462 if( data.contains( "display_title" ) )
463 title = data.at( "display_title" ).get<wxString>();
464 else
465 title = data.at( "title" ).get<wxString>();
466
467 if( title == aFootprintName )
468 {
469 fpUuid = uuid;
470 break;
471 }
472 }
473
474 if( !fpUuid )
475 THROW_IO_ERRORF( _( "Footprint '%s' not found in project '%s'" ), aFootprintName, aLibraryPath );
476
477 auto cb = [&]( const wxString& name, const wxString& baseName, wxInputStream& zip ) -> bool
478 {
479 if( !name.EndsWith( wxS( ".efoo" ) ) )
481
482 if( baseName != fpUuid )
484
485 std::vector<nlohmann::json> lines = EASYEDAPRO::ParseJsonLines( zip, name );
486
487 footprint = parser.ParseFootprint( project, fpUuid, lines );
488
489 if( !footprint )
490 THROW_IO_ERRORF( _( "Cannot load footprint '%s' from '%s'" ), aFootprintName, aLibraryPath );
491
492 LIB_ID fpID = EASYEDAPRO::ToKiCadLibID( wxEmptyString, aFootprintName );
493 footprint->SetFPID( fpID );
494
495 footprint->Reference().SetVisible( true );
496 footprint->Value().SetText( aFootprintName );
497 footprint->Value().SetVisible( true );
498 footprint->AutoPositionFields();
499
501 };
502 EASYEDAPRO::IterateZipFiles( aLibraryPath, cb );
503 }
504
505 return footprint;
506}
507
508
510{
511 std::vector<FOOTPRINT*> result;
512
513 if( !m_projectData )
514 return result;
515
516 for( auto& [fpUuid, footprint] : m_projectData->m_Footprints )
517 {
518 result.push_back( static_cast<FOOTPRINT*>( footprint->Clone() ) );
519 }
520
521 return result;
522}
const char * name
Information pertinent to a Pcbnew printed circuit board.
Definition board.h:410
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:241
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.
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:393
PCB_IO(const wxString &aName)
Definition pcb_io.h:372
const std::map< std::string, UTF8 > * m_props
Properties passed via Save() or Load(), no ownership, may be NULL.
Definition pcb_io.h:396
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)
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.