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