37#include <wx/wfstream.h>
38#include <wx/zipstrm.h>
39#include <wx/stdstream.h>
55static bool FindBoardInStream(
const wxString& aName, wxInputStream& aStream, nlohmann::json& aOut,
58 if( aName.Lower().EndsWith( wxS(
".json" ) ) )
60 wxStdInputStream sin( aStream );
61 nlohmann::json js = nlohmann::json::parse( sin,
nullptr,
false );
63 if( js.is_discarded() )
79 else if( aName.Lower().EndsWith( wxS(
".zip" ) ) )
81 std::shared_ptr<wxZipEntry> entry;
82 wxZipInputStream
zip( aStream );
87 while( entry.reset(
zip.GetNextEntry() ), entry.get() != NULL )
89 wxString
name = entry->GetName();
107 wxFFileInputStream in( aFileName );
113 catch( nlohmann::json::exception& )
116 catch( std::exception& )
137 const std::map<std::string, UTF8>* aProperties,
PROJECT* aProject )
159 wxFFileInputStream in( aFileName );
168 const int innerStart = 21;
169 const int innerEnd = 52;
171 int maxLayer = innerStart;
172 std::map<PCB_LAYER_ID, wxString> layerNames;
174 for(
const wxString& layerLine : pcbDoc.
layers )
176 wxArrayString parts = wxSplit( layerLine,
'~',
'\0' );
177 int layerId = wxAtoi( parts[0] );
178 wxString layerName = parts[1];
179 wxString layerColor = parts[2];
180 wxString visible = parts[3];
181 wxString active = parts[4];
182 bool enabled = parts[5] != wxS(
"false" );
184 if( layerId >= innerStart && layerId <= innerEnd && enabled )
185 maxLayer = layerId + 1;
187 layerNames[parser.
LayerToKi( parts[0] )] = layerName;
190 m_board->SetCopperLayerCount( 2 + maxLayer - innerStart );
192 for(
auto& [klayer,
name] : layerNames )
197 bool importedNetclassRules =
false;
202 std::map<wxString, nlohmann::json>& rules = *pcbDoc.
DRCRULE;
204 if(
auto defRules =
get_opt( rules,
"Default" ) )
208 key = wxS(
"trackWidth" );
209 if( defRules->find( key ) != defRules->end() && defRules->at( key ).is_number() )
211 double val = parser.
ScaleSize( defRules->at( key ) );
212 defNetclass->SetTrackWidth( val );
213 importedNetclassRules =
true;
216 key = wxS(
"clearance" );
217 if( defRules->find( key ) != defRules->end() && defRules->at( key ).is_number() )
219 double val = parser.
ScaleSize( defRules->at( key ) );
220 defNetclass->SetClearance( val );
221 importedNetclassRules =
true;
224 key = wxS(
"viaHoleD" );
225 if( defRules->find( key ) != defRules->end() && defRules->at( key ).is_number() )
227 double val = parser.
ScaleSize( defRules->at( key ) );
229 defNetclass->SetViaDrill( val );
230 importedNetclassRules =
true;
233 key = wxS(
"viaHoleDiameter" );
234 if( defRules->find( key ) != defRules->end() && defRules->at( key ).is_number() )
236 double val = parser.
ScaleSize( defRules->at( key ) );
237 defNetclass->SetViaDiameter( val );
238 importedNetclassRules =
true;
242 if( importedNetclassRules )
243 m_board->m_LegacyNetclassesLoaded =
true;
250 BOX2I outlineBbox =
m_board->ComputeBoundingBox(
true,
true );
259 offset.
x =
KiROUND( offset.
x / alignGrid ) * alignGrid;
260 offset.
y =
KiROUND( offset.
y / alignGrid ) * alignGrid;
265 catch( nlohmann::json::exception& e )
267 THROW_IO_ERRORF(
_(
"Error loading board '%s': %s" ), aFileName, e.what() );
269 catch( std::exception& e )
271 THROW_IO_ERRORF(
_(
"Error loading board '%s': %s" ), aFileName, e.what() );
283 const wxString& aLibraryPath,
bool aBestEfforts,
284 const std::map<std::string, UTF8>* aProperties )
288 wxFFileInputStream in( aLibraryPath );
293 THROW_IO_ERRORF(
_(
"Unable to find valid footprints in '%s'" ), aLibraryPath );
298 for( wxString shap : doc.
shape )
300 shap.Replace( wxS(
"#@$" ),
"\n" );
301 wxArrayString parts = wxSplit( shap,
'\n',
'\0' );
303 if( parts.size() < 1 )
306 wxArrayString paramsRoot = wxSplit( parts[0],
'~',
'\0' );
308 if( paramsRoot.size() < 1 )
311 wxString rootType = paramsRoot[0];
313 if( rootType == wxS(
"LIB" ) )
315 if( paramsRoot.size() < 4 )
318 wxString packageName = wxString::Format( wxS(
"Unknown_%s_%s" ), paramsRoot[1],
321 wxArrayString paramParts = wxSplit( paramsRoot[3],
'`',
'\0' );
323 std::map<wxString, wxString> paramMap;
325 for(
int i = 1; i < (int) paramParts.size(); i += 2 )
327 wxString key = paramParts[i - 1];
328 wxString value = paramParts[i];
330 if( key == wxS(
"package" ) )
333 paramMap[key] = value;
336 aFootprintNames.Add( packageName );
344 wxString packageName = wxString::Format( wxS(
"Unknown_%s" ),
345 pcbDoc.
uuid.value_or( wxS(
"Unknown" ) ) );
347 std::optional<std::map<wxString, wxString>> c_para;
355 packageName =
get_def( *c_para, wxS(
"package" ), packageName );
357 aFootprintNames.Add( packageName );
360 catch( nlohmann::json::exception& e )
362 THROW_IO_ERRORF(
_(
"Error enumerating footprints in library '%s': %s" ), aLibraryPath, e.what() );
364 catch( std::exception& e )
366 THROW_IO_ERRORF(
_(
"Error enumerating footprints in library '%s': %s" ), aLibraryPath, e.what() );
373 const std::map<std::string, UTF8>* aProperties )
384 wxFFileInputStream in( aLibraryPath );
389 THROW_IO_ERRORF(
_(
"Unable to find valid footprints in '%s'" ), aLibraryPath );
394 for( wxString shap : doc.
shape )
396 if( !shap.Contains( wxS(
"LIB" ) ) )
399 shap.Replace( wxS(
"#@$" ),
"\n" );
400 wxArrayString parts = wxSplit( shap,
'\n',
'\0' );
402 if( parts.size() < 1 )
405 wxArrayString paramsRoot = wxSplit( parts[0],
'~',
'\0' );
407 if( paramsRoot.size() < 1 )
410 wxString rootType = paramsRoot[0];
412 if( rootType == wxS(
"LIB" ) )
414 if( paramsRoot.size() < 4 )
418 parser.
Convert( paramsRoot[2] ) );
420 wxString packageName = wxString::Format( wxS(
"Unknown_%s_%s" ), paramsRoot[1],
423 wxArrayString paramParts = wxSplit( paramsRoot[3],
'`',
'\0' );
425 std::map<wxString, wxString> paramMap;
427 for(
int i = 1; i < (int) paramParts.size(); i += 2 )
429 wxString key = paramParts[i - 1];
430 wxString value = paramParts[i];
432 if( key == wxS(
"package" ) )
435 paramMap[key] = value;
439 if( !paramsRoot[4].IsEmpty() )
444 if( !paramsRoot[7].IsEmpty() )
445 layer = parser.
Convert( paramsRoot[7] );
447 if( packageName == aFootprintName )
457 footprint->Reference().SetPosition(
VECTOR2I() );
458 footprint->Reference().SetTextAngle(
ANGLE_0 );
459 footprint->Reference().SetVisible(
true );
461 footprint->Value().SetPosition(
VECTOR2I() );
462 footprint->Value().SetTextAngle(
ANGLE_0 );
463 footprint->Value().SetVisible(
true );
465 footprint->AutoPositionFields();
476 wxString packageName = wxString::Format( wxS(
"Unknown_%s" ),
477 pcbDoc.
uuid.value_or( wxS(
"Unknown" ) ) );
479 std::optional<std::map<wxString, wxString>> c_para;
488 packageName =
get_def( *c_para, wxS(
"package" ), packageName );
490 if( packageName != aFootprintName )
501 footprint->Reference().SetPosition(
VECTOR2I() );
502 footprint->Reference().SetTextAngle(
ANGLE_0 );
503 footprint->Reference().SetVisible(
true );
505 footprint->Value().SetPosition(
VECTOR2I() );
506 footprint->Value().SetTextAngle(
ANGLE_0 );
507 footprint->Value().SetVisible(
true );
509 footprint->AutoPositionFields();
515 catch( nlohmann::json::exception& e )
522 catch( std::exception& e )
536 std::vector<FOOTPRINT*>
result;
constexpr EDA_IU_SCALE pcbIUScale
constexpr BOX2I KiROUND(const BOX2D &aBoxD)
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.
constexpr const Vec GetCenter() const
static double Convert(const wxString &aValue)
RAII class to set and restore the fontconfig reporter.
PROGRESS_REPORTER * m_progressReporter
Progress reporter to track the progress of the operation, may be nullptr.
static LOAD_INFO_REPORTER & GetInstance()
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.
double GetHeightMils() const
double GetWidthMils() const
std::unique_ptr< 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
bool CanReadLibrary(const wxString &aFileName) const override
Checks if this IO object can read the specified library file/directory.
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.
void loadBoard(const wxString &aFileName, BOARD &aBoard, bool aIsNewLoad, const std::map< std::string, UTF8 > *aProperties=nullptr, PROJECT *aProject=nullptr) override
Parse aFileName into aBoard.
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).
bool CanReadFootprint(const wxString &aFileName) const override
Check if this PCB_IO can read a footprint from specified file or directory.
std::unique_ptr< 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...
BOARD * m_board
The board BOARD being worked on, no ownership here.
virtual bool CanReadBoard(const wxString &aFileName) const
PCB_IO(const wxString &aName)
const std::map< std::string, UTF8 > * m_props
Properties passed via Save() or Load(), no ownership, may be NULL.
Container for project specific data.
static constexpr EDA_ANGLE ANGLE_0
#define THROW_IO_ERRORF(msg,...)
#define THROW_IO_CANCELLED()
This file contains miscellaneous commonly used macros and functions.
wxString get_def(const std::map< wxString, wxString > &aMap, const char *aKey, const char *aDefval="")
std::optional< V > get_opt(const std::map< wxString, V > &aMap, const wxString &aKey)
bool UnwrapDataStr(nlohmann::json &aJson)
Replace a document the EasyEDA server wraps in a metadata envelope with the document it carries.
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
static EDA_ORIENTATION orientation(double d)
wxString result
Test unit parsing edge cases and error handling.
VECTOR2< int32_t > VECTOR2I
VECTOR2< double > VECTOR2D