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() )
77 else if( aName.Lower().EndsWith( wxS(
".zip" ) ) )
79 std::shared_ptr<wxZipEntry> entry;
80 wxZipInputStream
zip( aStream );
85 while( entry.reset(
zip.GetNextEntry() ), entry.get() != NULL )
87 wxString
name = entry->GetName();
105 wxFFileInputStream in( aFileName );
111 catch( nlohmann::json::exception& )
114 catch( std::exception& )
135 const std::map<std::string, UTF8>* aProperties,
PROJECT* aProject )
157 wxFFileInputStream in( aFileName );
166 const int innerStart = 21;
167 const int innerEnd = 52;
169 int maxLayer = innerStart;
170 std::map<PCB_LAYER_ID, wxString> layerNames;
172 for(
const wxString& layerLine : pcbDoc.
layers )
174 wxArrayString parts = wxSplit( layerLine,
'~',
'\0' );
175 int layerId = wxAtoi( parts[0] );
176 wxString layerName = parts[1];
177 wxString layerColor = parts[2];
178 wxString visible = parts[3];
179 wxString active = parts[4];
180 bool enabled = parts[5] != wxS(
"false" );
182 if( layerId >= innerStart && layerId <= innerEnd && enabled )
183 maxLayer = layerId + 1;
185 layerNames[parser.
LayerToKi( parts[0] )] = layerName;
188 m_board->SetCopperLayerCount( 2 + maxLayer - innerStart );
190 for(
auto& [klayer,
name] : layerNames )
195 bool importedNetclassRules =
false;
200 std::map<wxString, nlohmann::json>& rules = *pcbDoc.
DRCRULE;
202 if(
auto defRules =
get_opt( rules,
"Default" ) )
206 key = wxS(
"trackWidth" );
207 if( defRules->find( key ) != defRules->end() && defRules->at( key ).is_number() )
209 double val = parser.
ScaleSize( defRules->at( key ) );
210 defNetclass->SetTrackWidth( val );
211 importedNetclassRules =
true;
214 key = wxS(
"clearance" );
215 if( defRules->find( key ) != defRules->end() && defRules->at( key ).is_number() )
217 double val = parser.
ScaleSize( defRules->at( key ) );
218 defNetclass->SetClearance( val );
219 importedNetclassRules =
true;
222 key = wxS(
"viaHoleD" );
223 if( defRules->find( key ) != defRules->end() && defRules->at( key ).is_number() )
225 double val = parser.
ScaleSize( defRules->at( key ) );
227 defNetclass->SetViaDrill( val );
228 importedNetclassRules =
true;
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 );
236 importedNetclassRules =
true;
240 if( importedNetclassRules )
241 m_board->m_LegacyNetclassesLoaded =
true;
248 BOX2I outlineBbox =
m_board->ComputeBoundingBox(
true,
true );
257 offset.
x =
KiROUND( offset.
x / alignGrid ) * alignGrid;
258 offset.
y =
KiROUND( offset.
y / alignGrid ) * alignGrid;
263 catch( nlohmann::json::exception& e )
265 THROW_IO_ERRORF(
_(
"Error loading board '%s': %s" ), aFileName, e.what() );
267 catch( std::exception& e )
269 THROW_IO_ERRORF(
_(
"Error loading board '%s': %s" ), aFileName, e.what() );
281 const wxString& aLibraryPath,
bool aBestEfforts,
282 const std::map<std::string, UTF8>* aProperties )
286 wxFFileInputStream in( aLibraryPath );
291 THROW_IO_ERRORF(
_(
"Unable to find valid footprints in '%s'" ), aLibraryPath );
296 for( wxString shap : doc.
shape )
298 shap.Replace( wxS(
"#@$" ),
"\n" );
299 wxArrayString parts = wxSplit( shap,
'\n',
'\0' );
301 if( parts.size() < 1 )
304 wxArrayString paramsRoot = wxSplit( parts[0],
'~',
'\0' );
306 if( paramsRoot.size() < 1 )
309 wxString rootType = paramsRoot[0];
311 if( rootType == wxS(
"LIB" ) )
313 if( paramsRoot.size() < 4 )
316 wxString packageName = wxString::Format( wxS(
"Unknown_%s_%s" ), paramsRoot[1],
319 wxArrayString paramParts = wxSplit( paramsRoot[3],
'`',
'\0' );
321 std::map<wxString, wxString> paramMap;
323 for(
int i = 1; i < (int) paramParts.size(); i += 2 )
325 wxString key = paramParts[i - 1];
326 wxString value = paramParts[i];
328 if( key == wxS(
"package" ) )
331 paramMap[key] = value;
334 aFootprintNames.Add( packageName );
342 wxString packageName = wxString::Format( wxS(
"Unknown_%s" ),
343 pcbDoc.
uuid.value_or( wxS(
"Unknown" ) ) );
345 std::optional<std::map<wxString, wxString>> c_para;
353 packageName =
get_def( *c_para, wxS(
"package" ), packageName );
355 aFootprintNames.Add( packageName );
358 catch( nlohmann::json::exception& e )
360 THROW_IO_ERRORF(
_(
"Error enumerating footprints in library '%s': %s" ), aLibraryPath, e.what() );
362 catch( std::exception& e )
364 THROW_IO_ERRORF(
_(
"Error enumerating footprints in library '%s': %s" ), aLibraryPath, e.what() );
371 const std::map<std::string, UTF8>* aProperties )
382 wxFFileInputStream in( aLibraryPath );
387 THROW_IO_ERRORF(
_(
"Unable to find valid footprints in '%s'" ), aLibraryPath );
392 for( wxString shap : doc.
shape )
394 if( !shap.Contains( wxS(
"LIB" ) ) )
397 shap.Replace( wxS(
"#@$" ),
"\n" );
398 wxArrayString parts = wxSplit( shap,
'\n',
'\0' );
400 if( parts.size() < 1 )
403 wxArrayString paramsRoot = wxSplit( parts[0],
'~',
'\0' );
405 if( paramsRoot.size() < 1 )
408 wxString rootType = paramsRoot[0];
410 if( rootType == wxS(
"LIB" ) )
412 if( paramsRoot.size() < 4 )
416 parser.
Convert( paramsRoot[2] ) );
418 wxString packageName = wxString::Format( wxS(
"Unknown_%s_%s" ), paramsRoot[1],
421 wxArrayString paramParts = wxSplit( paramsRoot[3],
'`',
'\0' );
423 std::map<wxString, wxString> paramMap;
425 for(
int i = 1; i < (int) paramParts.size(); i += 2 )
427 wxString key = paramParts[i - 1];
428 wxString value = paramParts[i];
430 if( key == wxS(
"package" ) )
433 paramMap[key] = value;
437 if( !paramsRoot[4].IsEmpty() )
442 if( !paramsRoot[7].IsEmpty() )
443 layer = parser.
Convert( paramsRoot[7] );
445 if( packageName == aFootprintName )
455 footprint->Reference().SetPosition(
VECTOR2I() );
456 footprint->Reference().SetTextAngle(
ANGLE_0 );
457 footprint->Reference().SetVisible(
true );
459 footprint->Value().SetPosition(
VECTOR2I() );
460 footprint->Value().SetTextAngle(
ANGLE_0 );
461 footprint->Value().SetVisible(
true );
463 footprint->AutoPositionFields();
474 wxString packageName = wxString::Format( wxS(
"Unknown_%s" ),
475 pcbDoc.
uuid.value_or( wxS(
"Unknown" ) ) );
477 std::optional<std::map<wxString, wxString>> c_para;
486 packageName =
get_def( *c_para, wxS(
"package" ), packageName );
488 if( packageName != aFootprintName )
499 footprint->Reference().SetPosition(
VECTOR2I() );
500 footprint->Reference().SetTextAngle(
ANGLE_0 );
501 footprint->Reference().SetVisible(
true );
503 footprint->Value().SetPosition(
VECTOR2I() );
504 footprint->Value().SetTextAngle(
ANGLE_0 );
505 footprint->Value().SetVisible(
true );
507 footprint->AutoPositionFields();
513 catch( nlohmann::json::exception& e )
520 catch( std::exception& e )
534 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)
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