38#include <wx/wfstream.h>
39#include <wx/zipstrm.h>
40#include <wx/stdstream.h>
42#include <nlohmann/json.hpp>
56static bool FindBoardInStream(
const wxString& aName, wxInputStream& aStream, nlohmann::json& aOut,
59 if( aName.Lower().EndsWith( wxS(
".json" ) ) )
61 wxStdInputStream sin( aStream );
62 nlohmann::json js = nlohmann::json::parse( sin,
nullptr,
false );
64 if( js.is_discarded() )
78 else if( aName.Lower().EndsWith( wxS(
".zip" ) ) )
80 std::shared_ptr<wxZipEntry> entry;
81 wxZipInputStream
zip( aStream );
86 while( entry.reset(
zip.GetNextEntry() ), entry.get() != NULL )
88 wxString
name = entry->GetName();
106 wxFFileInputStream in( aFileName );
112 catch( nlohmann::json::exception& )
115 catch( std::exception& )
159 wxFFileInputStream in( aFileName );
166 wxString::Format(
_(
"Unable to find a valid board in '%s'" ), aFileName ) );
171 const int innerStart = 21;
172 const int innerEnd = 52;
174 int maxLayer = innerStart;
175 std::map<PCB_LAYER_ID, wxString> layerNames;
177 for(
const wxString& layerLine : pcbDoc.
layers )
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" );
187 if( layerId >= innerStart && layerId <= innerEnd && enabled )
188 maxLayer = layerId + 1;
190 layerNames[parser.
LayerToKi( parts[0] )] = layerName;
195 for(
auto& [klayer,
name] : layerNames )
199 std::shared_ptr<NETCLASS> defNetclass = bds.
m_NetSettings->m_DefaultNetClass;
203 std::map<wxString, nlohmann::json>& rules = *pcbDoc.
DRCRULE;
205 if(
auto defRules =
get_opt( rules,
"Default" ) )
209 key = wxS(
"trackWidth" );
210 if( defRules->find( key ) != defRules->end() && defRules->at( key ).is_number() )
212 double val = parser.
ScaleSize( defRules->at( key ) );
213 defNetclass->SetTrackWidth( val );
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 );
223 key = wxS(
"viaHoleD" );
224 if( defRules->find( key ) != defRules->end() && defRules->at( key ).is_number() )
226 double val = parser.
ScaleSize( defRules->at( key ) );
228 defNetclass->SetViaDrill( val );
231 key = wxS(
"viaHoleDiameter" );
232 if( defRules->find( key ) != defRules->end() && defRules->at( key ).is_number() )
234 double val = parser.
ScaleSize( defRules->at( key ) );
235 defNetclass->SetViaDiameter( val );
253 offset.
x =
KiROUND( offset.
x / alignGrid ) * alignGrid;
254 offset.
y =
KiROUND( offset.
y / alignGrid ) * alignGrid;
261 catch( nlohmann::json::exception& e )
264 wxString::Format(
_(
"Error loading board '%s': %s" ), aFileName, e.what() ) );
266 catch( std::exception& e )
269 wxString::Format(
_(
"Error loading board '%s': %s" ), aFileName, e.what() ) );
283 const wxString& aLibraryPath,
bool aBestEfforts,
288 wxFFileInputStream in( aLibraryPath );
294 THROW_IO_ERROR( wxString::Format(
_(
"Unable to find valid footprints in '%s'" ),
301 for( wxString shap : doc.
shape )
303 shap.Replace( wxS(
"#@$" ),
"\n" );
304 wxArrayString parts = wxSplit( shap,
'\n',
'\0' );
306 if( parts.size() < 1 )
309 wxArrayString paramsRoot = wxSplit( parts[0],
'~',
'\0' );
311 if( paramsRoot.size() < 1 )
314 wxString rootType = paramsRoot[0];
316 if( rootType == wxS(
"LIB" ) )
318 if( paramsRoot.size() < 4 )
321 wxString packageName = wxString::Format( wxS(
"Unknown_%s_%s" ), paramsRoot[1],
324 wxArrayString paramParts = wxSplit( paramsRoot[3],
'`',
'\0' );
326 std::map<wxString, wxString> paramMap;
328 for(
int i = 1; i < paramParts.size(); i += 2 )
330 wxString key = paramParts[i - 1];
331 wxString value = paramParts[i];
333 if( key == wxS(
"package" ) )
336 paramMap[key] = value;
339 aFootprintNames.Add( packageName );
347 wxString packageName = wxString::Format( wxS(
"Unknown_%s" ),
348 pcbDoc.
uuid.value_or( wxS(
"Unknown" ) ) );
350 std::optional<std::map<wxString, wxString>> c_para;
359 packageName =
get_def( *c_para, wxS(
"package" ), packageName );
362 aFootprintNames.Add( packageName );
365 catch( nlohmann::json::exception& e )
367 THROW_IO_ERROR( wxString::Format(
_(
"Error enumerating footprints in library '%s': %s" ),
368 aLibraryPath, e.what() ) );
370 catch( std::exception& e )
372 THROW_IO_ERROR( wxString::Format(
_(
"Error enumerating footprints in library '%s': %s" ),
373 aLibraryPath, e.what() ) );
379 const wxString& aFootprintName,
bool aKeepUUID,
388 wxFFileInputStream in( aLibraryPath );
394 THROW_IO_ERROR( wxString::Format(
_(
"Unable to find valid footprints in '%s'" ),
401 for( wxString shap : doc.
shape )
403 if( !shap.Contains( wxS(
"LIB" ) ) )
406 shap.Replace( wxS(
"#@$" ),
"\n" );
407 wxArrayString parts = wxSplit( shap,
'\n',
'\0' );
409 if( parts.size() < 1 )
412 wxArrayString paramsRoot = wxSplit( parts[0],
'~',
'\0' );
414 if( paramsRoot.size() < 1 )
417 wxString rootType = paramsRoot[0];
419 if( rootType == wxS(
"LIB" ) )
421 if( paramsRoot.size() < 4 )
425 parser.
Convert( paramsRoot[2] ) );
427 wxString packageName = wxString::Format( wxS(
"Unknown_%s_%s" ), paramsRoot[1],
430 wxArrayString paramParts = wxSplit( paramsRoot[3],
'`',
'\0' );
432 std::map<wxString, wxString> paramMap;
434 for(
int i = 1; i < paramParts.size(); i += 2 )
436 wxString key = paramParts[i - 1];
437 wxString value = paramParts[i];
439 if( key == wxS(
"package" ) )
442 paramMap[key] = value;
446 if( !paramsRoot[4].IsEmpty() )
451 if( !paramsRoot[7].IsEmpty() )
452 layer = parser.
Convert( paramsRoot[7] );
454 if( packageName == aFootprintName )
484 wxString packageName = wxString::Format( wxS(
"Unknown_%s" ),
485 pcbDoc.
uuid.value_or( wxS(
"Unknown" ) ) );
487 std::optional<std::map<wxString, wxString>> c_para;
496 packageName =
get_def( *c_para, wxS(
"package" ), packageName );
498 if( packageName != aFootprintName )
523 catch( nlohmann::json::exception& e )
525 THROW_IO_ERROR( wxString::Format(
_(
"Error reading footprint '%s' from library '%s': %s" ),
526 aFootprintName, aLibraryPath, e.what() ) );
528 catch( std::exception& e )
530 THROW_IO_ERROR( wxString::Format(
_(
"Error reading footprint '%s' from library '%s': %s" ),
531 aFootprintName, aLibraryPath, e.what() ) );
540 std::vector<FOOTPRINT*> result;
544 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)
virtual void SetVisible(bool aVisible)
virtual void SetTextAngle(const EDA_ANGLE &aAngle)
PROGRESS_REPORTER * m_progressReporter
Progress reporter to track the progress of the operation, may be nullptr.
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.
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.
BOARD * m_board
The board BOARD being worked on, no ownership here.
const STRING_UTF8_MAP * m_props
Properties passed via Save() or Load(), no ownership, may be NULL.
virtual bool CanReadBoard(const wxString &aFileName) const
Checks if this PCB_IO can read the specified board file.
virtual void SetPosition(const VECTOR2I &aPos) override
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
std::optional< std::map< wxString, wxString > > c_para
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".