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
126 if( fname.GetExt() == wxS( "epro" ) || fname.GetExt() == wxS( "zip" ) )
127 {
128 nlohmann::json project = EASYEDAPRO::ReadProjectOrDeviceFile( aFileName );
129
130 wxString pcbToLoad;
131
132 if( m_props && m_props->contains( "pcb_id" ) )
133 {
134 pcbToLoad = wxString::FromUTF8( m_props->at( "pcb_id" ) );
135 }
136 else
137 {
138 std::map<wxString, wxString> prjPcbNames = project.at( "pcbs" );
139
140 if( prjPcbNames.size() == 1 )
141 {
142 pcbToLoad = prjPcbNames.begin()->first;
143 }
144 else
145 {
146 std::vector<IMPORT_PROJECT_DESC> chosen = m_choose_project_handler(
148
149 if( chosen.size() > 0 )
150 pcbToLoad = chosen[0].PCBId;
151 }
152 }
153
154 if( pcbToLoad.empty() )
155 return nullptr;
156
157 LoadAllDataFromProject( aFileName, project );
158
159 if( !m_projectData )
160 return nullptr;
161
162 auto cb = [&]( const wxString& name, const wxString& pcbUuid, wxInputStream& zip ) -> bool
163 {
164 if( !name.EndsWith( wxS( ".epcb" ) ) )
166
167 if( pcbUuid != pcbToLoad )
169
170 std::vector<nlohmann::json> lines = EASYEDAPRO::ParseJsonLines( zip, name );
171
172 wxString boardKey = pcbUuid + wxS( "_0" );
173 wxScopedCharBuffer cb = boardKey.ToUTF8();
174 wxString boardPouredKey = wxBase64Encode( cb.data(), cb.length() );
175
176 const std::multimap<wxString, EASYEDAPRO::POURED>& boardPoured =
177 get_def( m_projectData->m_Poured, boardPouredKey );
178
180 m_projectData->m_Blobs, boardPoured, lines,
181 EASYEDAPRO::ShortenLibName( fname.GetName() ) );
182
184 };
185 EASYEDAPRO::IterateZipFiles( aFileName, cb );
186 }
187
188 return m_board;
189}
190
191
192long long PCB_IO_EASYEDAPRO::GetLibraryTimestamp( const wxString& aLibraryPath ) const
193{
194 return 0;
195}
196
197
198void PCB_IO_EASYEDAPRO::FootprintEnumerate( wxArrayString& aFootprintNames,
199 const wxString& aLibraryPath, bool aBestEfforts,
200 const std::map<std::string, UTF8>* aProperties )
201{
202 wxFileName fname( aLibraryPath );
203
204 if( fname.GetExt() == wxS( "efoo" ) )
205 {
206 wxFFileInputStream ffis( aLibraryPath );
207 wxTextInputStream txt( ffis, wxS( " " ), wxConvUTF8 );
208
209 while( ffis.CanRead() )
210 {
211 wxString line = txt.ReadLine();
212
213 if( !line.Contains( wxS( "ATTR" ) ) )
214 continue; // Don't bother parsing
215
216 nlohmann::json js = nlohmann::json::parse( line );
217 if( js.at( 0 ) == "ATTR" && js.at( 7 ) == "Footprint" )
218 {
219 aFootprintNames.Add( js.at( 8 ).get<wxString>() );
220 }
221 }
222 }
223 else if( fname.GetExt() == wxS( "elibz" ) || fname.GetExt() == wxS( "epro" )
224 || fname.GetExt() == wxS( "zip" ) )
225 {
226 nlohmann::json project = EASYEDAPRO::ReadProjectOrDeviceFile( aLibraryPath );
227 std::map<wxString, nlohmann::json> footprintMap = project.at( "footprints" );
228
229 for( auto& [key, value] : footprintMap )
230 {
231 wxString title;
232
233 if( value.contains( "display_title" ) )
234 title = value.at( "display_title" ).get<wxString>();
235 else
236 title = value.at( "title" ).get<wxString>();
237
238 aFootprintNames.Add( title );
239 }
240 }
241}
242
243
244void PCB_IO_EASYEDAPRO::LoadAllDataFromProject( const wxString& aProjectPath,
245 const nlohmann::json& aProject )
246{
247 if( m_projectData )
248 delete m_projectData;
249
250 m_projectData = new PRJ_DATA();
251
252 PCB_IO_EASYEDAPRO_PARSER parser( nullptr, nullptr );
253 wxFileName fname( aProjectPath );
254 wxString fpLibName = EASYEDAPRO::ShortenLibName( fname.GetName() );
255
256 std::map<wxString, std::unique_ptr<FOOTPRINT>> result;
257
258 auto cb = [&]( const wxString& name, const wxString& baseName, wxInputStream& zip ) -> bool
259 {
260 if( !name.EndsWith( wxS( ".efoo" ) ) && !name.EndsWith( wxS( ".eblob" ) )
261 && !name.EndsWith( wxS( ".ecop" ) ) )
262 {
264 }
265
266 std::vector<nlohmann::json> lines = EASYEDAPRO::ParseJsonLines( zip, name );
267
268 if( name.EndsWith( wxS( ".efoo" ) ) )
269 {
270 nlohmann::json fpData = aProject.at( "footprints" ).at( baseName );
271 wxString fpTitle = fpData.at( "title" );
272
273 FOOTPRINT* footprint = parser.ParseFootprint( aProject, baseName, lines );
274
275 if( !footprint )
277
278 LIB_ID fpID = EASYEDAPRO::ToKiCadLibID( fpLibName, fpTitle );
279 footprint->SetFPID( fpID );
280
281 m_projectData->m_Footprints.emplace( baseName, footprint );
282 }
283 else if( name.EndsWith( wxS( ".eblob" ) ) )
284 {
285 for( const nlohmann::json& line : lines )
286 {
287 if( line.at( 0 ) == "BLOB" )
288 {
289 EASYEDAPRO::BLOB blob = line;
290 m_projectData->m_Blobs[blob.objectId] = blob;
291 }
292 }
293 }
294 else if( name.EndsWith( wxS( ".ecop" ) ) && EASYEDAPRO::IMPORT_POURED )
295 {
296 for( const nlohmann::json& line : lines )
297 {
298 if( line.at( 0 ) == "POURED" )
299 {
300 if( !line.at( 2 ).is_string() )
301 continue; // Unknown type of POURED
302
303 EASYEDAPRO::POURED poured = line;
304 m_projectData->m_Poured[baseName].emplace( poured.parentId, poured );
305 }
306 }
307 }
309 };
310 EASYEDAPRO::IterateZipFiles( aProjectPath, cb );
311}
312
313
314FOOTPRINT* PCB_IO_EASYEDAPRO::FootprintLoad( const wxString& aLibraryPath,
315 const wxString& aFootprintName, bool aKeepUUID,
316 const std::map<std::string, UTF8>* aProperties )
317{
319
320 PCB_IO_EASYEDAPRO_PARSER parser( nullptr, nullptr );
321 FOOTPRINT* footprint = nullptr;
322
323 wxFileName libFname( aLibraryPath );
324
325 if( libFname.GetExt() == wxS( "efoo" ) )
326 {
327 wxFFileInputStream ffis( aLibraryPath );
328 wxTextInputStream txt( ffis, wxS( " " ), wxConvUTF8 );
329
330 std::vector<nlohmann::json> lines = EASYEDAPRO::ParseJsonLines( ffis, aLibraryPath );
331
332 for( const nlohmann::json& js : lines )
333 {
334 if( js.at( 0 ) == "ATTR" )
335 {
336 EASYEDAPRO::PCB_ATTR attr = js;
337
338 if( attr.key == wxS( "Footprint" ) && attr.value != aFootprintName )
339 return nullptr;
340 }
341 }
342
343 footprint = parser.ParseFootprint( nlohmann::json(), wxEmptyString, lines );
344
345 if( !footprint )
346 {
347 THROW_IO_ERROR( wxString::Format( _( "Cannot load footprint '%s' from '%s'" ),
348 aFootprintName, aLibraryPath ) );
349 }
350
351 LIB_ID fpID = EASYEDAPRO::ToKiCadLibID( wxEmptyString, aFootprintName );
352 footprint->SetFPID( fpID );
353
354 footprint->Reference().SetVisible( true );
355 footprint->Value().SetText( aFootprintName );
356 footprint->Value().SetVisible( true );
357 footprint->AutoPositionFields();
358 }
359 else if( libFname.GetExt() == wxS( "elibz" ) || libFname.GetExt() == wxS( "epro" )
360 || libFname.GetExt() == wxS( "zip" ) )
361 {
362 nlohmann::json project = EASYEDAPRO::ReadProjectOrDeviceFile( aLibraryPath );
363
364 wxString fpUuid;
365
366 std::map<wxString, nlohmann::json> footprintMap = project.at( "footprints" );
367 for( auto& [uuid, data] : footprintMap )
368 {
369 wxString title;
370
371 if( data.contains( "display_title" ) )
372 title = data.at( "display_title" ).get<wxString>();
373 else
374 title = data.at( "title" ).get<wxString>();
375
376 if( title == aFootprintName )
377 {
378 fpUuid = uuid;
379 break;
380 }
381 }
382
383 if( !fpUuid )
384 {
385 THROW_IO_ERROR( wxString::Format( _( "Footprint '%s' not found in project '%s'" ),
386 aFootprintName, aLibraryPath ) );
387 }
388
389 auto cb = [&]( const wxString& name, const wxString& baseName, wxInputStream& zip ) -> bool
390 {
391 if( !name.EndsWith( wxS( ".efoo" ) ) )
393
394 if( baseName != fpUuid )
396
397 std::vector<nlohmann::json> lines = EASYEDAPRO::ParseJsonLines( zip, name );
398
399 footprint = parser.ParseFootprint( project, fpUuid, lines );
400
401 if( !footprint )
402 {
403 THROW_IO_ERROR( wxString::Format( _( "Cannot load footprint '%s' from '%s'" ),
404 aFootprintName, aLibraryPath ) );
405 }
406
407 LIB_ID fpID = EASYEDAPRO::ToKiCadLibID( wxEmptyString, aFootprintName );
408 footprint->SetFPID( fpID );
409
410 footprint->Reference().SetVisible( true );
411 footprint->Value().SetText( aFootprintName );
412 footprint->Value().SetVisible( true );
413 footprint->AutoPositionFields();
414
416 };
417 EASYEDAPRO::IterateZipFiles( aLibraryPath, cb );
418 }
419
420 return footprint;
421}
422
423
425{
426 std::vector<FOOTPRINT*> result;
427
428 if( !m_projectData )
429 return result;
430
431 for( auto& [fpUuid, footprint] : m_projectData->m_Footprints )
432 {
433 result.push_back( static_cast<FOOTPRINT*>( footprint->Clone() ) );
434 }
435
436 return result;
437}
const char * name
Definition: DXF_plotter.cpp:57
Information pertinent to a Pcbnew printed circuit board.
Definition: board.h:289
void SetFileName(const wxString &aFileName)
Definition: board.h:324
virtual void SetVisible(bool aVisible)
Definition: eda_text.cpp:275
virtual void SetText(const wxString &aText)
Definition: eda_text.cpp:182
void SetFPID(const LIB_ID &aFPID)
Definition: footprint.h:248
PCB_FIELD & Value()
read/write accessors:
Definition: footprint.h:637
PCB_FIELD & Reference()
Definition: footprint.h:638
void AutoPositionFields()
Position Reference and Value fields at the top and bottom of footprint's bounding box.
Definition: footprint.cpp:2609
PROGRESS_REPORTER * m_progressReporter
Progress reporter to track the progress of the operation, may be nullptr.
Definition: io_base.h:220
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:63
static REPORTER & GetInstance()
Definition: reporter.cpp:175
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
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