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 (C) 2023 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
65{
66 if( m_projectData )
67 delete m_projectData;
68}
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
113
115 {
116 m_progressReporter->Report( wxString::Format( _( "Loading %s..." ), aFileName ) );
117
119 THROW_IO_ERROR( _( "Open cancelled by user." ) );
120 }
121
122 PCB_IO_EASYEDAPRO_PARSER parser( nullptr, nullptr );
123
124 wxFileName fname( aFileName );
125 wxString fpLibName = EASYEDAPRO::ShortenLibName( fname.GetName() );
126
127 if( fname.GetExt() == wxS( "epro" ) || fname.GetExt() == wxS( "zip" ) )
128 {
129 nlohmann::json project = EASYEDAPRO::ReadProjectOrDeviceFile( aFileName );
130
131 wxString pcbToLoad;
132
133 if( m_props && m_props->contains( "pcb_id" ) )
134 {
135 pcbToLoad = wxString::FromUTF8( m_props->at( "pcb_id" ) );
136 }
137 else
138 {
139 std::map<wxString, wxString> prjPcbNames = project.at( "pcbs" );
140
141 if( prjPcbNames.size() == 1 )
142 {
143 pcbToLoad = prjPcbNames.begin()->first;
144 }
145 else
146 {
147 std::vector<IMPORT_PROJECT_DESC> chosen = m_choose_project_handler(
149
150 if( chosen.size() > 0 )
151 pcbToLoad = chosen[0].PCBId;
152 }
153 }
154
155 if( pcbToLoad.empty() )
156 return nullptr;
157
158 LoadAllDataFromProject( aFileName, project );
159
160 if( !m_projectData )
161 return nullptr;
162
163 auto cb = [&]( const wxString& name, const wxString& pcbUuid, wxInputStream& zip ) -> bool
164 {
165 if( !name.EndsWith( wxS( ".epcb" ) ) )
167
168 if( pcbUuid != pcbToLoad )
170
171 std::vector<nlohmann::json>* pcbLines = nullptr;
172
173 std::vector<std::vector<nlohmann::json>> lineBlocks =
175
176 if( lineBlocks.empty() )
178
179 if( lineBlocks.size() > 1 )
180 {
181 for( std::vector<nlohmann::json>& block : lineBlocks )
182 {
183 wxString docType;
184 nlohmann::json headData;
185
186 for( const nlohmann::json& line : block )
187 {
188 if( line.size() < 2 )
189 continue;
190
191 if( !line.at( 0 ).is_string() )
192 continue;
193
194 wxString lineType = line.at( 0 ).get<wxString>();
195
196 if( lineType == wxS( "DOCTYPE" ) )
197 {
198 if( !line.at( 1 ).is_string() )
199 continue;
200
201 docType = line.at( 1 ).get<wxString>();
202 }
203 else if( lineType == wxS( "HEAD" ) )
204 {
205 if( !line.at( 1 ).is_object() )
206 continue;
207
208 headData = line.at( 1 );
209 }
210 }
211
212 if( docType == wxS( "FOOTPRINT" ) )
213 {
214 wxString fpUuid = headData.at( "uuid" );
215 wxString fpTitle = headData.at( "title" );
216
217 FOOTPRINT* footprint = parser.ParseFootprint( project, fpUuid, block );
218
219 if( !footprint )
221
222 LIB_ID fpID = EASYEDAPRO::ToKiCadLibID( fpLibName, fpTitle );
223 footprint->SetFPID( fpID );
224
225 m_projectData->m_Footprints.emplace( fpUuid, footprint );
226 }
227 else if( docType == wxS( "PCB" ) )
228 {
229 pcbLines = &block;
230 }
231 }
232 }
233
234 if( pcbLines == nullptr )
235 pcbLines = &lineBlocks[0];
236
237 wxString boardKey = pcbUuid + wxS( "_0" );
238 wxScopedCharBuffer cb = boardKey.ToUTF8();
239 wxString boardPouredKey = wxBase64Encode( cb.data(), cb.length() );
240
241 const std::multimap<wxString, EASYEDAPRO::POURED>& boardPoured =
242 get_def( m_projectData->m_Poured, boardPouredKey );
243
245 m_projectData->m_Blobs, boardPoured, *pcbLines,
246 EASYEDAPRO::ShortenLibName( fname.GetName() ) );
247
249 };
250 EASYEDAPRO::IterateZipFiles( aFileName, cb );
251 }
252
253 return m_board;
254}
255
256
257long long PCB_IO_EASYEDAPRO::GetLibraryTimestamp( const wxString& aLibraryPath ) const
258{
259 return 0;
260}
261
262
263void PCB_IO_EASYEDAPRO::FootprintEnumerate( wxArrayString& aFootprintNames,
264 const wxString& aLibraryPath, bool aBestEfforts,
265 const std::map<std::string, UTF8>* aProperties )
266{
267 wxFileName fname( aLibraryPath );
268
269 if( fname.GetExt() == wxS( "efoo" ) )
270 {
271 wxFFileInputStream ffis( aLibraryPath );
272 wxTextInputStream txt( ffis, wxS( " " ), wxConvUTF8 );
273
274 while( ffis.CanRead() )
275 {
276 wxString line = txt.ReadLine();
277
278 if( !line.Contains( wxS( "ATTR" ) ) )
279 continue; // Don't bother parsing
280
281 nlohmann::json js = nlohmann::json::parse( line );
282 if( js.at( 0 ) == "ATTR" && js.at( 7 ) == "Footprint" )
283 {
284 aFootprintNames.Add( js.at( 8 ).get<wxString>() );
285 }
286 }
287 }
288 else if( fname.GetExt() == wxS( "elibz" ) || fname.GetExt() == wxS( "epro" )
289 || fname.GetExt() == wxS( "zip" ) )
290 {
291 nlohmann::json project = EASYEDAPRO::ReadProjectOrDeviceFile( aLibraryPath );
292 std::map<wxString, nlohmann::json> footprintMap = project.at( "footprints" );
293
294 for( auto& [key, value] : footprintMap )
295 {
296 wxString title;
297
298 if( value.contains( "display_title" ) )
299 title = value.at( "display_title" ).get<wxString>();
300 else
301 title = value.at( "title" ).get<wxString>();
302
303 aFootprintNames.Add( title );
304 }
305 }
306}
307
308
309void PCB_IO_EASYEDAPRO::LoadAllDataFromProject( const wxString& aProjectPath,
310 const nlohmann::json& aProject )
311{
312 if( m_projectData )
313 delete m_projectData;
314
315 m_projectData = new PRJ_DATA();
316
317 PCB_IO_EASYEDAPRO_PARSER parser( nullptr, nullptr );
318 wxFileName fname( aProjectPath );
319 wxString fpLibName = EASYEDAPRO::ShortenLibName( fname.GetName() );
320
321 std::map<wxString, std::unique_ptr<FOOTPRINT>> result;
322
323 auto cb = [&]( const wxString& name, const wxString& baseName, wxInputStream& zip ) -> bool
324 {
325 if( !name.EndsWith( wxS( ".efoo" ) ) && !name.EndsWith( wxS( ".eblob" ) )
326 && !name.EndsWith( wxS( ".ecop" ) ) )
327 {
329 }
330
331 std::vector<nlohmann::json> lines = EASYEDAPRO::ParseJsonLines( zip, name );
332
333 if( name.EndsWith( wxS( ".efoo" ) ) )
334 {
335 nlohmann::json fpData = aProject.at( "footprints" ).at( baseName );
336 wxString fpTitle = fpData.at( "title" );
337
338 FOOTPRINT* footprint = parser.ParseFootprint( aProject, baseName, lines );
339
340 if( !footprint )
342
343 LIB_ID fpID = EASYEDAPRO::ToKiCadLibID( fpLibName, fpTitle );
344 footprint->SetFPID( fpID );
345
346 m_projectData->m_Footprints.emplace( baseName, footprint );
347 }
348 else if( name.EndsWith( wxS( ".eblob" ) ) )
349 {
350 for( const nlohmann::json& line : lines )
351 {
352 if( line.at( 0 ) == "BLOB" )
353 {
354 EASYEDAPRO::BLOB blob = line;
355 m_projectData->m_Blobs[blob.objectId] = blob;
356 }
357 }
358 }
359 else if( name.EndsWith( wxS( ".ecop" ) ) && EASYEDAPRO::IMPORT_POURED_ECOP )
360 {
361 for( const nlohmann::json& line : lines )
362 {
363 if( line.at( 0 ) == "POURED" )
364 {
365 if( !line.at( 2 ).is_string() )
366 continue; // Unknown type of POURED
367
368 EASYEDAPRO::POURED poured = line;
369 m_projectData->m_Poured[baseName].emplace( poured.parentId, poured );
370 }
371 }
372 }
374 };
375 EASYEDAPRO::IterateZipFiles( aProjectPath, cb );
376}
377
378
379FOOTPRINT* PCB_IO_EASYEDAPRO::FootprintLoad( const wxString& aLibraryPath,
380 const wxString& aFootprintName, bool aKeepUUID,
381 const std::map<std::string, UTF8>* aProperties )
382{
384
385 PCB_IO_EASYEDAPRO_PARSER parser( nullptr, nullptr );
386 FOOTPRINT* footprint = nullptr;
387
388 wxFileName libFname( aLibraryPath );
389
390 if( libFname.GetExt() == wxS( "efoo" ) )
391 {
392 wxFFileInputStream ffis( aLibraryPath );
393 wxTextInputStream txt( ffis, wxS( " " ), wxConvUTF8 );
394
395 std::vector<nlohmann::json> lines = EASYEDAPRO::ParseJsonLines( ffis, aLibraryPath );
396
397 for( const nlohmann::json& js : lines )
398 {
399 if( js.at( 0 ) == "ATTR" )
400 {
401 EASYEDAPRO::PCB_ATTR attr = js;
402
403 if( attr.key == wxS( "Footprint" ) && attr.value != aFootprintName )
404 return nullptr;
405 }
406 }
407
408 footprint = parser.ParseFootprint( nlohmann::json(), wxEmptyString, lines );
409
410 if( !footprint )
411 {
412 THROW_IO_ERROR( wxString::Format( _( "Cannot load footprint '%s' from '%s'" ),
413 aFootprintName, aLibraryPath ) );
414 }
415
416 LIB_ID fpID = EASYEDAPRO::ToKiCadLibID( wxEmptyString, aFootprintName );
417 footprint->SetFPID( fpID );
418
419 footprint->Reference().SetVisible( true );
420 footprint->Value().SetText( aFootprintName );
421 footprint->Value().SetVisible( true );
422 footprint->AutoPositionFields();
423 }
424 else if( libFname.GetExt() == wxS( "elibz" ) || libFname.GetExt() == wxS( "epro" )
425 || libFname.GetExt() == wxS( "zip" ) )
426 {
427 nlohmann::json project = EASYEDAPRO::ReadProjectOrDeviceFile( aLibraryPath );
428
429 wxString fpUuid;
430
431 std::map<wxString, nlohmann::json> footprintMap = project.at( "footprints" );
432 for( auto& [uuid, data] : footprintMap )
433 {
434 wxString title;
435
436 if( data.contains( "display_title" ) )
437 title = data.at( "display_title" ).get<wxString>();
438 else
439 title = data.at( "title" ).get<wxString>();
440
441 if( title == aFootprintName )
442 {
443 fpUuid = uuid;
444 break;
445 }
446 }
447
448 if( !fpUuid )
449 {
450 THROW_IO_ERROR( wxString::Format( _( "Footprint '%s' not found in project '%s'" ),
451 aFootprintName, aLibraryPath ) );
452 }
453
454 auto cb = [&]( const wxString& name, const wxString& baseName, wxInputStream& zip ) -> bool
455 {
456 if( !name.EndsWith( wxS( ".efoo" ) ) )
458
459 if( baseName != fpUuid )
461
462 std::vector<nlohmann::json> lines = EASYEDAPRO::ParseJsonLines( zip, name );
463
464 footprint = parser.ParseFootprint( project, fpUuid, lines );
465
466 if( !footprint )
467 {
468 THROW_IO_ERROR( wxString::Format( _( "Cannot load footprint '%s' from '%s'" ),
469 aFootprintName, aLibraryPath ) );
470 }
471
472 LIB_ID fpID = EASYEDAPRO::ToKiCadLibID( wxEmptyString, aFootprintName );
473 footprint->SetFPID( fpID );
474
475 footprint->Reference().SetVisible( true );
476 footprint->Value().SetText( aFootprintName );
477 footprint->Value().SetVisible( true );
478 footprint->AutoPositionFields();
479
481 };
482 EASYEDAPRO::IterateZipFiles( aLibraryPath, cb );
483 }
484
485 return footprint;
486}
487
488
490{
491 std::vector<FOOTPRINT*> result;
492
493 if( !m_projectData )
494 return result;
495
496 for( auto& [fpUuid, footprint] : m_projectData->m_Footprints )
497 {
498 result.push_back( static_cast<FOOTPRINT*>( footprint->Clone() ) );
499 }
500
501 return result;
502}
const char * name
Definition: DXF_plotter.cpp:57
Information pertinent to a Pcbnew printed circuit board.
Definition: board.h:290
void SetFileName(const wxString &aFileName)
Definition: board.h:325
virtual void SetVisible(bool aVisible)
Definition: eda_text.cpp:377
virtual void SetText(const wxString &aText)
Definition: eda_text.cpp:269
void SetFPID(const LIB_ID &aFPID)
Definition: footprint.h:249
PCB_FIELD & Value()
read/write accessors:
Definition: footprint.h:658
PCB_FIELD & Reference()
Definition: footprint.h:659
void AutoPositionFields()
Position Reference and Value fields at the top and bottom of footprint's bounding box.
Definition: footprint.cpp:2644
PROGRESS_REPORTER * m_progressReporter
Progress reporter to track the progress of the operation, may be nullptr.
Definition: io_base.h:221
A logical library item identifier and consists of various portions much like a URI.
Definition: lib_id.h:49
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)
PRJ_DATA * m_projectData
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.
A base class that BOARD loading and saving plugins should derive from.
Definition: pcb_io.h:71
BOARD * m_board
The board BOARD being worked on, no ownership here.
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:345
virtual bool KeepRefreshing(bool aWait=false)=0
Update the UI (if any).
virtual void Report(const wxString &aMessage)=0
Display aMessage in the progress bar dialog.
CHOOSE_PROJECT_HANDLER m_choose_project_handler
Callback to choose projects to import.
Container for project specific data.
Definition: project.h:64
static REPORTER & GetInstance()
Definition: reporter.cpp:199
static void SetReporter(REPORTER *aReporter)
Set the reporter to use for reporting font substitution warnings.
Definition: fontconfig.cpp:64
The common library.
#define EASY_IT_BREAK
#define EASY_IT_CONTINUE
#define _(s)
#define THROW_IO_ERROR(msg)
Definition: ki_exception.h:39
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