36#include <wx/txtstrm.h>
37#include <wx/wfstream.h>
38#include <wx/mstream.h>
39#include <wx/zipstrm.h>
50 std::map<wxString, EASYEDAPRO::BLOB>
m_Blobs;
51 std::map<wxString, std::multimap<wxString, EASYEDAPRO::POURED>>
m_Poured;
69 if( aFileName.Lower().EndsWith( wxS(
".epro" ) ) )
73 else if( aFileName.Lower().EndsWith( wxS(
".zip" ) ) )
75 std::shared_ptr<wxZipEntry> entry;
76 wxFFileInputStream in( aFileName );
77 wxZipInputStream
zip( in );
82 while( entry.reset(
zip.GetNextEntry() ), entry.get() != NULL )
84 wxString
name = entry->GetName();
86 if(
name == wxS(
"project.json" ) )
98 const std::map<std::string, UTF8>* aProperties,
PROJECT* aProject )
106 m_board->SetFileName( aFileName );
121 wxFileName fname( aFileName );
124 if( fname.GetExt() == wxS(
"epro" ) || fname.GetExt() == wxS(
"zip" ) )
132 pcbToLoad = wxString::FromUTF8(
m_props->at(
"pcb_id" ) );
136 std::map<wxString, wxString> prjPcbNames =
project.at(
"pcbs" );
138 if( prjPcbNames.size() == 1 )
140 pcbToLoad = prjPcbNames.begin()->first;
147 if( chosen.size() > 0 )
148 pcbToLoad = chosen[0].PCBId;
152 if( pcbToLoad.empty() )
160 auto cb = [&](
const wxString&
name,
const wxString& pcbUuid, wxInputStream&
zip ) ->
bool
162 if( !
name.EndsWith( wxS(
".epcb" ) ) )
165 if( pcbUuid != pcbToLoad )
168 std::vector<nlohmann::json>* pcbLines =
nullptr;
170 std::vector<std::vector<nlohmann::json>> lineBlocks =
173 if( lineBlocks.empty() )
176 if( lineBlocks.size() > 1 )
178 for( std::vector<nlohmann::json>& block : lineBlocks )
181 nlohmann::json headData;
183 for(
const nlohmann::json& line : block )
185 if( line.size() < 2 )
188 if( !line.at( 0 ).is_string() )
191 wxString lineType = line.at( 0 ).get<wxString>();
193 if( lineType == wxS(
"DOCTYPE" ) )
195 if( !line.at( 1 ).is_string() )
198 docType = line.at( 1 ).get<wxString>();
200 else if( lineType == wxS(
"HEAD" ) )
202 if( !line.at( 1 ).is_object() )
205 headData = line.at( 1 );
209 if( docType == wxS(
"FOOTPRINT" ) )
211 wxString fpUuid = headData.at(
"uuid" );
212 wxString fpTitle = headData.at(
"title" );
224 else if( docType == wxS(
"PCB" ) )
231 if( pcbLines ==
nullptr )
232 pcbLines = &lineBlocks[0];
234 wxString boardKey = pcbUuid + wxS(
"_0" );
235 wxScopedCharBuffer boardKeyBuffer = boardKey.ToUTF8();
236 wxString boardPouredKey = wxBase64Encode( boardKeyBuffer.data(), boardKeyBuffer.length() );
238 const std::multimap<wxString, EASYEDAPRO::POURED>& boardPoured =
261 const wxString& aLibraryPath,
bool aBestEfforts,
262 const std::map<std::string, UTF8>* aProperties )
264 wxFileName fname( aLibraryPath );
266 if( fname.GetExt() == wxS(
"efoo" ) )
268 wxFFileInputStream ffis( aLibraryPath );
269 wxTextInputStream txt( ffis, wxS(
" " ), wxConvUTF8 );
271 while( ffis.CanRead() )
273 wxString line = txt.ReadLine();
275 if( !line.Contains( wxS(
"ATTR" ) ) )
278 nlohmann::json js = nlohmann::json::parse( line );
279 if( js.at( 0 ) ==
"ATTR" && js.at( 7 ) ==
"Footprint" )
281 aFootprintNames.Add( js.at( 8 ).get<wxString>() );
285 else if( fname.GetExt() == wxS(
"elibz" ) || fname.GetExt() == wxS(
"epro" )
286 || fname.GetExt() == wxS(
"zip" ) )
289 std::map<wxString, nlohmann::json> footprintMap =
project.at(
"footprints" );
291 for(
auto& [key, value] : footprintMap )
295 if( value.contains(
"display_title" ) )
296 title = value.at(
"display_title" ).get<wxString>();
298 title = value.at(
"title" ).get<wxString>();
300 aFootprintNames.Add( title );
307 const nlohmann::json& aProject )
315 wxFileName fname( aProjectPath );
318 std::map<wxString, std::unique_ptr<FOOTPRINT>>
result;
320 auto cb = [&](
const wxString&
name,
const wxString& baseName, wxInputStream&
zip ) ->
bool
322 if( !
name.EndsWith( wxS(
".efoo" ) ) && !
name.EndsWith( wxS(
".eblob" ) )
323 && !
name.EndsWith( wxS(
".ecop" ) ) )
330 if(
name.EndsWith( wxS(
".efoo" ) ) )
332 nlohmann::json fpData = aProject.at(
"footprints" ).at( baseName );
333 wxString fpTitle = fpData.at(
"title" );
345 else if(
name.EndsWith( wxS(
".eblob" ) ) )
347 for(
const nlohmann::json& line : lines )
349 if( line.at( 0 ) ==
"BLOB" )
358 for(
const nlohmann::json& line : lines )
360 if( line.at( 0 ) ==
"POURED" )
362 if( !line.at( 2 ).is_string() )
377 const wxString& aFootprintName,
bool aKeepUUID,
378 const std::map<std::string, UTF8>* aProperties )
386 wxFileName libFname( aLibraryPath );
388 if( libFname.GetExt() == wxS(
"efoo" ) )
390 wxFFileInputStream ffis( aLibraryPath );
391 wxTextInputStream txt( ffis, wxS(
" " ), wxConvUTF8 );
395 for(
const nlohmann::json& js : lines )
397 if( js.at( 0 ) ==
"ATTR" )
401 if( attr.
key == wxS(
"Footprint" ) && attr.
value != aFootprintName )
406 footprint = parser.
ParseFootprint( nlohmann::json(), wxEmptyString, lines );
410 THROW_IO_ERROR( wxString::Format(
_(
"Cannot load footprint '%s' from '%s'" ),
411 aFootprintName, aLibraryPath ) );
422 else if( libFname.GetExt() == wxS(
"elibz" ) || libFname.GetExt() == wxS(
"epro" )
423 || libFname.GetExt() == wxS(
"zip" ) )
429 std::map<wxString, nlohmann::json> footprintMap =
project.at(
"footprints" );
430 for(
auto& [uuid, data] : footprintMap )
434 if( data.contains(
"display_title" ) )
435 title = data.at(
"display_title" ).get<wxString>();
437 title = data.at(
"title" ).get<wxString>();
439 if( title == aFootprintName )
448 THROW_IO_ERROR( wxString::Format(
_(
"Footprint '%s' not found in project '%s'" ),
449 aFootprintName, aLibraryPath ) );
452 auto cb = [&](
const wxString&
name,
const wxString& baseName, wxInputStream&
zip ) ->
bool
454 if( !
name.EndsWith( wxS(
".efoo" ) ) )
457 if( baseName != fpUuid )
466 THROW_IO_ERROR( wxString::Format(
_(
"Cannot load footprint '%s' from '%s'" ),
467 aFootprintName, aLibraryPath ) );
489 std::vector<FOOTPRINT*>
result;
494 for(
auto& [fpUuid, footprint] :
m_projectData->m_Footprints )
Information pertinent to a Pcbnew printed circuit board.
virtual void SetVisible(bool aVisible)
virtual void SetText(const wxString &aText)
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.
A logical library item identifier and consists of various portions much like a URI.
static LOAD_INFO_REPORTER & GetInstance()
void ParseBoard(BOARD *aBoard, const nlohmann::json &aProject, std::map< wxString, std::unique_ptr< FOOTPRINT > > &aFootprintMap, const std::map< wxString, EASYEDAPRO::BLOB > &aBlobMap, const std::multimap< wxString, EASYEDAPRO::POURED > &aPouredMap, const std::vector< nlohmann::json > &aLines, const wxString &aFpLibName)
FOOTPRINT * ParseFootprint(const nlohmann::json &aProject, const wxString &aFpUuid, const std::vector< nlohmann::json > &aLines)
void LoadAllDataFromProject(const wxString &aLibraryPath, const nlohmann::json &aProject)
std::vector< FOOTPRINT * > GetImportedCachedLibraryFootprints() override
Return a container with the cached library footprints generated in the last call to Load.
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 CanReadBoard(const wxString &aFileName) const override
Checks if this PCB_IO can read the specified board file.
long long GetLibraryTimestamp(const wxString &aLibraryPath) const override
Generate a timestamp representing all the files in the library (including the library 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.
BOARD * m_board
The board BOARD being worked on, no ownership here.
PCB_IO(const wxString &aName)
const std::map< std::string, UTF8 > * m_props
Properties passed via Save() or Load(), no ownership, may be NULL.
CHOOSE_PROJECT_HANDLER m_choose_project_handler
Callback to choose projects to import.
Container for project specific data.
#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="")
LIB_ID ToKiCadLibID(const wxString &aLibName, const wxString &aLibReference)
void IterateZipFiles(const wxString &aFileName, std::function< bool(const wxString &, const wxString &, wxInputStream &)> aCallback)
std::vector< nlohmann::json > ParseJsonLines(wxInputStream &aInput, const wxString &aSource)
static const bool IMPORT_POURED_ECOP
std::vector< std::vector< nlohmann::json > > ParseJsonLinesWithSeparation(wxInputStream &aInput, const wxString &aSource)
Multiple document types (e.g.
std::vector< IMPORT_PROJECT_DESC > ProjectToSelectorDialog(const nlohmann::json &aProject, bool aPcbOnly=false, bool aSchOnly=false)
nlohmann::json ReadProjectOrDeviceFile(const wxString &aZipFileName)
wxString ShortenLibName(wxString aProjectName)
std::map< wxString, std::multimap< wxString, EASYEDAPRO::POURED > > m_Poured
std::map< wxString, EASYEDAPRO::BLOB > m_Blobs
std::map< wxString, std::unique_ptr< FOOTPRINT > > m_Footprints
wxString result
Test unit parsing edge cases and error handling.