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