KiCad PCB EDA Suite
Loading...
Searching...
No Matches
pcb_io_easyeda_plugin.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
24#include <pcb_io/pcb_io.h>
25
26#include <font/fontconfig.h>
27#include <progress_reporter.h>
28#include <common.h>
29#include <macros.h>
30#include <board.h>
31#include <footprint.h>
34#include <reporter.h>
35
36#include <wx/log.h>
37#include <wx/wfstream.h>
38#include <wx/zipstrm.h>
39#include <wx/stdstream.h>
40
41#include <json_common.h>
42#include <core/map_helpers.h>
43
44
45PCB_IO_EASYEDA::PCB_IO_EASYEDA() : PCB_IO( wxS( "EasyEDA (JLCEDA) Standard" ) )
46{
47}
48
49
53
54
55static bool FindBoardInStream( const wxString& aName, wxInputStream& aStream, nlohmann::json& aOut,
56 EASYEDA::DOCUMENT& aDoc )
57{
58 if( aName.Lower().EndsWith( wxS( ".json" ) ) )
59 {
60 wxStdInputStream sin( aStream );
61 nlohmann::json js = nlohmann::json::parse( sin, nullptr, false );
62
63 if( js.is_discarded() )
64 return false;
65
67
71 {
72 aOut = js;
73 aDoc = doc;
74 return true;
75 }
76 }
77 else if( aName.Lower().EndsWith( wxS( ".zip" ) ) )
78 {
79 std::shared_ptr<wxZipEntry> entry;
80 wxZipInputStream zip( aStream );
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( FindBoardInStream( name, zip, aOut, aDoc ) )
90 return true;
91 }
92 }
93
94 return false;
95}
96
97
98bool PCB_IO_EASYEDA::CanReadBoard( const wxString& aFileName ) const
99{
100 if( !PCB_IO::CanReadBoard( aFileName ) )
101 return false;
102
103 try
104 {
105 wxFFileInputStream in( aFileName );
106 nlohmann::json js;
108
109 return FindBoardInStream( aFileName, in, js, doc );
110 }
111 catch( nlohmann::json::exception& )
112 {
113 }
114 catch( std::exception& )
115 {
116 }
117
118 return false;
119}
120
121
122bool PCB_IO_EASYEDA::CanReadFootprint( const wxString& aFileName ) const
123{
124 return CanReadBoard( aFileName );
125}
126
127
128bool PCB_IO_EASYEDA::CanReadLibrary( const wxString& aFileName ) const
129{
130 return CanReadBoard( aFileName );
131}
132
133
134BOARD* PCB_IO_EASYEDA::LoadBoard( const wxString& aFileName, BOARD* aAppendToMe,
135 const std::map<std::string, UTF8>* aProperties, PROJECT* aProject )
136{
137 m_loadedFootprints.clear();
138
139 m_props = aProperties;
140 m_board = aAppendToMe ? aAppendToMe : new BOARD();
141
142 // Collect the font substitution warnings (RAII - automatically reset on scope exit)
144
145 // Give the filename to the board if it's new
146 if( !aAppendToMe )
147 m_board->SetFileName( aFileName );
148
150 {
151 m_progressReporter->Report( wxString::Format( _( "Loading %s..." ), aFileName ) );
152
153 if( !m_progressReporter->KeepRefreshing() )
154 THROW_IO_ERROR( _( "File import canceled by user." ) );
155 }
156
157 PCB_IO_EASYEDA_PARSER parser( nullptr );
158
159 try
160 {
161 wxFFileInputStream in( aFileName );
162 nlohmann::json js;
164
165 if( !FindBoardInStream( aFileName, in, js, doc ) )
166 {
168 wxString::Format( _( "Unable to find a valid board in '%s'" ), aFileName ) );
169 }
170
172
173 const int innerStart = 21;
174 const int innerEnd = 52;
175
176 int maxLayer = innerStart;
177 std::map<PCB_LAYER_ID, wxString> layerNames;
178
179 for( const wxString& layerLine : pcbDoc.layers )
180 {
181 wxArrayString parts = wxSplit( layerLine, '~', '\0' );
182 int layerId = wxAtoi( parts[0] );
183 wxString layerName = parts[1];
184 wxString layerColor = parts[2];
185 wxString visible = parts[3];
186 wxString active = parts[4];
187 bool enabled = parts[5] != wxS( "false" );
188
189 if( layerId >= innerStart && layerId <= innerEnd && enabled )
190 maxLayer = layerId + 1;
191
192 layerNames[parser.LayerToKi( parts[0] )] = layerName;
193 }
194
195 m_board->SetCopperLayerCount( 2 + maxLayer - innerStart );
196
197 for( auto& [klayer, name] : layerNames )
198 m_board->SetLayerName( klayer, name );
199
200 BOARD_DESIGN_SETTINGS& bds = m_board->GetDesignSettings();
201 std::shared_ptr<NETCLASS> defNetclass = bds.m_NetSettings->GetDefaultNetclass();
202
203 if( pcbDoc.DRCRULE )
204 {
205 std::map<wxString, nlohmann::json>& rules = *pcbDoc.DRCRULE;
206
207 if( auto defRules = get_opt( rules, "Default" ) )
208 {
209 wxString key;
210
211 key = wxS( "trackWidth" );
212 if( defRules->find( key ) != defRules->end() && defRules->at( key ).is_number() )
213 {
214 double val = parser.ScaleSize( defRules->at( key ) );
215 defNetclass->SetTrackWidth( val );
216 }
217
218 key = wxS( "clearance" );
219 if( defRules->find( key ) != defRules->end() && defRules->at( key ).is_number() )
220 {
221 double val = parser.ScaleSize( defRules->at( key ) );
222 defNetclass->SetClearance( val );
223 }
224
225 key = wxS( "viaHoleD" );
226 if( defRules->find( key ) != defRules->end() && defRules->at( key ).is_number() )
227 {
228 double val = parser.ScaleSize( defRules->at( key ) );
229
230 defNetclass->SetViaDrill( val );
231 }
232
233 key = wxS( "viaHoleDiameter" ); // Yes, this is via diameter, not drill diameter
234 if( defRules->find( key ) != defRules->end() && defRules->at( key ).is_number() )
235 {
236 double val = parser.ScaleSize( defRules->at( key ) );
237 defNetclass->SetViaDiameter( val );
238 }
239 }
240 }
241
242 VECTOR2D origin( doc.head.x, doc.head.y );
243 parser.ParseBoard( m_board, origin, m_loadedFootprints, doc.shape );
244
245 // Center the board
246 BOX2I outlineBbox = m_board->ComputeBoundingBox( true, true );
247 PAGE_INFO pageInfo = m_board->GetPageSettings();
248
249 VECTOR2D pageCenter( pcbIUScale.MilsToIU( pageInfo.GetWidthMils() / 2 ),
250 pcbIUScale.MilsToIU( pageInfo.GetHeightMils() / 2 ) );
251
252 VECTOR2D offset = pageCenter - outlineBbox.GetCenter();
253
254 int alignGrid = pcbIUScale.mmToIU( 10 );
255 offset.x = KiROUND( offset.x / alignGrid ) * alignGrid;
256 offset.y = KiROUND( offset.y / alignGrid ) * alignGrid;
257
258 m_board->Move( offset );
259 bds.SetAuxOrigin( offset );
260
261 return m_board;
262 }
263 catch( nlohmann::json::exception& e )
264 {
265 THROW_IO_ERROR( wxString::Format( _( "Error loading board '%s': %s" ), aFileName, e.what() ) );
266 }
267 catch( std::exception& e )
268 {
269 THROW_IO_ERROR( wxString::Format( _( "Error loading board '%s': %s" ), aFileName, e.what() ) );
270 }
271}
272
273
274long long PCB_IO_EASYEDA::GetLibraryTimestamp( const wxString& aLibraryPath ) const
275{
276 return 0;
277}
278
279
280void PCB_IO_EASYEDA::FootprintEnumerate( wxArrayString& aFootprintNames,
281 const wxString& aLibraryPath, bool aBestEfforts,
282 const std::map<std::string, UTF8>* aProperties )
283{
284 try
285 {
286 wxFFileInputStream in( aLibraryPath );
287 nlohmann::json js;
289
290 if( !FindBoardInStream( aLibraryPath, in, js, doc ) )
291 {
292 THROW_IO_ERROR( wxString::Format( _( "Unable to find valid footprints in '%s'" ),
293 aLibraryPath ) );
294 }
295
298 {
299 for( wxString shap : doc.shape )
300 {
301 shap.Replace( wxS( "#@$" ), "\n" );
302 wxArrayString parts = wxSplit( shap, '\n', '\0' );
303
304 if( parts.size() < 1 )
305 continue;
306
307 wxArrayString paramsRoot = wxSplit( parts[0], '~', '\0' );
308
309 if( paramsRoot.size() < 1 )
310 continue;
311
312 wxString rootType = paramsRoot[0];
313
314 if( rootType == wxS( "LIB" ) )
315 {
316 if( paramsRoot.size() < 4 )
317 continue;
318
319 wxString packageName = wxString::Format( wxS( "Unknown_%s_%s" ), paramsRoot[1],
320 paramsRoot[2] );
321
322 wxArrayString paramParts = wxSplit( paramsRoot[3], '`', '\0' );
323
324 std::map<wxString, wxString> paramMap;
325
326 for( int i = 1; i < (int) paramParts.size(); i += 2 )
327 {
328 wxString key = paramParts[i - 1];
329 wxString value = paramParts[i];
330
331 if( key == wxS( "package" ) )
332 packageName = value;
333
334 paramMap[key] = value;
335 }
336
337 aFootprintNames.Add( packageName );
338 }
339 }
340 }
342 {
344
345 wxString packageName = wxString::Format( wxS( "Unknown_%s" ),
346 pcbDoc.uuid.value_or( wxS( "Unknown" ) ) );
347
348 std::optional<std::map<wxString, wxString>> c_para;
349
350 if( pcbDoc.c_para )
351 c_para = pcbDoc.c_para;
352 else if( doc.head.c_para )
353 c_para = doc.head.c_para;
354
355 if( c_para )
356 packageName = get_def( *c_para, wxS( "package" ), packageName );
357
358 aFootprintNames.Add( packageName );
359 }
360 }
361 catch( nlohmann::json::exception& e )
362 {
363 THROW_IO_ERROR( wxString::Format( _( "Error enumerating footprints in library '%s': %s" ),
364 aLibraryPath, e.what() ) );
365 }
366 catch( std::exception& e )
367 {
368 THROW_IO_ERROR( wxString::Format( _( "Error enumerating footprints in library '%s': %s" ),
369 aLibraryPath, e.what() ) );
370 }
371}
372
373
374FOOTPRINT* PCB_IO_EASYEDA::FootprintLoad( const wxString& aLibraryPath,
375 const wxString& aFootprintName, bool aKeepUUID,
376 const std::map<std::string, UTF8>* aProperties )
377{
378 // Suppress font substitution warnings (RAII - automatically restored on scope exit)
379 FONTCONFIG_REPORTER_SCOPE fontconfigScope( nullptr );
380
381 PCB_IO_EASYEDA_PARSER parser( nullptr );
382
383 m_loadedFootprints.clear();
384
385 try
386 {
387 wxFFileInputStream in( aLibraryPath );
388 nlohmann::json js;
390
391 if( !FindBoardInStream( aLibraryPath, in, js, doc ) )
392 {
393 THROW_IO_ERROR( wxString::Format( _( "Unable to find valid footprints in '%s'" ),
394 aLibraryPath ) );
395 }
396
399 {
400 for( wxString shap : doc.shape )
401 {
402 if( !shap.Contains( wxS( "LIB" ) ) )
403 continue;
404
405 shap.Replace( wxS( "#@$" ), "\n" );
406 wxArrayString parts = wxSplit( shap, '\n', '\0' );
407
408 if( parts.size() < 1 )
409 continue;
410
411 wxArrayString paramsRoot = wxSplit( parts[0], '~', '\0' );
412
413 if( paramsRoot.size() < 1 )
414 continue;
415
416 wxString rootType = paramsRoot[0];
417
418 if( rootType == wxS( "LIB" ) )
419 {
420 if( paramsRoot.size() < 4 )
421 continue;
422
423 VECTOR2D origin( parser.Convert( paramsRoot[1] ),
424 parser.Convert( paramsRoot[2] ) );
425
426 wxString packageName = wxString::Format( wxS( "Unknown_%s_%s" ), paramsRoot[1],
427 paramsRoot[2] );
428
429 wxArrayString paramParts = wxSplit( paramsRoot[3], '`', '\0' );
430
431 std::map<wxString, wxString> paramMap;
432
433 for( int i = 1; i < (int) paramParts.size(); i += 2 )
434 {
435 wxString key = paramParts[i - 1];
436 wxString value = paramParts[i];
437
438 if( key == wxS( "package" ) )
439 packageName = value;
440
441 paramMap[key] = value;
442 }
443
444 EDA_ANGLE orientation;
445 if( !paramsRoot[4].IsEmpty() )
446 orientation = EDA_ANGLE( parser.Convert( paramsRoot[4] ), DEGREES_T );
447
448 int layer = 1;
449
450 if( !paramsRoot[7].IsEmpty() )
451 layer = parser.Convert( paramsRoot[7] );
452
453 if( packageName == aFootprintName )
454 {
455 parts.RemoveAt( 0 );
456
457 FOOTPRINT* footprint = parser.ParseFootprint( origin, orientation, layer, nullptr,
458 paramMap, m_loadedFootprints, parts );
459
460 if( !footprint )
461 return nullptr;
462
463 footprint->Reference().SetPosition( VECTOR2I() );
464 footprint->Reference().SetTextAngle( ANGLE_0 );
465 footprint->Reference().SetVisible( true );
466
467 footprint->Value().SetPosition( VECTOR2I() );
468 footprint->Value().SetTextAngle( ANGLE_0 );
469 footprint->Value().SetVisible( true );
470
471 footprint->AutoPositionFields();
472
473 return footprint;
474 }
475 }
476 }
477 }
479 {
481
482 wxString packageName = wxString::Format( wxS( "Unknown_%s" ),
483 pcbDoc.uuid.value_or( wxS( "Unknown" ) ) );
484
485 std::optional<std::map<wxString, wxString>> c_para;
486
487 if( pcbDoc.c_para )
488 c_para = pcbDoc.c_para;
489 else if( doc.head.c_para )
490 c_para = doc.head.c_para;
491
492 if( c_para )
493 {
494 packageName = get_def( *c_para, wxS( "package" ), packageName );
495
496 if( packageName != aFootprintName )
497 return nullptr;
498
499 VECTOR2D origin( doc.head.x, doc.head.y );
500
501 FOOTPRINT* footprint = parser.ParseFootprint( origin, ANGLE_0, F_Cu, nullptr, *c_para,
503
504 if( !footprint )
505 return nullptr;
506
507 footprint->Reference().SetPosition( VECTOR2I() );
508 footprint->Reference().SetTextAngle( ANGLE_0 );
509 footprint->Reference().SetVisible( true );
510
511 footprint->Value().SetPosition( VECTOR2I() );
512 footprint->Value().SetTextAngle( ANGLE_0 );
513 footprint->Value().SetVisible( true );
514
515 footprint->AutoPositionFields();
516
517 return footprint;
518 }
519 }
520 }
521 catch( nlohmann::json::exception& e )
522 {
523 THROW_IO_ERROR( wxString::Format( _( "Error reading footprint '%s' from library '%s': %s" ),
524 aFootprintName, aLibraryPath, e.what() ) );
525 }
526 catch( std::exception& e )
527 {
528 THROW_IO_ERROR( wxString::Format( _( "Error reading footprint '%s' from library '%s': %s" ),
529 aFootprintName, aLibraryPath, e.what() ) );
530 }
531
532 return nullptr;
533}
534
535
537{
538 std::vector<FOOTPRINT*> result;
539
540 for( auto& [fpUuid, footprint] : m_loadedFootprints )
541 {
542 result.push_back( static_cast<FOOTPRINT*>( footprint->Clone() ) );
543 }
544
545 return result;
546}
const char * name
constexpr EDA_IU_SCALE pcbIUScale
Definition base_units.h:121
BOX2< VECTOR2I > BOX2I
Definition box2.h:918
constexpr BOX2I KiROUND(const BOX2D &aBoxD)
Definition box2.h:986
Container for design settings for a BOARD object.
std::shared_ptr< NET_SETTINGS > m_NetSettings
void SetAuxOrigin(const VECTOR2I &aOrigin)
Information pertinent to a Pcbnew printed circuit board.
Definition board.h:372
constexpr const Vec GetCenter() const
Definition box2.h:226
static double Convert(const wxString &aValue)
virtual void SetVisible(bool aVisible)
Definition eda_text.cpp:381
RAII class to set and restore the fontconfig reporter.
Definition reporter.h:332
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
static LOAD_INFO_REPORTER & GetInstance()
Definition reporter.cpp:247
std::shared_ptr< NETCLASS > GetDefaultNetclass() const
Gets the default netclass for the project.
Describe the page size and margins of a paper page on which to eventually print or plot.
Definition page_info.h:75
double GetHeightMils() const
Definition page_info.h:143
double GetWidthMils() const
Definition page_info.h:138
FOOTPRINT * ParseFootprint(const VECTOR2D &aOrigin, const EDA_ANGLE &aOrientation, int aLayer, BOARD *aParent, std::map< wxString, wxString > aParams, std::map< wxString, std::unique_ptr< FOOTPRINT > > &aFootprintMap, wxArrayString aShapes)
double ScaleSize(double aValue) override
void ParseBoard(BOARD *aBoard, const VECTOR2D &aOrigin, std::map< wxString, std::unique_ptr< FOOTPRINT > > &aFootprintMap, wxArrayString aShapes)
PCB_LAYER_ID LayerToKi(const wxString &aLayer)
bool CanReadBoard(const wxString &aFileName) const override
Checks if this PCB_IO can read the specified board file.
bool CanReadLibrary(const wxString &aFileName) const override
Checks if this IO object can read the specified library file/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.
std::map< wxString, std::unique_ptr< FOOTPRINT > > m_loadedFootprints
std::vector< FOOTPRINT * > GetImportedCachedLibraryFootprints() override
Return a container with the cached library footprints generated in the last call to Load.
long long GetLibraryTimestamp(const wxString &aLibraryPath) const override
Generate a timestamp representing all the files in the library (including the library directory).
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 CanReadFootprint(const wxString &aFileName) const override
Checks if this PCB_IO can read a footprint from specified file or directory.
BOARD * m_board
The board BOARD being worked on, no ownership here.
Definition pcb_io.h:349
virtual bool CanReadBoard(const wxString &aFileName) const
Checks if this PCB_IO can read the specified board file.
Definition pcb_io.cpp:38
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
virtual void SetPosition(const VECTOR2I &aPos) override
Definition pcb_text.h:95
void SetTextAngle(const EDA_ANGLE &aAngle) override
Definition pcb_text.cpp:552
Container for project specific data.
Definition project.h:62
The common library.
#define _(s)
static constexpr EDA_ANGLE ANGLE_0
Definition eda_angle.h:411
@ DEGREES_T
Definition eda_angle.h:31
#define THROW_IO_ERROR(msg)
macro which captures the "call site" values of FILE_, __FUNCTION & LINE
@ F_Cu
Definition layer_ids.h:60
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
std::optional< V > get_opt(const std::map< wxString, V > &aMap, const wxString &aKey)
Definition map_helpers.h:30
static bool FindBoardInStream(const wxString &aName, wxInputStream &aStream, nlohmann::json &aOut, EASYEDA::DOCUMENT &aDoc)
std::optional< std::map< wxString, nlohmann::json > > DRCRULE
std::optional< std::map< wxString, wxString > > c_para
std::vector< wxString > layers
std::optional< wxString > uuid
std::optional< std::map< wxString, wxString > > c_para
wxString result
Test unit parsing edge cases and error handling.
VECTOR2< int32_t > VECTOR2I
Definition vector2d.h:683
VECTOR2< double > VECTOR2D
Definition vector2d.h:682