37#include <wx/wfstream.h>
38#include <wx/zipstrm.h>
39#include <wx/stdstream.h>
41#include <nlohmann/json.hpp>
57static bool FindBoardInStream(
const wxString& aName, wxInputStream& aStream, nlohmann::json& aOut,
60 if( aName.Lower().EndsWith( wxS(
".json" ) ) )
62 wxStdInputStream sin( aStream );
63 nlohmann::json js = nlohmann::json::parse( sin,
nullptr,
false );
65 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& )
149 if( aProgressReporter )
151 aProgressReporter->
Report( wxString::Format(
_(
"Loading %s..." ), aFileName ) );
161 wxFFileInputStream in( aFileName );
168 wxString::Format(
_(
"Unable to find a valid board in '%s'" ), aFileName ) );
173 const int innerStart = 21;
174 const int innerEnd = 52;
176 int maxLayer = innerStart;
177 std::map<PCB_LAYER_ID, wxString> layerNames;
179 for(
const wxString& layerLine : pcbDoc.
layers )
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" );
189 if( layerId >= innerStart && layerId <= innerEnd && enabled )
190 maxLayer = layerId + 1;
192 layerNames[parser.
LayerToKi( parts[0] )] = layerName;
197 for(
auto& [klayer,
name] : layerNames )
201 std::shared_ptr<NETCLASS> defNetclass = bds.
m_NetSettings->m_DefaultNetClass;
205 std::map<wxString, nlohmann::json>& rules = *pcbDoc.
DRCRULE;
207 if(
auto defRules =
get_opt( rules,
"Default" ) )
211 key = wxS(
"trackWidth" );
212 if( defRules->find( key ) != defRules->end() && defRules->at( key ).is_number() )
214 double val = parser.
ScaleSize( defRules->at( key ) );
215 defNetclass->SetTrackWidth( val );
218 key = wxS(
"clearance" );
219 if( defRules->find( key ) != defRules->end() && defRules->at( key ).is_number() )
221 double val = parser.
ScaleSize( defRules->at( key ) );
222 defNetclass->SetClearance( val );
225 key = wxS(
"viaHoleD" );
226 if( defRules->find( key ) != defRules->end() && defRules->at( key ).is_number() )
228 double val = parser.
ScaleSize( defRules->at( key ) );
230 defNetclass->SetViaDrill( val );
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 );
255 offset.
x =
KiROUND( offset.
x / alignGrid ) * alignGrid;
256 offset.
y =
KiROUND( offset.
y / alignGrid ) * alignGrid;
263 catch( nlohmann::json::exception& e )
266 wxString::Format(
_(
"Error loading board '%s': %s" ), aFileName, e.what() ) );
268 catch( std::exception& e )
271 wxString::Format(
_(
"Error loading board '%s': %s" ), aFileName, e.what() ) );
285 const wxString& aLibraryPath,
bool aBestEfforts,
290 wxFFileInputStream in( aLibraryPath );
296 THROW_IO_ERROR( wxString::Format(
_(
"Unable to find valid footprints in '%s'" ),
303 for( wxString shap : doc.
shape )
305 shap.Replace( wxS(
"#@$" ),
"\n" );
306 wxArrayString parts = wxSplit( shap,
'\n',
'\0' );
308 if( parts.size() < 1 )
311 wxArrayString paramsRoot = wxSplit( parts[0],
'~',
'\0' );
313 if( paramsRoot.size() < 1 )
316 wxString rootType = paramsRoot[0];
318 if( rootType == wxS(
"LIB" ) )
320 if( paramsRoot.size() < 4 )
323 wxString packageName = wxString::Format( wxS(
"Unknown_%s_%s" ), paramsRoot[1],
326 wxArrayString paramParts = wxSplit( paramsRoot[3],
'`',
'\0' );
328 std::map<wxString, wxString> paramMap;
330 for(
int i = 1; i < paramParts.size(); i += 2 )
332 wxString key = paramParts[i - 1];
333 wxString value = paramParts[i];
335 if( key == wxS(
"package" ) )
338 paramMap[key] = value;
341 aFootprintNames.Add( packageName );
349 wxString packageName = wxString::Format( wxS(
"Unknown_%s" ),
350 pcbDoc.
uuid.value_or( wxS(
"Unknown" ) ) );
354 packageName =
get_def( *pcbDoc.
c_para, wxS(
"package" ), packageName );
357 aFootprintNames.Add( packageName );
360 catch( nlohmann::json::exception& e )
362 THROW_IO_ERROR( wxString::Format(
_(
"Error enumerating footprints in library '%s': %s" ),
363 aLibraryPath, e.what() ) );
365 catch( std::exception& e )
367 THROW_IO_ERROR( wxString::Format(
_(
"Error enumerating footprints in library '%s': %s" ),
368 aLibraryPath, e.what() ) );
374 const wxString& aFootprintName,
bool aKeepUUID,
383 wxFFileInputStream in( aLibraryPath );
389 THROW_IO_ERROR( wxString::Format(
_(
"Unable to find valid footprints in '%s'" ),
396 for( wxString shap : doc.
shape )
398 if( !shap.Contains( wxS(
"LIB" ) ) )
401 shap.Replace( wxS(
"#@$" ),
"\n" );
402 wxArrayString parts = wxSplit( shap,
'\n',
'\0' );
404 if( parts.size() < 1 )
407 wxArrayString paramsRoot = wxSplit( parts[0],
'~',
'\0' );
409 if( paramsRoot.size() < 1 )
412 wxString rootType = paramsRoot[0];
414 if( rootType == wxS(
"LIB" ) )
416 if( paramsRoot.size() < 4 )
420 parser.
Convert( paramsRoot[2] ) );
422 wxString packageName = wxString::Format( wxS(
"Unknown_%s_%s" ), paramsRoot[1],
425 wxArrayString paramParts = wxSplit( paramsRoot[3],
'`',
'\0' );
427 std::map<wxString, wxString> paramMap;
429 for(
int i = 1; i < paramParts.size(); i += 2 )
431 wxString key = paramParts[i - 1];
432 wxString value = paramParts[i];
434 if( key == wxS(
"package" ) )
437 paramMap[key] = value;
441 if( !paramsRoot[4].IsEmpty() )
446 if( !paramsRoot[7].IsEmpty() )
447 layer = parser.
Convert( paramsRoot[7] );
449 if( packageName == aFootprintName )
479 wxString packageName = wxString::Format( wxS(
"Unknown_%s" ),
480 pcbDoc.
uuid.value_or( wxS(
"Unknown" ) ) );
484 packageName =
get_def( *pcbDoc.
c_para, wxS(
"package" ), packageName );
486 if( packageName != aFootprintName )
512 catch( nlohmann::json::exception& e )
514 THROW_IO_ERROR( wxString::Format(
_(
"Error reading footprint '%s' from library '%s': %s" ),
515 aFootprintName, aLibraryPath, e.what() ) );
517 catch( std::exception& e )
519 THROW_IO_ERROR( wxString::Format(
_(
"Error reading footprint '%s' from library '%s': %s" ),
520 aFootprintName, aLibraryPath, e.what() ) );
529 std::vector<FOOTPRINT*> result;
533 result.push_back(
static_cast<FOOTPRINT*
>( footprint->Clone() ) );
constexpr EDA_IU_SCALE pcbIUScale
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.
void SetFileName(const wxString &aFileName)
const PAGE_INFO & GetPageSettings() const
bool SetLayerName(PCB_LAYER_ID aLayer, const wxString &aLayerName)
Changes the name of the layer given by aLayer.
void Move(const VECTOR2I &aMoveVector) override
Move this object.
BOX2I ComputeBoundingBox(bool aBoardEdgesOnly=false) const
Calculate the bounding box containing all board items (or board edge segments).
void SetCopperLayerCount(int aCount)
BOARD_DESIGN_SETTINGS & GetDesignSettings() const
const Vec GetCenter() const
static double Convert(const wxString &aValue)
bool CanReadFootprintLib(const wxString &aFileName) const override
Checks if this PLUGIN can read footprint library from specified file or directory.
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 PL...
const STRING_UTF8_MAP * m_props
BOARD * LoadBoard(const wxString &aFileName, BOARD *aAppendToMe, const STRING_UTF8_MAP *aProperties=nullptr, PROJECT *aProject=nullptr, PROGRESS_REPORTER *aProgressReporter=nullptr) override
Load information from some input file format that this PLUGIN implementation knows about into either ...
long long GetLibraryTimestamp(const wxString &aLibraryPath) const override
Generate a timestamp representing all the files in the library (including the library directory).
std::map< wxString, std::unique_ptr< FOOTPRINT > > m_loadedFootprints
bool CanReadBoard(const wxString &aFileName) const override
Checks if this PLUGIN can read the specified board file.
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.
bool CanReadFootprint(const wxString &aFileName) const override
Checks if this PLUGIN can read a footprint from specified file or directory.
virtual void SetVisible(bool aVisible)
virtual void SetTextAngle(const EDA_ANGLE &aAngle)
Describe the page size and margins of a paper page on which to eventually print or plot.
double GetHeightMils() const
double GetWidthMils() const
void ParseBoard(BOARD *aBoard, const VECTOR2D &aOrigin, std::map< wxString, std::unique_ptr< FOOTPRINT > > &aFootprintMap, wxArrayString aShapes)
PCB_LAYER_ID LayerToKi(const wxString &aLayer)
double ScaleSize(double aValue) override
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)
virtual void SetPosition(const VECTOR2I &aPos) override
virtual bool CanReadBoard(const wxString &aFileName) const
Checks if this PLUGIN can read the specified board file.
A progress reporter interface for use in multi-threaded environments.
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.
A name/value tuple with unique names and optional values.
static constexpr EDA_ANGLE & ANGLE_0
#define THROW_IO_ERROR(msg)
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
constexpr int MilsToIU(int mils) const
constexpr int mmToIU(double mm) const
constexpr ret_type KiROUND(fp_type v)
Round a floating point number to an integer using "round halfway cases away from zero".