40#include <wx/wfstream.h>
41#include <wx/zipstrm.h>
42#include <wx/stdstream.h>
58static bool FindBoardInStream(
const wxString& aName, wxInputStream& aStream, nlohmann::json& aOut,
61 if( aName.Lower().EndsWith( wxS(
".json" ) ) )
63 wxStdInputStream sin( aStream );
64 nlohmann::json js = nlohmann::json::parse( sin,
nullptr,
false );
66 if( js.is_discarded() )
80 else if( aName.Lower().EndsWith( wxS(
".zip" ) ) )
82 std::shared_ptr<wxZipEntry> entry;
83 wxZipInputStream
zip( aStream );
88 while( entry.reset(
zip.GetNextEntry() ), entry.get() != NULL )
90 wxString
name = entry->GetName();
108 wxFFileInputStream in( aFileName );
114 catch( nlohmann::json::exception& )
117 catch( std::exception& )
138 const std::map<std::string, UTF8>* aProperties,
PROJECT* aProject )
150 m_board->SetFileName( aFileName );
164 wxFFileInputStream in( aFileName );
171 wxString::Format(
_(
"Unable to find a valid board in '%s'" ), aFileName ) );
176 const int innerStart = 21;
177 const int innerEnd = 52;
179 int maxLayer = innerStart;
180 std::map<PCB_LAYER_ID, wxString> layerNames;
182 for(
const wxString& layerLine : pcbDoc.
layers )
184 wxArrayString parts = wxSplit( layerLine,
'~',
'\0' );
185 int layerId = wxAtoi( parts[0] );
186 wxString layerName = parts[1];
187 wxString layerColor = parts[2];
188 wxString visible = parts[3];
189 wxString active = parts[4];
190 bool enabled = parts[5] != wxS(
"false" );
192 if( layerId >= innerStart && layerId <= innerEnd && enabled )
193 maxLayer = layerId + 1;
195 layerNames[parser.
LayerToKi( parts[0] )] = layerName;
198 m_board->SetCopperLayerCount( 2 + maxLayer - innerStart );
200 for(
auto& [klayer,
name] : layerNames )
208 std::map<wxString, nlohmann::json>& rules = *pcbDoc.
DRCRULE;
210 if(
auto defRules =
get_opt( rules,
"Default" ) )
214 key = wxS(
"trackWidth" );
215 if( defRules->find( key ) != defRules->end() && defRules->at( key ).is_number() )
217 double val = parser.
ScaleSize( defRules->at( key ) );
218 defNetclass->SetTrackWidth( val );
221 key = wxS(
"clearance" );
222 if( defRules->find( key ) != defRules->end() && defRules->at( key ).is_number() )
224 double val = parser.
ScaleSize( defRules->at( key ) );
225 defNetclass->SetClearance( val );
228 key = wxS(
"viaHoleD" );
229 if( defRules->find( key ) != defRules->end() && defRules->at( key ).is_number() )
231 double val = parser.
ScaleSize( defRules->at( key ) );
233 defNetclass->SetViaDrill( val );
236 key = wxS(
"viaHoleDiameter" );
237 if( defRules->find( key ) != defRules->end() && defRules->at( key ).is_number() )
239 double val = parser.
ScaleSize( defRules->at( key ) );
240 defNetclass->SetViaDiameter( val );
249 BOX2I outlineBbox =
m_board->ComputeBoundingBox(
true );
258 offset.
x =
KiROUND( offset.
x / alignGrid ) * alignGrid;
259 offset.
y =
KiROUND( offset.
y / alignGrid ) * alignGrid;
266 catch( nlohmann::json::exception& e )
268 THROW_IO_ERROR( wxString::Format(
_(
"Error loading board '%s': %s" ), aFileName, e.what() ) );
270 catch( std::exception& e )
272 THROW_IO_ERROR( wxString::Format(
_(
"Error loading board '%s': %s" ), aFileName, e.what() ) );
284 const wxString& aLibraryPath,
bool aBestEfforts,
285 const std::map<std::string, UTF8>* aProperties )
289 wxFFileInputStream in( aLibraryPath );
295 THROW_IO_ERROR( wxString::Format(
_(
"Unable to find valid footprints in '%s'" ),
302 for( wxString shap : doc.
shape )
304 shap.Replace( wxS(
"#@$" ),
"\n" );
305 wxArrayString parts = wxSplit( shap,
'\n',
'\0' );
307 if( parts.size() < 1 )
310 wxArrayString paramsRoot = wxSplit( parts[0],
'~',
'\0' );
312 if( paramsRoot.size() < 1 )
315 wxString rootType = paramsRoot[0];
317 if( rootType == wxS(
"LIB" ) )
319 if( paramsRoot.size() < 4 )
322 wxString packageName = wxString::Format( wxS(
"Unknown_%s_%s" ), paramsRoot[1],
325 wxArrayString paramParts = wxSplit( paramsRoot[3],
'`',
'\0' );
327 std::map<wxString, wxString> paramMap;
329 for(
int i = 1; i < paramParts.size(); i += 2 )
331 wxString key = paramParts[i - 1];
332 wxString value = paramParts[i];
334 if( key == wxS(
"package" ) )
337 paramMap[key] = value;
340 aFootprintNames.Add( packageName );
348 wxString packageName = wxString::Format( wxS(
"Unknown_%s" ),
349 pcbDoc.
uuid.value_or( wxS(
"Unknown" ) ) );
351 std::optional<std::map<wxString, wxString>> c_para;
359 packageName =
get_def( *c_para, wxS(
"package" ), packageName );
361 aFootprintNames.Add( packageName );
364 catch( nlohmann::json::exception& e )
366 THROW_IO_ERROR( wxString::Format(
_(
"Error enumerating footprints in library '%s': %s" ),
367 aLibraryPath, e.what() ) );
369 catch( std::exception& e )
371 THROW_IO_ERROR( wxString::Format(
_(
"Error enumerating footprints in library '%s': %s" ),
372 aLibraryPath, e.what() ) );
378 const wxString& aFootprintName,
bool aKeepUUID,
379 const std::map<std::string, UTF8>* aProperties )
390 wxFFileInputStream in( aLibraryPath );
396 THROW_IO_ERROR( wxString::Format(
_(
"Unable to find valid footprints in '%s'" ),
403 for( wxString shap : doc.
shape )
405 if( !shap.Contains( wxS(
"LIB" ) ) )
408 shap.Replace( wxS(
"#@$" ),
"\n" );
409 wxArrayString parts = wxSplit( shap,
'\n',
'\0' );
411 if( parts.size() < 1 )
414 wxArrayString paramsRoot = wxSplit( parts[0],
'~',
'\0' );
416 if( paramsRoot.size() < 1 )
419 wxString rootType = paramsRoot[0];
421 if( rootType == wxS(
"LIB" ) )
423 if( paramsRoot.size() < 4 )
427 parser.
Convert( paramsRoot[2] ) );
429 wxString packageName = wxString::Format( wxS(
"Unknown_%s_%s" ), paramsRoot[1],
432 wxArrayString paramParts = wxSplit( paramsRoot[3],
'`',
'\0' );
434 std::map<wxString, wxString> paramMap;
436 for(
int i = 1; i < paramParts.size(); i += 2 )
438 wxString key = paramParts[i - 1];
439 wxString value = paramParts[i];
441 if( key == wxS(
"package" ) )
444 paramMap[key] = value;
448 if( !paramsRoot[4].IsEmpty() )
453 if( !paramsRoot[7].IsEmpty() )
454 layer = parser.
Convert( paramsRoot[7] );
456 if( packageName == aFootprintName )
485 wxString packageName = wxString::Format( wxS(
"Unknown_%s" ),
486 pcbDoc.
uuid.value_or( wxS(
"Unknown" ) ) );
488 std::optional<std::map<wxString, wxString>> c_para;
497 packageName =
get_def( *c_para, wxS(
"package" ), packageName );
499 if( packageName != aFootprintName )
524 catch( nlohmann::json::exception& e )
526 THROW_IO_ERROR( wxString::Format(
_(
"Error reading footprint '%s' from library '%s': %s" ),
527 aFootprintName, aLibraryPath, e.what() ) );
529 catch( std::exception& e )
531 THROW_IO_ERROR( wxString::Format(
_(
"Error reading footprint '%s' from library '%s': %s" ),
532 aFootprintName, aLibraryPath, e.what() ) );
541 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)
virtual void SetVisible(bool aVisible)
virtual void SetTextAngle(const EDA_ANGLE &aAngle)
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()
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
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.
virtual bool CanReadBoard(const wxString &aFileName) const
Checks if this PCB_IO can read the specified board file.
PCB_IO(const wxString &aName)
const std::map< std::string, UTF8 > * m_props
Properties passed via Save() or Load(), no ownership, may be NULL.
virtual void SetPosition(const VECTOR2I &aPos) override
Container for project specific data.
static constexpr EDA_ANGLE ANGLE_0
#define THROW_IO_ERROR(msg)
macro which captures the "call site" values of FILE_, __FUNCTION & LINE
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)
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
VECTOR2< double > VECTOR2D