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#include <string_utf8_map.h>
50
51
53{
54 std::map<wxString, std::unique_ptr<FOOTPRINT>> m_Footprints;
55 std::map<wxString, EASYEDAPRO::BLOB> m_Blobs;
56 std::map<wxString, std::multimap<wxString, EASYEDAPRO::POURED>> m_Poured;
57};
58
59
60PCB_IO_EASYEDAPRO::PCB_IO_EASYEDAPRO() : PCB_IO( wxS( "EasyEDA (JLCEDA) Professional" ) )
61{
62}
63
64
66{
67 if( m_projectData )
68 delete m_projectData;
69}
70
71
72bool PCB_IO_EASYEDAPRO::CanReadBoard( const wxString& aFileName ) const
73{
74 if( aFileName.Lower().EndsWith( wxS( ".epro" ) ) )
75 {
76 return true;
77 }
78 else if( aFileName.Lower().EndsWith( wxS( ".zip" ) ) )
79 {
80 std::shared_ptr<wxZipEntry> entry;
81 wxFFileInputStream in( aFileName );
82 wxZipInputStream zip( in );
83
84 if( !zip.IsOk() )
85 return false;
86
87 while( entry.reset( zip.GetNextEntry() ), entry.get() != NULL )
88 {
89 wxString name = entry->GetName();
90
91 if( name == wxS( "project.json" ) )
92 return true;
93 }
94
95 return false;
96 }
97
98 return false;
99}
100
101
102BOARD* PCB_IO_EASYEDAPRO::LoadBoard( const wxString& aFileName, BOARD* aAppendToMe,
103 const STRING_UTF8_MAP* aProperties, PROJECT* aProject )
104{
105 m_props = aProperties;
106
107 m_board = aAppendToMe ? aAppendToMe : new BOARD();
108
109 // Give the filename to the board if it's new
110 if( !aAppendToMe )
111 m_board->SetFileName( aFileName );
112
114
116 {
117 m_progressReporter->Report( wxString::Format( _( "Loading %s..." ), aFileName ) );
118
120 THROW_IO_ERROR( _( "Open cancelled by user." ) );
121 }
122
123 PCB_IO_EASYEDAPRO_PARSER parser( nullptr, nullptr );
124
125 wxFileName fname( aFileName );
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->Exists( "pcb_id" ) )
134 {
135 pcbToLoad = wxString::FromUTF8( aProperties->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> lines = EASYEDAPRO::ParseJsonLines( zip, name );
172
173 wxString boardKey = pcbUuid + wxS( "_0" );
174 wxScopedCharBuffer cb = boardKey.ToUTF8();
175 wxString boardPouredKey = wxBase64Encode( cb.data(), cb.length() );
176
177 const std::multimap<wxString, EASYEDAPRO::POURED>& boardPoured =
178 get_def( m_projectData->m_Poured, boardPouredKey );
179
181 m_projectData->m_Blobs, boardPoured, lines,
182 EASYEDAPRO::ShortenLibName( fname.GetName() ) );
183
185 };
186 EASYEDAPRO::IterateZipFiles( aFileName, cb );
187 }
188
189 return m_board;
190}
191
192
193long long PCB_IO_EASYEDAPRO::GetLibraryTimestamp( const wxString& aLibraryPath ) const
194{
195 return 0;
196}
197
198
199void PCB_IO_EASYEDAPRO::FootprintEnumerate( wxArrayString& aFootprintNames,
200 const wxString& aLibraryPath, bool aBestEfforts,
201 const STRING_UTF8_MAP* aProperties )
202{
203 wxFileName fname( aLibraryPath );
204
205 if( fname.GetExt() == wxS( "efoo" ) )
206 {
207 wxFFileInputStream ffis( aLibraryPath );
208 wxTextInputStream txt( ffis, wxS( " " ), wxConvUTF8 );
209
210 while( ffis.CanRead() )
211 {
212 wxString line = txt.ReadLine();
213
214 if( !line.Contains( wxS( "ATTR" ) ) )
215 continue; // Don't bother parsing
216
217 nlohmann::json js = nlohmann::json::parse( line );
218 if( js.at( 0 ) == "ATTR" && js.at( 7 ) == "Footprint" )
219 {
220 aFootprintNames.Add( js.at( 8 ).get<wxString>() );
221 }
222 }
223 }
224 else if( fname.GetExt() == wxS( "elibz" ) || fname.GetExt() == wxS( "epro" )
225 || fname.GetExt() == wxS( "zip" ) )
226 {
227 nlohmann::json project = EASYEDAPRO::ReadProjectOrDeviceFile( aLibraryPath );
228 std::map<wxString, nlohmann::json> footprintMap = project.at( "footprints" );
229
230 for( auto& [key, value] : footprintMap )
231 {
232 wxString title;
233
234 if( value.contains( "display_title" ) )
235 title = value.at( "display_title" ).get<wxString>();
236 else
237 title = value.at( "title" ).get<wxString>();
238
239 aFootprintNames.Add( title );
240 }
241 }
242}
243
244
245void PCB_IO_EASYEDAPRO::LoadAllDataFromProject( const wxString& aProjectPath,
246 const nlohmann::json& aProject )
247{
248 if( m_projectData )
249 delete m_projectData;
250
251 m_projectData = new PRJ_DATA();
252
253 PCB_IO_EASYEDAPRO_PARSER parser( nullptr, nullptr );
254 wxFileName fname( aProjectPath );
255 wxString fpLibName = EASYEDAPRO::ShortenLibName( fname.GetName() );
256
257 std::map<wxString, std::unique_ptr<FOOTPRINT>> result;
258
259 auto cb = [&]( const wxString& name, const wxString& baseName, wxInputStream& zip ) -> bool
260 {
261 if( !name.EndsWith( wxS( ".efoo" ) ) && !name.EndsWith( wxS( ".eblob" ) )
262 && !name.EndsWith( wxS( ".ecop" ) ) )
263 {
265 }
266
267 std::vector<nlohmann::json> lines = EASYEDAPRO::ParseJsonLines( zip, name );
268
269 if( name.EndsWith( wxS( ".efoo" ) ) )
270 {
271 nlohmann::json fpData = aProject.at( "footprints" ).at( baseName );
272 wxString fpTitle = fpData.at( "title" );
273
274 FOOTPRINT* footprint = parser.ParseFootprint( aProject, baseName, lines );
275
276 if( !footprint )
278
279 LIB_ID fpID = EASYEDAPRO::ToKiCadLibID( fpLibName, fpTitle );
280 footprint->SetFPID( fpID );
281
282 m_projectData->m_Footprints.emplace( baseName, footprint );
283 }
284 else if( name.EndsWith( wxS( ".eblob" ) ) )
285 {
286 for( const nlohmann::json& line : lines )
287 {
288 if( line.at( 0 ) == "BLOB" )
289 {
290 EASYEDAPRO::BLOB blob = line;
291 m_projectData->m_Blobs[blob.objectId] = blob;
292 }
293 }
294 }
295 else if( name.EndsWith( wxS( ".ecop" ) ) && EASYEDAPRO::IMPORT_POURED )
296 {
297 for( const nlohmann::json& line : lines )
298 {
299 if( line.at( 0 ) == "POURED" )
300 {
301 if( !line.at( 2 ).is_string() )
302 continue; // Unknown type of POURED
303
304 EASYEDAPRO::POURED poured = line;
305 m_projectData->m_Poured[baseName].emplace( poured.parentId, poured );
306 }
307 }
308 }
310 };
311 EASYEDAPRO::IterateZipFiles( aProjectPath, cb );
312}
313
314
315FOOTPRINT* PCB_IO_EASYEDAPRO::FootprintLoad( const wxString& aLibraryPath,
316 const wxString& aFootprintName, bool aKeepUUID,
317 const STRING_UTF8_MAP* aProperties )
318{
320
321 PCB_IO_EASYEDAPRO_PARSER parser( nullptr, nullptr );
322 FOOTPRINT* footprint = nullptr;
323
324 wxFileName libFname( aLibraryPath );
325
326 if( libFname.GetExt() == wxS( "efoo" ) )
327 {
328 wxFFileInputStream ffis( aLibraryPath );
329 wxTextInputStream txt( ffis, wxS( " " ), wxConvUTF8 );
330
331 std::vector<nlohmann::json> lines = EASYEDAPRO::ParseJsonLines( ffis, aLibraryPath );
332
333 for( const nlohmann::json& js : lines )
334 {
335 if( js.at( 0 ) == "ATTR" )
336 {
337 EASYEDAPRO::PCB_ATTR attr = js;
338
339 if( attr.key == wxS( "Footprint" ) && attr.value != aFootprintName )
340 return nullptr;
341 }
342 }
343
344 footprint = parser.ParseFootprint( nlohmann::json(), wxEmptyString, lines );
345
346 if( !footprint )
347 {
348 THROW_IO_ERROR( wxString::Format( _( "Cannot load footprint '%s' from '%s'" ),
349 aFootprintName, aLibraryPath ) );
350 }
351
352 LIB_ID fpID = EASYEDAPRO::ToKiCadLibID( wxEmptyString, aFootprintName );
353 footprint->SetFPID( fpID );
354
355 footprint->Reference().SetVisible( true );
356 footprint->Value().SetText( aFootprintName );
357 footprint->Value().SetVisible( true );
358 footprint->AutoPositionFields();
359 }
360 else if( libFname.GetExt() == wxS( "elibz" ) || libFname.GetExt() == wxS( "epro" )
361 || libFname.GetExt() == wxS( "zip" ) )
362 {
363 nlohmann::json project = EASYEDAPRO::ReadProjectOrDeviceFile( aLibraryPath );
364
365 wxString fpUuid;
366
367 std::map<wxString, nlohmann::json> footprintMap = project.at( "footprints" );
368 for( auto& [uuid, data] : footprintMap )
369 {
370 wxString title;
371
372 if( data.contains( "display_title" ) )
373 title = data.at( "display_title" ).get<wxString>();
374 else
375 title = data.at( "title" ).get<wxString>();
376
377 if( title == aFootprintName )
378 {
379 fpUuid = uuid;
380 break;
381 }
382 }
383
384 if( !fpUuid )
385 {
386 THROW_IO_ERROR( wxString::Format( _( "Footprint '%s' not found in project '%s'" ),
387 aFootprintName, aLibraryPath ) );
388 }
389
390 auto cb = [&]( const wxString& name, const wxString& baseName, wxInputStream& zip ) -> bool
391 {
392 if( !name.EndsWith( wxS( ".efoo" ) ) )
394
395 if( baseName != fpUuid )
397
398 std::vector<nlohmann::json> lines = EASYEDAPRO::ParseJsonLines( zip, name );
399
400 footprint = parser.ParseFootprint( project, fpUuid, lines );
401
402 if( !footprint )
403 {
404 THROW_IO_ERROR( wxString::Format( _( "Cannot load footprint '%s' from '%s'" ),
405 aFootprintName, aLibraryPath ) );
406 }
407
408 LIB_ID fpID = EASYEDAPRO::ToKiCadLibID( wxEmptyString, aFootprintName );
409 footprint->SetFPID( fpID );
410
411 footprint->Reference().SetVisible( true );
412 footprint->Value().SetText( aFootprintName );
413 footprint->Value().SetVisible( true );
414 footprint->AutoPositionFields();
415
417 };
418 EASYEDAPRO::IterateZipFiles( aLibraryPath, cb );
419 }
420
421 return footprint;
422}
423
424
426{
427 std::vector<FOOTPRINT*> result;
428
429 if( !m_projectData )
430 return result;
431
432 for( auto& [fpUuid, footprint] : m_projectData->m_Footprints )
433 {
434 result.push_back( static_cast<FOOTPRINT*>( footprint->Clone() ) );
435 }
436
437 return result;
438}
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:244
virtual void SetText(const wxString &aText)
Definition: eda_text.cpp:182
void SetFPID(const LIB_ID &aFPID)
Definition: footprint.h:238
PCB_FIELD & Value()
read/write accessors:
Definition: footprint.h:627
PCB_FIELD & Reference()
Definition: footprint.h:628
void AutoPositionFields()
Position Reference and Value fields at the top and bottom of footprint's bounding box.
Definition: footprint.cpp:2604
PROGRESS_REPORTER * m_progressReporter
Progress reporter to track the progress of the operation, may be nullptr.
Definition: io_base.h:218
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
static REPORTER & GetInstance()
Definition: reporter.cpp:212
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