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 <footprint.h>
33#include <progress_reporter.h>
34#include <common.h>
35#include <macros.h>
36
37#include <fstream>
38#include <wx/txtstrm.h>
39#include <wx/wfstream.h>
40#include <wx/mstream.h>
41#include <wx/zipstrm.h>
42#include <wx/log.h>
43
44#include <json_common.h>
46#include <core/map_helpers.h>
47#include <string_utf8_map.h>
48
49
51{
52 std::map<wxString, std::unique_ptr<FOOTPRINT>> m_Footprints;
53 std::map<wxString, EASYEDAPRO::BLOB> m_Blobs;
54 std::map<wxString, std::multimap<wxString, EASYEDAPRO::POURED>> m_Poured;
55};
56
57
58PCB_IO_EASYEDAPRO::PCB_IO_EASYEDAPRO() : PCB_IO( wxS( "EasyEDA (JLCEDA) Professional" ) )
59{
60}
61
62
64{
65 if( m_projectData )
66 delete m_projectData;
67}
68
69
70bool PCB_IO_EASYEDAPRO::CanReadBoard( const wxString& aFileName ) const
71{
72 if( aFileName.Lower().EndsWith( wxS( ".epro" ) ) )
73 {
74 return true;
75 }
76 else if( aFileName.Lower().EndsWith( wxS( ".zip" ) ) )
77 {
78 std::shared_ptr<wxZipEntry> entry;
79 wxFFileInputStream in( aFileName );
80 wxZipInputStream zip( in );
81
82 if( !zip.IsOk() )
83 return false;
84
85 while( entry.reset( zip.GetNextEntry() ), entry.get() != NULL )
86 {
87 wxString name = entry->GetName();
88
89 if( name == wxS( "project.json" ) )
90 return true;
91 }
92
93 return false;
94 }
95
96 return false;
97}
98
99
100BOARD* PCB_IO_EASYEDAPRO::LoadBoard( const wxString& aFileName, BOARD* aAppendToMe,
101 const STRING_UTF8_MAP* aProperties, PROJECT* aProject )
102{
103 m_props = aProperties;
104
105 m_board = aAppendToMe ? aAppendToMe : new BOARD();
106
107 // Give the filename to the board if it's new
108 if( !aAppendToMe )
109 m_board->SetFileName( aFileName );
110
112 {
113 m_progressReporter->Report( wxString::Format( _( "Loading %s..." ), aFileName ) );
114
116 THROW_IO_ERROR( _( "Open cancelled by user." ) );
117 }
118
119 PCB_IO_EASYEDAPRO_PARSER parser( nullptr, nullptr );
120
121 wxFileName fname( aFileName );
122
123 if( fname.GetExt() == wxS( "epro" ) || fname.GetExt() == wxS( "zip" ) )
124 {
125 nlohmann::json project = EASYEDAPRO::ReadProjectOrDeviceFile( aFileName );
126
127 wxString pcbToLoad;
128
129 if( m_props && m_props->Exists( "pcb_id" ) )
130 {
131 pcbToLoad = wxString::FromUTF8( aProperties->at( "pcb_id" ) );
132 }
133 else
134 {
135 std::map<wxString, wxString> prjPcbNames = project.at( "pcbs" );
136
137 if( prjPcbNames.size() == 1 )
138 {
139 pcbToLoad = prjPcbNames.begin()->first;
140 }
141 else
142 {
143 std::vector<IMPORT_PROJECT_DESC> chosen = m_choose_project_handler(
145
146 if( chosen.size() > 0 )
147 pcbToLoad = chosen[0].PCBId;
148 }
149 }
150
151 if( pcbToLoad.empty() )
152 return nullptr;
153
154 LoadAllDataFromProject( aFileName, project );
155
156 if( !m_projectData )
157 return nullptr;
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> lines = EASYEDAPRO::ParseJsonLines( zip, name );
168
169 wxString boardKey = pcbUuid + wxS( "_0" );
170 wxScopedCharBuffer cb = boardKey.ToUTF8();
171 wxString boardPouredKey = wxBase64Encode( cb.data(), cb.length() );
172
173 const std::multimap<wxString, EASYEDAPRO::POURED>& boardPoured =
174 get_def( m_projectData->m_Poured, boardPouredKey );
175
177 m_projectData->m_Blobs, boardPoured, lines,
178 EASYEDAPRO::ShortenLibName( fname.GetName() ) );
179
181 };
182 EASYEDAPRO::IterateZipFiles( aFileName, cb );
183 }
184
185 return m_board;
186}
187
188
189long long PCB_IO_EASYEDAPRO::GetLibraryTimestamp( const wxString& aLibraryPath ) const
190{
191 return 0;
192}
193
194
195void PCB_IO_EASYEDAPRO::FootprintEnumerate( wxArrayString& aFootprintNames,
196 const wxString& aLibraryPath, bool aBestEfforts,
197 const STRING_UTF8_MAP* aProperties )
198{
199 wxFileName fname( aLibraryPath );
200
201 if( fname.GetExt() == wxS( "efoo" ) )
202 {
203 wxFFileInputStream ffis( aLibraryPath );
204 wxTextInputStream txt( ffis, wxS( " " ), wxConvUTF8 );
205
206 while( ffis.CanRead() )
207 {
208 wxString line = txt.ReadLine();
209
210 if( !line.Contains( wxS( "ATTR" ) ) )
211 continue; // Don't bother parsing
212
213 nlohmann::json js = nlohmann::json::parse( line );
214 if( js.at( 0 ) == "ATTR" && js.at( 7 ) == "Footprint" )
215 {
216 aFootprintNames.Add( js.at( 8 ).get<wxString>() );
217 }
218 }
219 }
220 else if( fname.GetExt() == wxS( "elibz" ) || fname.GetExt() == wxS( "epro" )
221 || fname.GetExt() == wxS( "zip" ) )
222 {
223 nlohmann::json project = EASYEDAPRO::ReadProjectOrDeviceFile( aLibraryPath );
224 std::map<wxString, nlohmann::json> footprintMap = project.at( "footprints" );
225
226 for( auto& [key, value] : footprintMap )
227 {
228 wxString title;
229
230 if( value.contains( "display_title" ) )
231 title = value.at( "display_title" ).get<wxString>();
232 else
233 title = value.at( "title" ).get<wxString>();
234
235 aFootprintNames.Add( title );
236 }
237 }
238}
239
240
241void PCB_IO_EASYEDAPRO::LoadAllDataFromProject( const wxString& aProjectPath,
242 const nlohmann::json& aProject )
243{
244 if( m_projectData )
245 delete m_projectData;
246
247 m_projectData = new PRJ_DATA();
248
249 PCB_IO_EASYEDAPRO_PARSER parser( nullptr, nullptr );
250 wxFileName fname( aProjectPath );
251 wxString fpLibName = EASYEDAPRO::ShortenLibName( fname.GetName() );
252
253 std::map<wxString, std::unique_ptr<FOOTPRINT>> result;
254
255 auto cb = [&]( const wxString& name, const wxString& baseName, wxInputStream& zip ) -> bool
256 {
257 if( !name.EndsWith( wxS( ".efoo" ) ) && !name.EndsWith( wxS( ".eblob" ) )
258 && !name.EndsWith( wxS( ".ecop" ) ) )
259 {
261 }
262
263 std::vector<nlohmann::json> lines = EASYEDAPRO::ParseJsonLines( zip, name );
264
265 if( name.EndsWith( wxS( ".efoo" ) ) )
266 {
267 nlohmann::json fpData = aProject.at( "footprints" ).at( baseName );
268 wxString fpTitle = fpData.at( "title" );
269
270 FOOTPRINT* footprint = parser.ParseFootprint( aProject, baseName, lines );
271
272 if( !footprint )
274
275 LIB_ID fpID = EASYEDAPRO::ToKiCadLibID( fpLibName, fpTitle );
276 footprint->SetFPID( fpID );
277
278 m_projectData->m_Footprints.emplace( baseName, footprint );
279 }
280 else if( name.EndsWith( wxS( ".eblob" ) ) )
281 {
282 for( const nlohmann::json& line : lines )
283 {
284 if( line.at( 0 ) == "BLOB" )
285 {
286 EASYEDAPRO::BLOB blob = line;
287 m_projectData->m_Blobs[blob.objectId] = blob;
288 }
289 }
290 }
291 else if( name.EndsWith( wxS( ".ecop" ) ) && EASYEDAPRO::IMPORT_POURED )
292 {
293 for( const nlohmann::json& line : lines )
294 {
295 if( line.at( 0 ) == "POURED" )
296 {
297 if( !line.at( 2 ).is_string() )
298 continue; // Unknown type of POURED
299
300 EASYEDAPRO::POURED poured = line;
301 m_projectData->m_Poured[baseName].emplace( poured.parentId, poured );
302 }
303 }
304 }
306 };
307 EASYEDAPRO::IterateZipFiles( aProjectPath, cb );
308}
309
310
311FOOTPRINT* PCB_IO_EASYEDAPRO::FootprintLoad( const wxString& aLibraryPath,
312 const wxString& aFootprintName, bool aKeepUUID,
313 const STRING_UTF8_MAP* aProperties )
314{
315 PCB_IO_EASYEDAPRO_PARSER parser( nullptr, nullptr );
316 FOOTPRINT* footprint = nullptr;
317
318 wxFileName libFname( aLibraryPath );
319
320 if( libFname.GetExt() == wxS( "efoo" ) )
321 {
322 wxFFileInputStream ffis( aLibraryPath );
323 wxTextInputStream txt( ffis, wxS( " " ), wxConvUTF8 );
324
325 std::vector<nlohmann::json> lines = EASYEDAPRO::ParseJsonLines( ffis, aLibraryPath );
326
327 for( const nlohmann::json& js : lines )
328 {
329 if( js.at( 0 ) == "ATTR" )
330 {
331 EASYEDAPRO::PCB_ATTR attr = js;
332
333 if( attr.key == wxS( "Footprint" ) && attr.value != aFootprintName )
334 return nullptr;
335 }
336 }
337
338 footprint = parser.ParseFootprint( nlohmann::json(), wxEmptyString, lines );
339
340 if( !footprint )
341 {
342 THROW_IO_ERROR( wxString::Format( _( "Cannot load footprint '%s' from '%s'" ),
343 aFootprintName, aLibraryPath ) );
344 }
345
346 LIB_ID fpID = EASYEDAPRO::ToKiCadLibID( wxEmptyString, aFootprintName );
347 footprint->SetFPID( fpID );
348
349 footprint->Reference().SetVisible( true );
350 footprint->Value().SetText( aFootprintName );
351 footprint->Value().SetVisible( true );
352 footprint->AutoPositionFields();
353 }
354 else if( libFname.GetExt() == wxS( "elibz" ) || libFname.GetExt() == wxS( "epro" )
355 || libFname.GetExt() == wxS( "zip" ) )
356 {
357 nlohmann::json project = EASYEDAPRO::ReadProjectOrDeviceFile( aLibraryPath );
358
359 wxString fpUuid;
360
361 std::map<wxString, nlohmann::json> footprintMap = project.at( "footprints" );
362 for( auto& [uuid, data] : footprintMap )
363 {
364 wxString title;
365
366 if( data.contains( "display_title" ) )
367 title = data.at( "display_title" ).get<wxString>();
368 else
369 title = data.at( "title" ).get<wxString>();
370
371 if( title == aFootprintName )
372 {
373 fpUuid = uuid;
374 break;
375 }
376 }
377
378 if( !fpUuid )
379 {
380 THROW_IO_ERROR( wxString::Format( _( "Footprint '%s' not found in project '%s'" ),
381 aFootprintName, aLibraryPath ) );
382 }
383
384 auto cb = [&]( const wxString& name, const wxString& baseName, wxInputStream& zip ) -> bool
385 {
386 if( !name.EndsWith( wxS( ".efoo" ) ) )
388
389 if( baseName != fpUuid )
391
392 std::vector<nlohmann::json> lines = EASYEDAPRO::ParseJsonLines( zip, name );
393
394 footprint = parser.ParseFootprint( project, fpUuid, lines );
395
396 if( !footprint )
397 {
398 THROW_IO_ERROR( wxString::Format( _( "Cannot load footprint '%s' from '%s'" ),
399 aFootprintName, aLibraryPath ) );
400 }
401
402 LIB_ID fpID = EASYEDAPRO::ToKiCadLibID( wxEmptyString, aFootprintName );
403 footprint->SetFPID( fpID );
404
405 footprint->Reference().SetVisible( true );
406 footprint->Value().SetText( aFootprintName );
407 footprint->Value().SetVisible( true );
408 footprint->AutoPositionFields();
409
411 };
412 EASYEDAPRO::IterateZipFiles( aLibraryPath, cb );
413 }
414
415 return footprint;
416}
417
418
420{
421 std::vector<FOOTPRINT*> result;
422
423 if( !m_projectData )
424 return result;
425
426 for( auto& [fpUuid, footprint] : m_projectData->m_Footprints )
427 {
428 result.push_back( static_cast<FOOTPRINT*>( footprint->Clone() ) );
429 }
430
431 return result;
432}
const char * name
Definition: DXF_plotter.cpp:57
Information pertinent to a Pcbnew printed circuit board.
Definition: board.h:282
void SetFileName(const wxString &aFileName)
Definition: board.h:317
virtual void SetVisible(bool aVisible)
Definition: eda_text.cpp:243
virtual void SetText(const wxString &aText)
Definition: eda_text.cpp:181
void SetFPID(const LIB_ID &aFPID)
Definition: footprint.h:234
PCB_FIELD & Value()
read/write accessors:
Definition: footprint.h:622
PCB_FIELD & Reference()
Definition: footprint.h:623
void AutoPositionFields()
Position Reference and Value fields at the top and bottom of footprint's bounding box.
Definition: footprint.cpp:2576
PROGRESS_REPORTER * m_progressReporter
Progress reporter to track the progress of the operation, may be nullptr.
Definition: io_base.h:213
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)
BOARD * LoadBoard(const wxString &aFileName, BOARD *aAppendToMe, const STRING_UTF8_MAP *aProperties=nullptr, PROJECT *aProject=nullptr) override
Load information from some input file format that this PCB_IO implementation knows about into either ...
void LoadAllDataFromProject(const wxString &aLibraryPath, const nlohmann::json &aProject)
PRJ_DATA * m_projectData
FOOTPRINT * FootprintLoad(const wxString &aLibraryPath, const wxString &aFootprintName, bool aKeepUUID=false, const STRING_UTF8_MAP *aProperties=nullptr) override
Load a footprint having aFootprintName from the aLibraryPath containing a library format that this PC...
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.
void FootprintEnumerate(wxArrayString &aFootprintNames, const wxString &aLibraryPath, bool aBestEfforts, const STRING_UTF8_MAP *aProperties=nullptr) override
Return a list of footprint names contained within the library at aLibraryPath.
long long GetLibraryTimestamp(const wxString &aLibraryPath) const override
Generate a timestamp representing all the files in the library (including the library directory).
A base class that BOARD loading and saving plugins should derive from.
Definition: pcb_io.h:72
BOARD * m_board
The board BOARD being worked on, no ownership here.
Definition: pcb_io.h:343
const STRING_UTF8_MAP * m_props
Properties passed via Save() or Load(), no ownership, may be NULL.
Definition: pcb_io.h:346
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:62
A name/value tuple with unique names and optional values.
bool Exists(const std::string &aProperty) const
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