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