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