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, you may find one here:
19 * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
20 * or you may search the http://www.gnu.org website for the version 2 license,
21 * or you may write to the Free Software Foundation, Inc.,
22 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
23 */
24
29#include <pcb_io/pcb_io.h>
30
31#include <board.h>
32#include <font/fontconfig.h>
33#include <footprint.h>
34#include <progress_reporter.h>
35#include <common.h>
36#include <macros.h>
37#include <reporter.h>
38
39#include <fstream>
40#include <wx/txtstrm.h>
41#include <wx/wfstream.h>
42#include <wx/mstream.h>
43#include <wx/zipstrm.h>
44#include <wx/log.h>
45
46#include <json_common.h>
48#include <core/map_helpers.h>
49
50
52{
53 std::map<wxString, std::unique_ptr<FOOTPRINT>> m_Footprints;
54 std::map<wxString, EASYEDAPRO::BLOB> m_Blobs;
55 std::map<wxString, std::multimap<wxString, EASYEDAPRO::POURED>> m_Poured;
56};
57
58
59PCB_IO_EASYEDAPRO::PCB_IO_EASYEDAPRO() : PCB_IO( wxS( "EasyEDA (JLCEDA) Professional" ) )
60{
61}
62
63
69
70
71bool PCB_IO_EASYEDAPRO::CanReadBoard( const wxString& aFileName ) const
72{
73 if( aFileName.Lower().EndsWith( wxS( ".epro" ) ) )
74 {
75 return true;
76 }
77 else if( aFileName.Lower().EndsWith( wxS( ".zip" ) ) )
78 {
79 std::shared_ptr<wxZipEntry> entry;
80 wxFFileInputStream in( aFileName );
81 wxZipInputStream zip( in );
82
83 if( !zip.IsOk() )
84 return false;
85
86 while( entry.reset( zip.GetNextEntry() ), entry.get() != NULL )
87 {
88 wxString name = entry->GetName();
89
90 if( name == wxS( "project.json" ) )
91 return true;
92 }
93
94 return false;
95 }
96
97 return false;
98}
99
100
101BOARD* PCB_IO_EASYEDAPRO::LoadBoard( const wxString& aFileName, BOARD* aAppendToMe,
102 const std::map<std::string, UTF8>* aProperties, PROJECT* aProject )
103{
104 m_props = aProperties;
105
106 m_board = aAppendToMe ? aAppendToMe : new BOARD();
107
108 // Give the filename to the board if it's new
109 if( !aAppendToMe )
110 m_board->SetFileName( aFileName );
111
112 // Collect the font substitution warnings (RAII - automatically reset on scope exit)
114
116 {
117 m_progressReporter->Report( wxString::Format( _( "Loading %s..." ), aFileName ) );
118
119 if( !m_progressReporter->KeepRefreshing() )
120 THROW_IO_ERROR( _( "File import canceled by user." ) );
121 }
122
123 PCB_IO_EASYEDAPRO_PARSER parser( nullptr, nullptr );
124
125 wxFileName fname( aFileName );
126 wxString fpLibName = EASYEDAPRO::ShortenLibName( fname.GetName() );
127
128 if( fname.GetExt() == wxS( "epro" ) || fname.GetExt() == wxS( "zip" ) )
129 {
130 nlohmann::json project = EASYEDAPRO::ReadProjectOrDeviceFile( aFileName );
131
132 wxString pcbToLoad;
133
134 if( m_props && m_props->contains( "pcb_id" ) )
135 {
136 pcbToLoad = wxString::FromUTF8( m_props->at( "pcb_id" ) );
137 }
138 else
139 {
140 std::map<wxString, wxString> prjPcbNames = project.at( "pcbs" );
141
142 if( prjPcbNames.size() == 1 )
143 {
144 pcbToLoad = prjPcbNames.begin()->first;
145 }
146 else
147 {
148 std::vector<IMPORT_PROJECT_DESC> chosen = m_choose_project_handler(
150
151 if( chosen.size() > 0 )
152 pcbToLoad = chosen[0].PCBId;
153 }
154 }
155
156 if( pcbToLoad.empty() )
157 return nullptr;
158
159 LoadAllDataFromProject( aFileName, project );
160
161 if( !m_projectData )
162 return nullptr;
163
164 auto cb = [&]( const wxString& name, const wxString& pcbUuid, wxInputStream& zip ) -> bool
165 {
166 if( !name.EndsWith( wxS( ".epcb" ) ) )
168
169 if( pcbUuid != pcbToLoad )
171
172 std::vector<nlohmann::json>* pcbLines = nullptr;
173
174 std::vector<std::vector<nlohmann::json>> lineBlocks =
176
177 if( lineBlocks.empty() )
179
180 if( lineBlocks.size() > 1 )
181 {
182 for( std::vector<nlohmann::json>& block : lineBlocks )
183 {
184 wxString docType;
185 nlohmann::json headData;
186
187 for( const nlohmann::json& line : block )
188 {
189 if( line.size() < 2 )
190 continue;
191
192 if( !line.at( 0 ).is_string() )
193 continue;
194
195 wxString lineType = line.at( 0 ).get<wxString>();
196
197 if( lineType == wxS( "DOCTYPE" ) )
198 {
199 if( !line.at( 1 ).is_string() )
200 continue;
201
202 docType = line.at( 1 ).get<wxString>();
203 }
204 else if( lineType == wxS( "HEAD" ) )
205 {
206 if( !line.at( 1 ).is_object() )
207 continue;
208
209 headData = line.at( 1 );
210 }
211 }
212
213 if( docType == wxS( "FOOTPRINT" ) )
214 {
215 wxString fpUuid = headData.at( "uuid" );
216 wxString fpTitle = headData.at( "title" );
217
218 FOOTPRINT* footprint = parser.ParseFootprint( project, fpUuid, block );
219
220 if( !footprint )
222
223 LIB_ID fpID = EASYEDAPRO::ToKiCadLibID( fpLibName, fpTitle );
224 footprint->SetFPID( fpID );
225
226 m_projectData->m_Footprints.emplace( fpUuid, footprint );
227 }
228 else if( docType == wxS( "PCB" ) )
229 {
230 pcbLines = &block;
231 }
232 }
233 }
234
235 if( pcbLines == nullptr )
236 pcbLines = &lineBlocks[0];
237
238 wxString boardKey = pcbUuid + wxS( "_0" );
239 wxScopedCharBuffer cb = boardKey.ToUTF8();
240 wxString boardPouredKey = wxBase64Encode( cb.data(), cb.length() );
241
242 const std::multimap<wxString, EASYEDAPRO::POURED>& boardPoured =
243 get_def( m_projectData->m_Poured, boardPouredKey );
244
245 parser.ParseBoard( m_board, project, m_projectData->m_Footprints,
246 m_projectData->m_Blobs, boardPoured, *pcbLines,
247 EASYEDAPRO::ShortenLibName( fname.GetName() ) );
248
250 };
251 EASYEDAPRO::IterateZipFiles( aFileName, cb );
252 }
253
254 return m_board;
255}
256
257
258long long PCB_IO_EASYEDAPRO::GetLibraryTimestamp( const wxString& aLibraryPath ) const
259{
260 return 0;
261}
262
263
264void PCB_IO_EASYEDAPRO::FootprintEnumerate( wxArrayString& aFootprintNames,
265 const wxString& aLibraryPath, bool aBestEfforts,
266 const std::map<std::string, UTF8>* aProperties )
267{
268 wxFileName fname( aLibraryPath );
269
270 if( fname.GetExt() == wxS( "efoo" ) )
271 {
272 wxFFileInputStream ffis( aLibraryPath );
273 wxTextInputStream txt( ffis, wxS( " " ), wxConvUTF8 );
274
275 while( ffis.CanRead() )
276 {
277 wxString line = txt.ReadLine();
278
279 if( !line.Contains( wxS( "ATTR" ) ) )
280 continue; // Don't bother parsing
281
282 nlohmann::json js = nlohmann::json::parse( line );
283 if( js.at( 0 ) == "ATTR" && js.at( 7 ) == "Footprint" )
284 {
285 aFootprintNames.Add( js.at( 8 ).get<wxString>() );
286 }
287 }
288 }
289 else if( fname.GetExt() == wxS( "elibz" ) || fname.GetExt() == wxS( "epro" )
290 || fname.GetExt() == wxS( "zip" ) )
291 {
292 nlohmann::json project = EASYEDAPRO::ReadProjectOrDeviceFile( aLibraryPath );
293 std::map<wxString, nlohmann::json> footprintMap = project.at( "footprints" );
294
295 for( auto& [key, value] : footprintMap )
296 {
297 wxString title;
298
299 if( value.contains( "display_title" ) )
300 title = value.at( "display_title" ).get<wxString>();
301 else
302 title = value.at( "title" ).get<wxString>();
303
304 aFootprintNames.Add( title );
305 }
306 }
307}
308
309
310void PCB_IO_EASYEDAPRO::LoadAllDataFromProject( const wxString& aProjectPath,
311 const nlohmann::json& aProject )
312{
313 if( m_projectData )
314 delete m_projectData;
315
316 m_projectData = new PRJ_DATA();
317
318 PCB_IO_EASYEDAPRO_PARSER parser( nullptr, nullptr );
319 wxFileName fname( aProjectPath );
320 wxString fpLibName = EASYEDAPRO::ShortenLibName( fname.GetName() );
321
322 std::map<wxString, std::unique_ptr<FOOTPRINT>> result;
323
324 auto cb = [&]( const wxString& name, const wxString& baseName, wxInputStream& zip ) -> bool
325 {
326 if( !name.EndsWith( wxS( ".efoo" ) ) && !name.EndsWith( wxS( ".eblob" ) )
327 && !name.EndsWith( wxS( ".ecop" ) ) )
328 {
330 }
331
332 std::vector<nlohmann::json> lines = EASYEDAPRO::ParseJsonLines( zip, name );
333
334 if( name.EndsWith( wxS( ".efoo" ) ) )
335 {
336 nlohmann::json fpData = aProject.at( "footprints" ).at( baseName );
337 wxString fpTitle = fpData.at( "title" );
338
339 FOOTPRINT* footprint = parser.ParseFootprint( aProject, baseName, lines );
340
341 if( !footprint )
343
344 LIB_ID fpID = EASYEDAPRO::ToKiCadLibID( fpLibName, fpTitle );
345 footprint->SetFPID( fpID );
346
347 m_projectData->m_Footprints.emplace( baseName, footprint );
348 }
349 else if( name.EndsWith( wxS( ".eblob" ) ) )
350 {
351 for( const nlohmann::json& line : lines )
352 {
353 if( line.at( 0 ) == "BLOB" )
354 {
355 EASYEDAPRO::BLOB blob = line;
356 m_projectData->m_Blobs[blob.objectId] = blob;
357 }
358 }
359 }
360 else if( name.EndsWith( wxS( ".ecop" ) ) && EASYEDAPRO::IMPORT_POURED_ECOP )
361 {
362 for( const nlohmann::json& line : lines )
363 {
364 if( line.at( 0 ) == "POURED" )
365 {
366 if( !line.at( 2 ).is_string() )
367 continue; // Unknown type of POURED
368
369 EASYEDAPRO::POURED poured = line;
370 m_projectData->m_Poured[baseName].emplace( poured.parentId, poured );
371 }
372 }
373 }
375 };
376 EASYEDAPRO::IterateZipFiles( aProjectPath, cb );
377}
378
379
380FOOTPRINT* PCB_IO_EASYEDAPRO::FootprintLoad( const wxString& aLibraryPath,
381 const wxString& aFootprintName, bool aKeepUUID,
382 const std::map<std::string, UTF8>* aProperties )
383{
384 // Suppress font substitution warnings (RAII - automatically restored on scope exit)
385 FONTCONFIG_REPORTER_SCOPE fontconfigScope( nullptr );
386
387 PCB_IO_EASYEDAPRO_PARSER parser( nullptr, nullptr );
388 FOOTPRINT* footprint = nullptr;
389
390 wxFileName libFname( aLibraryPath );
391
392 if( libFname.GetExt() == wxS( "efoo" ) )
393 {
394 wxFFileInputStream ffis( aLibraryPath );
395 wxTextInputStream txt( ffis, wxS( " " ), wxConvUTF8 );
396
397 std::vector<nlohmann::json> lines = EASYEDAPRO::ParseJsonLines( ffis, aLibraryPath );
398
399 for( const nlohmann::json& js : lines )
400 {
401 if( js.at( 0 ) == "ATTR" )
402 {
403 EASYEDAPRO::PCB_ATTR attr = js;
404
405 if( attr.key == wxS( "Footprint" ) && attr.value != aFootprintName )
406 return nullptr;
407 }
408 }
409
410 footprint = parser.ParseFootprint( nlohmann::json(), wxEmptyString, lines );
411
412 if( !footprint )
413 {
414 THROW_IO_ERROR( wxString::Format( _( "Cannot load footprint '%s' from '%s'" ),
415 aFootprintName, aLibraryPath ) );
416 }
417
418 LIB_ID fpID = EASYEDAPRO::ToKiCadLibID( wxEmptyString, aFootprintName );
419 footprint->SetFPID( fpID );
420
421 footprint->Reference().SetVisible( true );
422 footprint->Value().SetText( aFootprintName );
423 footprint->Value().SetVisible( true );
424 footprint->AutoPositionFields();
425 }
426 else if( libFname.GetExt() == wxS( "elibz" ) || libFname.GetExt() == wxS( "epro" )
427 || libFname.GetExt() == wxS( "zip" ) )
428 {
429 nlohmann::json project = EASYEDAPRO::ReadProjectOrDeviceFile( aLibraryPath );
430
431 wxString fpUuid;
432
433 std::map<wxString, nlohmann::json> footprintMap = project.at( "footprints" );
434 for( auto& [uuid, data] : footprintMap )
435 {
436 wxString title;
437
438 if( data.contains( "display_title" ) )
439 title = data.at( "display_title" ).get<wxString>();
440 else
441 title = data.at( "title" ).get<wxString>();
442
443 if( title == aFootprintName )
444 {
445 fpUuid = uuid;
446 break;
447 }
448 }
449
450 if( !fpUuid )
451 {
452 THROW_IO_ERROR( wxString::Format( _( "Footprint '%s' not found in project '%s'" ),
453 aFootprintName, aLibraryPath ) );
454 }
455
456 auto cb = [&]( const wxString& name, const wxString& baseName, wxInputStream& zip ) -> bool
457 {
458 if( !name.EndsWith( wxS( ".efoo" ) ) )
460
461 if( baseName != fpUuid )
463
464 std::vector<nlohmann::json> lines = EASYEDAPRO::ParseJsonLines( zip, name );
465
466 footprint = parser.ParseFootprint( project, fpUuid, lines );
467
468 if( !footprint )
469 {
470 THROW_IO_ERROR( wxString::Format( _( "Cannot load footprint '%s' from '%s'" ),
471 aFootprintName, aLibraryPath ) );
472 }
473
474 LIB_ID fpID = EASYEDAPRO::ToKiCadLibID( wxEmptyString, aFootprintName );
475 footprint->SetFPID( fpID );
476
477 footprint->Reference().SetVisible( true );
478 footprint->Value().SetText( aFootprintName );
479 footprint->Value().SetVisible( true );
480 footprint->AutoPositionFields();
481
483 };
484 EASYEDAPRO::IterateZipFiles( aLibraryPath, cb );
485 }
486
487 return footprint;
488}
489
490
492{
493 std::vector<FOOTPRINT*> result;
494
495 if( !m_projectData )
496 return result;
497
498 for( auto& [fpUuid, footprint] : m_projectData->m_Footprints )
499 {
500 result.push_back( static_cast<FOOTPRINT*>( footprint->Clone() ) );
501 }
502
503 return result;
504}
const char * name
Information pertinent to a Pcbnew printed circuit board.
Definition board.h:322
virtual void SetVisible(bool aVisible)
Definition eda_text.cpp:395
virtual void SetText(const wxString &aText)
Definition eda_text.cpp:279
RAII class to set and restore the fontconfig reporter.
Definition reporter.h:322
void SetFPID(const LIB_ID &aFPID)
Definition footprint.h:350
PCB_FIELD & Value()
read/write accessors:
Definition footprint.h:777
PCB_FIELD & Reference()
Definition footprint.h:778
void AutoPositionFields()
Position Reference and Value fields at the top and bottom of footprint's bounding box.
PROGRESS_REPORTER * m_progressReporter
Progress reporter to track the progress of the operation, may be nullptr.
Definition io_base.h:240
A logical library item identifier and consists of various portions much like a URI.
Definition lib_id.h:49
static LOAD_INFO_REPORTER & GetInstance()
Definition reporter.cpp:218
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)
FOOTPRINT * ParseFootprint(const nlohmann::json &aProject, const wxString &aFpUuid, const std::vector< nlohmann::json > &aLines)
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.
BOARD * LoadBoard(const wxString &aFileName, BOARD *aAppendToMe, const std::map< std::string, UTF8 > *aProperties=nullptr, PROJECT *aProject=nullptr) override
Load information from some input file format that this PCB_IO implementation knows about into either ...
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).
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 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:326
PCB_IO(const wxString &aName)
Definition pcb_io.h:319
const std::map< std::string, UTF8 > * m_props
Properties passed via Save() or Load(), no ownership, may be NULL.
Definition pcb_io.h:329
CHOOSE_PROJECT_HANDLER m_choose_project_handler
Callback to choose projects to import.
Container for project specific data.
Definition project.h:65
The common library.
#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
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:64
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.