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 (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
28#include <pcb_io/pcb_io.h>
29
30#include <progress_reporter.h>
31#include <common.h>
32#include <macros.h>
33#include <board.h>
34#include <footprint.h>
36
37#include <wx/log.h>
38#include <wx/wfstream.h>
39#include <wx/zipstrm.h>
40#include <wx/stdstream.h>
41
42#include <nlohmann/json.hpp>
43#include <core/map_helpers.h>
44
45
46PCB_IO_EASYEDA::PCB_IO_EASYEDA() : PCB_IO( wxS( "EasyEDA (JLCEDA) Standard" ) )
47{
48}
49
50
52{
53}
54
55
56static bool FindBoardInStream( const wxString& aName, wxInputStream& aStream, nlohmann::json& aOut,
57 EASYEDA::DOCUMENT& aDoc )
58{
59 if( aName.Lower().EndsWith( wxS( ".json" ) ) )
60 {
61 wxStdInputStream sin( aStream );
62 nlohmann::json js = nlohmann::json::parse( sin, nullptr, false );
63
64 if( js.is_discarded() )
65 return false;
66
68
72 {
73 aOut = js;
74 aDoc = doc;
75 return true;
76 }
77 }
78 else if( aName.Lower().EndsWith( wxS( ".zip" ) ) )
79 {
80 std::shared_ptr<wxZipEntry> entry;
81 wxZipInputStream zip( aStream );
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( FindBoardInStream( name, zip, aOut, aDoc ) )
91 return true;
92 }
93 }
94
95 return false;
96}
97
98
99bool PCB_IO_EASYEDA::CanReadBoard( const wxString& aFileName ) const
100{
101 if( !PCB_IO::CanReadBoard( aFileName ) )
102 return false;
103
104 try
105 {
106 wxFFileInputStream in( aFileName );
107 nlohmann::json js;
109
110 return FindBoardInStream( aFileName, in, js, doc );
111 }
112 catch( nlohmann::json::exception& )
113 {
114 }
115 catch( std::exception& )
116 {
117 }
118
119 return false;
120}
121
122
123bool PCB_IO_EASYEDA::CanReadFootprint( const wxString& aFileName ) const
124{
125 return CanReadBoard( aFileName );
126}
127
128
129bool PCB_IO_EASYEDA::CanReadLibrary( const wxString& aFileName ) const
130{
131 return CanReadBoard( aFileName );
132}
133
134
135BOARD* PCB_IO_EASYEDA::LoadBoard( const wxString& aFileName, BOARD* aAppendToMe,
136 const STRING_UTF8_MAP* aProperties, PROJECT* aProject )
137{
138 m_loadedFootprints.clear();
139
140 m_props = aProperties;
141 m_board = aAppendToMe ? aAppendToMe : new BOARD();
142
143 // Give the filename to the board if it's new
144 if( !aAppendToMe )
145 m_board->SetFileName( aFileName );
146
148 {
149 m_progressReporter->Report( wxString::Format( _( "Loading %s..." ), aFileName ) );
150
152 THROW_IO_ERROR( _( "Open cancelled by user." ) );
153 }
154
155 PCB_IO_EASYEDA_PARSER parser( nullptr );
156
157 try
158 {
159 wxFFileInputStream in( aFileName );
160 nlohmann::json js;
162
163 if( !FindBoardInStream( aFileName, in, js, doc ) )
164 {
166 wxString::Format( _( "Unable to find a valid board in '%s'" ), aFileName ) );
167 }
168
170
171 const int innerStart = 21;
172 const int innerEnd = 52;
173
174 int maxLayer = innerStart;
175 std::map<PCB_LAYER_ID, wxString> layerNames;
176
177 for( const wxString& layerLine : pcbDoc.layers )
178 {
179 wxArrayString parts = wxSplit( layerLine, '~', '\0' );
180 int layerId = wxAtoi( parts[0] );
181 wxString layerName = parts[1];
182 wxString layerColor = parts[2];
183 wxString visible = parts[3];
184 wxString active = parts[4];
185 bool enabled = parts[5] != wxS( "false" );
186
187 if( layerId >= innerStart && layerId <= innerEnd && enabled )
188 maxLayer = layerId + 1;
189
190 layerNames[parser.LayerToKi( parts[0] )] = layerName;
191 }
192
193 m_board->SetCopperLayerCount( 2 + maxLayer - innerStart );
194
195 for( auto& [klayer, name] : layerNames )
196 m_board->SetLayerName( klayer, name );
197
199 std::shared_ptr<NETCLASS> defNetclass = bds.m_NetSettings->m_DefaultNetClass;
200
201 if( pcbDoc.DRCRULE )
202 {
203 std::map<wxString, nlohmann::json>& rules = *pcbDoc.DRCRULE;
204
205 if( auto defRules = get_opt( rules, "Default" ) )
206 {
207 wxString key;
208
209 key = wxS( "trackWidth" );
210 if( defRules->find( key ) != defRules->end() && defRules->at( key ).is_number() )
211 {
212 double val = parser.ScaleSize( defRules->at( key ) );
213 defNetclass->SetTrackWidth( val );
214 }
215
216 key = wxS( "clearance" );
217 if( defRules->find( key ) != defRules->end() && defRules->at( key ).is_number() )
218 {
219 double val = parser.ScaleSize( defRules->at( key ) );
220 defNetclass->SetClearance( val );
221 }
222
223 key = wxS( "viaHoleD" );
224 if( defRules->find( key ) != defRules->end() && defRules->at( key ).is_number() )
225 {
226 double val = parser.ScaleSize( defRules->at( key ) );
227
228 defNetclass->SetViaDrill( val );
229 }
230
231 key = wxS( "viaHoleDiameter" ); // Yes, this is via diameter, not drill diameter
232 if( defRules->find( key ) != defRules->end() && defRules->at( key ).is_number() )
233 {
234 double val = parser.ScaleSize( defRules->at( key ) );
235 defNetclass->SetViaDiameter( val );
236 }
237 }
238 }
239
240 VECTOR2D origin( doc.head.x, doc.head.y );
241 parser.ParseBoard( m_board, origin, m_loadedFootprints, doc.shape );
242
243 // Center the board
244 BOX2I outlineBbox = m_board->ComputeBoundingBox( true );
245 PAGE_INFO pageInfo = m_board->GetPageSettings();
246
247 VECTOR2D pageCenter( pcbIUScale.MilsToIU( pageInfo.GetWidthMils() / 2 ),
248 pcbIUScale.MilsToIU( pageInfo.GetHeightMils() / 2 ) );
249
250 VECTOR2D offset = pageCenter - outlineBbox.GetCenter();
251
252 int alignGrid = pcbIUScale.mmToIU( 10 );
253 offset.x = KiROUND( offset.x / alignGrid ) * alignGrid;
254 offset.y = KiROUND( offset.y / alignGrid ) * alignGrid;
255
256 m_board->Move( offset );
257 bds.SetAuxOrigin( offset );
258
259 return m_board;
260 }
261 catch( nlohmann::json::exception& e )
262 {
264 wxString::Format( _( "Error loading board '%s': %s" ), aFileName, e.what() ) );
265 }
266 catch( std::exception& e )
267 {
269 wxString::Format( _( "Error loading board '%s': %s" ), aFileName, e.what() ) );
270 }
271
272 return m_board;
273}
274
275
276long long PCB_IO_EASYEDA::GetLibraryTimestamp( const wxString& aLibraryPath ) const
277{
278 return 0;
279}
280
281
282void PCB_IO_EASYEDA::FootprintEnumerate( wxArrayString& aFootprintNames,
283 const wxString& aLibraryPath, bool aBestEfforts,
284 const STRING_UTF8_MAP* aProperties )
285{
286 try
287 {
288 wxFFileInputStream in( aLibraryPath );
289 nlohmann::json js;
291
292 if( !FindBoardInStream( aLibraryPath, in, js, doc ) )
293 {
294 THROW_IO_ERROR( wxString::Format( _( "Unable to find valid footprints in '%s'" ),
295 aLibraryPath ) );
296 }
297
300 {
301 for( wxString shap : doc.shape )
302 {
303 shap.Replace( wxS( "#@$" ), "\n" );
304 wxArrayString parts = wxSplit( shap, '\n', '\0' );
305
306 if( parts.size() < 1 )
307 continue;
308
309 wxArrayString paramsRoot = wxSplit( parts[0], '~', '\0' );
310
311 if( paramsRoot.size() < 1 )
312 continue;
313
314 wxString rootType = paramsRoot[0];
315
316 if( rootType == wxS( "LIB" ) )
317 {
318 if( paramsRoot.size() < 4 )
319 continue;
320
321 wxString packageName = wxString::Format( wxS( "Unknown_%s_%s" ), paramsRoot[1],
322 paramsRoot[2] );
323
324 wxArrayString paramParts = wxSplit( paramsRoot[3], '`', '\0' );
325
326 std::map<wxString, wxString> paramMap;
327
328 for( int i = 1; i < paramParts.size(); i += 2 )
329 {
330 wxString key = paramParts[i - 1];
331 wxString value = paramParts[i];
332
333 if( key == wxS( "package" ) )
334 packageName = value;
335
336 paramMap[key] = value;
337 }
338
339 aFootprintNames.Add( packageName );
340 }
341 }
342 }
344 {
346
347 wxString packageName = wxString::Format( wxS( "Unknown_%s" ),
348 pcbDoc.uuid.value_or( wxS( "Unknown" ) ) );
349
350 std::optional<std::map<wxString, wxString>> c_para;
351
352 if( pcbDoc.c_para )
353 c_para = pcbDoc.c_para;
354 else if( doc.head.c_para )
355 c_para = doc.head.c_para;
356
357 if( c_para )
358 {
359 packageName = get_def( *c_para, wxS( "package" ), packageName );
360 }
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 STRING_UTF8_MAP* aProperties )
381{
382 PCB_IO_EASYEDA_PARSER parser( nullptr );
383
384 m_loadedFootprints.clear();
385
386 try
387 {
388 wxFFileInputStream in( aLibraryPath );
389 nlohmann::json js;
391
392 if( !FindBoardInStream( aLibraryPath, in, js, doc ) )
393 {
394 THROW_IO_ERROR( wxString::Format( _( "Unable to find valid footprints in '%s'" ),
395 aLibraryPath ) );
396 }
397
400 {
401 for( wxString shap : doc.shape )
402 {
403 if( !shap.Contains( wxS( "LIB" ) ) )
404 continue;
405
406 shap.Replace( wxS( "#@$" ), "\n" );
407 wxArrayString parts = wxSplit( shap, '\n', '\0' );
408
409 if( parts.size() < 1 )
410 continue;
411
412 wxArrayString paramsRoot = wxSplit( parts[0], '~', '\0' );
413
414 if( paramsRoot.size() < 1 )
415 continue;
416
417 wxString rootType = paramsRoot[0];
418
419 if( rootType == wxS( "LIB" ) )
420 {
421 if( paramsRoot.size() < 4 )
422 continue;
423
424 VECTOR2D origin( parser.Convert( paramsRoot[1] ),
425 parser.Convert( paramsRoot[2] ) );
426
427 wxString packageName = wxString::Format( wxS( "Unknown_%s_%s" ), paramsRoot[1],
428 paramsRoot[2] );
429
430 wxArrayString paramParts = wxSplit( paramsRoot[3], '`', '\0' );
431
432 std::map<wxString, wxString> paramMap;
433
434 for( int i = 1; i < paramParts.size(); i += 2 )
435 {
436 wxString key = paramParts[i - 1];
437 wxString value = paramParts[i];
438
439 if( key == wxS( "package" ) )
440 packageName = value;
441
442 paramMap[key] = value;
443 }
444
445 EDA_ANGLE orientation;
446 if( !paramsRoot[4].IsEmpty() )
447 orientation = EDA_ANGLE( parser.Convert( paramsRoot[4] ), DEGREES_T );
448
449 int layer = 1;
450
451 if( !paramsRoot[7].IsEmpty() )
452 layer = parser.Convert( paramsRoot[7] );
453
454 if( packageName == aFootprintName )
455 {
456 parts.RemoveAt( 0 );
457
458 FOOTPRINT* footprint =
459 parser.ParseFootprint( origin, orientation, layer, nullptr,
460 paramMap, m_loadedFootprints, parts );
461
462 if( !footprint )
463 return nullptr;
464
465 footprint->Reference().SetPosition( VECTOR2I() );
466 footprint->Reference().SetTextAngle( ANGLE_0 );
467 footprint->Reference().SetVisible( true );
468
469 footprint->Value().SetPosition( VECTOR2I() );
470 footprint->Value().SetTextAngle( ANGLE_0 );
471 footprint->Value().SetVisible( true );
472
473 footprint->AutoPositionFields();
474
475 return footprint;
476 }
477 }
478 }
479 }
481 {
483
484 wxString packageName = wxString::Format( wxS( "Unknown_%s" ),
485 pcbDoc.uuid.value_or( wxS( "Unknown" ) ) );
486
487 std::optional<std::map<wxString, wxString>> c_para;
488
489 if( pcbDoc.c_para )
490 c_para = pcbDoc.c_para;
491 else if( doc.head.c_para )
492 c_para = doc.head.c_para;
493
494 if( c_para )
495 {
496 packageName = get_def( *c_para, wxS( "package" ), packageName );
497
498 if( packageName != aFootprintName )
499 return nullptr;
500
501 VECTOR2D origin( doc.head.x, doc.head.y );
502
503 FOOTPRINT* footprint = parser.ParseFootprint(
504 origin, ANGLE_0, F_Cu, nullptr, *c_para, m_loadedFootprints, doc.shape );
505
506 if( !footprint )
507 return nullptr;
508
509 footprint->Reference().SetPosition( VECTOR2I() );
510 footprint->Reference().SetTextAngle( ANGLE_0 );
511 footprint->Reference().SetVisible( true );
512
513 footprint->Value().SetPosition( VECTOR2I() );
514 footprint->Value().SetTextAngle( ANGLE_0 );
515 footprint->Value().SetVisible( true );
516
517 footprint->AutoPositionFields();
518
519 return footprint;
520 }
521 }
522 }
523 catch( nlohmann::json::exception& e )
524 {
525 THROW_IO_ERROR( wxString::Format( _( "Error reading footprint '%s' from library '%s': %s" ),
526 aFootprintName, aLibraryPath, e.what() ) );
527 }
528 catch( std::exception& e )
529 {
530 THROW_IO_ERROR( wxString::Format( _( "Error reading footprint '%s' from library '%s': %s" ),
531 aFootprintName, aLibraryPath, e.what() ) );
532 }
533
534 return nullptr;
535}
536
537
539{
540 std::vector<FOOTPRINT*> result;
541
542 for( auto& [fpUuid, footprint] : m_loadedFootprints )
543 {
544 result.push_back( static_cast<FOOTPRINT*>( footprint->Clone() ) );
545 }
546
547 return result;
548}
const char * name
Definition: DXF_plotter.cpp:57
constexpr EDA_IU_SCALE pcbIUScale
Definition: base_units.h:108
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:282
void SetFileName(const wxString &aFileName)
Definition: board.h:317
const PAGE_INFO & GetPageSettings() const
Definition: board.h:671
bool SetLayerName(PCB_LAYER_ID aLayer, const wxString &aLayerName)
Changes the name of the layer given by aLayer.
Definition: board.cpp:583
void Move(const VECTOR2I &aMoveVector) override
Move this object.
Definition: board.cpp:493
BOX2I ComputeBoundingBox(bool aBoardEdgesOnly=false) const
Calculate the bounding box containing all board items (or board edge segments).
Definition: board.cpp:1555
void SetCopperLayerCount(int aCount)
Definition: board.cpp:662
BOARD_DESIGN_SETTINGS & GetDesignSettings() const
Definition: board.cpp:797
const Vec GetCenter() const
Definition: box2.h:220
static double Convert(const wxString &aValue)
virtual void SetVisible(bool aVisible)
Definition: eda_text.cpp:243
virtual void SetTextAngle(const EDA_ANGLE &aAngle)
Definition: eda_text.cpp:203
PCB_FIELD & Value()
read/write accessors:
Definition: footprint.h:624
PCB_FIELD & Reference()
Definition: footprint.h:625
void AutoPositionFields()
Position Reference and Value fields at the top and bottom of footprint's bounding box.
Definition: footprint.cpp:2558
PROGRESS_REPORTER * m_progressReporter
Progress reporter to track the progress of the operation, may be nullptr.
Definition: io_base.h:213
Describe the page size and margins of a paper page on which to eventually print or plot.
Definition: page_info.h:59
double GetHeightMils() const
Definition: page_info.h:141
double GetWidthMils() const
Definition: page_info.h:136
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.
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 ...
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::map< wxString, std::unique_ptr< FOOTPRINT > > m_loadedFootprints
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.
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).
bool CanReadFootprint(const wxString &aFileName) const override
Checks if this PCB_IO can read a footprint from specified file or 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 CanReadBoard(const wxString &aFileName) const
Checks if this PCB_IO can read the specified board file.
Definition: pcb_io.cpp:43
virtual void SetPosition(const VECTOR2I &aPos) override
Definition: pcb_text.h:87
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.
Container for project specific data.
Definition: project.h:62
A name/value tuple with unique names and optional values.
The common library.
#define _(s)
static constexpr EDA_ANGLE ANGLE_0
Definition: eda_angle.h:435
@ DEGREES_T
Definition: eda_angle.h:31
#define THROW_IO_ERROR(msg)
Definition: ki_exception.h:39
@ 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
constexpr int MilsToIU(int mils) const
Definition: base_units.h:93
constexpr int mmToIU(double mm) const
Definition: base_units.h:88
constexpr ret_type KiROUND(fp_type v)
Round a floating point number to an integer using "round halfway cases away from zero".
Definition: util.h:118
VECTOR2< int > VECTOR2I
Definition: vector2d.h:588